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;
#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,
goto out_clean;
}
- run_updown(nozzle, "down.d", error_down);
-
ifr.ifr_flags &= ~IFF_UP;
err = ioctl(lib_cfg.ioctlfd, SIOCSIFFLAGS, &ifr);
goto out_clean;
}
- run_updown(nozzle, "post-down.d", error_postdown);
-
nozzle->up = 0;
out_clean:
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) {
goto out_clean;
}
- run_updown(nozzle, "up.d", error_up);
-
nozzle->up = 1;
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
*/
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