Skip to content

Commit 1aad1b9

Browse files
authored
Merge pull request #828 from rolandwalker/process-group-leader
Make tig the process group leader and automatically clean child processes
2 parents 4fbfb71 + ccbda97 commit 1aad1b9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

include/tig/display.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern unsigned int current_view;
4343
#define view_is_displayed(view) \
4444
(view == display[0] || view == display[1])
4545

46+
void init_tty(void);
4647
void init_display(void);
4748
void resize_display(void);
4849
void redraw_display(bool clear);

src/display.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ set_terminal_modes(void)
584584
leaveok(stdscr, false);
585585
}
586586

587-
static void
587+
void
588588
init_tty(void)
589589
{
590590
/* open */
@@ -598,6 +598,12 @@ init_tty(void)
598598
if (!opt_tty.attr)
599599
die("Failed allocation for tty attributes");
600600
tcgetattr(opt_tty.fd, opt_tty.attr);
601+
602+
/* process-group leader */
603+
signal(SIGTTOU, SIG_IGN);
604+
setpgid(getpid(), getpid());
605+
tcsetpgrp(opt_tty.fd, getpid());
606+
signal(SIGTTOU, SIG_DFL);
601607
}
602608

603609
void
@@ -607,7 +613,8 @@ init_display(void)
607613
const char *term;
608614
int x, y;
609615

610-
init_tty();
616+
if (!opt_tty.file)
617+
die("Can't initialize display without tty");
611618

612619
die_callback = done_display;
613620
if (atexit(done_display))

src/tig.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ die_if_failed(enum status_code code, const char *msg)
738738
die("%s: %s", msg, get_status_message(code));
739739
}
740740

741+
void
742+
hangup_children(void)
743+
{
744+
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
745+
return;
746+
killpg(getpid(), SIGHUP);
747+
}
748+
741749
static inline enum status_code
742750
handle_git_prefix(void)
743751
{
@@ -772,6 +780,10 @@ main(int argc, const char *argv[])
772780
enum request request = parse_options(argc, argv, pager_mode);
773781
struct view *view;
774782

783+
init_tty();
784+
785+
atexit(hangup_children);
786+
775787
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
776788
die("Failed to setup signal handler");
777789

test/README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ test scripts as long as `PATH` is set to include the directories `src/`
1111
and `test/tools`. The latter directory is where the test helper
1212
libraries are located, the most important of which is `libtest.sh`.
1313

14+
The test suite requires `stty -tostop` to be set in the running terminal,
15+
which is typically the default.
16+
1417
Options
1518
-------
1619

0 commit comments

Comments
 (0)