]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfsd.c
05506ee4ae6c2900fbd2fb4b62749f0789424959
[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 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <syslog.h>
22 #include <netdb.h>
23 #include "nfslib.h"
24
25 static void     usage(const char *);
26
27 int
28 main(int argc, char **argv)
29 {
30         int     count = 1, c, error, port, fd;
31         struct servent *ent;
32
33         ent = getservbyname ("nfs", "udp");
34         if (ent != NULL)
35                 port = ntohs (ent->s_port);
36         else
37                 port = 2049;
38
39         while ((c = getopt(argc, argv, "hp:P:")) != EOF) {
40                 switch(c) {
41                 case 'P':       /* XXX for nfs-server compatibility */
42                 case 'p':
43                         port = atoi(optarg);
44                         if (port <= 0 || port > 65535) {
45                                 fprintf(stderr, "%s: bad port number: %s\n",
46                                         argv[0], optarg);
47                                 usage(argv [0]);
48                         }
49                         break;
50                         break;
51                 case 'h':
52                 default:
53                         usage(argv[0]);
54                 }
55         }
56
57         if (chdir(NFS_STATEDIR)) {
58                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
59                         argv [0], NFS_STATEDIR, strerror(errno));
60                 exit(1);
61         }
62
63         if (optind < argc) {
64                 if ((count = atoi(argv[optind])) < 0) {
65                         /* insane # of servers */
66                         fprintf(stderr,
67                                 "%s: invalid server count (%d), using 1\n",
68                                 argv[0], count);
69                         count = 1;
70                 }
71         }
72
73         /* KLUDGE ALERT:
74            Some kernels let nfsd kernel threads inherit open files
75            from the program that spawns them (i.e. us).  So close
76            everything before spawning kernel threads.  --Chip */
77         fd = open("/dev/null", O_RDWR);
78         if (fd == -1)
79                 perror("/dev/null");
80         else {
81                 (void) dup2(fd, 0);
82                 (void) dup2(fd, 1);
83                 (void) dup2(fd, 2);
84         }
85         closeall(3);
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 }