Use a unified epoll_(add|del)().

Also, background() and setup_signals_ndhc() are moved from sys.c to ndhc.c.

background() also no longer attempts to re-configure signals; signals are
always set up on initial ndhc initialization.
This commit is contained in:
Nicholas J. Kain 2014-03-12 16:51:10 -04:00
parent 94a923b00a
commit 84a68001d3
8 changed files with 61 additions and 88 deletions

View File

@ -43,6 +43,7 @@
#include "state.h" #include "state.h"
#include "dhcp.h" #include "dhcp.h"
#include "sys.h" #include "sys.h"
#include "ndhc.h"
#include "ifchange.h" #include "ifchange.h"
#include "options.h" #include "options.h"
#include "leasefile.h" #include "leasefile.h"
@ -234,7 +235,7 @@ static int arp_open_fd(struct client_state_t *cs)
} }
cs->arpFd = fd; cs->arpFd = fd;
epoll_add(cs, fd); epoll_add(cs->epollFd, fd);
arpreply_clear(); arpreply_clear();
return 0; return 0;
out_fd: out_fd:
@ -274,7 +275,7 @@ static int arp_min_close_fd(struct client_state_t *cs)
{ {
if (cs->arpFd == -1) if (cs->arpFd == -1)
return 0; return 0;
epoll_del(cs, cs->arpFd); epoll_del(cs->epollFd, cs->arpFd);
close(cs->arpFd); close(cs->arpFd);
cs->arpFd = -1; cs->arpFd = -1;
arpState = AS_NONE; arpState = AS_NONE;
@ -498,7 +499,7 @@ void arp_success(struct client_state_t *cs)
if (client_config.quit_after_lease) if (client_config.quit_after_lease)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
if (!client_config.foreground) if (!client_config.foreground)
background(cs); background();
} }
static void arp_gw_success(struct client_state_t *cs) static void arp_gw_success(struct client_state_t *cs)

View File

