xioctl and friends by Tito <farmatito@tiscali.it>

function                                             old     new   delta
do_iptunnel                                          203     977    +774
process_dev                                         5328    5494    +166
ioctl_or_perror                                        -      54     +54
ioctl_or_perror_and_die                                -      51     +51
ioctl_alt_func                                         -      49     +49
bb_ioctl_or_warn                                       -      47     +47
do_add_ioctl                                         102     145     +43
bb_xioctl                                              -      39     +39
print_value_on_off                                     -      31     +31
get_lcm                                              105     123     +18
arp_main                                            2155    2167     +12
..................
zcip_main                                           1576    1566     -10
setlogcons_main                                       92      82     -10
dumpkmap_main                                        263     253     -10
do_get_ioctl                                          85      75     -10
setkeycodes_main                                     165     154     -11
write_table                                          244     232     -12
vconfig_main                                         318     306     -12
do_del_ioctl                                          93      81     -12
set_address                                           75      62     -13
maybe_set_utc                                         30      16     -14
loadfont_main                                        495     479     -16
slattach_main                                        712     695     -17
do_loadfont                                          191     174     -17
do_iplink                                           1155    1136     -19
getty_main                                          2583    2562     -21
fbset_main                                          2058    2035     -23
do_time                                              588     565     -23
xioctl                                                25       -     -25
read_rtc                                             186     160     -26
parse_conf                                          1299    1270     -29
udhcp_read_interface                                 269     239     -30
bb_ioctl                                              45       -     -45
bb_ioctl_alt                                          70       -     -70
bb_ioctl_on_off                                       78       -     -78
.rodata                                           129370  129018    -352
do_show                                              799       -    -799
------------------------------------------------------------------------------
(add/remove: 6/5 grow/shrink: 13/49 up/down: 1316/-1864)     Total: -548 bytes
   text	   data	    bss	    dec	    hex	filename
 675352	   2740	  13968	 692060	  a8f5c	busybox_old
 674804	   2740	  13968	 691512	  a8d38	busybox_unstripped

diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c
index a4add6a..fc10ba3 100644
--- a/networking/libiproute/ipaddress.c
+++ b/networking/libiproute/ipaddress.c
@@ -84,8 +84,7 @@
 
 	memset(&ifr, 0, sizeof(ifr));
 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
-	if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
-		bb_perror_msg("SIOCGIFXQLEN");
+	if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
 		close(s);
 		return;
 	}
diff --git a/networking/libiproute/iplink.c b/networking/libiproute/iplink.c
index 7db4d7e..3d3ea2a 100644
--- a/networking/libiproute/iplink.c
+++ b/networking/libiproute/iplink.c
@@ -49,14 +49,11 @@
 
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	fd = get_ctl_fd();
-	if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
-		bb_perror_msg_and_die("SIOCGIFFLAGS");
-	}
+	xioctl(fd, SIOCGIFFLAGS, &ifr);
 	if ((ifr.ifr_flags ^ flags) & mask) {
 		ifr.ifr_flags &= ~mask;
 		ifr.ifr_flags |= mask & flags;
-		if (ioctl(fd, SIOCSIFFLAGS, &ifr))
-			bb_perror_msg_and_die("SIOCSIFFLAGS");
+		xioctl(fd, SIOCSIFFLAGS, &ifr);
 	}
 	close(fd);
 }
@@ -66,15 +63,11 @@
 {
 	struct ifreq ifr;
 	int fd;
-	int err;
 
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
 	fd = get_ctl_fd();
-	err = ioctl(fd, SIOCSIFNAME, &ifr);
-	if (err) {
-		bb_perror_msg_and_die("SIOCSIFNAME");
-	}
+	xioctl(fd, SIOCSIFNAME, &ifr);
 	close(fd);
 }
 
@@ -88,9 +81,7 @@
 	memset(&ifr, 0, sizeof(ifr));
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	ifr.ifr_qlen = qlen;
-	if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
-		bb_perror_msg_and_die("SIOCSIFXQLEN");
-	}
+	xioctl(s, SIOCSIFTXQLEN, &ifr);
 	close(s);
 }
 
@@ -104,9 +95,7 @@
 	memset(&ifr, 0, sizeof(ifr));
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	ifr.ifr_mtu = mtu;
-	if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
-		bb_perror_msg_and_die("SIOCSIFMTU");
-	}
+	xioctl(s, SIOCSIFMTU, &ifr);
 	close(s);
 }
 
@@ -122,9 +111,7 @@
 
 	memset(&ifr, 0, sizeof(ifr));
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
-	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
-		bb_perror_msg_and_die("SIOCGIFINDEX");
-	}
+	xioctl(s, SIOCGIFINDEX, &ifr);
 
 	memset(&me, 0, sizeof(me));
 	me.sll_family = AF_PACKET;
@@ -163,9 +150,10 @@
 	int s;
 
 	s = get_ctl_fd();
