]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfsd.c
d0bbfb30abd721cecea08c80f8deee6b82a9425d
[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 <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26
27 #include "nfslib.h"
28
29 static void     usage(const char *);
30
31 static struct option longopts[] =
32 {
33         { "host", 1, 0, 'H' },
34         { "help", 0, 0, 'h' },
35         { "no-nfs-version", 1, 0, 'N' },
36         { "no-tcp", 0, 0, 'T' },
37         { "no-udp", 0, 0, 'U' },
38         { "port", 1, 0, 'P' },
39         { "port", 1, 0, 'p' },
40         { NULL, 0, 0, 0 }
41 };
42 unsigned int protobits = NFSCTL_ALLBITS;
43 unsigned int versbits = NFSCTL_ALLBITS;
44 char *haddr = NULL;
45
46 int
47 main(int argc, char **argv)
48 {
49         int     count = 1, c, error, port, fd, found_one;
50         struct servent *ent;
51         struct hostent *hp;
52
53         ent = getservbyname ("nfs", "udp");
54         if (ent != NULL)
55                 port = ntohs (ent->s_port);
56         else
57                 port = 2049;
58
59         while ((c = getopt_long(argc, argv, "H:hN:p:P:TU", longopts, NULL)) != EOF) {
60                 switch(c) {
61                 case 'H':
62                         if (inet_addr(optarg) != INADDR_NONE) {
63                                 haddr = strdup(optarg);
64                         } else if ((hp = gethostbyname(optarg)) != NULL) {
65                                 haddr = inet_ntoa((*(struct in_addr*)(hp->h_addr_list[0])));
66                         } else {
67                                 fprintf(stderr, "%s: Unknown hostname: %s\n",
68                                         argv[0], optarg);
69                                 usage(argv [0]);
70                         }
71                         break;
72                 case 'P':       /* XXX for nfs-server compatibility */
73                 case 'p':
74                         port = atoi(optarg);
75                         if (port <= 0 || port > 65535) {
76                                 fprintf(stderr, "%s: bad port number: %s\n",
77                                         argv[0], optarg);
78                                 usage(argv [0]);
79                         }
80                         break;
81                 case 'N':
82                         switch((c = atoi(optarg))) {
83                         case 2:
84                         case 3:
85                         case 4:
86                                 NFSCTL_VERUNSET(versbits, c);
87                                 break;
88                         default:
89                                 fprintf(stderr, "%c: Unsupported version\n", c);
90                                 exit(1);
91                         }
92                         break;
93                 case 'T':
94                                 NFSCTL_TCPUNSET(protobits);
95                                 break;
96                 case 'U':
97                                 NFSCTL_UDPUNSET(protobits);
98                                 break;
99                 default:
100                         fprintf(stderr, "Invalid argument: '%c'\n", c);
101                 case 'h':
102                         usage(argv[0]);
103                 }
104         }
105         /*
106          * Do some sanity checking, if the ctlbits are set
107          */
108         if (!NFSCTL_UDPISSET(protobits) && !NFSCTL_TCPISSET(protobits)) {
109                 fprintf(stderr, "invalid protocol specified\n");
110                 exit(1);
111         }
112         found_one = 0;
113         for (c = NFSD_MINVERS; c <= NFSD_MAXVERS; c++) {
114                 if (NFSCTL_VERISSET(versbits, c))
115                         found_one = 1;
116         }
117         if (!found_one) {
118                 fprintf(stderr, "no version specified\n");
119                 exit(1);
120         }                       
121         if (NFSCTL_VERISSET(versbits, 4) && !NFSCTL_TCPISSET(versbits)) {
122                 fprintf(stderr, "version 4 requires the TCP protocol\n");
123                 exit(1);
124         }
125         if (haddr == NULL) {
126                 struct in_addr in = {INADDR_ANY}; 
127                 haddr = strdup(inet_ntoa(in));
128         }
129
130         if (chdir(NFS_STATEDIR)) {
131                 fprintf(stderr, "%s: chdir(%s) failed: %s\n",
132                         argv [0], NFS_STATEDIR, strerror(errno));
133                 exit(1);
134         }
135
136         if (optind < argc) {
137                 if ((count = atoi(argv[optind])) < 0) {
138                         /* insane # of servers */
139                         fprintf(stderr,
140                                 "%s: invalid server count (%d), using 1\n",
141                                 argv[0], count);
142                         count = 1;
143                 }
144         }
145         /* KLUDGE ALERT:
146            Some kernels let nfsd kernel threads inherit open files
147            from the program that spawns them (i.e. us).  So close
148            everything before spawning kernel threads.  --Chip */
149         fd = open("/dev/null", O_RDWR);
150         if (fd == -1)
151                 perror("/dev/null");
152         else {
153                 (void) dup2(fd, 0);
154                 (void) dup2(fd, 1);
155                 (void) dup2(fd, 2);
156         }
157         closeall(3);
158
159         openlog("nfsd", LOG_PID, LOG_DAEMON);
160         if ((error = nfssvc(port, count, versbits, protobits, haddr)) < 0) {
161                 int e = errno;
162                 syslog(LOG_ERR, "nfssvc: %s", strerror(e));
163                 closelog();
164         }
165
166         return (error != 0);
167 }
168
169 static void
170 usage(const char *prog)
171 {
172         fprintf(stderr, "Usage:\n"
173                 "%s [-H hostname] [-p|-P|--port port] [-N|--no-nfs-version version ] [-T|--no-tcp] [-U|--no-udp] nrservs\n", 
174                 prog);
175         exit(2);
176 }