]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
63d5ce1797edd65e01f5d38819377ab4d5eb2ae8
[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         xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
207         *res = mountlist_list();
208
209         return 1;
210 }
211
212 bool_t
213 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *resp)
214 {
215         struct sockaddr_in *sin
216                 = (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
217         nfs_export      *exp;
218         char            *p = *argp;
219         char            rpath[MAXPATHLEN+1];
220
221         if (*p == '\0')
222                 p = "/";
223
224         if (realpath(p, rpath) != NULL) {
225                 rpath[sizeof (rpath) - 1] = '\0';
226                 p = rpath;
227         }
228
229         if (!(exp = auth_authenticate("unmount", sin, p))) {
230                 return 1;
231         }
232
233         if (!new_cache)
234                 export_reset (exp);
235
236         mountlist_del(inet_ntoa(sin->sin_addr), p);
237         return 1;
238 }
239
240 bool_t
241 mount_umntall_1_svc(struct svc_req *rqstp, void *argp, void *resp)
242 {
243         /* Reload /etc/xtab if necessary */
244         auth_reload();
245
246         mountlist_del_all((struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt));
247         return 1;
248 }
249
250 bool_t
251 mount_export_1_svc(struct svc_req *rqstp, void *argp, exports *resp)
252 {
253         struct sockaddr_in *addr =
254                 (struct sockaddr_in *) svc_getcaller(rqstp->rq_xprt);
255
256         xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
257         *resp = get_exportlist();
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         xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
269         *resp = get_exportlist();
270
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                 if (cache_export(exp, p)) {
421                         *error = NFSERR_ACCES;
422                         return NULL;
423                 }
424                 fh = cache_get_filehandle(exp, v3?64:32, p);
425                 if (fh == NULL) 
426                         *error = NFSERR_ACCES;
427                 else {
428                         *error = NFS_OK;
429                         mountlist_add(inet_ntoa(sin->sin_addr), p);
430                 }
431                 return fh;
432         } else {
433                 struct nfs_fh_len  *fh;
434
435                 if (exp->m_exported<1)
436                         export_export(exp);
437                 if (!exp->m_xtabent)
438                         xtab_append(exp);
439
440                 if (v3)
441                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
442                 if (!v3 || (fh == NULL && errno == EINVAL)) {
443                         /* We first try the new nfs syscall. */
444                         fh = getfh ((struct sockaddr *) sin, p);
445                         if (fh == NULL && errno == EINVAL)
446                                 /* Let's try the old one. */
447                                 fh = getfh_old ((struct sockaddr *) sin,
448                                                 stb.st_dev, stb.st_ino);
449                 }
450                 if (fh != NULL) {
451                         mountlist_add(inet_ntoa(sin->sin_addr), p);
452                         *error = NFS_OK;
453                         export_reset (exp);
454                         return fh;
455                 }
456                 xlog(L_WARNING, "getfh failed: %s", strerror(errno));
457                 *error = NFSERR_ACCES;
458         }
459         export_reset (exp);
460         return NULL;
461 }
462
463 static exports
464 get_exportlist(void)
465 {
466         static exports          elist = NULL;
467         struct exportnode       *e, *ne;
468         struct groupnode        *g, *ng, *c, **cp;
469         nfs_export              *exp;
470         int                     i;
471         static unsigned int     ecounter;
472         unsigned int            acounter;
473
474         acounter = auth_reload();
475         if (elist && acounter == ecounter)
476                 return elist;
477
478         ecounter = acounter;
479
480         for (e = elist; e != NULL; e = ne) {
481                 ne = e->ex_next;
482                 for (g = e->ex_groups; g != NULL; g = ng) {
483                         ng = g->gr_next;
484                         xfree(g->gr_name);
485                         xfree(g);
486                 }
487                 xfree(e->ex_dir);
488                 xfree(e);
489         }
490         elist = NULL;
491
492         for (i = 0; i < MCL_MAXTYPES; i++) {
493                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
494                         for (e = elist; e != NULL; e = e->ex_next) {
495                                 if (!strcmp(exp->m_export.m_path, e->ex_dir))
496                                         break;
497                         }
498                         if (!e) {
499                                 e = (struct exportnode *) xmalloc(sizeof(*e));
500                                 e->ex_next = elist;
501                                 e->ex_groups = NULL;
502                                 e->ex_dir = xstrdup(exp->m_export.m_path);
503                                 elist = e;
504                         }
505
506                         /* We need to check if we should remove
507                            previous ones. */
508                         if (i == MCL_ANONYMOUS && e->ex_groups) {
509                                 for (g = e->ex_groups; g; g = ng) {
510                                         ng = g->gr_next;
511                                         xfree(g->gr_name);
512                                         xfree(g);
513                                 }
514                                 e->ex_groups = NULL;
515                                 continue;
516                         }
517
518                         if (i != MCL_FQDN && e->ex_groups) {
519                           struct hostent        *hp;
520
521                           cp = &e->ex_groups;
522                           while ((c = *cp) != NULL) {
523                             if (client_gettype (c->gr_name) == MCL_FQDN
524                                 && (hp = gethostbyname(c->gr_name))) {
525                               hp = hostent_dup (hp);
526                               if (client_check(exp->m_client, hp)) {
527                                 *cp = c->gr_next;
528                                 xfree(c->gr_name);
529                                 xfree(c);
530                                 xfree (hp);
531                                 continue;
532                               }
533                               xfree (hp);
534                             }
535                             cp = &(c->gr_next);
536                           }
537                         }
538
539                         if (exp->m_export.e_hostname [0] != '\0') {
540                                 for (g = e->ex_groups; g; g = g->gr_next)
541                                         if (strcmp (exp->m_export.e_hostname,
542                                                     g->gr_name) == 0)
543                                                 break;
544                                 if (g)
545                                         continue;
546                                 g = (struct groupnode *) xmalloc(sizeof(*g));
547                                 g->gr_name = xstrdup(exp->m_export.e_hostname);
548                                 g->gr_next = e->ex_groups;
549                                 e->ex_groups = g;
550                         }
551                 }
552         }
553
554         return elist;
555 }
556
557 int
558 main(int argc, char **argv)
559 {
560         char    *export_file = _PATH_EXPORTS;
561         char    *state_dir = NFS_STATEDIR;
562         int     foreground = 0;
563         int     port = 0;
564         int     descriptors = 0;
565         int     c;
566         struct sigaction sa;
567         struct rlimit rlim;
568
569         /* Parse the command line options and arguments. */
570         opterr = 0;
571         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
572                 switch (c) {
573                 case 'g':
574                         manage_gids = 1;
575                         break;
576                 case 'o':
577                         descriptors = atoi(optarg);
578                         if (descriptors <= 0) {
579                                 fprintf(stderr, "%s: bad descriptors: %s\n",
580                                         argv [0], optarg);
581                                 usage(argv [0], 1);
582                         }
583                         break;
584                 case 'F':
585                         foreground = 1;
586                         break;
587                 case 'd':
588                         xlog_sconfig(optarg, 1);
589                         break;
590                 case 'f':
591                         export_file = optarg;
592                         break;
593                 case 'H': /* PRC: specify a high-availability callout program */
594                         ha_callout_prog = optarg;
595                         break;
596                 case 'h':
597                         usage(argv [0], 0);
598                         break;
599                 case 'P':       /* XXX for nfs-server compatibility */
600                 case 'p':
601                         port = atoi(optarg);
602                         if (port <= 0 || port > 65535) {
603                                 fprintf(stderr, "%s: bad port number: %s\n",
604                                         argv [0], optarg);
605                                 usage(argv [0], 1);
606                         }
607                         break;
608                 case 'N':
609                         nfs_version &= ~(1 << (atoi (optarg) - 1));
610                         break;
611                 case 'n':
612                         _rpcfdtype = SOCK_DGRAM;
613                         break;
614                 case 'r':
615                         reverse_resolve = 1;
616                         break;
617                 case 's':
618                         if ((state_dir = xstrdup(optarg)) == NULL) {
619                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
620                                         argv[0], optarg);
621                                 exit(1);
622                         }
623                         break;
624                 case 't':
625                         num_threads = atoi (optarg);
626                         break;
627                 case 'V':
628                         nfs_version |= 1 << (atoi (optarg) - 1);
629                         break;
630                 case 'v':
631                         printf("kmountd %s\n", VERSION);
632                         exit(0);
633                 case 0:
634                         break;
635                 case '?':
636                 default:
637                         usage(argv [0], 1);
638                 }
639
640         /* No more arguments allowed. */
641         if (optind != argc || !(nfs_version & 0x7))
642                 usage(argv [0], 1);
643
644         if (chdir(state_dir)) {
645                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
646                         argv [0], state_dir, strerror(errno));
647                 exit(1);
648         }
649
650         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
651                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
652                                 argv [0], strerror(errno));
653         else {
654                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
655                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
656                     descriptors > FD_SETSIZE)
657                         descriptors = FD_SETSIZE;
658                 if (descriptors) {
659                         rlim.rlim_cur = descriptors;
660                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
661                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
662                                         argv [0], strerror(errno));
663                                 exit(1);
664                         }
665                 }
666         }
667         /* Initialize logging. */
668         if (!foreground) xlog_stderr(0);
669         xlog_open("mountd");
670
671         sa.sa_handler = SIG_IGN;
672         sa.sa_flags = 0;
673         sigemptyset(&sa.sa_mask);
674         sigaction(SIGHUP, &sa, NULL);
675         sigaction(SIGINT, &sa, NULL);
676         sigaction(SIGTERM, &sa, NULL);
677         sigaction(SIGPIPE, &sa, NULL);
678         /* WARNING: the following works on Linux and SysV, but not BSD! */
679         sigaction(SIGCHLD, &sa, NULL);
680
681         /* Daemons should close all extra filehandles ... *before* RPC init. */
682         if (!foreground)
683                 closeall(3);
684
685         new_cache = check_new_cache();
686         if (new_cache)
687                 cache_open();
688
689         if (nfs_version & 0x1)
690                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
691                          mount_dispatch, port);
692         if (nfs_version & (0x1 << 1))
693                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
694                          mount_dispatch, port);
695         if (nfs_version & (0x1 << 2))
696                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
697                          mount_dispatch, port);
698
699         sa.sa_handler = killer;
700         sigaction(SIGINT, &sa, NULL);
701         sigaction(SIGTERM, &sa, NULL);
702         sa.sa_handler = sig_hup;
703         sigaction(SIGHUP, &sa, NULL);
704
705         auth_init(export_file);
706
707         if (!foreground) {
708                 /* We first fork off a child. */
709                 if ((c = fork()) > 0)
710                         exit(0);
711                 if (c < 0) {
712                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
713                                                 strerror(errno));
714                 }
715                 /* Now we remove ourselves from the foreground.
716                    Redirect stdin/stdout/stderr first. */
717                 {
718                         int fd = open("/dev/null", O_RDWR);
719                         (void) dup2(fd, 0);
720                         (void) dup2(fd, 1);
721                         (void) dup2(fd, 2);
722                         if (fd > 2) (void) close(fd);
723                 }
724                 setsid();
725         }
726
727         /* silently bounds check num_threads */
728         if (foreground)
729                 num_threads = 1;
730         else if (num_threads < 1)
731                 num_threads = 1;
732         else if (num_threads > MAX_THREADS)
733                 num_threads = MAX_THREADS;
734
735         if (num_threads > 1)
736                 fork_workers();
737
738         my_svc_run();
739
740         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
741         exit(1);
742 }
743
744 static void
745 usage(const char *prog, int n)
746 {
747         fprintf(stderr,
748 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
749 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
750 "       [-p|--port port] [-V version|--nfs-version version]\n"
751 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
752 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
753 "       [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);
754         exit(n);
755 }