]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Hangs in Netatalk which causes it to stop responding to connections
[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 /******************************************************************
135  * locking.c
136  ******************************************************************/
137
138 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
139 #define read_lock(fd, offset, whence, len) \
140     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
141 #define write_lock(fd, offset, whence, len) \
142     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
143 #define unlock(fd, offset, whence, len) \
144     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
145
146 /******************************************************************
147  * socket.c
148  ******************************************************************/
149
150 extern int setnonblock(int fd, int cmd);
151 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
152 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
153 extern const char *getip_string(const struct sockaddr *sa);
154 extern unsigned int getip_port(const struct sockaddr *sa);
155 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
156 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
157 extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
158
159 /* Structures and functions dealing with dynamic pollfd arrays */
160
161 enum asev_fdtype {IPC_FD, LISTEN_FD};
162
163 /**
164  * atalk socket event data
165  **/
166 struct asev_data {
167     enum asev_fdtype fdtype;  /* IPC fd or listening socket fd                 */
168     void            *private; /* pointer to AFPconfig for listening socket and *
169                                * pointer to afp_child_t for IPC fd             */
170 };
171
172 /**
173  * atalk socket event
174  **/
175 struct asev {
176     struct pollfd         *fdset; /* struct pollfd array for poll() */
177     struct asev_data      *data;  /* associated array of data       */
178     int                    max;
179     int                    used;
180 };
181
182 extern struct asev *asev_init(int max);
183 extern bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype, void *private);
184 extern bool asev_del_fd(struct asev *sev, int fd);
185
186 extern int send_fd(int socket, int fd);
187 extern int recv_fd(int fd, int nonblocking);
188
189 /******************************************************************
190  * unix.c
191  *****************************************************************/
192
193 extern const char *getcwdpath(void);
194 extern const char *fullpathname(const char *);
195 extern char *stripped_slashes_basename(char *p);
196 extern void randombytes(void *buf, int n);
197 extern int daemonize(int nochdir, int noclose);
198 extern int run_cmd(const char *cmd, char **cmd_argv);
199 extern char *realpath_safe(const char *path);
200 extern const char *basename_safe(const char *path);
201 extern char *strtok_quote (char *s, const char *delim);
202
203 extern int ochdir(const char *dir, int options);
204 extern int ostat(const char *path, struct stat *buf, int options);
205 extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
206 extern int ochown(const char *path, uid_t owner, gid_t group, int options);
207 extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
208
209 /******************************************************************
210  * cnid.c
211  *****************************************************************/
212
213 extern bstring rel_path_in_vol(const char *path, const char *volpath);
214 extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did);
215
216 /******************************************************************
217  * cnid.c
218  *****************************************************************/
219
220 extern void initline   (int, char *);
221 extern int  parseline  (int, char *);
222
223 #endif  /* _ATALK_UTIL_H */