summaryrefslogtreecommitdiffstats
path: root/signals.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2007-11-07 10:15:14 +0000
committerRoy Marples <roy@marples.name>2007-11-07 10:15:14 +0000
commitde8a9d9887e862c9fefc9b6ff3520c572a57b924 (patch)
tree75d2b37e5be274da2240c96e5b0d0b42de1d7e86 /signals.c
parentcfe160119d801f24ee3e99cf2778b7475085c70d (diff)
downloaddhcpcd-de8a9d9887e862c9fefc9b6ff3520c572a57b924.tar.xz
Clear the signal_pipe fd from the fdset when all signals have
been read.
Diffstat (limited to 'signals.c')
-rw-r--r--signals.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/signals.c b/signals.c
index 214a6247..bb36cf6c 100644
--- a/signals.c
+++ b/signals.c
@@ -84,22 +84,39 @@ int signal_fd_set (fd_set *rfds, int extra_fd)
return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
}
+/* Check if we have a signal or not */
+int signal_exists (const fd_set *rfds)
+{
+ if (signal_signal || (rfds && FD_ISSET (signal_pipe[0], rfds)))
+ return 0;
+ return -1;
+}
+
/* Read a signal from the signal pipe. Returns 0 if there is
* no signal, -1 on error (and sets errno appropriately), and
* your signal on success */
-int signal_read (const fd_set *rfds)
+int signal_read (fd_set *rfds)
{
- int sig;
+ int sig = -1;
+
+ if (signal_signal) {
+ sig = signal_signal;
+ signal_signal = 0;
+ }
+
+ if (rfds && FD_ISSET (signal_pipe[0], rfds)) {
+ int buflen = sizeof (sig) * 2;
+ char buf[buflen];
+ size_t bytes;
- if (signal_signal)
- return signal_signal;
+ memset (buf, 0, buflen);
+ bytes = read (signal_pipe[0], buf, buflen);
- if (! rfds || ! FD_ISSET (signal_pipe[0], rfds))
- return -1;
-
- if (read (signal_pipe[0], &sig, sizeof (sig)) == -1)
- return -1;
+ /* We need to clear us from rfds if nothing left in the buffer
+ * in case we are called many times */
+ if (bytes == sizeof (sig))
+ FD_CLR (signal_pipe[0], rfds);
+ }
return sig;
}
-