]> git.tuebingen.mpg.de Git - dss.git/blob - sig.c
9694684045145105001e7410b06558ee73bbfd39
[dss.git] / sig.c
1 /*
2  * Copyright (C) 2004-2010 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 #include <string.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <dirent.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <signal.h>
17 #include <stdlib.h>
18 #include <sys/select.h>
19
20
21 #include "gcc-compat.h"
22 #include "err.h"
23 #include "log.h"
24 #include "str.h"
25 #include "file.h"
26
27 static int signal_pipe[2];
28
29 /**
30  * Initialize the signal subsystem.
31  *
32  * This function creates a pipe, the signal pipe, to deliver pending signals to
33  * the application (Bernstein's trick). It should be called during the
34  * application's startup part, followed by subsequent calls to
35  * install_sighandler() for each signal that should be caught.
36  *
37  * signal_init() installs a generic signal handler which is used for all
38  * signals simultaneously. When a signal arrives, this generic signal handler
39  * writes the corresponding signal number to the signal pipe so that the
40  * application can test for pending signals simply by checking the signal pipe
41  * for reading, e.g. by using the select(2) system call.
42  *
43  * \return This function either succeeds or calls exit(2) to terminate
44  * the current process. On success, the file descriptor of the signal pipe is
45  * returned.
46  */
47 int signal_init(void)
48 {
49         int ret;
50         if (pipe(signal_pipe) < 0) {
51                 ret = -ERRNO_TO_DSS_ERROR(errno);
52                 goto err_out;
53         }
54         ret = mark_fd_nonblocking(signal_pipe[0]);
55         if (ret < 0)
56                 goto err_out;
57         ret = mark_fd_nonblocking(signal_pipe[1]);
58         if (ret < 0)
59                 goto err_out;
60         return signal_pipe[0];
61 err_out:
62         DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
63         exit(EXIT_FAILURE);
64 }
65
66 /*
67  * just write one integer to signal pipe
68  */
69 static void generic_signal_handler(int s)
70 {
71         write(signal_pipe[1], &s, sizeof(int));
72 }
73
74 /**
75  * Reap one child.
76  *
77  * \param pid In case a child died, its pid is returned here.
78  *
79  * Call waitpid() and print a log message containing the pid and the cause of
80  * the child's death.
81  *
82  * \return A (negative) error code on errors, zero, if no child died, one
83  * otherwise. If and only if the function returns one, the content of \a pid is
84  * meaningful.
85  *
86  * \sa waitpid(2)
87  */
88 int reap_child(pid_t *pid, int *status)
89 {
90         *pid = waitpid(-1, status, WNOHANG);
91
92         if (!*pid)
93                 return 0;
94         if (*pid < 0)
95                 return -ERRNO_TO_DSS_ERROR(errno);
96         if (WIFEXITED(*status))
97                 DSS_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
98                         WEXITSTATUS(*status));
99         else if (WIFSIGNALED(*status))
100                 DSS_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
101                         WTERMSIG(*status));
102         else
103                 DSS_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
104         return 1;
105 }
106
107 /**
108  * Wrapper around signal(2)
109  *
110  * \param sig The number of the signal to catch.
111  *
112  * This installs the generic signal handler for the given signal.
113  *
114  * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
115  * \sa signal(2)
116  */
117 int install_sighandler(int sig)
118 {
119         DSS_DEBUG_LOG("catching signal %d\n", sig);
120         if (signal(sig, &generic_signal_handler) != SIG_ERR)
121                 return 1;
122         return -E_SIGNAL_SIG_ERR;
123 }
124
125 /**
126  * Return number of next pending signal.
127  *
128  * This should be called if the fd for the signal pipe is ready for reading.
129  *
130  * \return On success, the number of the received signal is returned.
131  * If the read was interrupted by another signal the function returns 0.
132  * Otherwise a negative error code is returned.
133  */
134 int next_signal(void)
135 {
136         int s, err;
137         ssize_t r;
138
139         r = read(signal_pipe[0], &s, sizeof(s));
140         if (r == sizeof(s)) {
141                 DSS_DEBUG_LOG("next signal: %d\n", s);
142                 return s;
143         }
144         err = errno;
145         assert(r < 0);
146         if (err == EAGAIN)
147                 return 0;
148         DSS_ERROR_LOG("failed to read from signal pipe\n");
149         return -ERRNO_TO_DSS_ERROR(err);
150 }
151
152 /**
153  * Close the signal pipe.
154  */
155 void signal_shutdown(void)
156 {
157         close(signal_pipe[1]);
158 }