Skip to content

Commit f75ae23

Browse files
committed
sigchain: mask out common signals
Signed-off-by: Jeff King <[email protected]>
1 parent 86edd90 commit f75ae23

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

sigchain.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int maybe_sigaction(int sig,
3232
#endif
3333
}
3434

35-
int sigchain_push(int sig, sigchain_fun f)
35+
static int sigchain_push_with_mask(int sig, sigchain_fun f, const sigset_t *mask)
3636
{
3737
struct sigaction sa;
3838
struct sigchain_signal *s = signals + sig;
@@ -41,6 +41,10 @@ int sigchain_push(int sig, sigchain_fun f)
4141
memset(&sa, 0, sizeof(sa));
4242
sa.sa_handler = f;
4343
sa.sa_flags = SA_RESTART;
44+
#ifndef GIT_WINDOWS_NATIVE
45+
if (mask)
46+
memcpy(&sa.sa_mask, mask, sizeof(*mask));
47+
#endif
4448

4549
ALLOC_GROW(s->old, s->n + 1, s->alloc);
4650
if (maybe_sigaction(sig, &sa, &s->old[s->n]) < 0)
@@ -50,6 +54,11 @@ int sigchain_push(int sig, sigchain_fun f)
5054
return 0;
5155
}
5256

57+
int sigchain_push(int sig, sigchain_fun f)
58+
{
59+
return sigchain_push_with_mask(sig, f, NULL);
60+
}
61+
5362
int sigchain_pop(int sig)
5463
{
5564
struct sigaction dummy;
@@ -66,11 +75,20 @@ int sigchain_pop(int sig)
6675

6776
void sigchain_push_common(sigchain_fun f)
6877
{
69-
sigchain_push(SIGINT, f);
70-
sigchain_push(SIGHUP, f);
71-
sigchain_push(SIGTERM, f);
72-
sigchain_push(SIGQUIT, f);
73-
sigchain_push(SIGPIPE, f);
78+
sigset_t mask;
79+
80+
sigemptyset(&mask);
81+
sigaddset(&mask, SIGINT);
82+
sigaddset(&mask, SIGHUP);
83+
sigaddset(&mask, SIGTERM);
84+
sigaddset(&mask, SIGQUIT);
85+
sigaddset(&mask, SIGPIPE);
86+
87+
sigchain_push_with_mask(SIGINT, f, &mask);
88+
sigchain_push_with_mask(SIGHUP, f, &mask);
89+
sigchain_push_with_mask(SIGTERM, f, &mask);
90+
sigchain_push_with_mask(SIGQUIT, f, &mask);
91+
sigchain_push_with_mask(SIGPIPE, f, &mask);
7492
}
7593

7694
void sigchain_pop_common(void)

0 commit comments

Comments
 (0)