From: David Ahern Date: Mon, 12 Dec 2016 00:53:11 +0000 (-0800) Subject: move cmd_exec to lib utils X-Git-Tag: v4.10.0~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08bd33d77f59bc74b9f7a2f8d85f9a6ef0f09690;p=thirdparty%2Fiproute2.git move cmd_exec to lib utils Code move only; no functional change intended. Signed-off-by: David Ahern --- diff --git a/include/utils.h b/include/utils.h index 26c970daa..ac4517a3b 100644 --- a/include/utils.h +++ b/include/utils.h @@ -256,4 +256,6 @@ char *int_to_str(int val, char *buf); int get_guid(__u64 *guid, const char *arg); int get_real_family(int rtm_type, int rtm_family); +int cmd_exec(const char *cmd, char **argv, bool do_fork); + #endif /* __UTILS_H__ */ diff --git a/ip/ipnetns.c b/ip/ipnetns.c index bd1e90137..db9a54176 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -357,40 +357,6 @@ static int netns_list(int argc, char **argv) return 0; } -static int cmd_exec(const char *cmd, char **argv, bool do_fork) -{ - fflush(stdout); - if (do_fork) { - int status; - pid_t pid; - - pid = fork(); - if (pid < 0) { - perror("fork"); - exit(1); - } - - if (pid != 0) { - /* Parent */ - if (waitpid(pid, &status, 0) < 0) { - perror("waitpid"); - exit(1); - } - - if (WIFEXITED(status)) { - return WEXITSTATUS(status); - } - - exit(1); - } - } - - if (execvp(cmd, argv) < 0) - fprintf(stderr, "exec of \"%s\" failed: %s\n", - cmd, strerror(errno)); - _exit(1); -} - static int on_netns_exec(char *nsname, void *arg) { char **argv = arg; diff --git a/lib/Makefile b/lib/Makefile index 5b7ec1690..749073261 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -8,7 +8,7 @@ CFLAGS += -fPIC UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \ inet_proto.o namespace.o json_writer.o \ - names.o color.o bpf.o + names.o color.o bpf.o exec.o NLOBJ=libgenl.o ll_map.o libnetlink.o diff --git a/lib/exec.c b/lib/exec.c new file mode 100644 index 000000000..97c991202 --- /dev/null +++ b/lib/exec.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#include "utils.h" + +int cmd_exec(const char *cmd, char **argv, bool do_fork) +{ + fflush(stdout); + if (do_fork) { + int status; + pid_t pid; + + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(1); + } + + if (pid != 0) { + /* Parent */ + if (waitpid(pid, &status, 0) < 0) { + perror("waitpid"); + exit(1); + } + + if (WIFEXITED(status)) { + return WEXITSTATUS(status); + } + + exit(1); + } + } + + if (execvp(cmd, argv) < 0) + fprintf(stderr, "exec of \"%s\" failed: %s\n", + cmd, strerror(errno)); + _exit(1); +}