]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Merge master
[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
19 #include <atalk/unicode.h>
20 #include <atalk/bstrlib.h>
21
22 /* exit error codes */
23 #define EXITERR_CLNT 1  /* client related error */
24 #define EXITERR_CONF 2  /* error in config files/cmd line parameters */
25 #define EXITERR_SYS  3  /* local system error */
26
27 /* Print a SBT and exit */
28 #define AFP_PANIC(why) \
29     do {                                            \
30         netatalk_panic(why);                        \
31         abort();                                    \
32     } while(0);
33
34 /* LOG assert errors */
35 #ifndef NDEBUG
36 #define AFP_ASSERT(b) \
37     do {                                                                \
38         if (!(b)) {                                                     \
39             AFP_PANIC(#b);                                              \
40         } \
41     } while(0);
42 #else
43 #define AFP_ASSERT(b)
44 #endif /* NDEBUG */
45
46 #ifndef MIN
47 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
48 #endif
49
50 #ifndef MAX
51 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
52 #endif
53
54 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
55 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
56 #define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
57
58 #if BYTE_ORDER == BIG_ENDIAN
59 #define hton64(x)       (x)
60 #define ntoh64(x)       (x)
61 #else /* BYTE_ORDER == BIG_ENDIAN */
62 #define hton64(x)       ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
63                          (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
64 #define ntoh64(x)       (hton64(x))
65 #endif /* BYTE_ORDER == BIG_ENDIAN */
66
67 #ifdef WITH_SENDFILE
68 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
69 #endif
70
71 extern const int _diacasemap[], _dialowermap[];
72
73 extern char **getifacelist(void);
74 extern void freeifacelist(char **);
75
76 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
77 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
78 extern void bprint        (char *, int);
79 extern int strdiacasecmp  (const char *, const char *);
80 extern int strndiacasecmp (const char *, const char *, size_t);
81 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
82 extern int check_lockfile (const char *program, const char *pidfile);
83 extern int create_lockfile(const char *program, const char *pidfile);
84 extern void fault_setup   (void (*fn)(void *));
85 extern void netatalk_panic(const char *why);
86 #define server_unlock(x)  (unlink(x))
87
88 #ifndef HAVE_DLFCN_H
89 extern void *mod_open    (const char *);
90 extern void *mod_symbol  (void *, const char *);
91 extern void mod_close    (void *);
92 #define mod_error()      ""
93 #else /* ! HAVE_DLFCN_H */
94 #include <dlfcn.h>
95
96 #ifndef RTLD_NOW
97 #define RTLD_NOW 1
98 #endif /* ! RTLD_NOW */
99
100 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
101  * OpenBSD currently does not use the second arg for dlopen(). For
102  * future compatibility we define DL_LAZY */
103 #ifdef __NetBSD__
104 #define mod_open(a)      dlopen(a, RTLD_LAZY)
105 #elif defined(__OpenBSD__)
106 #define mod_open(a)      dlopen(a, DL_LAZY)
107 #else /* ! __NetBSD__ && ! __OpenBSD__ */
108 #define mod_open(a)      dlopen(a, RTLD_NOW)
109 #endif /* __NetBSD__ */
110
111 #ifndef DLSYM_PREPEND_UNDERSCORE
112 #define mod_symbol(a, b) dlsym(a, b)
113 #else /* ! DLSYM_PREPEND_UNDERSCORE */
114 extern void *mod_symbol  (void *, const char *);
115 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
116 #define mod_error()      dlerror()
117 #define mod_close(a)     dlclose(a)
118 #endif /* ! HAVE_DLFCN_H */
119
120 /******************************************************************
121  * locking.c
122  ******************************************************************/
123
124 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
125 #define read_lock(fd, offset, whence, len) \
126     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
127 #define write_lock(fd, offset, whence, len) \
128     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
129 #define unlock(fd, offset, whence, len) \
130     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
131
132 /******************************************************************
133  * socket.c
134  ******************************************************************/
135
136 extern int setnonblock(int fd, int cmd);
137 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
138 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
139 extern const char *getip_string(const struct sockaddr *sa);
140 extern unsigned int getip_port(const struct sockaddr *sa);
141 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
142 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
143
144 /* Structures and functions dealing with dynamic pollfd arrays */
145 enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
146 struct polldata {
147     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
148     void *data;         /* pointer to AFPconfig for listening socket and *
149                          * pointer to afp_child_t for IPC fd             */
150 };
151
152 extern void fdset_add_fd(int maxconns,
153                          struct pollfd **fdsetp,
154                          struct polldata **polldatap,
155                          int *fdset_usedp,
156                          int *fdset_sizep,
157                          int fd,
158                          enum fdtype fdtype,
159                          void *data);
160 extern void fdset_del_fd(struct pollfd **fdsetp,
161                          struct polldata **polldatap,
162                          int *fdset_usedp,
163                          int *fdset_sizep,
164                          int fd);
165 extern int send_fd(int socket, int fd);
166 extern int recv_fd(int fd, int nonblocking);
167
168 /******************************************************************
169  * unix.c
170  *****************************************************************/
171
172 extern const char *abspath(const char *name);
173 extern const char *getcwdpath(void);
174 extern char *stripped_slashes_basename(char *p);
175 extern int lchdir(const char *dir);
176 extern void randombytes(void *buf, int n);
177 extern int daemonize(int nochdir, int noclose);
178
179 /******************************************************************
180  * cnid.c
181  *****************************************************************/
182
183 extern bstring rel_path_in_vol(const char *path, const char *volpath);
184
185 #endif  /* _ATALK_UTIL_H */