]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Merge master
[netatalk.git] / include / atalk / util.h
1 /*!
2  * @file
3  * Netatalk utility functions
4  *
5  * Utility functions for these areas: \n
6  * * sockets \n
7  * * locking \n
8  * * misc UNIX function wrappers, eg for getcwd
9  */
10
11 #ifndef _ATALK_UTIL_H
12 #define _ATALK_UTIL_H 1
13
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17 #include <poll.h>
18
19 #include <atalk/unicode.h>
20 #include <atalk/bstrlib.h>
21
22 /* exit error codes */
23 #define EXITERR_CLNT 1  /* client related error */
24 #define EXITERR_CONF 2  /* error in config files/cmd line parameters */
25 #define EXITERR_SYS  3  /* local system error */
26
27 /* Print a SBT and exit */
28 #define AFP_PANIC(why) \
29     do {                                            \
30         netatalk_panic(why);                        \
31         abort();                                    \
32     } while(0);
33
34 /* LOG assert errors */
35 #ifndef NDEBUG
36 #define AFP_ASSERT(b) \
37     do {                                                                \
38         if (!(b)) {                                                     \
39             AFP_PANIC(#b);                                              \
40         } \
41     } while(0);
42 #else
43 #define AFP_ASSERT(b)
44 #endif /* NDEBUG */
45
46 #ifndef MIN
47 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
48 #endif
49
50 #ifndef MAX
51 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
52 #endif
53
54 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
55 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
56 #define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
57
58 #ifdef WITH_SENDFILE
59 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
60 #endif
61
62 extern const int _diacasemap[], _dialowermap[];
63
64 extern char **getifacelist(void);
65 extern void freeifacelist(char **);
66
67 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
68 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
69 extern void bprint        (char *, int);
70 extern int strdiacasecmp  (const char *, const char *);
71 extern int strndiacasecmp (const char *, const char *, size_t);
72 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
73 extern void fault_setup   (void (*fn)(void *));
74 extern void netatalk_panic(const char *why);
75 #define server_unlock(x)  (unlink(x))
76
77 #ifndef HAVE_DLFCN_H
78 extern void *mod_open    (const char *);
79 extern void *mod_symbol  (void *, const char *);
80 extern void mod_close    (void *);
81 #define mod_error()      ""
82 #else /* ! HAVE_DLFCN_H */
83 #include <dlfcn.h>
84
85 #ifndef RTLD_NOW
86 #define RTLD_NOW 1
87 #endif /* ! RTLD_NOW */
88
89 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
90  * OpenBSD currently does not use the second arg for dlopen(). For
91  * future compatibility we define DL_LAZY */
92 #ifdef __NetBSD__
93 #define mod_open(a)      dlopen(a, RTLD_LAZY)
94 #elif defined(__OpenBSD__)
95 #define mod_open(a)      dlopen(a, DL_LAZY)
96 #else /* ! __NetBSD__ && ! __OpenBSD__ */
97 #define mod_open(a)      dlopen(a, RTLD_NOW)
98 #endif /* __NetBSD__ */
99
100 #ifndef DLSYM_PREPEND_UNDERSCORE
101 #define mod_symbol(a, b) dlsym(a, b)
102 #else /* ! DLSYM_PREPEND_UNDERSCORE */
103 extern void *mod_symbol  (void *, const char *);
104 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
105 #define mod_error()      dlerror()
106 #define mod_close(a)     dlclose(a)
107 #endif /* ! HAVE_DLFCN_H */
108
109 /******************************************************************
110  * locking.c
111  ******************************************************************/
112
113 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
114 #define read_lock(fd, offset, whence, len) \
115     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
116 #define write_lock(fd, offset, whence, len) \
117     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
118 #define unlock(fd, offset, whence, len) \
119     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
120
121 /******************************************************************
122  * socket.c
123  ******************************************************************/
124
125 extern int setnonblock(int fd, int cmd);
126 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
127 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
128 extern const char *getip_string(const struct sockaddr *sa);
129 extern unsigned int getip_port(const struct sockaddr *sa);
130 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
131 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
132
133 /* Structures and functions dealing with dynamic pollfd arrays */
134 enum fdtype {IPC_FD, LISTEN_FD};
135 struct polldata {
136     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
137     void *data;         /* pointer to AFPconfig for listening socket and *
138                          * pointer to afp_child_t for IPC fd             */
139 };
140
141 extern void fdset_add_fd(struct pollfd **fdsetp,
142                          struct polldata **polldatap,
143                          int *fdset_usedp,
144                          int *fdset_sizep,
145                          int fd,
146                          enum fdtype fdtype,
147                          void *data);
148 extern void fdset_del_fd(struct pollfd **fdsetp,
149                          struct polldata **polldatap,
150                          int *fdset_usedp,
151                          int *fdset_sizep,
152                          int fd);
153 extern int send_fd(int socket, int fd);
154 extern int recv_fd(int fd, int nonblocking);
155
156 /******************************************************************
157  * unix.c
158  *****************************************************************/
159
160 extern const char *abspath(const char *name);
161 extern const char *getcwdpath(void);
162 extern char *stripped_slashes_basename(char *p);
163 extern int lchdir(const char *dir);
164 extern void randombytes(void *buf, int n);
165 #endif  /* _ATALK_UTIL_H */
166
167 /******************************************************************
168  * cnid.c
169  *****************************************************************/
170
171 extern bstring rel_path_in_vol(const char *path, const char *volpath);