/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
/*
* The functions daemon2_fork() and daemon2_detach() below provide
* functionality similar to daemon(3) but split into two phases.
* daemon2_fork() is called early, before creating resources that
* cannot be inherited across a fork, such as threads or kqueues.
* When the daemon is ready to provide service, daemon2_detach()
* is called to complete the daemonization and signal the parent
* process to exit.
*
* These functions could potentially be moved to a library and
* shared by other daemons.
*
* The return value from daemon2_fork() is a file descriptor to
* be passed as the first argument to daemon2_detach().
*/
static int
daemon2_fork(void)
{
int i;
int fd;
int r;
pid_t pid;
int detach_msg_pipe[2];
/*
* Set up a pipe for signalling the parent, making sure the
* write end does not get allocated one of the file
* descriptors that may be closed in daemon2_detach(). The
* read end does not need such protection.
*/
for (i = 0; i < 3; i++) {
r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
if (r == -1)
return -1;
if (detach_msg_pipe[1] <= STDERR_FILENO &&
(fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
(void)dup2(fd, detach_msg_pipe[0]);
(void)dup2(fd, detach_msg_pipe[1]);
if (fd > STDERR_FILENO)
(void)close(fd);
continue;
}
break;
}
while (1) {
ssize_t r = write(parentfd, "", 1);
if (r == -1) {
if (errno == EINTR)
continue;
else if (errno == EPIPE)
break;
else
return -1;
} else if (r == 0) {
/* Should not happen */
return -1;
} else {
break;
}
}
(void)close(parentfd);
return 0;
}
/*
* Nfs server daemon mostly just a user context for nfssvc()
*
* 1 - do file descriptor and signal cleanup
* 2 - create the nfsd thread(s)
* 3 - create server socket(s)
* 4 - register socket with portmap
*
* For connectionless protocols, just pass the socket into the kernel via
* nfssvc().
* For connection based sockets, loop doing accepts. When you get a new
* socket from accept, pass the msgsock into the kernel via nfssvc().
* The arguments are:
* -r - reregister with portmapper
* -t - support only tcp nfs clients
* -u - support only udp nfs clients
* -n num how many threads to create.
* -4 - use only ipv4
* -6 - use only ipv6
*/
int
main(int argc, char *argv[])
{
struct conf cfg[4];
struct pollfd set[__arraycount(cfg)];
int ch, connect_type_cnt;
size_t i, nfsdcnt;
int reregister;
int tcpflag, udpflag;
int ip6flag, ip4flag;
int s, compat;
int parent_fd = -1;
pthread_t *workers;
if (compat == 3) {
warnx("Old -tu options detected; enabling both udp and tcp.");
warnx("This is the default behavior now and you can remove");
warnx("all options.");
tcpflag = udpflag = 1;
if (ip6flag == 1 && ip4flag == 0)
ip4flag = 1;
}
if (debug == 0) {
parent_fd = daemon2_fork();
if (parent_fd == -1)
logit(LOG_ERR, "daemon2_fork failed");
openlog("nfsd", LOG_PID, LOG_DAEMON);
}
memset(cfg, 0, sizeof(cfg));
for (i = 0; i < __arraycount(cfg); i++) {
if (ip4flag == 0 && cfg_family[i] == PF_INET)
continue;
if (ip6flag == 0 && cfg_family[i] == PF_INET6)
continue;
if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
continue;
if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
continue;
tryconf(&cfg[i], i, reregister);
}
if (connect_type_cnt == 0) {
for (i = 0; i < nfsdcnt; i++)
pthread_join(workers[i], NULL);
exit(EXIT_SUCCESS);
}
/*
* Loop forever accepting connections and passing the sockets
* into the kernel for the mounts.
*/
for (;;) {
if (poll(set, __arraycount(set), INFTIM) == -1) {
logit(LOG_ERR, "poll failed: %s", strerror(errno));
exit(EXIT_FAILURE);
}
for (i = 0; i < __arraycount(set); i++) {
struct nfsd_args nfsdargs;
struct sockaddr_storage ss;
socklen_t len;
int msgsock;
int on = 1;