From 1e3a2970549d294a1ab31f3088cbdcc2affbba42 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Tue, 15 Dec 2015 23:09:10 +0200 Subject: proc: Add function to execute process with argv list Add proc_exec function which executes given process with argv list via fork + execvp. It allows to replace 'system' call approach which is used for invoking cpp and securely extend it with additional options like -D. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- proc.c | 27 +++++++++++++++++++++++++++ proc.h | 1 + 2 files changed, 28 insertions(+) diff --git a/proc.c b/proc.c index 672f00d..b0c577c 100644 --- a/proc.c +++ b/proc.c @@ -1,5 +1,6 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -79,3 +80,29 @@ ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len) return ret; } + +int proc_exec(const char *proc, char *const argv[]) +{ + int status; + pid_t pid; + + pid = fork(); + if (pid < 0) { + perror("fork"); + return -1; + } else if (pid == 0) { + if (execvp(proc, argv) < 0) + fprintf(stderr, "Failed to exec: %s\n", proc); + _exit(1); + } + + if (waitpid(pid, &status, 0) < 0) { + perror("waitpid"); + return -2; + } + + if (!WIFEXITED(status)) + return -WEXITSTATUS(status); + + return 0; +} diff --git a/proc.h b/proc.h index 996ce06..9220b2a 100644 --- a/proc.h +++ b/proc.h @@ -7,5 +7,6 @@ extern void cpu_affinity(int cpu); extern int set_proc_prio(int prio); extern int set_sched_status(int policy, int priority); extern ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len); +extern int proc_exec(const char *proc, char *const argv[]); #endif /* PROC_H */ -- cgit v1.2.3-54-g00ecf