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