2008-01-28 21:37:16 +00:00
|
|
|
/* source: xio-listen.c */
|
2016-07-22 06:54:31 +00:00
|
|
|
/* Copyright Gerhard Rieger and contributors (see file CHANGES) */
|
2008-01-27 12:00:08 +00:00
|
|
|
/* Published under the GNU General Public License V.2, see file COPYING */
|
|
|
|
|
|
|
|
/* this file contains the source for listen socket options */
|
|
|
|
|
|
|
|
#include "xiosysincludes.h"
|
|
|
|
|
|
|
|
#if WITH_LISTEN
|
|
|
|
|
|
|
|
#include "xioopen.h"
|
|
|
|
#include "xio-named.h"
|
|
|
|
#include "xio-socket.h"
|
|
|
|
#include "xio-ip.h"
|
|
|
|
#include "xio-ip4.h"
|
|
|
|
#include "xio-listen.h"
|
|
|
|
#include "xio-tcpwrap.h"
|
|
|
|
|
|
|
|
/***** LISTEN options *****/
|
|
|
|
const struct optdesc opt_backlog = { "backlog", NULL, OPT_BACKLOG, GROUP_LISTEN, PH_LISTEN, TYPE_INT, OFUNC_SPEC };
|
|
|
|
const struct optdesc opt_fork = { "fork", NULL, OPT_FORK, GROUP_CHILD, PH_PASTACCEPT, TYPE_BOOL, OFUNC_SPEC };
|
2023-10-26 16:50:29 +00:00
|
|
|
const struct optdesc opt_max_children = { "max-children", NULL, OPT_MAX_CHILDREN, GROUP_CHILD, PH_PASTACCEPT, TYPE_INT, OFUNC_SPEC };
|
2023-09-30 13:18:39 +00:00
|
|
|
const struct optdesc opt_children_shutup = { "children-shutup", "child-shutup", OPT_CHILDREN_SHUTUP, GROUP_CHILD, PH_PASTACCEPT, TYPE_INT, OFUNC_OFFSET, XIO_OFFSETOF(shutup) };
|
2008-01-27 12:00:08 +00:00
|
|
|
/**/
|
|
|
|
#if (WITH_UDP || WITH_TCP)
|
|
|
|
const struct optdesc opt_range = { "range", NULL, OPT_RANGE, GROUP_RANGE, PH_ACCEPT, TYPE_STRING, OFUNC_SPEC };
|
|
|
|
#endif
|
2020-12-31 13:56:04 +00:00
|
|
|
const struct optdesc opt_accept_timeout = { "accept-timeout", "listen-timeout", OPT_ACCEPT_TIMEOUT, GROUP_LISTEN, PH_LISTEN, TYPE_TIMEVAL, OFUNC_OFFSET, XIO_OFFSETOF(para.socket.accept_timeout) };
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2008-09-20 21:47:06 +00:00
|
|
|
/*
|
|
|
|
applies and consumes the following option:
|
|
|
|
PH_INIT, PH_PASTSOCKET, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_EARLY,
|
|
|
|
PH_PREOPEN, PH_FD, PH_CONNECTED, PH_LATE, PH_LATE2
|
|
|
|
OPT_FORK, OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_BACKLOG, OPT_RANGE, tcpwrap,
|
|
|
|
OPT_SOURCEPORT, OPT_LOWPORT, cloexec
|
|
|
|
*/
|
2008-01-27 12:00:08 +00:00
|
|
|
int
|
|
|
|
xioopen_listen(struct single *xfd, int xioflags,
|
|
|
|
struct sockaddr *us, socklen_t uslen,
|
|
|
|
struct opt *opts, struct opt *opts0,
|
|
|
|
int pf, int socktype, int proto) {
|
|
|
|
int level;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
#if WITH_RETRY
|
|
|
|
if (xfd->forever || xfd->retry) {
|
|
|
|
level = E_INFO;
|
|
|
|
} else
|
|
|
|
#endif /* WITH_RETRY */
|
|
|
|
level = E_ERROR;
|
|
|
|
|
|
|
|
while (true) { /* loop over failed attempts */
|
|
|
|
|
|
|
|
/* tcp listen; this can fork() for us; it only returns on error or on
|
|
|
|
successful establishment of tcp connection */
|
|
|
|
result = _xioopen_listen(xfd, xioflags,
|
|
|
|
(struct sockaddr *)us, uslen,
|
2011-12-04 14:14:34 +00:00
|
|
|
opts, pf, socktype, proto, level);
|
2008-01-27 12:00:08 +00:00
|
|
|
/*! not sure if we should try again on retry/forever */
|
|
|
|
switch (result) {
|
|
|
|
case STAT_OK: break;
|
|
|
|
#if WITH_RETRY
|
|
|
|
case STAT_RETRYLATER:
|
|
|
|
case STAT_RETRYNOW:
|
|
|
|
if (xfd->forever || xfd->retry) {
|
|
|
|
dropopts(opts, PH_ALL); opts = copyopts(opts0, GROUP_ALL);
|
|
|
|
if (result == STAT_RETRYLATER) {
|
|
|
|
Nanosleep(&xfd->intervall, NULL);
|
|
|
|
}
|
|
|
|
dropopts(opts, PH_ALL); opts = copyopts(opts0, GROUP_ALL);
|
|
|
|
--xfd->retry;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return STAT_NORETRY;
|
|
|
|
#endif /* WITH_RETRY */
|
|
|
|
default:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} /* drop out on success */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-20 21:47:06 +00:00
|
|
|
/* creates the listening socket, bind, applies options; waits for incoming
|
|
|
|
connection, checks its source address and port. Depending on fork option, it
|
|
|
|
may fork a subprocess.
|
2008-08-17 21:28:11 +00:00
|
|
|
pf specifies the syntax expected for range option. In the case of generic
|
2008-09-20 19:42:50 +00:00
|
|
|
socket it is 0 (expecting raw binary data), and the real pf can be obtained
|
2008-08-17 21:28:11 +00:00
|
|
|
from us->af_family; for other socket types pf == us->af_family
|
2008-01-27 12:00:08 +00:00
|
|
|
Returns 0 if a connection was accepted; with fork option, this is always in
|
|
|
|
a subprocess!
|
|
|
|
Other return values indicate a problem; this can happen in the master
|
|
|
|
process or in a subprocess.
|
2008-09-20 21:47:06 +00:00
|
|
|
This function does not retry. If you need retries, handle this in a
|
|
|
|
loop in the calling function (and always provide the options...)
|
2008-09-20 19:42:50 +00:00
|
|
|
After fork, we set the forever/retry of the child process to 0
|
2008-09-20 21:47:06 +00:00
|
|
|
applies and consumes the following option:
|
|
|
|
PH_INIT, PH_PASTSOCKET, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_EARLY,
|
|
|
|
PH_PREOPEN, PH_FD, PH_CONNECTED, PH_LATE, PH_LATE2
|
|
|
|
OPT_FORK, OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_BACKLOG, OPT_RANGE, tcpwrap,
|
|
|
|
OPT_SOURCEPORT, OPT_LOWPORT, cloexec
|
2008-01-27 12:00:08 +00:00
|
|
|
*/
|
|
|
|
int _xioopen_listen(struct single *xfd, int xioflags, struct sockaddr *us, socklen_t uslen,
|
|
|
|
struct opt *opts, int pf, int socktype, int proto, int level) {
|
|
|
|
int backlog = 5; /* why? 1 seems to cause problems under some load */
|
|
|
|
char infobuff[256];
|
2011-11-26 13:24:09 +00:00
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
|
|
|
|
|
2023-10-22 21:15:49 +00:00
|
|
|
if ((xfd->fd = xiosocket(opts, pf?pf:us->sa_family, socktype, proto, level)) < 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_PASTSOCKET);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2020-12-31 13:56:04 +00:00
|
|
|
applyopts_offset(xfd, opts);
|
2008-01-27 12:00:08 +00:00
|
|
|
applyopts_cloexec(xfd->fd, opts);
|
|
|
|
|
2023-10-26 17:45:01 +00:00
|
|
|
/* Phase prebind */
|
|
|
|
xiosock_reuseaddr(xfd->fd, proto, opts);
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_PREBIND);
|
2023-10-26 17:45:01 +00:00
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_BIND);
|
2008-01-27 12:00:08 +00:00
|
|
|
if (Bind(xfd->fd, (struct sockaddr *)us, uslen) < 0) {
|
2014-03-09 21:08:19 +00:00
|
|
|
Msg4(level, "bind(%d, {%s}, "F_socklen"): %s", xfd->fd,
|
2008-01-27 12:00:08 +00:00
|
|
|
sockaddr_info(us, uslen, infobuff, sizeof(infobuff)), uslen,
|
|
|
|
strerror(errno));
|
|
|
|
Close(xfd->fd);
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WITH_UNIX
|
|
|
|
if (us->sa_family == AF_UNIX) {
|
2021-10-31 10:39:47 +00:00
|
|
|
if (((union sockaddr_union *)us)->un.sun_path[0] != '\0') {
|
|
|
|
applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_FD);
|
|
|
|
} else {
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_FD);
|
2021-10-31 10:39:47 +00:00
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_PASTBIND);
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_UNIX
|
|
|
|
if (us->sa_family == AF_UNIX) {
|
2021-10-31 10:39:47 +00:00
|
|
|
if (((union sockaddr_union *)us)->un.sun_path[0] != '\0') {
|
|
|
|
applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_EARLY);
|
|
|
|
applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_PREOPEN);
|
|
|
|
} else {
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_EARLY);
|
|
|
|
applyopts(xfd, -1, opts, PH_PREOPEN);
|
2021-10-31 10:39:47 +00:00
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
#endif /* WITH_UNIX */
|
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_PRELISTEN);
|
2023-09-30 16:39:13 +00:00
|
|
|
retropt_int(opts, OPT_BACKLOG, &backlog);
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_LISTEN);
|
2023-09-30 16:39:13 +00:00
|
|
|
if (Listen(xfd->fd, backlog) < 0) {
|
|
|
|
Error3("listen(%d, %d): %s", xfd->fd, backlog, strerror(errno));
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
return _xioopen_accept_fd(xfd, xioflags, us, uslen, opts, pf, proto,level);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _xioopen_accept_fd(
|
|
|
|
struct single *xfd,
|
|
|
|
int xioflags,
|
|
|
|
struct sockaddr *us,
|
|
|
|
socklen_t uslen,
|
|
|
|
struct opt *opts,
|
|
|
|
int pf,
|
|
|
|
int proto,
|
|
|
|
int level)
|
|
|
|
{
|
|
|
|
struct sockaddr sa;
|
|
|
|
socklen_t salen;
|
|
|
|
char *rangename;
|
|
|
|
bool dofork = false;
|
|
|
|
int maxchildren = 0;
|
|
|
|
char infobuff[256];
|
|
|
|
char lisname[256];
|
|
|
|
union sockaddr_union _peername;
|
|
|
|
union sockaddr_union _sockname;
|
|
|
|
union sockaddr_union *pa = &_peername; /* peer address */
|
|
|
|
union sockaddr_union *la = &_sockname; /* local address */
|
|
|
|
socklen_t pas = sizeof(_peername); /* peer address size */
|
|
|
|
socklen_t las = sizeof(_sockname); /* local address size */
|
|
|
|
int result;
|
|
|
|
|
|
|
|
retropt_bool(opts, OPT_FORK, &dofork);
|
|
|
|
|
|
|
|
if (dofork) {
|
|
|
|
if (!(xioflags & XIO_MAYFORK)) {
|
|
|
|
Error("option fork not allowed here");
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
xfd->flags |= XIO_DOESFORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
retropt_int(opts, OPT_MAX_CHILDREN, &maxchildren);
|
|
|
|
|
|
|
|
if (! dofork && maxchildren) {
|
|
|
|
Error("option max-children not allowed without option fork");
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dofork) {
|
|
|
|
xiosetchilddied(); /* set SIGCHLD handler */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Under some circumstances (e.g., TCP listen on port 0) bind() fills empty
|
|
|
|
fields that we want to know. */
|
|
|
|
salen = sizeof(sa);
|
|
|
|
if (Getsockname(xfd->fd, us, &uslen) < 0) {
|
|
|
|
Warn4("getsockname(%d, %p, {%d}): %s",
|
|
|
|
xfd->fd, &us, uslen, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_IP4 /*|| WITH_IP6*/
|
|
|
|
if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
|
2023-11-05 12:56:58 +00:00
|
|
|
if (xioparserange(rangename, pf, &xfd->para.socket.range,
|
2023-11-05 18:39:01 +00:00
|
|
|
xfd->para.socket.ip.ai_flags)
|
2023-11-05 12:56:58 +00:00
|
|
|
< 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
free(rangename);
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
free(rangename);
|
|
|
|
xfd->para.socket.dorange = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
|
|
|
|
xio_retropt_tcpwrap(xfd, opts);
|
|
|
|
#endif /* && (WITH_TCP || WITH_UDP) && WITH_LIBWRAP */
|
|
|
|
|
|
|
|
#if WITH_TCP || WITH_UDP
|
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT, &xfd->para.socket.ip.sourceport) >= 0) {
|
|
|
|
xfd->para.socket.ip.dosourceport = true;
|
|
|
|
}
|
|
|
|
retropt_bool(opts, OPT_LOWPORT, &xfd->para.socket.ip.lowport);
|
|
|
|
#endif /* WITH_TCP || WITH_UDP */
|
|
|
|
|
2023-06-23 14:21:05 +00:00
|
|
|
if (xioparms.logopt == 'm') {
|
2008-01-27 12:00:08 +00:00
|
|
|
Info("starting accept loop, switching to syslog");
|
2023-06-23 14:21:05 +00:00
|
|
|
diag_set('y', xioparms.syslogfac); xioparms.logopt = 'y';
|
2008-01-27 12:00:08 +00:00
|
|
|
} else {
|
|
|
|
Info("starting accept loop");
|
|
|
|
}
|
|
|
|
while (true) { /* but we only loop if fork option is set */
|
|
|
|
char peername[256];
|
|
|
|
char sockname[256];
|
|
|
|
int ps; /* peer socket */
|
|
|
|
|
2009-05-05 20:34:05 +00:00
|
|
|
pa = &_peername;
|
|
|
|
la = &_sockname;
|
|
|
|
salen = sizeof(struct sockaddr);
|
2008-01-27 12:00:08 +00:00
|
|
|
do {
|
|
|
|
/*? int level = E_ERROR;*/
|
|
|
|
Notice1("listening on %s", sockaddr_info(us, uslen, lisname, sizeof(lisname)));
|
2020-12-31 13:56:04 +00:00
|
|
|
if (xfd->para.socket.accept_timeout.tv_sec > 0 ||
|
|
|
|
xfd->para.socket.accept_timeout.tv_usec > 0) {
|
|
|
|
fd_set rfd;
|
|
|
|
struct timeval tmo;
|
|
|
|
FD_ZERO(&rfd);
|
|
|
|
FD_SET(xfd->fd, &rfd);
|
|
|
|
tmo.tv_sec = xfd->para.socket.accept_timeout.tv_sec;
|
|
|
|
tmo.tv_usec = xfd->para.socket.accept_timeout.tv_usec;
|
|
|
|
while (1) {
|
|
|
|
if (Select(xfd->fd+1, &rfd, NULL, NULL, &tmo) < 0) {
|
|
|
|
if (errno != EINTR) {
|
2021-01-03 15:56:50 +00:00
|
|
|
Error5("Select(%d, &0x%lx, NULL, NULL, {%ld.%06ld}): %s", xfd->fd+1, 1L<<(xfd->fd+1),
|
2020-12-31 13:56:04 +00:00
|
|
|
xfd->para.socket.accept_timeout.tv_sec, xfd->para.socket.accept_timeout.tv_usec,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!FD_ISSET(xfd->fd, &rfd)) {
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
Warn1("accept: %s", strerror(ETIMEDOUT));
|
|
|
|
Close(xfd->fd);
|
|
|
|
Notice("Waiting for child processes to terminate");
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_flags = SA_NOCLDSTOP/*|SA_RESTART*/
|
|
|
|
#ifdef SA_SIGINFO /* not on Linux 2.0(.33) */
|
|
|
|
|SA_SIGINFO
|
|
|
|
#endif
|
|
|
|
#ifdef SA_NOMASK
|
|
|
|
|SA_NOMASK
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
#if HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined(SA_SIGINFO)
|
|
|
|
act.sa_sigaction = 0;
|
|
|
|
#else /* Linux 2.0(.33) does not have sigaction.sa_sigaction */
|
|
|
|
act.sa_handler = 0;
|
|
|
|
#endif
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
Sigaction(SIGCHLD, &act, NULL);
|
|
|
|
wait(NULL);
|
|
|
|
Exit(0);
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
ps = Accept(xfd->fd, (struct sockaddr *)&sa, &salen);
|
|
|
|
if (ps >= 0) {
|
|
|
|
/*0 Info4("accept(%d, %p, {"F_Zu"}) -> %d", xfd->fd, &sa, salen, ps);*/
|
|
|
|
break; /* success, break out of loop */
|
|
|
|
}
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (errno == ECONNABORTED) {
|
2014-03-09 21:08:19 +00:00
|
|
|
Notice4("accept(%d, %p, {"F_socklen"}): %s",
|
2008-01-27 12:00:08 +00:00
|
|
|
xfd->fd, &sa, salen, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-09 21:08:19 +00:00
|
|
|
Msg4(level, "accept(%d, %p, {"F_socklen"}): %s",
|
2008-01-27 12:00:08 +00:00
|
|
|
xfd->fd, &sa, salen, strerror(errno));
|
|
|
|
Close(xfd->fd);
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
} while (true);
|
|
|
|
applyopts_cloexec(ps, opts);
|
|
|
|
if (Getpeername(ps, &pa->soa, &pas) < 0) {
|
2023-06-14 09:14:46 +00:00
|
|
|
Notice4("getpeername(%d, %p, {"F_socklen"}): %s",
|
2008-01-27 12:00:08 +00:00
|
|
|
ps, pa, pas, strerror(errno));
|
2008-09-22 20:17:55 +00:00
|
|
|
pa = NULL;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
if (Getsockname(ps, &la->soa, &las) < 0) {
|
|
|
|
Warn4("getsockname(%d, %p, {"F_socklen"}): %s",
|
2008-09-22 20:17:55 +00:00
|
|
|
ps, la, las, strerror(errno));
|
|
|
|
la = NULL;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
Notice2("accepting connection from %s on %s",
|
2009-03-31 21:16:47 +00:00
|
|
|
pa?
|
|
|
|
sockaddr_info(&pa->soa, pas, peername, sizeof(peername)):"NULL",
|
|
|
|
la?
|
|
|
|
sockaddr_info(&la->soa, las, sockname, sizeof(sockname)):"NULL");
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2008-09-22 20:17:55 +00:00
|
|
|
if (pa != NULL && la != NULL && xiocheckpeer(xfd, pa, la) < 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
if (Shutdown(ps, 2) < 0) {
|
|
|
|
Info2("shutdown(%d, 2): %s", ps, strerror(errno));
|
|
|
|
}
|
2013-03-25 19:42:58 +00:00
|
|
|
Close(ps);
|
2008-01-27 12:00:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-31 21:16:47 +00:00
|
|
|
if (pa != NULL)
|
|
|
|
Info1("permitting connection from %s",
|
|
|
|
sockaddr_info((struct sockaddr *)pa, pas,
|
|
|
|
infobuff, sizeof(infobuff)));
|
2008-01-27 12:00:08 +00:00
|
|
|
|
|
|
|
if (dofork) {
|
2008-09-22 20:17:55 +00:00
|
|
|
pid_t pid; /* mostly int; only used with fork */
|
2011-11-26 13:24:09 +00:00
|
|
|
sigset_t mask_sigchld;
|
|
|
|
|
2023-06-23 20:48:26 +00:00
|
|
|
/* Block SIGCHLD until parent is ready to react */
|
2011-11-26 13:24:09 +00:00
|
|
|
sigemptyset(&mask_sigchld);
|
|
|
|
sigaddset(&mask_sigchld, SIGCHLD);
|
|
|
|
Sigprocmask(SIG_BLOCK, &mask_sigchld, NULL);
|
|
|
|
|
2023-10-26 16:50:29 +00:00
|
|
|
if ((pid =
|
|
|
|
xio_fork(false, level==E_ERROR?level:E_WARN,
|
2023-09-30 13:18:39 +00:00
|
|
|
xfd->shutup))
|
2023-10-26 16:50:29 +00:00
|
|
|
< 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
Close(xfd->fd);
|
2011-11-26 13:24:09 +00:00
|
|
|
Sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
if (pid == 0) { /* child */
|
2008-09-22 20:17:55 +00:00
|
|
|
pid_t cpid = Getpid();
|
2011-11-26 13:24:09 +00:00
|
|
|
Sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
|
2008-09-22 20:17:55 +00:00
|
|
|
|
2011-11-26 13:49:51 +00:00
|
|
|
Info1("just born: child process "F_pid, cpid);
|
2008-09-22 20:17:55 +00:00
|
|
|
xiosetenvulong("PID", cpid, 1);
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if (Close(xfd->fd) < 0) {
|
|
|
|
Info2("close(%d): %s", xfd->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
xfd->fd = ps;
|
|
|
|
|
|
|
|
#if WITH_RETRY
|
|
|
|
/* !? */
|
2008-09-22 20:17:55 +00:00
|
|
|
xfd->forever = false; xfd->retry = 0;
|
2008-01-27 12:00:08 +00:00
|
|
|
level = E_ERROR;
|
|
|
|
#endif /* WITH_RETRY */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* server: continue loop with listen */
|
|
|
|
/* shutdown() closes the socket even for the child process, but
|
|
|
|
close() does what we want */
|
|
|
|
if (Close(ps) < 0) {
|
|
|
|
Info2("close(%d): %s", ps, strerror(errno));
|
|
|
|
}
|
2011-11-26 13:24:09 +00:00
|
|
|
|
|
|
|
/* now we are ready to handle signals */
|
|
|
|
Sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
|
|
|
|
|
2023-06-23 20:48:26 +00:00
|
|
|
|
2011-11-26 13:24:09 +00:00
|
|
|
while (maxchildren) {
|
|
|
|
if (num_child < maxchildren) break;
|
|
|
|
Notice("maxchildren are active, waiting");
|
2015-01-19 20:48:35 +00:00
|
|
|
/* UINT_MAX would even be nicer, but Openindiana works only
|
|
|
|
with 31 bits */
|
|
|
|
while (!Sleep(INT_MAX)) ; /* any signal lets us continue */
|
2011-11-26 13:24:09 +00:00
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
Info("still listening");
|
|
|
|
} else {
|
|
|
|
if (Close(xfd->fd) < 0) {
|
|
|
|
Info2("close(%d): %s", xfd->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
xfd->fd = ps;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-26 07:13:31 +00:00
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(xfd, -1, opts, PH_FD);
|
|
|
|
applyopts(xfd, -1, opts, PH_PASTSOCKET);
|
|
|
|
applyopts(xfd, -1, opts, PH_CONNECTED);
|
2008-01-27 12:00:08 +00:00
|
|
|
if ((result = _xio_openlate(xfd, opts)) < 0)
|
|
|
|
return result;
|
|
|
|
|
2008-09-22 20:17:55 +00:00
|
|
|
/* set the env vars describing the local and remote sockets */
|
2009-03-31 21:16:47 +00:00
|
|
|
if (la != NULL) xiosetsockaddrenv("SOCK", la, las, proto);
|
|
|
|
if (pa != NULL) xiosetsockaddrenv("PEER", pa, pas, proto);
|
2008-09-22 20:17:55 +00:00
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_LISTEN */
|