]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfssvc.c
Imported Upstream version 1.2.3
[nfs-utils.git] / utils / nfsd / nfssvc.c
1 /*
2  * utils/nfsd/nfssvc.c
3  *
4  * Run an NFS daemon.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <stdlib.h>
23
24 #include "nfslib.h"
25 #include "xlog.h"
26
27 #ifndef NFSD_FS_DIR
28 #define NFSD_FS_DIR       "/proc/fs/nfsd"
29 #endif
30
31 #define NFSD_PORTS_FILE   NFSD_FS_DIR "/portlist"
32 #define NFSD_VERS_FILE    NFSD_FS_DIR "/versions"
33 #define NFSD_THREAD_FILE  NFSD_FS_DIR "/threads"
34
35 /*
36  * declaring a common static scratch buffer here keeps us from having to
37  * continually thrash the stack. The value of 128 bytes here is really just a
38  * SWAG and can be increased if necessary. It ought to be enough for the
39  * routines below however.
40  */
41 char buf[128];
42
43 /*
44  * Using the "new" interfaces for nfsd requires that /proc/fs/nfsd is
45  * actually mounted. Make an attempt to mount it here if it doesn't appear
46  * to be. If the mount attempt fails, no big deal -- fall back to using nfsctl
47  * instead.
48  */
49 void
50 nfssvc_mount_nfsdfs(char *progname)
51 {
52         int err;
53         struct stat statbuf;
54
55         err = stat(NFSD_THREAD_FILE, &statbuf);
56         if (err == 0)
57                 return;
58
59         if (errno != ENOENT) {
60                 xlog(L_ERROR, "Unable to stat %s: errno %d (%m)",
61                                 NFSD_THREAD_FILE, errno);
62                 return;
63         }
64
65         /*
66          * this call can return an error if modprobe is set up to automatically
67          * mount nfsdfs when nfsd.ko is plugged in. So, ignore the return
68          * code from it and just check for the "threads" file afterward.
69          */
70         system("/bin/mount -t nfsd nfsd " NFSD_FS_DIR " >/dev/null 2>&1");
71
72         err = stat(NFSD_THREAD_FILE, &statbuf);
73         if (err == 0)
74                 return;
75
76         xlog(L_WARNING, "Unable to access " NFSD_FS_DIR " errno %d (%m)." 
77                 "\nPlease try, as root, 'mount -t nfsd nfsd " NFSD_FS_DIR 
78                 "' and then restart %s to correct the problem", errno, progname);
79
80         return;
81 }
82
83 /*
84  * Are there already sockets configured? If not, then it is safe to try to
85  * open some and pass them through.
86  *
87  * Note: If the user explicitly asked for 'udp', then we should probably check
88  * if that is open, and should open it if not. However we don't yet. All
89  * sockets have to be opened when the first daemon is started.
90  */
91 int
92 nfssvc_inuse(void)
93 {
94         int fd, n;
95
96         fd = open(NFSD_PORTS_FILE, O_RDONLY);
97
98         /* problem opening file, assume that nothing is configured */
99         if (fd < 0)
100                 return 0;
101
102         n = read(fd, buf, sizeof(buf));
103         close(fd);
104
105         xlog(D_GENERAL, "knfsd is currently %s", (n > 0) ? "up" : "down");
106
107         return (n > 0);
108 }
109
110 static int
111 nfssvc_setfds(const struct addrinfo *hints, const char *node, const char *port)
112 {
113         int fd, on = 1, fac = L_ERROR;
114         int sockfd = -1, rc = 0;
115         struct addrinfo *addrhead = NULL, *addr;
116         char *proto, *family;
117
118         /*
119          * if file can't be opened, then assume that it's not available and
120          * that the caller should just fall back to the old nfsctl interface
121          */
122         fd = open(NFSD_PORTS_FILE, O_WRONLY);
123         if (fd < 0)
124                 return 0;
125
126         switch(hints->ai_family) {
127         case AF_INET:
128                 family = "inet";
129                 break;
130 #ifdef IPV6_SUPPORTED
131         case AF_INET6:
132                 family = "inet6";
133                 break;
134 #endif /* IPV6_SUPPORTED */
135         default:
136                 xlog(L_ERROR, "Unknown address family specified: %d\n",
137                                 hints->ai_family);
138                 rc = EAFNOSUPPORT;
139                 goto error;
140         }
141
142         rc = getaddrinfo(node, port, hints, &addrhead);
143         if (rc == EAI_NONAME && !strcmp(port, "nfs")) {
144                 snprintf(buf, sizeof(buf), "%d", NFS_PORT);
145                 rc = getaddrinfo(node, buf, hints, &addrhead);
146         }
147
148         if (rc != 0) {
149                 xlog(L_ERROR, "unable to resolve %s:%s to %s address: "
150                                 "%s", node ? node : "ANYADDR", port, family,
151                                 rc == EAI_SYSTEM ? strerror(errno) :
152                                         gai_strerror(rc));
153                 goto error;
154         }
155
156         addr = addrhead;
157         while(addr) {
158                 /* skip non-TCP / non-UDP sockets */
159                 switch(addr->ai_protocol) {
160                 case IPPROTO_UDP:
161                         proto = "UDP";
162                         break;
163                 case IPPROTO_TCP:
164                         proto = "TCP";
165                         break;
166                 default:
167                         addr = addr->ai_next;
168                         continue;
169                 }
170
171                 xlog(D_GENERAL, "Creating %s %s socket.", family, proto);
172
173                 /* open socket and prepare to hand it off to kernel */
174                 sockfd = socket(addr->ai_family, addr->ai_socktype,
175                                 addr->ai_protocol);
176                 if (sockfd < 0) {
177                         xlog(L_ERROR, "unable to create %s %s socket: "
178                                 "errno %d (%m)", family, proto, errno);
179                         rc = errno;
180                         goto error;
181                 }
182 #ifdef IPV6_SUPPORTED
183                 if (addr->ai_family == AF_INET6 &&
184                     setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on))) {
185                         xlog(L_ERROR, "unable to set IPV6_V6ONLY: "
186                                 "errno %d (%m)\n", errno);
187                         rc = errno;
188                         goto error;
189                 }
190 #endif /* IPV6_SUPPORTED */
191                 if (addr->ai_protocol == IPPROTO_TCP &&
192                     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
193                         xlog(L_ERROR, "unable to set SO_REUSEADDR on %s "
194                                 "socket: errno %d (%m)", family, errno);
195                         rc = errno;
196                         goto error;
197                 }
198                 if (bind(sockfd, addr->ai_addr, addr->ai_addrlen)) {
199                         xlog(L_ERROR, "unable to bind %s %s socket: "
200                                 "errno %d (%m)", family, proto, errno);
201                         rc = errno;
202                         goto error;
203                 }
204                 if (addr->ai_protocol == IPPROTO_TCP && listen(sockfd, 64)) {
205                         xlog(L_ERROR, "unable to create listening socket: "
206                                 "errno %d (%m)", errno);
207                         rc = errno;
208                         goto error;
209                 }
210
211                 if (fd < 0)
212                         fd = open(NFSD_PORTS_FILE, O_WRONLY);
213
214                 if (fd < 0) {
215                         xlog(L_ERROR, "couldn't open ports file: errno "
216                                       "%d (%m)", errno);
217                         goto error;
218                 }
219
220                 snprintf(buf, sizeof(buf), "%d\n", sockfd); 
221                 if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
222                         /*
223                          * this error may be common on older kernels that don't
224                          * support IPv6, so turn into a debug message.
225                          */
226                         if (errno == EAFNOSUPPORT)
227                                 fac = D_ALL;
228                         xlog(fac, "writing fd to kernel failed: errno %d (%m)",
229                                   errno);
230                         rc = errno;
231                         goto error;
232                 }
233                 close(fd);
234                 close(sockfd);
235                 sockfd = fd = -1;
236                 addr = addr->ai_next;
237         }
238 error:
239         if (fd >= 0)
240                 close(fd);
241         if (sockfd >= 0)
242                 close(sockfd);
243         if (addrhead)
244                 freeaddrinfo(addrhead);
245         return rc;
246 }
247
248 int
249 nfssvc_set_sockets(const int family, const unsigned int protobits,
250                    const char *host, const char *port)
251 {
252         struct addrinfo hints = { .ai_flags = AI_PASSIVE };
253
254         hints.ai_family = family;
255
256         if (!NFSCTL_ANYPROTO(protobits))
257                 return EPROTOTYPE;
258         else if (!NFSCTL_UDPISSET(protobits))
259                 hints.ai_protocol = IPPROTO_TCP;
260         else if (!NFSCTL_TCPISSET(protobits))
261                 hints.ai_protocol = IPPROTO_UDP;
262
263         return nfssvc_setfds(&hints, host, port);
264 }
265
266 void
267 nfssvc_setvers(unsigned int ctlbits, int minorvers4)
268 {
269         int fd, n, off;
270         char *ptr;
271
272         ptr = buf;
273         off = 0;
274         fd = open(NFSD_VERS_FILE, O_WRONLY);
275         if (fd < 0)
276                 return;
277
278         n = minorvers4 >= 0 ? minorvers4 : -minorvers4;
279         if (n >= NFSD_MINMINORVERS4 && n <= NFSD_MAXMINORVERS4)
280                     off += snprintf(ptr+off, sizeof(buf) - off, "%c4.%d ",
281                                     minorvers4 > 0 ? '+' : '-',
282                                     n);
283         for (n = NFSD_MINVERS; n <= NFSD_MAXVERS; n++) {
284                 if (NFSCTL_VERISSET(ctlbits, n))
285                     off += snprintf(ptr+off, sizeof(buf) - off, "+%d ", n);
286                 else
287                     off += snprintf(ptr+off, sizeof(buf) - off, "-%d ", n);
288         }
289         xlog(D_GENERAL, "Writing version string to kernel: %s", buf);
290         snprintf(ptr+off, sizeof(buf) - off, "\n");
291         if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
292                 xlog(L_ERROR, "Setting version failed: errno %d (%m)", errno);
293
294         close(fd);
295
296         return;
297 }
298
299 int
300 nfssvc_threads(unsigned short port, const int nrservs)
301 {
302         struct nfsctl_arg       arg;
303         struct servent *ent;
304         ssize_t n;
305         int fd;
306
307         fd = open(NFSD_THREAD_FILE, O_WRONLY);
308         if (fd < 0)
309                 fd = open("/proc/fs/nfs/threads", O_WRONLY);
310         if (fd >= 0) {
311                 /* 2.5+ kernel with nfsd filesystem mounted.
312                  * Just write the number of threads.
313                  */
314                 snprintf(buf, sizeof(buf), "%d\n", nrservs);
315                 n = write(fd, buf, strlen(buf));
316                 close(fd);
317                 if (n != (ssize_t)strlen(buf))
318                         return -1;
319                 else
320                         return 0;
321         }
322
323         if (!port) {
324                 ent = getservbyname("nfs", "udp");
325                 if (ent != NULL)
326                         port = ntohs(ent->s_port);
327                 else
328                         port = NFS_PORT;
329         }
330
331         arg.ca_version = NFSCTL_VERSION;
332         arg.ca_svc.svc_nthreads = nrservs;
333         arg.ca_svc.svc_port = port;
334         return nfsctl(NFSCTL_SVC, &arg, NULL);
335 }