Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.56
      1  1.56    pooka /*	$NetBSD: nfsd.c,v 1.56 2008/11/21 07:48:35 pooka Exp $	*/
      2  1.16      cgd 
      3   1.1      cgd /*
      4  1.11  mycroft  * Copyright (c) 1989, 1993, 1994
      5  1.11  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * This code is derived from software contributed to Berkeley by
      8   1.1      cgd  * Rick Macklem at The University of Guelph.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.43      agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1      cgd  *    may be used to endorse or promote products derived from this software
     20   1.1      cgd  *    without specific prior written permission.
     21   1.1      cgd  *
     22   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1      cgd  * SUCH DAMAGE.
     33   1.1      cgd  */
     34   1.1      cgd 
     35  1.23    lukem #include <sys/cdefs.h>
     36   1.1      cgd #ifndef lint
     37  1.54    lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
     38  1.54    lukem  The Regents of the University of California.  All rights reserved.");
     39  1.35      cgd #endif /* not lint */
     40   1.1      cgd 
     41   1.1      cgd #ifndef lint
     42  1.16      cgd #if 0
     43  1.18     fvdl static char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
     44  1.16      cgd #else
     45  1.56    pooka __RCSID("$NetBSD: nfsd.c,v 1.56 2008/11/21 07:48:35 pooka Exp $");
     46  1.16      cgd #endif
     47  1.18     fvdl #endif /* not lint */
     48   1.1      cgd 
     49  1.11  mycroft #include <sys/param.h>
     50   1.1      cgd #include <sys/ioctl.h>
     51   1.1      cgd #include <sys/stat.h>
     52   1.1      cgd #include <sys/wait.h>
     53  1.11  mycroft #include <sys/uio.h>
     54  1.11  mycroft #include <sys/ucred.h>
     55   1.1      cgd #include <sys/mount.h>
     56   1.1      cgd #include <sys/socket.h>
     57   1.1      cgd #include <sys/socketvar.h>
     58  1.39   itojun #include <poll.h>
     59  1.11  mycroft 
     60   1.1      cgd #include <rpc/rpc.h>
     61   1.1      cgd #include <rpc/pmap_clnt.h>
     62   1.1      cgd #include <rpc/pmap_prot.h>
     63  1.11  mycroft 
     64   1.1      cgd #include <nfs/rpcv2.h>
     65  1.18     fvdl #include <nfs/nfsproto.h>
     66  1.11  mycroft #include <nfs/nfs.h>
     67  1.11  mycroft 
     68  1.11  mycroft #include <err.h>
     69  1.11  mycroft #include <errno.h>
     70  1.11  mycroft #include <fcntl.h>
     71  1.11  mycroft #include <grp.h>
     72  1.11  mycroft #include <pwd.h>
     73  1.50     yamt #include <pthread.h>
     74  1.11  mycroft #include <signal.h>
     75  1.11  mycroft #include <stdio.h>
     76  1.11  mycroft #include <stdlib.h>
     77  1.25    perry #include <string.h>
     78  1.19  mycroft #include <syslog.h>
     79  1.11  mycroft #include <unistd.h>
     80  1.32     fvdl #include <netdb.h>
     81   1.1      cgd 
     82   1.1      cgd /* Global defs */
     83   1.1      cgd #ifdef DEBUG
     84  1.55    pooka #define	syslog(e, s, args...)						\
     85  1.55    pooka do {									\
     86  1.55    pooka     fprintf(stderr,(s), ## args);					\
     87  1.55    pooka     fprintf(stderr, "\n");						\
     88  1.55    pooka } while (/*CONSTCOND*/0)
     89   1.1      cgd int	debug = 1;
     90   1.1      cgd #else
     91   1.1      cgd int	debug = 0;
     92   1.1      cgd #endif
     93  1.11  mycroft 
     94  1.23    lukem int	main __P((int, char **));
     95  1.11  mycroft void	nonfs __P((int));
     96  1.11  mycroft void	usage __P((void));
     97   1.1      cgd 
     98  1.50     yamt static void *
     99  1.56    pooka worker(void *dummy)
    100  1.50     yamt {
    101  1.50     yamt 	struct	nfsd_srvargs nsd;
    102  1.50     yamt 	int nfssvc_flag;
    103  1.50     yamt 
    104  1.51     yamt 	pthread_setname_np(pthread_self(), "slave", NULL);
    105  1.50     yamt 	nfssvc_flag = NFSSVC_NFSD;
    106  1.50     yamt 	memset(&nsd, 0, sizeof(nsd));
    107  1.50     yamt 	while (nfssvc(nfssvc_flag, &nsd) < 0) {
    108  1.50     yamt 		if (errno != ENEEDAUTH) {
    109  1.50     yamt 			syslog(LOG_ERR, "nfssvc: %m");
    110  1.50     yamt 			exit(1);
    111  1.50     yamt 		}
    112  1.50     yamt 		nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
    113  1.50     yamt 	}
    114  1.50     yamt 
    115  1.50     yamt 	return NULL;
    116  1.50     yamt }
    117  1.50     yamt 
    118   1.1      cgd /*
    119   1.1      cgd  * Nfs server daemon mostly just a user context for nfssvc()
    120  1.11  mycroft  *
    121   1.1      cgd  * 1 - do file descriptor and signal cleanup
    122  1.50     yamt  * 2 - create the nfsd thread(s)
    123  1.11  mycroft  * 3 - create server socket(s)
    124  1.11  mycroft  * 4 - register socket with portmap
    125  1.11  mycroft  *
    126  1.28    lukem  * For connectionless protocols, just pass the socket into the kernel via
    127  1.11  mycroft  * nfssvc().
    128  1.11  mycroft  * For connection based sockets, loop doing accepts. When you get a new
    129  1.28    lukem  * socket from accept, pass the msgsock into the kernel via nfssvc().
    130   1.1      cgd  * The arguments are:
    131  1.11  mycroft  *	-c - support iso cltp clients
    132  1.11  mycroft  *	-r - reregister with portmapper
    133  1.11  mycroft  *	-t - support tcp nfs clients
    134  1.11  mycroft  *	-u - support udp nfs clients
    135  1.50     yamt  * followed by "n" which is the number of nfsd threads to create
    136   1.1      cgd  */
    137  1.11  mycroft int
    138  1.23    lukem main(argc, argv)
    139   1.1      cgd 	int argc;
    140  1.23    lukem 	char *argv[];
    141   1.1      cgd {
    142  1.11  mycroft 	struct nfsd_args nfsdargs;
    143  1.32     fvdl 	struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
    144  1.32     fvdl 	struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
    145  1.32     fvdl 	struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
    146  1.32     fvdl 	struct sockaddr_in inetpeer;
    147  1.32     fvdl 	struct sockaddr_in6 inet6peer;
    148  1.37  mycroft 	struct pollfd set[4];
    149  1.47      mrg 	socklen_t len;
    150  1.47      mrg 	int ch, cltpflag, connect_type_cnt, i, maxsock, msgsock;
    151  1.50     yamt 	int nfsdcnt, on = 1, reregister, sock, tcpflag, tcpsock;
    152  1.32     fvdl 	int tcp6sock, ip6flag;
    153  1.32     fvdl 	int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag, ecode, s;
    154  1.11  mycroft 
    155  1.11  mycroft #define	DEFNFSDCNT	 4
    156  1.11  mycroft 	nfsdcnt = DEFNFSDCNT;
    157  1.11  mycroft 	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
    158  1.32     fvdl 	tpipflag = udpflag = ip6flag = 0;
    159  1.45    lukem 	nconf_udp = nconf_tcp = nconf_udp6 = nconf_tcp6 = NULL;
    160  1.45    lukem 	maxsock = 0;
    161  1.45    lukem 	tcpsock = tcp6sock = -1;
    162  1.32     fvdl #define	GETOPT	"6n:rtu"
    163  1.11  mycroft #define	USAGE	"[-rtu] [-n num_servers]"
    164  1.30  thorpej 	while ((ch = getopt(argc, argv, GETOPT)) != -1) {
    165  1.11  mycroft 		switch (ch) {
    166  1.32     fvdl 		case '6':
    167  1.32     fvdl 			ip6flag = 1;
    168  1.32     fvdl 			s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    169  1.32     fvdl 			if (s < 0 && (errno == EPROTONOSUPPORT ||
    170  1.32     fvdl 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
    171  1.32     fvdl 				ip6flag = 0;
    172  1.32     fvdl 			else
    173  1.32     fvdl 				close(s);
    174  1.32     fvdl 			break;
    175  1.11  mycroft 		case 'n':
    176  1.11  mycroft 			nfsdcnt = atoi(optarg);
    177  1.53     yamt 			if (nfsdcnt < 1) {
    178  1.21     fvdl 				warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    179  1.11  mycroft 				nfsdcnt = DEFNFSDCNT;
    180  1.11  mycroft 			}
    181  1.11  mycroft 			break;
    182   1.1      cgd 		case 'r':
    183  1.11  mycroft 			reregister = 1;
    184   1.1      cgd 			break;
    185   1.1      cgd 		case 't':
    186  1.11  mycroft 			tcpflag = 1;
    187   1.1      cgd 			break;
    188   1.1      cgd 		case 'u':
    189  1.11  mycroft 			udpflag = 1;
    190  1.11  mycroft 			break;
    191   1.1      cgd 		default:
    192   1.1      cgd 		case '?':
    193   1.1      cgd 			usage();
    194  1.11  mycroft 		};
    195  1.30  thorpej 	}
    196  1.11  mycroft 	argv += optind;
    197  1.11  mycroft 	argc -= optind;
    198   1.1      cgd 
    199   1.1      cgd 	/*
    200  1.11  mycroft 	 * XXX
    201  1.11  mycroft 	 * Backward compatibility, trailing number is the count of daemons.
    202   1.1      cgd 	 */
    203  1.11  mycroft 	if (argc > 1)
    204  1.11  mycroft 		usage();
    205  1.11  mycroft 	if (argc == 1) {
    206  1.11  mycroft 		nfsdcnt = atoi(argv[0]);
    207  1.53     yamt 		if (nfsdcnt < 1) {
    208  1.21     fvdl 			warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    209  1.11  mycroft 			nfsdcnt = DEFNFSDCNT;
    210  1.11  mycroft 		}
    211   1.1      cgd 	}
    212  1.31    soren 
    213  1.31    soren 	/*
    214  1.31    soren 	 * If none of TCP or UDP are specified, default to UDP only.
    215  1.31    soren 	 */
    216  1.31    soren 	if (tcpflag == 0 && udpflag == 0)
    217  1.31    soren 		udpflag = 1;
    218   1.1      cgd 
    219   1.1      cgd 	if (debug == 0) {
    220   1.1      cgd 		daemon(0, 0);
    221  1.11  mycroft 		(void)signal(SIGHUP, SIG_IGN);
    222  1.11  mycroft 		(void)signal(SIGINT, SIG_IGN);
    223  1.11  mycroft 		(void)signal(SIGQUIT, SIG_IGN);
    224  1.11  mycroft 		(void)signal(SIGSYS, nonfs);
    225   1.1      cgd 	}
    226  1.11  mycroft 
    227  1.32     fvdl 	if (udpflag) {
    228  1.32     fvdl 		memset(&hints, 0, sizeof hints);
    229  1.32     fvdl 		hints.ai_flags = AI_PASSIVE;
    230  1.32     fvdl 		hints.ai_family = PF_INET;
    231  1.32     fvdl 		hints.ai_socktype = SOCK_DGRAM;
    232  1.32     fvdl 		hints.ai_protocol = IPPROTO_UDP;
    233  1.32     fvdl 
    234  1.32     fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
    235  1.32     fvdl 		if (ecode != 0) {
    236  1.32     fvdl 			syslog(LOG_ERR, "getaddrinfo udp: %s",
    237  1.32     fvdl 			    gai_strerror(ecode));
    238  1.32     fvdl 			exit(1);
    239  1.32     fvdl 		}
    240  1.32     fvdl 
    241  1.32     fvdl 		nconf_udp = getnetconfigent("udp");
    242  1.32     fvdl 
    243  1.32     fvdl 		if (nconf_udp == NULL)
    244  1.32     fvdl 			err(1, "getnetconfigent udp failed");
    245  1.32     fvdl 
    246  1.32     fvdl 		nb_udp.buf = ai_udp->ai_addr;
    247  1.32     fvdl 		nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
    248  1.32     fvdl 		if (reregister)
    249  1.32     fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp))
    250  1.32     fvdl 				err(1, "rpcb_set udp failed");
    251  1.32     fvdl 	}
    252  1.32     fvdl 
    253  1.32     fvdl 	if (tcpflag) {
    254  1.32     fvdl 		memset(&hints, 0, sizeof hints);
    255  1.32     fvdl 		hints.ai_flags = AI_PASSIVE;
    256  1.32     fvdl 		hints.ai_family = PF_INET;
    257  1.32     fvdl 		hints.ai_socktype = SOCK_STREAM;
    258  1.32     fvdl 		hints.ai_protocol = IPPROTO_TCP;
    259  1.32     fvdl 
    260  1.32     fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
    261  1.32     fvdl 		if (ecode != 0) {
    262  1.48  hubertf 			syslog(LOG_ERR, "getaddrinfo tcp: %s",
    263  1.32     fvdl 			    gai_strerror(ecode));
    264  1.32     fvdl 			exit(1);
    265  1.32     fvdl 		}
    266  1.32     fvdl 
    267  1.32     fvdl 		nconf_tcp = getnetconfigent("tcp");
    268  1.32     fvdl 
    269  1.32     fvdl 		if (nconf_tcp == NULL)
    270  1.32     fvdl 			err(1, "getnetconfigent tcp failed");
    271  1.32     fvdl 
    272  1.32     fvdl 		nb_tcp.buf = ai_tcp->ai_addr;
    273  1.32     fvdl 		nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
    274  1.32     fvdl 		if (reregister)
    275  1.32     fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp))
    276  1.32     fvdl 				err(1, "rpcb_set tcp failed");
    277  1.32     fvdl 	}
    278  1.32     fvdl 
    279  1.32     fvdl 	if (udpflag && ip6flag) {
    280  1.32     fvdl 		memset(&hints, 0, sizeof hints);
    281  1.32     fvdl 		hints.ai_flags = AI_PASSIVE;
    282  1.32     fvdl 		hints.ai_family = PF_INET6;
    283  1.32     fvdl 		hints.ai_socktype = SOCK_DGRAM;
    284  1.32     fvdl 		hints.ai_protocol = IPPROTO_UDP;
    285  1.32     fvdl 
    286  1.32     fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
    287  1.32     fvdl 		if (ecode != 0) {
    288  1.32     fvdl 			syslog(LOG_ERR, "getaddrinfo udp: %s",
    289  1.32     fvdl 			    gai_strerror(ecode));
    290  1.32     fvdl 			exit(1);
    291  1.32     fvdl 		}
    292  1.32     fvdl 
    293  1.32     fvdl 		nconf_udp6 = getnetconfigent("udp6");
    294  1.32     fvdl 
    295  1.32     fvdl 		if (nconf_udp6 == NULL)
    296  1.32     fvdl 			err(1, "getnetconfigent udp6 failed");
    297  1.32     fvdl 
    298  1.32     fvdl 		nb_udp6.buf = ai_udp6->ai_addr;
    299  1.32     fvdl 		nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
    300  1.32     fvdl 		if (reregister)
    301  1.32     fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6))
    302  1.32     fvdl 				err(1, "rpcb_set udp6 failed");
    303  1.32     fvdl 	}
    304  1.32     fvdl 
    305  1.32     fvdl 	if (tcpflag && ip6flag) {
    306  1.32     fvdl 		memset(&hints, 0, sizeof hints);
    307  1.32     fvdl 		hints.ai_flags = AI_PASSIVE;
    308  1.32     fvdl 		hints.ai_family = PF_INET6;
    309  1.32     fvdl 		hints.ai_socktype = SOCK_STREAM;
    310  1.32     fvdl 		hints.ai_protocol = IPPROTO_TCP;
    311  1.32     fvdl 
    312  1.32     fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
    313  1.32     fvdl 		if (ecode != 0) {
    314  1.48  hubertf 			syslog(LOG_ERR, "getaddrinfo tcp: %s",
    315  1.32     fvdl 			    gai_strerror(ecode));
    316  1.32     fvdl 			exit(1);
    317  1.32     fvdl 		}
    318  1.32     fvdl 
    319  1.32     fvdl 		nconf_tcp6 = getnetconfigent("tcp6");
    320  1.32     fvdl 
    321  1.32     fvdl 		if (nconf_tcp6 == NULL)
    322  1.32     fvdl 			err(1, "getnetconfigent tcp6 failed");
    323  1.32     fvdl 
    324  1.32     fvdl 		nb_tcp6.buf = ai_tcp6->ai_addr;
    325  1.32     fvdl 		nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
    326  1.32     fvdl 		if (reregister)
    327  1.32     fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6))
    328  1.32     fvdl 				err(1, "rpcb_set tcp6 failed");
    329   1.8      cgd 	}
    330  1.32     fvdl 
    331  1.33    lukem 	openlog("nfsd", LOG_PID, LOG_DAEMON);
    332  1.11  mycroft 
    333  1.11  mycroft 	for (i = 0; i < nfsdcnt; i++) {
    334  1.50     yamt 		pthread_t t;
    335  1.50     yamt 		int error;
    336  1.50     yamt 
    337  1.56    pooka 		error = pthread_create(&t, NULL, worker, NULL);
    338  1.50     yamt 		if (error) {
    339  1.50     yamt 			errno = error;
    340  1.50     yamt 			syslog(LOG_ERR, "pthread_create: %m");
    341  1.11  mycroft 			exit (1);
    342   1.1      cgd 		}
    343   1.1      cgd 	}
    344  1.11  mycroft 
    345  1.11  mycroft 	/* If we are serving udp, set up the socket. */
    346   1.1      cgd 	if (udpflag) {
    347  1.32     fvdl 		if ((sock = socket(ai_udp->ai_family, ai_udp->ai_socktype,
    348  1.32     fvdl 		    ai_udp->ai_protocol)) < 0) {
    349  1.11  mycroft 			syslog(LOG_ERR, "can't create udp socket");
    350   1.1      cgd 			exit(1);
    351   1.1      cgd 		}
    352  1.32     fvdl 		if (bind(sock, ai_udp->ai_addr, ai_udp->ai_addrlen) < 0) {
    353  1.32     fvdl 			syslog(LOG_ERR, "can't bind udp addr");
    354  1.32     fvdl 			exit(1);
    355  1.32     fvdl 		}
    356  1.32     fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp) ||
    357  1.32     fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_udp, &nb_udp)) {
    358  1.32     fvdl 			syslog(LOG_ERR, "can't register with udp portmap");
    359  1.32     fvdl 			exit(1);
    360  1.32     fvdl 		}
    361  1.32     fvdl 		nfsdargs.sock = sock;
    362  1.32     fvdl 		nfsdargs.name = NULL;
    363  1.32     fvdl 		nfsdargs.namelen = 0;
    364  1.32     fvdl 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    365  1.32     fvdl 			syslog(LOG_ERR, "can't add UDP socket");
    366  1.32     fvdl 			exit(1);
    367  1.32     fvdl 		}
    368  1.32     fvdl 		(void)close(sock);
    369  1.32     fvdl 	}
    370  1.32     fvdl 
    371  1.32     fvdl 	if (udpflag &&ip6flag) {
    372  1.32     fvdl 		if ((sock = socket(ai_udp6->ai_family, ai_udp6->ai_socktype,
    373  1.32     fvdl 		    ai_udp6->ai_protocol)) < 0) {
    374  1.32     fvdl 			syslog(LOG_ERR, "can't create udp socket");
    375  1.32     fvdl 			exit(1);
    376  1.32     fvdl 		}
    377  1.36   itojun 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
    378  1.32     fvdl 		    &on, sizeof on) < 0) {
    379  1.32     fvdl 			syslog(LOG_ERR, "can't set v6-only binding for udp6 "
    380  1.32     fvdl 					"socket: %m");
    381  1.32     fvdl 			exit(1);
    382  1.32     fvdl 		}
    383  1.32     fvdl 		if (bind(sock, ai_udp6->ai_addr, ai_udp6->ai_addrlen) < 0) {
    384  1.11  mycroft 			syslog(LOG_ERR, "can't bind udp addr");
    385   1.1      cgd 			exit(1);
    386   1.1      cgd 		}
    387  1.32     fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6) ||
    388  1.32     fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_udp6, &nb_udp6)) {
    389  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    390  1.11  mycroft 			exit(1);
    391  1.11  mycroft 		}
    392  1.11  mycroft 		nfsdargs.sock = sock;
    393  1.11  mycroft 		nfsdargs.name = NULL;
    394  1.11  mycroft 		nfsdargs.namelen = 0;
    395  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    396  1.32     fvdl 			syslog(LOG_ERR, "can't add UDP6 socket");
    397  1.11  mycroft 			exit(1);
    398  1.11  mycroft 		}
    399  1.11  mycroft 		(void)close(sock);
    400  1.11  mycroft 	}
    401  1.11  mycroft 
    402  1.11  mycroft 	/* Now set up the master server socket waiting for tcp connections. */
    403  1.11  mycroft 	on = 1;
    404  1.11  mycroft 	connect_type_cnt = 0;
    405   1.1      cgd 	if (tcpflag) {
    406  1.32     fvdl 		if ((tcpsock = socket(ai_tcp->ai_family, ai_tcp->ai_socktype,
    407  1.32     fvdl 		    ai_tcp->ai_protocol)) < 0) {
    408  1.11  mycroft 			syslog(LOG_ERR, "can't create tcp socket");
    409  1.11  mycroft 			exit(1);
    410  1.11  mycroft 		}
    411  1.11  mycroft 		if (setsockopt(tcpsock,
    412  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    413  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    414  1.32     fvdl 		if (bind(tcpsock, ai_tcp->ai_addr, ai_tcp->ai_addrlen) < 0) {
    415  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    416  1.11  mycroft 			exit(1);
    417  1.11  mycroft 		}
    418  1.11  mycroft 		if (listen(tcpsock, 5) < 0) {
    419  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    420  1.11  mycroft 			exit(1);
    421  1.11  mycroft 		}
    422  1.32     fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp) ||
    423  1.32     fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_tcp, &nb_tcp)) {
    424  1.32     fvdl 			syslog(LOG_ERR, "can't register tcp with rpcbind");
    425  1.11  mycroft 			exit(1);
    426  1.11  mycroft 		}
    427  1.37  mycroft 		set[0].fd = tcpsock;
    428  1.37  mycroft 		set[0].events = POLLIN;
    429  1.11  mycroft 		connect_type_cnt++;
    430  1.37  mycroft 	} else
    431  1.38  mycroft 		set[0].fd = -1;
    432   1.1      cgd 
    433  1.32     fvdl 	if (tcpflag && ip6flag) {
    434  1.32     fvdl 		if ((tcp6sock = socket(ai_tcp6->ai_family, ai_tcp6->ai_socktype,
    435  1.32     fvdl 		    ai_tcp6->ai_protocol)) < 0) {
    436  1.32     fvdl 			syslog(LOG_ERR, "can't create tcp socket");
    437  1.32     fvdl 			exit(1);
    438  1.32     fvdl 		}
    439  1.32     fvdl 		if (setsockopt(tcp6sock,
    440  1.32     fvdl 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    441  1.32     fvdl 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    442  1.36   itojun 		if (setsockopt(tcp6sock, IPPROTO_IPV6, IPV6_V6ONLY,
    443  1.32     fvdl 		    &on, sizeof on) < 0) {
    444  1.32     fvdl 			syslog(LOG_ERR, "can't set v6-only binding for tcp6 "
    445  1.32     fvdl 					"socket: %m");
    446  1.32     fvdl 			exit(1);
    447  1.32     fvdl 		}
    448  1.32     fvdl 		if (bind(tcp6sock, ai_tcp6->ai_addr, ai_tcp6->ai_addrlen) < 0) {
    449  1.32     fvdl 			syslog(LOG_ERR, "can't bind tcp6 addr");
    450  1.32     fvdl 			exit(1);
    451  1.32     fvdl 		}
    452  1.32     fvdl 		if (listen(tcp6sock, 5) < 0) {
    453  1.32     fvdl 			syslog(LOG_ERR, "listen failed");
    454  1.32     fvdl 			exit(1);
    455  1.32     fvdl 		}
    456  1.32     fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6) ||
    457  1.32     fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_tcp6, &nb_tcp6)) {
    458  1.32     fvdl 			syslog(LOG_ERR, "can't register tcp6 with rpcbind");
    459  1.32     fvdl 			exit(1);
    460  1.32     fvdl 		}
    461  1.37  mycroft 		set[1].fd = tcp6sock;
    462  1.37  mycroft 		set[1].events = POLLIN;
    463  1.32     fvdl 		connect_type_cnt++;
    464  1.37  mycroft 	} else
    465  1.38  mycroft 		set[1].fd = -1;
    466  1.32     fvdl 
    467  1.38  mycroft 	set[2].fd = -1;
    468  1.38  mycroft 	set[3].fd = -1;
    469  1.11  mycroft 
    470  1.11  mycroft 	if (connect_type_cnt == 0)
    471  1.11  mycroft 		exit(0);
    472  1.11  mycroft 
    473  1.51     yamt 	pthread_setname_np(pthread_self(), "master", NULL);
    474  1.11  mycroft 
    475  1.11  mycroft 	/*
    476  1.11  mycroft 	 * Loop forever accepting connections and passing the sockets
    477  1.11  mycroft 	 * into the kernel for the mounts.
    478  1.11  mycroft 	 */
    479  1.11  mycroft 	for (;;) {
    480  1.41  mycroft 		if (poll(set, 4, INFTIM) < 1) {
    481  1.49     elad 			syslog(LOG_ERR, "poll failed: %m");
    482  1.41  mycroft 			exit(1);
    483  1.11  mycroft 		}
    484  1.38  mycroft 
    485  1.37  mycroft 		if (set[0].revents & POLLIN) {
    486  1.11  mycroft 			len = sizeof(inetpeer);
    487  1.11  mycroft 			if ((msgsock = accept(tcpsock,
    488  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    489  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    490   1.1      cgd 				exit(1);
    491   1.1      cgd 			}
    492  1.11  mycroft 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
    493  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    494  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    495  1.11  mycroft 				syslog(LOG_ERR,
    496  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    497  1.11  mycroft 			nfsdargs.sock = msgsock;
    498  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    499  1.11  mycroft 			nfsdargs.namelen = sizeof(inetpeer);
    500  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    501  1.11  mycroft 			(void)close(msgsock);
    502  1.11  mycroft 		}
    503  1.32     fvdl 
    504  1.37  mycroft 		if (set[1].revents & POLLIN) {
    505  1.32     fvdl 			len = sizeof(inet6peer);
    506  1.32     fvdl 			if ((msgsock = accept(tcp6sock,
    507  1.32     fvdl 			    (struct sockaddr *)&inet6peer, &len)) < 0) {
    508  1.32     fvdl 				syslog(LOG_ERR, "accept failed: %m");
    509  1.32     fvdl 				exit(1);
    510  1.32     fvdl 			}
    511  1.32     fvdl 			if (setsockopt(msgsock, SOL_SOCKET,
    512  1.32     fvdl 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    513  1.32     fvdl 				syslog(LOG_ERR,
    514  1.32     fvdl 				    "setsockopt SO_KEEPALIVE: %m");
    515  1.32     fvdl 			nfsdargs.sock = msgsock;
    516  1.32     fvdl 			nfsdargs.name = (caddr_t)&inet6peer;
    517  1.32     fvdl 			nfsdargs.namelen = sizeof(inet6peer);
    518  1.32     fvdl 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    519  1.32     fvdl 			(void)close(msgsock);
    520  1.32     fvdl 		}
    521  1.32     fvdl 
    522  1.11  mycroft #ifdef notyet
    523  1.37  mycroft 		if (set[2].revents & POLLIN) {
    524  1.11  mycroft 			len = sizeof(isopeer);
    525  1.11  mycroft 			if ((msgsock = accept(tp4sock,
    526  1.11  mycroft 			    (struct sockaddr *)&isopeer, &len)) < 0) {
    527  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    528  1.11  mycroft 				exit(1);
    529   1.1      cgd 			}
    530  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    531  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    532  1.11  mycroft 				syslog(LOG_ERR,
    533  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    534  1.11  mycroft 			nfsdargs.sock = msgsock;
    535  1.11  mycroft 			nfsdargs.name = (caddr_t)&isopeer;
    536  1.11  mycroft 			nfsdargs.namelen = len;
    537  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    538  1.11  mycroft 			(void)close(msgsock);
    539  1.11  mycroft 		}
    540  1.38  mycroft 
    541  1.37  mycroft 		if (set[3].revents & POLLIN) {
    542  1.11  mycroft 			len = sizeof(inetpeer);
    543  1.11  mycroft 			if ((msgsock = accept(tpipsock,
    544  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    545  1.41  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    546   1.1      cgd 				exit(1);
    547   1.1      cgd 			}
    548  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    549  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    550  1.11  mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    551  1.11  mycroft 			nfsdargs.sock = msgsock;
    552  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    553  1.11  mycroft 			nfsdargs.namelen = len;
    554  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    555  1.11  mycroft 			(void)close(msgsock);
    556   1.1      cgd 		}
    557  1.11  mycroft #endif /* notyet */
    558   1.1      cgd 	}
    559   1.1      cgd }
    560   1.1      cgd 
    561  1.11  mycroft void
    562   1.1      cgd usage()
    563   1.1      cgd {
    564  1.15  mycroft 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
    565   1.1      cgd 	exit(1);
    566   1.1      cgd }
    567   1.1      cgd 
    568   1.1      cgd void
    569  1.11  mycroft nonfs(signo)
    570  1.11  mycroft 	int signo;
    571   1.1      cgd {
    572  1.11  mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    573   1.1      cgd }
    574