Skip to content

Commit 298caa6

Browse files
carenasgitster
authored andcommitted
daemon: use sigaction() to install child_handler()
In a future change, the flags used for processing SIGCHLD will need to be updated, which is only possible by using sigaction(). Factor out the call to set the signal handler and use sigaction instead of signal for the systems that allow that, which has the added benefit of using BSD semantics reliably and therefore not needing the rearming call. Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0dae92 commit 298caa6

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

daemon.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -912,14 +912,19 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
912912
add_child(&cld, addr, addrlen);
913913
}
914914

915-
static void child_handler(int signo UNUSED)
915+
static void child_handler(int signo MAYBE_UNUSED)
916916
{
917917
/*
918-
* Otherwise empty handler because systemcalls will get interrupted
919-
* upon signal receipt
920-
* SysV needs the handler to be rearmed
918+
* Otherwise empty handler because systemcalls should get interrupted
919+
* upon signal receipt.
921920
*/
922-
signal(SIGCHLD, child_handler);
921+
#ifdef USE_NON_POSIX_SIGNAL
922+
/*
923+
* SysV needs the handler to be rearmed, but this is known
924+
* to trigger infinite recursion crashes at least in AIX.
925+
*/
926+
signal(signo, child_handler);
927+
#endif
923928
}
924929

925930
static int set_reuse_addr(int sockfd)
@@ -1118,8 +1123,26 @@ static void socksetup(struct string_list *listen_addr, int listen_port, struct s
11181123
}
11191124
}
11201125

1126+
#ifndef USE_NON_POSIX_SIGNAL
1127+
1128+
static void set_signal_handler(struct sigaction *psa)
1129+
{
1130+
sigemptyset(&psa->sa_mask);
1131+
psa->sa_flags = SA_NOCLDSTOP | SA_RESTART;
1132+
psa->sa_handler = child_handler;
1133+
sigaction(SIGCHLD, psa, NULL);
1134+
}
1135+
1136+
#else
1137+
1138+
static void set_signal_handler(struct sigaction *psa UNUSED)
1139+
{
1140+
signal(SIGCHLD, child_handler);
1141+
}
1142+
11211143
static int service_loop(struct socketlist *socklist)
11221144
{
1145+
struct sigaction sa;
11231146
struct pollfd *pfd;
11241147

11251148
CALLOC_ARRAY(pfd, socklist->nr);
@@ -1129,7 +1152,7 @@ static int service_loop(struct socketlist *socklist)
11291152
pfd[i].events = POLLIN;
11301153
}
11311154

1132-
signal(SIGCHLD, child_handler);
1155+
set_signal_handler(&sa);
11331156

11341157
for (;;) {
11351158
check_dead_children();

0 commit comments

Comments
 (0)