libbb: add strncpy_IFNAMSIZ

function                                             old     new   delta
...
udhcp_read_interface                                 225     220      -5
brctl_main                                          1151    1146      -5
add_interface                                        109     104      -5
ipaddr_list_or_flush                                2174    2167      -7
do_add_ioctl                                          88      80      -8
vconfig_main                                         249     240      -9
do_del_ioctl                                          78      68     -10
do_iplink                                           1186    1173     -13
do_if_fetch                                          766     753     -13
buffer_fill_and_print                                196     179     -17
parse_args                                          1709    1684     -25
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/20 up/down: 11/-140)         Total: -129 bytes
This commit is contained in:
Denis Vlasenko 2008-12-02 18:18:50 +00:00
parent 928b2c0fee
commit 360d9661b6
19 changed files with 52 additions and 49 deletions

View File

@ -165,7 +165,7 @@ static int get_node_id(unsigned char *node_id)
n = ifc.ifc_len;
for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.ifr_name, ifrp->ifr_name);
#ifdef SIOCGIFHWADDR
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
continue;

View File

@ -540,6 +540,7 @@ char *xstrdup(const char *s) FAST_FUNC;
char *xstrndup(const char *s, int n) FAST_FUNC;
void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
/* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
* But potentially slow, don't use in one-billion-times loops */
int bb_putchar(int ch) FAST_FUNC;

View File

@ -40,6 +40,14 @@ int FAST_FUNC close_on_exec_on(int fd)
return fcntl(fd, F_SETFD, FD_CLOEXEC);
}
char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
{
#ifndef IFNAMSIZ
enum { IFNAMSIZ = 16 };
#endif
return strncpy(dst, src, IFNAMSIZ);
}
/* Convert unsigned long long value into compact 4-char
* representation. Examples: "1234", "1.2k", " 27M", "123T"
* String is not terminated (buf[4] is untouched) */

View File

@ -286,7 +286,7 @@ int arping_main(int argc UNUSED_PARAM, char **argv)
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
strncpy_IFNAMSIZ(ifr.ifr_name, device);
/* We use ifr.ifr_name in error msg so that problem
* with truncated name will be visible */
ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");

View File

@ -137,7 +137,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
if (!if_indextoname(bridx[i], brname))
bb_perror_msg_and_die("can't get bridge name for index %d", i);
strncpy(ifr.ifr_name, brname, IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.ifr_name, brname);
arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
(unsigned long) &bi, 0);
@ -191,7 +191,7 @@ int brctl_main(int argc UNUSED_PARAM, char **argv)
if (!*argv) /* all but 'addif/delif' need at least two arguments */
bb_show_usage();
strncpy(ifr.ifr_name, br, IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.ifr_name, br);
if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
brif = *argv;
ifr.ifr_ifindex = if_nametoindex(brif);

View File

