Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.clone/dtrace_fork_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_fork_parent_probes.h"
19
20 #if !defined(USE_SEMAPHORES)
21 #undef PARENT_MAIN_ENABLED
22 #define PARENT_MAIN_ENABLED() (1)
23 #undef PARENT_CHILD_ENABLED
24 #define PARENT_CHILD_ENABLED() (1)
25 #undef PARENT_CHILD_PID_ENABLED
26 #define PARENT_CHILD_PID_ENABLED() (1)
27 #undef PARENT_FINISHED_ENABLED
28 #define PARENT_FINISHED_ENABLED() (1)
29 #endif
30
31 int
32 main(int argc, char **argv)
33 {
34 int pid;
35 int rc = 0;
36 char *child_argv[] = { argv[1], NULL };
37
38 /* Create the child. */
39 if (PARENT_MAIN_ENABLED()) {
40 PARENT_MAIN(getpid());
41 }
42 pid = fork();
43 if (pid == 0) { /* child */
44 if (PARENT_CHILD_ENABLED()) {
45 PARENT_CHILD(getpid());
46 }
47 rc = execve(argv[1], child_argv, NULL);
48 _exit(rc);
49 }
50
51 if (PARENT_CHILD_PID_ENABLED()) {
52 PARENT_CHILD_PID(pid);
53 }
54 if (pid < 0) {
55 /* error */
56 perror("fork failed:");
57 rc = -1;
58 }
59 else {
60 int status;
61
62 waitpid(pid, &status, 0);
63 rc = WEXITSTATUS(status);
64 }
65
66 if (PARENT_FINISHED_ENABLED()) {
67 PARENT_FINISHED();
68 }
69 return rc;
70 }