nc: show help text on bad parameters
[oweals/busybox.git] / networking / ifupdown.c
index 6b9449213baef36c01ca62e3b4297cb22da5448d..864fc2acd0da48e09ede3d0e98f68e26d039cc09 100644 (file)
@@ -14,7 +14,7 @@
  *  (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build
  *  configuration.
  *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 #include "libbb.h"
@@ -106,7 +106,7 @@ enum {
 struct globals {
        char **my_environ;
        const char *startup_PATH;
-};
+} FIX_ALIASING;
 #define G (*(struct globals*)&bb_common_bufsiz1)
 #define INIT_G() do { } while (0)
 
@@ -483,7 +483,7 @@ struct dhcp_client_t {
 
 static const struct dhcp_client_t ext_dhcp_clients[] = {
        { "dhcpcd",
-               "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
+               "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
                "dhcpcd -k %iface%",
        },
        { "dhclient",
@@ -495,7 +495,7 @@ static const struct dhcp_client_t ext_dhcp_clients[] = {
                "pump -i %iface% -k",
        },
        { "udhcpc",
-               "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
+               "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
                                "[[ -s %script%]][[ %udhcpc_opts%]]",
                "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
        },
@@ -522,7 +522,7 @@ static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
        bb_error_msg("no dhcp clients found");
        return 0;
 }
-#elif ENABLE_APP_UDHCPC
+#elif ENABLE_UDHCPC
 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
 {
 #if ENABLE_FEATURE_IFUPDOWN_IP
@@ -535,7 +535,7 @@ static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
                return 0;
 #endif
        return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
-                       "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
+                       "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
                        ifd, exec);
 }
 #else
@@ -569,12 +569,14 @@ static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
        result += static_down(ifd, exec);
        return ((result == 3) ? 3 : 0);
 }
-#elif ENABLE_APP_UDHCPC
+#elif ENABLE_UDHCPC
 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
 {
        int result;
-       result = execute("kill "
-                      "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
+       result = execute(
+               "test -f /var/run/udhcpc.%iface%.pid && "
+               "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
+               ifd, exec);
        /* Also bring the hardware interface down since
           killing the dhcp client alone doesn't do it.
           This enables consecutive ifup->ifdown->ifup */
@@ -644,6 +646,9 @@ static const struct address_family_t addr_inet = {
 
 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
 
+/* Returns pointer to the next word, or NULL.
+ * In 1st case, advances *buf to the word after this one.
+ */
 static char *next_word(char **buf)
 {
        unsigned length;
@@ -663,7 +668,7 @@ static char *next_word(char **buf)
        if (word[length] != '\0')
                word[length++] = '\0';
 
-       *buf = word + length;
+       *buf = skip_whitespace(word + length);
 
        return word;
 }
@@ -966,7 +971,7 @@ static int doit(char *str)
                pid_t child;
                int status;
 
-               fflush(NULL);
+               fflush_all();
                child = vfork();
                switch (child) {
                case -1: /* failure */
@@ -1035,26 +1040,23 @@ static int popen2(FILE **in, FILE **out, char *command, char *param)
        xpiped_pair(infd);
        xpiped_pair(outfd);
 
-       fflush(NULL);
-       pid = vfork();
+       fflush_all();
+       pid = xvfork();
 
-       switch (pid) {
-       case -1:  /* failure */
-               bb_perror_msg_and_die("vfork");
-       case 0:  /* child */
+       if (pid == 0) {
+               /* Child */
                /* NB: close _first_, then move fds! */
                close(infd.wr);
                close(outfd.rd);
                xmove_fd(infd.rd, 0);
                xmove_fd(outfd.wr, 1);
-               BB_EXECVP(command, argv);
-               _exit(127);
+               BB_EXECVP_or_die(argv);
        }
        /* parent */
        close(infd.rd);
        close(outfd.wr);
-       *in = fdopen(infd.wr, "w");
-       *out = fdopen(outfd.rd, "r");
+       *in = xfdopen_for_write(infd.wr);
+       *out = xfdopen_for_read(outfd.rd);
        return pid;
 }
 
@@ -1139,7 +1141,7 @@ static llist_t *read_iface_state(void)
 
 
 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int ifupdown_main(int argc, char **argv)
+int ifupdown_main(int argc UNUSED_PARAM, char **argv)
 {
        int (*cmds)(struct interface_defn_t *);
        struct interfaces_file_t *defn;
@@ -1158,7 +1160,8 @@ int ifupdown_main(int argc, char **argv)
        }
 
        getopt32(argv, OPTION_STR, &interfaces);
-       if (argc - optind > 0) {
+       argv += optind;
+       if (argv[0]) {
                if (DO_ALL) bb_show_usage();
        } else {
                if (!DO_ALL) bb_show_usage();
@@ -1172,7 +1175,7 @@ int ifupdown_main(int argc, char **argv)
        if (DO_ALL) {
                target_list = defn->autointerfaces;
        } else {
-               llist_add_to_end(&target_list, argv[optind]);
+               llist_add_to_end(&target_list, argv[0]);
        }
 
        /* Update the interfaces */