]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
mountd: Replace "struct hostent" with "struct addrinfo"
[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 << 1)) {
84                 pmap_unset (MOUNTPROG, MOUNTVERS);
85                 pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
86         }
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 addrinfo *ai = NULL;
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                     && (ai = host_addrinfo(c->gr_name))) {
546                         if (client_check(exp->m_client, ai)) {
547                                 *cp = c->gr_next;
548                                 xfree(c->gr_name);
549                                 xfree(c);
550                                 freeaddrinfo(ai);
551                                 continue;
552                         }
553                         freeaddrinfo(ai);
554                 }
555                 cp = &(c->gr_next);
556         }
557 }
558
559 static exportnode *lookup_or_create_elist_entry(exports *elist, nfs_export *exp)
560 {
561         exportnode *e;
562
563         for (e = *elist; e != NULL; e = e->ex_next) {
564                 if (!strcmp(exp->m_export.e_path, e->ex_dir))
565                         return e;
566         }
567         e = xmalloc(sizeof(*e));
568         e->ex_next = *elist;
569         e->ex_groups = NULL;
570         e->ex_dir = xstrdup(exp->m_export.e_path);
571         *elist = e;
572         return e;
573 }
574
575 static void insert_group(struct exportnode *e, char *newname)
576 {
577         struct groupnode *g;
578
579         for (g = e->ex_groups; g; g = g->gr_next)
580                 if (strcmp(g->gr_name, newname))
581                         return;
582
583         g = xmalloc(sizeof(*g));
584         g->gr_name = xstrdup(newname);
585         g->gr_next = e->ex_groups;
586         e->ex_groups = g;
587 }
588
589 static exports
590 get_exportlist(void)
591 {
592         static exports          elist = NULL;
593         struct exportnode       *e;
594         nfs_export              *exp;
595         int                     i;
596         static unsigned int     ecounter;
597         unsigned int            acounter;
598
599         acounter = auth_reload();
600         if (elist && acounter == ecounter)
601                 return elist;
602
603         ecounter = acounter;
604
605         free_exportlist(&elist);
606
607         for (i = 0; i < MCL_MAXTYPES; i++) {
608                 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
609                          /* Don't show pseudo exports */
610                         if (exp->m_export.e_flags & NFSEXP_V4ROOT)
611                                 continue;
612                         e = lookup_or_create_elist_entry(&elist, exp);
613
614                         /* exports to "*" absorb any others */
615                         if (i == MCL_ANONYMOUS && e->ex_groups) {
616                                 remove_all_clients(e);
617                                 continue;
618                         }
619                         /* non-FQDN's absorb FQDN's they contain: */
620                         if (i != MCL_FQDN && e->ex_groups)
621                                 prune_clients(exp, e);
622
623                         if (exp->m_export.e_hostname[0] != '\0')
624                                 insert_group(e, exp->m_export.e_hostname);
625                 }
626         }
627
628         return elist;
629 }
630
631 int
632 main(int argc, char **argv)
633 {
634         char    *export_file = _PATH_EXPORTS;
635         char    *state_dir = NFS_STATEDIR;
636         int     foreground = 0;
637         int     port = 0;
638         int     descriptors = 0;
639         int     c;
640         struct sigaction sa;
641         struct rlimit rlim;
642
643         /* Parse the command line options and arguments. */
644         opterr = 0;
645         while ((c = getopt_long(argc, argv, "o:nFd:f:p:P:hH:N:V:vrs:t:g", longopts, NULL)) != EOF)
646                 switch (c) {
647                 case 'g':
648                         manage_gids = 1;
649                         break;
650                 case 'o':
651                         descriptors = atoi(optarg);
652                         if (descriptors <= 0) {
653                                 fprintf(stderr, "%s: bad descriptors: %s\n",
654                                         argv [0], optarg);
655                                 usage(argv [0], 1);
656                         }
657                         break;
658                 case 'F':
659                         foreground = 1;
660                         break;
661                 case 'd':
662                         xlog_sconfig(optarg, 1);
663                         break;
664                 case 'f':
665                         export_file = optarg;
666                         break;
667                 case 'H': /* PRC: specify a high-availability callout program */
668                         ha_callout_prog = optarg;
669                         break;
670                 case 'h':
671                         usage(argv [0], 0);
672                         break;
673                 case 'P':       /* XXX for nfs-server compatibility */
674                 case 'p':
675                         port = atoi(optarg);
676                         if (port <= 0 || port > 65535) {
677                                 fprintf(stderr, "%s: bad port number: %s\n",
678                                         argv [0], optarg);
679                                 usage(argv [0], 1);
680                         }
681                         break;
682                 case 'N':
683                         nfs_version &= ~(1 << (atoi (optarg) - 1));
684                         break;
685                 case 'n':
686                         _rpcfdtype = SOCK_DGRAM;
687                         break;
688                 case 'r':
689                         reverse_resolve = 1;
690                         break;
691                 case 's':
692                         if ((state_dir = xstrdup(optarg)) == NULL) {
693                                 fprintf(stderr, "%s: xstrdup(%s) failed!\n",
694                                         argv[0], optarg);
695                                 exit(1);
696                         }
697                         break;
698                 case 't':
699                         num_threads = atoi (optarg);
700                         break;
701                 case 'V':
702                         nfs_version |= 1 << (atoi (optarg) - 1);
703                         break;
704                 case 'v':
705                         printf("kmountd %s\n", VERSION);
706                         exit(0);
707                 case 0:
708                         break;
709                 case '?':
710                 default:
711                         usage(argv [0], 1);
712                 }
713
714         /* No more arguments allowed.
715          * Require at least one valid version (2, 3, or 4)
716          */
717         if (optind != argc || !(nfs_version & 0xE))
718                 usage(argv [0], 1);
719
720         if (chdir(state_dir)) {
721                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
722                         argv [0], state_dir, strerror(errno));
723                 exit(1);
724         }
725
726         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
727                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
728                                 argv [0], strerror(errno));
729         else {
730                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
731                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
732                     descriptors > FD_SETSIZE)
733                         descriptors = FD_SETSIZE;
734                 if (descriptors) {
735                         rlim.rlim_cur = descriptors;
736                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
737                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
738                                         argv [0], strerror(errno));
739                                 exit(1);
740                         }
741                 }
742         }
743         /* Initialize logging. */
744         if (!foreground) xlog_stderr(0);
745         xlog_open("mountd");
746
747         sa.sa_handler = SIG_IGN;
748         sa.sa_flags = 0;
749         sigemptyset(&sa.sa_mask);
750         sigaction(SIGHUP, &sa, NULL);
751         sigaction(SIGINT, &sa, NULL);
752         sigaction(SIGTERM, &sa, NULL);
753         sigaction(SIGPIPE, &sa, NULL);
754         /* WARNING: the following works on Linux and SysV, but not BSD! */
755         sigaction(SIGCHLD, &sa, NULL);
756
757         /* Daemons should close all extra filehandles ... *before* RPC init. */
758         if (!foreground)
759                 closeall(3);
760
761         new_cache = check_new_cache();
762         if (new_cache)
763                 cache_open();
764
765         if (nfs_version & (0x1 << 1)) {
766                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
767                          mount_dispatch, port);
768                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
769                          mount_dispatch, port);
770         }
771         if (nfs_version & (0x1 << 2))
772                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
773                          mount_dispatch, port);
774
775         sa.sa_handler = killer;
776         sigaction(SIGINT, &sa, NULL);
777         sigaction(SIGTERM, &sa, NULL);
778         sa.sa_handler = sig_hup;
779         sigaction(SIGHUP, &sa, NULL);
780
781         auth_init(export_file);
782
783         if (!foreground) {
784                 /* We first fork off a child. */
785                 if ((c = fork()) > 0)
786                         exit(0);
787                 if (c < 0) {
788                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
789                                                 strerror(errno));
790                 }
791                 /* Now we remove ourselves from the foreground.
792                    Redirect stdin/stdout/stderr first. */
793                 {
794                         int fd = open("/dev/null", O_RDWR);
795                         (void) dup2(fd, 0);
796                         (void) dup2(fd, 1);
797                         (void) dup2(fd, 2);
798                         if (fd > 2) (void) close(fd);
799                 }
800                 setsid();
801         }
802
803         /* silently bounds check num_threads */
804         if (foreground)
805                 num_threads = 1;
806         else if (num_threads < 1)
807                 num_threads = 1;
808         else if (num_threads > MAX_THREADS)
809                 num_threads = MAX_THREADS;
810
811         if (num_threads > 1)
812                 fork_workers();
813
814         my_svc_run();
815
816         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
817         exit(1);
818 }
819
820 static void
821 usage(const char *prog, int n)
822 {
823         fprintf(stderr,
824 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
825 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
826 "       [-p|--port port] [-V version|--nfs-version version]\n"
827 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
828 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
829 "       [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);
830         exit(n);
831 }