From 61a48b0fb692ac9732426efdcad853523209f070 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 14 Feb 2015 15:33:02 -0500 Subject: [PATCH] Fix the rfkill waiting. --- src/ndhc.c | 15 +++++++++------ src/rfkill.c | 20 +++++++++++--------- src/rfkill.h | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/ndhc.c b/src/ndhc.c index f63016e..e9511e5 100644 --- a/src/ndhc.c +++ b/src/ndhc.c @@ -485,13 +485,13 @@ static void wait_for_rfkill() { cs.rfkill_set = 1; struct epoll_event events[2]; - int rfkfd = rfkill_open(&client_config.enable_rfkill); - if (rfkfd < 0) + cs.rfkillFd = rfkill_open(&client_config.enable_rfkill); + if (cs.rfkillFd < 0) suicide("can't wait for rfkill to end if /dev/rfkill can't be opened"); int epfd = epoll_create1(0); if (epfd < 0) suicide("epoll_create1 failed"); - epoll_add(epfd, rfkfd); + epoll_add(epfd, cs.rfkillFd); for (;;) { int r = epoll_wait(epfd, events, 2, -1); if (r < 0) { @@ -502,9 +502,9 @@ static void wait_for_rfkill() } for (int i = 0; i < r; ++i) { int fd = events[i].data.fd; - if (fd == rfkfd) { + if (fd == cs.rfkillFd) { if (events[i].events & EPOLLIN) { - if (!rfkill_wait_for_end(&cs, rfkfd)) + if (!rfkill_wait_for_end(&cs)) goto rfkill_gone; } } else @@ -513,7 +513,10 @@ static void wait_for_rfkill() } rfkill_gone: close(epfd); - close(rfkfd); + // We always close because ifchd and sockd shouldn't keep + // an rfkill fd open. + close(cs.rfkillFd); + cs.rfkillFd = -1; } int main(int argc, char *argv[]) diff --git a/src/rfkill.c b/src/rfkill.c index d4a3b0d..c2cec6e 100644 --- a/src/rfkill.c +++ b/src/rfkill.c @@ -26,6 +26,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -52,9 +53,10 @@ int rfkill_open(char enable_rfkill[static 1]) return r; } -static int rfkill_check(struct client_state_t cs[static 1], uint32_t rfkidx, - int (*rfenable)(struct client_state_t[static 1]), - int (*rfdisable)(struct client_state_t[static 1])) +static int rfkill_check(struct client_state_t cs[static 1], + int (*rfenable)(struct client_state_t[static 1]), + int (*rfdisable)(struct client_state_t[static 1]), + bool check_idx, uint32_t rfkidx) { struct rfkill_event event; ssize_t len = safe_read(cs->rfkillFd, (char *)&event, sizeof event); @@ -68,7 +70,7 @@ static int rfkill_check(struct client_state_t cs[static 1], uint32_t rfkidx, } log_line("rfkill: idx[%u] type[%u] op[%u] soft[%u] hard[%u]", event.idx, event.type, event.op, event.soft, event.hard); - if (event.idx != rfkidx) + if (check_idx && event.idx != rfkidx) return 0; if (event.op != RFKILL_OP_CHANGE && event.op != RFKILL_OP_CHANGE_ALL) return 0; @@ -128,13 +130,13 @@ static int rfkill_wait_for_end_disable(struct client_state_t cs[static 1]) int handle_rfkill_notice(struct client_state_t cs[static 1], uint32_t rfkidx) { - return rfkill_check(cs, rfkidx, handle_rfkill_notice_enable, - handle_rfkill_notice_disable); + return rfkill_check(cs, handle_rfkill_notice_enable, + handle_rfkill_notice_disable, true, rfkidx); } -int rfkill_wait_for_end(struct client_state_t cs[static 1], uint32_t rfkidx) +int rfkill_wait_for_end(struct client_state_t cs[static 1]) { - return rfkill_check(cs, rfkidx, rfkill_wait_for_end_enable, - rfkill_wait_for_end_disable); + return rfkill_check(cs, rfkill_wait_for_end_enable, + rfkill_wait_for_end_disable, false, 0); } diff --git a/src/rfkill.h b/src/rfkill.h index 96ab123..6fb05a8 100644 --- a/src/rfkill.h +++ b/src/rfkill.h @@ -30,7 +30,7 @@ int rfkill_open(char enable_rfkill[static 1]); int handle_rfkill_notice(struct client_state_t cs[static 1], uint32_t rfkidx); -int rfkill_wait_for_end(struct client_state_t cs[static 1], uint32_t rfkidx); +int rfkill_wait_for_end(struct client_state_t cs[static 1]); #endif