2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/include/am_utils.h
41 * Definitions that are specific to the am-utils package.
50 /**************************************************************************/
52 /**************************************************************************/
59 #endif /* not FALSE */
64 # define MAX(a, b) ((a) > (b) ? (a) : (b))
67 # define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define ONE_HOUR (60 * 60) /* One hour in seconds */
72 #ifndef MAXHOSTNAMELEN
74 # define MAXHOSTNAMELEN HOSTNAMESZ
75 # else /* not HOSTNAMESZ */
76 # define MAXHOSTNAMELEN 256
77 # endif /* not HOSTNAMESZ */
78 #endif /* not MAXHOSTNAMELEN */
81 * for hlfsd, and amd for detecting uid/gid
84 /* this is also defined in include/am_utils.h */
85 # define INVALIDID (((unsigned short) ~0) - 3)
86 #endif /* not INVALIDID */
89 * String comparison macros
91 #define STREQ(s1, s2) (strcmp((s1), (s2)) == 0)
92 #define STRCEQ(s1, s2) (strcasecmp((s1), (s2)) == 0)
93 #define NSTREQ(s1, s2, n) (strncmp((s1), (s2), (n)) == 0)
94 #define NSTRCEQ(s1, s2, n) (strncasecmp((s1), (s2), (n)) == 0)
95 #define FSTREQ(s1, s2) ((*(s1) == *(s2)) && STREQ((s1),(s2)))
98 * Logging options/flags
100 #define XLOG_FATAL 0x0001
101 #define XLOG_ERROR 0x0002
102 #define XLOG_USER 0x0004
103 #define XLOG_WARNING 0x0008
104 #define XLOG_INFO 0x0010
105 #define XLOG_DEBUG 0x0020
106 #define XLOG_MAP 0x0040
107 #define XLOG_STATS 0x0080
108 /* log option compositions */
109 #define XLOG_MASK 0x00ff /* mask for all flags */
110 #define XLOG_MANDATORY (XLOG_FATAL|XLOG_ERROR) /* cannot turn these off */
111 #define XLOG_ALL (XLOG_FATAL|XLOG_ERROR|XLOG_USER|XLOG_WARNING|XLOG_INFO|XLOG_MAP|XLOG_STATS)
112 /* default: fatal + error + user + warning + info */
113 #define XLOG_DEFAULT (XLOG_MASK & (XLOG_ALL & ~XLOG_MAP & ~XLOG_STATS))
115 /* default: no logging options */
117 #define NO_SUBNET "notknown" /* default subnet name for no subnet */
118 #define NEXP_AP (1022) /* gdmr: was 254 */
119 #define NEXP_AP_MARGIN (128) /* ???? not used */
124 #define AM_FIRST(ty, q) ((ty *) ((q)->q_forw))
125 #define AM_LAST(ty, q) ((ty *) ((q)->q_back))
126 #define NEXT(ty, q) ((ty *) (((qelem *) q)->q_forw))
127 #define PREV(ty, q) ((ty *) (((qelem *) q)->q_back))
128 #define HEAD(ty, q) ((ty *) q)
129 #define ITER(v, ty, q) \
130 for ((v) = AM_FIRST(ty,(q)); (v) != HEAD(ty,(q)); (v) = NEXT(ty,(v)))
132 /* allocate anything of type ty */
133 #define ALLOC(ty) ((ty *) xmalloc(sizeof(ty)))
134 #define CALLOC(ty) ((ty *) xzalloc(sizeof(ty)))
135 /* simply allocate b bytes */
136 #define SALLOC(b) xmalloc((b))
139 * Systems which have the mount table in a file need to read it before
140 * they can perform an unmount() system call.
142 #define UMOUNT_FS(dir, mtb_name, unmount_flags) umount_fs(dir, mtb_name, unmount_flags)
143 /* next two are imported via $srcdir/conf/umount/umount_*.c */
144 extern int umount_fs(char *mntdir, const char *mnttabname, u_int unmount_flags);
145 #ifdef MNT2_GEN_OPT_FORCE
146 extern int umount2_fs(const char *mntdir, u_int unmount_flags);
147 #endif /* MNT2_GEN_OPT_FORCE */
149 /* unmount-related flags (special handling of autofs, forced/lazy, etc.) */
150 #define AMU_UMOUNT_FORCE 0x1
151 #define AMU_UMOUNT_DETACH 0x2
152 #define AMU_UMOUNT_AUTOFS 0x4
155 * The following values can be tuned...
157 #define ALLOWED_MOUNT_TIME 40 /* 40s for a mount */
160 * RPC-related macros.
162 #define RPC_XID_PORTMAP 0
163 #define RPC_XID_MOUNTD 1
164 #define RPC_XID_NFSPING 2
165 #define RPC_XID_WEBNFS 3
166 #define RPC_XID_MASK (0x0f) /* 16 id's for now */
167 #define MK_RPC_XID(type_id, uniq) ((type_id) | ((uniq) << 4))
170 * What level of AMD are we backward compatible with?
171 * This only applies to externally visible characteristics.
172 * Rev.Minor.Branch.Patch (2 digits each)
174 #define AMD_COMPAT 5000000 /* 5.0 */
177 /**************************************************************************/
178 /*** STRUCTURES AND TYPEDEFS ***/
179 /**************************************************************************/
181 /* some typedefs must come first */
182 typedef char *amq_string;
183 typedef struct _qelem qelem;
184 typedef struct mntlist mntlist;
188 * (the name 'struct qelem' conflicts with linux's unistd.h)
216 * List of mount table entries
219 struct mntlist *mnext;
226 typedef struct mnt_map mnt_map;
229 /**************************************************************************/
231 /**************************************************************************/
236 extern char *mnttab_file_name; /* Mount table */
237 extern char *cpu; /* "CPU type" */
238 extern char *endian; /* "big" */
239 extern char *hostdomain; /* "southseas.nz" */
240 extern char copyright[]; /* Copyright info */
241 extern char version[]; /* Version info */
246 extern AUTH *nfs_auth; /* Dummy authorization for remote servers */
247 extern FILE *logfp; /* Log file */
248 extern SVCXPRT *nfsxprt;
249 extern char *PrimNetName; /* Name of primary connected network */
250 extern char *PrimNetNum; /* Name of primary connected network */
251 extern char *SubsNetName; /* Name of subsidiary connected network */
252 extern char *SubsNetNum; /* Name of subsidiary connected network */
254 extern void am_set_progname(char *pn); /* "amd" */
255 extern const char *am_get_progname(void); /* "amd" */
256 extern void am_set_hostname(char *hn);
257 extern const char *am_get_hostname(void);
258 extern pid_t am_set_mypid(void);
259 extern pid_t am_mypid;
261 extern int foreground; /* Foreground process */
262 extern int orig_umask; /* umask() on startup */
263 extern serv_state amd_state; /* Should we go now */
264 extern struct in_addr myipaddr; /* (An) IP address of this host */
265 extern struct opt_tab xlog_opt[];
266 extern u_short nfs_port; /* Our NFS service port */
271 extern CLIENT *get_mount_client(char *unused_host, struct sockaddr_in *sin, struct timeval *tv, int *sock, u_long mnt_version);
272 extern RETSIGTYPE sigchld(int);
273 extern bool_t xdr_amq_string(XDR *xdrs, amq_string *objp);
274 extern bool_t xdr_dirpath(XDR *xdrs, dirpath *objp);
275 extern char **strsplit(char *, int, int);
276 extern char *expand_selectors(char *);
277 extern char *get_version_string(void);
278 extern char *inet_dquad(char *, size_t, u_long);
279 extern char *print_wires(void);
280 extern char *str3cat(char *, char *, char *, char *);
281 extern char *strvcat(const char *, ...);
282 extern char *strealloc(char *, char *);
283 extern char *strip_selectors(char *, char *);
284 extern char *strnsave(const char *, int);
285 extern int amu_close(int fd);
286 extern int bind_resv_port(int, u_short *);
287 extern int cmdoption(char *, struct opt_tab *, u_int *);
288 extern int compute_automounter_mount_flags(mntent_t *);
289 extern int compute_mount_flags(mntent_t *);
290 extern void discard_nfs_args(void *, u_long);
291 extern u_long get_amd_program_number(void);
292 extern int getcreds(struct svc_req *, uid_t *, gid_t *, SVCXPRT *);
293 extern int hasmntval(mntent_t *, char *);
294 extern unsigned int hasmntvalerr(mntent_t *, char *, int *);
295 extern char *hasmntstr(mntent_t *, char *);
296 extern char *hasmnteq(mntent_t *, char *);
297 extern char *haseq(char *);
298 extern int is_network_member(const char *net);
299 extern int is_interface_local(u_long);
300 extern int islocalnet(u_long);
301 extern int make_rpc_packet(char *, int, u_long, struct rpc_msg *, voidp, XDRPROC_T_TYPE, AUTH *);
302 extern int mkdirs(char *, int);
303 extern int mount_fs(mntent_t *, int, caddr_t, int, MTYPE_TYPE, u_long, const char *, const char *, int);
304 extern void nfs_program_2(struct svc_req *rqstp, SVCXPRT *transp);
305 extern void nfs_program_3(struct svc_req *rqstp, SVCXPRT *transp);
306 #define get_nfs_dispatcher_version(a) \
307 ((a) == nfs_program_2 ? NFS_VERSION : NFS_VERSION3)
308 extern int pickup_rpc_reply(voidp, int, voidp, XDRPROC_T_TYPE);
309 extern int switch_option(char *);
310 extern int switch_to_logfile(char *logfile, int orig_umask, int truncate_log);
311 extern mntlist *read_mtab(char *, const char *);
312 #ifndef HAVE_TRANSPORT_TYPE_TLI
313 extern struct sockaddr_in *amu_svc_getcaller(SVCXPRT *xprt);
314 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
315 extern time_t time(time_t *);
316 extern void amu_get_myaddress(struct in_addr *iap, const char *preferred_localhost);
317 extern void amu_release_controlling_tty(void);
318 extern void compute_automounter_nfs_args(nfs_args_t *nap, mntent_t *mntp);
319 extern void discard_mntlist(mntlist *mp);
320 extern void free_mntlist(mntlist *);
321 extern void getwire(char **name1, char **number1);
322 extern void going_down(int);
323 extern void mnt_free(mntent_t *);
324 extern void plog(int, const char *,...)
325 __attribute__ ((__format__ (__printf__, 2, 3)));
326 extern void rmdirs(char *);
327 extern void rpc_msg_init(struct rpc_msg *, u_long, u_long, u_long);
328 extern void set_amd_program_number(u_long program);
329 extern void show_opts(int ch, struct opt_tab *);
330 extern void unregister_amq(void);
331 extern voidp xmalloc(int);
332 extern voidp xrealloc(voidp, int);
333 extern voidp xzalloc(int);
334 extern char *xstrdup(const char *);
335 extern int check_pmap_up(char *host, struct sockaddr_in* sin);
336 extern u_long get_nfs_version(char *host, struct sockaddr_in *sin, u_long nfs_version, const char *proto, u_long def);
337 extern int nfs_valid_version(u_long vers);
338 extern long get_server_pid(void);
339 extern void setup_sighandler(int signum, void (*handler)(int));
340 extern time_t clocktime(nfstime *nt);
342 #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
343 # ifdef HAVE_C99_VARARGS_MACROS
344 #define xsnprintf(str,size,fmt,...) _xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),__VA_ARGS__)
345 # endif /* HAVE_C99_VARARGS_MACROS */
346 # ifdef HAVE_GCC_VARARGS_MACROS
347 #define xsnprintf(str,size,fmt,args...) _xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),args)
348 # endif /* HAVE_GCC_VARARGS_MACROS */
349 extern int _xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...);
350 #define xvsnprintf(str,size,fmt,ap) _xvsnprintf(__FILE__,__LINE__,(str),(size),(fmt),(ap))
351 extern int _xvsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, va_list ap);
352 #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
353 extern int xsnprintf(char *str, size_t size, const char *format, ...);
354 extern int xvsnprintf(char *str, size_t size, const char *format, va_list ap);
355 #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
358 extern void _xstrlcat(const char *filename, int lineno, char *dst, const char *src, size_t len);
359 # define xstrlcat(d,s,l) _xstrlcat(__FILE__,__LINE__,(d),(s),(l))
360 extern void _xstrlcpy(const char *filename, int lineno, char *dst, const char *src, size_t len);
361 # define xstrlcpy(d,s,l) _xstrlcpy(__FILE__,__LINE__,(d),(s),(l))
362 #else /* not DEBUG */
363 extern void xstrlcat(char *dst, const char *src, size_t len);
364 extern void xstrlcpy(char *dst, const char *src, size_t len);
365 #endif /* not DEBUG */
367 #ifdef MOUNT_TABLE_ON_FILE
368 extern void rewrite_mtab(mntlist *, const char *);
369 extern void unlock_mntlist(void);
370 extern void write_mntent(mntent_t *, const char *);
371 #endif /* MOUNT_TABLE_ON_FILE */
373 #if defined(HAVE_SYSLOG_H) || defined(HAVE_SYS_SYSLOG_H)
374 extern int syslogging;
375 #endif /* defined(HAVE_SYSLOG_H) || defined(HAVE_SYS_SYSLOG_H) */
377 extern void compute_nfs_args(void *nap, mntent_t *mntp, int genflags, struct netconfig *nfsncp, struct sockaddr_in *ip_addr, u_long nfs_version, char *nfs_proto, am_nfs_handle_t *fhp, char *host_name, char *fs_name);
378 extern void destroy_nfs_args(void *nap, u_long nfs_version);
379 extern int create_amq_service(int *udp_soAMQp, SVCXPRT **udp_amqpp, struct netconfig **udp_amqncpp, int *tcp_soAMQp, SVCXPRT **tcp_amqpp, struct netconfig **tcp_amqncpp, u_short preferred_amq_port);
380 extern int create_nfs_service(int *soNFSp, u_short *nfs_portp, SVCXPRT **nfs_xprtp, void (*dispatch_fxn)(struct svc_req *rqstp, SVCXPRT *transp), u_long nfs_version);
381 extern int amu_svc_register(SVCXPRT *, u_long, u_long, void (*)(struct svc_req *, SVCXPRT *), u_long, struct netconfig *);
383 #ifdef HAVE_TRANSPORT_TYPE_TLI
385 extern int get_knetconfig(struct knetconfig **kncpp, struct netconfig *in_ncp, char *nc_protoname);
386 extern struct netconfig *nfsncp;
387 extern void free_knetconfig(struct knetconfig *kncp);
389 #endif /* HAVE_TRANSPORT_TYPE_TLI */
391 #ifdef HAVE_FS_AUTOFS
392 extern int register_autofs_service(char *autofs_conftype, void (*autofs_dispatch)(struct svc_req *rqstp, SVCXPRT *xprt));
393 extern int unregister_autofs_service(char *autofs_conftype);
394 #endif /* HAVE_FS_AUTOFS */
397 #ifndef HAVE_STRUCT_FHSTATUS_FHS_FH
398 # define fhs_fh fhstatus_u.fhs_fhandle
399 #endif /* not HAVE_STRUCT_FHSTATUS_FHS_FH */
403 * Network File System: the old faithful generation NFS V.2
406 # define NFS_VERSION2 ((u_int) 2)
407 #endif /* not NFS_VERSION2 */
410 * Network File System: the not so new anymore generation NFS V.3
413 # ifndef NFS_VERSION3
414 # define NFS_VERSION3 ((u_int) 3)
415 # endif /* not NFS_VERSION3 */
416 #endif /* HAVE_FS_NFS3 */
419 * Network File System: the new generation NFS V.4
422 # ifndef NFS_VERSION4
423 # define NFS_VERSION4 ((u_int) 4)
424 # endif /* not NFS_VERSION4 */
425 #endif /* HAVE_FS_NFS4 */
427 /**************************************************************************/
429 /**************************************************************************/
437 # define D_DAEMON 0x0001 /* Enter daemon mode */
438 # define D_TRACE 0x0002 /* Do protocol trace */
439 # define D_FULL 0x0004 /* Do full trace */
440 # define D_MTAB 0x0008 /* Use local mtab */
441 # define D_AMQ 0x0010 /* Register amq program */
442 # define D_STR 0x0020 /* Debug string munging */
444 # define D_MEM 0x0040 /* Trace memory allocations */
445 # else /* not DEBUG_MEM */
446 # define D_MEM 0x0000 /* Dummy */
447 # endif /* not DEBUG_MEM */
448 # define D_FORK 0x0080 /* Fork server (hlfsd only) */
449 # define D_INFO 0x0100 /* info service specific debugging (hesiod, nis, etc) */
450 # define D_HRTIME 0x0200 /* Print high resolution time stamps */
451 # define D_XDRTRACE 0x0400 /* Trace xdr routines */
452 # define D_READDIR 0x0800 /* Show browsable_dir progress */
453 /* debug option compositions */
454 # define D_MASK 0x0fff /* mask of known flags */
455 # define D_BASIC (D_TRACE|D_FULL|D_STR|D_MEM|D_INFO|D_XDRTRACE|D_READDIR)
456 # define D_CONTROL (D_DAEMON|D_AMQ|D_FORK)
457 /* immutable flags: cannot be changed via "amq -D" */
458 # define D_IMMUTABLE (D_MTAB | D_CONTROL)
459 # define D_ALL (D_BASIC | D_CONTROL)
460 # define D_DEFAULT (D_MASK & D_ALL & ~D_XDRTRACE)
461 /* test mode: nodaemon, noamq, nofork, (local) mtab */
462 # define D_TEST (D_BASIC | D_MTAB)
464 # define amuDebug(x) (debug_flags & (x))
465 # define dlog if (amuDebug(D_FULL)) dplog
467 /* my favorite debugging tool -Erez */
468 #define EZKDBG plog(XLOG_INFO,"EZK:%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__)
472 * If debugging memory, then call a special freeing function that logs
473 * more info, and resets the pointer to NULL so it cannot be used again.
475 # define XFREE(x) dxfree(__FILE__,__LINE__,x)
476 extern void dxfree(char *file, int line, voidp ptr);
477 extern void malloc_verify(void);
478 # else /* not DEBUG_MEM */
480 * If regular debugging, then free the pointer and reset to NULL.
481 * This should remain so for as long as am-utils is in alpha/beta testing.
483 # define XFREE(x) do { free((voidp)x); x = NULL;} while (0)
484 # endif /* not DEBUG_MEM */
486 /* functions that depend solely on debugging */
487 extern void print_nfs_args(const void *, u_long nfs_version);
488 extern int debug_option (char *opt);
489 extern void dplog(const char *fmt, ...)
490 __attribute__ ((__format__ (__printf__, 1, 2)));
492 #else /* not DEBUG */
494 /* set dummy flags to zero */
495 # define D_DAEMON 0x0001 /* Enter daemon mode */
496 # define D_TRACE 0x0000 /* dummy: Do protocol trace */
497 # define D_FULL 0x0000 /* dummy: Do full trace */
498 # define D_MTAB 0x0000 /* dummy: Use local mtab */
499 # define D_AMQ 0x0010 /* Register amq program */
500 # define D_STR 0x0000 /* dummy: Debug string munging */
501 # define D_MEM 0x0000 /* dummy: Trace memory allocations */
502 # define D_FORK 0x0080 /* Fork server (hlfsd only) */
503 # define D_INFO 0x0000 /* dummy: info service debugging */
504 # define D_HRTIME 0x0000 /* dummy: hi-res time stamps */
505 # define D_XDRTRACE 0x0000 /* dummy: Trace xdr routines */
506 # define D_READDIR 0x0000 /* dummy: browsable_dir progress */
507 # define D_CONTROL (D_DAEMON|D_AMQ|D_FORK)
508 # define amuDebug(x) (debug_flags & (x))
510 * If not debugging, then also reset the pointer.
511 * It's safer -- and besides, free() should do that anyway.
513 # define XFREE(x) do { free((voidp)x); x = NULL;} while (0)
515 # if defined(HAVE_GCC_VARARGS_MACROS)
516 # define dlog(fmt...)
517 # elif defined(HAVE_C99_VARARGS_MACROS)
519 # else /* no c99 varargs */
520 /* this define means that we CCP leaves code behind the (list,of,args) */
522 # endif /* no c99 varargs */
524 # define print_nfs_args(nap, nfs_version)
525 # define debug_option(x) (1)
527 #endif /* not DEBUG */
529 extern u_int debug_flags; /* Debug options */
530 extern struct opt_tab dbg_opt[];
532 /**************************************************************************/
533 /*** MISC (stuff left to autoconfiscate) ***/
534 /**************************************************************************/
536 #endif /* not _AM_UTILS_H */