Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.58.2.2
      1  1.58.2.2      yamt /*	$NetBSD: nfsd.c,v 1.58.2.2 2014/05/22 11:43:07 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.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.58.2.2      yamt __RCSID("$NetBSD: nfsd.c,v 1.58.2.2 2014/05/22 11:43:07 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.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.58.2.2      yamt #include <paths.h>
     73      1.11   mycroft #include <pwd.h>
     74      1.50      yamt #include <pthread.h>
     75      1.11   mycroft #include <signal.h>
     76      1.11   mycroft #include <stdio.h>
     77      1.11   mycroft #include <stdlib.h>
     78      1.25     perry #include <string.h>
     79      1.19   mycroft #include <syslog.h>
     80      1.11   mycroft #include <unistd.h>
     81      1.32      fvdl #include <netdb.h>
     82       1.1       cgd 
     83       1.1       cgd /* Global defs */
     84       1.1       cgd #ifdef DEBUG
     85      1.55     pooka #define	syslog(e, s, args...)						\
     86      1.55     pooka do {									\
     87      1.55     pooka     fprintf(stderr,(s), ## args);					\
     88      1.55     pooka     fprintf(stderr, "\n");						\
     89      1.55     pooka } while (/*CONSTCOND*/0)
     90      1.58     joerg static int	debug = 1;
     91       1.1       cgd #else
     92      1.58     joerg static int	debug = 0;
     93       1.1       cgd #endif
     94      1.11   mycroft 
     95      1.58     joerg static void	nonfs(int);
     96      1.58     joerg __dead static void	usage(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.58.2.1      yamt struct conf {
    119  1.58.2.1      yamt 	struct addrinfo *ai;
    120  1.58.2.1      yamt 	struct netconfig *nc;
    121  1.58.2.1      yamt 	struct netbuf nb;
    122  1.58.2.1      yamt 	struct pollfd pfd;
    123  1.58.2.1      yamt };
    124  1.58.2.1      yamt 
    125  1.58.2.1      yamt #define NFS_UDP4	0
    126  1.58.2.1      yamt #define NFS_TCP4	1
    127  1.58.2.1      yamt #define NFS_UDP6	2
    128  1.58.2.1      yamt #define NFS_TCP6	3
    129  1.58.2.1      yamt 
    130  1.58.2.1      yamt static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
    131  1.58.2.1      yamt static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
    132  1.58.2.1      yamt static int cfg_socktype[] = {
    133  1.58.2.1      yamt     SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
    134  1.58.2.1      yamt static int cfg_protocol[] = {
    135  1.58.2.1      yamt     IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
    136  1.58.2.1      yamt 
    137  1.58.2.1      yamt static int
    138  1.58.2.1      yamt tryconf(struct conf *cfg, int t, int reregister)
    139  1.58.2.1      yamt {
    140  1.58.2.1      yamt 	struct addrinfo hints;
    141  1.58.2.1      yamt 	int ecode;
    142  1.58.2.1      yamt 
    143  1.58.2.1      yamt 	memset(cfg, 0, sizeof(*cfg));
    144  1.58.2.1      yamt 	memset(&hints, 0, sizeof hints);
    145  1.58.2.1      yamt 	hints.ai_flags = AI_PASSIVE;
    146  1.58.2.1      yamt 	hints.ai_family = cfg_family[t];
    147  1.58.2.1      yamt 	hints.ai_socktype = cfg_socktype[t];
    148  1.58.2.1      yamt 	hints.ai_protocol = cfg_protocol[t];
    149  1.58.2.1      yamt 
    150  1.58.2.1      yamt 	ecode = getaddrinfo(NULL, "nfs", &hints, &cfg->ai);
    151  1.58.2.1      yamt 	if (ecode != 0) {
    152  1.58.2.1      yamt 		syslog(LOG_ERR, "getaddrinfo %s: %s", cfg_netconf[t],
    153  1.58.2.1      yamt 		    gai_strerror(ecode));
    154  1.58.2.1      yamt 		return -1;
    155  1.58.2.1      yamt 	}
    156  1.58.2.1      yamt 
    157  1.58.2.1      yamt 	cfg->nc = getnetconfigent(cfg_netconf[t]);
    158  1.58.2.1      yamt 
    159  1.58.2.1      yamt 	if (cfg->nc == NULL) {
    160  1.58.2.1      yamt 		syslog(LOG_ERR, "getnetconfigent %s failed: %m",
    161  1.58.2.1      yamt 		    cfg_netconf[t]);
    162  1.58.2.1      yamt 		goto out;
    163  1.58.2.1      yamt 	}
    164  1.58.2.1      yamt 
    165  1.58.2.1      yamt 	cfg->nb.buf = cfg->ai->ai_addr;
    166  1.58.2.1      yamt 	cfg->nb.len = cfg->nb.maxlen = cfg->ai->ai_addrlen;
    167  1.58.2.1      yamt 	if (reregister)
    168  1.58.2.1      yamt 		if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb)) {
    169  1.58.2.1      yamt 			syslog(LOG_ERR, "rpcb_set %s failed", cfg_netconf[t]);
    170  1.58.2.1      yamt 			goto out1;
    171  1.58.2.1      yamt 		}
    172  1.58.2.1      yamt 	return 0;
    173  1.58.2.1      yamt out1:
    174  1.58.2.1      yamt 	freenetconfigent(cfg->nc);
    175  1.58.2.1      yamt 	cfg->nc = NULL;
    176  1.58.2.1      yamt out:
    177  1.58.2.1      yamt 	freeaddrinfo(cfg->ai);
    178  1.58.2.1      yamt 	cfg->ai = NULL;
    179  1.58.2.1      yamt 	return -1;
    180  1.58.2.1      yamt }
    181  1.58.2.1      yamt 
    182  1.58.2.1      yamt static int
    183  1.58.2.1      yamt setupsock(struct conf *cfg, struct pollfd *set, int p)
    184  1.58.2.1      yamt {
    185  1.58.2.1      yamt 	int sock;
    186  1.58.2.1      yamt 	struct nfsd_args nfsdargs;
    187  1.58.2.1      yamt 	struct addrinfo *ai = cfg->ai;
    188  1.58.2.1      yamt 	int on = 1;
    189  1.58.2.1      yamt 
    190  1.58.2.1      yamt 	sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    191  1.58.2.1      yamt 
    192  1.58.2.1      yamt 	if (sock == -1) {
    193  1.58.2.1      yamt 		syslog(LOG_ERR, "can't create %s socket: %m", cfg_netconf[p]);
    194  1.58.2.1      yamt 		return -1;
    195  1.58.2.1      yamt 	}
    196  1.58.2.1      yamt 	if (cfg_family[p] == PF_INET6) {
    197  1.58.2.1      yamt 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
    198  1.58.2.1      yamt 		    sizeof(on)) == -1) {
    199  1.58.2.1      yamt 			syslog(LOG_ERR, "can't set v6-only binding for %s "
    200  1.58.2.1      yamt 			    "socket: %m", cfg_netconf[p]);
    201  1.58.2.1      yamt 			goto out;
    202  1.58.2.1      yamt 		}
    203  1.58.2.1      yamt 	}
    204  1.58.2.1      yamt 
    205  1.58.2.1      yamt 	if (cfg_protocol[p] == IPPROTO_TCP) {
    206  1.58.2.1      yamt 		if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
    207  1.58.2.1      yamt 		    sizeof(on)) == -1) {
    208  1.58.2.1      yamt 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR for %s: %m",
    209  1.58.2.1      yamt 			    cfg_netconf[p]);
    210  1.58.2.1      yamt 			goto out;
    211  1.58.2.1      yamt 		}
    212  1.58.2.1      yamt 	}
    213  1.58.2.1      yamt 
    214  1.58.2.1      yamt 	if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
    215  1.58.2.1      yamt 		syslog(LOG_ERR, "can't bind %s addr: %m", cfg_netconf[p]);
    216  1.58.2.1      yamt 		goto out;
    217  1.58.2.1      yamt 	}
    218  1.58.2.1      yamt 
    219  1.58.2.1      yamt 	if (cfg_protocol[p] == IPPROTO_TCP) {
    220  1.58.2.1      yamt 		if (listen(sock, 5) == -1) {
    221  1.58.2.1      yamt 			syslog(LOG_ERR, "listen failed");
    222  1.58.2.1      yamt 			goto out;
    223  1.58.2.1      yamt 		}
    224  1.58.2.1      yamt 	}
    225  1.58.2.1      yamt 
    226  1.58.2.1      yamt 	if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb) ||
    227  1.58.2.1      yamt 	    !rpcb_set(RPCPROG_NFS, 3, cfg->nc, &cfg->nb)) {
    228  1.58.2.1      yamt 		syslog(LOG_ERR, "can't register with %s portmap",
    229  1.58.2.1      yamt 		    cfg_netconf[p]);
    230  1.58.2.1      yamt 		goto out;
    231  1.58.2.1      yamt 	}
    232  1.58.2.1      yamt 
    233  1.58.2.1      yamt 
    234  1.58.2.1      yamt 	if (cfg_protocol[p] == IPPROTO_TCP)
    235  1.58.2.1      yamt 		set->fd = sock;
    236  1.58.2.1      yamt 	else {
    237  1.58.2.1      yamt 		nfsdargs.sock = sock;
    238  1.58.2.1      yamt 		nfsdargs.name = NULL;
    239  1.58.2.1      yamt 		nfsdargs.namelen = 0;
    240  1.58.2.1      yamt 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    241  1.58.2.1      yamt 			syslog(LOG_ERR, "can't add %s socket", cfg_netconf[p]);
    242  1.58.2.1      yamt 			goto out;
    243  1.58.2.1      yamt 		}
    244  1.58.2.1      yamt 		(void)close(sock);
    245  1.58.2.1      yamt 	}
    246  1.58.2.1      yamt 	return 0;
    247  1.58.2.1      yamt out:
    248  1.58.2.1      yamt 	(void)close(sock);
    249  1.58.2.1      yamt 	return -1;
    250  1.58.2.1      yamt }
    251  1.58.2.1      yamt 
    252       1.1       cgd /*
    253  1.58.2.2      yamt  * The functions daemon2_fork() and daemon2_detach() below provide
    254  1.58.2.2      yamt  * functionality similar to daemon(3) but split into two phases.
    255  1.58.2.2      yamt  * daemon2_fork() is called early, before creating resources that
    256  1.58.2.2      yamt  * cannot be inherited across a fork, such as threads or kqueues.
    257  1.58.2.2      yamt  * When the daemon is ready to provide service, daemon2_detach()
    258  1.58.2.2      yamt  * is called to complete the daemonization and signal the parent
    259  1.58.2.2      yamt  * process to exit.
    260  1.58.2.2      yamt  *
    261  1.58.2.2      yamt  * These functions could potentially be moved to a library and
    262  1.58.2.2      yamt  * shared by other daemons.
    263  1.58.2.2      yamt  *
    264  1.58.2.2      yamt  * The return value from daemon2_fork() is a file descriptor to
    265  1.58.2.2      yamt  * be passed as the first argument to daemon2_detach().
    266  1.58.2.2      yamt  */
    267  1.58.2.2      yamt 
    268  1.58.2.2      yamt static int
    269  1.58.2.2      yamt daemon2_fork(void)
    270  1.58.2.2      yamt {
    271  1.58.2.2      yamt 	 int i;
    272  1.58.2.2      yamt 	 int fd;
    273  1.58.2.2      yamt 	 int r;
    274  1.58.2.2      yamt 	 pid_t pid;
    275  1.58.2.2      yamt 	 int detach_msg_pipe[2];
    276  1.58.2.2      yamt 
    277  1.58.2.2      yamt 	 /*
    278  1.58.2.2      yamt 	  * Set up a pipe for singalling the parent, making sure the
    279  1.58.2.2      yamt 	  * write end does not get allocated one of the file
    280  1.58.2.2      yamt 	  * descriptors that may be closed in daemon2_detach().  The
    281  1.58.2.2      yamt 	  * read end does not need such protection.
    282  1.58.2.2      yamt 	  */
    283  1.58.2.2      yamt 	 for (i = 0; i < 3; i++) {
    284  1.58.2.2      yamt 		 r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
    285  1.58.2.2      yamt 		 if (r < 0)
    286  1.58.2.2      yamt 			 return -1;
    287  1.58.2.2      yamt 		 if (detach_msg_pipe[1] <= STDERR_FILENO &&
    288  1.58.2.2      yamt 		     (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
    289  1.58.2.2      yamt 			 (void)dup2(fd, detach_msg_pipe[0]);
    290  1.58.2.2      yamt 			 (void)dup2(fd, detach_msg_pipe[1]);
    291  1.58.2.2      yamt 			 if (fd > STDERR_FILENO)
    292  1.58.2.2      yamt 				 (void)close(fd);
    293  1.58.2.2      yamt 			 continue;
    294  1.58.2.2      yamt 		 }
    295  1.58.2.2      yamt 		 break;
    296  1.58.2.2      yamt 	 }
    297  1.58.2.2      yamt 
    298  1.58.2.2      yamt 	 pid = fork();
    299  1.58.2.2      yamt 	 switch (pid) {
    300  1.58.2.2      yamt 	 case -1:
    301  1.58.2.2      yamt 		 return -1;
    302  1.58.2.2      yamt 	 case 0:
    303  1.58.2.2      yamt 		 /* child */
    304  1.58.2.2      yamt 		 (void)close(detach_msg_pipe[0]);
    305  1.58.2.2      yamt 		 return detach_msg_pipe[1];
    306  1.58.2.2      yamt 	 default:
    307  1.58.2.2      yamt 		 break;
    308  1.58.2.2      yamt 	}
    309  1.58.2.2      yamt 
    310  1.58.2.2      yamt 	/* Parent */
    311  1.58.2.2      yamt 	(void)close(detach_msg_pipe[1]);
    312  1.58.2.2      yamt 
    313  1.58.2.2      yamt 	for (;;) {
    314  1.58.2.2      yamt 		ssize_t nread;
    315  1.58.2.2      yamt 		char dummy;
    316  1.58.2.2      yamt 		nread = read(detach_msg_pipe[0], &dummy, 1);
    317  1.58.2.2      yamt 		if (nread < 0) {
    318  1.58.2.2      yamt 			if (errno == EINTR)
    319  1.58.2.2      yamt 				continue;
    320  1.58.2.2      yamt 			_exit(1);
    321  1.58.2.2      yamt 		} else if (nread == 0) {
    322  1.58.2.2      yamt 			_exit(1);
    323  1.58.2.2      yamt 		} else { /* nread > 0 */
    324  1.58.2.2      yamt 			_exit(0);
    325  1.58.2.2      yamt 		}
    326  1.58.2.2      yamt 	}
    327  1.58.2.2      yamt }
    328  1.58.2.2      yamt 
    329  1.58.2.2      yamt static int
    330  1.58.2.2      yamt daemon2_detach(int parentfd, int nochdir, int noclose)
    331  1.58.2.2      yamt {
    332  1.58.2.2      yamt 	int fd;
    333  1.58.2.2      yamt 
    334  1.58.2.2      yamt 	if (setsid() == -1)
    335  1.58.2.2      yamt 		return -1;
    336  1.58.2.2      yamt 
    337  1.58.2.2      yamt 	if (!nochdir)
    338  1.58.2.2      yamt 		(void)chdir("/");
    339  1.58.2.2      yamt 
    340  1.58.2.2      yamt 	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
    341  1.58.2.2      yamt 		(void)dup2(fd, STDIN_FILENO);
    342  1.58.2.2      yamt 		(void)dup2(fd, STDOUT_FILENO);
    343  1.58.2.2      yamt 		(void)dup2(fd, STDERR_FILENO);
    344  1.58.2.2      yamt 		if (fd > STDERR_FILENO)
    345  1.58.2.2      yamt 			(void)close(fd);
    346  1.58.2.2      yamt 	}
    347  1.58.2.2      yamt 
    348  1.58.2.2      yamt 	while (1) {
    349  1.58.2.2      yamt 		ssize_t r = write(parentfd, "", 1);
    350  1.58.2.2      yamt 		if (r < 0) {
    351  1.58.2.2      yamt 			if (errno == EINTR)
    352  1.58.2.2      yamt 				continue;
    353  1.58.2.2      yamt 			else if (errno == EPIPE)
    354  1.58.2.2      yamt 				break;
    355  1.58.2.2      yamt 			else
    356  1.58.2.2      yamt 				return -1;
    357  1.58.2.2      yamt 		} else if (r == 0) {
    358  1.58.2.2      yamt 			/* Should not happen */
    359  1.58.2.2      yamt 			return -1;
    360  1.58.2.2      yamt 		} else {
    361  1.58.2.2      yamt 			break;
    362  1.58.2.2      yamt 		}
    363  1.58.2.2      yamt 	}
    364  1.58.2.2      yamt 
    365  1.58.2.2      yamt 	(void)close(parentfd);
    366  1.58.2.2      yamt 
    367  1.58.2.2      yamt 	return 0;
    368  1.58.2.2      yamt }
    369  1.58.2.2      yamt 
    370  1.58.2.2      yamt /*
    371       1.1       cgd  * Nfs server daemon mostly just a user context for nfssvc()
    372      1.11   mycroft  *
    373       1.1       cgd  * 1 - do file descriptor and signal cleanup
    374      1.50      yamt  * 2 - create the nfsd thread(s)
    375      1.11   mycroft  * 3 - create server socket(s)
    376      1.11   mycroft  * 4 - register socket with portmap
    377      1.11   mycroft  *
    378      1.28     lukem  * For connectionless protocols, just pass the socket into the kernel via
    379      1.11   mycroft  * nfssvc().
    380      1.11   mycroft  * For connection based sockets, loop doing accepts. When you get a new
    381      1.28     lukem  * socket from accept, pass the msgsock into the kernel via nfssvc().
    382       1.1       cgd  * The arguments are:
    383      1.11   mycroft  *	-r - reregister with portmapper
    384  1.58.2.1      yamt  *	-t - support only tcp nfs clients
    385  1.58.2.1      yamt  *	-u - support only udp nfs clients
    386  1.58.2.1      yamt  *	-n num how many threads to create.
    387  1.58.2.1      yamt  *	-4 - use only ipv4
    388  1.58.2.1      yamt  *	-6 - use only ipv6
    389       1.1       cgd  */
    390      1.11   mycroft int
    391      1.58     joerg main(int argc, char *argv[])
    392       1.1       cgd {
    393  1.58.2.1      yamt 	struct conf cfg[4];
    394  1.58.2.1      yamt 	struct pollfd set[__arraycount(cfg)];
    395  1.58.2.1      yamt 	int ch, connect_type_cnt;
    396  1.58.2.1      yamt 	size_t i, nfsdcnt;
    397  1.58.2.1      yamt 	int reregister;
    398  1.58.2.1      yamt 	int tcpflag, udpflag;
    399  1.58.2.1      yamt 	int ip6flag, ip4flag;
    400  1.58.2.1      yamt 	int s, compat;
    401  1.58.2.2      yamt 	int parent_fd = -1;
    402      1.11   mycroft 
    403      1.11   mycroft #define	DEFNFSDCNT	 4
    404      1.11   mycroft 	nfsdcnt = DEFNFSDCNT;
    405  1.58.2.1      yamt 	compat = reregister = 0;
    406  1.58.2.1      yamt 	tcpflag = udpflag = 1;
    407  1.58.2.1      yamt 	ip6flag = ip4flag = 1;
    408  1.58.2.1      yamt #define	GETOPT	"46n:rtu"
    409  1.58.2.1      yamt #define	USAGE	"[-46rtu] [-n num_servers]"
    410      1.30   thorpej 	while ((ch = getopt(argc, argv, GETOPT)) != -1) {
    411      1.11   mycroft 		switch (ch) {
    412      1.32      fvdl 		case '6':
    413      1.32      fvdl 			ip6flag = 1;
    414  1.58.2.1      yamt 			ip4flag = 0;
    415      1.32      fvdl 			s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    416      1.32      fvdl 			if (s < 0 && (errno == EPROTONOSUPPORT ||
    417      1.32      fvdl 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
    418      1.32      fvdl 				ip6flag = 0;
    419      1.32      fvdl 			else
    420      1.32      fvdl 				close(s);
    421      1.32      fvdl 			break;
    422  1.58.2.1      yamt 		case '4':
    423  1.58.2.1      yamt 			ip6flag = 0;
    424  1.58.2.1      yamt 			ip4flag = 1;
    425  1.58.2.1      yamt 			s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    426  1.58.2.1      yamt 			if (s < 0 && (errno == EPROTONOSUPPORT ||
    427  1.58.2.1      yamt 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
    428  1.58.2.1      yamt 				ip4flag = 0;
    429  1.58.2.1      yamt 			else
    430  1.58.2.1      yamt 				close(s);
    431  1.58.2.1      yamt 			break;
    432      1.11   mycroft 		case 'n':
    433      1.11   mycroft 			nfsdcnt = atoi(optarg);
    434      1.53      yamt 			if (nfsdcnt < 1) {
    435  1.58.2.1      yamt 				warnx("nfsd count %zu; reset to %d", nfsdcnt,
    436  1.58.2.1      yamt 				    DEFNFSDCNT);
    437      1.11   mycroft 				nfsdcnt = DEFNFSDCNT;
    438      1.11   mycroft 			}
    439      1.11   mycroft 			break;
    440       1.1       cgd 		case 'r':
    441      1.11   mycroft 			reregister = 1;
    442       1.1       cgd 			break;
    443       1.1       cgd 		case 't':
    444  1.58.2.1      yamt 			compat |= 2;
    445      1.11   mycroft 			tcpflag = 1;
    446  1.58.2.1      yamt 			udpflag = 0;
    447       1.1       cgd 			break;
    448       1.1       cgd 		case 'u':
    449  1.58.2.1      yamt 			compat |= 1;
    450  1.58.2.1      yamt 			tcpflag = 0;
    451      1.11   mycroft 			udpflag = 1;
    452      1.11   mycroft 			break;
    453       1.1       cgd 		default:
    454       1.1       cgd 		case '?':
    455       1.1       cgd 			usage();
    456  1.58.2.1      yamt 		}
    457      1.30   thorpej 	}
    458      1.11   mycroft 	argv += optind;
    459      1.11   mycroft 	argc -= optind;
    460       1.1       cgd 
    461  1.58.2.1      yamt 	if (compat == 3) {
    462  1.58.2.1      yamt 		warnx("Old -tu options detected; enabling both udp and tcp.");
    463  1.58.2.1      yamt 		warnx("This is the default behavior now and you can remove");
    464  1.58.2.1      yamt 		warnx("all options.");
    465  1.58.2.1      yamt 		tcpflag = udpflag = 1;
    466  1.58.2.1      yamt 		if (ip6flag == 1 && ip4flag == 0)
    467  1.58.2.1      yamt 			ip4flag = 1;
    468       1.1       cgd 	}
    469      1.31     soren 
    470       1.1       cgd 	if (debug == 0) {
    471  1.58.2.2      yamt 		parent_fd = daemon2_fork();
    472       1.1       cgd 	}
    473      1.11   mycroft 
    474  1.58.2.1      yamt 	openlog("nfsd", LOG_PID, LOG_DAEMON);
    475      1.32      fvdl 
    476  1.58.2.1      yamt 	for (i = 0; i < __arraycount(cfg); i++) {
    477  1.58.2.1      yamt 		if (ip4flag == 0 && cfg_family[i] == PF_INET)
    478  1.58.2.1      yamt 			continue;
    479  1.58.2.1      yamt 		if (ip6flag == 0 && cfg_family[i] == PF_INET6)
    480  1.58.2.1      yamt 			continue;
    481  1.58.2.1      yamt 		if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
    482  1.58.2.1      yamt 			continue;
    483  1.58.2.1      yamt 		if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
    484  1.58.2.1      yamt 			continue;
    485  1.58.2.1      yamt 		tryconf(&cfg[i], i, reregister);
    486       1.8       cgd 	}
    487      1.32      fvdl 
    488      1.11   mycroft 	for (i = 0; i < nfsdcnt; i++) {
    489      1.50      yamt 		pthread_t t;
    490      1.50      yamt 		int error;
    491      1.50      yamt 
    492      1.56     pooka 		error = pthread_create(&t, NULL, worker, NULL);
    493      1.50      yamt 		if (error) {
    494      1.50      yamt 			errno = error;
    495      1.50      yamt 			syslog(LOG_ERR, "pthread_create: %m");
    496      1.32      fvdl 			exit(1);
    497      1.32      fvdl 		}
    498      1.11   mycroft 	}
    499      1.11   mycroft 
    500      1.11   mycroft 	connect_type_cnt = 0;
    501  1.58.2.1      yamt 	for (i = 0; i < __arraycount(cfg); i++) {
    502  1.58.2.1      yamt 		set[i].fd = -1;
    503  1.58.2.1      yamt 		set[i].events = POLLIN;
    504  1.58.2.1      yamt 		set[i].revents = 0;
    505  1.58.2.1      yamt 
    506  1.58.2.1      yamt 		if (cfg[i].nc == NULL)
    507  1.58.2.1      yamt 			continue;
    508  1.58.2.1      yamt 
    509  1.58.2.1      yamt 		setupsock(&cfg[i], &set[i], i);
    510  1.58.2.1      yamt 		if (set[i].fd != -1)
    511  1.58.2.1      yamt 			connect_type_cnt++;
    512      1.32      fvdl 
    513  1.58.2.1      yamt 	}
    514      1.11   mycroft 
    515      1.11   mycroft 	if (connect_type_cnt == 0)
    516      1.11   mycroft 		exit(0);
    517      1.11   mycroft 
    518      1.51      yamt 	pthread_setname_np(pthread_self(), "master", NULL);
    519      1.11   mycroft 
    520  1.58.2.2      yamt 	if (debug == 0) {
    521  1.58.2.2      yamt 		daemon2_detach(parent_fd, 0, 0);
    522  1.58.2.2      yamt 		(void)signal(SIGHUP, SIG_IGN);
    523  1.58.2.2      yamt 		(void)signal(SIGINT, SIG_IGN);
    524  1.58.2.2      yamt 		(void)signal(SIGQUIT, SIG_IGN);
    525  1.58.2.2      yamt 		(void)signal(SIGSYS, nonfs);
    526  1.58.2.2      yamt 	}
    527  1.58.2.2      yamt 
    528      1.11   mycroft 	/*
    529      1.11   mycroft 	 * Loop forever accepting connections and passing the sockets
    530      1.11   mycroft 	 * into the kernel for the mounts.
    531      1.11   mycroft 	 */
    532      1.11   mycroft 	for (;;) {
    533  1.58.2.1      yamt 		if (poll(set, __arraycount(set), INFTIM) == -1) {
    534      1.49      elad 			syslog(LOG_ERR, "poll failed: %m");
    535      1.41   mycroft 			exit(1);
    536      1.11   mycroft 		}
    537      1.38   mycroft 
    538  1.58.2.1      yamt 		for (i = 0; i < __arraycount(set); i++) {
    539  1.58.2.1      yamt 			struct nfsd_args nfsdargs;
    540  1.58.2.1      yamt 			struct sockaddr_storage ss;
    541  1.58.2.1      yamt 			socklen_t len;
    542  1.58.2.1      yamt 			int msgsock;
    543  1.58.2.1      yamt 			int on = 1;
    544  1.58.2.1      yamt 
    545  1.58.2.1      yamt 			if ((set[i].revents & POLLIN) == 0)
    546  1.58.2.1      yamt 				continue;
    547  1.58.2.1      yamt 			len = sizeof(ss);
    548  1.58.2.1      yamt 			if ((msgsock = accept(set[i].fd,
    549  1.58.2.1      yamt 			    (struct sockaddr *)&ss, &len)) == -1) {
    550  1.58.2.1      yamt 				int serrno = errno;
    551      1.41   mycroft 				syslog(LOG_ERR, "accept failed: %m");
    552      1.57  christos 				if (serrno == EINTR || serrno == ECONNABORTED)
    553      1.57  christos 					continue;
    554       1.1       cgd 				exit(1);
    555       1.1       cgd 			}
    556  1.58.2.1      yamt 			if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
    557  1.58.2.1      yamt 			    sizeof(on)) == -1)
    558      1.11   mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    559      1.11   mycroft 			nfsdargs.sock = msgsock;
    560  1.58.2.1      yamt 			nfsdargs.name = (void *)&ss;
    561      1.11   mycroft 			nfsdargs.namelen = len;
    562      1.11   mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    563      1.11   mycroft 			(void)close(msgsock);
    564       1.1       cgd 		}
    565       1.1       cgd 	}
    566       1.1       cgd }
    567       1.1       cgd 
    568      1.58     joerg static void
    569      1.58     joerg usage(void)
    570       1.1       cgd {
    571  1.58.2.1      yamt 	(void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
    572       1.1       cgd 	exit(1);
    573       1.1       cgd }
    574       1.1       cgd 
    575      1.58     joerg static void
    576      1.58     joerg nonfs(int signo)
    577       1.1       cgd {
    578      1.11   mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    579       1.1       cgd }
    580