diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2015-12-15 23:09:10 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2015-12-17 09:40:50 +0100 |
commit | 1e3a2970549d294a1ab31f3088cbdcc2affbba42 (patch) | |
tree | 7eace0657d677a1d1be0b3130a4f7ff4dab7764b /proc.c | |
parent | aec98a903f19ae408fe699ff0963b90990c46d83 (diff) |
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 <vadim4j@gmail.com>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'proc.c')
-rw-r--r-- | proc.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1,5 +1,6 @@ #define _GNU_SOURCE #include <sched.h> +#include <sys/wait.h> #include <sys/types.h> #include <sys/resource.h> #include <unistd.h> @@ -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; +} |