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