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