]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Rename exec_script to run_script. Split the exec/fork part to static exec_script...
authorRoy Marples <roy@marples.name>
Mon, 11 Aug 2008 09:28:53 +0000 (09:28 +0000)
committerRoy Marples <roy@marples.name>
Mon, 11 Aug 2008 09:28:53 +0000 (09:28 +0000)
client.c
configure.c
configure.h

index fbb1cd54cf728569d030c78dc64b6cd147b29160..6bef15727c5a7eb2d0d1e9ff4711ef3f61dabf19 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1393,7 +1393,7 @@ handle_dhcp(struct if_state *state, struct dhcp_message **dhcpp,
                get_option_addr(&lease->server.s_addr, dhcp, DHO_SERVERID);
                log_dhcp(LOG_INFO, "offered", dhcp);
                if (state->options & DHCPCD_TEST) {
-                       exec_script(options, iface->name, "TEST", dhcp, NULL);
+                       run_script(options, iface->name, "TEST", dhcp, NULL);
                        return 0;
                }
                state->state = STATE_REQUESTING;
index 22b7950e77164004c7c050e44653bd23adc13749..db2228eb8ce813bed986d1eaa7a238811054b8e1 100644 (file)
 
 #define DEFAULT_PATH   "PATH=/usr/bin:/usr/sbin:/bin:/sbin"
 
+
+static int
+exec_script(char *const *argv, char *const *env)
+{
+       pid_t pid;
+       sigset_t full;
+       sigset_t old;
+
+       /* OK, we need to block signals */
+       sigfillset(&full);
+       sigprocmask(SIG_SETMASK, &full, &old);
+       signal_reset();
+
+       switch (pid = vfork()) {
+       case -1:
+               logger(LOG_ERR, "vfork: %s", strerror(errno));
+               break;
+       case 0:
+               sigprocmask(SIG_SETMASK, &old, NULL);
+               execve(argv[0], argv, env);
+               logger(LOG_ERR, "%s: %s", argv[0], strerror(errno));
+               _exit(127);
+               /* NOTREACHED */
+       }
+
+       /* Restore our signals */
+       signal_setup();
+       sigprocmask(SIG_SETMASK, &old, NULL);
+       return pid;
+}
+
 int
-exec_script(const struct options *options, const char *iface,
-           const char *reason,
-           const struct dhcp_message *dhcpn, const struct dhcp_message *dhcpo)
+run_script(const struct options *options, const char *iface,
+           const char *reason,
+           const struct dhcp_message *dhcpn, const struct dhcp_message *dhcpo)
 {
        char *const argv[2] = { UNCONST(options->script), NULL };
        char **env = NULL, **ep;
        char *path;
        ssize_t e, elen;
-       int ret = 0;
        pid_t pid;
        int status = 0;
-       sigset_t full;
-       sigset_t old;
 
        logger(LOG_DEBUG, "executing `%s', reason %s", options->script, reason);
 
@@ -115,50 +143,17 @@ exec_script(const struct options *options, const char *iface,
        }
        env[elen] = '\0';
 
-       /* OK, we need to block signals */
-       sigfillset(&full);
-       sigprocmask(SIG_SETMASK, &full, &old);
-
-#ifdef THERE_IS_NO_FORK
-       signal_reset();
-       pid = vfork();
-#else
-       pid = fork();
-#endif
-
-       switch (pid) {
-       case -1:
-#ifdef THERE_IS_NO_FORK
-               logger(LOG_ERR, "vfork: %s", strerror(errno));
-#else
-               logger(LOG_ERR, "fork: %s", strerror(errno));
-#endif
-               ret = -1;
-               break;
-       case 0:
-#ifndef THERE_IS_NO_FORK
-               signal_reset();
-#endif
-               sigprocmask(SIG_SETMASK, &old, NULL);
-               execve(options->script, argv, env);
-               logger(LOG_ERR, "%s: %s", options->script, strerror(errno));
-               _exit(111);
-               /* NOTREACHED */
-       }
-
-#ifdef THERE_IS_NO_FORK
-       signal_setup();
-#endif
-
-       /* Restore our signals */
-       sigprocmask(SIG_SETMASK, &old, NULL);
-
-       /* Wait for the script to finish */
-       while (waitpid(pid, &status, 0) == -1) {
-               if (errno != EINTR) {
-                       logger(LOG_ERR, "waitpid: %s", strerror(errno));
-                       status = -1;
-                       break;
+       pid = exec_script(argv, env);
+       if (pid == -1)
+               status = -1;
+       else if (pid != 0) {
+               /* Wait for the script to finish */
+               while (waitpid(pid, &status, 0) == -1) {
+                       if (errno != EINTR) {
+                               logger(LOG_ERR, "waitpid: %s", strerror(errno));
+                               status = -1;
+                               break;
+                       }
                }
        }
 
@@ -167,7 +162,6 @@ exec_script(const struct options *options, const char *iface,
        while (*ep)
                free(*ep++);
        free(env);
-
        return status;
 }
 
@@ -375,7 +369,7 @@ configure(struct interface *iface, const char *reason,
                        delete_address(iface);
                }
 
-               exec_script(options, iface->name, reason, NULL, old);
+               run_script(options, iface->name, reason, NULL, old);
                return 0;
        }
 
@@ -419,6 +413,6 @@ configure(struct interface *iface, const char *reason,
                if (write_lease(iface, dhcp) == -1)
                        logger(LOG_ERR, "write_lease: %s", strerror(errno));
 
-       exec_script(options, iface->name, reason, dhcp, old);
+       run_script(options, iface->name, reason, dhcp, old);
        return 0;
 }
index c4c731cec2b6da4b9874bfbc885566c0fdd1281e..fe065db6d93271b6fbc95bfbf6bae4538a41ea28 100644 (file)
@@ -32,8 +32,8 @@
 #include "dhcp.h"
 #include "net.h"
 
-int exec_script(const struct options *, const char *, const char *,
-               const struct dhcp_message *, const struct dhcp_message *);
+int run_script(const struct options *, const char *, const char *,
+              const struct dhcp_message *, const struct dhcp_message *);
 int configure(struct interface *, const char *,
              const struct dhcp_message *, const struct dhcp_message *,
              const struct dhcp_lease *, const struct options *, int);