Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.clone/dtrace_vfork_parent.c

raw
1 /* Systemtap test case
2 * Copyright (C) 2010, Red Hat Inc.
3 *
4 * This file is part of systemtap, and is free software. You can
5 * redistribute it and/or modify it under the terms of the GNU General
6 * Public License (GPL); either version 2, or (at your option) any
7 * later version.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sched.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include "dtrace_vfork_parent_probes.h"
19
20 #if !defined(USE_SEMAPHORES)
21 #undef PARENT_MAIN_ENABLED
22 #define PARENT_MAIN_ENABLED() (1)
23 #undef PARENT_CHILD_PID_ENABLED
24 #define PARENT_CHILD_PID_ENABLED() (1)
25 #undef PARENT_FINISHED_ENABLED
26 #define PARENT_FINISHED_ENABLED() (1)
27 #endif
28
29 int
30 main(int argc, char **argv)
31 {
32 int pid;
33 int rc = 0;
34 char *child_argv[] = { argv[1], NULL };
35
36 /* Create the child. */
37 if (PARENT_MAIN_ENABLED()) {
38 PARENT_MAIN(getpid());
39 }
40 pid = vfork();
41 if (pid == 0) { /* child */
42 rc = execve(argv[1], child_argv, NULL);
43 _exit(rc);
44 }
45
46 if (PARENT_CHILD_PID_ENABLED()) {
47 PARENT_CHILD_PID(pid);
48 }
49 if (pid < 0) {
50 /* error */
51 perror("fork failed:");
52 rc = -1;
53 }
54 else {
55 int status;
56
57 waitpid(pid, &status, 0);
58 rc = WEXITSTATUS(status);
59 }
60
61 if (PARENT_FINISHED_ENABLED()) {
62 PARENT_FINISHED();
63 }
64 return rc;
65 }