]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
a0a1f2d03a7c61c5b339bd72498abbe8fac9bde3
[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         if (i == 0) {
363                 /* default when there is no sec= option: */
364                 i = 1;
365                 flavors[0] = AUTH_UNIX;
366         }
367         ok->auth_flavors.auth_flavors_val = flavors;
368         ok->auth_flavors.auth_flavors_len = i;
369 }
370
371 /*
372  * NFSv3 MOUNT procedure
373  */
374 bool_t
375 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
376 {
377         struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
378         nfs_export *exp;
379         struct nfs_fh_len *fh;
380
381         xlog(D_CALL, "MNT3(%s) called", *path);
382         fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
383         if (!fh)
384                 return 1;
385
386         ok->fhandle.fhandle3_len = fh->fh_size;
387         ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
388         set_authflavors(ok, exp);
389         return 1;
390 }
391
392 static struct nfs_fh_len *
393 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
394                 mountstat3 *error, int v3)
395 {
396         struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
397         struct stat     stb, estb;
398         nfs_export      *exp;
399         struct nfs_fh_len *fh;
400         char            rpath[MAXPATHLEN+1];
401         char            *p = *path;
402
403         if (*p == '\0')
404                 p = "/";
405
406         /* Reload /var/lib/nfs/etab if necessary */
407         auth_reload();
408
409         /* Resolve symlinks */
410         if (realpath(p, rpath) != NULL) {
411                 rpath[sizeof (rpath) - 1] = '\0';
412                 p = rpath;
413         }
414
415         /* Now authenticate the intruder... */
416         exp = auth_authenticate("mount", sin, p);
417         if (!exp) {
418                 *error = NFSERR_ACCES;
419                 return NULL;
420         }
421         if (stat(p, &stb) < 0) {
422                 xlog(L_WARNING, "can't stat exported dir %s: %s",
423                                 p, strerror(errno));
424                 if (errno == ENOENT)
425                         *error = NFSERR_NOENT;
426                 else
427                         *error = NFSERR_ACCES;
428                 return NULL;
429         }
430         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
431                 xlog(L_WARNING, "%s is not a directory or regular file", p);
432                 *error = NFSERR_NOTDIR;
433                 return NULL;
434         }
435         if (stat(exp->m_export.e_path, &estb) < 0) {
436                 xlog(L_WARNING, "can't stat export point %s: %s",
437                      p, strerror(errno));
438                 *error = NFSERR_NOENT;
439                 return NULL;
440         }
441         if (estb.st_dev != stb.st_dev
442                    && (!new_cache
443                            || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
444                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
445                      p, exp->m_export.e_path);
446                 *error = NFSERR_ACCES;
447                 return NULL;
448         }
449         if (exp->m_export.e_mountpoint &&
450                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
451                                   exp->m_export.e_mountpoint:
452                                   exp->m_export.e_path)) {
453                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
454                      p);
455                 *error = NFSERR_NOENT;
456                 return NULL;
457         }
458
459         if (new_cache) {
460                 /* This will be a static private nfs_export with just one
461                  * address.  We feed it to kernel then extract the filehandle,
462                  * 
463                  */
464
465                 if (cache_export(exp, p)) {
466                         *error = NFSERR_ACCES;
467                         return NULL;
468                 }
469                 fh = cache_get_filehandle(exp, v3?64:32, p);
470                 if (fh == NULL) {
471                         *error = NFSERR_ACCES;
472                         return NULL;
473                 }
474         } else {
475                 int did_export = 0;
476         retry:
477                 if (exp->m_exported<1) {
478                         export_export(exp);
479                         did_export = 1;
480                 }
481                 if (!exp->m_xtabent)
482                         xtab_append(exp);
483
484                 if (v3)
485                         fh = getfh_size ((struct sockaddr *) sin, p, 64);
486                 if (!v3 || (fh == NULL && errno == EINVAL)) {
487                         /* We first try the new nfs syscall. */
488                         fh = getfh ((struct sockaddr *) sin, p);
489                         if (fh == NULL && errno == EINVAL)
490                                 /* Let's try the old one. */
491                                 fh = getfh_old ((struct sockaddr *) sin,
492                                                 stb.st_dev, stb.st_ino);
493                 }
494                 if (fh == NULL && !did_export) {
495                         exp->m_exported = 0;
496                         goto retry;
497                 }
498
499                 if (fh == NULL) {
500                         xlog(L_WARNING, "getfh failed: %s", strerror(errno));
501                         *error = NFSERR_ACCES;
502                         return NULL;
503                 }
504         }
505         *error = NFS_OK;
506         mountlist_add(inet_ntoa(sin->sin_addr), p);
507         if (expret)
508                 *expret = exp;
509         return fh;
510 }
511
512 static void remove_all_clients(exportnode *e)
513 {
514         struct groupnode *g, *ng;
515
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 }
523
524 static void free_exportlist(exports *elist)
525 {
526         struct exportnode *e, *ne;
527
528         for (e = *elist; e != NULL; e = ne) {
529                 ne = e->ex_next;
530                 remove_all_clients(e);
531                 xfree(e->ex_dir);
532                 xfree(e);
533         }
534         *elist = NULL;
535 }
536
537 static void prune_clients(nfs_export *exp, struct exportnode *e)
538 {
539         struct hostent  *hp;
540         struct groupnode *c, **cp;
541
542         cp = &e->ex_groups;
543         while ((c = *cp) != NULL) {
544                 if (client_gettype(c->gr_name) == MCL_FQDN
545                                 && (hp = gethostbyname(c->gr_name))) {
546                         hp = hostent_dup(hp);
547                         if (client_check(exp->m_client, hp)) {
548                                 *cp = c->gr_next;
549                                 xfree(c->gr_name);
550                                 xfree(c);
551                                 xfree (hp);
552                                 continue;
553                         }
554                         xfree (hp);
555                 }
556                 cp = &(c->gr_next);
557         }
558 }
559
560 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
561 {
562         exportnode *e;
563
564         for (e = *elist; e != NULL; e = e->ex_next) {
565                 if (!strcmp(exp->m_export.e_path, e->ex_dir))
566                         return e;
567         }
568         e = xmalloc(sizeof(*e));
569         e->ex_next = *elist;
570         e->ex_groups = NULL;
571         e->ex_dir = xstrdup(exp->m_export.e_path);
572         *elist = e;
573         return e;
574 }
575
576 static void insert_group(struct exportnode *e, char *newname)
577 {
578         struct groupnode *g;
579
580         for (g = e->ex_groups; g; g = g->gr_next)
581                 if (strcmp(g->gr_name, newname))
582                         return;
583
584         g = xmalloc(sizeof(*g));
585         g->gr_name = xstrdup(newname);
586         g->gr_next = e->ex_groups;
587         e->ex_groups = g;
588 }
589
590 static exports
591 get_exportlist(void)
592 {
593         static exports          elist = NULL;
594         struct exportnode       *e;
595         nfs_export              *exp;
596         int                     i;
597         static unsigned int     ecounter;
598         unsigned int            acounter;
599
600         acounter = auth_reload();
601         if (elist && acounter == ecounter)
602                 return elist;
603
604         ecounter = acounter;
605
606         free_exportlist(&elist);
607
608         for (i = 0; i < MCL_MAXTYPES; i++) {
609                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
610                          /* Don't show pseudo exports */
611                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
612                                 continue;
613                         e = lookup_or_create_elist_entry(&elist, exp);
614
615                         /* exports to "*" absorb any others */
616                         if (i == MCL_ANONYMOUS && e->ex_groups) {
617                                 remove_all_clients(e);
618                                 continue;
619                         }
620                         /* non-FQDN's absorb FQDN's they contain: */
621                         if (i != MCL_FQDN && e->ex_groups)
622                                 prune_clients(exp, e);
623
624                         if (exp->m_export.e_hostname[0] != '\0')
625                                 insert_group(e, exp->m_export.e_hostname);
626                 }
627         }
628
629         return elist;
630 }
631
632 int
633 main(int argc, char **argv)
634 {
635         char    *export_file = _PATH_EXPORTS;
636         char    *state_dir = NFS_STATEDIR;
637         int     foreground = 0;
638         int     port = 0;
639         int     descriptors = 0;
640         int     c;
641         struct sigaction sa;
642         struct rlimit rlim;
643
644         /* Parse the command line options and arguments. */
645         opterr = 0;
646         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
647                 switch (c) {
648                 case 'g':
649                         manage_gids = 1;
650                         break;
651                 case 'o':
652                         descriptors = atoi(optarg);
653                         if (descriptors <= 0) {
654                                 fprintf(stderr, "%s: bad descriptors: %s\n",
655                                         argv [0], optarg);
656                                 usage(argv [0], 1);
657                         }
658                         break;
659                 case 'F':
660                         foreground = 1;
661                         break;
662                 case 'd':
663                         xlog_sconfig(optarg, 1);
664                         break;
665                 case 'f':
666                         export_file = optarg;
667                         break;
668                 case 'H': /* PRC: specify a high-availability callout program */
669                         ha_callout_prog = optarg;
670                         break;
671                 case 'h':
672                         usage(argv [0], 0);
673                         break;
674                 case 'P':       /* XXX for nfs-server compatibility */
675                 case 'p':
676                         port = atoi(optarg);
677                         if (port <= 0 || port > 65535) {
678                                 fprintf(stderr, "%s: bad port number: %s\n",
679                                         argv [0], optarg);
680                                 usage(argv [0], 1);
681                         }
682                         break;
683                 case 'N':
684                         nfs_version &= ~(1 << (atoi (optarg) - 1));
685                         break;
686                 case 'n':
687                         _rpcfdtype = SOCK_DGRAM;
688                         break;
689                 case 'r':
690                         reverse_resolve = 1;
691                         break;
692                 case 's':
693                         if ((state_dir = xstrdup(optarg)) == NULL) {
694                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
695                                         argv[0], optarg);
696                                 exit(1);
697                         }
698                         break;
699                 case 't':
700                         num_threads = atoi (optarg);
701                         break;
702                 case 'V':
703                         nfs_version |= 1 << (atoi (optarg) - 1);
704                         break;
705                 case 'v':
706                         printf("kmountd %s\n", VERSION);
707                         exit(0);
708                 case 0:
709                         break;
710                 case '?':
711                 default:
712                         usage(argv [0], 1);
713                 }
714
715         /* No more arguments allowed. */
716         if (optind != argc || !(nfs_version & 0x7))
717                 usage(argv [0], 1);
718
719         if (chdir(state_dir)) {
720                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
721                         argv [0], state_dir, strerror(errno));
722                 exit(1);
723         }
724
725         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
726                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
727                                 argv [0], strerror(errno));
728         else {
729                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
730                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
731                     descriptors > FD_SETSIZE)
732                         descriptors = FD_SETSIZE;
733                 if (descriptors) {
734                         rlim.rlim_cur = descriptors;
735                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
736                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
737                                         argv [0], strerror(errno));
738                                 exit(1);
739                         }
740                 }
741         }
742         /* Initialize logging. */
743         if (!foreground) xlog_stderr(0);
744         xlog_open("mountd");
745
746         sa.sa_handler = SIG_IGN;
747         sa.sa_flags = 0;
748         sigemptyset(&sa.sa_mask);
749         sigaction(SIGHUP, &sa, NULL);
750         sigaction(SIGINT, &sa, NULL);
751         sigaction(SIGTERM, &sa, NULL);
752         sigaction(SIGPIPE, &sa, NULL);
753         /* WARNING: the following works on Linux and SysV, but not BSD! */
754         sigaction(SIGCHLD, &sa, NULL);
755
756         /* Daemons should close all extra filehandles ... *before* RPC init. */
757         if (!foreground)
758                 closeall(3);
759
760         new_cache = check_new_cache();
761         if (new_cache)
762                 cache_open();
763
764         if (nfs_version & 0x1)
765                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
766                          mount_dispatch, port);
767         if (nfs_version & (0x1 << 1))
768                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
769                          mount_dispatch, port);
770         if (nfs_version & (0x1 << 2))
771                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
772                          mount_dispatch, port);
773
774         sa.sa_handler = killer;
775         sigaction(SIGINT, &sa, NULL);
776         sigaction(SIGTERM, &sa, NULL);
777         sa.sa_handler = sig_hup;
778         sigaction(SIGHUP, &sa, NULL);
779
780         auth_init(export_file);
781
782         if (!foreground) {
783                 /* We first fork off a child. */
784                 if ((c = fork()) > 0)
785                         exit(0);
786                 if (c < 0) {
787                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
788                                                 strerror(errno));
789                 }
790                 /* Now we remove ourselves from the foreground.
791                    Redirect stdin/stdout/stderr first. */
792                 {
793                         int fd = open("/dev/null", O_RDWR);
794                         (void) dup2(fd, 0);
795                         (void) dup2(fd, 1);
796                         (void) dup2(fd, 2);
797                         if (fd > 2) (void) close(fd);
798                 }
799                 setsid();
800         }
801
802         /* silently bounds check num_threads */
803         if (foreground)
804                 num_threads = 1;
805         else if (num_threads < 1)
806                 num_threads = 1;
807         else if (num_threads > MAX_THREADS)
808                 num_threads = MAX_THREADS;
809
810         if (num_threads > 1)
811                 fork_workers();
812
813         my_svc_run();
814
815         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
816         exit(1);
817 }
818
819 static void
820 usage(const char *prog, int n)
821 {
822         fprintf(stderr,
823 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
824 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
825 "       [-p|--port port] [-V version|--nfs-version version]\n"
826 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
827 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
828 "       [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);
829         exit(n);
830 }