Browse Source

[multiple] fdevent_waitpid() wrapper

master
Glenn Strauss 7 months ago
parent
commit
29f50bdeb0
  1. 6
      src/configfile.c
  2. 13
      src/fdevent.c
  3. 1
      src/fdevent.h
  4. 4
      src/gw_backend.c
  5. 4
      src/mod_cgi.c
  6. 3
      src/mod_rrdtool.c
  7. 10
      src/mod_ssi.c
  8. 15
      src/server.c

6
src/configfile.c

@ -15,7 +15,9 @@
#include "sys-crypto.h"
#include <sys/stat.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <stdlib.h>
#include <fcntl.h>
@ -2087,7 +2089,6 @@ int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
}
else {
ssize_t rd;
pid_t wpid;
int wstatus = 0;
buffer *out = buffer_init();
close(fds[1]);
@ -2102,8 +2103,7 @@ int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
}
close(fds[0]);
fds[0] = -1;
while (-1 == (wpid = waitpid(pid, &wstatus, 0)) && errno == EINTR) ;
if (wpid != pid) {
if (pid != fdevent_waitpid(pid, &wstatus, 0)) {
log_perror(srv->errh, __FILE__, __LINE__, "waitpid \"%s\"",cmd);
ret = -1;
}

13
src/fdevent.c

@ -6,7 +6,6 @@
#include "log.h"
#include <sys/types.h>
#include <sys/wait.h>
#include "sys-socket.h"
#include <unistd.h>
@ -800,6 +799,18 @@ pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin
}
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
int fdevent_waitpid(pid_t pid, int * const status, int nb) {
const int flags = nb ? WNOHANG : 0;
pid_t rv;
do { rv = waitpid(pid, status, flags); } while (-1 == rv && errno == EINTR);
return rv;
}
typedef struct fdevent_cmd_pipe {
pid_t pid;
int fds[2];

1
src/fdevent.h

@ -93,6 +93,7 @@ int fdevent_open_devnull(void);
int fdevent_open_dirname(char *path, int symlinks);
int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr);
pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd);
int fdevent_waitpid(pid_t pid, int *status, int nb);
int fdevent_open_logger(const char *logger);
int fdevent_cycle_logger(const char *logger, int *curfd);
int fdevent_reaped_logger_pipe(pid_t pid);

4
src/gw_backend.c

@ -343,9 +343,7 @@ static int gw_proc_waitpid(gw_host *host, gw_proc *proc, log_error_st *errh) {
if (!proc->is_local) return 0;
if (proc->pid <= 0) return 0;
do {
rc = waitpid(proc->pid, &status, WNOHANG);
} while (-1 == rc && errno == EINTR);
rc = fdevent_waitpid(proc->pid, &status, 1);
if (0 == rc) return 0; /* child still running */
/* child terminated */

4
src/mod_cgi.c

@ -14,7 +14,9 @@
#include <sys/types.h>
#include "sys-mmap.h"
#include "sys-socket.h"
# include <sys/wait.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <unistd.h>
#include <errno.h>

3
src/mod_rrdtool.c

@ -7,7 +7,6 @@
#include "plugin.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
@ -72,7 +71,7 @@ FREE_FUNC(mod_rrd_free) {
if (p->write_fd >= 0) close(p->write_fd);
if (p->rrdtool_pid > 0 && p->srv_pid == p->srv->pid) {
/* collect status (blocking) */
while (-1 == waitpid(p->rrdtool_pid, NULL, 0) && errno == EINTR) ;
fdevent_waitpid(p->rrdtool_pid, NULL, 0);
}
}

10
src/mod_ssi.c

@ -17,7 +17,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <ctype.h>
#include <stdlib.h>
@ -806,11 +808,9 @@ static int process_ssi_stmt(request_st * const r, handler_ctx * const p, const c
/*
* OpenBSD and Solaris send a EINTR on SIGCHILD even if we ignore it
*/
while (-1 == waitpid(pid, &status, 0)) {
if (errno != EINTR) {
log_perror(errh, __FILE__, __LINE__, "waitpid failed");
break;
}
if (fdevent_waitpid(pid, &status, 0) < 0) {
log_perror(errh, __FILE__, __LINE__, "waitpid failed");
break;
}
if (!WIFEXITED(status)) {
log_error(errh, __FILE__, __LINE__, "process exited abnormally: %s", cmd);

15
src/server.c

@ -31,7 +31,6 @@ static const buffer default_server_tag = { CONST_STR_LEN(PACKAGE_DESC)+1, 0 };
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
@ -52,10 +51,6 @@ static const buffer default_server_tag = { CONST_STR_LEN(PACKAGE_DESC)+1, 0 };
# include <valgrind/valgrind.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_PWD_H
# include <grp.h>
# include <pwd.h>
@ -779,7 +774,7 @@ static void server_graceful_signal_prev_generation (void)
pid_t pid = (pid_t)strtol(prev_gen, NULL, 10);
unsetenv("LIGHTTPD_PREV_GEN");
if (pid <= 0) return; /*(should not happen)*/
if (pid == waitpid(pid, NULL, WNOHANG)) return; /*(pid exited; unexpected)*/
if (pid == fdevent_waitpid(pid,NULL,1)) return; /*(pid exited; unexpected)*/
kill(pid, SIGINT); /* signal previous generation for graceful shutdown */
}
@ -874,7 +869,7 @@ static int server_graceful_state_bg (server *srv) {
buffer_append_int(tb, pid);
setenv("LIGHTTPD_PREV_GEN", tb->ptr, 1);
}
/*while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) ;*//* detach? */
/*fdevent_waitpid(pid, NULL, 0);*//* detach? */
execv(argv[0], argv);
_exit(1);
}
@ -1576,7 +1571,7 @@ static int server_main_setup (server * const srv, int argc, char **argv) {
} else {
int status;
if (-1 != (pid = wait(&status))) {
if (-1 != (pid = fdevent_waitpid(-1, &status, 0))) {
log_epoch_secs = time(NULL);
if (plugins_call_handle_waitpid(srv, pid, status) != HANDLER_GO_ON) {
if (!timer) alarm((timer = 5));
@ -1845,7 +1840,7 @@ static void server_handle_sigchld (server * const srv) {
pid_t pid;
do {
int status;
pid = waitpid(-1, &status, WNOHANG);
pid = fdevent_waitpid(-1, &status, 1);
if (pid > 0) {
if (plugins_call_handle_waitpid(srv, pid, status) != HANDLER_GO_ON) {
continue;
@ -1998,7 +1993,7 @@ int main (int argc, char **argv) {
if (rc < 0 || !graceful_restart) break;
/* wait for all children to exit before graceful restart */
while (waitpid(-1, NULL, 0) > 0) ;
while (fdevent_waitpid(-1, NULL, 0) > 0) ;
} while (graceful_restart);
return rc;

Loading…
Cancel
Save