From: benno Date: Tue, 27 Jun 2017 20:46:34 +0000 (+0000) Subject: Hoist some privileged code in preparation for future work. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3c0535b072eb1b319b26e6e4b019b69e99358d70;p=openbsd Hoist some privileged code in preparation for future work. Based on an approach in vmd with mc146818/ns8250. diff by Rob Pierce ok deraadt@ --- diff --git a/usr.sbin/ifstated/ifstated.c b/usr.sbin/ifstated/ifstated.c index 371c5eec9ba..f67e0d708ae 100644 --- a/usr.sbin/ifstated/ifstated.c +++ b/usr.sbin/ifstated/ifstated.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.c,v 1.42 2017/06/18 12:03:47 benno Exp $ */ +/* $OpenBSD: ifstated.c,v 1.43 2017/06/27 20:46:34 benno Exp $ */ /* * Copyright (c) 2004 Marco Pfatschbacher @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -87,8 +88,9 @@ int main(int argc, char *argv[]) { struct timeval tv; - int ch; + int ch, rt_fd; int debug = 0; + unsigned int rtfilter; log_init(1, LOG_DAEMON); /* log to stderr until daemonized */ log_setverbose(1); @@ -144,12 +146,25 @@ main(int argc, char *argv[]) log_init(debug, LOG_DAEMON); log_setverbose(opts & IFSD_OPT_VERBOSE); + if ((rt_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) + err(1, "no routing socket"); + + rtfilter = ROUTE_FILTER(RTM_IFINFO); + if (setsockopt(rt_fd, PF_ROUTE, ROUTE_MSGFILTER, + &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ + log_warn("%s: setsockopt msgfilter", __func__); + + rtfilter = RTABLE_ANY; + if (setsockopt(rt_fd, PF_ROUTE, ROUTE_TABLEFILTER, + &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ + log_warn("%s: setsockopt tablefilter", __func__); + signal_set(&sigchld_ev, SIGCHLD, sigchld_handler, NULL); signal_add(&sigchld_ev, NULL); /* Loading the config needs to happen in the event loop */ timerclear(&tv); - evtimer_set(&startup_ev, startup_handler, NULL); + evtimer_set(&startup_ev, startup_handler, (void *)(long)rt_fd); evtimer_add(&startup_ev, &tv); event_loop(0); @@ -159,28 +174,14 @@ main(int argc, char *argv[]) void startup_handler(int fd, short event, void *arg) { - int rt_fd; - unsigned int rtfilter; - - if ((rt_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) - err(1, "no routing socket"); + int rfd = (int)(long)arg; if (load_config() != 0) { log_warnx("unable to load config"); exit(1); } - - rtfilter = ROUTE_FILTER(RTM_IFINFO); - if (setsockopt(rt_fd, PF_ROUTE, ROUTE_MSGFILTER, - &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ - log_warn("%s: setsockopt msgfilter", __func__); - - rtfilter = RTABLE_ANY; - if (setsockopt(rt_fd, PF_ROUTE, ROUTE_TABLEFILTER, - &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */ - log_warn("%s: setsockopt tablefilter", __func__); - event_set(&rt_msg_ev, rt_fd, EV_READ|EV_PERSIST, rt_msg_handler, NULL); + event_set(&rt_msg_ev, rfd, EV_READ|EV_PERSIST, rt_msg_handler, NULL); event_add(&rt_msg_ev, NULL); signal_set(&sighup_ev, SIGHUP, sighup_handler, NULL);