| 1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <unistd.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include <ust/marker.h> |
| 24 | |
| 25 | int main(int argc, char **argv, char *env[]) |
| 26 | { |
| 27 | int result; |
| 28 | |
| 29 | if(argc < 2 ) { |
| 30 | fprintf(stderr, "usage: fork PROG_TO_EXEC\n"); |
| 31 | exit(1); |
| 32 | } |
| 33 | |
| 34 | printf("Fork test program, parent pid is %d\n", getpid()); |
| 35 | trace_mark(ust, before_fork, MARK_NOARGS); |
| 36 | |
| 37 | /* Sleep here to make sure the consumer is initialized before we fork */ |
| 38 | sleep(1); |
| 39 | |
| 40 | result = fork(); |
| 41 | if(result == -1) { |
| 42 | perror("fork"); |
| 43 | return 1; |
| 44 | } |
| 45 | if(result == 0) { |
| 46 | char *args[] = {"fork2", NULL}; |
| 47 | |
| 48 | printf("Child pid is %d\n", getpid()); |
| 49 | |
| 50 | trace_mark(ust, after_fork_child, MARK_NOARGS); |
| 51 | |
| 52 | trace_mark(ust, before_exec, "pid %d", getpid()); |
| 53 | |
| 54 | result = execve(argv[1], args, env); |
| 55 | if(result == -1) { |
| 56 | perror("execve"); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | trace_mark(ust, after_exec, "pid %d", getpid()); |
| 61 | } |
| 62 | else { |
| 63 | trace_mark(ust, after_fork_parent, MARK_NOARGS); |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |