]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfsd.c
2002-09-12 H.J. Lu <hjl@lucon.org>
[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 <netdb.h>
21 #include "nfslib.h"
22
23 static void     usage(const char *);
24
25 int
26 main(int argc, char **argv)
27 {
28         int     count = 1, c, error, port, fd;
29         struct servent *ent;
30
31         ent = getservbyname ("nfs", "udp");
32         if (ent != NULL)
33                 port = ntohs (ent->s_port);
34         else
35                 port = 2049;
36
37         while ((c = getopt(argc, argv, "hp:P:")) != EOF) {
38                 switch(c) {
39                 case 'P':       /* XXX for nfs-server compatibility */
40                 case 'p':
41                         port = atoi(optarg);
42                         if (port <= 0 || port > 65535) {
43                                 fprintf(stderr, "%s: bad port number: %s\n",
44                                         argv[0], optarg);
45                                 usage(argv [0]);
46                         }
47                         break;
48                         break;
49                 case 'h':
50                 default:
51                         usage(argv[0]);
52                 }
53         }
54
55         if (chdir(NFS_STATEDIR)) {
56                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
57                         argv [0], NFS_STATEDIR, strerror(errno));
58                 exit(1);
59         }
60
61         if (optind < argc) {
62                 if ((count = atoi(argv[optind])) < 0) {
63                         /* insane # of servers */
64                         fprintf(stderr,
65                                 "%s: invalid server count (%d), using 1\n",
66                                 argv[0], count);
67                         count = 1;
68                 }
69         }
70
71         /* KLUDGE ALERT:
72            Some kernels let nfsd kernel threads inherit open files
73            from the program that spawns them (i.e. us).  So close
74            everything before spawning kernel threads.  --Chip */
75         fd = open("/dev/null", O_RDWR);
76         if (fd == -1)
77                 perror("/dev/null");
78         else {
79                 (void) dup2(fd, 0);
80                 (void) dup2(fd, 1);
81                 (void) dup2(fd, 2);
82         }
83         fd = sysconf(_SC_OPEN_MAX);
84         while (--fd > 2)
85                 (void) close(fd);
86
87         if ((error = nfssvc(port, count)) < 0) {
88                 int e = errno;
89                 openlog("nfsd", LOG_PID, LOG_DAEMON);
90                 syslog(LOG_ERR, "nfssvc: %s", strerror(e));
91                 closelog();
92         }
93
94         return (error != 0);
95 }
96
97 static void
98 usage(const char *prog)
99 {
100         fprintf(stderr, "usage:\n"
101                         "%s nrservs\n", prog);
102         exit(2);
103 }