Add signal handler for SIGTERM
When the head process receives a SIGTERM we have to forward that to the init process, which in turn has to forward it to the executed process which is jailed. That process can then decide to exit, which also terminates the init and head process through SIGCHILD/wait means.
This commit is contained in:
parent
1f5e1a9c1f
commit
5fe9ba36a2
1 changed files with 26 additions and 0 deletions
26
main.c
26
main.c
|
@ -8,6 +8,9 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
pid_t pid_child;
|
||||||
|
|
||||||
void drop_root(void) {
|
void drop_root(void) {
|
||||||
// Drop root privileges
|
// Drop root privileges
|
||||||
|
@ -26,6 +29,15 @@ void drop_root(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void forward_signal(int sig)
|
||||||
|
{
|
||||||
|
if(kill(pid_child, sig) == -1) {
|
||||||
|
printf("Unable to forward signal %d to child\n", sig);
|
||||||
|
if(sig == SIGTERM)
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char** argdup(int argc, const char** argv)
|
char** argdup(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
char** newargs = malloc(sizeof(char*) * (argc+1));
|
char** newargs = malloc(sizeof(char*) * (argc+1));
|
||||||
|
@ -67,6 +79,12 @@ int main(int argc, const char** argv)
|
||||||
|
|
||||||
if (pid != 0)
|
if (pid != 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Setup signal handler to forward SIGTERM
|
||||||
|
pid_child = pid;
|
||||||
|
if(signal(SIGTERM, forward_signal) == SIG_ERR) {
|
||||||
|
printf("Unable to setup signal handler in head\n");
|
||||||
|
}
|
||||||
// parent waits for child then exits
|
// parent waits for child then exits
|
||||||
int status;
|
int status;
|
||||||
if(waitpid(pid, &status, 0) == -1)
|
if(waitpid(pid, &status, 0) == -1)
|
||||||
|
@ -93,6 +111,14 @@ int main(int argc, const char** argv)
|
||||||
pid_t exited_child;
|
pid_t exited_child;
|
||||||
int child_status;
|
int child_status;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
// Setup forward for SIGTERM
|
||||||
|
pid_child = first_child;
|
||||||
|
if(signal(SIGTERM, forward_signal) == SIG_ERR) {
|
||||||
|
printf("Unable to setup signal forward in init. Aborting.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
exited_child = wait(&child_status);
|
exited_child = wait(&child_status);
|
||||||
err = errno;
|
err = errno;
|
||||||
|
|
Loading…
Reference in a new issue