libposix: drafted SIGCHLD management
This commit is contained in:
parent
c27b2aa93b
commit
e0610f6b71
|
@ -221,6 +221,10 @@ extern int libposix_define_signal(PosixSignals signal, int code);
|
|||
|
||||
extern int libposix_define_realtime_signals(int sigrtmin, int sigrtmax);
|
||||
|
||||
/* Enable SIGCHLD emulation
|
||||
*/
|
||||
extern int libposix_emulate_SIGCHLD(void);
|
||||
|
||||
/* Define of WCONTINUED, WNOHANG and WUNTRACED bit flags.
|
||||
*
|
||||
* Note that WCONTINUED and WUNTRACED are not yet supported by libposix
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
"memory.c",
|
||||
"others.c",
|
||||
"processes.c",
|
||||
"sigchlds.c",
|
||||
"signals.c"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ libposix_init(int argc, char *argv[], PosixInit init)
|
|||
extern unsigned char *__signals_to_code_map;
|
||||
extern unsigned char *__code_to_signal_map;
|
||||
extern int *__handling_external_signal;
|
||||
extern int *__libposix_sigchld_target_pid;
|
||||
|
||||
WaitList *wait_list;
|
||||
int status;
|
||||
|
@ -47,6 +48,7 @@ libposix_init(int argc, char *argv[], PosixInit init)
|
|||
unsigned char signals_to_code[256];
|
||||
unsigned char code_to_signal[256];
|
||||
int handling_signal;
|
||||
int sigchld_target_pid;
|
||||
|
||||
assert(__initialized == 0);
|
||||
|
||||
|
@ -62,9 +64,11 @@ libposix_init(int argc, char *argv[], PosixInit init)
|
|||
memset(signals_to_code, 0, sizeof(signals_to_code));
|
||||
memset(code_to_signal, 0, sizeof(code_to_signal));
|
||||
handling_signal = 0;
|
||||
sigchld_target_pid = 0;
|
||||
__signals_to_code_map = signals_to_code;
|
||||
__code_to_signal_map = code_to_signal;
|
||||
__handling_external_signal = &handling_signal;
|
||||
__libposix_sigchld_target_pid = &sigchld_target_pid;
|
||||
if(!atnotify(__libposix_note_handler, 1))
|
||||
sysfatal("libposix: atnotify");
|
||||
|
||||
|
|
|
@ -39,5 +39,5 @@ extern int __libposix_note_handler(void *ureg, char *note);
|
|||
|
||||
extern void __libposix_free_wait_list(void);
|
||||
|
||||
|
||||
extern void __libposix_setup_new_process(void);
|
||||
|
||||
|
|
|
@ -28,6 +28,25 @@ static int __libposix_wnohang;
|
|||
|
||||
#define __POSIX_SIGNAL_PREFIX_LEN (sizeof(__POSIX_SIGNAL_PREFIX)-1)
|
||||
|
||||
static int
|
||||
fork_without_sigchld(int *errnop)
|
||||
{
|
||||
int pid = fork();
|
||||
|
||||
if(pid == 0)
|
||||
__libposix_setup_new_process();
|
||||
return pid;
|
||||
}
|
||||
|
||||
int (*__libposix_fork)(int *errnop) = fork_without_sigchld;
|
||||
|
||||
void
|
||||
__libposix_setup_new_process(void)
|
||||
{
|
||||
/* reset wait list for the child */
|
||||
*__libposix_wait_list = nil;
|
||||
}
|
||||
|
||||
void
|
||||
__libposix_free_wait_list(void)
|
||||
{
|
||||
|
@ -110,13 +129,7 @@ POSIX_getppid(int *errnop)
|
|||
int
|
||||
POSIX_fork(int *errnop)
|
||||
{
|
||||
int pid = fork();
|
||||
|
||||
if(pid == 0){
|
||||
/* reset wait list for the child */
|
||||
*__libposix_wait_list = nil;
|
||||
}
|
||||
return pid;
|
||||
return __libposix_fork(errnop);
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
* This file is part of Jehanne.
|
||||
*
|
||||
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* Jehanne is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <u.h>
|
||||
#include <lib9.h>
|
||||
#include <posix.h>
|
||||
#include "internal.h"
|
||||
|
||||
/* rendezvous points */
|
||||
extern unsigned char *__signals_to_code_map;
|
||||
extern unsigned char *__code_to_signal_map;
|
||||
|
||||
/* pointer to the pid to forward notes to */
|
||||
int *__libposix_sigchld_target_pid;
|
||||
|
||||
#define CHILD_READY(pid) ((long)rendezvous(&__signals_to_code_map, (void*)(~(pid))))
|
||||
#define C2P_READY(pid) ((long)rendezvous(&__code_to_signal_map, (void*)(~(pid))))
|
||||
|
||||
static void
|
||||
release_inherited_resources(void)
|
||||
{
|
||||
notify(nil);
|
||||
rfork(RFCNAMEG|RFCENVG|RFNOTEG|RFCFDG);
|
||||
bind("#P", "/proc", MREPL);
|
||||
rfork(RFNOMNT);
|
||||
}
|
||||
|
||||
static void
|
||||
forwarding_note_handler(void *ureg, char *note)
|
||||
{
|
||||
postnote(PNPROC, *__libposix_sigchld_target_pid, note);
|
||||
noted(NCONT);
|
||||
}
|
||||
|
||||
static void
|
||||
forward_wait_msg(int sigchld_receiver, char *name)
|
||||
{
|
||||
int n;
|
||||
char buf[512], err[ERRMAX], note[512], *fld[5];
|
||||
|
||||
snprint(buf, sizeof(buf), "/proc/%d/args", getpid());
|
||||
n = open(buf, OWRITE);
|
||||
write(n, name, strlen(name)+1);
|
||||
close(n);
|
||||
|
||||
n = 0;
|
||||
WaitInterrupted:
|
||||
n = await(buf, sizeof buf-1);
|
||||
if(n < 0){
|
||||
rerrstr(err, ERRMAX);
|
||||
if(strstr(err, "no living children") == nil)
|
||||
goto WaitInterrupted;
|
||||
snprint(note, sizeof(note), "%s: %r", name);
|
||||
if(sigchld_receiver)
|
||||
postnote(PNPROC, sigchld_receiver, note);
|
||||
exits(note);
|
||||
}
|
||||
buf[n] = '\0';
|
||||
if(jehanne_tokenize(buf, fld, nelem(fld)) != nelem(fld)){
|
||||
snprint(note, sizeof(note), "%s: couldn't parse wait message", name);
|
||||
if(sigchld_receiver)
|
||||
postnote(PNPROC, sigchld_receiver, note);
|
||||
exits(note);
|
||||
}
|
||||
snprint(note, sizeof(note), __POSIX_SIGNAL_PREFIX " %d", __signals_to_code_map[PosixSIGCHLD]);
|
||||
if(sigchld_receiver){
|
||||
postnote(PNPROC, sigchld_receiver, note);
|
||||
}
|
||||
exits(fld[4]);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
fork_with_sigchld(int *errnop)
|
||||
{
|
||||
int father = getpid();
|
||||
int p2c;
|
||||
long c2p = -1, child = -1;
|
||||
char proxy_name[256];
|
||||
|
||||
/* Father here:
|
||||
* - create P2C
|
||||
* - wait for C2P to be ready
|
||||
* - register P2C in children list
|
||||
* - return P2C pid
|
||||
*/
|
||||
switch(p2c = rfork(RFPROC|RFMEM)){
|
||||
case -1:
|
||||
return -1;
|
||||
case 0:
|
||||
/* P2C here:
|
||||
* - create C2P
|
||||
* - wait for the child pid
|
||||
* - release all inherited resources
|
||||
* - install forwarding_note_handler
|
||||
* - send to father the C2P pid
|
||||
* - start waiting for the child
|
||||
*/
|
||||
switch(c2p = rfork(RFPROC|RFMEM)){
|
||||
case -1:
|
||||
while(C2P_READY(-2) == -1)
|
||||
;
|
||||
exits("rfork (c2p)");
|
||||
case 0:
|
||||
/* C2P here:
|
||||
* - create child
|
||||
* - release all inherited resources
|
||||
* - install forwarding_note_handler
|
||||
* - send to P2C the child pid
|
||||
* - start forwarding notes to the father
|
||||
*/
|
||||
switch(child = fork()){
|
||||
case -1:
|
||||
while(CHILD_READY(-2) == -1)
|
||||
;
|
||||
exits("rfork (child)");
|
||||
case 0:
|
||||
/* Beloved child here
|
||||
*/
|
||||
__libposix_setup_new_process();
|
||||
return 0;
|
||||
default:
|
||||
release_inherited_resources();
|
||||
*__libposix_sigchld_target_pid = father;
|
||||
notify(forwarding_note_handler);
|
||||
snprint(proxy_name, sizeof(proxy_name), "libposix signal proxy %d < %d", father, child);
|
||||
while(CHILD_READY(child) == -1)
|
||||
;
|
||||
forward_wait_msg(father, proxy_name);
|
||||
}
|
||||
default:
|
||||
while((child = CHILD_READY(-3)) == -1)
|
||||
;
|
||||
child = ~child;
|
||||
if(child < 0){
|
||||
while(C2P_READY(-2) == -1)
|
||||
;
|
||||
waitpid();
|
||||
exits("rfork (child)");
|
||||
}
|
||||
release_inherited_resources();
|
||||
*__libposix_sigchld_target_pid = child;
|
||||
notify(forwarding_note_handler);
|
||||
snprint(proxy_name, sizeof(proxy_name), "libposix signal proxy %d > %d", father, child);
|
||||
while(C2P_READY(c2p) == -1)
|
||||
;
|
||||
forward_wait_msg(0, proxy_name);
|
||||
}
|
||||
default:
|
||||
while((c2p = C2P_READY(-3)) == -1)
|
||||
;
|
||||
c2p = ~c2p;
|
||||
if(c2p < 0){
|
||||
waitpid();
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* TODO: register p2c among known children so that kill can
|
||||
* handle them properly.
|
||||
*/
|
||||
|
||||
return p2c;
|
||||
}
|
||||
|
||||
int
|
||||
libposix_emulate_SIGCHLD(void)
|
||||
{
|
||||
extern int (*__libposix_fork)(int *errnop);
|
||||
__libposix_fork = fork_with_sigchld;
|
||||
return 1;
|
||||
}
|
|
@ -161,7 +161,7 @@ terminated_by_signal(int sig)
|
|||
exits(buf);
|
||||
}
|
||||
|
||||
static int
|
||||
int
|
||||
send_control_msg(int pid, char *msg)
|
||||
{
|
||||
int fd, n;
|
||||
|
|
Loading…
Reference in New Issue