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 <stdbool.h> 21#include <sys/stat.h> 22 23#include <netatalk/at.h> 24 25#include <atalk/unicode.h> 26#include <atalk/bstrlib.h> 27 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 */ 32 33/* Print a SBT and exit */ 34#define AFP_PANIC(why) \ 35 do { \ 36 netatalk_panic(why); \ 37 abort(); \ 38 } while(0); 39 40/* LOG assert errors */ 41#ifndef NDEBUG 42#define AFP_ASSERT(b) \ 43 do { \ 44 if (!(b)) { \ 45 AFP_PANIC(#b); \ 46 } \ 47 } while(0); 48#else 49#define AFP_ASSERT(b) 50#endif /* NDEBUG */ 51 52#define STRCMP(a,b,c) (strcmp(a,c) b 0) 53#ifndef MAX 54#define MAX(a,b) ((a) > (b) ? a : b) 55#endif 56#ifndef MIN 57#define MIN(a,b) ((a) < (b) ? a : b) 58#endif 59 60#if BYTE_ORDER == BIG_ENDIAN 61#define hton64(x) (x) 62#define ntoh64(x) (x) 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 */ 68 69#ifdef WITH_SENDFILE 70extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count); 71#endif 72 73extern const int _diacasemap[], _dialowermap[]; 74 75extern char **getifacelist(void); 76extern void freeifacelist(char **); 77 78#define diatolower(x) _dialowermap[(unsigned char) (x)] 79#define diatoupper(x) _diacasemap[(unsigned char) (x)] 80#ifndef NO_DDP 81extern int atalk_aton (char *, struct at_addr *); 82#endif 83extern void bprint (char *, int); 84extern int strdiacasecmp (const char *, const char *); 85extern int strndiacasecmp (const char *, const char *, size_t); 86extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/); 87extern int check_lockfile (const char *program, const char *pidfile); 88extern int create_lockfile(const char *program, const char *pidfile); 89extern void fault_setup (void (*fn)(void *)); 90extern void netatalk_panic(const char *why); 91#define server_unlock(x) (unlink(x)) 92 93/* strlcpy and strlcat are used by pam modules */ 94#ifndef UAM_MODULE_EXPORT 95#define UAM_MODULE_EXPORT 96#endif 97 98#ifndef HAVE_STRLCPY 99UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t); 100#endif 101 102#ifndef HAVE_STRLCAT 103UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t); 104#endif 105 106#ifndef HAVE_DLFCN_H 107extern void *mod_open (const char *); 108extern void *mod_symbol (void *, const char *); 109extern void mod_close (void *); 110#define mod_error() "" 111#else /* ! HAVE_DLFCN_H */ 112#include <dlfcn.h> 113 114#ifndef RTLD_NOW 115#define RTLD_NOW 1 116#endif /* ! RTLD_NOW */ 117 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 */ 121#ifdef __NetBSD__ 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__ */ 128 129#ifndef DLSYM_PREPEND_UNDERSCORE 130#define mod_symbol(a, b) dlsym(a, b) 131#else /* ! DLSYM_PREPEND_UNDERSCORE */ 132extern 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 */ 137 138/****************************************************************** 139 * locking.c 140 ******************************************************************/ 141 142extern 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)) 149 150/****************************************************************** 151 * socket.c 152 ******************************************************************/ 153 154extern int setnonblock(int fd, int cmd); 155extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout); 156extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout); 157extern const char *getip_string(const struct sockaddr *sa); 158extern unsigned int getip_port(const struct sockaddr *sa); 159extern void apply_ip_mask(struct sockaddr *ai, int maskbits); 160extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2); 161 162/* Structures and functions dealing with dynamic pollfd arrays */ 163enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD}; 164struct polldata { 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 */ 168}; 169 170extern void fdset_add_fd(int maxconns, 171 struct pollfd **fdsetp, 172 struct polldata **polldatap, 173 int *fdset_usedp, 174 int *fdset_sizep, 175 int fd, 176 enum fdtype fdtype, 177 void *data); 178extern void fdset_del_fd(struct pollfd **fdsetp, 179 struct polldata **polldatap, 180 int *fdset_usedp, 181 int *fdset_sizep, 182 int fd); 183extern int send_fd(int socket, int fd); 184extern int recv_fd(int fd, int nonblocking); 185 186/****************************************************************** 187 * unix.c 188 *****************************************************************/ 189 190extern const char *getcwdpath(void); 191extern const char *fullpathname(const char *); 192extern char *stripped_slashes_basename(char *p); 193extern void randombytes(void *buf, int n); 194extern int daemonize(int nochdir, int noclose); 195 196extern int ochdir(const char *dir, int options); 197extern int ostat(const char *path, struct stat *buf, int options); 198extern int ostatat(int dirfd, const char *path, struct stat *st, int options); 199extern int ochown(const char *path, uid_t owner, gid_t group, int options); 200extern int ochmod(const char *path, mode_t mode, const struct stat *st, int options); 201 202/****************************************************************** 203 * cnid.c 204 *****************************************************************/ 205 206extern bstring rel_path_in_vol(const char *path, const char *volpath); 207 208#endif /* _ATALK_UTIL_H */ 209