]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
remove extraneous mountlist_add/del calls and change remaining ones to use IP addresses
[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
229         if (!new_cache)
230                 export_reset (exp);
231
232         mountlist_del(inet_ntoa(sin->sin_addr), p);
233         return 1;
234 }
235
236 bool_t
237 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
238 {
239         /* Reload /etc/xtab if necessary */
240         auth_reload();
241
242         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
243         return 1;
244 }
245
246 bool_t
247 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
248 {
249         struct sockaddr_in *addr =
250                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
251
252         if ((*resp = get_exportlist()) == NULL)
253                 xlog(L_WARNING, "export request from %s failed.",
254                         inet_ntoa(addr->sin_addr));
255                 
256         return 1;
257 }
258
259 bool_t
260 mount_exportall_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
261 {
262         struct sockaddr_in *addr =
263                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
264
265         if ((*resp = get_exportlist()) == NULL)
266                 xlog(L_WARNING, "exportall request from %s failed.",
267                         inet_ntoa(addr->sin_addr));
268         return 1;
269 }
270
271 /*
272  * MNTv2 pathconf procedure
273  *
274  * The protocol doesn't include a status field, so Sun apparently considers
275  * it good practice to let anyone snoop on your system, even if it's
276  * pretty harmless data such as pathconf. We don't.
277  *
278  * Besides, many of the pathconf values don't make much sense on NFS volumes.
279  * FIFOs and tty device files represent devices on the *client*, so there's
280  * no point in getting the server's buffer sizes etc.
281  */
282 bool_t
283 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
284 {
285         struct sockaddr_in *sin
286                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
287         struct stat     stb;
288         nfs_export      *exp;
289         char            rpath[MAXPATHLEN+1];
290         char            *p = *path;
291
292         memset(res, 0, sizeof(*res));
293
294         if (*p == '\0')
295                 p = "/";
296
297         /* Reload /etc/xtab if necessary */
298         auth_reload();
299
300         /* Resolve symlinks */
301         if (realpath(p, rpath) != NULL) {
302                 rpath[sizeof (rpath) - 1] = '\0';
303                 p = rpath;
304         }
305
306         /* Now authenticate the intruder... */
307         if (!(exp = auth_authenticate("pathconf", sin, p))) {
308                 return 1;
309         } else if (stat(p, &stb) < 0) {
310                 xlog(L_WARNING, "can't stat exported dir %s: %s",
311                                 p, strerror(errno));
312                 export_reset (exp);
313                 return 1;
314         }
315
316         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
317         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
318         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
319         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
320         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
321         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
322         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
323
324         /* Can't figure out what to do with pc_mask */
325         res->pc_mask[0]   = 0;
326         res->pc_mask[1]   = 0;
327
328         export_reset (exp);
329
330         return 1;
331 }
332
333 /*
334  * NFSv3 MOUNT procedure
335  */
336 bool_t
337 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
338 {
339 #define AUTH_GSS_KRB5 390003
340 #define AUTH_GSS_KRB5I 390004
341 #define AUTH_GSS_KRB5P 390005
342         static int      flavors[] = { AUTH_NULL, AUTH_UNIX, AUTH_GSS_KRB5, AUTH_GSS_KRB5I, AUTH_GSS_KRB5P};
343         struct nfs_fh_len *fh;
344
345         xlog(D_CALL, "MNT3(%s) called", *path);
346         if ((fh = get_rootfh(rqstp, path, &res->fhs_status, 1)) != NULL) {
347                 struct mountres3_ok     *ok = &res->mountres3_u.mountinfo;
348
349                 ok->fhandle.fhandle3_len = fh->fh_size;
350                 ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
351                 ok->auth_flavors.auth_flavors_len
352                         = sizeof(flavors)/sizeof(flavors[0]);
353                 ok->auth_flavors.auth_flavors_val = flavors;
354         }
355         return 1;
356 }
357
358 static struct nfs_fh_len *
359 get_rootfh(struct svc_req *rqstp, dirpath *path, mountstat3 *error, int v3)
360 {
361         struct sockaddr_in *sin =
362                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
363         struct stat     stb, estb;
364         nfs_export      *exp;
365         char            rpath[MAXPATHLEN+1];
366         char            *p = *path;
367
368         if (*p == '\0')
369                 p = "/";
370
371         /* Reload /var/lib/nfs/etab if necessary */
372         auth_reload();
373
374         /* Resolve symlinks */
375         if (realpath(p, rpath) != NULL) {
376                 rpath[sizeof (rpath) - 1] = '\0';
377                 p = rpath;
378         }
379
380         /* Now authenticate the intruder... */
381         if (!(exp = auth_authenticate("mount", sin, p))) {
382                 *error = NFSERR_ACCES;
383         } else if (stat(p, &stb) < 0) {
384                 xlog(L_WARNING, "can't stat exported dir %s: %s",
385                                 p, strerror(errno));
386                 if (errno == ENOENT)
387                         *error = NFSERR_NOENT;
388                 else
389                         *error = NFSERR_ACCES;
390         } else if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
391                 xlog(L_WARNING, "%s is not a directory or regular file", p);
392                 *error = NFSERR_NOTDIR;
393         } else if (stat(exp->m_export.e_path, &estb) < 0) {
394                 xlog(L_WARNING, "can't stat export point %s: %s",
395                      p, strerror(errno));
396                 *error = NFSERR_NOENT;
397         } else if (estb.st_dev != stb.st_dev
398                    /* && (!new_cache || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) */
399                 ) {
400                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
401                      p, exp->m_export.e_path);
402                 *error = NFSERR_ACCES;
403         } else if (exp->m_export.e_mountpoint &&
404                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
405                                   exp->m_export.e_mountpoint:
406                                   exp->m_export.e_path)) {
407                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
408                      p);
409                 *error = NFSERR_NOENT;
410         } else if (new_cache) {
411                 /* This will be a static private nfs_export with just one
412                  * address.  We feed it to kernel then extract the filehandle,
413                  * 
414                  */
415                 struct nfs_fh_len  *fh;
416
417                 cache_export(exp);
418                 fh = cache_get_filehandle(exp, v3?64:32, p);
419                 if (fh == NULL) 
420                         *error = NFSERR_ACCES;
421                 else {
422                         *error = NFS_OK;
423                         mountlist_add(inet_ntoa(sin->sin_addr), p);
424                 }
425                 return fh;
426         } else {
427                 struct nfs_fh_len  *fh;
428
429                 if (exp->m_exported<1)
430                         export_export(exp);
431                 if (!exp->m_xtabent)
432                         xtab_append(exp);
433
434                 if (v3)
435                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
436                 if (!v3 || (fh == NULL && errno == EINVAL)) {
437                         /* We first try the new nfs syscall. */
438                         fh = getfh ((struct sockaddr *) sin, p);
439                         if (fh == NULL && errno == EINVAL)
440                                 /* Let's try the old one. */
441                                 fh = getfh_old ((struct sockaddr *) sin,
442                                                 stb.st_dev, stb.st_ino);
443                 }
444                 if (fh != NULL) {
445                         mountlist_add(inet_ntoa(sin->sin_addr), p);
446                         *error = NFS_OK;
447                         export_reset (exp);
448                         return fh;
449                 }
450                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
451                 *error = NFSERR_ACCES;
452         }
453         export_reset (exp);
454         return NULL;
455 }
456
457 static exports
458 get_exportlist(void)
459 {
460         static exports          elist = NULL;
461         struct exportnode       *e, *ne;
462         struct groupnode        *g, *ng, *c, **cp;
463         nfs_export              *exp;
464         int                     i;
465
466         if (!auth_reload() && elist)
467                 return elist;
468
469         for (e = elist; e != NULL; e = ne) {
470                 ne = e->ex_next;
471                 for (g = e->ex_groups; g != NULL; g = ng) {
472                         ng = g->gr_next;
473                         xfree(g->gr_name);
474                         xfree(g);
475                 }
476                 xfree(e->ex_dir);
477                 xfree(e);
478         }
479         elist = NULL;
480
481         for (i = 0; i < MCL_MAXTYPES; i++) {
482                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
483                         for (e = elist; e != NULL; e = e->ex_next) {
484                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
485                                         break;
486                         }
487                         if (!e) {
488                                 e = (struct exportnode *) xmalloc(sizeof(*e));
489                                 e->ex_next = elist;
490                                 e->ex_groups = NULL;
491                                 e->ex_dir = xstrdup(exp->m_export.m_path);
492                                 elist = e;
493                         }
494
495                         /* We need to check if we should remove
496                            previous ones. */
497                         if (i == MCL_ANONYMOUS && e->ex_groups) {
498                                 for (g = e->ex_groups; g; g = ng) {
499                                         ng = g->gr_next;
500                                         xfree(g->gr_name);
501                                         xfree(g);
502                                 }
503                                 e->ex_groups = NULL;
504                                 continue;
505                         }
506
507                         if (i != MCL_FQDN && e->ex_groups) {
508                           struct hostent        *hp;
509
510                           cp = &e->ex_groups;
511                           while ((c = *cp) != NULL) {
512                             if (client_gettype (c->gr_name) == MCL_FQDN
513                                 && (hp = gethostbyname(c->gr_name))) {
514                               hp = hostent_dup (hp);
515                               if (client_check(exp->m_client, hp)) {
516                                 *cp = c->gr_next;
517                                 xfree(c->gr_name);
518                                 xfree(c);
519                                 xfree (hp);
520                                 continue;
521                               }
522                               xfree (hp);
523                             }
524                             cp = &(c->gr_next);
525                           }
526                         }
527
528                         if (exp->m_export.e_hostname [0] != '\0') {
529                                 for (g = e->ex_groups; g; g = g->gr_next)
530                                         if (strcmp (exp->m_export.e_hostname,
531                                                     g->gr_name) == 0)
532                                                 break;
533                                 if (g)
534                                         continue;
535                                 g = (struct groupnode *) xmalloc(sizeof(*g));
536                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
537                                 g->gr_next = e->ex_groups;
538                                 e->ex_groups = g;
539                         }
540                 }
541         }
542
543         return elist;
544 }
545
546 int
547 main(int argc, char **argv)
548 {
549         char    *export_file = _PATH_EXPORTS;
550         char    *state_dir = NFS_STATEDIR;
551         int     foreground = 0;
552         int     port = 0;
553         int     descriptors = 0;
554         int     c;
555         struct sigaction sa;
556         struct rlimit rlim;
557
558         /* Parse the command line options and arguments. */
559         opterr = 0;
560         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vs:t:", longopts, NULL)) != EOF)
561                 switch (c) {
562                 case 'o':
563                         descriptors = atoi(optarg);
564                         if (descriptors <= 0) {
565                                 fprintf(stderr, "%s: bad descriptors: %s\n",
566                                         argv [0], optarg);
567                                 usage(argv [0], 1);
568                         }
569                         break;
570                 case 'F':
571                         foreground = 1;
572                         break;
573                 case 'd':
574                         xlog_sconfig(optarg, 1);
575                         break;
576                 case 'f':
577                         export_file = optarg;
578                         break;
579                 case 'H': /* PRC: specify a high-availability callout program */
580                         ha_callout_prog = optarg;
581                         break;
582                 case 'h':
583                         usage(argv [0], 0);
584                         break;
585                 case 'P':       /* XXX for nfs-server compatibility */
586                 case 'p':
587                         port = atoi(optarg);
588                         if (port <= 0 || port > 65535) {
589                                 fprintf(stderr, "%s: bad port number: %s\n",
590                                         argv [0], optarg);
591                                 usage(argv [0], 1);
592                         }
593                         break;
594                 case 'N':
595                         nfs_version &= ~(1 << (atoi (optarg) - 1));
596                         break;
597                 case 'n':
598                         _rpcfdtype = SOCK_DGRAM;
599                         break;
600                 case 's':
601                         if ((state_dir = xstrdup(optarg)) == NULL) {
602                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
603                                         argv[0], optarg);
604                                 exit(1);
605                         }
606                         break;
607                 case 't':
608                         num_threads = atoi (optarg);
609                         break;
610                 case 'V':
611                         nfs_version |= 1 << (atoi (optarg) - 1);
612                         break;
613                 case 'v':
614                         printf("kmountd %s\n", VERSION);
615                         exit(0);
616                 case 0:
617                         break;
618                 case '?':
619                 default:
620                         usage(argv [0], 1);
621                 }
622
623         /* No more arguments allowed. */
624         if (optind != argc || !(nfs_version & 0x7))
625                 usage(argv [0], 1);
626
627         if (chdir(state_dir)) {
628                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
629                         argv [0], state_dir, strerror(errno));
630                 exit(1);
631         }
632
633         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
634                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
635                                 argv [0], strerror(errno));
636         else {
637                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
638                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
639                     descriptors > FD_SETSIZE)
640                         descriptors = FD_SETSIZE;
641                 if (descriptors) {
642                         rlim.rlim_cur = descriptors;
643                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
644                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
645                                         argv [0], strerror(errno));
646                                 exit(1);
647                         }
648                 }
649         }
650         /* Initialize logging. */
651         if (!foreground) xlog_stderr(0);
652         xlog_open("mountd");
653
654         sa.sa_handler = SIG_IGN;
655         sa.sa_flags = 0;
656         sigemptyset(&sa.sa_mask);
657         sigaction(SIGHUP, &sa, NULL);
658         sigaction(SIGINT, &sa, NULL);
659         sigaction(SIGTERM, &sa, NULL);
660         sigaction(SIGPIPE, &sa, NULL);
661         /* WARNING: the following works on Linux and SysV, but not BSD! */
662         sigaction(SIGCHLD, &sa, NULL);
663
664         /* Daemons should close all extra filehandles ... *before* RPC init. */
665         if (!foreground)
666                 closeall(3);
667
668         new_cache = check_new_cache();
669         if (new_cache)
670                 cache_open();
671
672         if (nfs_version & 0x1)
673                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
674                          mount_dispatch, port);
675         if (nfs_version & (0x1 << 1))
676                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
677                          mount_dispatch, port);
678         if (nfs_version & (0x1 << 2))
679                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
680                          mount_dispatch, port);
681
682         sa.sa_handler = killer;
683         sigaction(SIGINT, &sa, NULL);
684         sigaction(SIGTERM, &sa, NULL);
685         sa.sa_handler = sig_hup;
686         sigaction(SIGHUP, &sa, NULL);
687
688         auth_init(export_file);
689
690         if (!foreground) {
691                 /* We first fork off a child. */
692                 if ((c = fork()) > 0)
693                         exit(0);
694                 if (c < 0) {
695                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
696                                                 strerror(errno));
697                 }
698                 /* Now we remove ourselves from the foreground.
699                    Redirect stdin/stdout/stderr first. */
700                 {
701                         int fd = open("/dev/null", O_RDWR);
702                         (void) dup2(fd, 0);
703                         (void) dup2(fd, 1);
704                         (void) dup2(fd, 2);
705                         if (fd > 2) (void) close(fd);
706                 }
707                 setsid();
708         }
709
710         /* silently bounds check num_threads */
711         if (foreground)
712                 num_threads = 1;
713         else if (num_threads < 1)
714                 num_threads = 1;
715         else if (num_threads > MAX_THREADS)
716                 num_threads = MAX_THREADS;
717
718         if (num_threads > 1)
719                 fork_workers();
720
721         my_svc_run();
722
723         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
724         exit(1);
725 }
726
727 static void
728 usage(const char *prog, int n)
729 {
730         fprintf(stderr,
731 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
732 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
733 "       [-p|--port port] [-V version|--nfs-version version]\n"
734 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
735 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
736 "       [-t num|--num-threads=num]\n", prog);
737         exit(n);
738 }