2009-04-26 06:38:51 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2010-03-30 19:19:57 +05:30
|
|
|
* ifplugd for busybox, based on ifplugd 0.28 (written by Lennart Poettering).
|
2009-04-26 06:38:51 +05:30
|
|
|
*
|
2009-04-26 06:47:44 +05:30
|
|
|
* Copyright (C) 2009 Maksym Kryzhanovskyy <xmaks@email.cz>
|
2009-04-26 06:38:51 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-04-26 06:38:51 +05:30
|
|
|
*/
|
2016-11-23 13:35:14 +05:30
|
|
|
//config:config IFPLUGD
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "ifplugd (10 kb)"
|
2016-11-23 13:35:14 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Network interface plug detection daemon.
|
2016-11-23 13:35:14 +05:30
|
|
|
|
|
|
|
//applet:IF_IFPLUGD(APPLET(ifplugd, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_IFPLUGD) += ifplugd.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define ifplugd_trivial_usage
|
|
|
|
//usage: "[OPTIONS]"
|
|
|
|
//usage:#define ifplugd_full_usage "\n\n"
|
|
|
|
//usage: "Network interface plug detection daemon\n"
|
|
|
|
//usage: "\n -n Don't daemonize"
|
|
|
|
//usage: "\n -s Don't log to syslog"
|
|
|
|
//usage: "\n -i IFACE Interface"
|
|
|
|
//usage: "\n -f/-F Treat link detection error as link down/link up"
|
|
|
|
//usage: "\n (otherwise exit on error)"
|
|
|
|
//usage: "\n -a Don't up interface at each link probe"
|
|
|
|
//usage: "\n -M Monitor creation/destruction of interface"
|
|
|
|
//usage: "\n (otherwise it must exist)"
|
|
|
|
//usage: "\n -r PROG Script to run"
|
|
|
|
//usage: "\n -x ARG Extra argument for script"
|
|
|
|
//usage: "\n -I Don't exit on nonzero exit code from script"
|
2012-06-03 13:56:16 +05:30
|
|
|
//usage: "\n -p Don't run \"up\" script on startup"
|
|
|
|
//usage: "\n -q Don't run \"down\" script on exit"
|
|
|
|
//usage: "\n -l Always run script on startup"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -t SECS Poll time in seconds"
|
|
|
|
//usage: "\n -u SECS Delay before running script after link up"
|
|
|
|
//usage: "\n -d SECS Delay after link down"
|
|
|
|
//usage: "\n -m MODE API mode (mii, priv, ethtool, wlan, iff, auto)"
|
|
|
|
//usage: "\n -k Kill running daemon"
|
|
|
|
|
2009-04-26 06:38:51 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2009-08-08 06:50:12 +05:30
|
|
|
#include "fix_u32.h"
|
2009-04-26 06:38:51 +05:30
|
|
|
#include <linux/if.h>
|
2009-05-01 12:34:25 +05:30
|
|
|
#include <linux/mii.h>
|
2009-04-26 06:38:51 +05:30
|
|
|
#include <linux/ethtool.h>
|
2011-06-10 08:47:59 +05:30
|
|
|
#ifdef HAVE_NET_ETHERNET_H
|
2015-10-05 14:45:43 +05:30
|
|
|
/* musl breakage:
|
|
|
|
* In file included from /usr/include/net/ethernet.h:10,
|
|
|
|
* from networking/ifplugd.c:41:
|
|
|
|
* /usr/include/netinet/if_ether.h:96: error: redefinition of 'struct ethhdr'
|
|
|
|
*
|
|
|
|
* Build succeeds without it on musl. Commented it out.
|
|
|
|
* If on your system you need it, consider removing <linux/ethtool.h>
|
|
|
|
* and copy-pasting its definitions here (<linux/ethtool.h> is what pulls in
|
|
|
|
* conflicting definition of struct ethhdr on musl).
|
|
|
|
*/
|
|
|
|
/* # include <net/ethernet.h> */
|
2011-06-10 08:47:59 +05:30
|
|
|
#endif
|
2009-04-26 06:38:51 +05:30
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#define __user
|
|
|
|
#include <linux/wireless.h>
|
|
|
|
|
2015-10-07 05:32:56 +05:30
|
|
|
#ifndef ETH_ALEN
|
|
|
|
# define ETH_ALEN 6
|
|
|
|
#endif
|
|
|
|
|
2009-04-26 06:47:44 +05:30
|
|
|
/*
|
2010-03-30 19:19:57 +05:30
|
|
|
From initial port to busybox, removed most of the redundancy by
|
|
|
|
converting implementation of a polymorphic interface to the strict
|
|
|
|
functional style. The main role is run a script when link state
|
|
|
|
changed, other activities like audio signal or detailed reports
|
|
|
|
are on the script itself.
|
2009-04-26 06:47:44 +05:30
|
|
|
|
|
|
|
One questionable point of the design is netlink usage:
|
|
|
|
|
|
|
|
We have 1 second timeout by default to poll the link status,
|
|
|
|
it is short enough so that there are no real benefits in
|
|
|
|
using netlink to get "instantaneous" interface creation/deletion
|
|
|
|
notifications. We can check for interface existence by just
|
|
|
|
doing some fast ioctl using its name.
|
|
|
|
|
|
|
|
Netlink code then can be just dropped (1k or more?)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-04-26 06:38:51 +05:30
|
|
|
#define IFPLUGD_ENV_PREVIOUS "IFPLUGD_PREVIOUS"
|
|
|
|
#define IFPLUGD_ENV_CURRENT "IFPLUGD_CURRENT"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FLAG_NO_AUTO = 1 << 0, // -a, Do not enable interface automatically
|
|
|
|
FLAG_NO_DAEMON = 1 << 1, // -n, Do not daemonize
|
|
|
|
FLAG_NO_SYSLOG = 1 << 2, // -s, Do not use syslog, use stderr instead
|
|
|
|
FLAG_IGNORE_FAIL = 1 << 3, // -f, Ignore detection failure, retry instead (failure is treated as DOWN)
|
|
|
|
FLAG_IGNORE_FAIL_POSITIVE = 1 << 4, // -F, Ignore detection failure, retry instead (failure is treated as UP)
|
|
|
|
FLAG_IFACE = 1 << 5, // -i, Specify ethernet interface
|
|
|
|
FLAG_RUN = 1 << 6, // -r, Specify program to execute
|
|
|
|
FLAG_IGNORE_RETVAL = 1 << 7, // -I, Don't exit on nonzero return value of program executed
|
|
|
|
FLAG_POLL_TIME = 1 << 8, // -t, Specify poll time in seconds
|
|
|
|
FLAG_DELAY_UP = 1 << 9, // -u, Specify delay for configuring interface
|
|
|
|
FLAG_DELAY_DOWN = 1 << 10, // -d, Specify delay for deconfiguring interface
|
|
|
|
FLAG_API_MODE = 1 << 11, // -m, Force API mode (mii, priv, ethtool, wlan, auto)
|
|
|
|
FLAG_NO_STARTUP = 1 << 12, // -p, Don't run script on daemon startup
|
|
|
|
FLAG_NO_SHUTDOWN = 1 << 13, // -q, Don't run script on daemon quit
|
|
|
|
FLAG_INITIAL_DOWN = 1 << 14, // -l, Run "down" script on startup if no cable is detected
|
|
|
|
FLAG_EXTRA_ARG = 1 << 15, // -x, Specify an extra argument for action script
|
|
|
|
FLAG_MONITOR = 1 << 16, // -M, Use interface monitoring
|
|
|
|
#if ENABLE_FEATURE_PIDFILE
|
|
|
|
FLAG_KILL = 1 << 17, // -k, Kill a running daemon
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#if ENABLE_FEATURE_PIDFILE
|
2016-07-07 01:28:02 +05:30
|
|
|
# define OPTION_STR "+ansfFi:r:It:+u:+d:+m:pqlx:Mk"
|
2009-04-26 06:38:51 +05:30
|
|
|
#else
|
2016-07-07 01:28:02 +05:30
|
|
|
# define OPTION_STR "+ansfFi:r:It:+u:+d:+m:pqlx:M"
|
2009-04-26 06:38:51 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
enum { // interface status
|
|
|
|
IFSTATUS_ERR = -1,
|
|
|
|
IFSTATUS_DOWN = 0,
|
|
|
|
IFSTATUS_UP = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum { // constant fds
|
|
|
|
ioctl_fd = 3,
|
|
|
|
netlink_fd = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
smallint iface_last_status;
|
2010-05-09 02:56:16 +05:30
|
|
|
smallint iface_prev_status;
|
2009-04-26 06:38:51 +05:30
|
|
|
smallint iface_exists;
|
2010-07-08 06:17:25 +05:30
|
|
|
smallint api_method_num;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
/* Used in getopt32, must have sizeof == sizeof(int) */
|
|
|
|
unsigned poll_time;
|
|
|
|
unsigned delay_up;
|
|
|
|
unsigned delay_down;
|
|
|
|
|
|
|
|
const char *iface;
|
|
|
|
const char *api_mode;
|
|
|
|
const char *script_name;
|
|
|
|
const char *extra_arg;
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
G.iface_last_status = -1; \
|
|
|
|
G.iface_exists = 1; \
|
|
|
|
G.poll_time = 1; \
|
|
|
|
G.delay_down = 5; \
|
|
|
|
G.iface = "eth0"; \
|
|
|
|
G.api_mode = "a"; \
|
|
|
|
G.script_name = "/etc/ifplugd/ifplugd.action"; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2010-07-22 05:48:05 +05:30
|
|
|
/* Utility routines */
|
2010-05-09 02:56:16 +05:30
|
|
|
|
2010-07-22 05:48:05 +05:30
|
|
|
static void set_ifreq_to_ifname(struct ifreq *ifreq)
|
2009-04-26 06:38:51 +05:30
|
|
|
{
|
2010-07-22 05:48:05 +05:30
|
|
|
memset(ifreq, 0, sizeof(struct ifreq));
|
|
|
|
strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface);
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
2010-01-08 16:57:57 +05:30
|
|
|
static int network_ioctl(int request, void* data, const char *errmsg)
|
2009-04-26 06:38:51 +05:30
|
|
|
{
|
2010-01-08 16:57:57 +05:30
|
|
|
int r = ioctl(ioctl_fd, request, data);
|
|
|
|
if (r < 0 && errmsg)
|
2010-03-29 12:39:05 +05:30
|
|
|
bb_perror_msg("%s failed", errmsg);
|
2010-01-08 16:57:57 +05:30
|
|
|
return r;
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
2010-07-22 05:48:05 +05:30
|
|
|
/* Link detection routines and table */
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
static smallint detect_link_mii(void)
|
|
|
|
{
|
2011-01-20 05:50:36 +05:30
|
|
|
/* char buffer instead of bona-fide struct avoids aliasing warning */
|
|
|
|
char buf[sizeof(struct ifreq)];
|
2011-01-20 15:59:00 +05:30
|
|
|
struct ifreq *const ifreq = (void *)buf;
|
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
set_ifreq_to_ifname(ifreq);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
if (network_ioctl(SIOCGMIIPHY, ifreq, "SIOCGMIIPHY") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:34:25 +05:30
|
|
|
mii->reg_num = 1;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
if (network_ioctl(SIOCGMIIREG, ifreq, "SIOCGMIIREG") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:34:25 +05:30
|
|
|
return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static smallint detect_link_priv(void)
|
|
|
|
{
|
2011-01-20 05:50:36 +05:30
|
|
|
/* char buffer instead of bona-fide struct avoids aliasing warning */
|
|
|
|
char buf[sizeof(struct ifreq)];
|
2011-01-20 15:59:00 +05:30
|
|
|
struct ifreq *const ifreq = (void *)buf;
|
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
set_ifreq_to_ifname(ifreq);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
if (network_ioctl(SIOCDEVPRIVATE, ifreq, "SIOCDEVPRIVATE") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:34:25 +05:30
|
|
|
mii->reg_num = 1;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2011-01-20 05:50:36 +05:30
|
|
|
if (network_ioctl(SIOCDEVPRIVATE+1, ifreq, "SIOCDEVPRIVATE+1") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:34:25 +05:30
|
|
|
return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static smallint detect_link_ethtool(void)
|
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
|
|
|
struct ethtool_value edata;
|
|
|
|
|
|
|
|
set_ifreq_to_ifname(&ifreq);
|
|
|
|
|
|
|
|
edata.cmd = ETHTOOL_GLINK;
|
2009-10-23 22:01:02 +05:30
|
|
|
ifreq.ifr_data = (void*) &edata;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2010-03-29 12:39:05 +05:30
|
|
|
if (network_ioctl(SIOCETHTOOL, &ifreq, "ETHTOOL_GLINK") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static smallint detect_link_iff(void)
|
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
|
|
|
|
|
|
|
set_ifreq_to_ifname(&ifreq);
|
|
|
|
|
2010-03-29 12:39:05 +05:30
|
|
|
if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2010-01-08 16:57:57 +05:30
|
|
|
/* If IFF_UP is not set (interface is down), IFF_RUNNING is never set
|
|
|
|
* regardless of link status. Simply continue to report last status -
|
|
|
|
* no point in reporting spurious link downs if interface is disabled
|
|
|
|
* by admin. When/if it will be brought up,
|
|
|
|
* we'll report real link status.
|
|
|
|
*/
|
|
|
|
if (!(ifreq.ifr_flags & IFF_UP) && G.iface_last_status != IFSTATUS_ERR)
|
|
|
|
return G.iface_last_status;
|
|
|
|
|
2009-04-26 06:38:51 +05:30
|
|
|
return (ifreq.ifr_flags & IFF_RUNNING) ? IFSTATUS_UP : IFSTATUS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static smallint detect_link_wlan(void)
|
|
|
|
{
|
2010-03-29 12:39:05 +05:30
|
|
|
int i;
|
2009-04-26 06:38:51 +05:30
|
|
|
struct iwreq iwrequest;
|
|
|
|
uint8_t mac[ETH_ALEN];
|
|
|
|
|
2010-03-29 12:39:05 +05:30
|
|
|
memset(&iwrequest, 0, sizeof(iwrequest));
|
2009-04-26 07:13:36 +05:30
|
|
|
strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2010-03-29 12:39:05 +05:30
|
|
|
if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) {
|
2009-04-26 06:38:51 +05:30
|
|
|
return IFSTATUS_ERR;
|
|
|
|
}
|
|
|
|
|
2010-03-29 12:39:05 +05:30
|
|
|
memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) {
|
2010-03-29 12:39:05 +05:30
|
|
|
for (i = 1; i < ETH_ALEN; ++i) {
|
2009-04-26 06:38:51 +05:30
|
|
|
if (mac[i] != mac[0])
|
|
|
|
return IFSTATUS_UP;
|
|
|
|
}
|
|
|
|
return IFSTATUS_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IFSTATUS_UP;
|
|
|
|
}
|
|
|
|
|
2010-07-22 05:48:05 +05:30
|
|
|
enum { // api mode
|
|
|
|
API_ETHTOOL, // 'e'
|
|
|
|
API_MII, // 'm'
|
|
|
|
API_PRIVATE, // 'p'
|
|
|
|
API_WLAN, // 'w'
|
|
|
|
API_IFF, // 'i'
|
|
|
|
API_AUTO, // 'a'
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char api_modes[] ALIGN1 = "empwia";
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
smallint (*func)(void);
|
|
|
|
} method_table[] = {
|
|
|
|
{ "SIOCETHTOOL" , &detect_link_ethtool },
|
|
|
|
{ "SIOCGMIIPHY" , &detect_link_mii },
|
|
|
|
{ "SIOCDEVPRIVATE" , &detect_link_priv },
|
|
|
|
{ "wireless extension", &detect_link_wlan },
|
|
|
|
{ "IFF_RUNNING" , &detect_link_iff },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *strstatus(int status)
|
|
|
|
{
|
|
|
|
if (status == IFSTATUS_ERR)
|
|
|
|
return "error";
|
|
|
|
return "down\0up" + (status * 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_script(const char *action)
|
|
|
|
{
|
|
|
|
char *env_PREVIOUS, *env_CURRENT;
|
|
|
|
char *argv[5];
|
|
|
|
int r;
|
|
|
|
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("executing '%s %s %s'", G.script_name, G.iface, action);
|
2010-07-22 05:48:05 +05:30
|
|
|
|
|
|
|
argv[0] = (char*) G.script_name;
|
|
|
|
argv[1] = (char*) G.iface;
|
|
|
|
argv[2] = (char*) action;
|
|
|
|
argv[3] = (char*) G.extra_arg;
|
|
|
|
argv[4] = NULL;
|
|
|
|
|
|
|
|
env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status));
|
|
|
|
putenv(env_PREVIOUS);
|
|
|
|
env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status));
|
|
|
|
putenv(env_CURRENT);
|
|
|
|
|
|
|
|
/* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */
|
|
|
|
r = spawn_and_wait(argv);
|
|
|
|
|
2017-07-22 06:34:20 +05:30
|
|
|
bb_unsetenv_and_free(env_PREVIOUS);
|
|
|
|
bb_unsetenv_and_free(env_CURRENT);
|
2010-07-22 05:48:05 +05:30
|
|
|
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("exit code: %d", r & 0xff);
|
2010-07-22 05:48:05 +05:30
|
|
|
return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void up_iface(void)
|
|
|
|
{
|
|
|
|
struct ifreq ifrequest;
|
|
|
|
|
|
|
|
if (!G.iface_exists)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_ifreq_to_ifname(&ifrequest);
|
|
|
|
if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) {
|
|
|
|
G.iface_exists = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ifrequest.ifr_flags & IFF_UP)) {
|
|
|
|
ifrequest.ifr_flags |= IFF_UP;
|
|
|
|
/* Let user know we mess up with interface */
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_info_msg("upping interface");
|
2016-08-17 00:09:52 +05:30
|
|
|
if (network_ioctl(SIOCSIFFLAGS, &ifrequest, "setting interface flags") < 0) {
|
2018-03-29 17:42:31 +05:30
|
|
|
if (errno != ENODEV && errno != EADDRNOTAVAIL)
|
2016-08-17 00:09:52 +05:30
|
|
|
xfunc_die();
|
|
|
|
G.iface_exists = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-22 05:48:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 /* why do we mess with IP addr? It's not our business */
|
|
|
|
if (network_ioctl(SIOCGIFADDR, &ifrequest, "can't get interface address") < 0) {
|
|
|
|
} else if (ifrequest.ifr_addr.sa_family != AF_INET) {
|
|
|
|
bb_perror_msg("the interface is not IP-based");
|
|
|
|
} else {
|
|
|
|
((struct sockaddr_in*)(&ifrequest.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
network_ioctl(SIOCSIFADDR, &ifrequest, "can't set interface address");
|
|
|
|
}
|
|
|
|
network_ioctl(SIOCGIFFLAGS, &ifrequest, "can't get interface flags");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void maybe_up_new_iface(void)
|
|
|
|
{
|
|
|
|
if (!(option_mask32 & FLAG_NO_AUTO))
|
|
|
|
up_iface();
|
|
|
|
|
|
|
|
#if 0 /* bloat */
|
|
|
|
struct ifreq ifrequest;
|
|
|
|
struct ethtool_drvinfo driver_info;
|
|
|
|
|
|
|
|
set_ifreq_to_ifname(&ifrequest);
|
|
|
|
driver_info.cmd = ETHTOOL_GDRVINFO;
|
|
|
|
ifrequest.ifr_data = &driver_info;
|
|
|
|
if (network_ioctl(SIOCETHTOOL, &ifrequest, NULL) == 0) {
|
|
|
|
char buf[sizeof("/xx:xx:xx:xx:xx:xx")];
|
|
|
|
|
|
|
|
/* Get MAC */
|
|
|
|
buf[0] = '\0';
|
|
|
|
set_ifreq_to_ifname(&ifrequest);
|
|
|
|
if (network_ioctl(SIOCGIFHWADDR, &ifrequest, NULL) == 0) {
|
|
|
|
sprintf(buf, "/%02X:%02X:%02X:%02X:%02X:%02X",
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[0]),
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[1]),
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[2]),
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[3]),
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[4]),
|
|
|
|
(uint8_t)(ifrequest.ifr_hwaddr.sa_data[5]));
|
|
|
|
}
|
|
|
|
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("using interface %s%s with driver<%s> (version: %s)",
|
2010-07-22 05:48:05 +05:30
|
|
|
G.iface, buf, driver_info.driver, driver_info.version);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (G.api_mode[0] == 'a')
|
|
|
|
G.api_method_num = API_AUTO;
|
|
|
|
}
|
|
|
|
|
2010-07-08 06:17:25 +05:30
|
|
|
static smallint detect_link(void)
|
2009-04-26 06:38:51 +05:30
|
|
|
{
|
|
|
|
smallint status;
|
|
|
|
|
|
|
|
if (!G.iface_exists)
|
|
|
|
return (option_mask32 & FLAG_MONITOR) ? IFSTATUS_DOWN : IFSTATUS_ERR;
|
|
|
|
|
2010-01-08 16:57:57 +05:30
|
|
|
/* Some drivers can't detect link status when the interface is down.
|
|
|
|
* I imagine detect_link_iff() is the most vulnerable.
|
|
|
|
* That's why -a "noauto" in an option, not a hardwired behavior.
|
|
|
|
*/
|
2009-04-26 06:38:51 +05:30
|
|
|
if (!(option_mask32 & FLAG_NO_AUTO))
|
|
|
|
up_iface();
|
|
|
|
|
2010-07-22 05:48:05 +05:30
|
|
|
if (G.api_method_num == API_AUTO) {
|
|
|
|
int i;
|
|
|
|
smallint sv_logmode;
|
|
|
|
|
|
|
|
sv_logmode = logmode;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(method_table); i++) {
|
|
|
|
logmode = LOGMODE_NONE;
|
|
|
|
status = method_table[i].func();
|
|
|
|
logmode = sv_logmode;
|
|
|
|
if (status != IFSTATUS_ERR) {
|
|
|
|
G.api_method_num = i;
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("using %s detection mode", method_table[i].name);
|
2010-07-22 05:48:05 +05:30
|
|
|
break;
|
2010-07-08 06:17:25 +05:30
|
|
|
}
|
|
|
|
}
|
2010-07-22 05:48:05 +05:30
|
|
|
} else {
|
|
|
|
status = method_table[G.api_method_num].func();
|
2010-07-08 06:17:25 +05:30
|
|
|
}
|
|
|
|
|
2009-04-26 06:38:51 +05:30
|
|
|
if (status == IFSTATUS_ERR) {
|
|
|
|
if (option_mask32 & FLAG_IGNORE_FAIL)
|
|
|
|
status = IFSTATUS_DOWN;
|
2010-07-08 06:17:25 +05:30
|
|
|
else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE)
|
2009-04-26 06:38:51 +05:30
|
|
|
status = IFSTATUS_UP;
|
2010-07-22 05:48:05 +05:30
|
|
|
else if (G.api_mode[0] == 'a')
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("can't detect link status");
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
2010-07-22 05:48:05 +05:30
|
|
|
|
2009-04-26 06:38:51 +05:30
|
|
|
if (status != G.iface_last_status) {
|
2010-05-09 02:56:16 +05:30
|
|
|
G.iface_prev_status = G.iface_last_status;
|
2009-04-26 06:38:51 +05:30
|
|
|
G.iface_last_status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NOINLINE int check_existence_through_netlink(void)
|
|
|
|
{
|
2010-04-02 10:34:44 +05:30
|
|
|
int iface_len;
|
2013-08-04 21:38:40 +05:30
|
|
|
/* Buffer was 1K, but on linux-3.9.9 it was reported to be too small.
|
|
|
|
* netlink.h: "limit to 8K to avoid MSG_TRUNC when PAGE_SIZE is very large".
|
|
|
|
* Note: on error returns (-1) we exit, no need to free replybuf.
|
|
|
|
*/
|
|
|
|
enum { BUF_SIZE = 8 * 1024 };
|
|
|
|
char *replybuf = xmalloc(BUF_SIZE);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
2010-04-02 10:34:44 +05:30
|
|
|
iface_len = strlen(G.iface);
|
2009-04-26 06:38:51 +05:30
|
|
|
while (1) {
|
|
|
|
struct nlmsghdr *mhdr;
|
|
|
|
ssize_t bytes;
|
|
|
|
|
2013-08-04 21:38:40 +05:30
|
|
|
bytes = recv(netlink_fd, replybuf, BUF_SIZE, MSG_DONTWAIT);
|
2009-04-26 06:38:51 +05:30
|
|
|
if (bytes < 0) {
|
|
|
|
if (errno == EAGAIN)
|
2013-08-04 21:38:40 +05:30
|
|
|
goto ret;
|
2009-04-26 06:38:51 +05:30
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("netlink: recv");
|
2009-04-26 06:38:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mhdr = (struct nlmsghdr*)replybuf;
|
|
|
|
while (bytes > 0) {
|
2010-03-30 19:19:57 +05:30
|
|
|
if (!NLMSG_OK(mhdr, bytes)) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("netlink packet too small or truncated");
|
2009-04-26 06:38:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) {
|
|
|
|
struct rtattr *attr;
|
|
|
|
int attr_len;
|
|
|
|
|
|
|
|
if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("netlink packet too small or truncated");
|
2009-04-26 06:38:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-30 19:19:57 +05:30
|
|
|
attr = IFLA_RTA(NLMSG_DATA(mhdr));
|
|
|
|
attr_len = IFLA_PAYLOAD(mhdr);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
while (RTA_OK(attr, attr_len)) {
|
|
|
|
if (attr->rta_type == IFLA_IFNAME) {
|
|
|
|
int len = RTA_PAYLOAD(attr);
|
|
|
|
if (len > IFNAMSIZ)
|
|
|
|
len = IFNAMSIZ;
|
2010-04-02 10:34:44 +05:30
|
|
|
if (iface_len <= len
|
|
|
|
&& strncmp(G.iface, RTA_DATA(attr), len) == 0
|
|
|
|
) {
|
2009-04-26 06:38:51 +05:30
|
|
|
G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attr = RTA_NEXT(attr, attr_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mhdr = NLMSG_NEXT(mhdr, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 21:38:40 +05:30
|
|
|
ret:
|
|
|
|
free(replybuf);
|
2009-04-26 06:38:51 +05:30
|
|
|
return G.iface_exists;
|
|
|
|
}
|
|
|
|
|
2009-05-01 12:34:25 +05:30
|
|
|
#if ENABLE_FEATURE_PIDFILE
|
2009-04-26 06:38:51 +05:30
|
|
|
static NOINLINE pid_t read_pid(const char *filename)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
len = open_read_close(filename, buf, 127);
|
|
|
|
if (len > 0) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
/* returns ULONG_MAX on error => -1 */
|
|
|
|
return bb_strtoul(buf, NULL, 10);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-01 12:34:25 +05:30
|
|
|
#endif
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
int ifplugd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int ifplugd_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int iface_status;
|
|
|
|
int delay_time;
|
|
|
|
const char *iface_status_str;
|
|
|
|
struct pollfd netlink_pollfd[1];
|
|
|
|
unsigned opts;
|
2010-07-08 06:17:25 +05:30
|
|
|
const char *api_mode_found;
|
2009-04-26 06:38:51 +05:30
|
|
|
#if ENABLE_FEATURE_PIDFILE
|
|
|
|
char *pidfile_name;
|
|
|
|
pid_t pid_from_pidfile;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
INIT_G();
|
|
|
|
|
|
|
|
opts = getopt32(argv, OPTION_STR,
|
|
|
|
&G.iface, &G.script_name, &G.poll_time, &G.delay_up,
|
|
|
|
&G.delay_down, &G.api_mode, &G.extra_arg);
|
2009-11-07 22:00:14 +05:30
|
|
|
G.poll_time *= 1000;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
applet_name = xasprintf("ifplugd(%s)", G.iface);
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_PIDFILE
|
2012-12-11 01:19:39 +05:30
|
|
|
pidfile_name = xasprintf(CONFIG_PID_FILE_PATH "/ifplugd.%s.pid", G.iface);
|
2009-04-26 06:38:51 +05:30
|
|
|
pid_from_pidfile = read_pid(pidfile_name);
|
|
|
|
|
|
|
|
if (opts & FLAG_KILL) {
|
|
|
|
if (pid_from_pidfile > 0)
|
2013-02-28 17:20:09 +05:30
|
|
|
/* Upstream tool use SIGINT for -k */
|
|
|
|
kill(pid_from_pidfile, SIGINT);
|
2009-04-26 06:38:51 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("daemon already running");
|
2009-04-26 06:38:51 +05:30
|
|
|
#endif
|
2010-07-22 05:48:05 +05:30
|
|
|
|
2010-07-08 06:17:25 +05:30
|
|
|
api_mode_found = strchr(api_modes, G.api_mode[0]);
|
|
|
|
if (!api_mode_found)
|
2009-04-26 06:38:51 +05:30
|
|
|
bb_error_msg_and_die("unknown API mode '%s'", G.api_mode);
|
2010-07-08 06:17:25 +05:30
|
|
|
G.api_method_num = api_mode_found - api_modes;
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
if (!(opts & FLAG_NO_DAEMON))
|
|
|
|
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
|
|
|
|
|
|
|
xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
|
|
|
|
if (opts & FLAG_MONITOR) {
|
2019-06-03 17:46:52 +05:30
|
|
|
int fd = create_and_bind_to_netlink(NETLINK_ROUTE, RTMGRP_LINK, 0);
|
2010-07-22 05:48:05 +05:30
|
|
|
xmove_fd(fd, netlink_fd);
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
write_pidfile(pidfile_name);
|
|
|
|
|
|
|
|
/* this can't be moved before socket creation */
|
|
|
|
if (!(opts & FLAG_NO_SYSLOG)) {
|
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
|
|
|
logmode |= LOGMODE_SYSLOG;
|
|
|
|
}
|
|
|
|
|
|
|
|
bb_signals(0
|
|
|
|
| (1 << SIGINT )
|
|
|
|
| (1 << SIGTERM)
|
|
|
|
| (1 << SIGQUIT)
|
|
|
|
| (1 << SIGHUP ) /* why we ignore it? */
|
|
|
|
/* | (1 << SIGCHLD) - run_script does not use it anymore */
|
|
|
|
, record_signo);
|
|
|
|
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("started: %s", bb_banner);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
if (opts & FLAG_MONITOR) {
|
|
|
|
struct ifreq ifrequest;
|
|
|
|
set_ifreq_to_ifname(&ifrequest);
|
2010-01-08 16:57:57 +05:30
|
|
|
G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0);
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (G.iface_exists)
|
|
|
|
maybe_up_new_iface();
|
|
|
|
|
|
|
|
iface_status = detect_link();
|
|
|
|
if (iface_status == IFSTATUS_ERR)
|
|
|
|
goto exiting;
|
|
|
|
iface_status_str = strstatus(iface_status);
|
|
|
|
|
|
|
|
if (opts & FLAG_MONITOR) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("interface %s",
|
2009-04-26 06:38:51 +05:30
|
|
|
G.iface_exists ? "exists"
|
|
|
|
: "doesn't exist, waiting");
|
|
|
|
}
|
|
|
|
/* else we assume it always exists, but don't mislead user
|
|
|
|
* by potentially lying that it really exists */
|
|
|
|
|
|
|
|
if (G.iface_exists) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("link is %s", iface_status_str);
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if ((!(opts & FLAG_NO_STARTUP)
|
|
|
|
&& iface_status == IFSTATUS_UP
|
|
|
|
)
|
|
|
|
|| (opts & FLAG_INITIAL_DOWN)
|
|
|
|
) {
|
|
|
|
if (run_script(iface_status_str) != 0)
|
|
|
|
goto exiting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
netlink_pollfd[0].fd = netlink_fd;
|
|
|
|
netlink_pollfd[0].events = POLLIN;
|
|
|
|
delay_time = 0;
|
|
|
|
while (1) {
|
|
|
|
int iface_status_old;
|
|
|
|
|
|
|
|
switch (bb_got_signal) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
bb_got_signal = 0;
|
|
|
|
goto cleanup;
|
|
|
|
case SIGQUIT:
|
|
|
|
bb_got_signal = 0;
|
|
|
|
goto exiting;
|
|
|
|
default:
|
|
|
|
bb_got_signal = 0;
|
2018-04-29 17:16:49 +05:30
|
|
|
/* do not clear bb_got_signal if already 0, this can lose signals */
|
|
|
|
case 0:
|
2009-04-26 06:38:51 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (poll(netlink_pollfd,
|
|
|
|
(opts & FLAG_MONITOR) ? 1 : 0,
|
2009-11-07 22:00:14 +05:30
|
|
|
G.poll_time
|
2009-04-26 06:38:51 +05:30
|
|
|
) < 0
|
|
|
|
) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("poll");
|
2009-04-26 06:38:51 +05:30
|
|
|
goto exiting;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opts & FLAG_MONITOR)
|
|
|
|
&& (netlink_pollfd[0].revents & POLLIN)
|
|
|
|
) {
|
2014-02-10 14:14:25 +05:30
|
|
|
int iface_exists_old;
|
|
|
|
|
|
|
|
iface_exists_old = G.iface_exists;
|
2009-04-26 06:38:51 +05:30
|
|
|
G.iface_exists = check_existence_through_netlink();
|
|
|
|
if (G.iface_exists < 0) /* error */
|
|
|
|
goto exiting;
|
|
|
|
if (iface_exists_old != G.iface_exists) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("interface %sappeared",
|
2009-04-26 06:38:51 +05:30
|
|
|
G.iface_exists ? "" : "dis");
|
|
|
|
if (G.iface_exists)
|
|
|
|
maybe_up_new_iface();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: if !G.iface_exists, returns DOWN */
|
2014-02-10 14:14:25 +05:30
|
|
|
iface_status_old = iface_status;
|
2009-04-26 06:38:51 +05:30
|
|
|
iface_status = detect_link();
|
|
|
|
if (iface_status == IFSTATUS_ERR) {
|
|
|
|
if (!(opts & FLAG_MONITOR))
|
|
|
|
goto exiting;
|
|
|
|
iface_status = IFSTATUS_DOWN;
|
|
|
|
}
|
|
|
|
iface_status_str = strstatus(iface_status);
|
|
|
|
|
|
|
|
if (iface_status_old != iface_status) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("link is %s", iface_status_str);
|
2009-04-26 06:38:51 +05:30
|
|
|
|
|
|
|
if (delay_time) {
|
|
|
|
/* link restored its old status before
|
2014-02-10 14:14:25 +05:30
|
|
|
* we ran script. don't run the script: */
|
2009-04-26 06:38:51 +05:30
|
|
|
delay_time = 0;
|
|
|
|
} else {
|
|
|
|
delay_time = monotonic_sec();
|
|
|
|
if (iface_status == IFSTATUS_UP)
|
|
|
|
delay_time += G.delay_up;
|
|
|
|
if (iface_status == IFSTATUS_DOWN)
|
|
|
|
delay_time += G.delay_down;
|
2014-02-10 14:14:25 +05:30
|
|
|
#if 0 /* if you are back in 1970... */
|
|
|
|
if (delay_time == 0) {
|
|
|
|
sleep(1);
|
|
|
|
delay_time = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) {
|
|
|
|
if (run_script(iface_status_str) != 0)
|
|
|
|
goto exiting;
|
2014-02-10 14:14:25 +05:30
|
|
|
delay_time = 0;
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|
|
|
|
} /* while (1) */
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (!(opts & FLAG_NO_SHUTDOWN)
|
|
|
|
&& (iface_status == IFSTATUS_UP
|
|
|
|
|| (iface_status == IFSTATUS_DOWN && delay_time)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
setenv(IFPLUGD_ENV_PREVIOUS, strstatus(iface_status), 1);
|
|
|
|
setenv(IFPLUGD_ENV_CURRENT, strstatus(-1), 1);
|
|
|
|
run_script("down\0up"); /* reusing string */
|
|
|
|
}
|
|
|
|
|
|
|
|
exiting:
|
|
|
|
remove_pidfile(pidfile_name);
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("exiting");
|
2009-04-26 06:38:51 +05:30
|
|
|
}
|