Revert previous, don't write to the parent too early because it can exit

before the child is ready to serve. The child will write in daemon2_detach()
when it is ready.
While here:
- return EXIT_{SUCCESS,FAILURE)
- check syscall error against -1
This commit is contained in:
christos 2020-09-17 12:48:12 +00:00
parent fc25529d69
commit 74f5524807
1 changed files with 19 additions and 20 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: nfsd.c,v 1.72 2020/08/26 13:35:12 gson Exp $ */
/* $NetBSD: nfsd.c,v 1.73 2020/09/17 12:48:12 christos Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
#if 0
static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
#else
__RCSID("$NetBSD: nfsd.c,v 1.72 2020/08/26 13:35:12 gson Exp $");
__RCSID("$NetBSD: nfsd.c,v 1.73 2020/09/17 12:48:12 christos Exp $");
#endif
#endif /* not lint */
@ -128,10 +128,10 @@ worker(void *dummy)
pthread_setname_np(pthread_self(), "slave", NULL);
nfssvc_flag = NFSSVC_NFSD;
memset(&nsd, 0, sizeof(nsd));
while (nfssvc(nfssvc_flag, &nsd) < 0) {
while (nfssvc(nfssvc_flag, &nsd) == -1) {
if (errno != ENEEDAUTH) {
logit(LOG_ERR, "nfssvc: %s", strerror(errno));
exit(1);
exit(EXIT_FAILURE);
}
nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
}
@ -262,7 +262,7 @@ setupsock(struct conf *cfg, struct pollfd *set, int p)
nfsdargs.sock = sock;
nfsdargs.name = NULL;
nfsdargs.namelen = 0;
if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) == -1) {
logit(LOG_ERR, "can't add %s socket: %s",
cfg_netconf[p], strerror(errno));
goto out;
@ -308,7 +308,7 @@ daemon2_fork(void)
*/
for (i = 0; i < 3; i++) {
r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
if (r < 0)
if (r == -1)
return -1;
if (detach_msg_pipe[1] <= STDERR_FILENO &&
(fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
@ -328,7 +328,6 @@ daemon2_fork(void)
case 0:
/* child */
(void)close(detach_msg_pipe[0]);
(void)write(detach_msg_pipe[1], "", 1);
return detach_msg_pipe[1];
default:
break;
@ -341,14 +340,14 @@ daemon2_fork(void)
ssize_t nread;
char dummy;
nread = read(detach_msg_pipe[0], &dummy, 1);
if (nread < 0) {
if (nread == -1) {
if (errno == EINTR)
continue;
_exit(1);
_exit(EXIT_FAILURE);
} else if (nread == 0) {
_exit(1);
_exit(EXIT_FAILURE);
} else { /* nread > 0 */
_exit(0);
_exit(EXIT_SUCCESS);
}
}
}
@ -374,7 +373,7 @@ daemon2_detach(int parentfd, int nochdir, int noclose)
while (1) {
ssize_t r = write(parentfd, "", 1);
if (r < 0) {
if (r == -1) {
if (errno == EINTR)
continue;
else if (errno == EPIPE)
@ -441,7 +440,7 @@ main(int argc, char *argv[])
ip6flag = 1;
ip4flag = 0;
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0 && (errno == EPROTONOSUPPORT ||
if (s == -1 && (errno == EPROTONOSUPPORT ||
errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
ip6flag = 0;
else
@ -451,7 +450,7 @@ main(int argc, char *argv[])
ip6flag = 0;
ip4flag = 1;
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0 && (errno == EPROTONOSUPPORT ||
if (s == -1 && (errno == EPROTONOSUPPORT ||
errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
ip4flag = 0;
else
@ -522,7 +521,7 @@ main(int argc, char *argv[])
workers = calloc(nfsdcnt, sizeof(*workers));
if (workers == NULL) {
logit(LOG_ERR, "thread alloc %s", strerror(errno));
exit(1);
exit(EXIT_FAILURE);
}
for (i = 0; i < nfsdcnt; i++) {
@ -532,7 +531,7 @@ main(int argc, char *argv[])
if (error) {
errno = error;
logit(LOG_ERR, "pthread_create: %s", strerror(errno));
exit(1);
exit(EXIT_FAILURE);
}
}
@ -563,7 +562,7 @@ main(int argc, char *argv[])
if (connect_type_cnt == 0) {
for (i = 0; i < nfsdcnt; i++)
pthread_join(workers[i], NULL);
exit(0);
exit(EXIT_SUCCESS);
}
/*
@ -573,7 +572,7 @@ main(int argc, char *argv[])
for (;;) {
if (poll(set, __arraycount(set), INFTIM) == -1) {
logit(LOG_ERR, "poll failed: %s", strerror(errno));
exit(1);
exit(EXIT_FAILURE);
}
for (i = 0; i < __arraycount(set); i++) {
@ -593,7 +592,7 @@ main(int argc, char *argv[])
strerror(errno));
if (serrno == EINTR || serrno == ECONNABORTED)
continue;
exit(1);
exit(EXIT_FAILURE);
}
if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
sizeof(on)) == -1)
@ -612,7 +611,7 @@ static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
exit(1);
exit(EXIT_FAILURE);
}
static void