Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Maxim Kryzhanovsky | 2004fa1 | 2010-03-30 15:49:57 +0200 | [diff] [blame] | 3 | * ifplugd for busybox, based on ifplugd 0.28 (written by Lennart Poettering). |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 4 | * |
Denis Vlasenko | f4e4563 | 2009-04-26 01:17:44 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 Maksym Kryzhanovskyy <xmaks@email.cz> |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 8 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 9 | |
| 10 | //usage:#define ifplugd_trivial_usage |
| 11 | //usage: "[OPTIONS]" |
| 12 | //usage:#define ifplugd_full_usage "\n\n" |
| 13 | //usage: "Network interface plug detection daemon\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | //usage: "\n -n Don't daemonize" |
| 15 | //usage: "\n -s Don't log to syslog" |
| 16 | //usage: "\n -i IFACE Interface" |
| 17 | //usage: "\n -f/-F Treat link detection error as link down/link up" |
| 18 | //usage: "\n (otherwise exit on error)" |
| 19 | //usage: "\n -a Don't up interface at each link probe" |
| 20 | //usage: "\n -M Monitor creation/destruction of interface" |
| 21 | //usage: "\n (otherwise it must exist)" |
| 22 | //usage: "\n -r PROG Script to run" |
| 23 | //usage: "\n -x ARG Extra argument for script" |
| 24 | //usage: "\n -I Don't exit on nonzero exit code from script" |
Denys Vlasenko | 21f620f | 2012-06-03 10:26:16 +0200 | [diff] [blame] | 25 | //usage: "\n -p Don't run \"up\" script on startup" |
| 26 | //usage: "\n -q Don't run \"down\" script on exit" |
| 27 | //usage: "\n -l Always run script on startup" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage: "\n -t SECS Poll time in seconds" |
| 29 | //usage: "\n -u SECS Delay before running script after link up" |
| 30 | //usage: "\n -d SECS Delay after link down" |
| 31 | //usage: "\n -m MODE API mode (mii, priv, ethtool, wlan, iff, auto)" |
| 32 | //usage: "\n -k Kill running daemon" |
| 33 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
| 35 | |
Denys Vlasenko | 0568b6e | 2009-08-08 03:20:12 +0200 | [diff] [blame] | 36 | #include "fix_u32.h" |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 37 | #include <linux/if.h> |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 38 | #include <linux/mii.h> |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 39 | #include <linux/ethtool.h> |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 40 | #ifdef HAVE_NET_ETHERNET_H |
Denys Vlasenko | 5fa6d1a | 2015-10-05 11:15:43 +0200 | [diff] [blame] | 41 | /* musl breakage: |
| 42 | * In file included from /usr/include/net/ethernet.h:10, |
| 43 | * from networking/ifplugd.c:41: |
| 44 | * /usr/include/netinet/if_ether.h:96: error: redefinition of 'struct ethhdr' |
| 45 | * |
| 46 | * Build succeeds without it on musl. Commented it out. |
| 47 | * If on your system you need it, consider removing <linux/ethtool.h> |
| 48 | * and copy-pasting its definitions here (<linux/ethtool.h> is what pulls in |
| 49 | * conflicting definition of struct ethhdr on musl). |
| 50 | */ |
| 51 | /* # include <net/ethernet.h> */ |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 52 | #endif |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 53 | #include <linux/netlink.h> |
| 54 | #include <linux/rtnetlink.h> |
| 55 | #include <linux/sockios.h> |
| 56 | #include <syslog.h> |
| 57 | |
| 58 | #define __user |
| 59 | #include <linux/wireless.h> |
| 60 | |
Denis Vlasenko | f4e4563 | 2009-04-26 01:17:44 +0000 | [diff] [blame] | 61 | /* |
Maxim Kryzhanovsky | 2004fa1 | 2010-03-30 15:49:57 +0200 | [diff] [blame] | 62 | From initial port to busybox, removed most of the redundancy by |
| 63 | converting implementation of a polymorphic interface to the strict |
| 64 | functional style. The main role is run a script when link state |
| 65 | changed, other activities like audio signal or detailed reports |
| 66 | are on the script itself. |
Denis Vlasenko | f4e4563 | 2009-04-26 01:17:44 +0000 | [diff] [blame] | 67 | |
| 68 | One questionable point of the design is netlink usage: |
| 69 | |
| 70 | We have 1 second timeout by default to poll the link status, |
| 71 | it is short enough so that there are no real benefits in |
| 72 | using netlink to get "instantaneous" interface creation/deletion |
| 73 | notifications. We can check for interface existence by just |
| 74 | doing some fast ioctl using its name. |
| 75 | |
| 76 | Netlink code then can be just dropped (1k or more?) |
| 77 | */ |
| 78 | |
| 79 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 80 | #define IFPLUGD_ENV_PREVIOUS "IFPLUGD_PREVIOUS" |
| 81 | #define IFPLUGD_ENV_CURRENT "IFPLUGD_CURRENT" |
| 82 | |
| 83 | enum { |
| 84 | FLAG_NO_AUTO = 1 << 0, // -a, Do not enable interface automatically |
| 85 | FLAG_NO_DAEMON = 1 << 1, // -n, Do not daemonize |
| 86 | FLAG_NO_SYSLOG = 1 << 2, // -s, Do not use syslog, use stderr instead |
| 87 | FLAG_IGNORE_FAIL = 1 << 3, // -f, Ignore detection failure, retry instead (failure is treated as DOWN) |
| 88 | FLAG_IGNORE_FAIL_POSITIVE = 1 << 4, // -F, Ignore detection failure, retry instead (failure is treated as UP) |
| 89 | FLAG_IFACE = 1 << 5, // -i, Specify ethernet interface |
| 90 | FLAG_RUN = 1 << 6, // -r, Specify program to execute |
| 91 | FLAG_IGNORE_RETVAL = 1 << 7, // -I, Don't exit on nonzero return value of program executed |
| 92 | FLAG_POLL_TIME = 1 << 8, // -t, Specify poll time in seconds |
| 93 | FLAG_DELAY_UP = 1 << 9, // -u, Specify delay for configuring interface |
| 94 | FLAG_DELAY_DOWN = 1 << 10, // -d, Specify delay for deconfiguring interface |
| 95 | FLAG_API_MODE = 1 << 11, // -m, Force API mode (mii, priv, ethtool, wlan, auto) |
| 96 | FLAG_NO_STARTUP = 1 << 12, // -p, Don't run script on daemon startup |
| 97 | FLAG_NO_SHUTDOWN = 1 << 13, // -q, Don't run script on daemon quit |
| 98 | FLAG_INITIAL_DOWN = 1 << 14, // -l, Run "down" script on startup if no cable is detected |
| 99 | FLAG_EXTRA_ARG = 1 << 15, // -x, Specify an extra argument for action script |
| 100 | FLAG_MONITOR = 1 << 16, // -M, Use interface monitoring |
| 101 | #if ENABLE_FEATURE_PIDFILE |
| 102 | FLAG_KILL = 1 << 17, // -k, Kill a running daemon |
| 103 | #endif |
| 104 | }; |
| 105 | #if ENABLE_FEATURE_PIDFILE |
| 106 | # define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:Mk" |
| 107 | #else |
| 108 | # define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:M" |
| 109 | #endif |
| 110 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 111 | enum { // interface status |
| 112 | IFSTATUS_ERR = -1, |
| 113 | IFSTATUS_DOWN = 0, |
| 114 | IFSTATUS_UP = 1, |
| 115 | }; |
| 116 | |
| 117 | enum { // constant fds |
| 118 | ioctl_fd = 3, |
| 119 | netlink_fd = 4, |
| 120 | }; |
| 121 | |
| 122 | struct globals { |
| 123 | smallint iface_last_status; |
Denys Vlasenko | 19afe84 | 2010-05-08 23:26:16 +0200 | [diff] [blame] | 124 | smallint iface_prev_status; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 125 | smallint iface_exists; |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 126 | smallint api_method_num; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 127 | |
| 128 | /* Used in getopt32, must have sizeof == sizeof(int) */ |
| 129 | unsigned poll_time; |
| 130 | unsigned delay_up; |
| 131 | unsigned delay_down; |
| 132 | |
| 133 | const char *iface; |
| 134 | const char *api_mode; |
| 135 | const char *script_name; |
| 136 | const char *extra_arg; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 137 | }; |
| 138 | #define G (*ptr_to_globals) |
| 139 | #define INIT_G() do { \ |
| 140 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 141 | G.iface_last_status = -1; \ |
| 142 | G.iface_exists = 1; \ |
| 143 | G.poll_time = 1; \ |
| 144 | G.delay_down = 5; \ |
| 145 | G.iface = "eth0"; \ |
| 146 | G.api_mode = "a"; \ |
| 147 | G.script_name = "/etc/ifplugd/ifplugd.action"; \ |
| 148 | } while (0) |
| 149 | |
| 150 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 151 | /* Utility routines */ |
| 152 | |
| 153 | static void set_ifreq_to_ifname(struct ifreq *ifreq) |
Denys Vlasenko | 19afe84 | 2010-05-08 23:26:16 +0200 | [diff] [blame] | 154 | { |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 155 | memset(ifreq, 0, sizeof(struct ifreq)); |
| 156 | strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 159 | static int network_ioctl(int request, void* data, const char *errmsg) |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 160 | { |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 161 | int r = ioctl(ioctl_fd, request, data); |
| 162 | if (r < 0 && errmsg) |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 163 | bb_perror_msg("%s failed", errmsg); |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 164 | return r; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 167 | /* Link detection routines and table */ |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 168 | |
| 169 | static smallint detect_link_mii(void) |
| 170 | { |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 171 | /* char buffer instead of bona-fide struct avoids aliasing warning */ |
| 172 | char buf[sizeof(struct ifreq)]; |
Denys Vlasenko | b3b6c8b | 2011-01-20 11:29:00 +0100 | [diff] [blame] | 173 | struct ifreq *const ifreq = (void *)buf; |
| 174 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 175 | struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 176 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 177 | set_ifreq_to_ifname(ifreq); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 178 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 179 | if (network_ioctl(SIOCGMIIPHY, ifreq, "SIOCGMIIPHY") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 180 | return IFSTATUS_ERR; |
| 181 | } |
| 182 | |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 183 | mii->reg_num = 1; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 184 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 185 | if (network_ioctl(SIOCGMIIREG, ifreq, "SIOCGMIIREG") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 186 | return IFSTATUS_ERR; |
| 187 | } |
| 188 | |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 189 | return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static smallint detect_link_priv(void) |
| 193 | { |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 194 | /* char buffer instead of bona-fide struct avoids aliasing warning */ |
| 195 | char buf[sizeof(struct ifreq)]; |
Denys Vlasenko | b3b6c8b | 2011-01-20 11:29:00 +0100 | [diff] [blame] | 196 | struct ifreq *const ifreq = (void *)buf; |
| 197 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 198 | struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 199 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 200 | set_ifreq_to_ifname(ifreq); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 201 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 202 | if (network_ioctl(SIOCDEVPRIVATE, ifreq, "SIOCDEVPRIVATE") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 203 | return IFSTATUS_ERR; |
| 204 | } |
| 205 | |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 206 | mii->reg_num = 1; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 207 | |
Denys Vlasenko | 53f30b4 | 2011-01-20 01:20:36 +0100 | [diff] [blame] | 208 | if (network_ioctl(SIOCDEVPRIVATE+1, ifreq, "SIOCDEVPRIVATE+1") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 209 | return IFSTATUS_ERR; |
| 210 | } |
| 211 | |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 212 | return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static smallint detect_link_ethtool(void) |
| 216 | { |
| 217 | struct ifreq ifreq; |
| 218 | struct ethtool_value edata; |
| 219 | |
| 220 | set_ifreq_to_ifname(&ifreq); |
| 221 | |
| 222 | edata.cmd = ETHTOOL_GLINK; |
Denys Vlasenko | 337a31b | 2009-10-23 18:31:02 +0200 | [diff] [blame] | 223 | ifreq.ifr_data = (void*) &edata; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 224 | |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 225 | if (network_ioctl(SIOCETHTOOL, &ifreq, "ETHTOOL_GLINK") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 226 | return IFSTATUS_ERR; |
| 227 | } |
| 228 | |
| 229 | return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN; |
| 230 | } |
| 231 | |
| 232 | static smallint detect_link_iff(void) |
| 233 | { |
| 234 | struct ifreq ifreq; |
| 235 | |
| 236 | set_ifreq_to_ifname(&ifreq); |
| 237 | |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 238 | if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 239 | return IFSTATUS_ERR; |
| 240 | } |
| 241 | |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 242 | /* If IFF_UP is not set (interface is down), IFF_RUNNING is never set |
| 243 | * regardless of link status. Simply continue to report last status - |
| 244 | * no point in reporting spurious link downs if interface is disabled |
| 245 | * by admin. When/if it will be brought up, |
| 246 | * we'll report real link status. |
| 247 | */ |
| 248 | if (!(ifreq.ifr_flags & IFF_UP) && G.iface_last_status != IFSTATUS_ERR) |
| 249 | return G.iface_last_status; |
| 250 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 251 | return (ifreq.ifr_flags & IFF_RUNNING) ? IFSTATUS_UP : IFSTATUS_DOWN; |
| 252 | } |
| 253 | |
| 254 | static smallint detect_link_wlan(void) |
| 255 | { |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 256 | int i; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 257 | struct iwreq iwrequest; |
| 258 | uint8_t mac[ETH_ALEN]; |
| 259 | |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 260 | memset(&iwrequest, 0, sizeof(iwrequest)); |
Denis Vlasenko | 80e57eb | 2009-04-26 01:43:36 +0000 | [diff] [blame] | 261 | strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 262 | |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 263 | if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 264 | return IFSTATUS_ERR; |
| 265 | } |
| 266 | |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 267 | memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 268 | |
| 269 | if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) { |
Maxim Kryzhanovsky | fcb84c8 | 2010-03-29 09:09:05 +0200 | [diff] [blame] | 270 | for (i = 1; i < ETH_ALEN; ++i) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 271 | if (mac[i] != mac[0]) |
| 272 | return IFSTATUS_UP; |
| 273 | } |
| 274 | return IFSTATUS_DOWN; |
| 275 | } |
| 276 | |
| 277 | return IFSTATUS_UP; |
| 278 | } |
| 279 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 280 | enum { // api mode |
| 281 | API_ETHTOOL, // 'e' |
| 282 | API_MII, // 'm' |
| 283 | API_PRIVATE, // 'p' |
| 284 | API_WLAN, // 'w' |
| 285 | API_IFF, // 'i' |
| 286 | API_AUTO, // 'a' |
| 287 | }; |
| 288 | |
| 289 | static const char api_modes[] ALIGN1 = "empwia"; |
| 290 | |
| 291 | static const struct { |
| 292 | const char *name; |
| 293 | smallint (*func)(void); |
| 294 | } method_table[] = { |
| 295 | { "SIOCETHTOOL" , &detect_link_ethtool }, |
| 296 | { "SIOCGMIIPHY" , &detect_link_mii }, |
| 297 | { "SIOCDEVPRIVATE" , &detect_link_priv }, |
| 298 | { "wireless extension", &detect_link_wlan }, |
| 299 | { "IFF_RUNNING" , &detect_link_iff }, |
| 300 | }; |
| 301 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 302 | static const char *strstatus(int status) |
| 303 | { |
| 304 | if (status == IFSTATUS_ERR) |
| 305 | return "error"; |
| 306 | return "down\0up" + (status * 5); |
| 307 | } |
| 308 | |
| 309 | static int run_script(const char *action) |
| 310 | { |
| 311 | char *env_PREVIOUS, *env_CURRENT; |
| 312 | char *argv[5]; |
| 313 | int r; |
| 314 | |
| 315 | bb_error_msg("executing '%s %s %s'", G.script_name, G.iface, action); |
| 316 | |
| 317 | argv[0] = (char*) G.script_name; |
| 318 | argv[1] = (char*) G.iface; |
| 319 | argv[2] = (char*) action; |
| 320 | argv[3] = (char*) G.extra_arg; |
| 321 | argv[4] = NULL; |
| 322 | |
| 323 | env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status)); |
| 324 | putenv(env_PREVIOUS); |
| 325 | env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status)); |
| 326 | putenv(env_CURRENT); |
| 327 | |
| 328 | /* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */ |
| 329 | r = spawn_and_wait(argv); |
| 330 | |
| 331 | unsetenv(IFPLUGD_ENV_PREVIOUS); |
| 332 | unsetenv(IFPLUGD_ENV_CURRENT); |
| 333 | free(env_PREVIOUS); |
| 334 | free(env_CURRENT); |
| 335 | |
| 336 | bb_error_msg("exit code: %d", r & 0xff); |
| 337 | return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r; |
| 338 | } |
| 339 | |
| 340 | static void up_iface(void) |
| 341 | { |
| 342 | struct ifreq ifrequest; |
| 343 | |
| 344 | if (!G.iface_exists) |
| 345 | return; |
| 346 | |
| 347 | set_ifreq_to_ifname(&ifrequest); |
| 348 | if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) { |
| 349 | G.iface_exists = 0; |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (!(ifrequest.ifr_flags & IFF_UP)) { |
| 354 | ifrequest.ifr_flags |= IFF_UP; |
| 355 | /* Let user know we mess up with interface */ |
| 356 | bb_error_msg("upping interface"); |
| 357 | if (network_ioctl(SIOCSIFFLAGS, &ifrequest, "setting interface flags") < 0) |
| 358 | xfunc_die(); |
| 359 | } |
| 360 | |
| 361 | #if 0 /* why do we mess with IP addr? It's not our business */ |
| 362 | if (network_ioctl(SIOCGIFADDR, &ifrequest, "can't get interface address") < 0) { |
| 363 | } else if (ifrequest.ifr_addr.sa_family != AF_INET) { |
| 364 | bb_perror_msg("the interface is not IP-based"); |
| 365 | } else { |
| 366 | ((struct sockaddr_in*)(&ifrequest.ifr_addr))->sin_addr.s_addr = INADDR_ANY; |
| 367 | network_ioctl(SIOCSIFADDR, &ifrequest, "can't set interface address"); |
| 368 | } |
| 369 | network_ioctl(SIOCGIFFLAGS, &ifrequest, "can't get interface flags"); |
| 370 | #endif |
| 371 | } |
| 372 | |
| 373 | static void maybe_up_new_iface(void) |
| 374 | { |
| 375 | if (!(option_mask32 & FLAG_NO_AUTO)) |
| 376 | up_iface(); |
| 377 | |
| 378 | #if 0 /* bloat */ |
| 379 | struct ifreq ifrequest; |
| 380 | struct ethtool_drvinfo driver_info; |
| 381 | |
| 382 | set_ifreq_to_ifname(&ifrequest); |
| 383 | driver_info.cmd = ETHTOOL_GDRVINFO; |
| 384 | ifrequest.ifr_data = &driver_info; |
| 385 | if (network_ioctl(SIOCETHTOOL, &ifrequest, NULL) == 0) { |
| 386 | char buf[sizeof("/xx:xx:xx:xx:xx:xx")]; |
| 387 | |
| 388 | /* Get MAC */ |
| 389 | buf[0] = '\0'; |
| 390 | set_ifreq_to_ifname(&ifrequest); |
| 391 | if (network_ioctl(SIOCGIFHWADDR, &ifrequest, NULL) == 0) { |
| 392 | sprintf(buf, "/%02X:%02X:%02X:%02X:%02X:%02X", |
| 393 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[0]), |
| 394 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[1]), |
| 395 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[2]), |
| 396 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[3]), |
| 397 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[4]), |
| 398 | (uint8_t)(ifrequest.ifr_hwaddr.sa_data[5])); |
| 399 | } |
| 400 | |
| 401 | bb_error_msg("using interface %s%s with driver<%s> (version: %s)", |
| 402 | G.iface, buf, driver_info.driver, driver_info.version); |
| 403 | } |
| 404 | #endif |
| 405 | if (G.api_mode[0] == 'a') |
| 406 | G.api_method_num = API_AUTO; |
| 407 | } |
| 408 | |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 409 | static smallint detect_link(void) |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 410 | { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 411 | smallint status; |
| 412 | |
| 413 | if (!G.iface_exists) |
| 414 | return (option_mask32 & FLAG_MONITOR) ? IFSTATUS_DOWN : IFSTATUS_ERR; |
| 415 | |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 416 | /* Some drivers can't detect link status when the interface is down. |
| 417 | * I imagine detect_link_iff() is the most vulnerable. |
| 418 | * That's why -a "noauto" in an option, not a hardwired behavior. |
| 419 | */ |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 420 | if (!(option_mask32 & FLAG_NO_AUTO)) |
| 421 | up_iface(); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 422 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 423 | if (G.api_method_num == API_AUTO) { |
| 424 | int i; |
| 425 | smallint sv_logmode; |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 426 | |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 427 | sv_logmode = logmode; |
| 428 | for (i = 0; i < ARRAY_SIZE(method_table); i++) { |
| 429 | logmode = LOGMODE_NONE; |
| 430 | status = method_table[i].func(); |
| 431 | logmode = sv_logmode; |
| 432 | if (status != IFSTATUS_ERR) { |
| 433 | G.api_method_num = i; |
| 434 | bb_error_msg("using %s detection mode", method_table[i].name); |
| 435 | break; |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 436 | } |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 437 | } |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 438 | } else { |
| 439 | status = method_table[G.api_method_num].func(); |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 442 | if (status == IFSTATUS_ERR) { |
| 443 | if (option_mask32 & FLAG_IGNORE_FAIL) |
| 444 | status = IFSTATUS_DOWN; |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 445 | else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE) |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 446 | status = IFSTATUS_UP; |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 447 | else if (G.api_mode[0] == 'a') |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 448 | bb_error_msg("can't detect link status"); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 449 | } |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 450 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 451 | if (status != G.iface_last_status) { |
Denys Vlasenko | 19afe84 | 2010-05-08 23:26:16 +0200 | [diff] [blame] | 452 | G.iface_prev_status = G.iface_last_status; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 453 | G.iface_last_status = status; |
| 454 | } |
| 455 | |
| 456 | return status; |
| 457 | } |
| 458 | |
| 459 | static NOINLINE int check_existence_through_netlink(void) |
| 460 | { |
Denys Vlasenko | 3720121 | 2010-04-02 07:04:44 +0200 | [diff] [blame] | 461 | int iface_len; |
Tito Ragusa | da331d7 | 2013-08-04 18:08:40 +0200 | [diff] [blame] | 462 | /* Buffer was 1K, but on linux-3.9.9 it was reported to be too small. |
| 463 | * netlink.h: "limit to 8K to avoid MSG_TRUNC when PAGE_SIZE is very large". |
| 464 | * Note: on error returns (-1) we exit, no need to free replybuf. |
| 465 | */ |
| 466 | enum { BUF_SIZE = 8 * 1024 }; |
| 467 | char *replybuf = xmalloc(BUF_SIZE); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 468 | |
Denys Vlasenko | 3720121 | 2010-04-02 07:04:44 +0200 | [diff] [blame] | 469 | iface_len = strlen(G.iface); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 470 | while (1) { |
| 471 | struct nlmsghdr *mhdr; |
| 472 | ssize_t bytes; |
| 473 | |
Tito Ragusa | da331d7 | 2013-08-04 18:08:40 +0200 | [diff] [blame] | 474 | bytes = recv(netlink_fd, replybuf, BUF_SIZE, MSG_DONTWAIT); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 475 | if (bytes < 0) { |
| 476 | if (errno == EAGAIN) |
Tito Ragusa | da331d7 | 2013-08-04 18:08:40 +0200 | [diff] [blame] | 477 | goto ret; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 478 | if (errno == EINTR) |
| 479 | continue; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 480 | bb_perror_msg("netlink: recv"); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | mhdr = (struct nlmsghdr*)replybuf; |
| 485 | while (bytes > 0) { |
Maxim Kryzhanovsky | 2004fa1 | 2010-03-30 15:49:57 +0200 | [diff] [blame] | 486 | if (!NLMSG_OK(mhdr, bytes)) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 487 | bb_error_msg("netlink packet too small or truncated"); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) { |
| 492 | struct rtattr *attr; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 493 | int attr_len; |
| 494 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 495 | if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) { |
| 496 | bb_error_msg("netlink packet too small or truncated"); |
| 497 | return -1; |
| 498 | } |
| 499 | |
Maxim Kryzhanovsky | 2004fa1 | 2010-03-30 15:49:57 +0200 | [diff] [blame] | 500 | attr = IFLA_RTA(NLMSG_DATA(mhdr)); |
| 501 | attr_len = IFLA_PAYLOAD(mhdr); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 502 | |
| 503 | while (RTA_OK(attr, attr_len)) { |
| 504 | if (attr->rta_type == IFLA_IFNAME) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 505 | int len = RTA_PAYLOAD(attr); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 506 | if (len > IFNAMSIZ) |
| 507 | len = IFNAMSIZ; |
Denys Vlasenko | 3720121 | 2010-04-02 07:04:44 +0200 | [diff] [blame] | 508 | if (iface_len <= len |
| 509 | && strncmp(G.iface, RTA_DATA(attr), len) == 0 |
| 510 | ) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 511 | G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK); |
| 512 | } |
| 513 | } |
| 514 | attr = RTA_NEXT(attr, attr_len); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | mhdr = NLMSG_NEXT(mhdr, bytes); |
| 519 | } |
| 520 | } |
| 521 | |
Tito Ragusa | da331d7 | 2013-08-04 18:08:40 +0200 | [diff] [blame] | 522 | ret: |
| 523 | free(replybuf); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 524 | return G.iface_exists; |
| 525 | } |
| 526 | |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 527 | #if ENABLE_FEATURE_PIDFILE |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 528 | static NOINLINE pid_t read_pid(const char *filename) |
| 529 | { |
| 530 | int len; |
| 531 | char buf[128]; |
| 532 | |
| 533 | len = open_read_close(filename, buf, 127); |
| 534 | if (len > 0) { |
| 535 | buf[len] = '\0'; |
| 536 | /* returns ULONG_MAX on error => -1 */ |
| 537 | return bb_strtoul(buf, NULL, 10); |
| 538 | } |
| 539 | return 0; |
| 540 | } |
Denys Vlasenko | 9c35a1c | 2009-05-01 09:04:25 +0200 | [diff] [blame] | 541 | #endif |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 542 | |
| 543 | int ifplugd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 544 | int ifplugd_main(int argc UNUSED_PARAM, char **argv) |
| 545 | { |
| 546 | int iface_status; |
| 547 | int delay_time; |
| 548 | const char *iface_status_str; |
| 549 | struct pollfd netlink_pollfd[1]; |
| 550 | unsigned opts; |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 551 | const char *api_mode_found; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 552 | #if ENABLE_FEATURE_PIDFILE |
| 553 | char *pidfile_name; |
| 554 | pid_t pid_from_pidfile; |
| 555 | #endif |
| 556 | |
| 557 | INIT_G(); |
| 558 | |
| 559 | opt_complementary = "t+:u+:d+"; |
| 560 | opts = getopt32(argv, OPTION_STR, |
| 561 | &G.iface, &G.script_name, &G.poll_time, &G.delay_up, |
| 562 | &G.delay_down, &G.api_mode, &G.extra_arg); |
Denys Vlasenko | 5a34d02 | 2009-11-07 17:30:14 +0100 | [diff] [blame] | 563 | G.poll_time *= 1000; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 564 | |
| 565 | applet_name = xasprintf("ifplugd(%s)", G.iface); |
| 566 | |
| 567 | #if ENABLE_FEATURE_PIDFILE |
Anthony G. Basile | 12677ac | 2012-12-10 14:49:39 -0500 | [diff] [blame] | 568 | pidfile_name = xasprintf(CONFIG_PID_FILE_PATH "/ifplugd.%s.pid", G.iface); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 569 | pid_from_pidfile = read_pid(pidfile_name); |
| 570 | |
| 571 | if (opts & FLAG_KILL) { |
| 572 | if (pid_from_pidfile > 0) |
Denys Vlasenko | 216e952 | 2013-02-28 12:50:09 +0100 | [diff] [blame] | 573 | /* Upstream tool use SIGINT for -k */ |
| 574 | kill(pid_from_pidfile, SIGINT); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 575 | return EXIT_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0) |
| 579 | bb_error_msg_and_die("daemon already running"); |
| 580 | #endif |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 581 | |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 582 | api_mode_found = strchr(api_modes, G.api_mode[0]); |
| 583 | if (!api_mode_found) |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 584 | bb_error_msg_and_die("unknown API mode '%s'", G.api_mode); |
Maksym Kryzhanovskyy | 9388b4e | 2010-07-08 02:47:25 +0200 | [diff] [blame] | 585 | G.api_method_num = api_mode_found - api_modes; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 586 | |
| 587 | if (!(opts & FLAG_NO_DAEMON)) |
| 588 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |
| 589 | |
| 590 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd); |
| 591 | if (opts & FLAG_MONITOR) { |
Maksym Kryzhanovskyy | 4f0279b | 2010-07-22 02:18:05 +0200 | [diff] [blame] | 592 | struct sockaddr_nl addr; |
| 593 | int fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 594 | |
| 595 | memset(&addr, 0, sizeof(addr)); |
| 596 | addr.nl_family = AF_NETLINK; |
| 597 | addr.nl_groups = RTMGRP_LINK; |
| 598 | addr.nl_pid = getpid(); |
| 599 | |
| 600 | xbind(fd, (struct sockaddr*)&addr, sizeof(addr)); |
| 601 | xmove_fd(fd, netlink_fd); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | write_pidfile(pidfile_name); |
| 605 | |
| 606 | /* this can't be moved before socket creation */ |
| 607 | if (!(opts & FLAG_NO_SYSLOG)) { |
| 608 | openlog(applet_name, 0, LOG_DAEMON); |
| 609 | logmode |= LOGMODE_SYSLOG; |
| 610 | } |
| 611 | |
| 612 | bb_signals(0 |
| 613 | | (1 << SIGINT ) |
| 614 | | (1 << SIGTERM) |
| 615 | | (1 << SIGQUIT) |
| 616 | | (1 << SIGHUP ) /* why we ignore it? */ |
| 617 | /* | (1 << SIGCHLD) - run_script does not use it anymore */ |
| 618 | , record_signo); |
| 619 | |
| 620 | bb_error_msg("started: %s", bb_banner); |
| 621 | |
| 622 | if (opts & FLAG_MONITOR) { |
| 623 | struct ifreq ifrequest; |
| 624 | set_ifreq_to_ifname(&ifrequest); |
Denys Vlasenko | f422a72 | 2010-01-08 12:27:57 +0100 | [diff] [blame] | 625 | G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0); |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (G.iface_exists) |
| 629 | maybe_up_new_iface(); |
| 630 | |
| 631 | iface_status = detect_link(); |
| 632 | if (iface_status == IFSTATUS_ERR) |
| 633 | goto exiting; |
| 634 | iface_status_str = strstatus(iface_status); |
| 635 | |
| 636 | if (opts & FLAG_MONITOR) { |
| 637 | bb_error_msg("interface %s", |
| 638 | G.iface_exists ? "exists" |
| 639 | : "doesn't exist, waiting"); |
| 640 | } |
| 641 | /* else we assume it always exists, but don't mislead user |
| 642 | * by potentially lying that it really exists */ |
| 643 | |
| 644 | if (G.iface_exists) { |
| 645 | bb_error_msg("link is %s", iface_status_str); |
| 646 | } |
| 647 | |
| 648 | if ((!(opts & FLAG_NO_STARTUP) |
| 649 | && iface_status == IFSTATUS_UP |
| 650 | ) |
| 651 | || (opts & FLAG_INITIAL_DOWN) |
| 652 | ) { |
| 653 | if (run_script(iface_status_str) != 0) |
| 654 | goto exiting; |
| 655 | } |
| 656 | |
| 657 | /* Main loop */ |
| 658 | netlink_pollfd[0].fd = netlink_fd; |
| 659 | netlink_pollfd[0].events = POLLIN; |
| 660 | delay_time = 0; |
| 661 | while (1) { |
| 662 | int iface_status_old; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 663 | |
| 664 | switch (bb_got_signal) { |
| 665 | case SIGINT: |
| 666 | case SIGTERM: |
| 667 | bb_got_signal = 0; |
| 668 | goto cleanup; |
| 669 | case SIGQUIT: |
| 670 | bb_got_signal = 0; |
| 671 | goto exiting; |
| 672 | default: |
| 673 | bb_got_signal = 0; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | if (poll(netlink_pollfd, |
| 678 | (opts & FLAG_MONITOR) ? 1 : 0, |
Denys Vlasenko | 5a34d02 | 2009-11-07 17:30:14 +0100 | [diff] [blame] | 679 | G.poll_time |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 680 | ) < 0 |
| 681 | ) { |
| 682 | if (errno == EINTR) |
| 683 | continue; |
| 684 | bb_perror_msg("poll"); |
| 685 | goto exiting; |
| 686 | } |
| 687 | |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 688 | if ((opts & FLAG_MONITOR) |
| 689 | && (netlink_pollfd[0].revents & POLLIN) |
| 690 | ) { |
Denys Vlasenko | 50e4cc2 | 2014-02-10 09:44:25 +0100 | [diff] [blame] | 691 | int iface_exists_old; |
| 692 | |
| 693 | iface_exists_old = G.iface_exists; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 694 | G.iface_exists = check_existence_through_netlink(); |
| 695 | if (G.iface_exists < 0) /* error */ |
| 696 | goto exiting; |
| 697 | if (iface_exists_old != G.iface_exists) { |
| 698 | bb_error_msg("interface %sappeared", |
| 699 | G.iface_exists ? "" : "dis"); |
| 700 | if (G.iface_exists) |
| 701 | maybe_up_new_iface(); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* note: if !G.iface_exists, returns DOWN */ |
Denys Vlasenko | 50e4cc2 | 2014-02-10 09:44:25 +0100 | [diff] [blame] | 706 | iface_status_old = iface_status; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 707 | iface_status = detect_link(); |
| 708 | if (iface_status == IFSTATUS_ERR) { |
| 709 | if (!(opts & FLAG_MONITOR)) |
| 710 | goto exiting; |
| 711 | iface_status = IFSTATUS_DOWN; |
| 712 | } |
| 713 | iface_status_str = strstatus(iface_status); |
| 714 | |
| 715 | if (iface_status_old != iface_status) { |
| 716 | bb_error_msg("link is %s", iface_status_str); |
| 717 | |
| 718 | if (delay_time) { |
| 719 | /* link restored its old status before |
Denys Vlasenko | 50e4cc2 | 2014-02-10 09:44:25 +0100 | [diff] [blame] | 720 | * we ran script. don't run the script: */ |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 721 | delay_time = 0; |
| 722 | } else { |
| 723 | delay_time = monotonic_sec(); |
| 724 | if (iface_status == IFSTATUS_UP) |
| 725 | delay_time += G.delay_up; |
| 726 | if (iface_status == IFSTATUS_DOWN) |
| 727 | delay_time += G.delay_down; |
Denys Vlasenko | 50e4cc2 | 2014-02-10 09:44:25 +0100 | [diff] [blame] | 728 | #if 0 /* if you are back in 1970... */ |
| 729 | if (delay_time == 0) { |
| 730 | sleep(1); |
| 731 | delay_time = 1; |
| 732 | } |
| 733 | #endif |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) { |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 738 | if (run_script(iface_status_str) != 0) |
| 739 | goto exiting; |
Denys Vlasenko | 50e4cc2 | 2014-02-10 09:44:25 +0100 | [diff] [blame] | 740 | delay_time = 0; |
Denis Vlasenko | 71c1657 | 2009-04-26 01:08:51 +0000 | [diff] [blame] | 741 | } |
| 742 | } /* while (1) */ |
| 743 | |
| 744 | cleanup: |
| 745 | if (!(opts & FLAG_NO_SHUTDOWN) |
| 746 | && (iface_status == IFSTATUS_UP |
| 747 | || (iface_status == IFSTATUS_DOWN && delay_time) |
| 748 | ) |
| 749 | ) { |
| 750 | setenv(IFPLUGD_ENV_PREVIOUS, strstatus(iface_status), 1); |
| 751 | setenv(IFPLUGD_ENV_CURRENT, strstatus(-1), 1); |
| 752 | run_script("down\0up"); /* reusing string */ |
| 753 | } |
| 754 | |
| 755 | exiting: |
| 756 | remove_pidfile(pidfile_name); |
| 757 | bb_error_msg_and_die("exiting"); |
| 758 | } |