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 */
20 #include <netatalk/at.h>
22 #include <atalk/unicode.h>
23 #include <atalk/bstrlib.h>
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 */
30 /* Print a SBT and exit */
31 #define AFP_PANIC(why) \
33 netatalk_panic(why); \
37 /* LOG assert errors */
39 #define AFP_ASSERT(b) \
49 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
51 #if BYTE_ORDER == BIG_ENDIAN
54 #else /* BYTE_ORDER == BIG_ENDIAN */
55 #define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
56 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
57 #define ntoh64(x) (hton64(x))
58 #endif /* BYTE_ORDER == BIG_ENDIAN */
61 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
64 extern const int _diacasemap[], _dialowermap[];
66 extern char **getifacelist(void);
67 extern void freeifacelist(char **);
69 #define diatolower(x) _dialowermap[(unsigned char) (x)]
70 #define diatoupper(x) _diacasemap[(unsigned char) (x)]
71 extern int atalk_aton (char *, struct at_addr *);
72 extern void bprint (char *, int);
73 extern int strdiacasecmp (const char *, const char *);
74 extern int strndiacasecmp (const char *, const char *, size_t);
75 extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/);
76 extern int check_lockfile (const char *program, const char *pidfile);
77 extern int create_lockfile(const char *program, const char *pidfile);
78 extern void fault_setup (void (*fn)(void *));
79 extern void netatalk_panic(const char *why);
80 #define server_unlock(x) (unlink(x))
82 /* strlcpy and strlcat are used by pam modules */
83 #ifndef UAM_MODULE_EXPORT
84 #define UAM_MODULE_EXPORT
88 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
92 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
96 extern void *mod_open (const char *);
97 extern void *mod_symbol (void *, const char *);
98 extern void mod_close (void *);
99 #define mod_error() ""
100 #else /* ! HAVE_DLFCN_H */
105 #endif /* ! RTLD_NOW */
107 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
108 * OpenBSD currently does not use the second arg for dlopen(). For
109 * future compatibility we define DL_LAZY */
111 #define mod_open(a) dlopen(a, RTLD_LAZY)
112 #elif defined(__OpenBSD__)
113 #define mod_open(a) dlopen(a, DL_LAZY)
114 #else /* ! __NetBSD__ && ! __OpenBSD__ */
115 #define mod_open(a) dlopen(a, RTLD_NOW)
116 #endif /* __NetBSD__ */
118 #ifndef DLSYM_PREPEND_UNDERSCORE
119 #define mod_symbol(a, b) dlsym(a, b)
120 #else /* ! DLSYM_PREPEND_UNDERSCORE */
121 extern void *mod_symbol (void *, const char *);
122 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
123 #define mod_error() dlerror()
124 #define mod_close(a) dlclose(a)
125 #endif /* ! HAVE_DLFCN_H */
127 /******************************************************************
129 ******************************************************************/
131 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
132 #define read_lock(fd, offset, whence, len) \
133 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
134 #define write_lock(fd, offset, whence, len) \
135 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
136 #define unlock(fd, offset, whence, len) \
137 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
139 /******************************************************************
141 ******************************************************************/
143 extern int setnonblock(int fd, int cmd);
144 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
145 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
146 extern const char *getip_string(const struct sockaddr *sa);
147 extern unsigned int getip_port(const struct sockaddr *sa);
148 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
149 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
151 /* Structures and functions dealing with dynamic pollfd arrays */
152 enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
154 enum fdtype fdtype; /* IPC fd or listening socket fd */
155 void *data; /* pointer to AFPconfig for listening socket and *
156 * pointer to afp_child_t for IPC fd */
159 extern void fdset_add_fd(int maxconns,
160 struct pollfd **fdsetp,
161 struct polldata **polldatap,
167 extern void fdset_del_fd(struct pollfd **fdsetp,
168 struct polldata **polldatap,
172 extern int send_fd(int socket, int fd);
173 extern int recv_fd(int fd, int nonblocking);
175 /******************************************************************
177 *****************************************************************/
179 extern const char *getcwdpath(void);
180 extern const char *fullpathname(const char *);
181 extern char *stripped_slashes_basename(char *p);
182 extern int lchdir(const char *dir);
183 extern void randombytes(void *buf, int n);
184 extern int daemonize(int nochdir, int noclose);
186 /******************************************************************
188 *****************************************************************/
190 extern bstring rel_path_in_vol(const char *path, const char *volpath);
192 #endif /* _ATALK_UTIL_H */