]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Remove bdb env on exit
[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/cdefs.h>
15 #include <sys/types.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include <poll.h>
20 #include <netatalk/at.h>
21
22 #include <atalk/unicode.h>
23 #include <atalk/bstrlib.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
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 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
50 #ifndef MAX
51 #define MAX(a,b) ((a) > (b) ? a : b)
52 #endif
53 #ifndef MIN
54 #define MIN(a,b) ((a) < (b) ? a : b)
55 #endif
56
57 #if BYTE_ORDER == BIG_ENDIAN
58 #define hton64(x)       (x)
59 #define ntoh64(x)       (x)
60 #else /* BYTE_ORDER == BIG_ENDIAN */
61 #define hton64(x)       ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
62                          (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
63 #define ntoh64(x)       (hton64(x))
64 #endif /* BYTE_ORDER == BIG_ENDIAN */
65
66 #ifdef WITH_SENDFILE
67 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
68 #endif
69
70 extern const int _diacasemap[], _dialowermap[];
71
72 extern char **getifacelist(void);
73 extern void freeifacelist(char **);
74
75 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
76 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
77 #ifndef NO_DDP
78 extern int atalk_aton     (char *, struct at_addr *);
79 #endif
80 extern void bprint        (char *, int);
81 extern int strdiacasecmp  (const char *, const char *);
82 extern int strndiacasecmp (const char *, const char *, size_t);
83 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
84 extern int check_lockfile (const char *program, const char *pidfile);
85 extern int create_lockfile(const char *program, const char *pidfile);
86 extern void fault_setup   (void (*fn)(void *));
87 extern void netatalk_panic(const char *why);
88 #define server_unlock(x)  (unlink(x))
89
90 /* strlcpy and strlcat are used by pam modules */
91 #ifndef UAM_MODULE_EXPORT
92 #define UAM_MODULE_EXPORT 
93 #endif
94
95 #ifndef HAVE_STRLCPY
96 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
97 #endif
98  
99 #ifndef HAVE_STRLCAT
100 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
101 #endif
102
103 #ifndef HAVE_DLFCN_H
104 extern void *mod_open    (const char *);
105 extern void *mod_symbol  (void *, const char *);
106 extern void mod_close    (void *);
107 #define mod_error()      ""
108 #else /* ! HAVE_DLFCN_H */
109 #include <dlfcn.h>
110
111 #ifndef RTLD_NOW
112 #define RTLD_NOW 1
113 #endif /* ! RTLD_NOW */
114
115 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
116  * OpenBSD currently does not use the second arg for dlopen(). For
117  * future compatibility we define DL_LAZY */
118 #ifdef __NetBSD__
119 #define mod_open(a)      dlopen(a, RTLD_LAZY)
120 #elif defined(__OpenBSD__)
121 #define mod_open(a)      dlopen(a, DL_LAZY)
122 #else /* ! __NetBSD__ && ! __OpenBSD__ */
123 #define mod_open(a)      dlopen(a, RTLD_NOW)
124 #endif /* __NetBSD__ */
125
126 #ifndef DLSYM_PREPEND_UNDERSCORE
127 #define mod_symbol(a, b) dlsym(a, b)
128 #else /* ! DLSYM_PREPEND_UNDERSCORE */
129 extern void *mod_symbol  (void *, const char *);
130 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
131 #define mod_error()      dlerror()
132 #define mod_close(a)     dlclose(a)
133 #endif /* ! HAVE_DLFCN_H */
134
135 /******************************************************************
136  * locking.c
137  ******************************************************************/
138
139 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
140 #define read_lock(fd, offset, whence, len) \
141     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
142 #define write_lock(fd, offset, whence, len) \
143     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
144 #define unlock(fd, offset, whence, len) \
145     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
146
147 /******************************************************************
148  * socket.c
149  ******************************************************************/
150
151 extern int setnonblock(int fd, int cmd);
152 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
153 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
154 extern const char *getip_string(const struct sockaddr *sa);
155 extern unsigned int getip_port(const struct sockaddr *sa);
156 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
157 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
158
159 /* Structures and functions dealing with dynamic pollfd arrays */
160 enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
161 struct polldata {
162     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
163     void *data;         /* pointer to AFPconfig for listening socket and *
164                          * pointer to afp_child_t for IPC fd             */
165 };
166
167 extern void fdset_add_fd(int maxconns,
168                          struct pollfd **fdsetp,
169                          struct polldata **polldatap,
170                          int *fdset_usedp,
171                          int *fdset_sizep,
172                          int fd,
173                          enum fdtype fdtype,
174                          void *data);
175 extern void fdset_del_fd(struct pollfd **fdsetp,
176                          struct polldata **polldatap,
177                          int *fdset_usedp,
178                          int *fdset_sizep,
179                          int fd);
180 extern int send_fd(int socket, int fd);
181 extern int recv_fd(int fd, int nonblocking);
182
183 /******************************************************************
184  * unix.c
185  *****************************************************************/
186
187 extern const char *getcwdpath(void);
188 extern const char *fullpathname(const char *);
189 extern char *stripped_slashes_basename(char *p);
190 extern int lchdir(const char *dir);
191 extern void randombytes(void *buf, int n);
192 extern int daemonize(int nochdir, int noclose);
193
194 /******************************************************************
195  * cnid.c
196  *****************************************************************/
197
198 extern bstring rel_path_in_vol(const char *path, const char *volpath);
199
200 #endif  /* _ATALK_UTIL_H */