]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfsd.c
Imported Upstream version 1.2.3
[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 <netdb.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25
26 #include "nfslib.h"
27 #include "nfssvc.h"
28 #include "xlog.h"
29
30 static void     usage(const char *);
31
32 static struct option longopts[] =
33 {
34         { "host", 1, 0, 'H' },
35         { "help", 0, 0, 'h' },
36         { "no-nfs-version", 1, 0, 'N' },
37         { "no-tcp", 0, 0, 'T' },
38         { "no-udp", 0, 0, 'U' },
39         { "port", 1, 0, 'P' },
40         { "port", 1, 0, 'p' },
41         { "debug", 0, 0, 'd' },
42         { "syslog", 0, 0, 's' },
43         { NULL, 0, 0, 0 }
44 };
45
46 /* given a family and ctlbits, disable any that aren't listed in netconfig */
47 #ifdef HAVE_LIBTIRPC
48 static void
49 nfsd_enable_protos(unsigned int *proto4, unsigned int *proto6)
50 {
51         struct netconfig *nconf;
52         unsigned int *famproto;
53         void *handle;
54
55         xlog(D_GENERAL, "Checking netconfig for visible protocols.");
56
57         handle = setnetconfig();
58         while((nconf = getnetconfig(handle))) {
59                 if (!(nconf->nc_flag & NC_VISIBLE))
60                         continue;
61
62                 if (!strcmp(nconf->nc_protofmly, NC_INET))
63                         famproto = proto4;
64                 else if (!strcmp(nconf->nc_protofmly, NC_INET6))
65                         famproto = proto6;
66                 else
67                         continue;
68
69                 if (!strcmp(nconf->nc_proto, NC_TCP))
70                         NFSCTL_TCPSET(*famproto);
71                 else if (!strcmp(nconf->nc_proto, NC_UDP))
72                         NFSCTL_UDPSET(*famproto);
73
74                 xlog(D_GENERAL, "Enabling %s %s.", nconf->nc_protofmly,
75                         nconf->nc_proto);
76         }
77         endnetconfig(handle);
78         return;
79 }
80 #else /* HAVE_LIBTIRPC */
81 static void
82 nfsd_enable_protos(unsigned int *proto4, unsigned int *proto6)
83 {
84         /* Enable all IPv4 protocols if no TIRPC support */
85         *proto4 = NFSCTL_ALLBITS;
86         *proto6 = 0;
87 }
88 #endif /* HAVE_LIBTIRPC */
89
90 int
91 main(int argc, char **argv)
92 {
93         int     count = 1, c, error = 0, portnum = 0, fd, found_one;
94         char *p, *progname, *port;
95         char *haddr = NULL;
96         int     socket_up = 0;
97         int minorvers4 = NFSD_MAXMINORVERS4;    /* nfsv4 minor version */
98         unsigned int versbits = NFSCTL_ALLBITS;
99         unsigned int protobits = NFSCTL_ALLBITS;
100         unsigned int proto4 = 0;
101         unsigned int proto6 = 0;
102
103         progname = strdup(basename(argv[0]));
104         if (!progname) {
105                 fprintf(stderr, "%s: unable to allocate memory.\n", argv[0]);
106                 exit(1);
107         }
108
109         port = strdup("nfs");
110         if (!port) {
111                 fprintf(stderr, "%s: unable to allocate memory.\n", progname);
112                 exit(1);
113         }
114
115         xlog_syslog(0);
116         xlog_stderr(1);
117
118         while ((c = getopt_long(argc, argv, "dH:hN:p:P:sTU", longopts, NULL)) != EOF) {
119                 switch(c) {
120                 case 'd':
121                         xlog_config(D_ALL, 1);
122                         break;
123                 case 'H':
124                         /*
125                          * for now, this only handles one -H option. Use the
126                          * last one specified.
127                          */
128                         free(haddr);
129                         haddr = strdup(optarg);
130                         if (!haddr) {
131                                 fprintf(stderr, "%s: unable to allocate "
132                                         "memory.\n", progname);
133                                 exit(1);
134                         }
135                         break;
136                 case 'P':       /* XXX for nfs-server compatibility */
137                 case 'p':
138                         /* only the last -p option has any effect */
139                         portnum = atoi(optarg);
140                         if (portnum <= 0 || portnum > 65535) {
141                                 fprintf(stderr, "%s: bad port number: %s\n",
142                                         progname, optarg);
143                                 usage(progname);
144                         }
145                         free(port);
146                         port = strdup(optarg);
147                         if (!port) {
148                                 fprintf(stderr, "%s: unable to allocate "
149                                                 "memory.\n", progname);
150                                 exit(1);
151                         }
152                         break;
153                 case 'N':
154                         switch((c = strtol(optarg, &p, 0))) {
155                         case 4:
156                                 if (*p == '.') {
157                                         minorvers4 = -atoi(p + 1);
158                                         break;
159                                 }
160                         case 3:
161                         case 2:
162                                 NFSCTL_VERUNSET(versbits, c);
163                                 break;
164                         default:
165                                 fprintf(stderr, "%s: Unsupported version\n", optarg);
166                                 exit(1);
167                         }
168                         break;
169                 case 's':
170                         xlog_syslog(1);
171                         xlog_stderr(0);
172                         break;
173                 case 'T':
174                         NFSCTL_TCPUNSET(protobits);
175                         break;
176                 case 'U':
177                         NFSCTL_UDPUNSET(protobits);
178                         break;
179                 default:
180                         fprintf(stderr, "Invalid argument: '%c'\n", c);
181                 case 'h':
182                         usage(progname);
183                 }
184         }
185
186         if (optind < argc) {
187                 if ((count = atoi(argv[optind])) < 0) {
188                         /* insane # of servers */
189                         fprintf(stderr,
190                                 "%s: invalid server count (%d), using 1\n",
191                                 argv[0], count);
192                         count = 1;
193                 } else if (count == 0) {
194                         /*
195                          * don't bother setting anything else if the threads
196                          * are coming down anyway.
197                          */
198                         socket_up = 1;
199                         goto set_threads;
200                 }
201         }
202
203         xlog_open(progname);
204
205         nfsd_enable_protos(&proto4, &proto6);
206
207         if (!NFSCTL_TCPISSET(protobits)) {
208                 NFSCTL_TCPUNSET(proto4);
209                 NFSCTL_TCPUNSET(proto6);
210         }
211
212         if (!NFSCTL_UDPISSET(protobits)) {
213                 NFSCTL_UDPUNSET(proto4);
214                 NFSCTL_UDPUNSET(proto6);
215         }
216
217         /* make sure that at least one version is enabled */
218         found_one = 0;
219         for (c = NFSD_MINVERS; c <= NFSD_MAXVERS; c++) {
220                 if (NFSCTL_VERISSET(versbits, c))
221                         found_one = 1;
222         }
223         if (!found_one) {
224                 xlog(L_ERROR, "no version specified");
225                 exit(1);
226         }                       
227
228         if (NFSCTL_VERISSET(versbits, 4) &&
229             !NFSCTL_TCPISSET(proto4) &&
230             !NFSCTL_TCPISSET(proto6)) {
231                 xlog(L_ERROR, "version 4 requires the TCP protocol");
232                 exit(1);
233         }
234
235         if (chdir(NFS_STATEDIR)) {
236                 xlog(L_ERROR, "chdir(%s) failed: %m", NFS_STATEDIR);
237                 exit(1);
238         }
239
240         /* make sure nfsdfs is mounted if it's available */
241         nfssvc_mount_nfsdfs(progname);
242
243         /* can only change number of threads if nfsd is already up */
244         if (nfssvc_inuse()) {
245                 socket_up = 1;
246                 goto set_threads;
247         }
248
249         /*
250          * must set versions before the fd's so that the right versions get
251          * registered with rpcbind. Note that on older kernels w/o the right
252          * interfaces, these are a no-op.
253          */
254         nfssvc_setvers(versbits, minorvers4);
255  
256         error = nfssvc_set_sockets(AF_INET, proto4, haddr, port);
257         if (!error)
258                 socket_up = 1;
259
260 #ifdef IPV6_SUPPORTED
261         error = nfssvc_set_sockets(AF_INET6, proto6, haddr, port);
262         if (!error)
263                 socket_up = 1;
264 #endif /* IPV6_SUPPORTED */
265
266 set_threads:
267         /* don't start any threads if unable to hand off any sockets */
268         if (!socket_up) {
269                 xlog(L_ERROR, "unable to set any sockets for nfsd");
270                 goto out;
271         }
272         error = 0;
273
274         /*
275          * KLUDGE ALERT:
276          * Some kernels let nfsd kernel threads inherit open files
277          * from the program that spawns them (i.e. us).  So close
278          * everything before spawning kernel threads.  --Chip
279          */
280         fd = open("/dev/null", O_RDWR);
281         if (fd == -1)
282                 xlog(L_ERROR, "Unable to open /dev/null: %m");
283         else {
284                 /* switch xlog output to syslog since stderr is being closed */
285                 xlog_syslog(1);
286                 xlog_stderr(0);
287                 (void) dup2(fd, 0);
288                 (void) dup2(fd, 1);
289                 (void) dup2(fd, 2);
290         }
291         closeall(3);
292
293         if ((error = nfssvc_threads(portnum, count)) < 0)
294                 xlog(L_ERROR, "error starting threads: errno %d (%m)", errno);
295 out:
296         free(port);
297         free(haddr);
298         free(progname);
299         return (error != 0);
300 }
301
302 static void
303 usage(const char *prog)
304 {
305         fprintf(stderr, "Usage:\n"
306                 "%s [-d|--debug] [-H hostname] [-p|-P|--port port] [-N|--no-nfs-version version ] [-s|--syslog] [-T|--no-tcp] [-U|--no-udp] nrservs\n", 
307                 prog);
308         exit(2);
309 }