]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfsd.c
772f72df54693751550eac1c9fbe978d427720b9
[nfs-utils.git] / utils / nfsd / nfsd.c
1 /*
2  * nfsd
3  *
4  * This is the user level part of nfsd. This is very primitive, because
5  * all the work is now done in the kernel module.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <getopt.h>
19 #include <syslog.h>
20 #include "nfslib.h"
21
22 static void     usage(const char *);
23
24 int
25 main(int argc, char **argv)
26 {
27         int     count = 1, c, error, port, fd;
28
29         port = 2049;
30
31         /* FIXME: Check for nfs in /etc/services */
32
33         while ((c = getopt(argc, argv, "hp:P:")) != EOF) {
34                 switch(c) {
35                 case 'P':       /* XXX for nfs-server compatibility */
36                 case 'p':
37                         port = atoi(optarg);
38                         if (port <= 0 || port > 65535) {
39                                 fprintf(stderr, "%s: bad port number: %s\n",
40                                         argv[0], optarg);
41                                 usage(argv [0]);
42                         }
43                         break;
44                         break;
45                 case 'h':
46                 default:
47                         usage(argv[0]);
48                 }
49         }
50
51         if (chdir(NFS_STATEDIR)) {
52                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
53                         argv [0], NFS_STATEDIR, strerror(errno));
54                 exit(1);
55         }
56
57         if (optind < argc) {
58                 if ((count = atoi(argv[optind])) < 0) {
59                         /* insane # of servers */
60                         fprintf(stderr,
61                                 "%s: invalid server count (%d), using 1\n",
62                                 argv[0], count);
63                         count = 1;
64                 }
65         }
66
67         /* KLUDGE ALERT:
68            Some kernels let nfsd kernel threads inherit open files
69            from the program that spawns them (i.e. us).  So close
70            everything before spawning kernel threads.  --Chip */
71         fd = open("/dev/null", O_RDWR);
72         if (fd == -1)
73                 perror("/dev/null");
74         else {
75                 (void) dup2(fd, 0);
76                 (void) dup2(fd, 1);
77                 (void) dup2(fd, 2);
78         }
79         fd = sysconf(_SC_OPEN_MAX);
80         while (--fd > 2)
81                 (void) close(fd);
82
83         if ((error = nfssvc(port, count)) < 0) {
84                 int e = errno;
85                 openlog("nfsd", LOG_PID, LOG_DAEMON);
86                 syslog(LOG_ERR, "nfssvc: %s", strerror(e));
87                 closelog();
88         }
89
90         return (error != 0);
91 }
92
93 static void
94 usage(const char *prog)
95 {
96         fprintf(stderr, "usage:\n"
97                         "%s nrservs\n", prog);
98         exit(2);
99 }