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