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