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