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