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))
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, len, 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;
112 if (modfind("nfscl") < 0) {
113 /* Not present in kernel, try loading it */
114 if (kldload("nfscl") < 0 ||
115 modfind("nfscl") < 0)
116 errx(1, "nfscl is not available");
119 * First, get our fully qualified host name, if possible.
121 if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
122 cp = strchr(myname, '.');
123 if (cp != NULL && *(cp + 1) != '\0') {
127 * No domain on myname, so try looking it up.
130 memset((void *)&hints, 0, sizeof (hints));
131 hints.ai_flags = AI_CANONNAME;
132 error = getaddrinfo(myname, NULL, &hints, &aip);
134 if (aip->ai_canonname != NULL &&
135 (cp = strchr(aip->ai_canonname, '.')) != NULL
136 && *(cp + 1) != '\0') {
137 cp = aip->ai_canonname;
145 warnx("Can't get fully qualified host name");
150 #define GETOPT "p:P:"
151 #define USAGE "[ -p port_num ] [ -P client_principal ]"
152 while ((ch = getopt(argc, argv, GETOPT)) != -1)
155 myport = atoi(optarg);
157 warnx("port# non-positive, reset to %d",
159 myport = NFSV4_CBPORT;
164 if (cp != NULL && strlen(cp) > 0 &&
165 strlen(cp) < sizeof (princname)) {
166 if (strchr(cp, '@') == NULL &&
168 snprintf(princname, sizeof (princname),
169 "%s@%s", cp, myfqdnname);
171 strlcpy(princname, cp,
174 warnx("client princ invalid. ignored\n");
189 nfscbdargs2.principal = (const char *)princname;
192 (void)signal(SIGTERM, SIG_IGN);
193 (void)signal(SIGHUP, SIG_IGN);
194 (void)signal(SIGINT, SIG_IGN);
195 (void)signal(SIGQUIT, SIG_IGN);
197 (void)signal(SIGSYS, nonfs);
198 (void)signal(SIGCHLD, reapchild);
200 openlog("nfscbd:", LOG_PID, LOG_DAEMON);
204 syslog(LOG_ERR, "fork: %m");
206 } else if (pid > 0) {
209 (void)signal(SIGUSR1, child_cleanup);
210 setproctitle("server");
211 nfssvc_flag = NFSSVC_NFSCBD;
212 if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
213 syslog(LOG_ERR, "nfssvc: %m");
218 (void)signal(SIGUSR1, cleanup);
220 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
221 syslog(LOG_ERR, "can't create udp socket");
224 memset(&inetaddr, 0, sizeof inetaddr);
225 inetaddr.sin_family = AF_INET;
226 inetaddr.sin_addr.s_addr = INADDR_ANY;
227 inetaddr.sin_port = htons(myport);
228 inetaddr.sin_len = sizeof(inetaddr);
229 ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
230 /* If bind() fails, this is a restart, so just skip UDP. */
232 len = sizeof(inetaddr);
233 if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
234 syslog(LOG_ERR, "can't get bound addr");
237 nfscbdargs.port = ntohs(inetaddr.sin_port);
238 if (nfscbdargs.port != myport) {
239 syslog(LOG_ERR, "BAD PORT#");
242 nfscbdargs.sock = sock;
243 nfscbdargs.name = NULL;
244 nfscbdargs.namelen = 0;
245 if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
246 syslog(LOG_ERR, "can't Add UDP socket");
252 /* Now set up the master server socket waiting for tcp connections. */
255 connect_type_cnt = 0;
256 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
257 syslog(LOG_ERR, "can't create tcp socket");
260 if (setsockopt(tcpsock,
261 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
262 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
263 /* sin_port is already set */
264 inetaddr.sin_family = AF_INET;
265 inetaddr.sin_addr.s_addr = INADDR_ANY;
266 inetaddr.sin_port = htons(myport);
267 inetaddr.sin_len = sizeof(inetaddr);
269 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
270 syslog(LOG_ERR, "can't bind tcp addr");
273 if (listen(tcpsock, 5) < 0) {
274 syslog(LOG_ERR, "listen failed");
277 FD_SET(tcpsock, &sockbits);
281 setproctitle("master");
284 * Loop forever accepting connections and passing the sockets
285 * into the kernel for the mounts.
289 if (connect_type_cnt > 1) {
290 if (select(maxsock + 1,
291 &ready, NULL, NULL, NULL) < 1) {
292 syslog(LOG_ERR, "select failed: %m");
296 if (FD_ISSET(tcpsock, &ready)) {
297 len = sizeof(inetpeer);
298 if ((msgsock = accept(tcpsock,
299 (struct sockaddr *)&inetpeer, &len)) < 0) {
300 syslog(LOG_ERR, "accept failed: %m");
303 memset(inetpeer.sin_zero, 0,
304 sizeof (inetpeer.sin_zero));
305 if (setsockopt(msgsock, SOL_SOCKET,
306 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
308 "setsockopt SO_KEEPALIVE: %m");
309 nfscbdargs.sock = msgsock;
310 nfscbdargs.name = (caddr_t)&inetpeer;
311 nfscbdargs.namelen = sizeof(inetpeer);
312 nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
313 (void)close(msgsock);
322 errx(1, "usage: nfscbd %s", USAGE);
326 nonfs(int signo __unused)
328 syslog(LOG_ERR, "missing system call: NFS not available");
332 reapchild(int signo __unused)
336 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
347 kill(children, SIGKILL);
351 * Cleanup master after SIGUSR1.
354 cleanup(int signo __unused)
360 * Cleanup child after SIGUSR1.
363 child_cleanup(int signo __unused)
369 nfscbd_exit(int status __unused)