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