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