]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
add -r flag to make mountd do reverse resolve of ipaddress on the fly
[nfs-utils.git] / utils / mountd / mountd.c
1 /*
2  * utils/mountd/mountd.c
3  *
4  * Authenticate mount requests and retrieve file handle.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <signal.h>
14 #include <sys/stat.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/resource.h>
24 #include <sys/wait.h>
25 #include "xmalloc.h"
26 #include "misc.h"
27 #include "mountd.h"
28 #include "rpcmisc.h"
29
30 extern void     cache_open(void);
31 extern struct nfs_fh_len *cache_get_filehandle(nfs_export *exp, int len, char *p);
32 extern void cache_export(nfs_export *exp);
33
34 extern void my_svc_run(void);
35
36 static void             usage(const char *, int exitcode);
37 static exports          get_exportlist(void);
38 static struct nfs_fh_len *get_rootfh(struct svc_req *, dirpath *, mountstat3 *, int v3);
39
40 int reverse_resolve = 0;
41 int new_cache = 0;
42
43 /* PRC: a high-availability callout program can be specified with -H
44  * When this is done, the program will receive callouts whenever clients
45  * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
46 char *ha_callout_prog = NULL;
47
48 /* Number of mountd threads to start.   Default is 1 and
49  * that's probably enough unless you need hundreds of
50  * clients to be able to mount at once.  */
51 static int num_threads = 1;
52 /* Arbitrary limit on number of threads */
53 #define MAX_THREADS 64
54
55 static struct option longopts[] =
56 {
57         { "foreground", 0, 0, 'F' },
58         { "descriptors", 1, 0, 'o' },
59         { "debug", 1, 0, 'd' },
60         { "help", 0, 0, 'h' },
61         { "exports-file", 1, 0, 'f' },
62         { "nfs-version", 1, 0, 'V' },
63         { "no-nfs-version", 1, 0, 'N' },
64         { "version", 0, 0, 'v' },
65         { "port", 1, 0, 'p' },
66         { "no-tcp", 0, 0, 'n' },
67         { "ha-callout", 1, 0, 'H' },
68         { "state-directory-path", 1, 0, 's' },
69         { "num-threads", 1, 0, 't' },
70         { NULL, 0, 0, 0 }
71 };
72
73 static int nfs_version = -1;
74
75 static void
76 unregister_services (void)
77 {
78         if (nfs_version & 0x1)
79                 pmap_unset (MOUNTPROG, MOUNTVERS);
80         if (nfs_version & (0x1 << 1))
81                 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
82         if (nfs_version & (0x1 << 2))
83                 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
84 }
85
86 /* Wait for all worker child processes to exit and reap them */
87 static void
88 wait_for_workers (void)
89 {
90         int status;
91         pid_t pid;
92
93         for (;;) {
94
95                 pid = waitpid(0, &status, 0);
96
97                 if (pid < 0) {
98                         if (errno == ECHILD)
99                                 return; /* no more children */
100                         xlog(L_FATAL, "mountd: can't wait: %s\n",
101                                         strerror(errno));
102                 }
103
104                 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
105                  * does not happen on 2.6 kernels, and waitpid() blocks
106                  * until all the children are dead then returns with
107                  * -ECHILD.  But, we don't need to do anything on the
108                  * death of individual workers, so we don't care. */
109                 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
110                                 (int)pid, status);
111         }
112 }
113
114 /* Fork num_threads worker children and wait for them */
115 static void
116 fork_workers(void)
117 {
118         int i;
119         pid_t pid;
120
121         xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
122
123         for (i = 0 ; i < num_threads ; i++) {
124                 pid = fork();
125                 if (pid < 0) {
126                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
127                                         strerror(errno));
128                 }
129                 if (pid == 0) {
130                         /* worker child */
131
132                         /* Re-enable the default action on SIGTERM et al
133                          * so that workers die naturally when sent them.
134                          * Only the parent unregisters with pmap and
135                          * hence needs to do special SIGTERM handling. */
136                         struct sigaction sa;
137                         sa.sa_handler = SIG_DFL;
138                         sa.sa_flags = 0;
139                         sigemptyset(&sa.sa_mask);
140                         sigaction(SIGHUP, &sa, NULL);
141                         sigaction(SIGINT, &sa, NULL);
142                         sigaction(SIGTERM, &sa, NULL);
143
144                         /* fall into my_svc_run in caller */
145                         return;
146                 }
147         }
148
149         /* in parent */
150         wait_for_workers();
151         unregister_services();
152         xlog(L_NOTICE, "mountd: no more workers, exiting\n");
153         exit(0);
154 }
155
156 /*
157  * Signal handler.
158  */
159 static void 
160 killer (int sig)
161 {
162         unregister_services();
163         if (num_threads > 1) {
164                 /* play Kronos and eat our children */
165                 kill(0, SIGTERM);
166                 wait_for_workers();
167         }
168         xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
169 }
170
171 static void
172 sig_hup (int sig)
173 {
174   /* don't exit on SIGHUP */
175   xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
176   return;
177 }
178
179 bool_t
180 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
181 {
182         return 1;
183 }
184
185 bool_t
186 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
187 {
188         struct nfs_fh_len *fh;
189
190         xlog(D_CALL, "MNT1(%s) called", *path);
191         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
192                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
193         return 1;
194 }
195
196 bool_t
197 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
198 {
199         struct sockaddr_in *addr =
200                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
201
202         if ((*res = mountlist_list()) == NULL)
203                 xlog(L_WARNING, "dump request from %s failed.",
204                         inet_ntoa(addr->sin_addr));
205
206         return 1;
207 }
208
209 bool_t
210 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
211 {
212         struct sockaddr_in *sin
213                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
214         nfs_export      *exp;
215         char            *p = *argp;
216         char            rpath[MAXPATHLEN+1];
217
218         if (*p == '\0')
219                 p = "/";
220
221         if (realpath(p, rpath) != NULL) {
222                 rpath[sizeof (rpath) - 1] = '\0';
223                 p = rpath;
224         }
225
226         if (!(exp = auth_authenticate("unmount", sin, p))) {
227                 return 1;
228         }
229
230         if (!new_cache)
231                 export_reset (exp);
232
233         mountlist_del(inet_ntoa(sin->sin_addr), p);
234         return 1;
235 }
236
237 bool_t
238 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
239 {
240         /* Reload /etc/xtab if necessary */
241         auth_reload();
242
243         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
244         return 1;
245 }
246
247 bool_t
248 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
249 {
250         struct sockaddr_in *addr =
251                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
252
253         if ((*resp = get_exportlist()) == NULL)
254                 xlog(L_WARNING, "export request from %s failed.",
255                         inet_ntoa(addr->sin_addr));
256                 
257         return 1;
258 }
259
260 bool_t
261 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
262 {
263         struct sockaddr_in *addr =
264                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
265
266         if ((*resp = get_exportlist()) == NULL)
267                 xlog(L_WARNING, "exportall request from %s failed.",
268                         inet_ntoa(addr->sin_addr));
269         return 1;
270 }
271
272 /*
273  * MNTv2 pathconf procedure
274  *
275  * The protocol doesn't include a status field, so Sun apparently considers
276  * it good practice to let anyone snoop on your system, even if it's
277  * pretty harmless data such as pathconf. We don't.
278  *
279  * Besides, many of the pathconf values don't make much sense on NFS volumes.
280  * FIFOs and tty device files represent devices on the *client*, so there's
281  * no point in getting the server's buffer sizes etc.
282  */
283 bool_t
284 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
285 {
286         struct sockaddr_in *sin
287                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
288         struct stat     stb;
289         nfs_export      *exp;
290         char            rpath[MAXPATHLEN+1];
291         char            *p = *path;
292
293         memset(res, 0, sizeof(*res));
294
295         if (*p == '\0')
296                 p = "/";
297
298         /* Reload /etc/xtab if necessary */
299         auth_reload();
300
301         /* Resolve symlinks */
302         if (realpath(p, rpath) != NULL) {
303                 rpath[sizeof (rpath) - 1] = '\0';
304                 p = rpath;
305         }
306
307         /* Now authenticate the intruder... */
308         if (!(exp = auth_authenticate("pathconf", sin, p))) {
309                 return 1;
310         } else if (stat(p, &stb) < 0) {
311                 xlog(L_WARNING, "can't stat exported dir %s: %s",
312                                 p, strerror(errno));
313                 export_reset (exp);
314                 return 1;
315         }
316
317         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
318         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
319         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
320         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
321         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
322         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
323         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
324
325         /* Can't figure out what to do with pc_mask */
326         res->pc_mask[0]   = 0;
327         res->pc_mask[1]   = 0;
328
329         export_reset (exp);
330
331         return 1;
332 }
333
334 /*
335  * NFSv3 MOUNT procedure
336  */
337 bool_t
338 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
339 {
340 #define AUTH_GSS_KRB5 390003
341 #define AUTH_GSS_KRB5I 390004
342 #define AUTH_GSS_KRB5P 390005
343         static int      flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
344         struct nfs_fh_len *fh;
345
346         xlog(D_CALL, "MNT3(%s) called", *path);
347         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
348                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
349
350                 ok->fhandle.fhandle3_len = fh->fh_size;
351                 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
352                 ok->auth_flavors.auth_flavors_len
353                         = sizeof(flavors)/sizeof(flavors[0]);
354                 ok->auth_flavors.auth_flavors_val = flavors;
355         }
356         return 1;
357 }
358
359 static struct nfs_fh_len *
360 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
361 {
362         struct sockaddr_in *sin =
363                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
364         struct stat     stb, estb;
365         nfs_export      *exp;
366         char            rpath[MAXPATHLEN+1];
367         char            *p = *path;
368
369         if (*p == '\0')
370                 p = "/";
371
372         /* Reload /var/lib/nfs/etab if necessary */
373         auth_reload();
374
375         /* Resolve symlinks */
376         if (realpath(p, rpath) != NULL) {
377                 rpath[sizeof (rpath) - 1] = '\0';
378                 p = rpath;
379         }
380
381         /* Now authenticate the intruder... */
382         if (!(exp = auth_authenticate("mount", sin, p))) {
383                 *error = NFSERR_ACCES;
384         } else if (stat(p, &stb) < 0) {
385                 xlog(L_WARNING, "can't stat exported dir %s: %s",
386                                 p, strerror(errno));
387                 if (errno == ENOENT)
388                         *error = NFSERR_NOENT;
389                 else
390                         *error = NFSERR_ACCES;
391         } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
392                 xlog(L_WARNING, "%s is not a directory or regular file", p);
393                 *error = NFSERR_NOTDIR;
394         } else if (stat(exp->m_export.e_path, &estb) < 0) {
395                 xlog(L_WARNING, "can't stat export point %s: %s",
396                      p, strerror(errno));
397                 *error = NFSERR_NOENT;
398         } else if (estb.st_dev != stb.st_dev
399                    /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
400                 ) {
401                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
402                      p, exp->m_export.e_path);
403                 *error = NFSERR_ACCES;
404         } else if (exp->m_export.e_mountpoint &&
405                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
406                                   exp->m_export.e_mountpoint:
407                                   exp->m_export.e_path)) {
408                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
409                      p);
410                 *error = NFSERR_NOENT;
411         } else if (new_cache) {
412                 /* This will be a static private nfs_export with just one
413                  * address.  We feed it to kernel then extract the filehandle,
414                  * 
415                  */
416                 struct nfs_fh_len  *fh;
417
418                 cache_export(exp);
419                 fh = cache_get_filehandle(exp, v3?64:32, p);
420                 if (fh == NULL) 
421                         *error = NFSERR_ACCES;
422                 else {
423                         *error = NFS_OK;
424                         mountlist_add(inet_ntoa(sin->sin_addr), p);
425                 }
426                 return fh;
427         } else {
428                 struct nfs_fh_len  *fh;
429
430                 if (exp->m_exported<1)
431                         export_export(exp);
432                 if (!exp->m_xtabent)
433                         xtab_append(exp);
434
435                 if (v3)
436                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
437                 if (!v3 || (fh == NULL && errno == EINVAL)) {
438                         /* We first try the new nfs syscall. */
439                         fh = getfh ((struct sockaddr *) sin, p);
440                         if (fh == NULL && errno == EINVAL)
441                                 /* Let's try the old one. */
442                                 fh = getfh_old ((struct sockaddr *) sin,
443                                                 stb.st_dev, stb.st_ino);
444                 }
445                 if (fh != NULL) {
446                         mountlist_add(inet_ntoa(sin->sin_addr), p);
447                         *error = NFS_OK;
448                         export_reset (exp);
449                         return fh;
450                 }
451                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
452                 *error = NFSERR_ACCES;
453         }
454         export_reset (exp);
455         return NULL;
456 }
457
458 static exports
459 get_exportlist(void)
460 {
461         static exports          elist = NULL;
462         struct exportnode       *e, *ne;
463         struct groupnode        *g, *ng, *c, **cp;
464         nfs_export              *exp;
465         int                     i;
466
467         if (!auth_reload() && elist)
468                 return elist;
469
470         for (e = elist; e != NULL; e = ne) {
471                 ne = e->ex_next;
472                 for (g = e->ex_groups; g != NULL; g = ng) {
473                         ng = g->gr_next;
474                         xfree(g->gr_name);
475                         xfree(g);
476                 }
477                 xfree(e->ex_dir);
478                 xfree(e);
479         }
480         elist = NULL;
481
482         for (i = 0; i < MCL_MAXTYPES; i++) {
483                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
484                         for (e = elist; e != NULL; e = e->ex_next) {
485                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
486                                         break;
487                         }
488                         if (!e) {
489                                 e = (struct exportnode *) xmalloc(sizeof(*e));
490                                 e->ex_next = elist;
491                                 e->ex_groups = NULL;
492                                 e->ex_dir = xstrdup(exp->m_export.m_path);
493                                 elist = e;
494                         }
495
496                         /* We need to check if we should remove
497                            previous ones. */
498                         if (i == MCL_ANONYMOUS && e->ex_groups) {
499                                 for (g = e->ex_groups; g; g = ng) {
500                                         ng = g->gr_next;
501                                         xfree(g->gr_name);
502                                         xfree(g);
503                                 }
504                                 e->ex_groups = NULL;
505                                 continue;
506                         }
507
508                         if (i != MCL_FQDN && e->ex_groups) {
509                           struct hostent        *hp;
510
511                           cp = &e->ex_groups;
512                           while ((c = *cp) != NULL) {
513                             if (client_gettype (c->gr_name) == MCL_FQDN
514                                 && (hp = gethostbyname(c->gr_name))) {
515                               hp = hostent_dup (hp);
516                               if (client_check(exp->m_client, hp)) {
517                                 *cp = c->gr_next;
518                                 xfree(c->gr_name);
519                                 xfree(c);
520                                 xfree (hp);
521                                 continue;
522                               }
523                               xfree (hp);
524                             }
525                             cp = &(c->gr_next);
526                           }
527                         }
528
529                         if (exp->m_export.e_hostname [0] != '\0') {
530                                 for (g = e->ex_groups; g; g = g->gr_next)
531                                         if (strcmp (exp->m_export.e_hostname,
532                                                     g->gr_name) == 0)
533                                                 break;
534                                 if (g)
535                                         continue;
536                                 g = (struct groupnode *) xmalloc(sizeof(*g));
537                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
538                                 g->gr_next = e->ex_groups;
539                                 e->ex_groups = g;
540                         }
541                 }
542         }
543
544         return elist;
545 }
546
547 int
548 main(int argc, char **argv)
549 {
550         char    *export_file = _PATH_EXPORTS;
551         char    *state_dir = NFS_STATEDIR;
552         int     foreground = 0;
553         int     port = 0;
554         int     descriptors = 0;
555         int     c;
556         struct sigaction sa;
557         struct rlimit rlim;
558
559         /* Parse the command line options and arguments. */
560         opterr = 0;
561         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:", longopts, NULL)) != EOF)
562                 switch (c) {
563                 case 'o':
564                         descriptors = atoi(optarg);
565                         if (descriptors <= 0) {
566                                 fprintf(stderr, "%s: bad descriptors: %s\n",
567                                         argv [0], optarg);
568                                 usage(argv [0], 1);
569                         }
570                         break;
571                 case 'F':
572                         foreground = 1;
573                         break;
574                 case 'd':
575                         xlog_sconfig(optarg, 1);
576                         break;
577                 case 'f':
578                         export_file = optarg;
579                         break;
580                 case 'H': /* PRC: specify a high-availability callout program */
581                         ha_callout_prog = optarg;
582                         break;
583                 case 'h':
584                         usage(argv [0], 0);
585                         break;
586                 case 'P':       /* XXX for nfs-server compatibility */
587                 case 'p':
588                         port = atoi(optarg);
589                         if (port <= 0 || port > 65535) {
590                                 fprintf(stderr, "%s: bad port number: %s\n",
591                                         argv [0], optarg);
592                                 usage(argv [0], 1);
593                         }
594                         break;
595                 case 'N':
596                         nfs_version &= ~(1 << (atoi (optarg) - 1));
597                         break;
598                 case 'n':
599                         _rpcfdtype = SOCK_DGRAM;
600                         break;
601                 case 'r':
602                         reverse_resolve = 1;
603                         break;
604                 case 's':
605                         if ((state_dir = xstrdup(optarg)) == NULL) {
606                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
607                                         argv[0], optarg);
608                                 exit(1);
609                         }
610                         break;
611                 case 't':
612                         num_threads = atoi (optarg);
613                         break;
614                 case 'V':
615                         nfs_version |= 1 << (atoi (optarg) - 1);
616                         break;
617                 case 'v':
618                         printf("kmountd %s\n", VERSION);
619                         exit(0);
620                 case 0:
621                         break;
622                 case '?':
623                 default:
624                         usage(argv [0], 1);
625                 }
626
627         /* No more arguments allowed. */
628         if (optind != argc || !(nfs_version & 0x7))
629                 usage(argv [0], 1);
630
631         if (chdir(state_dir)) {
632                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
633                         argv [0], state_dir, strerror(errno));
634                 exit(1);
635         }
636
637         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
638                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
639                                 argv [0], strerror(errno));
640         else {
641                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
642                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
643                     descriptors > FD_SETSIZE)
644                         descriptors = FD_SETSIZE;
645                 if (descriptors) {
646                         rlim.rlim_cur = descriptors;
647                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
648                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
649                                         argv [0], strerror(errno));
650                                 exit(1);
651                         }
652                 }
653         }
654         /* Initialize logging. */
655         if (!foreground) xlog_stderr(0);
656         xlog_open("mountd");
657
658         sa.sa_handler = SIG_IGN;
659         sa.sa_flags = 0;
660         sigemptyset(&sa.sa_mask);
661         sigaction(SIGHUP, &sa, NULL);
662         sigaction(SIGINT, &sa, NULL);
663         sigaction(SIGTERM, &sa, NULL);
664         sigaction(SIGPIPE, &sa, NULL);
665         /* WARNING: the following works on Linux and SysV, but not BSD! */
666         sigaction(SIGCHLD, &sa, NULL);
667
668         /* Daemons should close all extra filehandles ... *before* RPC init. */
669         if (!foreground)
670                 closeall(3);
671
672         new_cache = check_new_cache();
673         if (new_cache)
674                 cache_open();
675
676         if (nfs_version & 0x1)
677                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
678                          mount_dispatch, port);
679         if (nfs_version & (0x1 << 1))
680                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
681                          mount_dispatch, port);
682         if (nfs_version & (0x1 << 2))
683                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
684                          mount_dispatch, port);
685
686         sa.sa_handler = killer;
687         sigaction(SIGINT, &sa, NULL);
688         sigaction(SIGTERM, &sa, NULL);
689         sa.sa_handler = sig_hup;
690         sigaction(SIGHUP, &sa, NULL);
691
692         auth_init(export_file);
693
694         if (!foreground) {
695                 /* We first fork off a child. */
696                 if ((c = fork()) > 0)
697                         exit(0);
698                 if (c < 0) {
699                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
700                                                 strerror(errno));
701                 }
702                 /* Now we remove ourselves from the foreground.
703                    Redirect stdin/stdout/stderr first. */
704                 {
705                         int fd = open("/dev/null", O_RDWR);
706                         (void) dup2(fd, 0);
707                         (void) dup2(fd, 1);
708                         (void) dup2(fd, 2);
709                         if (fd > 2) (void) close(fd);
710                 }
711                 setsid();
712         }
713
714         /* silently bounds check num_threads */
715         if (foreground)
716                 num_threads = 1;
717         else if (num_threads < 1)
718                 num_threads = 1;
719         else if (num_threads > MAX_THREADS)
720                 num_threads = MAX_THREADS;
721
722         if (num_threads > 1)
723                 fork_workers();
724
725         my_svc_run();
726
727         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
728         exit(1);
729 }
730
731 static void
732 usage(const char *prog, int n)
733 {
734         fprintf(stderr,
735 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
736 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
737 "       [-p|--port port] [-V version|--nfs-version version]\n"
738 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
739 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
740 "       [-t num|--num-threads=num]\n", prog);
741         exit(n);
742 }