Skip to content

Commit

Permalink
[nozzle] start to cleanup nozzle_close to be more informative and eff…
Browse files Browse the repository at this point in the history
…ective

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Apr 19, 2018
1 parent 4b94dc1 commit b403e14
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 34 deletions.
207 changes: 174 additions & 33 deletions libnozzle/libnozzle.c
Expand Up @@ -564,20 +564,22 @@ nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath)
return NULL;
}

// TODO: consider better error report from here
int nozzle_close(nozzle_t nozzle)
int nozzle_close(nozzle_t nozzle, char **error_down, char **error_postdown)
{
int err = 0;
int err = 0, savederrno = 0;
nozzle_t temp = lib_cfg.head;
nozzle_t prev = lib_cfg.head;
struct _ip *ip, *ip_next;
char *error_string = NULL;
char *error_down = NULL, *error_postdown = NULL;

pthread_mutex_lock(&lib_mutex);
savederrno = pthread_mutex_lock(&lib_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!_check(nozzle)) {
errno = EINVAL;
savederrno = EINVAL;
err = -1;
goto out_clean;
}
Expand All @@ -593,11 +595,7 @@ int nozzle_close(nozzle_t nozzle)
prev->next = nozzle->next;
}

_set_down(nozzle, &error_down, &error_postdown);
if (error_down)
free(error_down);
if (error_postdown)
free(error_postdown);
_set_down(nozzle, error_down, error_postdown);

ip = nozzle->ip;
while (ip) {
Expand All @@ -616,7 +614,7 @@ int nozzle_close(nozzle_t nozzle)

out_clean:
pthread_mutex_unlock(&lib_mutex);

errno = savederrno;
return err;
}

Expand Down Expand Up @@ -1194,6 +1192,7 @@ static int is_if_in_system(char *name)
static int test_iface(char *name, size_t size, const char *updownpath)
{
nozzle_t nozzle;
char *error_down = NULL, *error_postdown = NULL;

nozzle=nozzle_open(name, size, updownpath);
if (!nozzle) {
Expand All @@ -1216,7 +1215,17 @@ static int test_iface(char *name, size_t size, const char *updownpath)
printf("Found interface %s in nozzle db\n", name);
}

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);

if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}

if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}

if (is_if_in_system(name) == 0)
printf("Successfully removed interface %s from the system\n", name);
Expand Down Expand Up @@ -1318,6 +1327,7 @@ static int check_knet_multi_eth(void)
int err=0;
nozzle_t nozzle1 = NULL;
nozzle_t nozzle2 = NULL;
char *error_down = NULL, *error_postdown = NULL;

printf("Testing multiple knet interface instances\n");

Expand Down Expand Up @@ -1350,10 +1360,29 @@ static int check_knet_multi_eth(void)
printf("Unable to find interface %s on the system\n", device_name2);
}

