3 * Netatalk utility functions
5 * Utility functions for these areas: \n
8 * * misc UNIX function wrappers, eg for getcwd
12 #define _ATALK_UTIL_H 1
14 #include <sys/cdefs.h>
15 #include <sys/types.h>
18 #endif /* HAVE_UNISTD_H */
23 #include <netatalk/at.h>
25 #include <atalk/unicode.h>
26 #include <atalk/bstrlib.h>
28 /* exit error codes */
29 #define EXITERR_CLNT 1 /* client related error */
30 #define EXITERR_CONF 2 /* error in config files/cmd line parameters */
31 #define EXITERR_SYS 3 /* local system error */
33 /* Print a SBT and exit */
34 #define AFP_PANIC(why) \
36 netatalk_panic(why); \
40 /* LOG assert errors */
42 #define AFP_ASSERT(b) \
52 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
54 #define MAX(a,b) ((a) > (b) ? a : b)
57 #define MIN(a,b) ((a) < (b) ? a : b)
60 #if BYTE_ORDER == BIG_ENDIAN
63 #else /* BYTE_ORDER == BIG_ENDIAN */
64 #define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
65 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
66 #define ntoh64(x) (hton64(x))
67 #endif /* BYTE_ORDER == BIG_ENDIAN */
70 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
73 extern const int _diacasemap[], _dialowermap[];
75 extern char **getifacelist(void);
76 extern void freeifacelist(char **);
78 #define diatolower(x) _dialowermap[(unsigned char) (x)]
79 #define diatoupper(x) _diacasemap[(unsigned char) (x)]
81 extern int atalk_aton (char *, struct at_addr *);
83 extern void bprint (char *, int);
84 extern int strdiacasecmp (const char *, const char *);
85 extern int strndiacasecmp (const char *, const char *, size_t);
86 extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/);
87 extern int check_lockfile (const char *program, const char *pidfile);
88 extern int create_lockfile(const char *program, const char *pidfile);
89 extern void fault_setup (void (*fn)(void *));
90 extern void netatalk_panic(const char *why);
91 #define server_unlock(x) (unlink(x))
93 /* strlcpy and strlcat are used by pam modules */
94 #ifndef UAM_MODULE_EXPORT
95 #define UAM_MODULE_EXPORT
99 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
103 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
107 extern void *mod_open (const char *);
108 extern void *mod_symbol (void *, const char *);
109 extern void mod_close (void *);
110 #define mod_error() ""
111 #else /* ! HAVE_DLFCN_H */
116 #endif /* ! RTLD_NOW */
118 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
119 * OpenBSD currently does not use the second arg for dlopen(). For
120 * future compatibility we define DL_LAZY */
122 #define mod_open(a) dlopen(a, RTLD_LAZY)
123 #elif defined(__OpenBSD__)
124 #define mod_open(a) dlopen(a, DL_LAZY)
125 #else /* ! __NetBSD__ && ! __OpenBSD__ */
126 #define mod_open(a) dlopen(a, RTLD_NOW)
127 #endif /* __NetBSD__ */
129 #ifndef DLSYM_PREPEND_UNDERSCORE
130 #define mod_symbol(a, b) dlsym(a, b)
131 #else /* ! DLSYM_PREPEND_UNDERSCORE */
132 extern void *mod_symbol (void *, const char *);
133 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
134 #define mod_error() dlerror()
135 #define mod_close(a) dlclose(a)
136 #endif /* ! HAVE_DLFCN_H */
138 /******************************************************************
140 ******************************************************************/
142 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
143 #define read_lock(fd, offset, whence, len) \
144 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
145 #define write_lock(fd, offset, whence, len) \
146 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
147 #define unlock(fd, offset, whence, len) \
148 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
150 /******************************************************************
152 ******************************************************************/
154 extern int setnonblock(int fd, int cmd);
155 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
156 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
157 extern const char *getip_string(const struct sockaddr *sa);
158 extern unsigned int getip_port(const struct sockaddr *sa);
159 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
160 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
162 /* Structures and functions dealing with dynamic pollfd arrays */
163 enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
165 enum fdtype fdtype; /* IPC fd or listening socket fd */
166 void *data; /* pointer to AFPconfig for listening socket and *
167 * pointer to afp_child_t for IPC fd */
170 extern void fdset_add_fd(int maxconns,
171 struct pollfd **fdsetp,
172 struct polldata **polldatap,
178 extern void fdset_del_fd(struct pollfd **fdsetp,
179 struct polldata **polldatap,
183 extern int send_fd(int socket, int fd);
184 extern int recv_fd(int fd, int nonblocking);
186 /******************************************************************
188 *****************************************************************/
190 extern const char *getcwdpath(void);
191 extern const char *fullpathname(const char *);
192 extern char *stripped_slashes_basename(char *p);
193 extern void randombytes(void *buf, int n);
194 extern int daemonize(int nochdir, int noclose);
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(const char *path, mode_t mode, const struct stat *st, int options);
202 /******************************************************************
204 *****************************************************************/
206 extern bstring rel_path_in_vol(const char *path, const char *volpath);
208 #endif /* _ATALK_UTIL_H */