1 /* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2 /** \file signal.c Signal handling functions. */
16 static int signal_pipe
[2];
19 * Initialize the paraslash signal subsystem.
21 * This function creates a pipe, the signal pipe, to deliver pending
22 * signals to the application (Bernstein's trick). It should be called
23 * during the application's startup part, followed by subsequent calls
24 * to \ref para_install_sighandler() for each signal that should be caught.
26 * A generic signal handler is used for all signals simultaneously. When a
27 * signal arrives, the signal handler writes the number of the signal received
28 * to one end of the signal pipe. The application can test for pending signals
29 * by checking if the file descriptor of the other end of the signal pipe is
30 * ready for reading, see select(2).
32 * \return This function either succeeds or calls exit(3) to terminate the
33 * current process. On success, a signal task structure is returned.
35 struct signal_task
*signal_init_or_die(void)
37 struct signal_task
*st
;
40 PARA_NOTICE_LOG("setting up signal handling\n");
41 if (pipe(signal_pipe
) < 0) {
42 ret
= -ERRNO_TO_PARA_ERROR(errno
);
45 ret
= mark_fd_nonblocking(signal_pipe
[0]);
48 ret
= mark_fd_nonblocking(signal_pipe
[1]);
51 st
= para_calloc(sizeof(*st
));
52 st
->fd
= signal_pipe
[0];
55 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
59 /* Write the signal number to signal pipe. */
60 static void generic_signal_handler(int s
)
63 * Signal handlers that make system calls must save a copy of errno on
64 * entry to the handler and restore it on exit, to prevent the
65 * possibility of overwriting a errno value that had previously been
66 * set in the main program.
68 int save_errno
= errno
;
69 ssize_t ret
= write(signal_pipe
[1], &s
, sizeof(int));
71 if (ret
== sizeof(int)) {
76 PARA_EMERG_LOG("%s\n", strerror(errno
));
78 PARA_EMERG_LOG("short write to signal pipe\n");
85 * \param pid In case a child died, its pid is returned here.
87 * Call waitpid() and print a log message containing the pid and the cause of
90 * \return A (negative) paraslash error code on errors, zero, if no child died,
91 * one otherwise. If and only if the function returns one, the content of \a
96 int para_reap_child(pid_t
*pid
)
99 *pid
= waitpid(-1, &status
, WNOHANG
);
104 return -ERRNO_TO_PARA_ERROR(errno
);
105 if (WIFEXITED(status
))
106 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid
,
107 WEXITSTATUS(status
));
108 else if (WIFSIGNALED(status
))
109 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid
,
112 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid
);
117 * Install the given handler for the given signal.
119 * \param sig The number of the signal to catch.
120 * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
122 * This either succeeds or calls exit(EXIT_FAILURE).
126 void para_sigaction(int sig
, void (*handler
)(int))
128 struct sigaction act
;
130 PARA_DEBUG_LOG("catching signal %d\n", sig
);
131 act
.sa_handler
= handler
;
132 sigemptyset(&act
.sa_mask
);
134 if (sig
== SIGALRM
) {
135 #ifdef SA_INTERRUPT /* SunOS */
136 act
.sa_flags
|= SA_INTERRUPT
;
139 #ifdef SA_RESTART /* BSD */
140 act
.sa_flags
|= SA_RESTART
;
143 if (sigaction(sig
, &act
, NULL
) >= 0)
145 PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
151 * Install the generic signal handler for the given signal number.
153 * \param sig The number of the signal to catch.
155 * \sa signal(2), sigaction(2).
157 void para_install_sighandler(int sig
)
159 para_sigaction(sig
, &generic_signal_handler
);
163 * Block a signal for the caller.
165 * \param sig The signal to block.
167 * This sets the given signal in the current signal mask of the calling process
168 * to prevent this signal from delivery.
170 * \sa \ref para_unblock_signal(), sigprocmask(2), sigaddset(3).
172 void para_block_signal(int sig
)
176 PARA_DEBUG_LOG("blocking signal %d\n", sig
);
178 sigaddset(&set
, sig
);
179 sigprocmask(SIG_BLOCK
, &set
, NULL
);
185 * \param sig The signal to unblock.
187 * This function removes the given signal from the current set of blocked
190 * \sa \ref para_block_signal(), sigprocmask(2), sigaddset(3).
192 void para_unblock_signal(int sig
)
196 PARA_DEBUG_LOG("unblocking signal %d\n", sig
);
198 sigaddset(&set
, sig
);
199 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
203 * Return the number of the next pending signal.
205 * \param rfds The fd_set containing the signal pipe.
207 * \return On success, the number of the received signal is returned. If there
208 * is no signal currently pending, the function returns zero. On read errors
209 * from the signal pipe, the process is terminated.
211 int para_next_signal(fd_set
*rfds
)
214 int s
, ret
= read_nonblock(signal_pipe
[0], &s
, sizeof(s
), rfds
, &n
);
217 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
222 assert(n
== sizeof(s
));
223 PARA_DEBUG_LOG("next signal: %d\n", s
);
228 * Close the write end of the signal pipe, deallocate resources.
230 * \param st The pointer obtained earlier from signal_init_or_die().
232 void signal_shutdown(struct signal_task
*st
)
234 close(signal_pipe
[1]);