netatalk  4.6.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
include/atalk/util.h
Go to the documentation of this file.
1
10
11#ifndef _ATALK_UTIL_H
12#define _ATALK_UTIL_H 1
13
14#include <poll.h>
15#include <stdbool.h>
16#include <sys/socket.h>
17#include <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <bstrlib.h>
22
23#include <netatalk/at.h>
24
25#include <atalk/cnid.h>
26#include <atalk/unicode.h>
27
28#ifndef RLIM_MAX
29/* Desired afpd fd budget: backs the full 0xffff refnum space at ~2 fds/fork
30 * plus infra fds. A target only; setlimits() probes down if the kernel says no. */
31#define RLIM_MAX 133120
32#endif
33
34#ifndef RLIM_MAX_CNID_METAD
35/* cnid_metad's fd need is bounded by MAXVOLS, not the refnum space; modest fixed
36 * raise. Guarded separately so a platform-defined RLIM_MAX can't skip it. */
37#define RLIM_MAX_CNID_METAD 4200
38#endif
39
40/* exit error codes */
41#define EXITERR_CLNT 1
42#define EXITERR_CONF 2
43#define EXITERR_SYS 3
44#define EXITERR_CLOSED 4
45
47#define AFP_PANIC(why) \
48 do { \
49 netatalk_panic(why); \
50 abort(); \
51 } while(0)
52
53#ifndef NDEBUG
55#define AFP_ASSERT(b) \
56 do { \
57 if (!(b)) { \
58 AFP_PANIC(#b); \
59 } \
60 } while(0)
61#else
62#define AFP_ASSERT(b) do {} while(0)
63#endif /* NDEBUG */
64
65#ifndef MIN
66#define MIN(a, b) ((a) < (b) ? (a) : (b))
67#endif
68
69#ifndef MAX
70#define MAX(a, b) ((a) > (b) ? (a) : (b))
71#endif
72
73#define STRCMP(a,b,c) (strcmp(a,c) b 0)
74#define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
75#define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
76
77#ifdef WORDS_BIGENDIAN
78#define hton64(x) (x)
79#define ntoh64(x) (x)
80#else
81#define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
82 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
83#define ntoh64(x) (hton64(x))
84#endif
85
86#ifndef SAFE_FREE
87#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
88#endif
89
90#ifdef WITH_SENDFILE
91extern ssize_t sys_sendfile(int __out_fd, int __in_fd, off_t *__offset,
92 size_t __count);
93#endif
94
95extern const int _diacasemap[], _dialowermap[];
96
97extern char **getifacelist(void);
98extern void freeifacelist(char **);
99
100#define diatolower(x) _dialowermap[(unsigned char) (x)]
101#define diatoupper(x) _diacasemap[(unsigned char) (x)]
102#ifndef NO_DDP
103extern int atalk_aton(char *, struct at_addr *);
104#endif
105extern void bprint(char *, int);
106extern int strdiacasecmp(const char *, const char *);
107extern int strndiacasecmp(const char *, const char *, size_t);
108extern pid_t server_lock(char * /*program*/, char * /*file*/, int /*debug*/);
109extern int check_lockfile(const char *program, const char *pidfile);
110extern int create_lockfile(const char *program, const char *pidfile);
111extern void fault_setup(void (*fn)(void *));
112extern void fault_setup_thread(void);
113extern void netatalk_panic(const char *why);
114#define server_unlock(x) (unlink(x))
115
116#ifndef HAVE_DLFCN_H
117extern void *mod_open(const char *);
118extern void *mod_symbol(void *, const char *);
119extern void mod_close(void *);
120#define mod_error() ""
121#else /* ! HAVE_DLFCN_H */
122#include <dlfcn.h>
123
124#ifndef RTLD_NOW
125#define RTLD_NOW 1
126#endif /* ! RTLD_NOW */
127
128#define mod_open(a) dlopen(a, RTLD_NOW | RTLD_GLOBAL)
129
130#define mod_symbol(a, b) dlsym(a, b)
131#define mod_error() dlerror()
132#define mod_close(a) dlclose(a)
133#endif /* ! HAVE_DLFCN_H */
134
135#ifndef strequal
136#define strequal(a,b) (strcmp((a),(b)) == 0)
137#endif
138
139#define cfrombstr(b) ((char *)((b)->data))
140
142#define BSTRING_STRIP_SLASH(a) \
143 do { \
144 while (bchar((a), blength(a) - 1) == '/') \
145 bdelete((a), blength(a) - 1, 1); \
146 } while (0);
147
148/******************************************************************
149 * locking.c
150 ******************************************************************/
151
152extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence,
153 off_t len);
154#define read_lock(fd, offset, whence, len) \
155 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
156#define write_lock(fd, offset, whence, len) \
157 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
158#define unlock(fd, offset, whence, len) \
159 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
160
161/******************************************************************
162 * sigpipe.c
163 ******************************************************************/
164
165extern int atalk_sigpipe_init(void);
166extern int atalk_sigpipe_readfd(void);
167extern void atalk_sigpipe_notify(void);
168extern void atalk_sigpipe_drain(void);
169
170/******************************************************************
171 * socket.c
172 ******************************************************************/
173
174extern int setnonblock(int fd, int cmd);
175extern ssize_t readt(int socket, void *data, const size_t length,
176 int setnonblocking, int timeout);
177extern ssize_t writet(int socket, void *data, const size_t length,
178 int setnonblocking, int timeout);
179extern const char *getip_string(const struct sockaddr *sa);
180extern unsigned int getip_port(const struct sockaddr *sa);
181extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
182extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
183extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
184
185/* Structures and functions dealing with dynamic pollfd arrays */
186
188
192struct asev_data {
194 void *private;
197};
198
202struct asev {
203 struct pollfd *fdset;
204 struct asev_data *data;
205 int max;
206 int used;
207};
208
209extern struct asev *asev_init(int max);
210extern bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype,
211 void *private, int protocol);
212extern bool asev_del_fd(struct asev *sev, int fd);
213
214extern int send_fd(int socket, int fd);
215extern int recv_fd(int fd, int nonblocking);
216
217/******************************************************************
218 * unix.c
219 *****************************************************************/
220
221extern const char *getcwdpath(void);
222extern const char *fullpathname(const char *);
223extern char *stripped_slashes_basename(char *p);
224extern void randombytes(void *buf, int n);
225extern int daemonize(void);
226extern int run_cmd(const char *cmd, char **cmd_argv);
227extern char *realpath_safe(const char *path);
228extern const char *basename_safe(const char *path);
229extern char *strtok_quote(char *s, const char *delim);
230extern const char *tmpdir(void);
231extern int safe_atoi(const char *str, const char *param_name,
232 int min_val, int max_val, int default_val);
233
234extern int ochdir(const char *dir, int options);
235extern int ostat(const char *path, struct stat *buf, int options);
236extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
237extern int ochown(const char *path, uid_t owner, gid_t group, int options);
238extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
239
240/******************************************************************
241 * cnid.c
242 *****************************************************************/
243
244extern bstring rel_path_in_vol(const char *path, const char *volpath);
245extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath,
246 const char *path, cnid_t *did);
247extern char *uuid_strip_dashes(const char *uuid);
248
249/******************************************************************
250 * gettok.c
251 *****************************************************************/
252
253extern void initline(int, char *);
254extern int parseline(int, char *);
255
256/******************************************************************
257 * pathconv.c
258 *****************************************************************/
259
260extern char *convert_utf8_to_mac(const struct vol *vol, const char *upath);
261
262#endif /* _ATALK_UTIL_H */
uint32_t cnid_t
Definition adouble.h:157
static char buf[MAXPATHLEN+1]
Definition afppasswd.c:85
uid_t uuid
C implementaion of bstring functions.
struct tagbstring * bstring
Definition bstrlib.h:86
#define data
Definition hash.c:52
int atalk_sigpipe_readfd(void)
Definition sigpipe.c:71
void freeifacelist(char **)
Definition getiface.c:157
const char * tmpdir(void)
Find a suitable temporary directory for Netatalk.
Definition libatalk/util/unix.c:289
void atalk_sigpipe_drain(void)
Definition sigpipe.c:85
ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout)
non-blocking drop-in replacement for read with timeout using select
Definition socket.c:88
const char * getip_string(const struct sockaddr *sa)
convert an IPv4 or IPv6 address to a static string using inet_ntop
Definition socket.c:327
const char * getcwdpath(void)
get cwd in static buffer
Definition libatalk/util/unix.c:219
int parseline(int, char *)
Definition gettok.c:31
void fault_setup_thread(void)
Definition fault.c:266
pid_t server_lock(char *, char *, int)
Definition server_lock.c:33
bstring rel_path_in_vol(const char *path, const char *volpath)
Build path relative to volume root.
Definition util/cnid.c:71
char * stripped_slashes_basename(char *p)
Takes a buffer with a path, strips slashs, returns basename.
Definition libatalk/util/unix.c:271
int ochmod(char *path, mode_t mode, const struct stat *st, int options)
chmod() wrapper for symlink and ACL handling
Definition libatalk/util/unix.c:425
void atalk_sigpipe_notify(void)
Definition sigpipe.c:77
int ochown(const char *path, uid_t owner, gid_t group, int options)
Definition libatalk/util/unix.c:402
int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
compare IP addresses for equality
Definition socket.c:460
asev_fdtype
Definition include/atalk/util.h:187
@ STATS_FD
Definition include/atalk/util.h:187
@ IPC_FD
Definition include/atalk/util.h:187
@ LISTEN_FD
Definition include/atalk/util.h:187
const int _dialowermap[]
Definition include/atalk/util.h:95
int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len)
lock a file with fctnl
Definition locking.c:86
int safe_atoi(const char *str, const char *param_name, int min_val, int max_val, int default_val)
Safe string to integer conversion with bounds checking.
Definition netatalk_conf.c:1075
char * realpath_safe(const char *path)
Definition libatalk/util/unix.c:616
int ochdir(const char *dir, int options)
symlink safe chdir replacement
Definition libatalk/util/unix.c:481
int ostat(const char *path, struct stat *buf, int options)
Definition libatalk/util/unix.c:393
char * uuid_strip_dashes(const char *uuid)
Definition util/cnid.c:229
int tokenize_ip_port(const char *ipurl, char **address, char **port)
Tokenize IP(4/6) addresses with an optional port into address and port.
Definition socket.c:492
const char * fullpathname(const char *)
Request absolute path.
Definition libatalk/util/unix.c:236
int strdiacasecmp(const char *, const char *)
Definition strdicasecmp.c:527
const int _diacasemap[]
Definition strdicasecmp.c:7
bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype, void *private, int protocol)
Add a fd to a dynamic pollfd array and associated data array.
Definition socket.c:594
unsigned int getip_port(const struct sockaddr *sa)
return port number from struct sockaddr
Definition socket.c:373
char * strtok_quote(char *s, const char *delim)
extended strtok allows the quoted strings
Definition libatalk/util/unix.c:669
void * mod_symbol(void *, const char *)
struct asev * asev_init(int max)
Definition socket.c:564
int atalk_sigpipe_init(void)
Definition sigpipe.c:40
char * convert_utf8_to_mac(const struct vol *vol, const char *upath)
Convert a UTF-8 filename to a mangled Mac filename.
Definition pathconv.c:25
ssize_t sys_sendfile(int __out_fd, int __in_fd, off_t *__offset, size_t __count)
Definition ad_sendfile.c:78
int create_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:138
int run_cmd(const char *cmd, char **cmd_argv)
Definition libatalk/util/unix.c:63
void * mod_open(const char *)
void apply_ip_mask(struct sockaddr *ai, int maskbits)
apply netmask to IP (v4 or v6)
Definition socket.c:397
int ostatat(int dirfd, const char *path, struct stat *st, int options)
ostat/fsstatat multiplexer
Definition libatalk/util/unix.c:463
int send_fd(int socket, int fd)
Definition socket.c:746
const char * basename_safe(const char *path)
safe basename() replacement
Definition libatalk/util/unix.c:657
int daemonize(void)
Fork, exit parent, setsid(), chdir("/"), close all fds.
Definition libatalk/util/unix.c:120
void netatalk_panic(const char *why)
Definition fault.c:172
void initline(int, char *)
Definition gettok.c:20
void fault_setup(void(*fn)(void *))
Definition fault.c:277
int check_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:111
void bprint(char *, int)
Definition bprint.c:17
cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did)
Resolves CNID of a given path.
Definition util/cnid.c:177
char ** getifacelist(void)
Get interfaces from the kernel.
Definition getiface.c:136
ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout)
non-blocking drop-in replacement for read with timeout using select
Definition socket.c:217
void mod_close(void *)
void randombytes(void *buf, int n)
Definition libatalk/util/unix.c:571
int recv_fd(int fd, int nonblocking)
Receive a fd on a suitable socket.
Definition socket.c:682
bool asev_del_fd(struct asev *sev, int fd)
Remove fd from asev.
Definition socket.c:622
int strndiacasecmp(const char *, const char *, size_t)
Definition strdicasecmp.c:540
int atalk_aton(char *, struct at_addr *)
Check whether "cp" is a valid ascii representation of an AppleTalk address and convert to a binary ad...
Definition atalk_addr.c:32
int setnonblock(int fd, int cmd)
set or unset non-blocking IO on a fd
Definition socket.c:53
cnid_t did
Definition nad_cp.c:92
Definition cnid.h:84
atalk socket event data
Definition include/atalk/util.h:192
int protocol
Definition include/atalk/util.h:196
enum asev_fdtype fdtype
Definition include/atalk/util.h:193
atalk socket event
Definition include/atalk/util.h:202
int max
Definition include/atalk/util.h:205
int used
Definition include/atalk/util.h:206
struct asev_data * data
Definition include/atalk/util.h:204
struct pollfd * fdset
Definition include/atalk/util.h:203
Definition at.h:68
Definition include/atalk/directory.h:72
Definition include/atalk/directory.h:140
Definition include/atalk/volume.h:33