]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/nfsd/nfssvc.c
106f6e7621305c43b34bb713ee7287f1fae73405
[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 <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21
22 #include "nfslib.h"
23 #include "xlog.h"
24
25 #define NFSD_PORTS_FILE     "/proc/fs/nfsd/portlist"
26 #define NFSD_VERS_FILE    "/proc/fs/nfsd/versions"
27 #define NFSD_THREAD_FILE  "/proc/fs/nfsd/threads"
28
29 /*
30  * declaring a common static scratch buffer here keeps us from having to
31  * continually thrash the stack. The value of 128 bytes here is really just a
32  * SWAG and can be increased if necessary. It ought to be enough for the
33  * routines below however.
34  */
35 char buf[128];
36
37 /*
38  * Are there already sockets configured? If not, then it is safe to try to
39  * open some and pass them through.
40  *
41  * Note: If the user explicitly asked for 'udp', then we should probably check
42  * if that is open, and should open it if not. However we don't yet. All
43  * sockets have to be opened when the first daemon is started.
44  */
45 int
46 nfssvc_inuse(void)
47 {
48         int fd, n;
49
50         fd = open(NFSD_PORTS_FILE, O_RDONLY);
51
52         /* problem opening file, assume that nothing is configured */
53         if (fd < 0)
54                 return 0;
55
56         n = read(fd, buf, sizeof(buf));
57         close(fd);
58
59         xlog(D_GENERAL, "knfsd is currently %s", (n > 0) ? "up" : "down");
60
61         return (n > 0);
62 }
63
64 static int
65 nfssvc_setfds(const struct addrinfo *hints, const char *node, const char *port)
66 {
67         int fd, on = 1, fac = L_ERROR;
68         int sockfd = -1, rc = 0;
69         struct addrinfo *addrhead = NULL, *addr;
70         char *proto, *family;
71
72         /*
73          * if file can't be opened, then assume that it's not available and
74          * that the caller should just fall back to the old nfsctl interface
75          */
76         fd = open(NFSD_PORTS_FILE, O_WRONLY);
77         if (fd < 0)
78                 return 0;
79
80         switch(hints->ai_family) {
81         case AF_INET:
82                 family = "inet";
83                 break;
84         default:
85                 xlog(L_ERROR, "Unknown address family specified: %d\n",
86                                 hints->ai_family);
87                 rc = EAFNOSUPPORT;
88                 goto error;
89         }
90
91         rc = getaddrinfo(node, port, hints, &addrhead);
92         if (rc == EAI_NONAME && !strcmp(port, "nfs")) {
93                 snprintf(buf, sizeof(buf), "%d", NFS_PORT);
94                 rc = getaddrinfo(node, buf, hints, &addrhead);
95         }
96
97         if (rc != 0) {
98                 xlog(L_ERROR, "unable to resolve %s:%s to %s address: "
99                                 "%s", node ? node : "ANYADDR", port, family,
100                                 rc == EAI_SYSTEM ? strerror(errno) :
101                                         gai_strerror(rc));
102                 goto error;
103         }
104
105         addr = addrhead;
106         while(addr) {
107                 /* skip non-TCP / non-UDP sockets */
108                 switch(addr->ai_protocol) {
109                 case IPPROTO_UDP:
110                         proto = "UDP";
111                         break;
112                 case IPPROTO_TCP:
113                         proto = "TCP";
114                         break;
115                 default:
116                         addr = addr->ai_next;
117                         continue;
118                 }
119
120                 xlog(D_GENERAL, "Creating %s %s socket.", family, proto);
121
122                 /* open socket and prepare to hand it off to kernel */
123                 sockfd = socket(addr->ai_family, addr->ai_socktype,
124                                 addr->ai_protocol);
125                 if (sockfd < 0) {
126                         xlog(L_ERROR, "unable to create %s %s socket: "
127                                 "errno %d (%m)", family, proto, errno);
128                         rc = errno;
129                         goto error;
130                 }
131                 if (addr->ai_protocol == IPPROTO_TCP &&
132                     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
133                         xlog(L_ERROR, "unable to set SO_REUSEADDR on %s "
134                                 "socket: errno %d (%m)", family, errno);
135                         rc = errno;
136                         goto error;
137                 }
138                 if (bind(sockfd, addr->ai_addr, addr->ai_addrlen)) {
139                         xlog(L_ERROR, "unable to bind %s %s socket: "
140                                 "errno %d (%m)", family, proto, errno);
141                         rc = errno;
142                         goto error;
143                 }
144                 if (addr->ai_protocol == IPPROTO_TCP && listen(sockfd, 64)) {
145                         xlog(L_ERROR, "unable to create listening socket: "
146                                 "errno %d (%m)", errno);
147                         rc = errno;
148                         goto error;
149                 }
150
151                 if (fd < 0)
152                         fd = open(NFSD_PORTS_FILE, O_WRONLY);
153
154                 if (fd < 0) {
155                         xlog(L_ERROR, "couldn't open ports file: errno "
156                                       "%d (%m)", errno);
157                         goto error;
158                 }
159
160                 snprintf(buf, sizeof(buf), "%d\n", sockfd); 
161                 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
162                         /*
163                          * this error may be common on older kernels that don't
164                          * support IPv6, so turn into a debug message.
165                          */
166                         if (errno == EAFNOSUPPORT)
167                                 fac = D_ALL;
168                         xlog(fac, "writing fd to kernel failed: errno %d (%m)",
169                                   errno);
170                         rc = errno;
171                         goto error;
172                 }
173                 close(fd);
174                 close(sockfd);
175                 sockfd = fd = -1;
176                 addr = addr->ai_next;
177         }
178 error:
179         if (fd >= 0)
180                 close(fd);
181         if (sockfd >= 0)
182                 close(sockfd);
183         if (addrhead)
184                 freeaddrinfo(addrhead);
185         return rc;
186 }
187
188 int
189 nfssvc_set_sockets(const int family, const unsigned int protobits,
190                    const char *host, const char *port)
191 {
192         struct addrinfo hints = { .ai_flags = AI_PASSIVE | AI_ADDRCONFIG };
193
194         hints.ai_family = family;
195
196         if (!NFSCTL_ANYPROTO(protobits))
197                 return EPROTOTYPE;
198         else if (!NFSCTL_UDPISSET(protobits))
199                 hints.ai_protocol = IPPROTO_TCP;
200         else if (!NFSCTL_TCPISSET(protobits))
201                 hints.ai_protocol = IPPROTO_UDP;
202
203         return nfssvc_setfds(&hints, host, port);
204 }
205
206 void
207 nfssvc_setvers(unsigned int ctlbits, int minorvers4)
208 {
209         int fd, n, off;
210         char *ptr;
211
212         ptr = buf;
213         off = 0;
214         fd = open(NFSD_VERS_FILE, O_WRONLY);
215         if (fd < 0)
216                 return;
217
218         for (n = NFSD_MINVERS; n <= NFSD_MAXVERS; n++) {
219                 if (NFSCTL_VERISSET(ctlbits, n))
220                     off += snprintf(ptr+off, sizeof(buf) - off, "+%d ", n);
221                 else
222                     off += snprintf(ptr+off, sizeof(buf) - off, "-%d ", n);
223         }
224         n = minorvers4 >= 0 ? minorvers4 : -minorvers4;
225         if (n >= NFSD_MINMINORVERS4 && n <= NFSD_MAXMINORVERS4)
226                     off += snprintf(ptr+off, sizeof(buf) - off, "%c4.%d",
227                                     minorvers4 > 0 ? '+' : '-',
228                                     n);
229         xlog(D_GENERAL, "Writing version string to kernel: %s", buf);
230         snprintf(ptr+off, sizeof(buf) - off, "\n");
231         if (write(fd, buf, strlen(buf)) != strlen(buf))
232                 xlog(L_ERROR, "Setting version failed: errno %d (%m)", errno);
233
234         close(fd);
235
236         return;
237 }
238
239 int
240 nfssvc_threads(unsigned short port, const int nrservs)
241 {
242         struct nfsctl_arg       arg;
243         struct servent *ent;
244         ssize_t n;
245         int fd;
246
247         fd = open(NFSD_THREAD_FILE, O_WRONLY);
248         if (fd < 0)
249                 fd = open("/proc/fs/nfs/threads", O_WRONLY);
250         if (fd >= 0) {
251                 /* 2.5+ kernel with nfsd filesystem mounted.
252                  * Just write the number of threads.
253                  */
254                 snprintf(buf, sizeof(buf), "%d\n", nrservs);
255                 n = write(fd, buf, strlen(buf));
256                 close(fd);
257                 if (n != strlen(buf))
258                         return -1;
259                 else
260                         return 0;
261         }
262
263         if (!port) {
264                 ent = getservbyname("nfs", "udp");
265                 if (ent != NULL)
266                         port = ntohs(ent->s_port);
267                 else
268                         port = NFS_PORT;
269         }
270
271         arg.ca_version = NFSCTL_VERSION;
272         arg.ca_svc.svc_nthreads = nrservs;
273         arg.ca_svc.svc_port = port;
274         return nfsctl(NFSCTL_SVC, &arg, NULL);
275 }