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