ifplugd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 tarball for details.
  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 { // api mode
  65. API_AUTO = 'a',
  66. API_ETHTOOL = 'e',
  67. API_MII = 'm',
  68. API_PRIVATE = 'p',
  69. API_WLAN = 'w',
  70. API_IFF = 'i',
  71. };
  72. enum { // interface status
  73. IFSTATUS_ERR = -1,
  74. IFSTATUS_DOWN = 0,
  75. IFSTATUS_UP = 1,
  76. };
  77. enum { // constant fds
  78. ioctl_fd = 3,
  79. netlink_fd = 4,
  80. };
  81. struct globals {
  82. smallint iface_last_status;
  83. smallint iface_prev_status;
  84. smallint iface_exists;
  85. /* Used in getopt32, must have sizeof == sizeof(int) */
  86. unsigned poll_time;
  87. unsigned delay_up;
  88. unsigned delay_down;
  89. const char *iface;
  90. const char *api_mode;
  91. const char *script_name;
  92. const char *extra_arg;
  93. smallint (*detect_link_func)(void);
  94. smallint (*cached_detect_link_func)(void);
  95. };
  96. #define G (*ptr_to_globals)
  97. #define INIT_G() do { \
  98. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  99. G.iface_last_status = -1; \
  100. G.iface_exists = 1; \
  101. G.poll_time = 1; \
  102. G.delay_down = 5; \
  103. G.iface = "eth0"; \
  104. G.api_mode = "a"; \
  105. G.script_name = "/etc/ifplugd/ifplugd.action"; \
  106. } while (0)
  107. static const char *strstatus(int status)
  108. {
  109. if (status == IFSTATUS_ERR)
  110. return "error";
  111. return "down\0up" + (status * 5);
  112. }
  113. static int run_script(const char *action)
  114. {
  115. char *env_PREVIOUS, *env_CURRENT;
  116. char *argv[5];
  117. int r;
  118. bb_error_msg("executing '%s %s %s'", G.script_name, G.iface, action);
  119. argv[0] = (char*) G.script_name;
  120. argv[1] = (char*) G.iface;
  121. argv[2] = (char*) action;
  122. argv[3] = (char*) G.extra_arg;
  123. argv[4] = NULL;
  124. env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status));
  125. putenv(env_PREVIOUS);
  126. env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status));
  127. putenv(env_CURRENT);
  128. /* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */
  129. r = spawn_and_wait(argv);
  130. unsetenv(IFPLUGD_ENV_PREVIOUS);
  131. unsetenv(IFPLUGD_ENV_CURRENT);
  132. free(env_PREVIOUS);
  133. free(env_CURRENT);
  134. bb_error_msg("exit code: %d", r & 0xff);
  135. return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r;
  136. }
  137. static int network_ioctl(int request, void* data, const char *errmsg)
  138. {
  139. int r = ioctl(ioctl_fd, request, data);
  140. if (r < 0 && errmsg)
  141. bb_perror_msg("%s failed", errmsg);
  142. return r;
  143. }
  144. static void set_ifreq_to_ifname(struct ifreq *ifreq)
  145. {
  146. memset(ifreq, 0, sizeof(struct ifreq));
  147. strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface);
  148. }
  149. static void up_iface(void)
  150. {
  151. struct ifreq ifrequest;
  152. if (!G.iface_exists)
  153. return;
  154. set_ifreq_to_ifname(&ifrequest);
  155. if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) {
  156. G.iface_exists = 0;
  157. return;
  158. }
  159. if (!(ifrequest.ifr_flags & IFF_UP)) {
  160. ifrequest.ifr_flags |= IFF_UP;
  161. /* Let user know we mess up with interface */
  162. bb_error_msg("upping interface");
  163. if (network_ioctl(SIOCSIFFLAGS, &ifrequest, "setting interface flags") < 0)
  164. xfunc_die();
  165. }
  166. #if 0 /* why do we mess with IP addr? It's not our business */
  167. if (network_ioctl(SIOCGIFADDR, &ifrequest, "can't get interface address") < 0) {
  168. } else if (ifrequest.ifr_addr.sa_family != AF_INET) {
  169. bb_perror_msg("the interface is not IP-based");
  170. } else {
  171. ((struct sockaddr_in*)(&ifrequest.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
  172. network_ioctl(SIOCSIFADDR, &ifrequest, "can't set interface address");
  173. }
  174. network_ioctl(SIOCGIFFLAGS, &ifrequest, "can't get interface flags");
  175. #endif
  176. }
  177. static void maybe_up_new_iface(void)
  178. {
  179. if (!(option_mask32 & FLAG_NO_AUTO))
  180. up_iface();
  181. #if 0 /* bloat */
  182. struct ifreq ifrequest;
  183. struct ethtool_drvinfo driver_info;
  184. set_ifreq_to_ifname(&ifrequest);
  185. driver_info.cmd = ETHTOOL_GDRVINFO;
  186. ifrequest.ifr_data = &driver_info;
  187. if (network_ioctl(SIOCETHTOOL, &ifrequest, NULL) == 0) {
  188. char buf[sizeof("/xx:xx:xx:xx:xx:xx")];
  189. /* Get MAC */
  190. buf[0] = '\0';
  191. set_ifreq_to_ifname(&ifrequest);
  192. if (network_ioctl(SIOCGIFHWADDR, &ifrequest, NULL) == 0) {
  193. sprintf(buf, "/%02X:%02X:%02X:%02X:%02X:%02X",
  194. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[0]),
  195. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[1]),
  196. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[2]),
  197. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[3]),
  198. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[4]),
  199. (uint8_t)(ifrequest.ifr_hwaddr.sa_data[5]));
  200. }
  201. bb_error_msg("using interface %s%s with driver<%s> (version: %s)",
  202. G.iface, buf, driver_info.driver, driver_info.version);
  203. }
  204. #endif
  205. G.cached_detect_link_func = NULL;
  206. }
  207. static smallint detect_link_mii(void)
  208. {
  209. struct ifreq ifreq;
  210. struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
  211. set_ifreq_to_ifname(&ifreq);
  212. if (network_ioctl(SIOCGMIIPHY, &ifreq, "SIOCGMIIPHY") < 0) {
  213. return IFSTATUS_ERR;
  214. }
  215. mii->reg_num = 1;
  216. if (network_ioctl(SIOCGMIIREG, &ifreq, "SIOCGMIIREG") < 0) {
  217. return IFSTATUS_ERR;
  218. }
  219. return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
  220. }
  221. static smallint detect_link_priv(void)
  222. {
  223. struct ifreq ifreq;
  224. struct mii_ioctl_data *mii = (void *)&ifreq.ifr_data;
  225. set_ifreq_to_ifname(&ifreq);
  226. if (network_ioctl(SIOCDEVPRIVATE, &ifreq, "SIOCDEVPRIVATE") < 0) {
  227. return IFSTATUS_ERR;
  228. }
  229. mii->reg_num = 1;
  230. if (network_ioctl(SIOCDEVPRIVATE+1, &ifreq, "SIOCDEVPRIVATE+1") < 0) {
  231. return IFSTATUS_ERR;
  232. }
  233. return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
  234. }
  235. static smallint detect_link_ethtool(void)
  236. {
  237. struct ifreq ifreq;
  238. struct ethtool_value edata;
  239. set_ifreq_to_ifname(&ifreq);
  240. edata.cmd = ETHTOOL_GLINK;
  241. ifreq.ifr_data = (void*) &edata;
  242. if (network_ioctl(SIOCETHTOOL, &ifreq, "ETHTOOL_GLINK") < 0) {
  243. return IFSTATUS_ERR;
  244. }
  245. return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN;
  246. }
  247. static smallint detect_link_iff(void)
  248. {
  249. struct ifreq ifreq;
  250. set_ifreq_to_ifname(&ifreq);
  251. if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) {
  252. return IFSTATUS_ERR;
  253. }
  254. /* If IFF_UP is not set (interface is down), IFF_RUNNING is never set
  255. * regardless of link status. Simply continue to report last status -
  256. * no point in reporting spurious link downs if interface is disabled
  257. * by admin. When/if it will be brought up,
  258. * we'll report real link status.
  259. */
  260. if (!(ifreq.ifr_flags & IFF_UP) && G.iface_last_status != IFSTATUS_ERR)
  261. return G.iface_last_status;
  262. return (ifreq.ifr_flags & IFF_RUNNING) ? IFSTATUS_UP : IFSTATUS_DOWN;
  263. }
  264. static smallint detect_link_wlan(void)
  265. {
  266. int i;
  267. struct iwreq iwrequest;
  268. uint8_t mac[ETH_ALEN];
  269. memset(&iwrequest, 0, sizeof(iwrequest));
  270. strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface);
  271. if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) {
  272. return IFSTATUS_ERR;
  273. }
  274. memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN);
  275. if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) {
  276. for (i = 1; i < ETH_ALEN; ++i) {
  277. if (mac[i] != mac[0])
  278. return IFSTATUS_UP;
  279. }
  280. return IFSTATUS_DOWN;
  281. }
  282. return IFSTATUS_UP;
  283. }
  284. static smallint detect_link_auto(void)
  285. {
  286. static const struct {
  287. const char *name;
  288. smallint (*func)(void);
  289. } method[] = {
  290. { "SIOCETHTOOL" , &detect_link_ethtool },
  291. { "SIOCGMIIPHY" , &detect_link_mii },
  292. { "SIOCDEVPRIVATE" , &detect_link_priv },
  293. { "wireless extension", &detect_link_wlan },
  294. { "IFF_RUNNING" , &detect_link_iff },
  295. };
  296. int i;
  297. smallint iface_status;
  298. smallint sv_logmode;
  299. if (G.cached_detect_link_func) {
  300. iface_status = G.cached_detect_link_func();
  301. if (iface_status != IFSTATUS_ERR)
  302. return iface_status;
  303. }
  304. sv_logmode = logmode;
  305. for (i = 0; i < ARRAY_SIZE(method); i++) {
  306. logmode = LOGMODE_NONE;
  307. iface_status = method[i].func();
  308. logmode = sv_logmode;
  309. if (iface_status != IFSTATUS_ERR) {
  310. G.cached_detect_link_func = method[i].func;
  311. bb_error_msg("using %s detection mode", method[i].name);
  312. break;
  313. }
  314. }
  315. return iface_status;
  316. }
  317. static smallint detect_link(void)
  318. {
  319. smallint status;
  320. if (!G.iface_exists)
  321. return (option_mask32 & FLAG_MONITOR) ? IFSTATUS_DOWN : IFSTATUS_ERR;
  322. /* Some drivers can't detect link status when the interface is down.
  323. * I imagine detect_link_iff() is the most vulnerable.
  324. * That's why -a "noauto" in an option, not a hardwired behavior.
  325. */
  326. if (!(option_mask32 & FLAG_NO_AUTO))
  327. up_iface();
  328. status = G.detect_link_func();
  329. if (status == IFSTATUS_ERR) {
  330. if (option_mask32 & FLAG_IGNORE_FAIL)
  331. status = IFSTATUS_DOWN;
  332. if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE)
  333. status = IFSTATUS_UP;
  334. }
  335. if (status == IFSTATUS_ERR
  336. && G.detect_link_func == detect_link_auto
  337. ) {
  338. bb_error_msg("can't detect link status");
  339. }
  340. if (status != G.iface_last_status) {
  341. G.iface_prev_status = G.iface_last_status;
  342. G.iface_last_status = status;
  343. }
  344. return status;
  345. }
  346. static NOINLINE int check_existence_through_netlink(void)
  347. {
  348. int iface_len;
  349. char replybuf[1024];
  350. iface_len = strlen(G.iface);
  351. while (1) {
  352. struct nlmsghdr *mhdr;
  353. ssize_t bytes;
  354. bytes = recv(netlink_fd, &replybuf, sizeof(replybuf), MSG_DONTWAIT);
  355. if (bytes < 0) {
  356. if (errno == EAGAIN)
  357. return G.iface_exists;
  358. if (errno == EINTR)
  359. continue;
  360. bb_perror_msg("netlink: recv");
  361. return -1;
  362. }
  363. mhdr = (struct nlmsghdr*)replybuf;
  364. while (bytes > 0) {
  365. if (!NLMSG_OK(mhdr, bytes)) {
  366. bb_error_msg("netlink packet too small or truncated");
  367. return -1;
  368. }
  369. if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) {
  370. struct rtattr *attr;
  371. int attr_len;
  372. if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
  373. bb_error_msg("netlink packet too small or truncated");
  374. return -1;
  375. }
  376. attr = IFLA_RTA(NLMSG_DATA(mhdr));
  377. attr_len = IFLA_PAYLOAD(mhdr);
  378. while (RTA_OK(attr, attr_len)) {
  379. if (attr->rta_type == IFLA_IFNAME) {
  380. int len = RTA_PAYLOAD(attr);
  381. if (len > IFNAMSIZ)
  382. len = IFNAMSIZ;
  383. if (iface_len <= len
  384. && strncmp(G.iface, RTA_DATA(attr), len) == 0
  385. ) {
  386. G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK);
  387. }
  388. }
  389. attr = RTA_NEXT(attr, attr_len);
  390. }
  391. }
  392. mhdr = NLMSG_NEXT(mhdr, bytes);
  393. }
  394. }
  395. return G.iface_exists;
  396. }
  397. static NOINLINE int netlink_open(void)
  398. {
  399. int fd;
  400. struct sockaddr_nl addr;
  401. fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
  402. memset(&addr, 0, sizeof(addr));
  403. addr.nl_family = AF_NETLINK;
  404. addr.nl_groups = RTMGRP_LINK;
  405. addr.nl_pid = getpid();
  406. xbind(fd, (struct sockaddr*)&addr, sizeof(addr));
  407. return fd;
  408. }
  409. #if ENABLE_FEATURE_PIDFILE
  410. static NOINLINE pid_t read_pid(const char *filename)
  411. {
  412. int len;
  413. char buf[128];
  414. len = open_read_close(filename, buf, 127);
  415. if (len > 0) {
  416. buf[len] = '\0';
  417. /* returns ULONG_MAX on error => -1 */
  418. return bb_strtoul(buf, NULL, 10);
  419. }
  420. return 0;
  421. }
  422. #endif
  423. int ifplugd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  424. int ifplugd_main(int argc UNUSED_PARAM, char **argv)
  425. {
  426. int iface_status;
  427. int delay_time;
  428. const char *iface_status_str;
  429. struct pollfd netlink_pollfd[1];
  430. unsigned opts;
  431. #if ENABLE_FEATURE_PIDFILE
  432. char *pidfile_name;
  433. pid_t pid_from_pidfile;
  434. #endif
  435. INIT_G();
  436. opt_complementary = "t+:u+:d+";
  437. opts = getopt32(argv, OPTION_STR,
  438. &G.iface, &G.script_name, &G.poll_time, &G.delay_up,
  439. &G.delay_down, &G.api_mode, &G.extra_arg);
  440. G.poll_time *= 1000;
  441. applet_name = xasprintf("ifplugd(%s)", G.iface);
  442. #if ENABLE_FEATURE_PIDFILE
  443. pidfile_name = xasprintf(_PATH_VARRUN"ifplugd.%s.pid", G.iface);
  444. pid_from_pidfile = read_pid(pidfile_name);
  445. if (opts & FLAG_KILL) {
  446. if (pid_from_pidfile > 0)
  447. kill(pid_from_pidfile, SIGQUIT);
  448. return EXIT_SUCCESS;
  449. }
  450. if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0)
  451. bb_error_msg_and_die("daemon already running");
  452. #endif
  453. switch (G.api_mode[0]) {
  454. case API_AUTO:
  455. G.detect_link_func = detect_link_auto;
  456. break;
  457. case API_ETHTOOL:
  458. G.detect_link_func = detect_link_ethtool;
  459. break;
  460. case API_MII:
  461. G.detect_link_func = detect_link_mii;
  462. break;
  463. case API_PRIVATE:
  464. G.detect_link_func = detect_link_priv;
  465. break;
  466. case API_WLAN:
  467. G.detect_link_func = detect_link_wlan;
  468. break;
  469. case API_IFF:
  470. G.detect_link_func = detect_link_iff;
  471. break;
  472. default:
  473. bb_error_msg_and_die("unknown API mode '%s'", G.api_mode);
  474. }
  475. if (!(opts & FLAG_NO_DAEMON))
  476. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  477. xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
  478. if (opts & FLAG_MONITOR) {
  479. xmove_fd(netlink_open(), netlink_fd);
  480. }
  481. write_pidfile(pidfile_name);
  482. /* this can't be moved before socket creation */
  483. if (!(opts & FLAG_NO_SYSLOG)) {
  484. openlog(applet_name, 0, LOG_DAEMON);
  485. logmode |= LOGMODE_SYSLOG;
  486. }
  487. bb_signals(0
  488. | (1 << SIGINT )
  489. | (1 << SIGTERM)
  490. | (1 << SIGQUIT)
  491. | (1 << SIGHUP ) /* why we ignore it? */
  492. /* | (1 << SIGCHLD) - run_script does not use it anymore */
  493. , record_signo);
  494. bb_error_msg("started: %s", bb_banner);
  495. if (opts & FLAG_MONITOR) {
  496. struct ifreq ifrequest;
  497. set_ifreq_to_ifname(&ifrequest);
  498. G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0);
  499. }
  500. if (G.iface_exists)
  501. maybe_up_new_iface();
  502. iface_status = detect_link();
  503. if (iface_status == IFSTATUS_ERR)
  504. goto exiting;
  505. iface_status_str = strstatus(iface_status);
  506. if (opts & FLAG_MONITOR) {
  507. bb_error_msg("interface %s",
  508. G.iface_exists ? "exists"
  509. : "doesn't exist, waiting");
  510. }
  511. /* else we assume it always exists, but don't mislead user
  512. * by potentially lying that it really exists */
  513. if (G.iface_exists) {
  514. bb_error_msg("link is %s", iface_status_str);
  515. }
  516. if ((!(opts & FLAG_NO_STARTUP)
  517. && iface_status == IFSTATUS_UP
  518. )
  519. || (opts & FLAG_INITIAL_DOWN)
  520. ) {
  521. if (run_script(iface_status_str) != 0)
  522. goto exiting;
  523. }
  524. /* Main loop */
  525. netlink_pollfd[0].fd = netlink_fd;
  526. netlink_pollfd[0].events = POLLIN;
  527. delay_time = 0;
  528. while (1) {
  529. int iface_status_old;
  530. int iface_exists_old;
  531. switch (bb_got_signal) {
  532. case SIGINT:
  533. case SIGTERM:
  534. bb_got_signal = 0;
  535. goto cleanup;
  536. case SIGQUIT:
  537. bb_got_signal = 0;
  538. goto exiting;
  539. default:
  540. bb_got_signal = 0;
  541. break;
  542. }
  543. if (poll(netlink_pollfd,
  544. (opts & FLAG_MONITOR) ? 1 : 0,
  545. G.poll_time
  546. ) < 0
  547. ) {
  548. if (errno == EINTR)
  549. continue;
  550. bb_perror_msg("poll");
  551. goto exiting;
  552. }
  553. iface_status_old = iface_status;
  554. iface_exists_old = G.iface_exists;
  555. if ((opts & FLAG_MONITOR)
  556. && (netlink_pollfd[0].revents & POLLIN)
  557. ) {
  558. G.iface_exists = check_existence_through_netlink();
  559. if (G.iface_exists < 0) /* error */
  560. goto exiting;
  561. if (iface_exists_old != G.iface_exists) {
  562. bb_error_msg("interface %sappeared",
  563. G.iface_exists ? "" : "dis");
  564. if (G.iface_exists)
  565. maybe_up_new_iface();
  566. }
  567. }
  568. /* note: if !G.iface_exists, returns DOWN */
  569. iface_status = detect_link();
  570. if (iface_status == IFSTATUS_ERR) {
  571. if (!(opts & FLAG_MONITOR))
  572. goto exiting;
  573. iface_status = IFSTATUS_DOWN;
  574. }
  575. iface_status_str = strstatus(iface_status);
  576. if (iface_status_old != iface_status) {
  577. bb_error_msg("link is %s", iface_status_str);
  578. if (delay_time) {
  579. /* link restored its old status before
  580. * we run script. don't run the script: */
  581. delay_time = 0;
  582. } else {
  583. delay_time = monotonic_sec();
  584. if (iface_status == IFSTATUS_UP)
  585. delay_time += G.delay_up;
  586. if (iface_status == IFSTATUS_DOWN)
  587. delay_time += G.delay_down;
  588. if (delay_time == 0)
  589. delay_time++;
  590. }
  591. }
  592. if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) {
  593. delay_time = 0;
  594. if (run_script(iface_status_str) != 0)
  595. goto exiting;
  596. }
  597. } /* while (1) */
  598. cleanup:
  599. if (!(opts & FLAG_NO_SHUTDOWN)
  600. && (iface_status == IFSTATUS_UP
  601. || (iface_status == IFSTATUS_DOWN && delay_time)
  602. )
  603. ) {
  604. setenv(IFPLUGD_ENV_PREVIOUS, strstatus(iface_status), 1);
  605. setenv(IFPLUGD_ENV_CURRENT, strstatus(-1), 1);
  606. run_script("down\0up"); /* reusing string */
  607. }
  608. exiting:
  609. remove_pidfile(pidfile_name);
  610. bb_error_msg_and_die("exiting");
  611. }