Skip to content

Commit

Permalink
Fix dying without a message on failed verbose_system calls, improve i…
Browse files Browse the repository at this point in the history
…mplementation
  • Loading branch information
Alexander Funke committed Jun 2, 2022
1 parent 675d786 commit 2f6914c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
13 changes: 7 additions & 6 deletions gtests/net/packetdrill/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static void create_device(struct config *config, struct local_netdev *netdev)
if (config->mtu != TUN_DRIVER_DEFAULT_MTU) {
asprintf(&command, "ifconfig %s mtu %d", netdev->name, config->mtu);
if (verbose_system(command) != STATUS_OK)
die("");
die("Error executing %s\n", command);
free(command);
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ static void create_device(struct config *config, struct local_netdev *netdev)
#if defined(__FreeBSD__)
if ((tun_fd < 0) && (errno == ENOENT)) {
if (verbose_system("kldload -q if_tun") != STATUS_OK) {
die_perror("");
die_perror("kldload -q if_tun");
}
tun_fd = open(tun_path, O_RDWR);
}
Expand Down Expand Up @@ -268,7 +268,7 @@ static void create_device(struct config *config, struct local_netdev *netdev)
asprintf(&command, "ethtool -s %s speed %u autoneg off",
netdev->name, config->speed);
if (verbose_system(command) != STATUS_OK)
die("");
die("Error executing %s\n", command);
free(command);

/* Need to bring interface down and up so the interface speed
Expand All @@ -277,7 +277,7 @@ static void create_device(struct config *config, struct local_netdev *netdev)
asprintf(&command, "ifconfig %s down; sleep 1; ifconfig %s up; "
"sleep 1", netdev->name, netdev->name);
if(verbose_system(command) != STATUS_OK)
die("");
die("Error executing %s\n", command);
free(command);
}

Expand All @@ -286,7 +286,7 @@ static void create_device(struct config *config, struct local_netdev *netdev)
asprintf(&command, "ifconfig %s mtu %d",
netdev->name, config->mtu);
if(verbose_system(command) != STATUS_OK)
die("");
die("Error executing route command '%s'\n", command);
free(command);
}
#endif
Expand Down Expand Up @@ -369,7 +369,8 @@ static void route_traffic_to_device(struct config *config,
assert(!"bad wire protocol");
}
#endif /* defined(linux) */
verbose_system(route_command);
if(verbose_system(command) != STATUS_OK)
die("Error executing %s\n", command);
free(route_command);
}

Expand Down
31 changes: 15 additions & 16 deletions gtests/net/packetdrill/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@

#include "logging.h"

static int checked_system(const char *command, char **error)
static void checked_system(const char *command, char **error)
{
int result = system(command);
if (result == -1) {
int status;
status = system(command);
if (status == -1) {
asprintf(error, "%s", strerror(errno));
} else if (WIFSIGNALED(result) &&
(WTERMSIG(result) == SIGINT || WTERMSIG(result) == SIGQUIT)) {
} else if (WIFSIGNALED(status) &&
(WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGQUIT)) {
asprintf(error, "got signal %d (%s)",
WTERMSIG(result), strsignal(WTERMSIG(result)));
} else if (WEXITSTATUS(result) != 0) {
asprintf(error, "non-zero status %d", WEXITSTATUS(result));
WTERMSIG(status), strsignal(WTERMSIG(status)));
} else if (WEXITSTATUS(status) != 0) {
asprintf(error, "non-zero status %d", WEXITSTATUS(status));
} else {
*error = NULL;
}
return result;
}

int safe_system(const char *command, char **error)
{
assert(*error == NULL);
checked_system(command, error);
if (*error != NULL)
return STATUS_ERR;
Expand All @@ -59,15 +60,13 @@ int safe_system(const char *command, char **error)

int verbose_system(const char *command)
{
int result;
char *error = NULL;

DEBUGP("running: '%s'\n", command);
result = checked_system(command, &error);
if (result != 0) {
checked_system(command, &error);
if (*error != NULL) {
DEBUGP("error: %s executing command '%s'\n", error, command);
} else {
DEBUGP("result: %d\n", result);
return STATUS_ERR;
}
return result;
return STATUS_OK;
}

0 comments on commit 2f6914c

Please sign in to comment.