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