Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.50
      1  1.50      yamt /*	$NetBSD: nfsd.c,v 1.50 2007/07/10 13:52:51 yamt 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.23     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     38  1.23     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.50      yamt __RCSID("$NetBSD: nfsd.c,v 1.50 2007/07/10 13:52:51 yamt 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.11   mycroft #ifdef ISO
     65  1.11   mycroft #include <netiso/iso.h>
     66  1.11   mycroft #endif
     67   1.1       cgd #include <nfs/rpcv2.h>
     68  1.18      fvdl #include <nfs/nfsproto.h>
     69  1.11   mycroft #include <nfs/nfs.h>
     70  1.11   mycroft 
     71  1.11   mycroft #include <err.h>
     72  1.11   mycroft #include <errno.h>
     73  1.11   mycroft #include <fcntl.h>
     74  1.11   mycroft #include <grp.h>
     75  1.11   mycroft #include <pwd.h>
     76  1.50      yamt #include <pthread.h>
     77  1.11   mycroft #include <signal.h>
     78  1.11   mycroft #include <stdio.h>
     79  1.11   mycroft #include <stdlib.h>
     80  1.25     perry #include <string.h>
     81  1.19   mycroft #include <syslog.h>
     82  1.11   mycroft #include <unistd.h>
     83  1.32      fvdl #include <netdb.h>
     84   1.1       cgd 
     85   1.1       cgd /* Global defs */
     86   1.1       cgd #ifdef DEBUG
     87   1.1       cgd #define	syslog(e, s)	fprintf(stderr,(s))
     88   1.1       cgd int	debug = 1;
     89   1.1       cgd #else
     90   1.1       cgd int	debug = 0;
     91   1.1       cgd #endif
     92  1.11   mycroft 
     93  1.23     lukem int	main __P((int, char **));
     94  1.11   mycroft void	nonfs __P((int));
     95  1.11   mycroft void	reapchild __P((int));
     96  1.11   mycroft void	usage __P((void));
     97   1.1       cgd 
     98  1.50      yamt static void *
     99  1.50      yamt child(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.50      yamt 	nfssvc_flag = NFSSVC_NFSD;
    105  1.50      yamt 	memset(&nsd, 0, sizeof(nsd));
    106  1.50      yamt 	while (nfssvc(nfssvc_flag, &nsd) < 0) {
    107  1.50      yamt 		if (errno != ENEEDAUTH) {
    108  1.50      yamt 			syslog(LOG_ERR, "nfssvc: %m");
    109  1.50      yamt 			exit(1);
    110  1.50      yamt 		}
    111  1.50      yamt 		nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
    112  1.50      yamt 	}
    113  1.50      yamt 
    114  1.50      yamt 	return NULL;
    115  1.50      yamt }
    116  1.50      yamt 
    117   1.1       cgd /*
    118   1.1       cgd  * Nfs server daemon mostly just a user context for nfssvc()
    119  1.11   mycroft  *
    120   1.1       cgd  * 1 - do file descriptor and signal cleanup
    121  1.50      yamt  * 2 - create the nfsd thread(s)
    122  1.11   mycroft  * 3 - create server socket(s)
    123  1.11   mycroft  * 4 - register socket with portmap
    124  1.11   mycroft  *
    125  1.28     lukem  * For connectionless protocols, just pass the socket into the kernel via
    126  1.11   mycroft  * nfssvc().
    127  1.11   mycroft  * For connection based sockets, loop doing accepts. When you get a new
    128  1.28     lukem  * socket from accept, pass the msgsock into the kernel via nfssvc().
    129   1.1       cgd  * The arguments are:
    130  1.11   mycroft  *	-c - support iso cltp clients
    131  1.11   mycroft  *	-r - reregister with portmapper
    132  1.11   mycroft  *	-t - support tcp nfs clients
    133  1.11   mycroft  *	-u - support udp nfs clients
    134  1.50      yamt  * followed by "n" which is the number of nfsd threads to create
    135   1.1       cgd  */
    136  1.11   mycroft int
    137  1.23     lukem main(argc, argv)
    138   1.1       cgd 	int argc;
    139  1.23     lukem 	char *argv[];
    140   1.1       cgd {
    141  1.11   mycroft 	struct nfsd_args nfsdargs;
    142  1.32      fvdl 	struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
    143  1.32      fvdl 	struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
    144  1.32      fvdl 	struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
    145  1.32      fvdl 	struct sockaddr_in inetpeer;
    146  1.32      fvdl 	struct sockaddr_in6 inet6peer;
    147  1.11   mycroft #ifdef ISO
    148  1.11   mycroft 	struct sockaddr_iso isoaddr, isopeer;
    149  1.11   mycroft #endif
    150  1.37   mycroft 	struct pollfd set[4];
    151  1.47       mrg 	socklen_t len;
    152  1.47       mrg 	int ch, cltpflag, connect_type_cnt, i, maxsock, msgsock;
    153  1.50      yamt 	int nfsdcnt, on = 1, reregister, sock, tcpflag, tcpsock;
    154  1.32      fvdl 	int tcp6sock, ip6flag;
    155  1.32      fvdl 	int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag, ecode, s;
    156  1.11   mycroft 
    157  1.44  jonathan #define	MAXNFSDCNT	1024
    158  1.11   mycroft #define	DEFNFSDCNT	 4
    159  1.11   mycroft 	nfsdcnt = DEFNFSDCNT;
    160  1.11   mycroft 	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
    161  1.32      fvdl 	tpipflag = udpflag = ip6flag = 0;
    162  1.45     lukem 	nconf_udp = nconf_tcp = nconf_udp6 = nconf_tcp6 = NULL;
    163  1.45     lukem 	maxsock = 0;
    164  1.45     lukem 	tcpsock = tcp6sock = -1;
    165  1.11   mycroft #ifdef ISO
    166  1.32      fvdl #define	GETOPT	"6cn:rtu"
    167  1.11   mycroft #define	USAGE	"[-crtu] [-n num_servers]"
    168  1.11   mycroft #else
    169  1.32      fvdl #define	GETOPT	"6n:rtu"
    170  1.11   mycroft #define	USAGE	"[-rtu] [-n num_servers]"
    171  1.11   mycroft #endif
    172  1.30   thorpej 	while ((ch = getopt(argc, argv, GETOPT)) != -1) {
    173  1.11   mycroft 		switch (ch) {
    174  1.32      fvdl 		case '6':
    175  1.32      fvdl 			ip6flag = 1;
    176  1.32      fvdl 			s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    177  1.32      fvdl 			if (s < 0 && (errno == EPROTONOSUPPORT ||
    178  1.32      fvdl 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
    179  1.32      fvdl 				ip6flag = 0;
    180  1.32      fvdl 			else
    181  1.32      fvdl 				close(s);
    182  1.32      fvdl 			break;
    183  1.11   mycroft 		case 'n':
    184  1.11   mycroft 			nfsdcnt = atoi(optarg);
    185  1.11   mycroft 			if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    186  1.21      fvdl 				warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    187  1.11   mycroft 				nfsdcnt = DEFNFSDCNT;
    188  1.11   mycroft 			}
    189  1.11   mycroft 			break;
    190   1.1       cgd 		case 'r':
    191  1.11   mycroft 			reregister = 1;
    192   1.1       cgd 			break;
    193   1.1       cgd 		case 't':
    194  1.11   mycroft 			tcpflag = 1;
    195   1.1       cgd 			break;
    196   1.1       cgd 		case 'u':
    197  1.11   mycroft 			udpflag = 1;
    198  1.11   mycroft 			break;
    199  1.11   mycroft #ifdef ISO
    200  1.11   mycroft 		case 'c':
    201  1.11   mycroft 			cltpflag = 1;
    202   1.1       cgd 			break;
    203  1.11   mycroft #ifdef notyet
    204  1.11   mycroft 		case 'i':
    205  1.11   mycroft 			tp4cnt = 1;
    206  1.11   mycroft 			break;
    207  1.11   mycroft 		case 'p':
    208  1.11   mycroft 			tpipcnt = 1;
    209  1.11   mycroft 			break;
    210  1.11   mycroft #endif /* notyet */
    211  1.11   mycroft #endif /* ISO */
    212   1.1       cgd 		default:
    213   1.1       cgd 		case '?':
    214   1.1       cgd 			usage();
    215  1.11   mycroft 		};
    216  1.30   thorpej 	}
    217  1.11   mycroft 	argv += optind;
    218  1.11   mycroft 	argc -= optind;
    219   1.1       cgd 
    220   1.1       cgd 	/*
    221  1.11   mycroft 	 * XXX
    222  1.11   mycroft 	 * Backward compatibility, trailing number is the count of daemons.
    223   1.1       cgd 	 */
    224  1.11   mycroft 	if (argc > 1)
    225  1.11   mycroft 		usage();
    226  1.11   mycroft 	if (argc == 1) {
    227  1.11   mycroft 		nfsdcnt = atoi(argv[0]);
    228  1.11   mycroft 		if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    229  1.21      fvdl 			warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    230  1.11   mycroft 			nfsdcnt = DEFNFSDCNT;
    231  1.11   mycroft 		}
    232   1.1       cgd 	}
    233  1.31     soren 
    234  1.31     soren 	/*
    235  1.31     soren 	 * If none of TCP or UDP are specified, default to UDP only.
    236  1.31     soren 	 */
    237  1.31     soren 	if (tcpflag == 0 && udpflag == 0)
    238  1.31     soren 		udpflag = 1;
    239   1.1       cgd 
    240   1.1       cgd 	if (debug == 0) {
    241   1.1       cgd 		daemon(0, 0);
    242  1.11   mycroft 		(void)signal(SIGHUP, SIG_IGN);
    243  1.11   mycroft 		(void)signal(SIGINT, SIG_IGN);
    244  1.11   mycroft 		(void)signal(SIGQUIT, SIG_IGN);
    245  1.11   mycroft 		(void)signal(SIGSYS, nonfs);
    246   1.1       cgd 	}
    247  1.11   mycroft 	(void)signal(SIGCHLD, reapchild);
    248  1.11   mycroft 
    249  1.32      fvdl 	if (udpflag) {
    250  1.32      fvdl 		memset(&hints, 0, sizeof hints);
    251  1.32      fvdl 		hints.ai_flags = AI_PASSIVE;
    252  1.32      fvdl 		hints.ai_family = PF_INET;
    253  1.32      fvdl 		hints.ai_socktype = SOCK_DGRAM;
    254  1.32      fvdl 		hints.ai_protocol = IPPROTO_UDP;
    255  1.32      fvdl 
    256  1.32      fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
    257  1.32      fvdl 		if (ecode != 0) {
    258  1.32      fvdl 			syslog(LOG_ERR, "getaddrinfo udp: %s",
    259  1.32      fvdl 			    gai_strerror(ecode));
    260  1.32      fvdl 			exit(1);
    261  1.32      fvdl 		}
    262  1.32      fvdl 
    263  1.32      fvdl 		nconf_udp = getnetconfigent("udp");
    264  1.32      fvdl 
    265  1.32      fvdl 		if (nconf_udp == NULL)
    266  1.32      fvdl 			err(1, "getnetconfigent udp failed");
    267  1.32      fvdl 
    268  1.32      fvdl 		nb_udp.buf = ai_udp->ai_addr;
    269  1.32      fvdl 		nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
    270  1.32      fvdl 		if (reregister)
    271  1.32      fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp))
    272  1.32      fvdl 				err(1, "rpcb_set udp failed");
    273  1.32      fvdl 	}
    274  1.32      fvdl 
    275  1.32      fvdl 	if (tcpflag) {
    276  1.32      fvdl 		memset(&hints, 0, sizeof hints);
    277  1.32      fvdl 		hints.ai_flags = AI_PASSIVE;
    278  1.32      fvdl 		hints.ai_family = PF_INET;
    279  1.32      fvdl 		hints.ai_socktype = SOCK_STREAM;
    280  1.32      fvdl 		hints.ai_protocol = IPPROTO_TCP;
    281  1.32      fvdl 
    282  1.32      fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
    283  1.32      fvdl 		if (ecode != 0) {
    284  1.48   hubertf 			syslog(LOG_ERR, "getaddrinfo tcp: %s",
    285  1.32      fvdl 			    gai_strerror(ecode));
    286  1.32      fvdl 			exit(1);
    287  1.32      fvdl 		}
    288  1.32      fvdl 
    289  1.32      fvdl 		nconf_tcp = getnetconfigent("tcp");
    290  1.32      fvdl 
    291  1.32      fvdl 		if (nconf_tcp == NULL)
    292  1.32      fvdl 			err(1, "getnetconfigent tcp failed");
    293  1.32      fvdl 
    294  1.32      fvdl 		nb_tcp.buf = ai_tcp->ai_addr;
    295  1.32      fvdl 		nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
    296  1.32      fvdl 		if (reregister)
    297  1.32      fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp))
    298  1.32      fvdl 				err(1, "rpcb_set tcp failed");
    299  1.32      fvdl 	}
    300  1.32      fvdl 
    301  1.32      fvdl 	if (udpflag && ip6flag) {
    302  1.32      fvdl 		memset(&hints, 0, sizeof hints);
    303  1.32      fvdl 		hints.ai_flags = AI_PASSIVE;
    304  1.32      fvdl 		hints.ai_family = PF_INET6;
    305  1.32      fvdl 		hints.ai_socktype = SOCK_DGRAM;
    306  1.32      fvdl 		hints.ai_protocol = IPPROTO_UDP;
    307  1.32      fvdl 
    308  1.32      fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
    309  1.32      fvdl 		if (ecode != 0) {
    310  1.32      fvdl 			syslog(LOG_ERR, "getaddrinfo udp: %s",
    311  1.32      fvdl 			    gai_strerror(ecode));
    312  1.32      fvdl 			exit(1);
    313  1.32      fvdl 		}
    314  1.32      fvdl 
    315  1.32      fvdl 		nconf_udp6 = getnetconfigent("udp6");
    316  1.32      fvdl 
    317  1.32      fvdl 		if (nconf_udp6 == NULL)
    318  1.32      fvdl 			err(1, "getnetconfigent udp6 failed");
    319  1.32      fvdl 
    320  1.32      fvdl 		nb_udp6.buf = ai_udp6->ai_addr;
    321  1.32      fvdl 		nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
    322  1.32      fvdl 		if (reregister)
    323  1.32      fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6))
    324  1.32      fvdl 				err(1, "rpcb_set udp6 failed");
    325  1.32      fvdl 	}
    326  1.32      fvdl 
    327  1.32      fvdl 	if (tcpflag && ip6flag) {
    328  1.32      fvdl 		memset(&hints, 0, sizeof hints);
    329  1.32      fvdl 		hints.ai_flags = AI_PASSIVE;
    330  1.32      fvdl 		hints.ai_family = PF_INET6;
    331  1.32      fvdl 		hints.ai_socktype = SOCK_STREAM;
    332  1.32      fvdl 		hints.ai_protocol = IPPROTO_TCP;
    333  1.32      fvdl 
    334  1.32      fvdl 		ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
    335  1.32      fvdl 		if (ecode != 0) {
    336  1.48   hubertf 			syslog(LOG_ERR, "getaddrinfo tcp: %s",
    337  1.32      fvdl 			    gai_strerror(ecode));
    338  1.32      fvdl 			exit(1);
    339  1.32      fvdl 		}
    340  1.32      fvdl 
    341  1.32      fvdl 		nconf_tcp6 = getnetconfigent("tcp6");
    342  1.32      fvdl 
    343  1.32      fvdl 		if (nconf_tcp6 == NULL)
    344  1.32      fvdl 			err(1, "getnetconfigent tcp6 failed");
    345  1.32      fvdl 
    346  1.32      fvdl 		nb_tcp6.buf = ai_tcp6->ai_addr;
    347  1.32      fvdl 		nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
    348  1.32      fvdl 		if (reregister)
    349  1.32      fvdl 			if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6))
    350  1.32      fvdl 				err(1, "rpcb_set tcp6 failed");
    351   1.8       cgd 	}
    352  1.32      fvdl 
    353  1.33     lukem 	openlog("nfsd", LOG_PID, LOG_DAEMON);
    354  1.11   mycroft 
    355  1.11   mycroft 	for (i = 0; i < nfsdcnt; i++) {
    356  1.50      yamt 		pthread_t t;
    357  1.50      yamt 		int error;
    358  1.50      yamt 
    359  1.50      yamt 		error = pthread_create(&t, NULL, child, NULL);
    360  1.50      yamt 		if (error) {
    361  1.50      yamt 			errno = error;
    362  1.50      yamt 			syslog(LOG_ERR, "pthread_create: %m");
    363  1.11   mycroft 			exit (1);
    364   1.1       cgd 		}
    365   1.1       cgd 	}
    366  1.11   mycroft 
    367  1.11   mycroft 	/* If we are serving udp, set up the socket. */
    368   1.1       cgd 	if (udpflag) {
    369  1.32      fvdl 		if ((sock = socket(ai_udp->ai_family, ai_udp->ai_socktype,
    370  1.32      fvdl 		    ai_udp->ai_protocol)) < 0) {
    371  1.11   mycroft 			syslog(LOG_ERR, "can't create udp socket");
    372   1.1       cgd 			exit(1);
    373   1.1       cgd 		}
    374  1.32      fvdl 		if (bind(sock, ai_udp->ai_addr, ai_udp->ai_addrlen) < 0) {
    375  1.32      fvdl 			syslog(LOG_ERR, "can't bind udp addr");
    376  1.32      fvdl 			exit(1);
    377  1.32      fvdl 		}
    378  1.32      fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp) ||
    379  1.32      fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_udp, &nb_udp)) {
    380  1.32      fvdl 			syslog(LOG_ERR, "can't register with udp portmap");
    381  1.32      fvdl 			exit(1);
    382  1.32      fvdl 		}
    383  1.32      fvdl 		nfsdargs.sock = sock;
    384  1.32      fvdl 		nfsdargs.name = NULL;
    385  1.32      fvdl 		nfsdargs.namelen = 0;
    386  1.32      fvdl 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    387  1.32      fvdl 			syslog(LOG_ERR, "can't add UDP socket");
    388  1.32      fvdl 			exit(1);
    389  1.32      fvdl 		}
    390  1.32      fvdl 		(void)close(sock);
    391  1.32      fvdl 	}
    392  1.32      fvdl 
    393  1.32      fvdl 	if (udpflag &&ip6flag) {
    394  1.32      fvdl 		if ((sock = socket(ai_udp6->ai_family, ai_udp6->ai_socktype,
    395  1.32      fvdl 		    ai_udp6->ai_protocol)) < 0) {
    396  1.32      fvdl 			syslog(LOG_ERR, "can't create udp socket");
    397  1.32      fvdl 			exit(1);
    398  1.32      fvdl 		}
    399  1.36    itojun 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
    400  1.32      fvdl 		    &on, sizeof on) < 0) {
    401  1.32      fvdl 			syslog(LOG_ERR, "can't set v6-only binding for udp6 "
    402  1.32      fvdl 					"socket: %m");
    403  1.32      fvdl 			exit(1);
    404  1.32      fvdl 		}
    405  1.32      fvdl 		if (bind(sock, ai_udp6->ai_addr, ai_udp6->ai_addrlen) < 0) {
    406  1.11   mycroft 			syslog(LOG_ERR, "can't bind udp addr");
    407   1.1       cgd 			exit(1);
    408   1.1       cgd 		}
    409  1.32      fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6) ||
    410  1.32      fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_udp6, &nb_udp6)) {
    411  1.11   mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    412  1.11   mycroft 			exit(1);
    413  1.11   mycroft 		}
    414  1.11   mycroft 		nfsdargs.sock = sock;
    415  1.11   mycroft 		nfsdargs.name = NULL;
    416  1.11   mycroft 		nfsdargs.namelen = 0;
    417  1.11   mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    418  1.32      fvdl 			syslog(LOG_ERR, "can't add UDP6 socket");
    419  1.11   mycroft 			exit(1);
    420  1.11   mycroft 		}
    421  1.11   mycroft 		(void)close(sock);
    422  1.11   mycroft 	}
    423  1.11   mycroft 
    424  1.11   mycroft #ifdef ISO
    425  1.11   mycroft 	/* If we are serving cltp, set up the socket. */
    426  1.11   mycroft 	if (cltpflag) {
    427  1.11   mycroft 		if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
    428  1.11   mycroft 			syslog(LOG_ERR, "can't create cltp socket");
    429  1.11   mycroft 			exit(1);
    430  1.11   mycroft 		}
    431  1.11   mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    432  1.11   mycroft 		isoaddr.siso_family = AF_ISO;
    433  1.11   mycroft 		isoaddr.siso_tlen = 2;
    434  1.11   mycroft 		cp = TSEL(&isoaddr);
    435  1.11   mycroft 		*cp++ = (NFS_PORT >> 8);
    436  1.11   mycroft 		*cp = (NFS_PORT & 0xff);
    437  1.11   mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    438  1.11   mycroft 		if (bind(sock,
    439  1.11   mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    440  1.11   mycroft 			syslog(LOG_ERR, "can't bind cltp addr");
    441   1.1       cgd 			exit(1);
    442   1.1       cgd 		}
    443  1.11   mycroft #ifdef notyet
    444   1.1       cgd 		/*
    445  1.11   mycroft 		 * XXX
    446  1.11   mycroft 		 * Someday this should probably use "rpcbind", the son of
    447  1.11   mycroft 		 * portmap.
    448   1.1       cgd 		 */
    449  1.11   mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
    450  1.11   mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    451  1.11   mycroft 			exit(1);
    452  1.11   mycroft 		}
    453  1.11   mycroft #endif /* notyet */
    454  1.11   mycroft 		nfsdargs.sock = sock;
    455  1.11   mycroft 		nfsdargs.name = NULL;
    456  1.11   mycroft 		nfsdargs.namelen = 0;
    457  1.11   mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    458  1.11   mycroft 			syslog(LOG_ERR, "can't add UDP socket");
    459  1.11   mycroft 			exit(1);
    460  1.11   mycroft 		}
    461   1.1       cgd 		close(sock);
    462   1.1       cgd 	}
    463  1.11   mycroft #endif /* ISO */
    464   1.1       cgd 
    465  1.11   mycroft 	/* Now set up the master server socket waiting for tcp connections. */
    466  1.11   mycroft 	on = 1;
    467  1.11   mycroft 	connect_type_cnt = 0;
    468   1.1       cgd 	if (tcpflag) {
    469  1.32      fvdl 		if ((tcpsock = socket(ai_tcp->ai_family, ai_tcp->ai_socktype,
    470  1.32      fvdl 		    ai_tcp->ai_protocol)) < 0) {
    471  1.11   mycroft 			syslog(LOG_ERR, "can't create tcp socket");
    472  1.11   mycroft 			exit(1);
    473  1.11   mycroft 		}
    474  1.11   mycroft 		if (setsockopt(tcpsock,
    475  1.11   mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    476  1.11   mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    477  1.32      fvdl 		if (bind(tcpsock, ai_tcp->ai_addr, ai_tcp->ai_addrlen) < 0) {
    478  1.11   mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    479  1.11   mycroft 			exit(1);
    480  1.11   mycroft 		}
    481  1.11   mycroft 		if (listen(tcpsock, 5) < 0) {
    482  1.11   mycroft 			syslog(LOG_ERR, "listen failed");
    483  1.11   mycroft 			exit(1);
    484  1.11   mycroft 		}
    485  1.32      fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp) ||
    486  1.32      fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_tcp, &nb_tcp)) {
    487  1.32      fvdl 			syslog(LOG_ERR, "can't register tcp with rpcbind");
    488  1.11   mycroft 			exit(1);
    489  1.11   mycroft 		}
    490  1.37   mycroft 		set[0].fd = tcpsock;
    491  1.37   mycroft 		set[0].events = POLLIN;
    492  1.11   mycroft 		connect_type_cnt++;
    493  1.37   mycroft 	} else
    494  1.38   mycroft 		set[0].fd = -1;
    495   1.1       cgd 
    496  1.32      fvdl 	if (tcpflag && ip6flag) {
    497  1.32      fvdl 		if ((tcp6sock = socket(ai_tcp6->ai_family, ai_tcp6->ai_socktype,
    498  1.32      fvdl 		    ai_tcp6->ai_protocol)) < 0) {
    499  1.32      fvdl 			syslog(LOG_ERR, "can't create tcp socket");
    500  1.32      fvdl 			exit(1);
    501  1.32      fvdl 		}
    502  1.32      fvdl 		if (setsockopt(tcp6sock,
    503  1.32      fvdl 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    504  1.32      fvdl 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    505  1.36    itojun 		if (setsockopt(tcp6sock, IPPROTO_IPV6, IPV6_V6ONLY,
    506  1.32      fvdl 		    &on, sizeof on) < 0) {
    507  1.32      fvdl 			syslog(LOG_ERR, "can't set v6-only binding for tcp6 "
    508  1.32      fvdl 					"socket: %m");
    509  1.32      fvdl 			exit(1);
    510  1.32      fvdl 		}
    511  1.32      fvdl 		if (bind(tcp6sock, ai_tcp6->ai_addr, ai_tcp6->ai_addrlen) < 0) {
    512  1.32      fvdl 			syslog(LOG_ERR, "can't bind tcp6 addr");
    513  1.32      fvdl 			exit(1);
    514  1.32      fvdl 		}
    515  1.32      fvdl 		if (listen(tcp6sock, 5) < 0) {
    516  1.32      fvdl 			syslog(LOG_ERR, "listen failed");
    517  1.32      fvdl 			exit(1);
    518  1.32      fvdl 		}
    519  1.32      fvdl 		if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6) ||
    520  1.32      fvdl 		    !rpcb_set(RPCPROG_NFS, 3, nconf_tcp6, &nb_tcp6)) {
    521  1.32      fvdl 			syslog(LOG_ERR, "can't register tcp6 with rpcbind");
    522  1.32      fvdl 			exit(1);
    523  1.32      fvdl 		}
    524  1.37   mycroft 		set[1].fd = tcp6sock;
    525  1.37   mycroft 		set[1].events = POLLIN;
    526  1.32      fvdl 		connect_type_cnt++;
    527  1.37   mycroft 	} else
    528  1.38   mycroft 		set[1].fd = -1;
    529  1.32      fvdl 
    530  1.11   mycroft #ifdef notyet
    531  1.11   mycroft 	/* Now set up the master server socket waiting for tp4 connections. */
    532  1.11   mycroft 	if (tp4flag) {
    533  1.11   mycroft 		if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
    534  1.11   mycroft 			syslog(LOG_ERR, "can't create tp4 socket");
    535   1.1       cgd 			exit(1);
    536   1.1       cgd 		}
    537  1.11   mycroft 		if (setsockopt(tp4sock,
    538  1.11   mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    539   1.1       cgd 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    540  1.11   mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    541  1.11   mycroft 		isoaddr.siso_family = AF_ISO;
    542  1.11   mycroft 		isoaddr.siso_tlen = 2;
    543  1.11   mycroft 		cp = TSEL(&isoaddr);
    544  1.11   mycroft 		*cp++ = (NFS_PORT >> 8);
    545  1.11   mycroft 		*cp = (NFS_PORT & 0xff);
    546  1.11   mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    547  1.11   mycroft 		if (bind(tp4sock,
    548  1.12   mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    549  1.11   mycroft 			syslog(LOG_ERR, "can't bind tp4 addr");
    550   1.1       cgd 			exit(1);
    551   1.1       cgd 		}
    552  1.11   mycroft 		if (listen(tp4sock, 5) < 0) {
    553  1.11   mycroft 			syslog(LOG_ERR, "listen failed");
    554   1.1       cgd 			exit(1);
    555   1.1       cgd 		}
    556  1.11   mycroft 		/*
    557  1.11   mycroft 		 * XXX
    558  1.11   mycroft 		 * Someday this should probably use "rpcbind", the son of
    559  1.11   mycroft 		 * portmap.
    560  1.11   mycroft 		 */
    561   1.1       cgd 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    562  1.11   mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    563  1.11   mycroft 			exit(1);
    564  1.11   mycroft 		}
    565  1.37   mycroft 		set[2].fd = tp4sock;
    566  1.37   mycroft 		set[2].events = POLLIN;
    567  1.11   mycroft 		connect_type_cnt++;
    568  1.37   mycroft 	} else
    569  1.38   mycroft 		set[2].fd = -1;
    570  1.11   mycroft 
    571  1.11   mycroft 	/* Now set up the master server socket waiting for tpip connections. */
    572  1.11   mycroft 	if (tpipflag) {
    573  1.11   mycroft 		if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
    574  1.11   mycroft 			syslog(LOG_ERR, "can't create tpip socket");
    575  1.11   mycroft 			exit(1);
    576  1.11   mycroft 		}
    577  1.11   mycroft 		if (setsockopt(tpipsock,
    578  1.11   mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    579  1.11   mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    580  1.11   mycroft 		inetaddr.sin_family = AF_INET;
    581  1.11   mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    582  1.11   mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    583  1.11   mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    584  1.20   mycroft 		memset(inetaddr.sin_zero, 0, sizeof(inetaddr.sin_zero));
    585  1.11   mycroft 		if (bind(tpipsock,
    586  1.11   mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    587  1.11   mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    588  1.11   mycroft 			exit(1);
    589  1.11   mycroft 		}
    590  1.11   mycroft 		if (listen(tpipsock, 5) < 0) {
    591  1.11   mycroft 			syslog(LOG_ERR, "listen failed");
    592   1.1       cgd 			exit(1);
    593   1.1       cgd 		}
    594   1.1       cgd 		/*
    595  1.11   mycroft 		 * XXX
    596  1.11   mycroft 		 * Someday this should probably use "rpcbind", the son of
    597  1.11   mycroft 		 * portmap.
    598   1.1       cgd 		 */
    599  1.11   mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    600  1.11   mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    601  1.11   mycroft 			exit(1);
    602  1.11   mycroft 		}
    603  1.37   mycroft 		set[3].fd = tpipsock;
    604  1.37   mycroft 		set[3].events = POLLIN;
    605  1.11   mycroft 		connect_type_cnt++;
    606  1.37   mycroft 	} else
    607  1.38   mycroft 		set[3].fd = -1;
    608  1.37   mycroft #else
    609  1.38   mycroft 	set[2].fd = -1;
    610  1.38   mycroft 	set[3].fd = -1;
    611  1.11   mycroft #endif /* notyet */
    612  1.11   mycroft 
    613  1.11   mycroft 	if (connect_type_cnt == 0)
    614  1.11   mycroft 		exit(0);
    615  1.11   mycroft 
    616  1.13   mycroft 	setproctitle("master");
    617  1.11   mycroft 
    618  1.11   mycroft 	/*
    619  1.11   mycroft 	 * Loop forever accepting connections and passing the sockets
    620  1.11   mycroft 	 * into the kernel for the mounts.
    621  1.11   mycroft 	 */
    622  1.11   mycroft 	for (;;) {
    623  1.41   mycroft 		if (poll(set, 4, INFTIM) < 1) {
    624  1.49      elad 			syslog(LOG_ERR, "poll failed: %m");
    625  1.41   mycroft 			exit(1);
    626  1.11   mycroft 		}
    627  1.38   mycroft 
    628  1.37   mycroft 		if (set[0].revents & POLLIN) {
    629  1.11   mycroft 			len = sizeof(inetpeer);
    630  1.11   mycroft 			if ((msgsock = accept(tcpsock,
    631  1.11   mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    632  1.11   mycroft 				syslog(LOG_ERR, "accept failed: %m");
    633   1.1       cgd 				exit(1);
    634   1.1       cgd 			}
    635  1.11   mycroft 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
    636  1.11   mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    637  1.11   mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    638  1.11   mycroft 				syslog(LOG_ERR,
    639  1.11   mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    640  1.11   mycroft 			nfsdargs.sock = msgsock;
    641  1.11   mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    642  1.11   mycroft 			nfsdargs.namelen = sizeof(inetpeer);
    643  1.11   mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    644  1.11   mycroft 			(void)close(msgsock);
    645  1.11   mycroft 		}
    646  1.32      fvdl 
    647  1.37   mycroft 		if (set[1].revents & POLLIN) {
    648  1.32      fvdl 			len = sizeof(inet6peer);
    649  1.32      fvdl 			if ((msgsock = accept(tcp6sock,
    650  1.32      fvdl 			    (struct sockaddr *)&inet6peer, &len)) < 0) {
    651  1.32      fvdl 				syslog(LOG_ERR, "accept failed: %m");
    652  1.32      fvdl 				exit(1);
    653  1.32      fvdl 			}
    654  1.32      fvdl 			if (setsockopt(msgsock, SOL_SOCKET,
    655  1.32      fvdl 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    656  1.32      fvdl 				syslog(LOG_ERR,
    657  1.32      fvdl 				    "setsockopt SO_KEEPALIVE: %m");
    658  1.32      fvdl 			nfsdargs.sock = msgsock;
    659  1.32      fvdl 			nfsdargs.name = (caddr_t)&inet6peer;
    660  1.32      fvdl 			nfsdargs.namelen = sizeof(inet6peer);
    661  1.32      fvdl 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    662  1.32      fvdl 			(void)close(msgsock);
    663  1.32      fvdl 		}
    664  1.32      fvdl 
    665  1.11   mycroft #ifdef notyet
    666  1.37   mycroft 		if (set[2].revents & POLLIN) {
    667  1.11   mycroft 			len = sizeof(isopeer);
    668  1.11   mycroft 			if ((msgsock = accept(tp4sock,
    669  1.11   mycroft 			    (struct sockaddr *)&isopeer, &len)) < 0) {
    670  1.11   mycroft 				syslog(LOG_ERR, "accept failed: %m");
    671  1.11   mycroft 				exit(1);
    672   1.1       cgd 			}
    673  1.11   mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    674  1.11   mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    675  1.11   mycroft 				syslog(LOG_ERR,
    676  1.11   mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    677  1.11   mycroft 			nfsdargs.sock = msgsock;
    678  1.11   mycroft 			nfsdargs.name = (caddr_t)&isopeer;
    679  1.11   mycroft 			nfsdargs.namelen = len;
    680  1.11   mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    681  1.11   mycroft 			(void)close(msgsock);
    682  1.11   mycroft 		}
    683  1.38   mycroft 
    684  1.37   mycroft 		if (set[3].revents & POLLIN) {
    685  1.11   mycroft 			len = sizeof(inetpeer);
    686  1.11   mycroft 			if ((msgsock = accept(tpipsock,
    687  1.11   mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    688  1.41   mycroft 				syslog(LOG_ERR, "accept failed: %m");
    689   1.1       cgd 				exit(1);
    690   1.1       cgd 			}
    691  1.11   mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    692  1.11   mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    693  1.11   mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    694  1.11   mycroft 			nfsdargs.sock = msgsock;
    695  1.11   mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    696  1.11   mycroft 			nfsdargs.namelen = len;
    697  1.11   mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    698  1.11   mycroft 			(void)close(msgsock);
    699   1.1       cgd 		}
    700  1.11   mycroft #endif /* notyet */
    701   1.1       cgd 	}
    702   1.1       cgd }
    703   1.1       cgd 
    704  1.11   mycroft void
    705   1.1       cgd usage()
    706   1.1       cgd {
    707  1.15   mycroft 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
    708   1.1       cgd 	exit(1);
    709   1.1       cgd }
    710   1.1       cgd 
    711   1.1       cgd void
    712  1.11   mycroft nonfs(signo)
    713  1.11   mycroft 	int signo;
    714   1.1       cgd {
    715  1.11   mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    716   1.1       cgd }
    717   1.4     glass 
    718  1.11   mycroft void
    719  1.11   mycroft reapchild(signo)
    720  1.11   mycroft 	int signo;
    721   1.4     glass {
    722  1.18      fvdl 	while (wait3(NULL, WNOHANG, NULL) > 0);
    723   1.4     glass }
    724