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