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