mp3: Add support for id3 version 2 tags.
[paraslash.git] / signal.c
1 /*
2 * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file signal.c Signal handling functions. */
7
8 #include <signal.h>
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "fd.h"
15
16 static int signal_pipe[2];
17
18 /**
19 * Initialize the paraslash signal subsystem.
20 *
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 para_install_sighandler() for each signal that should be caught.
25 *
26 * para_signal_init() installs a generic signal handler which is used for all
27 * signals simultaneously. When a signal arrives, this generic signal handler
28 * writes the corresponding signal number to the signal pipe so that the
29 * application can test for pending signals simply by checking the signal pipe
30 * for reading, e.g. by using the select(2) system call.
31 *
32 * \return This function either succeeds or calls exit(2) to terminate
33 * the current process. On success, the file descriptor of the signal pipe is
34 * returned.
35 */
36 int para_signal_init(void)
37 {
38 int ret;
39 if (pipe(signal_pipe) < 0) {
40 ret = -ERRNO_TO_PARA_ERROR(errno);
41 goto err_out;
42 }
43 ret = mark_fd_nonblocking(signal_pipe[0]);
44 if (ret < 0)
45 goto err_out;
46 ret = mark_fd_nonblocking(signal_pipe[1]);
47 if (ret < 0)
48 goto err_out;
49 return signal_pipe[0];
50 err_out:
51 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
52 exit(EXIT_FAILURE);
53 }
54
55 /*
56 * just write one integer to signal pipe
57 */
58 static void generic_signal_handler(int s)
59 {
60 write(signal_pipe[1], &s, sizeof(int));
61 //fprintf(stderr, "got sig %i, write returned %d\n", s, ret);
62 }
63
64 /**
65 * Reap one child.
66 *
67 * \param pid In case a child died, its pid is returned here.
68 *
69 * Call waitpid() and print a log message containing the pid and the cause of
70 * the child's death.
71 *
72 * \return A (negative) paraslash error code on errors, zero, if no child died,
73 * one otherwise. If and only if the function returns one, the content of \a
74 * pid is meaningful.
75 *
76 * \sa waitpid(2).
77 */
78 int para_reap_child(pid_t *pid)
79 {
80 int status;
81 *pid = waitpid(-1, &status, WNOHANG);
82
83 if (!*pid)
84 return 0;
85 if (*pid < 0)
86 return -ERRNO_TO_PARA_ERROR(errno);
87 if (WIFEXITED(status))
88 PARA_DEBUG_LOG("child %i exited. Exit status: %i\n", (int)*pid,
89 WEXITSTATUS(status));
90 else if (WIFSIGNALED(status))
91 PARA_DEBUG_LOG("child %i was killed by signal %i\n", (int)*pid,
92 WTERMSIG(status));
93 else
94 PARA_WARNING_LOG("child %i terminated abormally\n", (int)*pid);
95 return 1;
96 }
97
98 /**
99 * Paraslash's zombie killer.
100 *
101 * It just calls \p para_reap_child() until there are no more children left to
102 * reap.
103 */
104 void para_reap_children(void)
105 {
106 pid_t pid;
107
108 while (para_reap_child(&pid) > 0)
109 ; /* nothing */
110 }
111
112 /**
113 * Wrapper around signal(2).
114 *
115 * \param sig The number of the signal to catch.
116 *
117 * This installs the generic signal handler for the given signal.
118 *
119 * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
120 *
121 * \sa signal(2).
122 */
123 int para_install_sighandler(int sig)
124 {
125 PARA_DEBUG_LOG("catching signal %d\n", sig);
126 return signal(sig, &generic_signal_handler) == SIG_ERR? -E_SIGNAL_SIG_ERR : 1;
127 }
128
129 /**
130 * Return the number of next pending signal.
131 *
132 * This should be called if the fd for the signal pipe is ready for reading.
133 *
134 * \return On success, the number of the received signal is returned. If the
135 * read returned zero or was interrupted by another signal the function returns
136 * 0. Otherwise, a negative error value is returned.
137 */
138 int para_next_signal(void)
139 {
140 int s;
141 ssize_t r = read(signal_pipe[0], &s, sizeof(s));
142
143 if (!r) {
144 PARA_CRIT_LOG("read from signal pipe returned zero\n");
145 return 0;
146 }
147 if (r < 0) {
148 if (errno == EAGAIN || errno == EINTR)
149 return 0;
150 return -ERRNO_TO_PARA_ERROR(errno);
151 }
152 assert(r == sizeof(s));
153 PARA_DEBUG_LOG("next signal: %d\n", s);
154 return s;
155 }
156
157 /**
158 * Close the write end of the signal pipe.
159 */
160 void para_signal_shutdown(void)
161 {
162 close(signal_pipe[1]);
163 }