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