Skip to content

Commit 61a8e1f

Browse files
committed
Avoid using signal() to establish a signal handler
This is explicitly warned against by man:signal(2). With glibc, its behavior depends on compiler options. Use sigaction(2) instead.
1 parent e6695dd commit 61a8e1f

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

agent/qrexec-agent-data.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ static void sigusr1_handler(int __attribute__((__unused__))x)
7474
void prepare_child_env(void) {
7575
char pid_s[10];
7676

77-
signal(SIGCHLD, sigchld_handler);
78-
signal(SIGUSR1, sigusr1_handler);
77+
struct sigaction action = {
78+
.sa_handler = sigchld_handler,
79+
.sa_flags = 0,
80+
};
81+
sigemptyset(&action.sa_mask);
82+
if (sigaction(SIGCHLD, &action, NULL)) abort();
83+
action.sa_handler = sigusr1_handler;
84+
if (sigaction(SIGUSR1, &action, NULL)) abort();
7985
int res = snprintf(pid_s, sizeof(pid_s), "%d", getpid());
8086
if (res < 0) abort();
8187
if (res >= (int)sizeof(pid_s)) abort();

agent/qrexec-agent.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,14 @@ int main(int argc, char **argv)
928928
}
929929

930930
init();
931-
signal(SIGCHLD, sigchld_handler);
932-
signal(SIGTERM, sigterm_handler);
931+
struct sigaction action = {
932+
.sa_handler = sigchld_handler,
933+
.sa_flags = SA_RESTART,
934+
};
935+
sigemptyset(&action.sa_mask);
936+
sigaction(SIGCHLD, &action, NULL);
937+
action.sa_handler = sigterm_handler;
938+
sigaction(SIGTERM, &action, NULL);
933939
signal(SIGPIPE, SIG_IGN);
934940

935941
sigemptyset(&selectmask);

daemon/qrexec-daemon-common.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ int handle_agent_handshake(libvchan_t *vchan, bool remote_send_first)
219219
static void sigchld_handler(int x __attribute__((__unused__)))
220220
{
221221
sigchld = 1;
222-
signal(SIGCHLD, sigchld_handler);
223222
}
224223

225224
/* See also qrexec-agent.c:wait_for_session_maybe() */
@@ -271,7 +270,12 @@ int prepare_local_fds(struct qrexec_parsed_command *command, struct buffer *stdi
271270
{
272271
if (stdin_buffer == NULL)
273272
abort();
274-
if (signal(SIGCHLD, sigchld_handler) == SIG_ERR)
273+
struct sigaction action = {
274+
.sa_handler = sigchld_handler,
275+
.sa_flags = 0,
276+
};
277+
sigemptyset(&action.sa_mask);
278+
if (sigaction(SIGCHLD, &action, NULL))
275279
return 126;
276280
return execute_parsed_qubes_rpc_command(command, &local_pid, &local_stdin_fd, &local_stdout_fd,
277281
NULL, stdin_buffer);

0 commit comments

Comments
 (0)