@ -465,7 +465,7 @@ static void change_listen_mode(struct client_state_t *cs, int new_mode)
{ {
cs->listenMode = new_mode; cs->listenMode = new_mode;
if (cs->listenFd >= 0) { if (cs->listenFd >= 0) {
epoll_del(cs, cs->listenFd); epoll_del(cs->epollFd, cs->listenFd);
close(cs->listenFd); close(cs->listenFd);
cs->listenFd = -1; cs->listenFd = -1;
} }
@ -478,7 +478,7 @@ static void change_listen_mode(struct client_state_t *cs, int new_mode)
log_error("FATAL: Couldn't listen on socket: %s.", strerror(errno)); log_error("FATAL: Couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
epoll_add(cs, cs->listenFd); epoll_add(cs->epollFd, cs->listenFd);
} }
void set_listen_raw(struct client_state_t *cs) void set_listen_raw(struct client_state_t *cs)

View File

@ -56,6 +56,7 @@
#include "strl.h" #include "strl.h"
#include "cap.h" #include "cap.h"
#include "io.h" #include "io.h"
#include "sys.h"
#include "ifset.h" #include "ifset.h"
#include "seccomp.h" #include "seccomp.h"
@ -81,29 +82,6 @@ static void writeordie(int fd, const char *buf, int len)
suicide("write returned error"); suicide("write returned error");
} }
static void ep_add(int fd)
{
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd;
int r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
if (r == -1)
suicide("%s failed: %s", __func__, strerror(errno));
}
// XXX: Remove dead code.
#if 0
static void epoll_del(int fd)
{
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd;
int r = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
if (r == -1)
suicide("epoll_del failed %s", strerror(errno));
}
#endif
/* Writes a new resolv.conf based on the information we have received. */ /* Writes a new resolv.conf based on the information we have received. */
static void write_resolve_conf(void) static void write_resolve_conf(void)
{ {
@ -358,8 +336,8 @@ void do_ifch_work(void)
ifchd_client_init(); ifchd_client_init();
ep_add(pToIfchR); epoll_add(epollfd, pToIfchR);
ep_add(signalFd); epoll_add(epollfd, signalFd);
for (;;) { for (;;) {
int r = epoll_wait(epollfd, events, 2, -1); int r = epoll_wait(epollfd, events, 2, -1);

View File

@ -49,6 +49,7 @@
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
#include "ndhc.h"
#include "ndhc-defines.h" #include "ndhc-defines.h"
#include "config.h" #include "config.h"
#include "state.h" #include "state.h"
@ -124,6 +125,27 @@ static void show_usage(void)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
static void setup_signals_ndhc(void)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGPIPE);
sigaddset(&mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
suicide("sigprocmask failed");
if (cs.signalFd >= 0) {
epoll_del(cs.epollFd, cs.signalFd);
close(cs.signalFd);
}
cs.signalFd = signalfd(-1, &mask, SFD_NONBLOCK);
if (cs.signalFd < 0)
suicide("signalfd failed");
epoll_add(cs.epollFd, cs.signalFd);
}
static void signal_dispatch(void) static void signal_dispatch(void)
{ {
int t; int t;
@ -203,9 +225,9 @@ static void do_ndhc_work(void)
if (enforce_seccomp_ndhc()) if (enforce_seccomp_ndhc())
log_line("ndhc seccomp filter cannot be installed"); log_line("ndhc seccomp filter cannot be installed");
setup_signals_ndhc(&cs); setup_signals_ndhc();
epoll_add(&cs, cs.nlFd); epoll_add(cs.epollFd, cs.nlFd);
set_listen_raw(&cs); set_listen_raw(&cs);
nowts = curms(); nowts = curms();
goto jumpstart; goto jumpstart;
@ -262,6 +284,7 @@ jumpstart:
char chroot_dir[MAX_PATH_LENGTH] = ""; char chroot_dir[MAX_PATH_LENGTH] = "";
char resolv_conf_d[MAX_PATH_LENGTH] = ""; char resolv_conf_d[MAX_PATH_LENGTH] = "";
static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
static uid_t ndhc_uid = 0; static uid_t ndhc_uid = 0;
static gid_t ndhc_gid = 0; static gid_t ndhc_gid = 0;
int pToNdhcR; int pToNdhcR;
@ -317,6 +340,22 @@ static void ndhc_main(void) {
do_ndhc_work(); do_ndhc_work();
} }
void background(void)
{
static char called;
if (!called) {
called = 1; // Do not fork again.
if (daemon(0, 0) == -1) {
perror("fork");
exit(EXIT_SUCCESS);
}
}
if (file_exists(pidfile, "w") == -1) {
log_line("Cannot open pidfile for write!");
} else
write_pid(pidfile);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
static const struct option arg_options[] = { static const struct option arg_options[] = {

View File

@ -37,4 +37,6 @@ extern int pToNdhcW;
extern char chroot_dir[MAX_PATH_LENGTH]; extern char chroot_dir[MAX_PATH_LENGTH];
extern char resolv_conf_d[MAX_PATH_LENGTH]; extern char resolv_conf_d[MAX_PATH_LENGTH];
extern void background(void);
#endif /* NJK_NDHC_NDHC_H_ */ #endif /* NJK_NDHC_NDHC_H_ */

View File

@ -36,6 +36,7 @@
#include "arp.h" #include "arp.h"
#include "options.h" #include "options.h"
#include "log.h" #include "log.h"
#include "ndhc.h"
#include "sys.h" #include "sys.h"
#include "random.h" #include "random.h"
@ -299,7 +300,7 @@ static void selecting_timeout(struct client_state_t *cs, long long nowts)
if (client_config.background_if_no_lease) { if (client_config.background_if_no_lease) {
log_line("No lease, going to background."); log_line("No lease, going to background.");
cs->init = 0; cs->init = 0;
background(cs); background();
} else if (client_config.abort_if_no_lease) { } else if (client_config.abort_if_no_lease) {
log_line("No lease, failing."); log_line("No lease, failing.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -31,74 +31,30 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include "sys.h"
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
#include "pidfile.h"
#include "sys.h"
char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT; void epoll_add(int epfd, int fd)
void setup_signals_ndhc(struct client_state_t *cs)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR1);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGPIPE);
sigaddset(&mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
suicide("sigprocmask failed");
if (cs->signalFd >= 0) {
epoll_del(cs, cs->signalFd);
close(cs->signalFd);
}
cs->signalFd = signalfd(-1, &mask, SFD_NONBLOCK);
if (cs->signalFd < 0)
suicide("signalfd failed");
epoll_add(cs, cs->signalFd);
}
// @cs can be NULL
void background(struct client_state_t *cs)
{
static char called;
if (!called) {
called = 1; // Do not fork again.
if (daemon(0, 0) == -1) {
perror("fork");
exit(EXIT_SUCCESS);
}
if (cs)
setup_signals_ndhc(cs);
}
if (file_exists(pidfile, "w") == -1) {
log_line("Cannot open pidfile for write!");
} else
write_pid(pidfile);
}
void epoll_add(struct client_state_t *cs, int fd)
{ {
struct epoll_event ev; struct epoll_event ev;
int r; int r;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd; ev.data.fd = fd;
r = epoll_ctl(cs->epollFd, EPOLL_CTL_ADD, fd, &ev); r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (r == -1) if (r == -1)
suicide("epoll_add failed %s", strerror(errno)); suicide("epoll_add failed %s", strerror(errno));
} }
void epoll_del(struct client_state_t *cs, int fd) void epoll_del(int epfd, int fd)
{ {
struct epoll_event ev; struct epoll_event ev;
int r; int r;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd; ev.data.fd = fd;
r = epoll_ctl(cs->epollFd, EPOLL_CTL_DEL, fd, &ev); r = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);
if (r == -1) if (r == -1)
suicide("epoll_del failed %s", strerror(errno)); suicide("epoll_del failed %s", strerror(errno));
} }

View File

@ -39,11 +39,7 @@ static inline unsigned long long curms()
return ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000ULL; return ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000ULL;
} }
extern char pidfile[MAX_PATH_LENGTH]; extern void epoll_add(int epfd, int fd);
extern void epoll_del(int epfd, int fd);
void setup_signals_ndhc(struct client_state_t *cs);
void background(struct client_state_t *cs);
void epoll_add(struct client_state_t *cs, int fd);
void epoll_del(struct client_state_t *cs, int fd);
#endif /* SYS_H_ */ #endif /* SYS_H_ */