Skip to content

Commit

Permalink
[nozzle] decouple running pre-up.d/up.d/down.d/post-down.d from inter…
Browse files Browse the repository at this point in the history
…face status (part 1)

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>
  • Loading branch information
fabbione committed Apr 20, 2018
1 parent 4130d8d commit e6baab6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 35 deletions.
26 changes: 0 additions & 26 deletions libnozzle/internals.c
Expand Up @@ -135,32 +135,6 @@ int execute_bin_sh_command(const char *command, char **error_string)
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;
Expand Down
1 change: 0 additions & 1 deletion libnozzle/internals.h
Expand Up @@ -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,
Expand Down
76 changes: 68 additions & 8 deletions libnozzle/libnozzle.c
Expand Up @@ -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);
Expand All @@ -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:
Expand Down Expand Up @@ -650,17 +646,13 @@ 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) {
savederrno = errno;
goto out_clean;
}

run_updown(nozzle, "up.d", error_up);

nozzle->up = 1;

out_clean:
Expand Down Expand Up @@ -894,6 +886,74 @@ int nozzle_get_ips(const nozzle_t nozzle, char **ipaddr_list, int *entries)
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
*/
Expand Down
25 changes: 25 additions & 0 deletions libnozzle/libnozzle.h
Expand Up @@ -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
Expand Down

0 comments on commit e6baab6

Please sign in to comment.