libbb: nonblock_safe_read->nonblock_immune_read, remove unused param of xmalloc_reads
[oweals/busybox.git] / networking / vconfig.c
index e063d0a49d02ee7a5baec6e17297ef59caf4d42b..4fa341ac39c85ea6c28383c7d6f0a2920b9fc14f 100644 (file)
@@ -4,11 +4,23 @@
  *
  * Copyright (C) 2001  Manuel Novoa III  <mjn3@codepoet.org>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 /* BB_AUDIT SUSv3 N/A */
 
+//usage:#define vconfig_trivial_usage
+//usage:       "COMMAND [OPTIONS]"
+//usage:#define vconfig_full_usage "\n\n"
+//usage:       "Create and remove virtual ethernet devices\n"
+//usage:     "\nOptions:"
+//usage:     "\n       add             [interface-name] [vlan_id]"
+//usage:     "\n       rem             [vlan-name]"
+//usage:     "\n       set_flag        [interface-name] [flag-num] [0 | 1]"
+//usage:     "\n       set_egress_map  [vlan-name] [skb_priority] [vlan_qos]"
+//usage:     "\n       set_ingress_map [vlan-name] [skb_priority] [vlan_qos]"
+//usage:     "\n       set_name_type   [name-type]"
+
 #include "libbb.h"
 #include <net/if.h>
 
@@ -47,23 +59,24 @@ struct vlan_ioctl_args {
        short vlan_qos;
 };
 
-#define VLAN_GROUP_ARRAY_LEN 4096
-#define SIOCSIFVLAN    0x8983          /* Set 802.1Q VLAN options */
+#define VLAN_GROUP_ARRAY_LEN  4096
+#define SIOCSIFVLAN           0x8983  /* Set 802.1Q VLAN options */
 
-/* On entry, table points to the length of the current string plus
- * nul terminator plus data length for the subsequent entry.  The
- * return value is the last data entry for the matching string. */
+/* On entry, table points to the length of the current string
+ * plus NUL terminator plus data length for the subsequent entry.
+ * The return value is the last data entry for the matching string. */
 static const char *xfind_str(const char *table, const char *str)
 {
        while (strcasecmp(str, table+1) != 0) {
-               if (!*(table += table[0])) {
+               table += table[0];
+               if (!*table) {
                        bb_show_usage();
                }
        }
        return table - 1;
 }
 
-static const char cmds[] = {
+static const char cmds[] ALIGN1 = {
        4, ADD_VLAN_CMD, 7,
        'a', 'd', 'd', 0,
        3, DEL_VLAN_CMD, 7,
@@ -85,7 +98,7 @@ static const char cmds[] = {
        'm', 'a', 'p', 0,
 };
 
-static const char name_types[] = {
+static const char name_types[] ALIGN1 = {
        VLAN_NAME_TYPE_PLUS_VID, 16,
        'V', 'L', 'A', 'N',
        '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
@@ -104,9 +117,9 @@ static const char name_types[] = {
        '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
 };
 
-static const char conf_file_name[] = "/proc/net/vlan/config";
+static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
 
-int vconfig_main(int argc, char **argv);
+int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int vconfig_main(int argc, char **argv)
 {
        struct vlan_ioctl_args ifr;
@@ -121,7 +134,7 @@ int vconfig_main(int argc, char **argv)
        /* Will die if 802.1q is not present */
        xopen(conf_file_name, O_RDONLY);
 
-       memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
+       memset(&ifr, 0, sizeof(ifr));
 
        ++argv;
        p = xfind_str(cmds+2, *argv);
@@ -133,10 +146,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 {
-               if (strlen(argv[1]) >= IF_NAMESIZE) {
-                       bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE);
-               }
-               strcpy(ifr.device1, argv[1]);
+               strncpy_IFNAMSIZ(ifr.device1, argv[1]);
                p = argv[2];
 
                /* I suppose one could try to combine some of the function calls below,
@@ -157,9 +167,8 @@ int vconfig_main(int argc, char **argv)
        }
 
        fd = xsocket(AF_INET, SOCK_STREAM, 0);
-       if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) {
-               bb_perror_msg_and_die("ioctl error for %s", *argv);
-       }
+       ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
+                                               "ioctl error for %s", *argv);
 
        return 0;
 }