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 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <sys/mount.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
37 #include <sys/ucred.h>
39 #include <sys/vnode.h>
42 #include <nfs/nfssvc.h>
46 #include <fs/nfs/rpcv2.h>
47 #include <fs/nfs/nfsproto.h>
48 #include <fs/nfs/nfskpiport.h>
49 #include <fs/nfs/nfs.h>
66 #define syslog(e, s) fprintf(stderr,(s))
72 static pid_t children;
74 static void nonfs(int);
75 static void reapchild(int);
76 static void usage(void);
77 static void cleanup(int);
78 static void child_cleanup(int);
79 static void nfscbd_exit(int);
80 static void killchildren(void);
83 * Nfs callback server daemon.
85 * 1 - do file descriptor and signal cleanup
86 * 2 - fork the nfscbd(s)
87 * 4 - create callback server socket(s)
88 * 5 - set up server socket for rpc
90 * For connectionless protocols, just pass the socket into the kernel via.
92 * For connection based sockets, loop doing accepts. When you get a new
93 * socket from accept, pass the msgsock into the kernel via. nfssvc().
96 main(int argc, char *argv[])
98 struct nfscbd_args nfscbdargs;
99 struct nfsd_nfscbd_args nfscbdargs2;
100 struct sockaddr_in inetaddr, inetpeer;
101 fd_set ready, sockbits;
102 int ch, connect_type_cnt, maxsock, msgsock, error;
103 int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
104 char *cp, princname[128];
105 char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
106 struct addrinfo *aip, hints;
108 short myport = NFSV4_CBPORT;
111 if (modfind("nfscl") < 0) {
112 /* Not present in kernel, try loading it */
113 if (kldload("nfscl") < 0 ||
114 modfind("nfscl") < 0)
115 errx(1, "nfscl is not available");
118 * First, get our fully qualified host name, if possible.
120 if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
121 cp = strchr(myname, '.');
122 if (cp != NULL && *(cp + 1) != '\0') {
126 * No domain on myname, so try looking it up.
129 memset((void *)&hints, 0, sizeof (hints));
130 hints.ai_flags = AI_CANONNAME;
131 error = getaddrinfo(myname, NULL, &hints, &aip);
133 if (aip->ai_canonname != NULL &&
134 (cp = strchr(aip->ai_canonname, '.')) != NULL
135 && *(cp + 1) != '\0') {
136 cp = aip->ai_canonname;
144 warnx("Can't get fully qualified host name");
149 #define GETOPT "p:P:"
150 #define USAGE "[ -p port_num ] [ -P client_principal ]"
151 while ((ch = getopt(argc, argv, GETOPT)) != -1)
154 myport = atoi(optarg);
156 warnx("port# non-positive, reset to %d",
158 myport = NFSV4_CBPORT;
163 if (cp != NULL && strlen(cp) > 0 &&
164 strlen(cp) < sizeof (princname)) {
165 if (strchr(cp, '@') == NULL &&
167 snprintf(princname, sizeof (princname),
168 "%s@%s", cp, myfqdnname);
170 strlcpy(princname, cp,
173 warnx("client princ invalid. ignored\n");
188 nfscbdargs2.principal = (const char *)princname;
191 (void)signal(SIGTERM, SIG_IGN);
192 (void)signal(SIGHUP, SIG_IGN);
193 (void)signal(SIGINT, SIG_IGN);
194 (void)signal(SIGQUIT, SIG_IGN);
196 (void)signal(SIGSYS, nonfs);
197 (void)signal(SIGCHLD, reapchild);
199 openlog("nfscbd:", LOG_PID, LOG_DAEMON);
203 syslog(LOG_ERR, "fork: %m");
205 } else if (pid > 0) {
208 (void)signal(SIGUSR1, child_cleanup);
209 setproctitle("server");
210 nfssvc_flag = NFSSVC_NFSCBD;
211 if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
212 syslog(LOG_ERR, "nfssvc: %m");
217 (void)signal(SIGUSR1, cleanup);
219 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
220 syslog(LOG_ERR, "can't create udp socket");
223 memset(&inetaddr, 0, sizeof inetaddr);
224 inetaddr.sin_family = AF_INET;
225 inetaddr.sin_addr.s_addr = INADDR_ANY;
226 inetaddr.sin_port = htons(myport);
227 inetaddr.sin_len = sizeof(inetaddr);
228 ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
229 /* If bind() fails, this is a restart, so just skip UDP. */
231 len = sizeof(inetaddr);
232 if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
233 syslog(LOG_ERR, "can't get bound addr");
236 nfscbdargs.port = ntohs(inetaddr.sin_port);
237 if (nfscbdargs.port != myport) {
238 syslog(LOG_ERR, "BAD PORT#");
241 nfscbdargs.sock = sock;
242 nfscbdargs.name = NULL;
243 nfscbdargs.namelen = 0;
244 if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
245 syslog(LOG_ERR, "can't Add UDP socket");
251 /* Now set up the master server socket waiting for tcp connections. */
254 connect_type_cnt = 0;
255 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
256 syslog(LOG_ERR, "can't create tcp socket");
259 if (setsockopt(tcpsock,
260 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
261 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
262 /* sin_port is already set */
263 inetaddr.sin_family = AF_INET;
264 inetaddr.sin_addr.s_addr = INADDR_ANY;
265 inetaddr.sin_port = htons(myport);
266 inetaddr.sin_len = sizeof(inetaddr);
268 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
269 syslog(LOG_ERR, "can't bind tcp addr");
272 if (listen(tcpsock, 5) < 0) {
273 syslog(LOG_ERR, "listen failed");
276 FD_SET(tcpsock, &sockbits);
280 setproctitle("master");
283 * Loop forever accepting connections and passing the sockets
284 * into the kernel for the mounts.
288 if (connect_type_cnt > 1) {
289 if (select(maxsock + 1,
290 &ready, NULL, NULL, NULL) < 1) {
291 syslog(LOG_ERR, "select failed: %m");
295 if (FD_ISSET(tcpsock, &ready)) {
296 len = sizeof(inetpeer);
297 if ((msgsock = accept(tcpsock,
298 (struct sockaddr *)&inetpeer, &len)) < 0) {
299 syslog(LOG_ERR, "accept failed: %m");
302 memset(inetpeer.sin_zero, 0,
303 sizeof (inetpeer.sin_zero));
304 if (setsockopt(msgsock, SOL_SOCKET,
305 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
307 "setsockopt SO_KEEPALIVE: %m");
308 nfscbdargs.sock = msgsock;
309 nfscbdargs.name = (caddr_t)&inetpeer;
310 nfscbdargs.namelen = sizeof(inetpeer);
311 nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
312 (void)close(msgsock);
321 errx(1, "usage: nfscbd %s", USAGE);
325 nonfs(int signo __unused)
327 syslog(LOG_ERR, "missing system call: NFS not available");
331 reapchild(int signo __unused)
335 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
346 kill(children, SIGKILL);
350 * Cleanup master after SIGUSR1.
353 cleanup(int signo __unused)
359 * Cleanup child after SIGUSR1.
362 child_cleanup(int signo __unused)
368 nfscbd_exit(int status __unused)