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