if (nozzle1)
nozzle_close(nozzle1);
if (nozzle2)
nozzle_close(nozzle2);
if (nozzle1) {
nozzle_close(nozzle1, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

if (nozzle2) {
nozzle_close(nozzle2, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

printf("Testing error conditions\n");

Expand Down Expand Up @@ -1382,10 +1411,30 @@ static int check_knet_multi_eth(void)
}

out_clean:
if (nozzle1)
nozzle_close(nozzle1);
if (nozzle2)
nozzle_close(nozzle2);
if (nozzle1) {
nozzle_close(nozzle1, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

if (nozzle2) {
nozzle_close(nozzle2, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

return err;
}

Expand All @@ -1395,6 +1444,7 @@ static int check_knet_mtu(void)
size_t size = IFNAMSIZ;
int err=0;
nozzle_t nozzle;
char *error_down = NULL, *error_postdown = NULL;

int current_mtu = 0;
int expected_mtu = 1500;
Expand Down Expand Up @@ -1458,7 +1508,17 @@ static int check_knet_mtu(void)
}

out_clean:
nozzle_close(nozzle);
if (nozzle) {
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

return err;
}
Expand All @@ -1471,6 +1531,7 @@ static int check_knet_mtu_ipv6(void)
int err=0;
nozzle_t nozzle;
char *error_string = NULL;
char *error_down = NULL, *error_postdown = NULL;

printf("Testing get/set MTU with IPv6 address\n");

Expand Down Expand Up @@ -1622,7 +1683,17 @@ static int check_knet_mtu_ipv6(void)
}

out_clean:
nozzle_close(nozzle);
if (nozzle) {
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

return err;
}
Expand All @@ -1635,6 +1706,7 @@ static int check_knet_mac(void)
nozzle_t nozzle;
char *current_mac = NULL, *temp_mac = NULL, *err_mac = NULL;
struct ether_addr *cur_mac, *tmp_mac;
char *error_down = NULL, *error_postdown = NULL;

printf("Testing get/set MAC\n");

Expand Down Expand Up @@ -1728,7 +1800,17 @@ static int check_knet_mac(void)
if (temp_mac)
free(temp_mac);

nozzle_close(nozzle);
if (nozzle) {
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("Error from error_down: %s\n", error_down);
free(error_down);
}
if (error_postdown) {
printf("Error from error_down: %s\n", error_postdown);
free(error_postdown);
}
}

return err;
}
Expand Down Expand Up @@ -1925,7 +2007,17 @@ static int check_knet_up_down(void)
goto out_clean;
}

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

printf("Testing interface pre-up/up/down/post-down (exec errors)\n");

Expand Down Expand Up @@ -1974,7 +2066,17 @@ static int check_knet_up_down(void)
goto out_clean;
}

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

printf("Testing interface pre-up/up/down/post-down\n");

Expand Down Expand Up @@ -2024,7 +2126,17 @@ static int check_knet_up_down(void)
goto out_clean;
}

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

printf("Test ERROR conditions\n");

Expand Down Expand Up @@ -2077,8 +2189,17 @@ static int check_knet_up_down(void)
}

out_clean:

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

return err;
}
Expand All @@ -2090,6 +2211,7 @@ static int check_knet_close_leak(void)
int err=0;
nozzle_t nozzle;
char *error_string = NULL;
char *error_down = NULL, *error_postdown = NULL;

printf("Testing close leak (needs valgrind)\n");

Expand Down Expand Up @@ -2130,8 +2252,17 @@ static int check_knet_close_leak(void)
}

out_clean:

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

return err;
}
Expand All @@ -2146,6 +2277,7 @@ static int check_knet_set_del_ip(void)
char *ip_list = NULL;
int ip_list_entries = 0, i, offset = 0;
char *error_string = NULL;
char *error_down = NULL, *error_postdown = NULL;

printf("Testing interface add/remove ip\n");

Expand Down Expand Up @@ -2374,8 +2506,17 @@ static int check_knet_set_del_ip(void)
}

out_clean:

nozzle_close(nozzle);
nozzle_close(nozzle, &error_down, &error_postdown);
if (error_down) {
printf("down output: %s\n", error_down);
free(error_down);
error_down = NULL;
}
if (error_postdown) {
printf("postdown output: %s\n", error_down);
free(error_down);
error_down = NULL;
}

return err;
}
Expand Down
24 changes: 23 additions & 1 deletion libnozzle/libnozzle.h
Expand Up @@ -53,7 +53,29 @@ typedef struct nozzle_iface *nozzle_t;

nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath);

int nozzle_close(nozzle_t nozzle);
/**
* nozzle_close
* @brief deconfigure and destroy a nozzle device
*
* nozzle - pointer to the nozzle struct to destroy
*
* error_down - pointers to string to record errors from executing down.d
* when configured. The string is malloc'ed, the caller needs to free those
* buffers.
*
* error_postdown - pointers to string to record errors from executing post-down.d
* when configured. The string is malloc'ed, the caller needs to free
* those buffers.
*
* @return
* 0 on success
* -1 on error and error is set.
* error_down / error_postdown are set to NULL if execution of external scripts
* is sucessful
* error_down / error_postdown will contain strings recording the execution error.
*/

int nozzle_close(nozzle_t nozzle, char **error_down, char **error_postdown);

nozzle_t nozzle_find(char *dev, size_t dev_size);

Expand Down

0 comments on commit b403e14

Please sign in to comment.