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