| 1 | /* |
| 2 | * Copyright (C) 2009 Pierre-Marc Fournier |
| 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: LGPL-2.1-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/wait.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #define TRACEPOINT_DEFINE |
| 16 | #define TRACEPOINT_CREATE_PROBES |
| 17 | #include "ust_tests_fork.h" |
| 18 | |
| 19 | int main(int argc, char **argv, char *env[]) |
| 20 | { |
| 21 | int result; |
| 22 | |
| 23 | if (argc < 2) { |
| 24 | fprintf(stderr, "usage: fork PROG_TO_EXEC\n"); |
| 25 | exit(1); |
| 26 | } |
| 27 | |
| 28 | printf("parent_pid %d\n", getpid()); |
| 29 | tracepoint(ust_tests_fork, before_fork, getpid()); |
| 30 | |
| 31 | result = fork(); |
| 32 | if (result == -1) { |
| 33 | perror("fork"); |
| 34 | return 1; |
| 35 | } |
| 36 | if (result == 0) { |
| 37 | char *args[] = { (char *) "fork2", NULL }; |
| 38 | |
| 39 | tracepoint(ust_tests_fork, after_fork_child, getpid()); |
| 40 | |
| 41 | result = execve(argv[1], args, env); |
| 42 | if (result == -1) { |
| 43 | perror("execve"); |
| 44 | result = 1; |
| 45 | goto end; |
| 46 | } |
| 47 | } else { |
| 48 | printf("child_pid %d\n", result); |
| 49 | tracepoint(ust_tests_fork, after_fork_parent, getpid()); |
| 50 | if (waitpid(result, NULL, 0) < 0) { |
| 51 | perror("waitpid"); |
| 52 | result = 1; |
| 53 | goto end; |
| 54 | } |
| 55 | } |
| 56 | result = 0; |
| 57 | end: |
| 58 | return result; |
| 59 | } |