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