Skip to content

Commit 498b723

Browse files
committed
Set child process as foreground
When stdin is a tty, this sets the child process as foreground process group so that it handles signals from the terminal. For instance if running a Python shell or similar, ctrl-c will go as SIGINT to the child Python process instead of the monitor process, which would terminate when Python is capable of handling the terminal. Fixes #369 Signed-off-by: Valentin David <me@valentindavid.com>
1 parent 0c408e1 commit 498b723

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

bubblewrap.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,7 @@ main (int argc,
28782878
pid_t pid;
28792879
int event_fd = -1;
28802880
int child_wait_fd = -1;
2881+
int pgid_wait_fd = -1;
28812882
int setup_finished_pipe[] = {-1, -1};
28822883
const char *new_cwd;
28832884
uid_t ns_uid;
@@ -3102,6 +3103,12 @@ main (int argc,
31023103
if (child_wait_fd == -1)
31033104
die_with_error ("eventfd()");
31043105

3106+
if (isatty (0)) {
3107+
pgid_wait_fd = eventfd (0, EFD_CLOEXEC);
3108+
if (pgid_wait_fd == -1)
3109+
die_with_error ("eventfd()");
3110+
}
3111+
31053112
/* Track whether pre-exec setup finished if we're reporting process exit */
31063113
if (opt_json_status_fd != -1)
31073114
{
@@ -3149,6 +3156,21 @@ main (int argc,
31493156

31503157
if (pid != 0)
31513158
{
3159+
if (pgid_wait_fd != -1)
3160+
{
3161+
pid_t pgrp;
3162+
3163+
/* wait for child call to setpgrp */
3164+
res = read (pgid_wait_fd, &val, 8);
3165+
close (pgid_wait_fd);
3166+
3167+
pgrp = getpgid (pid);
3168+
if (tcsetpgrp (0, pgrp) == -1)
3169+
{
3170+
die_with_error ("Setting terminal foreground process group failed");
3171+
}
3172+
}
3173+
31523174
/* Parent, outside sandbox, privileged (initially) */
31533175

31543176
if (intermediate_pids_sockets[0] != -1)
@@ -3219,6 +3241,15 @@ main (int argc,
32193241
return monitor_child (event_fd, pid, setup_finished_pipe[0]);
32203242
}
32213243

3244+
if (pgid_wait_fd != -1)
3245+
{
3246+
setpgrp ();
3247+
3248+
val = 1;
3249+
TEMP_FAILURE_RETRY (write (pgid_wait_fd, &val, 8));
3250+
close (pgid_wait_fd);
3251+
}
3252+
32223253
if (opt_pidns_fd > 0)
32233254
{
32243255
if (setns (opt_pidns_fd, CLONE_NEWPID) != 0)

0 commit comments

Comments
 (0)