@ -219,7 +219,7 @@ int ether_wake_main(int argc UNUSED_PARAM, char **argv)
{
struct ifreq if_hwaddr;
strncpy(if_hwaddr.ifr_name, ifname, sizeof(if_hwaddr.ifr_name));
strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
@ -255,7 +255,7 @@ int ether_wake_main(int argc UNUSED_PARAM, char **argv)
#if defined(PF_PACKET)
{
struct ifreq ifr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
xioctl(s, SIOCGIFINDEX, &ifr);
memset(&whereto, 0, sizeof(whereto));
whereto.sll_family = AF_PACKET;

View File

@ -313,7 +313,7 @@ int ifconfig_main(int argc, char **argv)
sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
/* get interface name */
strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
/* Process the remaining arguments. */
while (*++argv != (char *) NULL) {

View File

@ -140,11 +140,6 @@ struct globals {
/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
static void strncpy_IFNAMSIZ(char *dst, const char *src)
{
strncpy(dst, src, IFNAMSIZ);
}
static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
{
return ioctl(skfd, request, ifr);

View File

@ -395,7 +395,7 @@ static struct interface *add_interface(char *name)
}
new = xzalloc(sizeof(*new));
strncpy(new->name, name, IFNAMSIZ);
strncpy_IFNAMSIZ(new->name, name);
nextp = ife ? &ife->next : &int_list;
new->prev = ife;
new->next = *nextp;
@ -614,39 +614,39 @@ static int if_fetch(struct interface *ife)
skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
ife->flags = ifr.ifr_flags;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(ife->hwaddr, 0, 32);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
ife->type = ifr.ifr_hwaddr.sa_family;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->metric = 0;
if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
ife->metric = ifr.ifr_metric;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->mtu = 0;
if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
ife->mtu = ifr.ifr_mtu;
memset(&ife->map, 0, sizeof(struct ifmap));
#ifdef SIOCGIFMAP
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
ife->map = ifr.ifr_map;
#endif
#ifdef HAVE_TXQUEUELEN
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->tx_queue_len = -1; /* unknown value */
if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
ife->tx_queue_len = ifr.ifr_qlen;
@ -654,23 +654,23 @@ static int if_fetch(struct interface *ife)
ife->tx_queue_len = -1; /* unknown value */
#endif
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ifr.ifr_addr.sa_family = AF_INET;
memset(&ife->addr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
ife->has_ip = 1;
ife->addr = ifr.ifr_addr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
ife->dstaddr = ifr.ifr_dstaddr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
ife->broadaddr = ifr.ifr_broadaddr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->netmask, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
ife->netmask = ifr.ifr_netmask;

View File

@ -83,7 +83,7 @@ static void print_queuelen(char *name)
return;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, name);
if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
close(s);
return;

View File

@ -41,7 +41,7 @@ static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = get_ctl_fd();
xioctl(fd, SIOCGIFFLAGS, &ifr);
if ((ifr.ifr_flags ^ flags) & mask) {
@ -58,8 +58,8 @@ static void do_changename(char *dev, char *newdev)
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
fd = get_ctl_fd();
xioctl(fd, SIOCSIFNAME, &ifr);
close(fd);
@ -73,7 +73,7 @@ static void set_qlen(char *dev, int qlen)
s = get_ctl_fd();
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
ifr.ifr_qlen = qlen;
xioctl(s, SIOCSIFTXQLEN, &ifr);
close(s);
@ -87,7 +87,7 @@ static void set_mtu(char *dev, int mtu)
s = get_ctl_fd();
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
ifr.ifr_mtu = mtu;
xioctl(s, SIOCSIFMTU, &ifr);
close(s);
@ -104,7 +104,7 @@ static int get_address(char *dev, int *htype)
s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
xioctl(s, SIOCGIFINDEX, &ifr);
memset(&me, 0, sizeof(me));
@ -128,7 +128,7 @@ static void parse_address(char *dev, int hatype, int halen, char *lla, struct if
int alen;
memset(ifr, 0, sizeof(*ifr));
strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
strncpy_IFNAMSIZ(ifr->ifr_name, dev);
ifr->ifr_hwaddr.sa_family = hatype;
alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;

View File

@ -34,7 +34,7 @@ static int do_ioctl_get_ifindex(char *dev)
struct ifreq ifr;
int fd;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
xioctl(fd, SIOCGIFINDEX, &ifr);
close(fd);
@ -47,7 +47,7 @@ static int do_ioctl_get_iftype(char *dev)
int fd;
int err;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
close(fd);
@ -73,7 +73,7 @@ static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
int fd;
int err;
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
@ -88,9 +88,9 @@ static int do_add_ioctl(int cmd, const char *basedev, struct ip_tunnel_parm *p)
int fd;
if (cmd == SIOCCHGTUNNEL && p->name[0]) {
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
} else {
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
@ -114,9 +114,9 @@ static int do_del_ioctl(const char *basedev, struct ip_tunnel_parm *p)
int fd;
if (p->name[0]) {
strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
} else {
strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
@ -148,7 +148,7 @@ static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
int key;
memset(p, 0, sizeof(*p));
memset(&medium, 0, sizeof(medium));
medium[0] = '\0';
p->iph.version = 4;
p->iph.ihl = 5;
@ -250,7 +250,7 @@ static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
p->iph.saddr = get_addr32(*argv);
} else if (key == ARG_dev) {
NEXT_ARG();
strncpy(medium, *argv, IFNAMSIZ-1);
strncpy_IFNAMSIZ(medium, *argv);
} else if (key == ARG_ttl) {
unsigned uval;
NEXT_ARG();
@ -279,7 +279,7 @@ static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
}
if (p->name[0])
duparg2("name", *argv);
strncpy(p->name, *argv, IFNAMSIZ);
strncpy_IFNAMSIZ(p->name, *argv);
if (cmd == SIOCCHGTUNNEL && count == 0) {
struct ip_tunnel_parm old_p;
memset(&old_p, 0, sizeof(old_p));
@ -324,7 +324,6 @@ static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
}
}
/* Return value becomes exitcode. It's okay to not return at all */
static int do_add(int cmd, char **argv)
{

View File

@ -176,7 +176,7 @@ int xll_name_to_index(const char *const name)
struct ifreq ifr;
int tmp;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.ifr_name, name);
ifr.ifr_ifindex = -1;
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
close(sock_fd);

View File

@ -177,7 +177,7 @@ int nameif_main(int argc, char **argv)
/* Find the current interface name and copy it to ifr.ifr_name */
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, token[0], sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
#if ENABLE_FEATURE_NAMEIF_EXTENDED
/* Check for driver etc. */

View File

@ -423,7 +423,7 @@ static void INET6_setroute(int action, char **args)
if (devname) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, devname);
xioctl(skfd, SIOGIFINDEX, &ifr);
rt.rtmsg_ifindex = ifr.ifr_ifindex;
}

View File

@ -447,7 +447,7 @@ ifaddrlist(struct IFADDRLIST **ipaddrp)
* SIOCGIFFLAGS stomps over it because the requests
* are returned in a union.)
*/
strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, ifrp->ifr_name);
if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
if (errno == ENXIO)
continue;

View File

@ -47,7 +47,7 @@ int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, interface);
if (addr) {
if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
"is interface %s up and configured?", interface)

View File

@ -133,7 +133,7 @@ int vconfig_main(int argc, char **argv)
if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
} else {
strncpy(ifr.device1, argv[1], IFNAMSIZ);
strncpy_IFNAMSIZ(ifr.device1, argv[1]);
p = argv[2];
/* I suppose one could try to combine some of the function calls below,

View File

@ -268,7 +268,7 @@ int zcip_main(int argc, char **argv)
// get the interface's ethernet address
//memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, argv_intf, sizeof(ifr.ifr_name));
strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
memcpy(&eth_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);