]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[nozzle] decouple running pre-up.d/up.d/down.d/post-down.d from interface status...
authorFabio M. Di Nitto <fdinitto@redhat.com>
Fri, 22 Dec 2017 04:07:33 +0000 (05:07 +0100)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Tue, 18 Dec 2018 08:30:22 +0000 (09:30 +0100)
provide the facility to do it via nozzle_run_updown but delegate the task to
the application.

This has the benefit of much better fine grained control over errors during those
code paths.

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
libnozzle/internals.c
libnozzle/internals.h
libnozzle/libnozzle.c
libnozzle/libnozzle.h

index a1e410045a2f74c54a2f07c34a45ff33b1b9cf94..48815e4d64db977fd1cdd6ee6d51710a98ea48df 100644 (file)
@@ -135,32 +135,6 @@ out_clean0:
        return err;
 }
 
-int run_updown(const nozzle_t nozzle, const char *action, char **error_string)
-{
-       char command[PATH_MAX];
-       struct stat sb;
-       int err = 0;
-
-       if (!nozzle->hasupdown)
-               return 0;
-
-       memset(command, 0, PATH_MAX);
-
-       snprintf(command, PATH_MAX, "%s%s/%s", nozzle->updownpath, action, nozzle->name);
-
-       err = stat(command, &sb);
-       if ((err < 0) && (errno == ENOENT))
-               return 0;
-
-       err = execute_bin_sh_command(command, error_string);
-       if ((!err) && (*error_string)) {
-               free(*error_string);
-               *error_string = NULL;
-       }
-
-       return err;
-}
-
 char *generate_v4_broadcast(const char *ipaddr, const char *prefix)
 {
        int prefix_len;
index 6a940ee956da98a602bd857ed4c0d0b922698526..f6c25a92fb5b9562365185cb5dd19e77c34fef88 100644 (file)
@@ -61,7 +61,6 @@ struct nozzle_iface {
 #define ifname ifr.ifr_name
 
 int execute_bin_sh_command(const char *command, char **error_string);
-int run_updown(const nozzle_t nozzle, const char *action, char **error_string);
 
 int find_ip(nozzle_t nozzle,
            const char *ipaddr, const char *prefix,
index 30b5d4ce0b54c94e30d3f1fbf15afc5c115c32af..948e836387db849b71596f8cc646094d2aace667 100644 (file)
@@ -99,8 +99,6 @@ static int _set_down(nozzle_t nozzle, char **error_down, char **error_postdown)
                goto out_clean;
        }
 
-       run_updown(nozzle, "down.d", error_down);
-
        ifr.ifr_flags &= ~IFF_UP;
 
        err = ioctl(lib_cfg.ioctlfd, SIOCSIFFLAGS, &ifr);
@@ -109,8 +107,6 @@ static int _set_down(nozzle_t nozzle, char **error_down, char **error_postdown)
                goto out_clean;
        }
 
-       run_updown(nozzle, "post-down.d", error_postdown);
-
        nozzle->up = 0;
 
 out_clean:
@@ -650,8 +646,6 @@ int nozzle_set_up(nozzle_t nozzle, char **error_preup, char **error_up)
                goto out_clean;
        }
 
-       run_updown(nozzle, "pre-up.d", error_preup);
-
        ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
        err = ioctl(lib_cfg.ioctlfd, SIOCSIFFLAGS, &ifr);
        if (err) {
@@ -659,8 +653,6 @@ int nozzle_set_up(nozzle_t nozzle, char **error_preup, char **error_up)
                goto out_clean;
        }
 
-       run_updown(nozzle, "up.d", error_up);
-
        nozzle->up = 1;
 
 out_clean:
@@ -894,6 +886,74 @@ out_clean:
        return err;
 }
 
+int nozzle_run_updown(const nozzle_t nozzle, uint8_t action, char **exec_string)
+{
+       int err = 0, savederrno = 0;
+       char command[PATH_MAX];
+       const char *action_str = NULL;
+       struct stat sb;
+
+       if (action > NOZZLE_POSTDOWN) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (!exec_string) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       savederrno = pthread_mutex_lock(&config_mutex);
+       if (savederrno) {
+               errno = savederrno;
+               return -1;
+       }
+
+       if (!is_valid_nozzle(nozzle)) {
+               savederrno = EINVAL;
+               err = -1;
+               goto out_clean;
+       }
+
+       if (!nozzle->hasupdown) {
+               savederrno = EINVAL;
+               err = -1;
+               goto out_clean;
+       }
+
+       switch(action) {
+               case NOZZLE_PREUP:
+                       action_str = "pre-up.d";
+                       break;
+               case NOZZLE_UP:
+                       action_str = "up.d";
+                       break;
+               case NOZZLE_DOWN:
+                       action_str = "down.d";
+                       break;
+               case NOZZLE_POSTDOWN:
+                       action_str = "post-down.d";
+                       break;
+       }
+
+       memset(command, 0, PATH_MAX);
+
+       snprintf(command, PATH_MAX, "%s%s/%s", nozzle->updownpath, action_str, nozzle->name);
+
+       err = stat(command, &sb);
+       if (err) {
+               savederrno = errno;
+               goto out_clean;
+       }
+
+       err = execute_bin_sh_command(command, exec_string);
+
+out_clean:
+       pthread_mutex_unlock(&config_mutex);
+       errno =  savederrno;
+       return err;
+}
+
 /*
  * functions below should be completed
  */
index dbbadf51b657b58c7c73877a5cbb415f3722ce98..5d28154e0f5415ba361269c63199c140ef7adb48 100644 (file)
@@ -77,6 +77,31 @@ nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath)
 
 int nozzle_close(nozzle_t nozzle, char **error_down, char **error_postdown);
 
+
+#define NOZZLE_PREUP    0
+#define NOZZLE_UP       1
+#define NOZZLE_DOWN     2
+#define NOZZLE_POSTDOWN 3
+
+/**
+ * nozzle_run_updown
+ * @brief execute updown commands associated with a nozzle device
+ *
+ * nozzle - pointer to the nozzle struct
+ *
+ * action - pre-up.d / up.d / down.d / post-down.d (see defines above)
+ *
+ * exec_string - pointers to string to record executing action stdout/stderr.
+ *               The string is malloc'ed, the caller needs to free the buffer.
+ *               If the script generates no output this string might be NULL.
+ *
+ * @return
+ * 0 on success
+ * -1 on error and errno is set.
+ */
+
+int nozzle_run_updown(const nozzle_t nozzle, uint8_t action, char **exec_string);
+
 /**
  * nozzle_set_up
  * @brief equivalent of ifconfig up, executes pre-up.d up.d if configured