]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
38e1e7b2dd4f36a115c4ca2a73ba5b0d8b60d326
[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 #include <sys/stat.h>
19
20 #include <atalk/unicode.h>
21 #include <atalk/bstrlib.h>
22 #include <atalk/cnid.h>
23
24 /* exit error codes */
25 #define EXITERR_CLNT 1  /* client related error */
26 #define EXITERR_CONF 2  /* error in config files/cmd line parameters */
27 #define EXITERR_SYS  3  /* local system error */
28 #define EXITERR_CLOSED 4  /* connection was immediately closed after TCP handshake */
29
30 /* Print a SBT and exit */
31 #define AFP_PANIC(why) \
32     do {                                            \
33         netatalk_panic(why);                        \
34         abort();                                    \
35     } while(0);
36
37 /* LOG assert errors */
38 #ifndef NDEBUG
39 #define AFP_ASSERT(b) \
40     do {                                                                \
41         if (!(b)) {                                                     \
42             AFP_PANIC(#b);                                              \
43         } \
44     } while(0);
45 #else
46 #define AFP_ASSERT(b)
47 #endif /* NDEBUG */
48
49 #ifndef MIN
50 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
51 #endif
52
53 #ifndef MAX
54 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
55 #endif
56
57 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
58 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
59 #define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
60 #ifndef MAX
61 #define MAX(a,b) ((a) > (b) ? a : b)
62 #endif
63 #ifndef MIN
64 #define MIN(a,b) ((a) < (b) ? a : b)
65 #endif
66
67 #ifdef WORDS_BIGENDIAN
68 #define hton64(x)       (x)
69 #define ntoh64(x)       (x)
70 #else
71 #define hton64(x)       ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
72                          (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
73 #define ntoh64(x)       (hton64(x))
74 #endif
75
76 #ifdef WITH_SENDFILE
77 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
78 #endif
79
80 extern const int _diacasemap[], _dialowermap[];
81
82 extern char **getifacelist(void);
83 extern void freeifacelist(char **);
84
85 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
86 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
87 extern void bprint        (char *, int);
88 extern int strdiacasecmp  (const char *, const char *);
89 extern int strndiacasecmp (const char *, const char *, size_t);
90 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
91 extern int check_lockfile (const char *program, const char *pidfile);
92 extern int create_lockfile(const char *program, const char *pidfile);
93 extern void fault_setup   (void (*fn)(void *));
94 extern void netatalk_panic(const char *why);
95 #define server_unlock(x)  (unlink(x))
96
97 #ifndef HAVE_DLFCN_H
98 extern void *mod_open    (const char *);
99 extern void *mod_symbol  (void *, const char *);
100 extern void mod_close    (void *);
101 #define mod_error()      ""
102 #else /* ! HAVE_DLFCN_H */
103 #include <dlfcn.h>
104
105 #ifndef RTLD_NOW
106 #define RTLD_NOW 1
107 #endif /* ! RTLD_NOW */
108
109 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
110  * OpenBSD currently does not use the second arg for dlopen(). For
111  * future compatibility we define DL_LAZY */
112 #ifdef __NetBSD__
113 #define mod_open(a)      dlopen(a, RTLD_LAZY)
114 #elif defined(__OpenBSD__)
115 #define mod_open(a)      dlopen(a, DL_LAZY)
116 #else /* ! __NetBSD__ && ! __OpenBSD__ */
117 #define mod_open(a)      dlopen(a, RTLD_NOW)
118 #endif /* __NetBSD__ */
119
120 #ifndef DLSYM_PREPEND_UNDERSCORE
121 #define mod_symbol(a, b) dlsym(a, b)
122 #else /* ! DLSYM_PREPEND_UNDERSCORE */
123 extern void *mod_symbol  (void *, const char *);
124 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
125 #define mod_error()      dlerror()
126 #define mod_close(a)     dlclose(a)
127 #endif /* ! HAVE_DLFCN_H */
128
129 /******************************************************************
130  * locking.c
131  ******************************************************************/
132
133 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
134 #define read_lock(fd, offset, whence, len) \
135     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
136 #define write_lock(fd, offset, whence, len) \
137     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
138 #define unlock(fd, offset, whence, len) \
139     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
140
141 /******************************************************************
142  * socket.c
143  ******************************************************************/
144
145 extern int setnonblock(int fd, int cmd);
146 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
147 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
148 extern const char *getip_string(const struct sockaddr *sa);
149 extern unsigned int getip_port(const struct sockaddr *sa);
150 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
151 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
152 extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
153
154 /* Structures and functions dealing with dynamic pollfd arrays */
155 enum fdtype {IPC_FD, LISTEN_FD};
156 struct polldata {
157     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
158     void *data;         /* pointer to AFPconfig for listening socket and *
159                          * pointer to afp_child_t for IPC fd             */
160 };
161
162 extern void fdset_add_fd(int maxconns,
163                          struct pollfd **fdsetp,
164                          struct polldata **polldatap,
165                          int *fdset_usedp,
166                          int *fdset_sizep,
167                          int fd,
168                          enum fdtype fdtype,
169                          void *data);
170 extern void fdset_del_fd(struct pollfd **fdsetp,
171                          struct polldata **polldatap,
172                          int *fdset_usedp,
173                          int *fdset_sizep,
174                          int fd);
175 extern int send_fd(int socket, int fd);
176 extern int recv_fd(int fd, int nonblocking);
177
178 /******************************************************************
179  * unix.c
180  *****************************************************************/
181
182 extern const char *getcwdpath(void);
183 extern const char *fullpathname(const char *);
184 extern char *stripped_slashes_basename(char *p);
185 extern void randombytes(void *buf, int n);
186 extern int daemonize(int nochdir, int noclose);
187 extern int run_cmd(const char *cmd, char **cmd_argv);
188 extern char *realpath_safe(const char *path);
189 extern const char *basename_safe(const char *path);
190 extern char *strtok_quote (char *s, const char *delim);
191
192 extern int ochdir(const char *dir, int options);
193 extern int ostat(const char *path, struct stat *buf, int options);
194 extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
195 extern int ochown(const char *path, uid_t owner, gid_t group, int options);
196 extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
197
198 /******************************************************************
199  * cnid.c
200  *****************************************************************/
201
202 extern bstring rel_path_in_vol(const char *path, const char *volpath);
203 extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did);
204
205 /******************************************************************
206  * cnid.c
207  *****************************************************************/
208
209 extern void initline   (int, char *);
210 extern int  parseline  (int, char *);
211
212 #endif  /* _ATALK_UTIL_H */