Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/proc_fork_exec.c

raw
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/wait.h>
5
6 int
7 main(int argc, char **argv)
8 {
9 pid_t pid;
10 int rstatus;
11 char *new_argv[] = { NULL, "3", NULL };
12
13 if (argc != 2) {
14 fprintf(stderr, "Usage: %s exepath\n", argv[0]);
15 return -1;
16 }
17
18 new_argv[0] = argv[1];
19
20 pid = fork();
21 if (pid < 0)
22 {
23 perror("fork");
24 return -1;
25 }
26
27 if (pid == 0) /* child process */
28 {
29 /* Actually run the command. */
30 if (execv(new_argv[0], new_argv) < 0)
31 perror("execv");
32 _exit(1);
33 }
34
35 if (waitpid(pid, &rstatus, 0) < 0) {
36 perror("waitpid");
37 return -1;
38 }
39
40 if (WIFEXITED(rstatus))
41 return WEXITSTATUS(rstatus);
42 return -1;
43 }