]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Support for using $u username variable in AFP volume definitions
[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 #ifndef SAFE_FREE
77 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
78 #endif
79
80 #ifdef WITH_SENDFILE
81 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
82 #endif
83
84 extern const int _diacasemap[], _dialowermap[];
85
86 extern char **getifacelist(void);
87 extern void freeifacelist(char **);
88
89 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
90 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
91 extern void bprint        (char *, int);
92 extern int strdiacasecmp  (const char *, const char *);
93 extern int strndiacasecmp (const char *, const char *, size_t);
94 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
95 extern int check_lockfile (const char *program, const char *pidfile);
96 extern int create_lockfile(const char *program, const char *pidfile);
97 extern void fault_setup   (void (*fn)(void *));
98 extern void netatalk_panic(const char *why);
99 #define server_unlock(x)  (unlink(x))
100
101 #ifndef HAVE_DLFCN_H
102 extern void *mod_open    (const char *);
103 extern void *mod_symbol  (void *, const char *);
104 extern void mod_close    (void *);
105 #define mod_error()      ""
106 #else /* ! HAVE_DLFCN_H */
107 #include <dlfcn.h>
108
109 #ifndef RTLD_NOW
110 #define RTLD_NOW 1
111 #endif /* ! RTLD_NOW */
112
113 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
114  * OpenBSD currently does not use the second arg for dlopen(). For
115  * future compatibility we define DL_LAZY */
116 #ifdef __NetBSD__
117 #define mod_open(a)      dlopen(a, RTLD_LAZY)
118 #elif defined(__OpenBSD__)
119 #define mod_open(a)      dlopen(a, DL_LAZY)
120 #else /* ! __NetBSD__ && ! __OpenBSD__ */
121 #define mod_open(a)      dlopen(a, RTLD_NOW)
122 #endif /* __NetBSD__ */
123
124 #ifndef DLSYM_PREPEND_UNDERSCORE
125 #define mod_symbol(a, b) dlsym(a, b)
126 #else /* ! DLSYM_PREPEND_UNDERSCORE */
127 extern void *mod_symbol  (void *, const char *);
128 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
129 #define mod_error()      dlerror()
130 #define mod_close(a)     dlclose(a)
131 #endif /* ! HAVE_DLFCN_H */
132
133 /******************************************************************
134  * locking.c
135  ******************************************************************/
136
137 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
138 #define read_lock(fd, offset, whence, len) \
139     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
140 #define write_lock(fd, offset, whence, len) \
141     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
142 #define unlock(fd, offset, whence, len) \
143     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
144
145 /******************************************************************
146  * socket.c
147  ******************************************************************/
148
149 extern int setnonblock(int fd, int cmd);
150 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
151 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
152 extern const char *getip_string(const struct sockaddr *sa);
153 extern unsigned int getip_port(const struct sockaddr *sa);
154 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
155 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
156 extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
157
158 /* Structures and functions dealing with dynamic pollfd arrays */
159 enum fdtype {IPC_FD, LISTEN_FD};
160 struct polldata {
161     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
162     void *data;         /* pointer to AFPconfig for listening socket and *
163                          * pointer to afp_child_t for IPC fd             */
164 };
165
166 extern void fdset_add_fd(int maxconns,
167                          struct pollfd **fdsetp,
168                          struct polldata **polldatap,
169                          int *fdset_usedp,
170                          int *fdset_sizep,
171                          int fd,
172                          enum fdtype fdtype,
173                          void *data);
174 extern void fdset_del_fd(struct pollfd **fdsetp,
175                          struct polldata **polldatap,
176                          int *fdset_usedp,
177                          int *fdset_sizep,
178                          int fd);
179 extern int send_fd(int socket, int fd);
180 extern int recv_fd(int fd, int nonblocking);
181
182 /******************************************************************
183  * unix.c
184  *****************************************************************/
185
186 extern const char *getcwdpath(void);
187 extern const char *fullpathname(const char *);
188 extern char *stripped_slashes_basename(char *p);
189 extern void randombytes(void *buf, int n);
190 extern int daemonize(int nochdir, int noclose);
191 extern int run_cmd(const char *cmd, char **cmd_argv);
192 extern char *realpath_safe(const char *path);
193 extern const char *basename_safe(const char *path);
194 extern char *strtok_quote (char *s, const char *delim);
195
196 extern int ochdir(const char *dir, int options);
197 extern int ostat(const char *path, struct stat *buf, int options);
198 extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
199 extern int ochown(const char *path, uid_t owner, gid_t group, int options);
200 extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
201
202 /******************************************************************
203  * cnid.c
204  *****************************************************************/
205
206 extern bstring rel_path_in_vol(const char *path, const char *volpath);
207 extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did);
208
209 /******************************************************************
210  * cnid.c
211  *****************************************************************/
212
213 extern void initline   (int, char *);
214 extern int  parseline  (int, char *);
215
216 #endif  /* _ATALK_UTIL_H */