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