From c8f302df1addf189bee8dd56911a2bab8ba7822c Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 12 Jun 2013 00:59:19 +0200 Subject: [PATCH] Add process group support to init and sh. --- utils/init.cpp | 2 ++ utils/mxsh.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/utils/init.cpp b/utils/init.cpp index aa414ffc..ee2a7a56 100644 --- a/utils/init.cpp +++ b/utils/init.cpp @@ -39,6 +39,8 @@ int child() snprintf(init_pid_str, sizeof(pid_t)*3, "%ju", (uintmax_t) init_pid); setenv("INIT_PID", init_pid_str, 1); + setpgid(0, 0); + const char* programname = "sh"; const char* newargv[] = { programname, NULL }; diff --git a/utils/mxsh.cpp b/utils/mxsh.cpp index 95187164..affa8ad5 100644 --- a/utils/mxsh.cpp +++ b/utils/mxsh.cpp @@ -83,6 +83,7 @@ int runcommandline(const char** tokens, bool* exitexec) const char* execmode; const char* outputfile; pid_t childpid; + pid_t pgid = -1; bool internal; int internalresult; readcmd: @@ -166,6 +167,13 @@ readcmd: if ( childpid < 0 ) { perror("fork"); goto out; } if ( childpid ) { + if ( !internal ) + { + if ( pgid == -1 ) + pgid = childpid; + setpgid(childpid, pgid); + } + if ( pipein != 0 ) { close(pipein); pipein = 0; } if ( pipeout != 1 ) { close(pipeout); pipeout = 1; } if ( pipeinnext != 0 ) { pipein = pipeinnext; pipeinnext = 0; } @@ -175,6 +183,9 @@ readcmd: result = 0; goto out; } + if ( strcmp(execmode, "&") == 0 ) + pgid = -1; + if ( strcmp(execmode, "&") == 0 || strcmp(execmode, "|") == 0 ) { goto readcmd; @@ -205,6 +216,8 @@ readcmd: goto out; } + setpgid(0, pgid != -1 ? pgid : 0); + if ( pipeinnext != 0 ) { close(pipeinnext); } if ( pipein != 0 )