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