[core] allow LISTEN_PID to be ppid if TRACEME (fixes #3137)

allow LISTEN_PID to be ppid (parent pid) if TRACEME set in environment
(e.g. for strace, gdb on Linux; valgrind starts lighttpd as LISTEN_PID)

x-ref:
  "TRACEME environment option in tests broken with LISTEN_PID"
  https://redmine.lighttpd.net/issues/3137
master
Glenn Strauss 1 year ago
parent aeba314454
commit 1e335b3724

@ -581,7 +581,13 @@ static int network_socket_activation_from_env(server *srv, network_socket_config
char *listen_fds = getenv("LISTEN_FDS");
pid_t lpid = listen_pid ? (pid_t)strtoul(listen_pid,NULL,10) : 0;
int nfds = listen_fds ? atoi(listen_fds) : 0;
int rc = (lpid == getpid() && nfds > 0 && nfds < 5000)
int rc = (nfds > 0 && nfds < 5000
&& (lpid == getpid()
#ifndef _WIN32
|| (0 == strncmp(listen_pid, "parent:", 7)
&& getppid() == (pid_t)strtoul(listen_pid+7,NULL,10))
#endif
))
? network_socket_activation_nfds(srv, s, nfds)
: 0;
unsetenv("LISTEN_PID");

@ -115,6 +115,7 @@ sub stop_proc {
my $pid = $self->{LIGHTTPD_PID};
if (defined $pid && $pid != -1) {
kill('USR1', $pid) if (($ENV{"TRACEME"}||'') eq 'strace');
kill('TERM', $pid) or return -1;
return -1 if ($pid != waitpid($pid, 0));
} else {
@ -205,6 +206,9 @@ sub start_proc {
# set up systemd socket activation env vars
$ENV{LISTEN_FDS} = "1";
$ENV{LISTEN_PID} = $$;
if (defined($ENV{"TRACEME"}) && $ENV{"TRACEME"} ne "valgrind") {
$ENV{LISTEN_PID} = "parent:$$"; # lighttpd extension
}
listen($SOCK, 1024) || die "listen: $!";
if (fileno($SOCK) != 3) { # SD_LISTEN_FDS_START 3
require POSIX;

Loading…
Cancel
Save