]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
c1d18d4cebc9b37b1f227d7de33fe1aaf4ccfaaf
[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 new_cache = 0;
41
42 /* PRC: a high-availability callout program can be specified with -H
43  * When this is done, the program will receive callouts whenever clients
44  * send mount or unmount requests -- the callout is not needed for 2.6 kernel */
45 char *ha_callout_prog = NULL;
46
47 /* Number of mountd threads to start.   Default is 1 and
48  * that's probably enough unless you need hundreds of
49  * clients to be able to mount at once.  */
50 static int num_threads = 1;
51 /* Arbitrary limit on number of threads */
52 #define MAX_THREADS 64
53
54 static struct option longopts[] =
55 {
56         { "foreground", 0, 0, 'F' },
57         { "descriptors", 1, 0, 'o' },
58         { "debug", 1, 0, 'd' },
59         { "help", 0, 0, 'h' },
60         { "exports-file", 1, 0, 'f' },
61         { "nfs-version", 1, 0, 'V' },
62         { "no-nfs-version", 1, 0, 'N' },
63         { "version", 0, 0, 'v' },
64         { "port", 1, 0, 'p' },
65         { "no-tcp", 0, 0, 'n' },
66         { "ha-callout", 1, 0, 'H' },
67         { "state-directory-path", 1, 0, 's' },
68         { "num-threads", 1, 0, 't' },
69         { NULL, 0, 0, 0 }
70 };
71
72 static int nfs_version = -1;
73
74 static void
75 unregister_services (void)
76 {
77         if (nfs_version & 0x1)
78                 pmap_unset (MOUNTPROG, MOUNTVERS);
79         if (nfs_version & (0x1 << 1))
80                 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
81         if (nfs_version & (0x1 << 2))
82                 pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
83 }
84
85 /* Wait for all worker child processes to exit and reap them */
86 static void
87 wait_for_workers (void)
88 {
89         int status;
90         pid_t pid;
91
92         for (;;) {
93
94                 pid = waitpid(0, &status, 0);
95
96                 if (pid < 0) {
97                         if (errno == ECHILD)
98                                 return; /* no more children */
99                         xlog(L_FATAL, "mountd: can't wait: %s\n",
100                                         strerror(errno));
101                 }
102
103                 /* Note: because we SIG_IGN'd SIGCHLD earlier, this
104                  * does not happen on 2.6 kernels, and waitpid() blocks
105                  * until all the children are dead then returns with
106                  * -ECHILD.  But, we don't need to do anything on the
107                  * death of individual workers, so we don't care. */
108                 xlog(L_NOTICE, "mountd: reaped child %d, status %d\n",
109                                 (int)pid, status);
110         }
111 }
112
113 /* Fork num_threads worker children and wait for them */
114 static void
115 fork_workers(void)
116 {
117         int i;
118         pid_t pid;
119
120         xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads);
121
122         for (i = 0 ; i < num_threads ; i++) {
123                 pid = fork();
124                 if (pid < 0) {
125                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
126                                         strerror(errno));
127                 }
128                 if (pid == 0) {
129                         /* worker child */
130
131                         /* Re-enable the default action on SIGTERM et al
132                          * so that workers die naturally when sent them.
133                          * Only the parent unregisters with pmap and
134                          * hence needs to do special SIGTERM handling. */
135                         struct sigaction sa;
136                         sa.sa_handler = SIG_DFL;
137                         sa.sa_flags = 0;
138                         sigemptyset(&sa.sa_mask);
139                         sigaction(SIGHUP, &sa, NULL);
140                         sigaction(SIGINT, &sa, NULL);
141                         sigaction(SIGTERM, &sa, NULL);
142
143                         /* fall into my_svc_run in caller */
144                         return;
145                 }
146         }
147
148         /* in parent */
149         wait_for_workers();
150         unregister_services();
151         xlog(L_NOTICE, "mountd: no more workers, exiting\n");
152         exit(0);
153 }
154
155 /*
156  * Signal handler.
157  */
158 static void 
159 killer (int sig)
160 {
161         unregister_services();
162         if (num_threads > 1) {
163                 /* play Kronos and eat our children */
164                 kill(0, SIGTERM);
165                 wait_for_workers();
166         }
167         xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
168 }
169
170 static void
171 sig_hup (int sig)
172 {
173   /* don't exit on SIGHUP */
174   xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n", sig);
175   return;
176 }
177
178 bool_t
179 mount_null_1_svc(struct svc_req *rqstp, void *argp, void *resp)
180 {
181         return 1;
182 }
183
184 bool_t
185 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
186 {
187         struct nfs_fh_len *fh;
188
189         xlog(D_CALL, "MNT1(%s) called", *path);
190         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 0)) != NULL)
191                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
192         return 1;
193 }
194
195 bool_t
196 mount_dump_1_svc(struct svc_req *rqstp, void *argp, mountlist *res)
197 {
198         struct sockaddr_in *addr =
199                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
200
201         if ((*res = mountlist_list()) == NULL)
202                 xlog(L_WARNING, "dump request from %s failed.",
203                         inet_ntoa(addr->sin_addr));
204
205         return 1;
206 }
207
208 bool_t
209 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
210 {
211         struct sockaddr_in *sin
212                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
213         nfs_export      *exp;
214         char            *p = *argp;
215         char            rpath[MAXPATHLEN+1];
216
217         if (*p == '\0')
218                 p = "/";
219
220         if (realpath(p, rpath) != NULL) {
221                 rpath[sizeof (rpath) - 1] = '\0';
222                 p = rpath;
223         }
224
225         if (!(exp = auth_authenticate("unmount", sin, p))) {
226                 return 1;
227         }
228         if (new_cache) {
229                 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
230                         mountlist_del(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
231                 mountlist_del(exp->m_client->m_hostname, p);
232         } else {
233                 mountlist_del(exp->m_client->m_hostname, p);
234                 export_reset (exp);
235         }
236         return 1;
237 }
238
239 bool_t
240 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
241 {
242         /* Reload /etc/xtab if necessary */
243         auth_reload();
244
245         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
246         return 1;
247 }
248
249 bool_t
250 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
251 {
252         struct sockaddr_in *addr =
253                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
254
255         if ((*resp = get_exportlist()) == NULL)
256                 xlog(L_WARNING, "export request from %s failed.",
257                         inet_ntoa(addr->sin_addr));
258                 
259         return 1;
260 }
261
262 bool_t
263 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
264 {
265         struct sockaddr_in *addr =
266                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
267
268         if ((*resp = get_exportlist()) == NULL)
269                 xlog(L_WARNING, "exportall request from %s failed.",
270                         inet_ntoa(addr->sin_addr));
271         return 1;
272 }
273
274 /*
275  * MNTv2 pathconf procedure
276  *
277  * The protocol doesn't include a status field, so Sun apparently considers
278  * it good practice to let anyone snoop on your system, even if it's
279  * pretty harmless data such as pathconf. We don't.
280  *
281  * Besides, many of the pathconf values don't make much sense on NFS volumes.
282  * FIFOs and tty device files represent devices on the *client*, so there's
283  * no point in getting the server's buffer sizes etc.
284  */
285 bool_t
286 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
287 {
288         struct sockaddr_in *sin
289                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
290         struct stat     stb;
291         nfs_export      *exp;
292         char            rpath[MAXPATHLEN+1];
293         char            *p = *path;
294
295         memset(res, 0, sizeof(*res));
296
297         if (*p == '\0')
298                 p = "/";
299
300         /* Reload /etc/xtab if necessary */
301         auth_reload();
302
303         /* Resolve symlinks */
304         if (realpath(p, rpath) != NULL) {
305                 rpath[sizeof (rpath) - 1] = '\0';
306                 p = rpath;
307         }
308
309         /* Now authenticate the intruder... */
310         if (!(exp = auth_authenticate("pathconf", sin, p))) {
311                 return 1;
312         } else if (stat(p, &stb) < 0) {
313                 xlog(L_WARNING, "can't stat exported dir %s: %s",
314                                 p, strerror(errno));
315                 export_reset (exp);
316                 return 1;
317         }
318
319         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
320         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
321         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
322         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
323         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
324         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
325         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
326
327         /* Can't figure out what to do with pc_mask */
328         res->pc_mask[0]   = 0;
329         res->pc_mask[1]   = 0;
330
331         export_reset (exp);
332
333         return 1;
334 }
335
336 /*
337  * NFSv3 MOUNT procedure
338  */
339 bool_t
340 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
341 {
342 #define AUTH_GSS_KRB5 390003
343 #define AUTH_GSS_KRB5I 390004
344 #define AUTH_GSS_KRB5P 390005
345         static int      flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
346         struct nfs_fh_len *fh;
347
348         xlog(D_CALL, "MNT3(%s) called", *path);
349         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
350                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
351
352                 ok->fhandle.fhandle3_len = fh->fh_size;
353                 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
354                 ok->auth_flavors.auth_flavors_len
355                         = sizeof(flavors)/sizeof(flavors[0]);
356                 ok->auth_flavors.auth_flavors_val = flavors;
357         }
358         return 1;
359 }
360
361 static struct nfs_fh_len *
362 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
363 {
364         struct sockaddr_in *sin =
365                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
366         struct stat     stb, estb;
367         nfs_export      *exp;
368         char            rpath[MAXPATHLEN+1];
369         char            *p = *path;
370
371         if (*p == '\0')
372                 p = "/";
373
374         /* Reload /var/lib/nfs/etab if necessary */
375         auth_reload();
376
377         /* Resolve symlinks */
378         if (realpath(p, rpath) != NULL) {
379                 rpath[sizeof (rpath) - 1] = '\0';
380                 p = rpath;
381         }
382
383         /* Now authenticate the intruder... */
384         if (!(exp = auth_authenticate("mount", sin, p))) {
385                 *error = NFSERR_ACCES;
386         } else if (stat(p, &stb) < 0) {
387                 xlog(L_WARNING, "can't stat exported dir %s: %s",
388                                 p, strerror(errno));
389                 if (errno == ENOENT)
390                         *error = NFSERR_NOENT;
391                 else
392                         *error = NFSERR_ACCES;
393         } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
394                 xlog(L_WARNING, "%s is not a directory or regular file", p);
395                 *error = NFSERR_NOTDIR;
396         } else if (stat(exp->m_export.e_path, &estb) < 0) {
397                 xlog(L_WARNING, "can't stat export point %s: %s",
398                      p, strerror(errno));
399                 *error = NFSERR_NOENT;
400         } else if (estb.st_dev != stb.st_dev
401                    /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
402                 ) {
403                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
404                      p, exp->m_export.e_path);
405                 *error = NFSERR_ACCES;
406         } else if (exp->m_export.e_mountpoint &&
407                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
408                                   exp->m_export.e_mountpoint:
409                                   exp->m_export.e_path)) {
410                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
411                      p);
412                 *error = NFSERR_NOENT;
413         } else if (new_cache) {
414                 /* This will be a static private nfs_export with just one
415                  * address.  We feed it to kernel then extract the filehandle,
416                  * 
417                  */
418                 struct nfs_fh_len  *fh;
419
420                 cache_export(exp);
421                 fh = cache_get_filehandle(exp, v3?64:32, p);
422                 if (fh == NULL) 
423                         *error = NFSERR_ACCES;
424                 else
425                         *error = NFS_OK;
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(exp->m_client->m_hostname, 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:n:Fd:f:p:P:hH:N:V:vs: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 's':
602                         if ((state_dir = xstrdup(optarg)) == NULL) {
603                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
604                                         argv[0], optarg);
605                                 exit(1);
606                         }
607                         break;
608                 case 't':
609                         num_threads = atoi (optarg);
610                         break;
611                 case 'V':
612                         nfs_version |= 1 << (atoi (optarg) - 1);
613                         break;
614                 case 'v':
615                         printf("kmountd %s\n", VERSION);
616                         exit(0);
617                 case 0:
618                         break;
619                 case '?':
620                 default:
621                         usage(argv [0], 1);
622                 }
623
624         /* No more arguments allowed. */
625         if (optind != argc || !(nfs_version & 0x7))
626                 usage(argv [0], 1);
627
628         if (chdir(state_dir)) {
629                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
630                         argv [0], state_dir, strerror(errno));
631                 exit(1);
632         }
633
634         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
635                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
636                                 argv [0], strerror(errno));
637         else {
638                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
639                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
640                     descriptors > FD_SETSIZE)
641                         descriptors = FD_SETSIZE;
642                 if (descriptors) {
643                         rlim.rlim_cur = descriptors;
644                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
645                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
646                                         argv [0], strerror(errno));
647                                 exit(1);
648                         }
649                 }
650         }
651         /* Initialize logging. */
652         if (!foreground) xlog_stderr(0);
653         xlog_open("mountd");
654
655         sa.sa_handler = SIG_IGN;
656         sa.sa_flags = 0;
657         sigemptyset(&sa.sa_mask);
658         sigaction(SIGHUP, &sa, NULL);
659         sigaction(SIGINT, &sa, NULL);
660         sigaction(SIGTERM, &sa, NULL);
661         sigaction(SIGPIPE, &sa, NULL);
662         /* WARNING: the following works on Linux and SysV, but not BSD! */
663         sigaction(SIGCHLD, &sa, NULL);
664
665         /* Daemons should close all extra filehandles ... *before* RPC init. */
666         if (!foreground)
667                 closeall(3);
668
669         new_cache = check_new_cache();
670         if (new_cache)
671                 cache_open();
672
673         if (nfs_version & 0x1)
674                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
675                          mount_dispatch, port);
676         if (nfs_version & (0x1 << 1))
677                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
678                          mount_dispatch, port);
679         if (nfs_version & (0x1 << 2))
680                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
681                          mount_dispatch, port);
682
683         sa.sa_handler = killer;
684         sigaction(SIGINT, &sa, NULL);
685         sigaction(SIGTERM, &sa, NULL);
686         sa.sa_handler = sig_hup;
687         sigaction(SIGHUP, &sa, NULL);
688
689         auth_init(export_file);
690
691         if (!foreground) {
692                 /* We first fork off a child. */
693                 if ((c = fork()) > 0)
694                         exit(0);
695                 if (c < 0) {
696                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
697                                                 strerror(errno));
698                 }
699                 /* Now we remove ourselves from the foreground.
700                    Redirect stdin/stdout/stderr first. */
701                 {
702                         int fd = open("/dev/null", O_RDWR);
703                         (void) dup2(fd, 0);
704                         (void) dup2(fd, 1);
705                         (void) dup2(fd, 2);
706                         if (fd > 2) (void) close(fd);
707                 }
708                 setsid();
709         }
710
711         /* silently bounds check num_threads */
712         if (foreground)
713                 num_threads = 1;
714         else if (num_threads < 1)
715                 num_threads = 1;
716         else if (num_threads > MAX_THREADS)
717                 num_threads = MAX_THREADS;
718
719         if (num_threads > 1)
720                 fork_workers();
721
722         my_svc_run();
723
724         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
725         exit(1);
726 }
727
728 static void
729 usage(const char *prog, int n)
730 {
731         fprintf(stderr,
732 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
733 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
734 "       [-p|--port port] [-V version|--nfs-version version]\n"
735 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
736 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
737 "       [-t num|--num-threads=num]\n", prog);
738         exit(n);
739 }