diff --git a/server/src/tc.c b/server/src/tc.c index 2804f74f5..bfda86339 100644 --- a/server/src/tc.c +++ b/server/src/tc.c @@ -20,9 +20,8 @@ int tc_reset(struct interface *i) snprintf(cmd, sizeof(cmd), "tc qdisc del dev %s root", i->name); debug(6, "Reset traffic control for interface '%s'", i->name); - debug(8, "System: %s", cmd); - return system(cmd); + return system2(cmd); } int tc_prio(struct interface *i, tc_hdl_t handle, int bands) @@ -39,9 +38,8 @@ int tc_prio(struct interface *i, tc_hdl_t handle, int bands) len += snprintf(cmd+len, sizeof(cmd)-len, " %u", priomap[i] + bands); debug(6, "Replace master qdisc for interface '%s'", i->name); - debug(8, "System: %s", cmd); - return system(cmd); + return system2(cmd); } int tc_netem(struct interface *i, tc_hdl_t parent, struct netem *em) @@ -69,9 +67,8 @@ int tc_netem(struct interface *i, tc_hdl_t parent, struct netem *em) len += snprintf(cmd+len, sizeof(cmd)-len, " corrupt %u", em->corrupt); debug(6, "Setup netem qdisc for interface '%s'", i->name); - debug(8, "System: %s", cmd); - return system(cmd); + return system2(cmd); } int tc_mark(struct interface *i, tc_hdl_t flowid, int mark) @@ -83,8 +80,7 @@ int tc_mark(struct interface *i, tc_hdl_t flowid, int mark) debug(7, "Add traffic filter to interface '%s': fwmark %u => flowid %u:%u", i->name, mark, TC_HDL_MAJ(flowid), TC_HDL_MIN(flowid)); - debug(8, "System: %s", cmd); - return system(cmd); + return system2(cmd); } diff --git a/server/src/utils.c b/server/src/utils.c index b585b8cd9..0bbf3ff25 100644 --- a/server/src/utils.c +++ b/server/src/utils.c @@ -4,6 +4,7 @@ * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC */ +#include #include #include #include @@ -144,3 +145,30 @@ void hist_dump(unsigned *hist, int length) info("Matlab: hist = [ %s]", buf); } + +/** @todo: Proper way: create additional pipe for stderr in child process */ +int system2(const char *cmd, ...) +{ + char buf[1024]; + + va_list ap; + va_start(ap, cmd); + + vsnprintf(buf, sizeof(buf), cmd, ap); + strncat(buf, " 2>&1", sizeof(buf)); + + va_end(ap); + + debug(1, "System: %s", buf); + + FILE *f = popen(buf, "r"); + if (f == NULL) + perror("Failed to execute: '%s'", cmd); + + while (!feof(f) && fgets(buf, sizeof(buf), f) != NULL) { INDENT + strtok(buf, "\n"); /* strip trailing newline */ + info(buf); + } + + return pclose(f); +}