]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/mountd.c
mountd: add IPv6 support in auth_authenticate()
[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 *UNUSED(rqstp), void *UNUSED(argp), 
196         void *UNUSED(resp))
197 {
198         return 1;
199 }
200
201 bool_t
202 mount_mnt_1_svc(struct svc_req *rqstp, dirpath *path, fhstatus *res)
203 {
204         struct nfs_fh_len *fh;
205
206         xlog(D_CALL, "MNT1(%s) called", *path);
207         fh = get_rootfh(rqstp, path, NULL, &res->fhs_status, 0);
208         if (fh)
209                 memcpy(&res->fhstatus_u.fhs_fhandle, fh->fh_handle, 32);
210         return 1;
211 }
212
213 bool_t
214 mount_dump_1_svc(struct svc_req *rqstp, void *UNUSED(argp), mountlist *res)
215 {
216         struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
217
218         xlog(D_CALL, "dump request from %s.", inet_ntoa(addr->sin_addr));
219         *res = mountlist_list();
220
221         return 1;
222 }
223
224 bool_t
225 mount_umnt_1_svc(struct svc_req *rqstp, dirpath *argp, void *UNUSED(resp))
226 {
227         struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
228         nfs_export      *exp;
229         char            *p = *argp;
230         char            rpath[MAXPATHLEN+1];
231
232         if (*p == '\0')
233                 p = "/";
234
235         if (realpath(p, rpath) != NULL) {
236                 rpath[sizeof (rpath) - 1] = '\0';
237                 p = rpath;
238         }
239
240         exp = auth_authenticate("unmount", (struct sockaddr *)sin, p);
241         if (exp == NULL)
242                 return 1;
243
244         mountlist_del(inet_ntoa(sin->sin_addr), p);
245         return 1;
246 }
247
248 bool_t
249 mount_umntall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), 
250         void *UNUSED(resp))
251 {
252         /* Reload /etc/xtab if necessary */
253         auth_reload();
254
255         mountlist_del_all(nfs_getrpccaller_in(rqstp->rq_xprt));
256         return 1;
257 }
258
259 bool_t
260 mount_export_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
261 {
262         struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
263
264         xlog(D_CALL, "export request from %s.", inet_ntoa(addr->sin_addr));
265         *resp = get_exportlist();
266                 
267         return 1;
268 }
269
270 bool_t
271 mount_exportall_1_svc(struct svc_req *rqstp, void *UNUSED(argp), exports *resp)
272 {
273         struct sockaddr_in *addr = nfs_getrpccaller_in(rqstp->rq_xprt);
274
275         xlog(D_CALL, "exportall request from %s.", inet_ntoa(addr->sin_addr));
276         *resp = get_exportlist();
277
278         return 1;
279 }
280
281 /*
282  * MNTv2 pathconf procedure
283  *
284  * The protocol doesn't include a status field, so Sun apparently considers
285  * it good practice to let anyone snoop on your system, even if it's
286  * pretty harmless data such as pathconf. We don't.
287  *
288  * Besides, many of the pathconf values don't make much sense on NFS volumes.
289  * FIFOs and tty device files represent devices on the *client*, so there's
290  * no point in getting the server's buffer sizes etc.
291  */
292 bool_t
293 mount_pathconf_2_svc(struct svc_req *rqstp, dirpath *path, ppathcnf *res)
294 {
295         struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
296         struct stat     stb;
297         nfs_export      *exp;
298         char            rpath[MAXPATHLEN+1];
299         char            *p = *path;
300
301         memset(res, 0, sizeof(*res));
302
303         if (*p == '\0')
304                 p = "/";
305
306         /* Reload /etc/xtab if necessary */
307         auth_reload();
308
309         /* Resolve symlinks */
310         if (realpath(p, rpath) != NULL) {
311                 rpath[sizeof (rpath) - 1] = '\0';
312                 p = rpath;
313         }
314
315         /* Now authenticate the intruder... */
316         exp = auth_authenticate("pathconf", (struct sockaddr *)sin, p);
317         if (exp == NULL)
318                 return 1;
319         else if (stat(p, &stb) < 0) {
320                 xlog(L_WARNING, "can't stat exported dir %s: %s",
321                                 p, strerror(errno));
322                 return 1;
323         }
324
325         res->pc_link_max  = pathconf(p, _PC_LINK_MAX);
326         res->pc_max_canon = pathconf(p, _PC_MAX_CANON);
327         res->pc_max_input = pathconf(p, _PC_MAX_INPUT);
328         res->pc_name_max  = pathconf(p, _PC_NAME_MAX);
329         res->pc_path_max  = pathconf(p, _PC_PATH_MAX);
330         res->pc_pipe_buf  = pathconf(p, _PC_PIPE_BUF);
331         res->pc_vdisable  = pathconf(p, _PC_VDISABLE);
332
333         /* Can't figure out what to do with pc_mask */
334         res->pc_mask[0]   = 0;
335         res->pc_mask[1]   = 0;
336
337         return 1;
338 }
339
340 /*
341  * We should advertise the preferred flavours first. (See RFC 2623
342  * section 2.7.)  We leave that to the administrator, by advertising
343  * flavours in the order they were listed in /etc/exports.  AUTH_NULL is
344  * dropped from the list to avoid backward compatibility issue with
345  * older Linux clients, who inspect the list in reversed order.
346  *
347  * XXX: It might be more helpful to rearrange these so that flavors
348  * giving more access (as determined from readonly and id-squashing
349  * options) come first.  (If we decide to do that we should probably do
350  * that when reading the exports rather than here.)
351  */
352 static void set_authflavors(struct mountres3_ok *ok, nfs_export *exp)
353 {
354         struct sec_entry *s;
355         static int flavors[SECFLAVOR_COUNT];
356         int i = 0;
357
358         for (s = exp->m_export.e_secinfo; s->flav; s++) {
359                 if (s->flav->fnum == AUTH_NULL)
360                         continue;
361                 flavors[i] = s->flav->fnum;
362                 i++;
363         }
364         if (i == 0) {
365                 /* default when there is no sec= option: */
366                 i = 1;
367                 flavors[0] = AUTH_UNIX;
368         }
369         ok->auth_flavors.auth_flavors_val = flavors;
370         ok->auth_flavors.auth_flavors_len = i;
371 }
372
373 /*
374  * NFSv3 MOUNT procedure
375  */
376 bool_t
377 mount_mnt_3_svc(struct svc_req *rqstp, dirpath *path, mountres3 *res)
378 {
379         struct mountres3_ok *ok = &res->mountres3_u.mountinfo;
380         nfs_export *exp;
381         struct nfs_fh_len *fh;
382
383         xlog(D_CALL, "MNT3(%s) called", *path);
384         fh = get_rootfh(rqstp, path, &exp, &res->fhs_status, 1);
385         if (!fh)
386                 return 1;
387
388         ok->fhandle.fhandle3_len = fh->fh_size;
389         ok->fhandle.fhandle3_val = (char *)fh->fh_handle;
390         set_authflavors(ok, exp);
391         return 1;
392 }
393
394 static struct nfs_fh_len *
395 get_rootfh(struct svc_req *rqstp, dirpath *path, nfs_export **expret,
396                 mountstat3 *error, int v3)
397 {
398         struct sockaddr_in *sin = nfs_getrpccaller_in(rqstp->rq_xprt);
399         struct stat     stb, estb;
400         nfs_export      *exp;
401         struct nfs_fh_len *fh;
402         char            rpath[MAXPATHLEN+1];
403         char            *p = *path;
404
405         if (*p == '\0')
406                 p = "/";
407
408         /* Reload /var/lib/nfs/etab if necessary */
409         auth_reload();
410
411         /* Resolve symlinks */
412         if (realpath(p, rpath) != NULL) {
413                 rpath[sizeof (rpath) - 1] = '\0';
414                 p = rpath;
415         }
416
417         /* Now authenticate the intruder... */
418         exp = auth_authenticate("mount", (struct sockaddr *)sin, p);
419         if (exp == NULL) {
420                 *error = NFSERR_ACCES;
421                 return NULL;
422         }
423         if (stat(p, &stb) < 0) {
424                 xlog(L_WARNING, "can't stat exported dir %s: %s",
425                                 p, strerror(errno));
426                 if (errno == ENOENT)
427                         *error = NFSERR_NOENT;
428                 else
429                         *error = NFSERR_ACCES;
430                 return NULL;
431         }
432         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
433                 xlog(L_WARNING, "%s is not a directory or regular file", p);
434                 *error = NFSERR_NOTDIR;
435                 return NULL;
436         }
437         if (stat(exp->m_export.e_path, &estb) < 0) {
438                 xlog(L_WARNING, "can't stat export point %s: %s",
439                      p, strerror(errno));
440                 *error = NFSERR_NOENT;
441                 return NULL;
442         }
443         if (estb.st_dev != stb.st_dev
444                    && (!new_cache
445                            || !(exp->m_export.e_flags & NFSEXP_CROSSMOUNT))) {
446                 xlog(L_WARNING, "request to export directory %s below nearest filesystem %s",
447                      p, exp->m_export.e_path);
448                 *error = NFSERR_ACCES;
449                 return NULL;
450         }
451         if (exp->m_export.e_mountpoint &&
452                    !is_mountpoint(exp->m_export.e_mountpoint[0]?
453                                   exp->m_export.e_mountpoint:
454                                   exp->m_export.e_path)) {
455                 xlog(L_WARNING, "request to export an unmounted filesystem: %s",
456                      p);
457                 *error = NFSERR_NOENT;
458                 return NULL;
459         }
460
461         if (new_cache) {
462                 /* This will be a static private nfs_export with just one
463                  * address.  We feed it to kernel then extract the filehandle,
464                  * 
465                  */
466
467                 if (cache_export(exp, p)) {
468                         *error = NFSERR_ACCES;
469                         return NULL;
470                 }
471                 fh = cache_get_filehandle(exp, v3?64:32, p);
472                 if (fh == NULL) {
473                         *error = NFSERR_ACCES;
474                         return NULL;
475                 }
476         } else {
477                 int did_export = 0;
478         retry:
479                 if (exp->m_exported<1) {
480                         export_export(exp);
481                         did_export = 1;
482                 }
483                 if (!exp->m_xtabent)
484                         xtab_append(exp);
485
486                 if (v3)
487                         fh = getfh_size(sin, p, 64);
488                 if (!v3 || (fh == NULL && errno == EINVAL)) {
489                         /* We first try the new nfs syscall. */
490                         fh = getfh(sin, p);
491                         if (fh == NULL && errno == EINVAL)
492                                 /* Let's try the old one. */
493                                 fh = getfh_old(sin, stb.st_dev, stb.st_ino);
494                 }
495                 if (fh == NULL && !did_export) {
496                         exp->m_exported = 0;
497                         goto retry;
498                 }
499
500                 if (fh == NULL) {
501                         xlog(L_WARNING, "getfh failed: %s", strerror(errno));
502                         *error = NFSERR_ACCES;
503                         return NULL;
504                 }
505         }
506         *error = NFS_OK;
507         mountlist_add(inet_ntoa(sin->sin_addr), p);
508         if (expret)
509                 *expret = exp;
510         return fh;
511 }
512
513 static void remove_all_clients(exportnode *e)
514 {
515         struct groupnode *g, *ng;
516
517         for (g = e->ex_groups; g; g = ng) {
518                 ng = g->gr_next;
519                 xfree(g->gr_name);
520                 xfree(g);
521         }
522         e->ex_groups = NULL;
523 }
524
525 static void free_exportlist(exports *elist)
526 {
527         struct exportnode *e, *ne;
528
529         for (e = *elist; e != NULL; e = ne) {
530                 ne = e->ex_next;
531                 remove_all_clients(e);
532                 xfree(e->ex_dir);
533                 xfree(e);
534         }
535         *elist = NULL;
536 }
537
538 static void prune_clients(nfs_export *exp, struct exportnode *e)
539 {
540         struct addrinfo *ai = NULL;
541         struct groupnode *c, **cp;
542
543         cp = &e->ex_groups;
544         while ((c = *cp) != NULL) {
545                 if (client_gettype(c->gr_name) == MCL_FQDN
546                     && (ai = host_addrinfo(c->gr_name))) {
547                         if (client_check(exp->m_client, ai)) {
548                                 *cp = c->gr_next;
549                                 xfree(c->gr_name);
550                                 xfree(c);
551                                 freeaddrinfo(ai);
552                                 continue;
553                         }
554                         freeaddrinfo(ai);
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          * Require at least one valid version (2, 3, or 4)
717          */
718         if (optind != argc || !(nfs_version & 0xE))
719                 usage(argv [0], 1);
720
721         if (chdir(state_dir)) {
722                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
723                         argv [0], state_dir, strerror(errno));
724                 exit(1);
725         }
726
727         if (getrlimit (RLIMIT_NOFILE, &rlim) != 0)
728                 fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n",
729                                 argv [0], strerror(errno));
730         else {
731                 /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */
732                 if ((descriptors == 0 && rlim.rlim_cur > FD_SETSIZE) ||
733                     descriptors > FD_SETSIZE)
734                         descriptors = FD_SETSIZE;
735                 if (descriptors) {
736                         rlim.rlim_cur = descriptors;
737                         if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) {
738                                 fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n",
739                                         argv [0], strerror(errno));
740                                 exit(1);
741                         }
742                 }
743         }
744         /* Initialize logging. */
745         if (!foreground) xlog_stderr(0);
746         xlog_open("mountd");
747
748         sa.sa_handler = SIG_IGN;
749         sa.sa_flags = 0;
750         sigemptyset(&sa.sa_mask);
751         sigaction(SIGHUP, &sa, NULL);
752         sigaction(SIGINT, &sa, NULL);
753         sigaction(SIGTERM, &sa, NULL);
754         sigaction(SIGPIPE, &sa, NULL);
755         /* WARNING: the following works on Linux and SysV, but not BSD! */
756         sigaction(SIGCHLD, &sa, NULL);
757
758         /* Daemons should close all extra filehandles ... *before* RPC init. */
759         if (!foreground)
760                 closeall(3);
761
762         new_cache = check_new_cache();
763         if (new_cache)
764                 cache_open();
765
766         if (nfs_version & (0x1 << 1)) {
767                 rpc_init("mountd", MOUNTPROG, MOUNTVERS,
768                          mount_dispatch, port);
769                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_POSIX,
770                          mount_dispatch, port);
771         }
772         if (nfs_version & (0x1 << 2))
773                 rpc_init("mountd", MOUNTPROG, MOUNTVERS_NFSV3,
774                          mount_dispatch, port);
775
776         sa.sa_handler = killer;
777         sigaction(SIGINT, &sa, NULL);
778         sigaction(SIGTERM, &sa, NULL);
779         sa.sa_handler = sig_hup;
780         sigaction(SIGHUP, &sa, NULL);
781
782         auth_init(export_file);
783
784         if (!foreground) {
785                 /* We first fork off a child. */
786                 if ((c = fork()) > 0)
787                         exit(0);
788                 if (c < 0) {
789                         xlog(L_FATAL, "mountd: cannot fork: %s\n",
790                                                 strerror(errno));
791                 }
792                 /* Now we remove ourselves from the foreground.
793                    Redirect stdin/stdout/stderr first. */
794                 {
795                         int fd = open("/dev/null", O_RDWR);
796                         (void) dup2(fd, 0);
797                         (void) dup2(fd, 1);
798                         (void) dup2(fd, 2);
799                         if (fd > 2) (void) close(fd);
800                 }
801                 setsid();
802         }
803
804         /* silently bounds check num_threads */
805         if (foreground)
806                 num_threads = 1;
807         else if (num_threads < 1)
808                 num_threads = 1;
809         else if (num_threads > MAX_THREADS)
810                 num_threads = MAX_THREADS;
811
812         if (num_threads > 1)
813                 fork_workers();
814
815         my_svc_run();
816
817         xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
818         exit(1);
819 }
820
821 static void
822 usage(const char *prog, int n)
823 {
824         fprintf(stderr,
825 "Usage: %s [-F|--foreground] [-h|--help] [-v|--version] [-d kind|--debug kind]\n"
826 "       [-o num|--descriptors num] [-f exports-file|--exports-file=file]\n"
827 "       [-p|--port port] [-V version|--nfs-version version]\n"
828 "       [-N version|--no-nfs-version version] [-n|--no-tcp]\n"
829 "       [-H ha-callout-prog] [-s|--state-directory-path path]\n"
830 "       [-g|--manage-gids] [-t num|--num-threads=num]\n", prog);
831         exit(n);
832 }