-	if (ioctl(s, brd ? SIOCSIFHWBROADCAST  :SIOCSIFHWADDR, ifr) < 0) {
-		bb_perror_msg_and_die(brd ? "SIOCSIFHWBROADCAST" : "SIOCSIFHWADDR");
-	}
+	if (brd)
+		xioctl(s, SIOCSIFHWBROADCAST, ifr);
+	else
+		xioctl(s, SIOCSIFHWADDR, ifr);
 	close(s);
 }
 
diff --git a/networking/libiproute/iptunnel.c b/networking/libiproute/iptunnel.c
index b12bceb..90d0e11 100644
--- a/networking/libiproute/iptunnel.c
+++ b/networking/libiproute/iptunnel.c
@@ -14,8 +14,6 @@
  * Phil Karn <karn@ka9q.ampr.org>	990408:	"pmtudisc" flag
  */
 
-//#include <sys/socket.h>
-//#include <sys/ioctl.h>
 #include <netinet/ip.h>
 #include <net/if.h>
 #include <net/if_arp.h>
@@ -38,9 +36,7 @@
 
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
-		bb_perror_msg_and_die("SIOCGIFINDEX");
-	}
+	xioctl(fd, SIOCGIFINDEX, &ifr);
 	close(fd);
 	return ifr.ifr_ifindex;
 }
@@ -49,30 +45,26 @@
 {
 	struct ifreq ifr;
 	int fd;
+	int err;
 
 	strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
-		bb_perror_msg("SIOCGIFHWADDR");
-		return -1;
-	}
+	err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
 	close(fd);
-	return ifr.ifr_addr.sa_family;
+	return err ? -1 : ifr.ifr_addr.sa_family;
 }
 
 static char *do_ioctl_get_ifname(int idx)
 {
 	struct ifreq ifr;
 	int fd;
+	int err;
 
 	ifr.ifr_ifindex = idx;
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	if (ioctl(fd, SIOCGIFNAME, &ifr)) {
-		bb_perror_msg("SIOCGIFNAME");
-		return NULL;
-	}
+	err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
 	close(fd);
-	return xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
+	return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
 }
 
 static int do_get_ioctl(const char *basedev, struct ip_tunnel_parm *p)
@@ -84,10 +76,7 @@
 	strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
 	ifr.ifr_ifru.ifru_data = (void*)p;
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	err = ioctl(fd, SIOCGETTUNNEL, &ifr);
-	if (err) {
-		bb_perror_msg("SIOCGETTUNNEL");
-	}
+	err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
 	close(fd);
 	return err;
 }
@@ -105,9 +94,15 @@
 	}
 	ifr.ifr_ifru.ifru_data = (void*)p;
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	if (ioctl(fd, cmd, &ifr)) {
-		bb_perror_msg_and_die("ioctl");
-	}
+#if ENABLE_IOCTL_HEX2STR_ERROR
+	/* #define magic will turn ioctl# into string */
+	if (cmd == SIOCCHGTUNNEL)
+		xioctl(fd, SIOCCHGTUNNEL, &ifr);
+	else
+		xioctl(fd, SIOCADDTUNNEL, &ifr);
+#else
+	xioctl(fd, cmd, &ifr);
+#endif
 	close(fd);
 	return 0;
 }
@@ -125,9 +120,7 @@
 	}
 	ifr.ifr_ifru.ifru_data = (void*)p;
 	fd = xsocket(AF_INET, SOCK_DGRAM, 0);
-	if (ioctl(fd, SIOCDELTUNNEL, &ifr)) {
-		bb_perror_msg_and_die("SIOCDELTUNNEL");
-	}
+	xioctl(fd, SIOCDELTUNNEL, &ifr);
 	close(fd);
 	return 0;
 }
@@ -526,29 +519,24 @@
 /* Return value becomes exitcode. It's okay to not return at all */
 int do_iptunnel(int argc, char **argv)
 {
-	static const char * const keywords[] = {
+	static const char *const keywords[] = {
 		"add", "change", "delete", "show", "list", "lst", NULL
 	};
-	enum {ARG_add = 1, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst};
-	smalluint key = 4; /* show */
-	if (argc > 0) {
-		key = index_in_substr_array(keywords, *argv) +1;
+	enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst };
+	int key;
+
+	if (argc) {
+		key = index_in_substr_array(keywords, *argv);
+		if (key < 0)
+			bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
 		--argc;
 		++argv;
-	} else
-		return do_show(0, NULL);
-	if (key < ARG_add)
- bail:
-		bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
-
-	if (key == ARG_add)
-		return do_add(SIOCADDTUNNEL, argc, argv);
-	if (key == ARG_change)
-		return do_add(SIOCCHGTUNNEL, argc, argv);
-	if (key == ARG_del)
-		return do_del(argc, argv);
-	if (key == ARG_show || key == ARG_list || key == ARG_lst)
-		return do_show(argc, argv);
-	/* be gentle to gcc; avoid warning about non returning */
-	goto bail; /* never reached */
+		if (key == ARG_add)
+			return do_add(SIOCADDTUNNEL, argc, argv);
+		if (key == ARG_change)
+			return do_add(SIOCCHGTUNNEL, argc, argv);
+		if (key == ARG_del)
+			return do_del(argc, argv);
+	}
+	return do_show(argc, argv);
 }