2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/mount.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
39 #include <sys/ucred.h>
41 #include <sys/vnode.h>
44 #include <nfs/nfssvc.h>
48 #include <fs/nfs/rpcv2.h>
49 #include <fs/nfs/nfsproto.h>
50 #include <fs/nfs/nfskpiport.h>
51 #include <fs/nfs/nfs.h>
68 #define syslog(e, s) fprintf(stderr,(s))
74 static pid_t children;
76 static void nonfs(int);
77 static void reapchild(int);
78 static void usage(void);
79 static void cleanup(int);
80 static void child_cleanup(int);
81 static void nfscbd_exit(int);
82 static void killchildren(void);
85 * Nfs callback server daemon.
87 * 1 - do file descriptor and signal cleanup
88 * 2 - fork the nfscbd(s)
89 * 4 - create callback server socket(s)
90 * 5 - set up server socket for rpc
92 * For connectionless protocols, just pass the socket into the kernel via.
94 * For connection based sockets, loop doing accepts. When you get a new
95 * socket from accept, pass the msgsock into the kernel via. nfssvc().
98 main(int argc, char *argv[])
100 struct nfscbd_args nfscbdargs;
101 struct nfsd_nfscbd_args nfscbdargs2;
102 struct sockaddr_in inetaddr, inetpeer;
103 fd_set ready, sockbits;
104 int ch, connect_type_cnt, maxsock, msgsock, error;
105 int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
106 char *cp, princname[128];
107 char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
108 struct addrinfo *aip, hints;
110 short myport = NFSV4_CBPORT;
113 if (modfind("nfscl") < 0) {
114 /* Not present in kernel, try loading it */
115 if (kldload("nfscl") < 0 ||
116 modfind("nfscl") < 0)
117 errx(1, "nfscl is not available");
120 * First, get our fully qualified host name, if possible.
122 if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
123 cp = strchr(myname, '.');
124 if (cp != NULL && *(cp + 1) != '\0') {
128 * No domain on myname, so try looking it up.
131 memset((void *)&hints, 0, sizeof (hints));
132 hints.ai_flags = AI_CANONNAME;
133 error = getaddrinfo(myname, NULL, &hints, &aip);
135 if (aip->ai_canonname != NULL &&
136 (cp = strchr(aip->ai_canonname, '.')) != NULL
137 && *(cp + 1) != '\0') {
138 cp = aip->ai_canonname;
146 warnx("Can't get fully qualified host name");
151 #define GETOPT "p:P:"
152 #define USAGE "[ -p port_num ] [ -P client_principal ]"
153 while ((ch = getopt(argc, argv, GETOPT)) != -1)
156 myport = atoi(optarg);
158 warnx("port# non-positive, reset to %d",
160 myport = NFSV4_CBPORT;
165 if (cp != NULL && strlen(cp) > 0 &&
166 strlen(cp) < sizeof (princname)) {
167 if (strchr(cp, '@') == NULL &&
169 snprintf(princname, sizeof (princname),
170 "%s@%s", cp, myfqdnname);
172 strlcpy(princname, cp,
175 warnx("client princ invalid. ignored\n");
190 nfscbdargs2.principal = (const char *)princname;
193 (void)signal(SIGTERM, SIG_IGN);
194 (void)signal(SIGHUP, SIG_IGN);
195 (void)signal(SIGINT, SIG_IGN);
196 (void)signal(SIGQUIT, SIG_IGN);
198 (void)signal(SIGSYS, nonfs);
199 (void)signal(SIGCHLD, reapchild);
201 openlog("nfscbd:", LOG_PID, LOG_DAEMON);
205 syslog(LOG_ERR, "fork: %m");
207 } else if (pid > 0) {
210 (void)signal(SIGUSR1, child_cleanup);
211 setproctitle("server");
212 nfssvc_flag = NFSSVC_NFSCBD;
213 if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
214 syslog(LOG_ERR, "nfssvc: %m");
219 (void)signal(SIGUSR1, cleanup);
221 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
222 syslog(LOG_ERR, "can't create udp socket");
225 memset(&inetaddr, 0, sizeof inetaddr);
226 inetaddr.sin_family = AF_INET;
227 inetaddr.sin_addr.s_addr = INADDR_ANY;
228 inetaddr.sin_port = htons(myport);
229 inetaddr.sin_len = sizeof(inetaddr);
230 ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
231 /* If bind() fails, this is a restart, so just skip UDP. */
233 len = sizeof(inetaddr);
234 if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
235 syslog(LOG_ERR, "can't get bound addr");
238 nfscbdargs.port = ntohs(inetaddr.sin_port);
239 if (nfscbdargs.port != myport) {
240 syslog(LOG_ERR, "BAD PORT#");
243 nfscbdargs.sock = sock;
244 nfscbdargs.name = NULL;
245 nfscbdargs.namelen = 0;
246 if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
247 syslog(LOG_ERR, "can't Add UDP socket");
253 /* Now set up the master server socket waiting for tcp connections. */
256 connect_type_cnt = 0;
257 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
258 syslog(LOG_ERR, "can't create tcp socket");
261 if (setsockopt(tcpsock,
262 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
263 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
264 /* sin_port is already set */
265 inetaddr.sin_family = AF_INET;
266 inetaddr.sin_addr.s_addr = INADDR_ANY;
267 inetaddr.sin_port = htons(myport);
268 inetaddr.sin_len = sizeof(inetaddr);
270 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
271 syslog(LOG_ERR, "can't bind tcp addr");
274 if (listen(tcpsock, 5) < 0) {
275 syslog(LOG_ERR, "listen failed");
278 FD_SET(tcpsock, &sockbits);
282 setproctitle("master");
285 * Loop forever accepting connections and passing the sockets
286 * into the kernel for the mounts.
290 if (connect_type_cnt > 1) {
291 if (select(maxsock + 1,
292 &ready, NULL, NULL, NULL) < 1) {
293 syslog(LOG_ERR, "select failed: %m");
297 if (FD_ISSET(tcpsock, &ready)) {
298 len = sizeof(inetpeer);
299 if ((msgsock = accept(tcpsock,
300 (struct sockaddr *)&inetpeer, &len)) < 0) {
301 syslog(LOG_ERR, "accept failed: %m");
304 memset(inetpeer.sin_zero, 0,
305 sizeof (inetpeer.sin_zero));
306 if (setsockopt(msgsock, SOL_SOCKET,
307 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
309 "setsockopt SO_KEEPALIVE: %m");
310 nfscbdargs.sock = msgsock;
311 nfscbdargs.name = (caddr_t)&inetpeer;
312 nfscbdargs.namelen = sizeof(inetpeer);
313 nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
314 (void)close(msgsock);
323 errx(1, "usage: nfscbd %s", USAGE);
327 nonfs(int signo __unused)
329 syslog(LOG_ERR, "missing system call: NFS not available");
333 reapchild(int signo __unused)
337 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
348 kill(children, SIGKILL);
352 * Cleanup master after SIGUSR1.
355 cleanup(int signo __unused)
361 * Cleanup child after SIGUSR1.
364 child_cleanup(int signo __unused)
370 nfscbd_exit(int status __unused)