2 * Copyright (c) 1997-2004 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
41 * $Id: amd.c,v 1.8.2.6 2004/01/06 03:15:16 ezk Exp $
52 #endif /* HAVE_CONFIG_H */
56 struct amu_global_options gopt; /* where global options are stored */
58 char pid_fsname[16 + MAXHOSTNAMELEN]; /* "kiska.southseas.nz:(pid%d)" */
59 char *hostdomain = "unknown.domain";
60 char hostd[2 * MAXHOSTNAMELEN + 1]; /* Host+domain */
61 char *endian = ARCH_ENDIAN; /* Big or Little endian */
62 char *cpu = HOST_CPU; /* CPU type */
63 char *PrimNetName; /* name of primary network */
64 char *PrimNetNum; /* number of primary network */
66 int immediate_abort; /* Should close-down unmounts be retried */
68 int select_intr_valid;
71 struct amd_stats amd_stats; /* Server statistics */
72 struct in_addr myipaddr; /* (An) IP address of this host */
73 time_t do_mapc_reload = 0; /* mapc_reload() call required? */
77 #endif /* HAVE_SIGACTION */
82 * SIGINT - tells amd to do a full shutdown, including unmounting all
84 * SIGTERM - tells amd to shutdown now. Just unmounts the automount nodes.
89 #ifdef REINSTALL_SIGNAL_HANDLER
91 #endif /* REINSTALL_SIGNAL_HANDLER */
100 /* fall through... */
103 plog(XLOG_WARNING, "WARNING: automounter going down on signal %d", sig);
106 if (select_intr_valid)
107 longjmp(select_intr, sig);
112 * Hook for cache reload.
113 * When a SIGHUP arrives it schedules a call to mapc_reload
118 #ifdef REINSTALL_SIGNAL_HANDLER
120 #endif /* REINSTALL_SIGNAL_HANDLER */
124 dlog("spurious call to sighup");
127 * Force a reload by zero'ing the timer
129 if (amd_state == Run)
146 #ifdef HAVE_SIGACTION
147 struct sigaction sa, osa;
149 sa.sa_handler = parent_exit;
151 sigemptyset(&(sa.sa_mask));
152 sigaddset(&(sa.sa_mask), SIGQUIT);
153 sigaction(SIGQUIT, &sa, &osa);
154 #else /* not HAVE_SIGACTION */
155 signal(SIGQUIT, parent_exit);
156 #endif /* not HAVE_SIGACTION */
158 bgpid = background();
162 * Now wait for the automount points to
167 /* should never reach here */
169 #ifdef HAVE_SIGACTION
170 sigaction(SIGQUIT, &osa, NULL);
171 #else /* not HAVE_SIGACTION */
172 signal(SIGQUIT, SIG_DFL);
173 #endif /* not HAVE_SIGACTION */
176 * Record our pid to make it easier to kill the correct amd.
178 if (gopt.flags & CFM_PRINT_PID) {
179 if (STREQ(gopt.pid_file, "/dev/stdout")) {
180 printf("%ld\n", (long) am_mypid);
181 /* flush stdout, just in case */
185 mode_t prev_umask = umask(0022); /* set secure temporary umask */
187 f = fopen(gopt.pid_file, "w");
189 fprintf(f, "%ld\n", (long) am_mypid);
192 fprintf(stderr, "cannot open %s (errno=%d)\n", gopt.pid_file, errno);
194 umask(prev_umask); /* restore umask */
199 * Pretend we are in the foreground again
204 * Dissociate from the controlling terminal
206 amu_release_controlling_tty();
213 * Initialize global options structure.
216 init_global_options(void)
218 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
219 static struct utsname un;
220 #endif /* defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) */
222 memset(&gopt, 0, sizeof(struct amu_global_options));
224 /* name of current architecture */
225 gopt.arch = HOST_ARCH;
227 /* automounter temp dir */
228 gopt.auto_dir = "/.amd_mnt";
234 * kernel architecture: this you must get from uname() if possible.
236 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
238 gopt.karch = un.machine;
240 #endif /* defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) */
241 gopt.karch = HOST_ARCH;
246 /* operating system name */
247 gopt.op_sys = HOST_OS_NAME;
250 gopt.op_sys_ver = HOST_OS_VERSION;
252 /* full OS name and version */
253 gopt.op_sys_full = HOST_OS;
256 gopt.op_sys_vendor = HOST_VENDOR;
259 gopt.pid_file = "/dev/stdout";
262 gopt.sub_domain = NULL;
264 /* NFS retransmit counter */
265 gopt.amfs_auto_retrans = -1;
267 /* NFS retry interval */
268 gopt.amfs_auto_timeo = -1;
271 gopt.am_timeo = AM_TTL;
273 /* dismount interval */
274 gopt.am_timeo_w = AM_TTL_W;
277 * various CFM_* flags.
278 * by default, only the "plock" option is on (if available).
280 gopt.flags = CFM_PROCESS_LOCK;
282 #ifdef HAVE_MAP_HESIOD
283 /* Hesiod rhs zone */
284 gopt.hesiod_base = "automount";
285 #endif /* HAVE_MAP_HESIOD */
289 gopt.ldap_base = NULL;
291 /* LDAP host ports */
292 gopt.ldap_hostports = NULL;
295 gopt.ldap_cache_seconds = 0;
296 gopt.ldap_cache_maxmem = 131072;
297 #endif /* HAVE_MAP_LDAP */
301 gopt.nis_domain = NULL;
302 #endif /* HAVE_MAP_NIS */
307 main(int argc, char *argv[])
309 char *domdot, *verstr;
312 char *progname = NULL; /* "amd" */
313 char hostname[MAXHOSTNAMELEN + 1] = "localhost"; /* Hostname */
314 #ifdef HAVE_SIGACTION
316 #endif /* HAVE_SIGACTION */
319 * Make sure some built-in assumptions are true before we start
321 assert(sizeof(nfscookie) >= sizeof(u_int));
322 assert(sizeof(int) >= 4);
325 * Set processing status.
330 * Determine program name
333 progname = strrchr(argv[0], '/');
334 if (progname && progname[1])
341 am_set_progname(progname);
344 * Initialize process id. This is kept
345 * cached since it is used for generating
346 * and using file handles.
351 * Get local machine name
353 if (gethostname(hostname, sizeof(hostname)) < 0) {
354 plog(XLOG_FATAL, "gethostname: %m");
357 hostname[sizeof(hostname) - 1] = '\0';
360 * Check it makes sense
363 plog(XLOG_FATAL, "host name is not set");
368 /* initialize debugging flags (Register AMQ, Enter daemon mode) */
369 debug_flags = D_AMQ | D_DAEMON;
373 * Initialize global options structure.
375 init_global_options();
378 * Partially initialize hostd[]. This
379 * is completed in get_args().
381 if ((domdot = strchr(hostname, '.'))) {
383 * Hostname already contains domainname.
384 * Split out hostname and domainname
390 strcpy(hostd, hostname);
391 am_set_hostname(hostname);
394 * Trap interrupts for shutdowns.
396 #ifdef HAVE_SIGACTION
397 sa.sa_handler = sigterm;
399 sigemptyset(&(sa.sa_mask));
400 sigaddset(&(sa.sa_mask), SIGINT);
401 sigaddset(&(sa.sa_mask), SIGTERM);
402 sigaction(SIGINT, &sa, NULL);
403 sigaction(SIGTERM, &sa, NULL);
404 #else /* not HAVE_SIGACTION */
405 (void) signal(SIGINT, sigterm);
406 #endif /* not HAVE_SIGACTION */
409 * Trap Terminate so that we can shutdown gracefully (some chance)
411 #ifdef HAVE_SIGACTION
412 sa.sa_handler = sigterm;
414 sigemptyset(&(sa.sa_mask));
415 sigaddset(&(sa.sa_mask), SIGTERM);
416 sigaction(SIGTERM, &sa, NULL);
417 #else /* not HAVE_SIGACTION */
418 (void) signal(SIGTERM, sigterm);
419 #endif /* not HAVE_SIGACTION */
422 * Hangups tell us to reload the cache
424 #ifdef HAVE_SIGACTION
425 sa.sa_handler = sighup;
427 sigemptyset(&(sa.sa_mask));
428 sigaddset(&(sa.sa_mask), SIGHUP);
429 sigaction(SIGHUP, &sa, NULL);
430 #else /* not HAVE_SIGACTION */
431 (void) signal(SIGHUP, sighup);
432 #endif /* not HAVE_SIGACTION */
435 * Trap Death-of-a-child. These allow us to
436 * pick up the exit status of backgrounded mounts.
439 #ifdef HAVE_SIGACTION
440 sa.sa_handler = sigchld;
442 sigemptyset(&(sa.sa_mask));
443 sigaddset(&(sa.sa_mask), SIGCHLD);
444 sigaction(SIGCHLD, &sa, NULL);
447 * construct global "masked_sigs" used in nfs_start.c
449 sigemptyset(&masked_sigs);
450 sigaddset(&masked_sigs, SIGHUP);
451 sigaddset(&masked_sigs, SIGCHLD);
452 sigaddset(&masked_sigs, SIGTERM);
453 sigaddset(&masked_sigs, SIGINT);
454 #else /* not HAVE_SIGACTION */
455 (void) signal(SIGCHLD, sigchld);
456 #endif /* not HAVE_SIGACTION */
459 * Fix-up any umask problems. Most systems default
460 * to 002 which is not too convenient for our purposes
462 orig_umask = umask(0);
465 * Figure out primary network name
467 getwire(&PrimNetName, &PrimNetNum);
470 * Determine command-line arguments
472 get_args(argc, argv);
475 * Log version information.
477 verstr = strtok(get_version_string(), "\n");
478 plog(XLOG_INFO, "AM-UTILS VERSION INFORMATION:");
480 plog(XLOG_INFO, "%s", verstr);
481 verstr = strtok(NULL, "\n");
485 * Get our own IP address so that we
486 * can mount the automounter.
488 amu_get_myaddress(&myipaddr);
489 plog(XLOG_INFO, "My ip addr is %s", inet_ntoa(myipaddr));
491 /* avoid hanging on other NFS servers if started elsewhere */
493 plog(XLOG_INFO, "cannot chdir to /: %m");
496 * Now check we are root.
498 if (geteuid() != 0) {
499 plog(XLOG_FATAL, "Must be root to mount filesystems (euid = %ld)", (long) geteuid());
504 * Lock process text and data segment in memory.
507 if (gopt.flags & CFM_PROCESS_LOCK) {
510 * On AIX you must lower the stack size using ulimit() before calling
511 * plock. Otherwise plock will reserve a lot of memory space based on
512 * your maximum stack size limit. Since it is not easily possible to
513 * tell what should the limit be, I print a warning before calling
514 * plock(). See the manual pages for ulimit(1,3,4) on your AIX system.
516 plog(XLOG_WARNING, "AIX: may need to lower stack size using ulimit(3) before calling plock");
518 if (plock(PROCLOCK) != 0) {
519 plog(XLOG_WARNING, "Couldn't lock process text and data segment in memory: %m");
521 plog(XLOG_INFO, "Locked process text and data segment in memory");
524 #endif /* HAVE_PLOCK */
528 * If the domain was specified then bind it here
529 * to circumvent any default bindings that may
530 * be done in the C library.
532 if (gopt.nis_domain && yp_bind(gopt.nis_domain)) {
533 plog(XLOG_FATAL, "Can't bind to NIS domain \"%s\"", gopt.nis_domain);
536 #endif /* HAVE_MAP_NIS */
541 ppid = daemon_mode();
543 sprintf(pid_fsname, "%s:(pid%ld)", am_get_hostname(), (long) am_mypid);
545 do_mapc_reload = clocktime() + ONE_HOUR;
548 * Register automounter with system.
550 error = mount_automounter(ppid);
556 return 1; /* should never get here */