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 <[email protected]>
---
 proc.c | 29 +++++++++++++++++++++++++++++
 proc.h |  1 +
 2 files changed, 30 insertions(+)

diff --git a/proc.c b/proc.c
index 672f00d..0937159 100644
--- a/proc.c
+++ b/proc.c
@@ -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,31 @@ ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, 
size_t len)
 
        return ret;
 }
+
+int proc_exec(char *proc, char **argv)
+{
+       int status;
+       pid_t pid;
+
+       pid = fork();
+       if (pid < 0) {
+               perror("fork");
+               return -1;
+       }
+       if (!pid) {
+               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..a4b5443 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(char *proc, char **argv);
 
 #endif /* PROC_H */
-- 
2.6.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to