/**
* initialize the paraslash signal subsystem
*
- * This function creates a pipe, the signal pipe, to deliver pending signals to
- * the application. It should be called during the application's startup part,
- * followed by subsequent calls to para_install_sighandler() for each signal
- * that should be caught.
+ * This function creates a pipe, the signal pipe, to deliver pending
+ * signals to the application (Bernstein's trick). It should be called
+ * during the application's startup part, followed by subsequent calls
+ * to para_install_sighandler() for each signal that should be caught.
*
* para_signal_init() installs a generic signal handler which is used for all
* signals simultaneously. When a signal arrives, this generic signal handler
*/
int para_signal_init(void)
{
- if (!pipe(signal_pipe))
- return signal_pipe[0];
+ int i;
+ if (pipe(signal_pipe))
+ goto err_out;
+ for (i = 0; i < 2; i++) {
+ int fd = signal_pipe[i], flags = fcntl(fd, F_GETFL);
+ if (flags < 0)
+ goto err_out;
+ if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
+ goto err_out;
+ }
+ return signal_pipe[0];
+err_out:
PARA_EMERG_LOG("%s", "pipe error: Can not setup signal pipe");
exit(EXIT_FAILURE);
}