ifplugd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ifplugd for busybox, based on ifplugd 0.28 (written by Lennart Poettering).
  4. *
  5. * Copyright (C) 2009 Maksym Kryzhanovskyy <xmaks@email.cz>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #include "fix_u32.h"
  11. #include <linux/if.h>
  12. #include <linux/mii.h>
  13. #include <linux/ethtool.h>
  14. #include <net/ethernet.h>
  15. #include <linux/netlink.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/sockios.h>
  18. #include <syslog.h>
  19. #define __user
  20. #include <linux/wireless.h>
  21. /*
  22. From initial port to busybox, removed most of the redundancy by
  23. converting implementation of a polymorphic interface to the strict
  24. functional style. The main role is run a script when link state
  25. changed, other activities like audio signal or detailed reports
  26. are on the script itself.
  27. One questionable point of the design is netlink usage:
  28. We have 1 second timeout by default to poll the link status,
  29. it is short enough so that there are no real benefits in
  30. using netlink to get "instantaneous" interface creation/deletion
  31. notifications. We can check for interface existence by just
  32. doing some fast ioctl using its name.
  33. Netlink code then can be just dropped (1k or more?)
  34. */
  35. #define IFPLUGD_ENV_PREVIOUS "IFPLUGD_PREVIOUS"
  36. #define IFPLUGD_ENV_CURRENT "IFPLUGD_CURRENT"
  37. enum {
  38. FLAG_NO_AUTO = 1 << 0, // -a, Do not enable interface automatically
  39. FLAG_NO_DAEMON = 1 << 1, // -n, Do not daemonize
  40. FLAG_NO_SYSLOG = 1 << 2, // -s, Do not use syslog, use stderr instead
  41. FLAG_IGNORE_FAIL = 1 << 3, // -f, Ignore detection failure, retry instead (failure is treated as DOWN)
  42. FLAG_IGNORE_FAIL_POSITIVE = 1 << 4, // -F, Ignore detection failure, retry instead (failure is treated as UP)
  43. FLAG_IFACE = 1 << 5, // -i, Specify ethernet interface
  44. FLAG_RUN = 1 << 6, // -r, Specify program to execute
  45. FLAG_IGNORE_RETVAL = 1 << 7, // -I, Don't exit on nonzero return value of program executed
  46. FLAG_POLL_TIME = 1 << 8, // -t, Specify poll time in seconds
  47. FLAG_DELAY_UP = 1 << 9, // -u, Specify delay for configuring interface
  48. FLAG_DELAY_DOWN = 1 << 10, // -d, Specify delay for deconfiguring interface
  49. FLAG_API_MODE = 1 << 11, // -m, Force API mode (mii, priv, ethtool, wlan, auto)
  50. FLAG_NO_STARTUP = 1 << 12, // -p, Don't run script on daemon startup
  51. FLAG_NO_SHUTDOWN = 1 << 13, // -q, Don't run script on daemon quit
  52. FLAG_INITIAL_DOWN = 1 << 14, // -l, Run "down" script on startup if no cable is detected
  53. FLAG_EXTRA_ARG = 1 << 15, // -x, Specify an extra argument for action script
  54. FLAG_MONITOR = 1 << 16, // -M, Use interface monitoring
  55. #if ENABLE_FEATURE_PIDFILE
  56. FLAG_KILL = 1 << 17, // -k, Kill a running daemon
  57. #endif
  58. };
  59. #if ENABLE_FEATURE_PIDFILE
  60. # define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:Mk"
  61. #else
  62. # define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:M"
  63. #endif
  64. enum { // interface status
  65. IFSTATUS_ERR = -1,
  66. IFSTATUS_DOWN = 0,
  67. IFSTATUS_UP = 1,
  68. };
  69. enum { // constant fds
  70. ioctl_fd = 3,
  71. netlink_fd = 4,
  72. };
  73. struct globals {
  74. smallint iface_last_status;
  75. smallint iface_prev_status;
  76. smallint iface_exists;
  77. smallint api_method_num;
  78. /* Used in getopt32, must have sizeof == sizeof(int) */
  79. unsigned poll_time;
  80. unsigned delay_up;
  81. unsigned delay_down;
  82. const char *iface;
  83. const char *api_mode;
  84. const char *script_name;
  85. const char *extra_arg;
  86. };
  87. #define G (*ptr_to_globals)
  88. #define INIT_G() do { \
  89. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  90. G.iface_last_status = -1; \
  91. G.iface_exists = 1; \
  92. G.poll_time = 1; \
  93. G.delay_down = 5; \
  94. G.iface = "eth0"; \
  95. G.api_mode = "a"; \
  96. G.script_name = "/etc/ifplugd/ifplugd.action"; \
  97. } while (0)
  98. /* Utility routines */
  99. static void set_ifreq_to_ifname(struct ifreq *ifreq)
  100. {
  101. memset(ifreq, 0, sizeof(struct ifreq));
  102. strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface);
  103. }
  104. static int network_ioctl(int request, void* data, const char *errmsg)
  105. {
  106. int r = ioctl(ioctl_fd, request, data);
  107. if (r < 0 && errmsg)
  108. bb_perror_msg("%s failed", errmsg);
  109. return r;
  110. }
  111. /* Link detection routines and table */
  112. static smallint detect_link_mii(void)
  113. {
  114. struct ifreq ifreq;
  115. struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
  116. set_ifreq_to_ifname(&ifreq);
  117. if (network_ioctl(SIOCGMIIPHY, &ifreq, "SIOCGMIIPHY") < 0) {
  118. return IFSTATUS_ERR;
  119. }
  120. mii->reg_num = 1;
  121. if (network_ioctl(SIOCGMIIREG, &ifreq, "SIOCGMIIREG") < 0) {
  122. return IFSTATUS_ERR;
  123. }
  124. return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
  125. }
  126. static smallint detect_link_priv(void)
  127. {
  128. struct ifreq ifreq;
  129. struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
  130. set_ifreq_to_ifname(&ifreq);
  131. if (network_ioctl(SIOCDEVPRIVATE, &ifreq, "SIOCDEVPRIVATE") < 0) {
  132. return IFSTATUS_ERR;
  133. }
  134. mii->reg_num = 1;
  135. if (network_ioctl(SIOCDEVPRIVATE+1, &ifreq, "SIOCDEVPRIVATE+1") < 0) {
  136. return IFSTATUS_ERR;
  137. }
  138. return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
  139. }
  140. static smallint detect_link_ethtool(void)
  141. {
  142. struct ifreq ifreq;
  143. struct ethtool_value edata;
  144. set_ifreq_to_ifname(&ifreq);
  145. edata.cmd = ETHTOOL_GLINK;
  146. ifreq.ifr_data = (void*) &edata;
  147. if (network_ioctl(SIOCETHTOOL, &ifreq, "ETHTOOL_GLINK") < 0) {
  148. return IFSTATUS_ERR;
  149. }
  150. return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN;
  151. }
  152. static smallint detect_link_iff(void)
  153. {
  154. struct ifreq ifreq;
  155. set_ifreq_to_ifname(&ifreq);
  156. if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) {
  157. return IFSTATUS_ERR;
  158. }
  159. /* If IFF_UP is not set (interface is down), IFF_RUNNING is never set
  160. * regardless of link status. Simply continue to report last status -
  161. * no point in reporting spurious link downs if interface is disabled
  162. * by admin. When/if it will be brought up,
  163. * we'll report real link status.
  164. */
  165. if (!(ifreq.ifr_flags & IFF_UP) && G.iface_last_status != IFSTATUS_ERR)
  166. return G.iface_last_status;
  167. return (ifreq.ifr_flags & IFF_RUNNING) ? IFSTATUS_UP : IFSTATUS_DOWN;
  168. }
  169. static smallint detect_link_wlan(void)
  170. {
  171. int i;
  172. struct iwreq iwrequest;
  173. uint8_t mac[ETH_ALEN];
  174. memset(&iwrequest, 0, sizeof(iwrequest));
  175. strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface);
  176. if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) {
  177. return IFSTATUS_ERR;
  178. }
  179. memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN);
  180. if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) {
  181. for (i = 1; i < ETH_ALEN; ++i) {
  182. if (mac[i] != mac[0])
  183. return IFSTATUS_UP;
  184. }
  185. return IFSTATUS_DOWN;
  186. }
  187. return IFSTATUS_UP;
  188. }
  189. enum { // api mode
  190. API_ETHTOOL, // 'e'
  191. API_MII, // 'm'
  192. API_PRIVATE, // 'p'
  193. API_WLAN, // 'w'
  194. API_IFF, // 'i'
  195. API_AUTO, // 'a'
  196. };
  197. static const char api_modes[] ALIGN1 = "empwia";
  198. static const struct {
  199. const char *name;
  200. smallint (*func)(void);
  201. } method_table[] = {
  202. { "SIOCETHTOOL" , &detect_link_ethtool },
  203. { "SIOCGMIIPHY" , &detect_link_mii },
  204. { "SIOCDEVPRIVATE" , &detect_link_priv },
  205. { "wireless extension", &detect_link_wlan },
  206. { "IFF_RUNNING" , &detect_link_iff },
  207. };
  208. static const char *strstatus(int status)
  209. {
  210. if (status == IFSTATUS_ERR)
  211. return "error";
  212. return "down\0up" + (status * 5);
  213. }
  214. static int run_script(const char *action)
  215. {
  216. char *env_PREVIOUS, *env_CURRENT;
  217. char *argv[5];
  218. int r;
  219. bb_error_msg("executing '%s %s %s'", G.script_name, G.iface, action);
  220. argv[0] = (char*) G.script_name;
  221. argv[1] = (char*) G.iface;
  222. argv[2] = (char*) action;
  223. argv[3] = (char*) G.extra_arg;
  224. argv[4] = NULL;
  225. env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status));
  226. putenv(env_PREVIOUS);
  227. env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status));
  228. putenv(env_CURRENT);
  229. /* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */
  230. r = spawn_and_wait(argv);
  231. unsetenv(IFPLUGD_ENV_PREVIOUS);
  232. unsetenv(IFPLUGD_ENV_CURRENT);
  233. free(env_PREVIOUS);
  234. free(env_CURRENT);
  235. bb_error_msg("exit code: %d", r & 0xff);
  236. return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r;
  237. }
  238. static void up_iface(void)
  239. {
  240. struct ifreq ifrequest;
  241. if (!G.iface_exists)
  242. return;
  243. set_ifreq_to_ifname(&ifrequest);
  244. if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) {
  245. G.iface_exists = 0;
  246. return;
  247. }
  248. if (!(ifrequest.ifr_flags & IFF_UP)) {
  249. ifrequest.ifr_flags |= IFF_UP;
  250. /* Let user know we mess up with interface */
  251. bb_error_msg("upping interface");
  252. if (network_ioctl(SIOCSIFFLAGS, &ifrequest, "setting interface flags") < 0)
  253. xfunc_die();
  254. }
  255. #if 0 /* why do we mess with IP addr? It's not our business */
  256. if (network_ioctl(SIOCGIFADDR, &ifrequest, "can't get interface address") < 0) {
  257. } else if (ifrequest.ifr_addr.sa_family != AF_INET) {
  258. bb_perror_msg("the interface is not IP-based");
  259. } else {
  260. ((struct sockaddr_in*)(&ifrequest.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
  261. network_ioctl(SIOCSIFADDR, &ifrequest, "can't set interface address");
  262. }
  263. network_ioctl(SIOCGIFFLAGS, &ifrequest, "can't get interface flags");
  264. #endif
  265. }
  266. static void maybe_up_new_iface(void)
  267. {
  268. if (!(option_mask32 & FLAG_NO_AUTO))
  269. up_iface();
  270. #if 0 /* bloat */
  271. struct ifreq ifrequest;
  272. struct ethtool_drvinfo driver_info;
  273. set_ifreq_to_ifname(&ifrequest);
  274. driver_info.cmd = ETHTOOL_GDRVINFO;
  275. ifrequest.ifr_data = &driver_info;
  276. if (network_ioctl(SIOCETHTOOL, &ifrequest, NULL) == 0) {
  277. char buf[sizeof("/xx:xx:xx:xx:xx:xx")];
  278. /* Get MAC */
  279. buf[0] = '\0';
  280. set_ifreq_to_ifname(&ifrequest);
  281. if (network_ioctl(SIOCGIFHWADDR, &ifrequest, NULL) == 0) {
  282. sprintf(buf, "/%02X:%02X:%02X:%02X:%02X:%02X",
  283. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[0]),
  284. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[1]),
  285. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[2]),
  286. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[3]),
  287. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[4]),
  288. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[5]));
  289. }
  290. bb_error_msg("using interface %s%s with driver<%s> (version: %s)",
  291. G.iface, buf, driver_info.driver, driver_info.version);
  292. }
  293. #endif
  294. if (G.api_mode[0] == 'a')
  295. G.api_method_num = API_AUTO;
  296. }
  297. static smallint detect_link(void)
  298. {
  299. smallint status;
  300. if (!G.iface_exists)
  301. return (option_mask32 & FLAG_MONITOR) ? IFSTATUS_DOWN : IFSTATUS_ERR;
  302. /* Some drivers can't detect link status when the interface is down.
  303. * I imagine detect_link_iff() is the most vulnerable.
  304. * That's why -a "noauto" in an option, not a hardwired behavior.
  305. */
  306. if (!(option_mask32 & FLAG_NO_AUTO))
  307. up_iface();
  308. if (G.api_method_num == API_AUTO) {
  309. int i;
  310. smallint sv_logmode;
  311. sv_logmode = logmode;
  312. for (i = 0; i < ARRAY_SIZE(method_table); i++) {
  313. logmode = LOGMODE_NONE;
  314. status = method_table[i].func();
  315. logmode = sv_logmode;
  316. if (status != IFSTATUS_ERR) {
  317. G.api_method_num = i;
  318. bb_error_msg("using %s detection mode", method_table[i].name);
  319. break;
  320. }
  321. }
  322. } else {
  323. status = method_table[G.api_method_num].func();
  324. }
  325. if (status == IFSTATUS_ERR) {
  326. if (option_mask32 & FLAG_IGNORE_FAIL)
  327. status = IFSTATUS_DOWN;
  328. else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE)
  329. status = IFSTATUS_UP;
  330. else if (G.api_mode[0] == 'a')
  331. bb_error_msg("can't detect link status");
  332. }
  333. if (status != G.iface_last_status) {
  334. G.iface_prev_status = G.iface_last_status;
  335. G.iface_last_status = status;
  336. }
  337. return status;
  338. }
  339. static NOINLINE int check_existence_through_netlink(void)
  340. {
  341. int iface_len;
  342. char replybuf[1024];
  343. iface_len = strlen(G.iface);
  344. while (1) {
  345. struct nlmsghdr *mhdr;
  346. ssize_t bytes;
  347. bytes = recv(netlink_fd, &replybuf, sizeof(replybuf), MSG_DONTWAIT);
  348. if (bytes < 0) {
  349. if (errno == EAGAIN)
  350. return G.iface_exists;
  351. if (errno == EINTR)
  352. continue;
  353. bb_perror_msg("netlink: recv");
  354. return -1;
  355. }
  356. mhdr = (struct nlmsghdr*)replybuf;
  357. while (bytes > 0) {
  358. if (!NLMSG_OK(mhdr, bytes)) {
  359. bb_error_msg("netlink packet too small or truncated");
  360. return -1;
  361. }
  362. if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) {
  363. struct rtattr *attr;
  364. int attr_len;
  365. if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
  366. bb_error_msg("netlink packet too small or truncated");
  367. return -1;
  368. }
  369. attr = IFLA_RTA(NLMSG_DATA(mhdr));
  370. attr_len = IFLA_PAYLOAD(mhdr);
  371. while (RTA_OK(attr, attr_len)) {
  372. if (attr->rta_type == IFLA_IFNAME) {
  373. int len = RTA_PAYLOAD(attr);
  374. if (len > IFNAMSIZ)
  375. len = IFNAMSIZ;
  376. if (iface_len <= len
  377. && strncmp(G.iface, RTA_DATA(attr), len) == 0
  378. ) {
  379. G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK);
  380. }
  381. }
  382. attr = RTA_NEXT(attr, attr_len);
  383. }
  384. }
  385. mhdr = NLMSG_NEXT(mhdr, bytes);
  386. }
  387. }
  388. return G.iface_exists;
  389. }
  390. #if ENABLE_FEATURE_PIDFILE
  391. static NOINLINE pid_t read_pid(const char *filename)
  392. {
  393. int len;
  394. char buf[128];
  395. len = open_read_close(filename, buf, 127);
  396. if (len > 0) {
  397. buf[len] = '\0';
  398. /* returns ULONG_MAX on error => -1 */
  399. return bb_strtoul(buf, NULL, 10);
  400. }
  401. return 0;
  402. }
  403. #endif
  404. int ifplugd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  405. int ifplugd_main(int argc UNUSED_PARAM, char **argv)
  406. {
  407. int iface_status;
  408. int delay_time;
  409. const char *iface_status_str;
  410. struct pollfd netlink_pollfd[1];
  411. unsigned opts;
  412. const char *api_mode_found;
  413. #if ENABLE_FEATURE_PIDFILE
  414. char *pidfile_name;
  415. pid_t pid_from_pidfile;
  416. #endif
  417. INIT_G();
  418. opt_complementary = "t+:u+:d+";
  419. opts = getopt32(argv, OPTION_STR,
  420. &G.iface, &G.script_name, &G.poll_time, &G.delay_up,
  421. &G.delay_down, &G.api_mode, &G.extra_arg);
  422. G.poll_time *= 1000;
  423. applet_name = xasprintf("ifplugd(%s)", G.iface);
  424. #if ENABLE_FEATURE_PIDFILE
  425. pidfile_name = xasprintf(_PATH_VARRUN"ifplugd.%s.pid", G.iface);
  426. pid_from_pidfile = read_pid(pidfile_name);
  427. if (opts & FLAG_KILL) {
  428. if (pid_from_pidfile > 0)
  429. kill(pid_from_pidfile, SIGQUIT);
  430. return EXIT_SUCCESS;
  431. }
  432. if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0)
  433. bb_error_msg_and_die("daemon already running");
  434. #endif
  435. api_mode_found = strchr(api_modes, G.api_mode[0]);
  436. if (!api_mode_found)
  437. bb_error_msg_and_die("unknown API mode '%s'", G.api_mode);
  438. G.api_method_num = api_mode_found - api_modes;
  439. if (!(opts & FLAG_NO_DAEMON))
  440. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  441. xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
  442. if (opts & FLAG_MONITOR) {
  443. struct sockaddr_nl addr;
  444. int fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
  445. memset(&addr, 0, sizeof(addr));
  446. addr.nl_family = AF_NETLINK;
  447. addr.nl_groups = RTMGRP_LINK;
  448. addr.nl_pid = getpid();
  449. xbind(fd, (struct sockaddr*)&addr, sizeof(addr));
  450. xmove_fd(fd, netlink_fd);
  451. }
  452. write_pidfile(pidfile_name);
  453. /* this can't be moved before socket creation */
  454. if (!(opts & FLAG_NO_SYSLOG)) {
  455. openlog(applet_name, 0, LOG_DAEMON);
  456. logmode |= LOGMODE_SYSLOG;
  457. }
  458. bb_signals(0
  459. | (1 << SIGINT )
  460. | (1 << SIGTERM)
  461. | (1 << SIGQUIT)
  462. | (1 << SIGHUP ) /* why we ignore it? */
  463. /* | (1 << SIGCHLD) - run_script does not use it anymore */
  464. , record_signo);
  465. bb_error_msg("started: %s", bb_banner);
  466. if (opts & FLAG_MONITOR) {
  467. struct ifreq ifrequest;
  468. set_ifreq_to_ifname(&ifrequest);
  469. G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0);
  470. }
  471. if (G.iface_exists)
  472. maybe_up_new_iface();
  473. iface_status = detect_link();
  474. if (iface_status == IFSTATUS_ERR)
  475. goto exiting;
  476. iface_status_str = strstatus(iface_status);
  477. if (opts & FLAG_MONITOR) {
  478. bb_error_msg("interface %s",
  479. G.iface_exists ? "exists"
  480. : "doesn't exist, waiting");
  481. }
  482. /* else we assume it always exists, but don't mislead user
  483. * by potentially lying that it really exists */
  484. if (G.iface_exists) {
  485. bb_error_msg("link is %s", iface_status_str);
  486. }
  487. if ((!(opts & FLAG_NO_STARTUP)
  488. && iface_status == IFSTATUS_UP
  489. )
  490. || (opts & FLAG_INITIAL_DOWN)
  491. ) {
  492. if (run_script(iface_status_str) != 0)
  493. goto exiting;
  494. }
  495. /* Main loop */
  496. netlink_pollfd[0].fd = netlink_fd;
  497. netlink_pollfd[0].events = POLLIN;
  498. delay_time = 0;
  499. while (1) {
  500. int iface_status_old;
  501. int iface_exists_old;
  502. switch (bb_got_signal) {
  503. case SIGINT:
  504. case SIGTERM:
  505. bb_got_signal = 0;
  506. goto cleanup;
  507. case SIGQUIT:
  508. bb_got_signal = 0;
  509. goto exiting;
  510. default:
  511. bb_got_signal = 0;
  512. break;
  513. }
  514. if (poll(netlink_pollfd,
  515. (opts & FLAG_MONITOR) ? 1 : 0,
  516. G.poll_time
  517. ) < 0
  518. ) {
  519. if (errno == EINTR)
  520. continue;
  521. bb_perror_msg("poll");
  522. goto exiting;
  523. }
  524. iface_status_old = iface_status;
  525. iface_exists_old = G.iface_exists;
  526. if ((opts & FLAG_MONITOR)
  527. && (netlink_pollfd[0].revents & POLLIN)
  528. ) {
  529. G.iface_exists = check_existence_through_netlink();
  530. if (G.iface_exists < 0) /* error */
  531. goto exiting;
  532. if (iface_exists_old != G.iface_exists) {
  533. bb_error_msg("interface %sappeared",
  534. G.iface_exists ? "" : "dis");
  535. if (G.iface_exists)
  536. maybe_up_new_iface();
  537. }
  538. }
  539. /* note: if !G.iface_exists, returns DOWN */
  540. iface_status = detect_link();
  541. if (iface_status == IFSTATUS_ERR) {
  542. if (!(opts & FLAG_MONITOR))
  543. goto exiting;
  544. iface_status = IFSTATUS_DOWN;
  545. }
  546. iface_status_str = strstatus(iface_status);
  547. if (iface_status_old != iface_status) {
  548. bb_error_msg("link is %s", iface_status_str);
  549. if (delay_time) {
  550. /* link restored its old status before
  551. * we run script. don't run the script: */
  552. delay_time = 0;
  553. } else {
  554. delay_time = monotonic_sec();
  555. if (iface_status == IFSTATUS_UP)
  556. delay_time += G.delay_up;
  557. if (iface_status == IFSTATUS_DOWN)
  558. delay_time += G.delay_down;
  559. if (delay_time == 0)
  560. delay_time++;
  561. }
  562. }
  563. if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) {
  564. delay_time = 0;
  565. if (run_script(iface_status_str) != 0)
  566. goto exiting;
  567. }
  568. } /* while (1) */
  569. cleanup:
  570. if (!(opts & FLAG_NO_SHUTDOWN)
  571. && (iface_status == IFSTATUS_UP
  572. || (iface_status == IFSTATUS_DOWN && delay_time)
  573. )
  574. ) {
  575. setenv(IFPLUGD_ENV_PREVIOUS, strstatus(iface_status), 1);
  576. setenv(IFPLUGD_ENV_CURRENT, strstatus(-1), 1);
  577. run_script("down\0up"); /* reusing string */
  578. }
  579. exiting:
  580. remove_pidfile(pidfile_name);
  581. bb_error_msg_and_die("exiting");
  582. }