2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * $Id: amd.c,v 1.8.2.6 2004/01/06 03:15:16 ezk Exp $
41 * File: am-utils/amd/amd.c
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
54 #endif /* HAVE_CONFIG_H */
58 struct amu_global_options gopt; /* where global options are stored */
60 char pid_fsname[SIZEOF_PID_FSNAME]; /* "kiska.southseas.nz:(pid%d)" */
61 char *hostdomain = "unknown.domain";
62 #define SIZEOF_HOSTD (2 * MAXHOSTNAMELEN + 1) /* Host+domain */
63 char hostd[SIZEOF_HOSTD]; /* Host+domain */
64 char *endian = ARCH_ENDIAN; /* Big or Little endian */
65 char *cpu = HOST_CPU; /* CPU type */
66 char *PrimNetName; /* name of primary network */
67 char *PrimNetNum; /* number of primary network */
69 int immediate_abort; /* Should close-down unmounts be retried */
71 int select_intr_valid;
74 struct amd_stats amd_stats; /* Server statistics */
75 struct in_addr myipaddr; /* (An) IP address of this host */
76 time_t do_mapc_reload = 0; /* mapc_reload() call required? */
79 int amd_use_autofs = 0;
80 #endif /* HAVE_FS_AUTOFS */
84 #endif /* HAVE_SIGACTION */
89 * SIGINT - tells amd to do a full shutdown, including unmounting all
91 * SIGTERM - tells amd to shutdown now. Just unmounts the automount nodes.
96 #ifdef REINSTALL_SIGNAL_HANDLER
98 #endif /* REINSTALL_SIGNAL_HANDLER */
102 immediate_abort = 15;
106 immediate_abort = -1;
107 /* fall through... */
110 plog(XLOG_WARNING, "WARNING: automounter going down on signal %d", sig);
113 if (select_intr_valid)
114 longjmp(select_intr, sig);
119 * Hook for cache reload.
120 * When a SIGHUP arrives it schedules a call to mapc_reload
125 #ifdef REINSTALL_SIGNAL_HANDLER
127 #endif /* REINSTALL_SIGNAL_HANDLER */
130 dlog("spurious call to sighup");
132 * Force a reload by zero'ing the timer
134 if (amd_state == Run)
143 * This signal handler is called during Amd initialization. The parent
144 * forks a child to do all the hard automounting work, and waits for a
145 * SIGQUIT signal from the child. When the parent gets the signal it's
146 * supposed to call this handler and exit(3), thus completing the
147 * daemonizing process. Alas, on some systems, especially Linux 2.4/2.6
148 * with Glibc, exit(3) doesn't always terminate the parent process.
149 * Worse, the parent process now refuses to accept any more SIGQUIT
150 * signals -- they are blocked. What's really annoying is that this
151 * doesn't happen all the time, suggesting a race condition somewhere.
152 * (This happens even if I change the logic to use another signal.) I
153 * traced this to something which exit(3) does in addition to exiting the
154 * process, probably some atexit() stuff or other side-effects related to
155 * signal handling. Either way, since at this stage the parent process
156 * just needs to terminate, I'm simply calling _exit(2). Note also that
157 * the OpenGroup doesn't list exit(3) as a recommended "Base Interface"
158 * but they do list _exit(2) as one. This fix seems to work reliably all
159 * the time. -Erez (2/27/2005)
170 #ifdef HAVE_SIGACTION
171 struct sigaction sa, osa;
173 memset(&sa, 0, sizeof(sa));
174 sa.sa_handler = parent_exit;
176 sigemptyset(&(sa.sa_mask));
177 sigaddset(&(sa.sa_mask), SIGQUIT);
178 sigaction(SIGQUIT, &sa, &osa);
179 #else /* not HAVE_SIGACTION */
180 signal(SIGQUIT, parent_exit);
181 #endif /* not HAVE_SIGACTION */
183 bgpid = background();
187 * Now wait for the automount points to
192 /* should never reach here */
194 #ifdef HAVE_SIGACTION
195 sigaction(SIGQUIT, &osa, NULL);
196 #else /* not HAVE_SIGACTION */
197 signal(SIGQUIT, SIG_DFL);
198 #endif /* not HAVE_SIGACTION */
201 * Record our pid to make it easier to kill the correct amd.
203 if (gopt.flags & CFM_PRINT_PID) {
204 if (STREQ(gopt.pid_file, "/dev/stdout")) {
205 printf("%ld\n", (long) am_mypid);
206 /* flush stdout, just in case */
210 mode_t prev_umask = umask(0022); /* set secure temporary umask */
212 f = fopen(gopt.pid_file, "w");
214 fprintf(f, "%ld\n", (long) am_mypid);
217 fprintf(stderr, "cannot open %s (errno=%d)\n", gopt.pid_file, errno);
219 umask(prev_umask); /* restore umask */
224 * Pretend we are in the foreground again
229 * Dissociate from the controlling terminal
231 amu_release_controlling_tty();
238 * Initialize global options structure.
241 init_global_options(void)
243 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
244 static struct utsname un;
245 #endif /* defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) */
248 memset(&gopt, 0, sizeof(struct amu_global_options));
250 /* name of current architecture */
251 gopt.arch = HOST_ARCH;
253 /* automounter temp dir */
254 gopt.auto_dir = "/.amd_mnt";
256 /* toplevel attribute cache timeout */
257 gopt.auto_attrcache = 0;
262 /* executable map timeout */
263 gopt.exec_map_timeout = AMFS_EXEC_MAP_TIMEOUT;
266 * kernel architecture: this you must get from uname() if possible.
268 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
270 gopt.karch = un.machine;
272 #endif /* defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) */
273 gopt.karch = HOST_ARCH;
278 /* operating system name */
279 gopt.op_sys = HOST_OS_NAME;
282 gopt.op_sys_ver = HOST_OS_VERSION;
284 /* full OS name and version */
285 gopt.op_sys_full = HOST_OS;
288 gopt.op_sys_vendor = HOST_VENDOR;
291 gopt.pid_file = "/dev/stdout";
294 gopt.sub_domain = NULL;
296 /* reset NFS (and toplvl) retransmit counter and retry interval */
297 for (i=0; i<AMU_TYPE_MAX; ++i) {
298 gopt.amfs_auto_retrans[i] = -1; /* -1 means "never set before" */
299 gopt.amfs_auto_timeo[i] = -1; /* -1 means "never set before" */
303 gopt.am_timeo = AM_TTL;
305 /* dismount interval */
306 gopt.am_timeo_w = AM_TTL_W;
308 /* map reload intervl */
309 gopt.map_reload_interval = ONE_HOUR;
312 * various CFM_* flags that are on by default.
314 gopt.flags = CFM_DEFAULT_FLAGS;
316 #ifdef HAVE_MAP_HESIOD
317 /* Hesiod rhs zone */
318 gopt.hesiod_base = "automount";
319 #endif /* HAVE_MAP_HESIOD */
323 gopt.ldap_base = NULL;
325 /* LDAP host ports */
326 gopt.ldap_hostports = NULL;
329 gopt.ldap_cache_seconds = 0;
330 gopt.ldap_cache_maxmem = 131072;
332 /* LDAP protocol version */
333 gopt.ldap_proto_version = 2;
334 #endif /* HAVE_MAP_LDAP */
338 gopt.nis_domain = NULL;
339 #endif /* HAVE_MAP_NIS */
344 * Lock process text and data segment in memory (after forking the daemon)
347 do_memory_locking(void)
349 #if defined(HAVE_PLOCK) || defined(HAVE_MLOCKALL)
351 #else /* not HAVE_PLOCK and not HAVE_MLOCKALL */
352 plog(XLOG_WARNING, "Process memory locking not supported by the OS");
353 #endif /* not HAVE_PLOCK and not HAVE_MLOCKALL */
357 * On AIX you must lower the stack size using ulimit() before calling
358 * plock. Otherwise plock will reserve a lot of memory space based on
359 * your maximum stack size limit. Since it is not easily possible to
360 * tell what should the limit be, I print a warning before calling
361 * plock(). See the manual pages for ulimit(1,3,4) on your AIX system.
363 plog(XLOG_WARNING, "AIX: may need to lower stack size using ulimit(3) before calling plock");
365 if (!locked_ok && plock(PROCLOCK) != 0)
366 plog(XLOG_WARNING, "Couldn't lock process pages in memory using plock(): %m");
369 #endif /* HAVE_PLOCK */
371 if (!locked_ok && mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
372 plog(XLOG_WARNING, "Couldn't lock process pages in memory using mlockall(): %m");
375 #endif /* HAVE_MLOCKALL */
376 #if defined(HAVE_PLOCK) || defined(HAVE_MLOCKALL)
378 plog(XLOG_INFO, "Locked process pages in memory");
379 #endif /* HAVE_PLOCK || HAVE_MLOCKALL */
381 #if defined(HAVE_MADVISE) && defined(MADV_PROTECT)
382 madvise(0, 0, MADV_PROTECT); /* may be redundant of the above worked out */
383 #endif /* defined(HAVE_MADVISE) && defined(MADV_PROTECT) */
388 main(int argc, char *argv[])
390 char *domdot, *verstr, *vertmp;
393 char *progname = NULL; /* "amd" */
394 char hostname[MAXHOSTNAMELEN + 1] = "localhost"; /* Hostname */
397 * Make sure some built-in assumptions are true before we start
399 assert(sizeof(nfscookie) >= sizeof(u_int));
400 assert(sizeof(int) >= 4);
403 * Set processing status.
408 * Determine program name
411 progname = strrchr(argv[0], '/');
412 if (progname && progname[1])
419 am_set_progname(progname);
422 * Initialize process id. This is kept
423 * cached since it is used for generating
424 * and using file handles.
429 * Get local machine name
431 if (gethostname(hostname, sizeof(hostname)) < 0) {
432 plog(XLOG_FATAL, "gethostname: %m");
435 hostname[sizeof(hostname) - 1] = '\0';
438 * Check it makes sense
441 plog(XLOG_FATAL, "host name is not set");
446 * Initialize global options structure.
448 init_global_options();
451 * Partially initialize hostd[]. This
452 * is completed in get_args().
454 if ((domdot = strchr(hostname, '.'))) {
456 * Hostname already contains domainname.
457 * Split out hostname and domainname
463 xstrlcpy(hostd, hostname, sizeof(hostd));
464 am_set_hostname(hostname);
467 * Setup signal handlers
469 /* SIGINT: trap interrupts for shutdowns */
470 setup_sighandler(SIGINT, sigterm);
471 /* SIGTERM: trap terminate so we can shutdown cleanly (some chance) */
472 setup_sighandler(SIGTERM, sigterm);
473 /* SIGHUP: hangups tell us to reload the cache */
474 setup_sighandler(SIGHUP, sighup);
476 * SIGCHLD: trap Death-of-a-child. These allow us to pick up the exit
477 * status of backgrounded mounts. See "sched.c".
479 setup_sighandler(SIGCHLD, sigchld);
480 #ifdef HAVE_SIGACTION
481 /* construct global "masked_sigs" used in nfs_start.c */
482 sigemptyset(&masked_sigs);
483 sigaddset(&masked_sigs, SIGINT);
484 sigaddset(&masked_sigs, SIGTERM);
485 sigaddset(&masked_sigs, SIGHUP);
486 sigaddset(&masked_sigs, SIGCHLD);
487 #endif /* HAVE_SIGACTION */
490 * Fix-up any umask problems. Most systems default
491 * to 002 which is not too convenient for our purposes
493 orig_umask = umask(0);
496 * Figure out primary network name
498 getwire(&PrimNetName, &PrimNetNum);
501 * Determine command-line arguments
503 get_args(argc, argv);
506 * Log version information.
508 vertmp = get_version_string();
509 verstr = strtok(vertmp, "\n");
510 plog(XLOG_INFO, "AM-UTILS VERSION INFORMATION:");
512 plog(XLOG_INFO, "%s", verstr);
513 verstr = strtok(NULL, "\n");
518 * Get our own IP address so that we can mount the automounter. We pass
519 * localhost_address which could be used as the default localhost
520 * name/address in amu_get_myaddress().
522 amu_get_myaddress(&myipaddr, gopt.localhost_address);
523 plog(XLOG_INFO, "My ip addr is %s", inet_ntoa(myipaddr));
525 /* avoid hanging on other NFS servers if started elsewhere */
527 plog(XLOG_INFO, "cannot chdir to /: %m");
530 * Now check we are root.
532 if (geteuid() != 0) {
533 plog(XLOG_FATAL, "Must be root to mount filesystems (euid = %ld)", (long) geteuid());
539 * If the domain was specified then bind it here
540 * to circumvent any default bindings that may
541 * be done in the C library.
543 if (gopt.nis_domain && yp_bind(gopt.nis_domain)) {
544 plog(XLOG_FATAL, "Can't bind to NIS domain \"%s\"", gopt.nis_domain);
547 #endif /* HAVE_MAP_NIS */
549 if (!amuDebug(D_DAEMON))
550 ppid = daemon_mode();
553 * Lock process text and data segment in memory.
555 if (gopt.flags & CFM_PROCESS_LOCK) {
559 do_mapc_reload = clocktime(NULL) + gopt.map_reload_interval;
562 * Register automounter with system.
564 error = mount_automounter(ppid);
568 #ifdef HAVE_FS_AUTOFS
570 * XXX this should be part of going_down(), but I can't move it there
571 * because it would be calling non-library code from the library... ugh
574 destroy_autofs_service();
575 #endif /* HAVE_FS_AUTOFS */
580 return 1; /* should never get here */