}
if (ioctl(fd, BIOCSETF, &pf) == -1)
goto eexit;
- close_on_exec(fd);
+ if (set_cloexec(fd) == -1)
+ goto eexit;
if (*fdp != -1)
close(*fdp);
*fdp = fd;
}
int
-close_on_exec(int fd)
+set_cloexec(int fd)
{
int flags;
return 0;
}
+int
+set_nonblock(int fd)
+{
+ int flags;
+
+ if ((flags = fcntl(fd, F_GETFL, 0)) == -1
+ || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
+ {
+ logger(LOG_ERR, "fcntl: %s", strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
/* Handy function to get the time.
* We only care about time advancements, not the actual time itself
* Which is why we use CLOCK_MONOTONIC, but it is not available on all
#endif
int close_fds(void);
-int close_on_exec(int);
+int set_cloexec(int);
+int set_nonblock(int);
int fd_hasdata(int);
ssize_t get_line(char **, size_t *, FILE *);
int get_time(struct timeval *);
goto abort;
}
- close_on_exec(pid_fd);
+ if (set_cloexec(pid_fd) == -1)
+ goto abort;
writepid(pid_fd, getpid());
logger(LOG_INFO, PACKAGE " " VERSION " starting");
}
}
if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0)
goto eexit;
- if ((flags = fcntl(s, F_GETFL, 0)) == -1
- || fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
+ if (set_cloexec(s) == -1)
goto eexit;
- if (bind(s, &su.sa, sizeof(su)) == -1)
+ if (set_nonblock(s) == -1)
goto eexit;
- if (close_on_exec(s) == -1)
+ if (bind(s, &su.sa, sizeof(su)) == -1)
goto eexit;
#ifdef ENABLE_ARP
if (protocol == ETHERTYPE_ARP)
goto eexit;
iface->udp_fd = s;
- close_on_exec(s);
+ set_cloexec(s);
return 0;
eexit:
#include "signals.h"
static int signal_pipe[2];
-static int signals[5];
static const int handle_sigs[] = {
SIGHUP,
static void
signal_handler(int sig)
{
- unsigned int i = 0;
int serrno = errno;
- /* Add a signal to our stack */
- while (signals[i])
- i++;
- if (i <= sizeof(signals) / sizeof(signals[0]))
- signals[i] = sig;
-
write(signal_pipe[1], &sig, sizeof(sig));
-
/* Restore errno */
errno = serrno;
}
int
signal_exists(int fd)
{
- if (signals[0] || fd_hasdata(fd) == 1)
+ if (fd_hasdata(fd) == 1)
return 0;
return -1;
}
signal_read(int fd)
{
int sig = -1;
- unsigned int i = 0;
char buf[16];
size_t bytes;
- /* Pop a signal off the our stack */
- if (signals[0]) {
- sig = signals[0];
- while (i < (sizeof(signals) / sizeof(signals[0])) - 1) {
- signals[i] = signals[i + 1];
- if (!signals[++i])
- break;
- }
- }
-
if (fd_hasdata(fd) == 1) {
memset(buf, 0, sizeof(buf));
bytes = read(signal_pipe[0], buf, sizeof(buf));
{
if (pipe(signal_pipe) == -1)
return -1;
-
+ /* Don't block on read */
+ if (set_nonblock(signal_pipe[0]) == -1)
+ return -1;
/* Stop any scripts from inheriting us */
- close_on_exec(signal_pipe[0]);
- close_on_exec(signal_pipe[1]);
-
- memset(signals, 0, sizeof(signals));
+ if (set_cloexec(signal_pipe[0]) == -1)
+ return -1;
+ if (set_cloexec(signal_pipe[1]) == -1)
+ return -1;
return 0;
}