1 /* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1989, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95"; 42 #endif 43 static const char rcsid[] = 44 "$FreeBSD$"; 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/syslog.h> 49 #include <sys/wait.h> 50 #include <sys/mount.h> 51 #include <sys/fcntl.h> 52 #include <sys/linker.h> 53 #include <sys/module.h> 54 #include <sys/types.h> 55 #include <sys/stat.h> 56 #include <sys/sysctl.h> 57 #include <sys/ucred.h> 58 59 #include <rpc/rpc.h> 60 #include <rpc/pmap_clnt.h> 61 #include <rpcsvc/nfs_prot.h> 62 63 #include <netdb.h> 64 #include <arpa/inet.h> 65 #include <nfsserver/nfs.h> 66 #include <nfs/nfssvc.h> 67 68 #include <err.h> 69 #include <errno.h> 70 #include <signal.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <string.h> 74 #include <unistd.h> 75 #include <sysexits.h> 76 77 #include <getopt.h> 78 79 static int debug = 0; 80 81 #define NFSD_STABLERESTART "/var/db/nfs-stablerestart" 82 #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak" 83 #define MAXNFSDCNT 256 84 #define DEFNFSDCNT 4 85 static pid_t children[MAXNFSDCNT]; /* PIDs of children */ 86 static int nfsdcnt; /* number of children */ 87 static int nfsdcnt_set; 88 static int minthreads; 89 static int maxthreads; 90 static int new_syscall; 91 static int run_v4server = 1; /* Force running of nfsv4 server */ 92 static int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */ 93 static int stablefd = -1; /* Fd for the stable restart file */ 94 static int backupfd; /* Fd for the backup stable restart file */ 95 static const char *getopt_shortopts; 96 static const char *getopt_usage; 97 98 static int minthreads_set; 99 static int maxthreads_set; 100 101 static struct option longopts[] = { 102 { "debug", no_argument, &debug, 1 }, 103 { "minthreads", required_argument, &minthreads_set, 1 }, 104 { "maxthreads", required_argument, &maxthreads_set, 1 }, 105 { NULL, 0, NULL, 0} 106 }; 107 108 static void cleanup(int); 109 static void child_cleanup(int); 110 static void killchildren(void); 111 static void nfsd_exit(int); 112 static void nonfs(int); 113 static void reapchild(int); 114 static int setbindhost(struct addrinfo **ia, const char *bindhost, 115 struct addrinfo hints); 116 static void start_server(int); 117 static void unregistration(void); 118 static void usage(void); 119 static void open_stable(int *, int *); 120 static void copy_stable(int, int); 121 static void backup_stable(int); 122 static void set_nfsdcnt(int); 123 124 /* 125 * Nfs server daemon mostly just a user context for nfssvc() 126 * 127 * 1 - do file descriptor and signal cleanup 128 * 2 - fork the nfsd(s) 129 * 3 - create server socket(s) 130 * 4 - register socket with rpcbind 131 * 132 * For connectionless protocols, just pass the socket into the kernel via. 133 * nfssvc(). 134 * For connection based sockets, loop doing accepts. When you get a new 135 * socket from accept, pass the msgsock into the kernel via. nfssvc(). 136 * The arguments are: 137 * -r - reregister with rpcbind 138 * -d - unregister with rpcbind 139 * -t - support tcp nfs clients 140 * -u - support udp nfs clients 141 * -e - forces it to run a server that supports nfsv4 142 * followed by "n" which is the number of nfsds' to fork off 143 */ 144 int 145 main(int argc, char **argv) 146 { 147 struct nfsd_addsock_args addsockargs; 148 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints; 149 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6; 150 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6; 151 struct sockaddr_in inetpeer; 152 struct sockaddr_in6 inet6peer; 153 fd_set ready, sockbits; 154 fd_set v4bits, v6bits; 155 int ch, connect_type_cnt, i, maxsock, msgsock; 156 socklen_t len; 157 int on = 1, unregister, reregister, sock; 158 int tcp6sock, ip6flag, tcpflag, tcpsock; 159 int udpflag, ecode, error, s, srvcnt; 160 int bindhostc, bindanyflag, rpcbreg, rpcbregcnt; 161 int nfssvc_addsock; 162 int longindex = 0; 163 const char *lopt; 164 char **bindhost = NULL; 165 pid_t pid; 166 167 nfsdcnt = DEFNFSDCNT; 168 unregister = reregister = tcpflag = maxsock = 0; 169 bindanyflag = udpflag = connect_type_cnt = bindhostc = 0; 170 getopt_shortopts = "ah:n:rdtueo"; 171 getopt_usage = 172 "usage:\n" 173 " nfsd [-ardtueo] [-h bindip]\n" 174 " [-n numservers] [--minthreads #] [--maxthreads #]\n"; 175 while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts, 176 &longindex)) != -1) 177 switch (ch) { 178 case 'a': 179 bindanyflag = 1; 180 break; 181 case 'n': 182 set_nfsdcnt(atoi(optarg)); 183 break; 184 case 'h': 185 bindhostc++; 186 bindhost = realloc(bindhost,sizeof(char *)*bindhostc); 187 if (bindhost == NULL) 188 errx(1, "Out of memory"); 189 bindhost[bindhostc-1] = strdup(optarg); 190 if (bindhost[bindhostc-1] == NULL) 191 errx(1, "Out of memory"); 192 break; 193 case 'r': 194 reregister = 1; 195 break; 196 case 'd': 197 unregister = 1; 198 break; 199 case 't': 200 tcpflag = 1; 201 break; 202 case 'u': 203 udpflag = 1; 204 break; 205 case 'e': 206 /* now a no-op, since this is the default */ 207 break; 208 case 'o': 209 run_v4server = 0; 210 break; 211 case 0: 212 lopt = longopts[longindex].name; 213 if (!strcmp(lopt, "minthreads")) { 214 minthreads = atoi(optarg); 215 } else if (!strcmp(lopt, "maxthreads")) { 216 maxthreads = atoi(optarg); 217 } 218 break; 219 default: 220 case '?': 221 usage(); 222 }; 223 if (!tcpflag && !udpflag) 224 udpflag = 1; 225 argv += optind; 226 argc -= optind; 227 if (minthreads_set && maxthreads_set && minthreads > maxthreads) 228 errx(EX_USAGE, 229 "error: minthreads(%d) can't be greater than " 230 "maxthreads(%d)", minthreads, maxthreads); 231 232 /* 233 * XXX 234 * Backward compatibility, trailing number is the count of daemons. 235 */ 236 if (argc > 1) 237 usage(); 238 if (argc == 1) 239 set_nfsdcnt(atoi(argv[0])); 240 241 /* 242 * Unless the "-o" option was specified, try and run "nfsd". 243 * If "-o" was specified, try and run "nfsserver". 244 */ 245 if (run_v4server > 0) { 246 if (modfind("nfsd") < 0) { 247 /* Not present in kernel, try loading it */ 248 if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 249 errx(1, "NFS server is not available"); 250 } 251 } else if (modfind("nfsserver") < 0) { 252 /* Not present in kernel, try loading it */ 253 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 254 errx(1, "NFS server is not available"); 255 } 256 257 ip6flag = 1; 258 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 259 if (s == -1) { 260 if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT) 261 err(1, "socket"); 262 ip6flag = 0; 263 } else if (getnetconfigent("udp6") == NULL || 264 getnetconfigent("tcp6") == NULL) { 265 ip6flag = 0; 266 } 267 if (s != -1) 268 close(s); 269 270 if (bindhostc == 0 || bindanyflag) { 271 bindhostc++; 272 bindhost = realloc(bindhost,sizeof(char *)*bindhostc); 273 if (bindhost == NULL) 274 errx(1, "Out of memory"); 275 bindhost[bindhostc-1] = strdup("*"); 276 if (bindhost[bindhostc-1] == NULL) 277 errx(1, "Out of memory"); 278 } 279 280 if (unregister) { 281 unregistration(); 282 exit (0); 283 } 284 if (reregister) { 285 if (udpflag) { 286 memset(&hints, 0, sizeof hints); 287 hints.ai_flags = AI_PASSIVE; 288 hints.ai_family = AF_INET; 289 hints.ai_socktype = SOCK_DGRAM; 290 hints.ai_protocol = IPPROTO_UDP; 291 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); 292 if (ecode != 0) 293 err(1, "getaddrinfo udp: %s", gai_strerror(ecode)); 294 nconf_udp = getnetconfigent("udp"); 295 if (nconf_udp == NULL) 296 err(1, "getnetconfigent udp failed"); 297 nb_udp.buf = ai_udp->ai_addr; 298 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; 299 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) || 300 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp))) 301 err(1, "rpcb_set udp failed"); 302 freeaddrinfo(ai_udp); 303 } 304 if (udpflag && ip6flag) { 305 memset(&hints, 0, sizeof hints); 306 hints.ai_flags = AI_PASSIVE; 307 hints.ai_family = AF_INET6; 308 hints.ai_socktype = SOCK_DGRAM; 309 hints.ai_protocol = IPPROTO_UDP; 310 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); 311 if (ecode != 0) 312 err(1, "getaddrinfo udp6: %s", gai_strerror(ecode)); 313 nconf_udp6 = getnetconfigent("udp6"); 314 if (nconf_udp6 == NULL) 315 err(1, "getnetconfigent udp6 failed"); 316 nb_udp6.buf = ai_udp6->ai_addr; 317 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; 318 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) || 319 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6))) 320 err(1, "rpcb_set udp6 failed"); 321 freeaddrinfo(ai_udp6); 322 } 323 if (tcpflag) { 324 memset(&hints, 0, sizeof hints); 325 hints.ai_flags = AI_PASSIVE; 326 hints.ai_family = AF_INET; 327 hints.ai_socktype = SOCK_STREAM; 328 hints.ai_protocol = IPPROTO_TCP; 329 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp); 330 if (ecode != 0) 331 err(1, "getaddrinfo tcp: %s", gai_strerror(ecode)); 332 nconf_tcp = getnetconfigent("tcp"); 333 if (nconf_tcp == NULL) 334 err(1, "getnetconfigent tcp failed"); 335 nb_tcp.buf = ai_tcp->ai_addr; 336 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; 337 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, &nb_tcp)) || 338 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, &nb_tcp))) 339 err(1, "rpcb_set tcp failed"); 340 freeaddrinfo(ai_tcp); 341 } 342 if (tcpflag && ip6flag) { 343 memset(&hints, 0, sizeof hints); 344 hints.ai_flags = AI_PASSIVE; 345 hints.ai_family = AF_INET6; 346 hints.ai_socktype = SOCK_STREAM; 347 hints.ai_protocol = IPPROTO_TCP; 348 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); 349 if (ecode != 0) 350 err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode)); 351 nconf_tcp6 = getnetconfigent("tcp6"); 352 if (nconf_tcp6 == NULL) 353 err(1, "getnetconfigent tcp6 failed"); 354 nb_tcp6.buf = ai_tcp6->ai_addr; 355 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; 356 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) || 357 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6))) 358 err(1, "rpcb_set tcp6 failed"); 359 freeaddrinfo(ai_tcp6); 360 } 361 exit (0); 362 } 363 if (debug == 0) { 364 daemon(0, 0); 365 (void)signal(SIGHUP, SIG_IGN); 366 (void)signal(SIGINT, SIG_IGN); 367 /* 368 * nfsd sits in the kernel most of the time. It needs 369 * to ignore SIGTERM/SIGQUIT in order to stay alive as long 370 * as possible during a shutdown, otherwise loopback 371 * mounts will not be able to unmount. 372 */ 373 (void)signal(SIGTERM, SIG_IGN); 374 (void)signal(SIGQUIT, SIG_IGN); 375 } 376 (void)signal(SIGSYS, nonfs); 377 (void)signal(SIGCHLD, reapchild); 378 (void)signal(SIGUSR2, backup_stable); 379 380 openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON); 381 382 /* 383 * For V4, we open the stablerestart file and call nfssvc() 384 * to get it loaded. This is done before the daemons do the 385 * regular nfssvc() call to service NFS requests. 386 * (This way the file remains open until the last nfsd is killed 387 * off.) 388 * It and the backup copy will be created as empty files 389 * the first time this nfsd is started and should never be 390 * deleted/replaced if at all possible. It should live on a 391 * local, non-volatile storage device that does not do hardware 392 * level write-back caching. (See SCSI doc for more information 393 * on how to prevent write-back caching on SCSI disks.) 394 */ 395 if (run_v4server > 0) { 396 open_stable(&stablefd, &backupfd); 397 if (stablefd < 0) { 398 syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART); 399 exit(1); 400 } 401 /* This system call will fail for old kernels, but that's ok. */ 402 nfssvc(NFSSVC_BACKUPSTABLE, NULL); 403 if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) { 404 syslog(LOG_ERR, "Can't read stable storage file: %m\n"); 405 exit(1); 406 } 407 nfssvc_addsock = NFSSVC_NFSDADDSOCK; 408 nfssvc_nfsd = NFSSVC_NFSDNFSD; 409 new_syscall = TRUE; 410 } else { 411 nfssvc_addsock = NFSSVC_ADDSOCK; 412 nfssvc_nfsd = NFSSVC_NFSD; 413 /* 414 * Figure out if the kernel supports the new-style 415 * NFSSVC_NFSD. Old kernels will return ENXIO because they 416 * don't recognise the flag value, new ones will return EINVAL 417 * because argp is NULL. 418 */ 419 new_syscall = FALSE; 420 if (nfssvc(NFSSVC_NFSD, NULL) < 0 && errno == EINVAL) 421 new_syscall = TRUE; 422 } 423 424 if (!new_syscall) { 425 /* If we use UDP only, we start the last server below. */ 426 srvcnt = tcpflag ? nfsdcnt : nfsdcnt - 1; 427 for (i = 0; i < srvcnt; i++) { 428 switch ((pid = fork())) { 429 case -1: 430 syslog(LOG_ERR, "fork: %m"); 431 nfsd_exit(1); 432 case 0: 433 break; 434 default: 435 children[i] = pid; 436 continue; 437 } 438 (void)signal(SIGUSR1, child_cleanup); 439 setproctitle("server"); 440 441 start_server(0); 442 } 443 } else if (tcpflag) { 444 /* 445 * For TCP mode, we fork once to start the first 446 * kernel nfsd thread. The kernel will add more 447 * threads as needed. 448 */ 449 pid = fork(); 450 if (pid == -1) { 451 syslog(LOG_ERR, "fork: %m"); 452 nfsd_exit(1); 453 } 454 if (pid) { 455 children[0] = pid; 456 } else { 457 (void)signal(SIGUSR1, child_cleanup); 458 setproctitle("server"); 459 start_server(0); 460 } 461 } 462 463 (void)signal(SIGUSR1, cleanup); 464 FD_ZERO(&v4bits); 465 FD_ZERO(&v6bits); 466 FD_ZERO(&sockbits); 467 468 rpcbregcnt = 0; 469 /* Set up the socket for udp and rpcb register it. */ 470 if (udpflag) { 471 rpcbreg = 0; 472 for (i = 0; i < bindhostc; i++) { 473 memset(&hints, 0, sizeof hints); 474 hints.ai_flags = AI_PASSIVE; 475 hints.ai_family = AF_INET; 476 hints.ai_socktype = SOCK_DGRAM; 477 hints.ai_protocol = IPPROTO_UDP; 478 if (setbindhost(&ai_udp, bindhost[i], hints) == 0) { 479 rpcbreg = 1; 480 rpcbregcnt++; 481 if ((sock = socket(ai_udp->ai_family, 482 ai_udp->ai_socktype, 483 ai_udp->ai_protocol)) < 0) { 484 syslog(LOG_ERR, 485 "can't create udp socket"); 486 nfsd_exit(1); 487 } 488 if (bind(sock, ai_udp->ai_addr, 489 ai_udp->ai_addrlen) < 0) { 490 syslog(LOG_ERR, 491 "can't bind udp addr %s: %m", 492 bindhost[i]); 493 nfsd_exit(1); 494 } 495 freeaddrinfo(ai_udp); 496 addsockargs.sock = sock; 497 addsockargs.name = NULL; 498 addsockargs.namelen = 0; 499 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { 500 syslog(LOG_ERR, "can't Add UDP socket"); 501 nfsd_exit(1); 502 } 503 (void)close(sock); 504 } 505 } 506 if (rpcbreg == 1) { 507 memset(&hints, 0, sizeof hints); 508 hints.ai_flags = AI_PASSIVE; 509 hints.ai_family = AF_INET; 510 hints.ai_socktype = SOCK_DGRAM; 511 hints.ai_protocol = IPPROTO_UDP; 512 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); 513 if (ecode != 0) { 514 syslog(LOG_ERR, "getaddrinfo udp: %s", 515 gai_strerror(ecode)); 516 nfsd_exit(1); 517 } 518 nconf_udp = getnetconfigent("udp"); 519 if (nconf_udp == NULL) 520 err(1, "getnetconfigent udp failed"); 521 nb_udp.buf = ai_udp->ai_addr; 522 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; 523 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) || 524 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp))) 525 err(1, "rpcb_set udp failed"); 526 freeaddrinfo(ai_udp); 527 } 528 } 529 530 /* Set up the socket for udp6 and rpcb register it. */ 531 if (udpflag && ip6flag) { 532 rpcbreg = 0; 533 for (i = 0; i < bindhostc; i++) { 534 memset(&hints, 0, sizeof hints); 535 hints.ai_flags = AI_PASSIVE; 536 hints.ai_family = AF_INET6; 537 hints.ai_socktype = SOCK_DGRAM; 538 hints.ai_protocol = IPPROTO_UDP; 539 if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) { 540 rpcbreg = 1; 541 rpcbregcnt++; 542 if ((sock = socket(ai_udp6->ai_family, 543 ai_udp6->ai_socktype, 544 ai_udp6->ai_protocol)) < 0) { 545 syslog(LOG_ERR, 546 "can't create udp6 socket"); 547 nfsd_exit(1); 548 } 549 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 550 &on, sizeof on) < 0) { 551 syslog(LOG_ERR, 552 "can't set v6-only binding for " 553 "udp6 socket: %m"); 554 nfsd_exit(1); 555 } 556 if (bind(sock, ai_udp6->ai_addr, 557 ai_udp6->ai_addrlen) < 0) { 558 syslog(LOG_ERR, 559 "can't bind udp6 addr %s: %m", 560 bindhost[i]); 561 nfsd_exit(1); 562 } 563 freeaddrinfo(ai_udp6); 564 addsockargs.sock = sock; 565 addsockargs.name = NULL; 566 addsockargs.namelen = 0; 567 if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { 568 syslog(LOG_ERR, 569 "can't add UDP6 socket"); 570 nfsd_exit(1); 571 } 572 (void)close(sock); 573 } 574 } 575 if (rpcbreg == 1) { 576 memset(&hints, 0, sizeof hints); 577 hints.ai_flags = AI_PASSIVE; 578 hints.ai_family = AF_INET6; 579 hints.ai_socktype = SOCK_DGRAM; 580 hints.ai_protocol = IPPROTO_UDP; 581 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); 582 if (ecode != 0) { 583 syslog(LOG_ERR, "getaddrinfo udp6: %s", 584 gai_strerror(ecode)); 585 nfsd_exit(1); 586 } 587 nconf_udp6 = getnetconfigent("udp6"); 588 if (nconf_udp6 == NULL) 589 err(1, "getnetconfigent udp6 failed"); 590 nb_udp6.buf = ai_udp6->ai_addr; 591 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; 592 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) || 593 (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6))) 594 err(1, "rpcb_set udp6 failed"); 595 freeaddrinfo(ai_udp6); 596 } 597 } 598 599 /* Set up the socket for tcp and rpcb register it. */ 600 if (tcpflag) { 601 rpcbreg = 0; 602 for (i = 0; i < bindhostc; i++) { 603 memset(&hints, 0, sizeof hints); 604 hints.ai_flags = AI_PASSIVE; 605 hints.ai_family = AF_INET; 606 hints.ai_socktype = SOCK_STREAM; 607 hints.ai_protocol = IPPROTO_TCP; 608 if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) { 609 rpcbreg = 1; 610 rpcbregcnt++; 611 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 612 0)) < 0) { 613 syslog(LOG_ERR, 614 "can't create tcp socket"); 615 nfsd_exit(1); 616 } 617 if (setsockopt(tcpsock, SOL_SOCKET, 618 SO_REUSEADDR, 619 (char *)&on, sizeof(on)) < 0) 620 syslog(LOG_ERR, 621 "setsockopt SO_REUSEADDR: %m"); 622 if (bind(tcpsock, ai_tcp->ai_addr, 623 ai_tcp->ai_addrlen) < 0) { 624 syslog(LOG_ERR, 625 "can't bind tcp addr %s: %m", 626 bindhost[i]); 627 nfsd_exit(1); 628 } 629 if (listen(tcpsock, -1) < 0) { 630 syslog(LOG_ERR, "listen failed"); 631 nfsd_exit(1); 632 } 633 freeaddrinfo(ai_tcp); 634 FD_SET(tcpsock, &sockbits); 635 FD_SET(tcpsock, &v4bits); 636 maxsock = tcpsock; 637 connect_type_cnt++; 638 } 639 } 640 if (rpcbreg == 1) { 641 memset(&hints, 0, sizeof hints); 642 hints.ai_flags = AI_PASSIVE; 643 hints.ai_family = AF_INET; 644 hints.ai_socktype = SOCK_STREAM; 645 hints.ai_protocol = IPPROTO_TCP; 646 ecode = getaddrinfo(NULL, "nfs", &hints, 647 &ai_tcp); 648 if (ecode != 0) { 649 syslog(LOG_ERR, "getaddrinfo tcp: %s", 650 gai_strerror(ecode)); 651 nfsd_exit(1); 652 } 653 nconf_tcp = getnetconfigent("tcp"); 654 if (nconf_tcp == NULL) 655 err(1, "getnetconfigent tcp failed"); 656 nb_tcp.buf = ai_tcp->ai_addr; 657 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; 658 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, 659 &nb_tcp)) || (!rpcb_set(NFS_PROGRAM, 3, 660 nconf_tcp, &nb_tcp))) 661 err(1, "rpcb_set tcp failed"); 662 freeaddrinfo(ai_tcp); 663 } 664 } 665 666 /* Set up the socket for tcp6 and rpcb register it. */ 667 if (tcpflag && ip6flag) { 668 rpcbreg = 0; 669 for (i = 0; i < bindhostc; i++) { 670 memset(&hints, 0, sizeof hints); 671 hints.ai_flags = AI_PASSIVE; 672 hints.ai_family = AF_INET6; 673 hints.ai_socktype = SOCK_STREAM; 674 hints.ai_protocol = IPPROTO_TCP; 675 if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) { 676 rpcbreg = 1; 677 rpcbregcnt++; 678 if ((tcp6sock = socket(ai_tcp6->ai_family, 679 ai_tcp6->ai_socktype, 680 ai_tcp6->ai_protocol)) < 0) { 681 syslog(LOG_ERR, 682 "can't create tcp6 socket"); 683 nfsd_exit(1); 684 } 685 if (setsockopt(tcp6sock, SOL_SOCKET, 686 SO_REUSEADDR, 687 (char *)&on, sizeof(on)) < 0) 688 syslog(LOG_ERR, 689 "setsockopt SO_REUSEADDR: %m"); 690 if (setsockopt(tcp6sock, IPPROTO_IPV6, 691 IPV6_V6ONLY, &on, sizeof on) < 0) { 692 syslog(LOG_ERR, 693 "can't set v6-only binding for tcp6 " 694 "socket: %m"); 695 nfsd_exit(1); 696 } 697 if (bind(tcp6sock, ai_tcp6->ai_addr, 698 ai_tcp6->ai_addrlen) < 0) { 699 syslog(LOG_ERR, 700 "can't bind tcp6 addr %s: %m", 701 bindhost[i]); 702 nfsd_exit(1); 703 } 704 if (listen(tcp6sock, -1) < 0) { 705 syslog(LOG_ERR, "listen failed"); 706 nfsd_exit(1); 707 } 708 freeaddrinfo(ai_tcp6); 709 FD_SET(tcp6sock, &sockbits); 710 FD_SET(tcp6sock, &v6bits); 711 if (maxsock < tcp6sock) 712 maxsock = tcp6sock; 713 connect_type_cnt++; 714 } 715 } 716 if (rpcbreg == 1) { 717 memset(&hints, 0, sizeof hints); 718 hints.ai_flags = AI_PASSIVE; 719 hints.ai_family = AF_INET6; 720 hints.ai_socktype = SOCK_STREAM; 721 hints.ai_protocol = IPPROTO_TCP; 722 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); 723 if (ecode != 0) { 724 syslog(LOG_ERR, "getaddrinfo tcp6: %s", 725 gai_strerror(ecode)); 726 nfsd_exit(1); 727 } 728 nconf_tcp6 = getnetconfigent("tcp6"); 729 if (nconf_tcp6 == NULL) 730 err(1, "getnetconfigent tcp6 failed"); 731 nb_tcp6.buf = ai_tcp6->ai_addr; 732 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; 733 if ((!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) || 734 (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6))) 735 err(1, "rpcb_set tcp6 failed"); 736 freeaddrinfo(ai_tcp6); 737 } 738 } 739 740 if (rpcbregcnt == 0) { 741 syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m"); 742 nfsd_exit(1); 743 } 744 745 if (tcpflag && connect_type_cnt == 0) { 746 syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m"); 747 nfsd_exit(1); 748 } 749 750 setproctitle("master"); 751 /* 752 * We always want a master to have a clean way to to shut nfsd down 753 * (with unregistration): if the master is killed, it unregisters and 754 * kills all children. If we run for UDP only (and so do not have to 755 * loop waiting waiting for accept), we instead make the parent 756 * a "server" too. start_server will not return. 757 */ 758 if (!tcpflag) 759 start_server(1); 760 761 /* 762 * Loop forever accepting connections and passing the sockets 763 * into the kernel for the mounts. 764 */ 765 for (;;) { 766 ready = sockbits; 767 if (connect_type_cnt > 1) { 768 if (select(maxsock + 1, 769 &ready, NULL, NULL, NULL) < 1) { 770 error = errno; 771 if (error == EINTR) 772 continue; 773 syslog(LOG_ERR, "select failed: %m"); 774 nfsd_exit(1); 775 } 776 } 777 for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) { 778 if (FD_ISSET(tcpsock, &ready)) { 779 if (FD_ISSET(tcpsock, &v4bits)) { 780 len = sizeof(inetpeer); 781 if ((msgsock = accept(tcpsock, 782 (struct sockaddr *)&inetpeer, &len)) < 0) { 783 error = errno; 784 syslog(LOG_ERR, "accept failed: %m"); 785 if (error == ECONNABORTED || 786 error == EINTR) 787 continue; 788 nfsd_exit(1); 789 } 790 memset(inetpeer.sin_zero, 0, 791 sizeof(inetpeer.sin_zero)); 792 if (setsockopt(msgsock, SOL_SOCKET, 793 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) 794 syslog(LOG_ERR, 795 "setsockopt SO_KEEPALIVE: %m"); 796 addsockargs.sock = msgsock; 797 addsockargs.name = (caddr_t)&inetpeer; 798 addsockargs.namelen = len; 799 nfssvc(nfssvc_addsock, &addsockargs); 800 (void)close(msgsock); 801 } else if (FD_ISSET(tcpsock, &v6bits)) { 802 len = sizeof(inet6peer); 803 if ((msgsock = accept(tcpsock, 804 (struct sockaddr *)&inet6peer, 805 &len)) < 0) { 806 error = errno; 807 syslog(LOG_ERR, 808 "accept failed: %m"); 809 if (error == ECONNABORTED || 810 error == EINTR) 811 continue; 812 nfsd_exit(1); 813 } 814 if (setsockopt(msgsock, SOL_SOCKET, 815 SO_KEEPALIVE, (char *)&on, 816 sizeof(on)) < 0) 817 syslog(LOG_ERR, "setsockopt " 818 "SO_KEEPALIVE: %m"); 819 addsockargs.sock = msgsock; 820 addsockargs.name = (caddr_t)&inet6peer; 821 addsockargs.namelen = len; 822 nfssvc(nfssvc_addsock, &addsockargs); 823 (void)close(msgsock); 824 } 825 } 826 } 827 } 828 } 829 830 static int 831 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints) 832 { 833 int ecode; 834 u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 835 const char *hostptr; 836 837 if (bindhost == NULL || strcmp("*", bindhost) == 0) 838 hostptr = NULL; 839 else 840 hostptr = bindhost; 841 842 if (hostptr != NULL) { 843 switch (hints.ai_family) { 844 case AF_INET: 845 if (inet_pton(AF_INET, hostptr, host_addr) == 1) { 846 hints.ai_flags = AI_NUMERICHOST; 847 } else { 848 if (inet_pton(AF_INET6, hostptr, 849 host_addr) == 1) 850 return (1); 851 } 852 break; 853 case AF_INET6: 854 if (inet_pton(AF_INET6, hostptr, host_addr) == 1) { 855 hints.ai_flags = AI_NUMERICHOST; 856 } else { 857 if (inet_pton(AF_INET, hostptr, 858 host_addr) == 1) 859 return (1); 860 } 861 break; 862 default: 863 break; 864 } 865 } 866 867 ecode = getaddrinfo(hostptr, "nfs", &hints, ai); 868 if (ecode != 0) { 869 syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost, 870 gai_strerror(ecode)); 871 return (1); 872 } 873 return (0); 874 } 875 876 static void 877 set_nfsdcnt(int proposed) 878 { 879 880 if (proposed < 1) { 881 warnx("nfsd count too low %d; reset to %d", proposed, 882 DEFNFSDCNT); 883 nfsdcnt = DEFNFSDCNT; 884 } else if (proposed > MAXNFSDCNT) { 885 warnx("nfsd count too high %d; truncated to %d", proposed, 886 MAXNFSDCNT); 887 nfsdcnt = MAXNFSDCNT; 888 } else 889 nfsdcnt = proposed; 890 nfsdcnt_set = 1; 891 } 892 893 static void 894 usage(void) 895 { 896 (void)fprintf(stderr, "%s", getopt_usage); 897 exit(1); 898 } 899 900 static void 901 nonfs(__unused int signo) 902 { 903 syslog(LOG_ERR, "missing system call: NFS not available"); 904 } 905 906 static void 907 reapchild(__unused int signo) 908 { 909 pid_t pid; 910 int i; 911 912 while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) { 913 for (i = 0; i < nfsdcnt; i++) 914 if (pid == children[i]) 915 children[i] = -1; 916 } 917 } 918 919 static void 920 unregistration(void) 921 { 922 if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) || 923 (!rpcb_unset(NFS_PROGRAM, 3, NULL))) 924 syslog(LOG_ERR, "rpcb_unset failed"); 925 } 926 927 static void 928 killchildren(void) 929 { 930 int i; 931 932 for (i = 0; i < nfsdcnt; i++) { 933 if (children[i] > 0) 934 kill(children[i], SIGKILL); 935 } 936 } 937 938 /* 939 * Cleanup master after SIGUSR1. 940 */ 941 static void 942 cleanup(__unused int signo) 943 { 944 nfsd_exit(0); 945 } 946 947 /* 948 * Cleanup child after SIGUSR1. 949 */ 950 static void 951 child_cleanup(__unused int signo) 952 { 953 exit(0); 954 } 955 956 static void 957 nfsd_exit(int status) 958 { 959 killchildren(); 960 unregistration(); 961 exit(status); 962 } 963 964 static int 965 get_tuned_nfsdcount(void) 966 { 967 int ncpu, error, tuned_nfsdcnt; 968 size_t ncpu_size; 969 970 ncpu_size = sizeof(ncpu); 971 error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0); 972 if (error) { 973 warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers", 974 DEFNFSDCNT); 975 tuned_nfsdcnt = DEFNFSDCNT; 976 } else { 977 tuned_nfsdcnt = ncpu * 8; 978 } 979 if (!new_syscall && tuned_nfsdcnt > MAXNFSDCNT) { 980 warnx("nfsd count %d; truncated to %d", tuned_nfsdcnt, 981 MAXNFSDCNT); 982 tuned_nfsdcnt = MAXNFSDCNT; 983 } 984 return tuned_nfsdcnt; 985 } 986 987 static void 988 start_server(int master) 989 { 990 char principal[MAXHOSTNAMELEN + 5]; 991 struct nfsd_nfsd_args nfsdargs; 992 int status, error; 993 char hostname[MAXHOSTNAMELEN + 1], *cp; 994 struct addrinfo *aip, hints; 995 996 status = 0; 997 if (new_syscall) { 998 gethostname(hostname, sizeof (hostname)); 999 snprintf(principal, sizeof (principal), "nfs@%s", hostname); 1000 if ((cp = strchr(hostname, '.')) == NULL || 1001 *(cp + 1) == '\0') { 1002 /* If not fully qualified, try getaddrinfo() */ 1003 memset((void *)&hints, 0, sizeof (hints)); 1004 hints.ai_flags = AI_CANONNAME; 1005 error = getaddrinfo(hostname, NULL, &hints, &aip); 1006 if (error == 0) { 1007 if (aip->ai_canonname != NULL && 1008 (cp = strchr(aip->ai_canonname, '.')) != 1009 NULL && *(cp + 1) != '\0') 1010 snprintf(principal, sizeof (principal), 1011 "nfs@%s", aip->ai_canonname); 1012 freeaddrinfo(aip); 1013 } 1014 } 1015 nfsdargs.principal = principal; 1016 1017 if (nfsdcnt_set) 1018 nfsdargs.minthreads = nfsdargs.maxthreads = nfsdcnt; 1019 else { 1020 nfsdargs.minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount(); 1021 nfsdargs.maxthreads = maxthreads_set ? maxthreads : nfsdargs.minthreads; 1022 if (nfsdargs.maxthreads < nfsdargs.minthreads) 1023 nfsdargs.maxthreads = nfsdargs.minthreads; 1024 } 1025 error = nfssvc(nfssvc_nfsd, &nfsdargs); 1026 if (error < 0 && errno == EAUTH) { 1027 /* 1028 * This indicates that it could not register the 1029 * rpcsec_gss credentials, usually because the 1030 * gssd daemon isn't running. 1031 * (only the experimental server with nfsv4) 1032 */ 1033 syslog(LOG_ERR, "No gssd, using AUTH_SYS only"); 1034 principal[0] = '\0'; 1035 error = nfssvc(nfssvc_nfsd, &nfsdargs); 1036 } 1037 if (error < 0) { 1038 syslog(LOG_ERR, "nfssvc: %m"); 1039 status = 1; 1040 } 1041 } else { 1042 if (nfssvc(NFSSVC_OLDNFSD, NULL) < 0) { 1043 syslog(LOG_ERR, "nfssvc: %m"); 1044 status = 1; 1045 } 1046 } 1047 if (master) 1048 nfsd_exit(status); 1049 else 1050 exit(status); 1051 } 1052 1053 /* 1054 * Open the stable restart file and return the file descriptor for it. 1055 */ 1056 static void 1057 open_stable(int *stable_fdp, int *backup_fdp) 1058 { 1059 int stable_fd, backup_fd = -1, ret; 1060 struct stat st, backup_st; 1061 1062 /* Open and stat the stable restart file. */ 1063 stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0); 1064 if (stable_fd < 0) 1065 stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600); 1066 if (stable_fd >= 0) { 1067 ret = fstat(stable_fd, &st); 1068 if (ret < 0) { 1069 close(stable_fd); 1070 stable_fd = -1; 1071 } 1072 } 1073 1074 /* Open and stat the backup stable restart file. */ 1075 if (stable_fd >= 0) { 1076 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0); 1077 if (backup_fd < 0) 1078 backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT, 1079 0600); 1080 if (backup_fd >= 0) { 1081 ret = fstat(backup_fd, &backup_st); 1082 if (ret < 0) { 1083 close(backup_fd); 1084 backup_fd = -1; 1085 } 1086 } 1087 if (backup_fd < 0) { 1088 close(stable_fd); 1089 stable_fd = -1; 1090 } 1091 } 1092 1093 *stable_fdp = stable_fd; 1094 *backup_fdp = backup_fd; 1095 if (stable_fd < 0) 1096 return; 1097 1098 /* Sync up the 2 files, as required. */ 1099 if (st.st_size > 0) 1100 copy_stable(stable_fd, backup_fd); 1101 else if (backup_st.st_size > 0) 1102 copy_stable(backup_fd, stable_fd); 1103 } 1104 1105 /* 1106 * Copy the stable restart file to the backup or vice versa. 1107 */ 1108 static void 1109 copy_stable(int from_fd, int to_fd) 1110 { 1111 int cnt, ret; 1112 static char buf[1024]; 1113 1114 ret = lseek(from_fd, (off_t)0, SEEK_SET); 1115 if (ret >= 0) 1116 ret = lseek(to_fd, (off_t)0, SEEK_SET); 1117 if (ret >= 0) 1118 ret = ftruncate(to_fd, (off_t)0); 1119 if (ret >= 0) 1120 do { 1121 cnt = read(from_fd, buf, 1024); 1122 if (cnt > 0) 1123 ret = write(to_fd, buf, cnt); 1124 else if (cnt < 0) 1125 ret = cnt; 1126 } while (cnt > 0 && ret >= 0); 1127 if (ret >= 0) 1128 ret = fsync(to_fd); 1129 if (ret < 0) 1130 syslog(LOG_ERR, "stable restart copy failure: %m"); 1131 } 1132 1133 /* 1134 * Back up the stable restart file when indicated by the kernel. 1135 */ 1136 static void 1137 backup_stable(__unused int signo) 1138 { 1139 1140 if (stablefd >= 0) 1141 copy_stable(stablefd, backupfd); 1142 } 1143 1144