Commit | Line | Data |
---|---|---|
2d99476b PMF |
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 | #define _GNU_SOURCE | |
19 | #include <dlfcn.h> | |
20 | #include <unistd.h> | |
21 | #include <stdio.h> | |
df793c55 | 22 | #include <signal.h> |
616ed36a PMF |
23 | #include <ust/ust.h> |
24 | #include <sched.h> | |
25 | #include <stdarg.h> | |
26 | #include <ust/tracectl.h> | |
fbca6b62 | 27 | #include "usterr.h" |
2d99476b | 28 | |
18126790 PMF |
29 | struct user_desc; |
30 | ||
2d99476b PMF |
31 | pid_t fork(void) |
32 | { | |
33 | static pid_t (*plibc_func)(void) = NULL; | |
616ed36a | 34 | ust_fork_info_t fork_info; |
2d99476b PMF |
35 | |
36 | pid_t retval; | |
37 | ||
38 | if(plibc_func == NULL) { | |
39 | plibc_func = dlsym(RTLD_NEXT, "fork"); | |
40 | if(plibc_func == NULL) { | |
41 | fprintf(stderr, "libcwrap: unable to find fork\n"); | |
2c10b7fd | 42 | return -1; |
2d99476b PMF |
43 | } |
44 | } | |
45 | ||
616ed36a | 46 | ust_before_fork(&fork_info); |
df793c55 PMF |
47 | |
48 | /* Do the real fork */ | |
2d99476b PMF |
49 | retval = plibc_func(); |
50 | ||
df793c55 PMF |
51 | if(retval == 0) { |
52 | /* child */ | |
616ed36a | 53 | ust_after_fork_child(&fork_info); |
df793c55 | 54 | } |
616ed36a PMF |
55 | else { |
56 | ust_after_fork_parent(&fork_info); | |
df793c55 | 57 | } |
2d99476b PMF |
58 | |
59 | return retval; | |
60 | } | |
97b042a3 PMF |
61 | |
62 | int execve(const char *filename, char *const argv[], char *const envp[]) | |
63 | { | |
64 | static int (*plibc_func)(const char *filename, char *const argv[], char *const envp[]) = NULL; | |
65 | ||
66 | pid_t retval; | |
67 | ||
68 | if(plibc_func == NULL) { | |
69 | plibc_func = dlsym(RTLD_NEXT, "execve"); | |
70 | if(plibc_func == NULL) { | |
616ed36a | 71 | fprintf(stderr, "libinterfork: unable to find execve\n"); |
97b042a3 PMF |
72 | return -1; |
73 | } | |
74 | } | |
75 | ||
76 | ust_potential_exec(); | |
77 | ||
78 | retval = plibc_func(filename, argv, envp); | |
79 | ||
80 | return retval; | |
81 | } | |
616ed36a PMF |
82 | |
83 | struct interfork_clone_info { | |
84 | int (*fn)(void *); | |
85 | void *arg; | |
73513f95 | 86 | ust_fork_info_t fork_info; |
616ed36a PMF |
87 | }; |
88 | ||
89 | static int clone_fn(void *arg) | |
90 | { | |
91 | struct interfork_clone_info *info = (struct interfork_clone_info *)arg; | |
92 | ||
93 | /* clone is now done and we are in child */ | |
94 | ust_after_fork_child(&info->fork_info); | |
95 | ||
96 | return info->fn(info->arg); | |
97 | } | |
98 | ||
99 | int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...) | |
100 | { | |
101 | static int (*plibc_func)(int (*fn)(void *), void *child_stack, int flags, void *arg, pid_t *ptid, struct user_desc *tls, pid_t *ctid) = NULL; | |
102 | ||
103 | /* varargs */ | |
104 | pid_t *ptid; | |
105 | struct user_desc *tls; | |
106 | pid_t *ctid; | |
107 | ||
108 | int retval; | |
109 | ||
110 | va_list ap; | |
111 | ||
112 | va_start(ap, arg); | |
113 | ptid = va_arg(ap, pid_t *); | |
114 | tls = va_arg(ap, struct user_desc *); | |
115 | ctid = va_arg(ap, pid_t *); | |
116 | va_end(ap); | |
117 | ||
118 | if(plibc_func == NULL) { | |
119 | plibc_func = dlsym(RTLD_NEXT, "clone"); | |
120 | if(plibc_func == NULL) { | |
121 | fprintf(stderr, "libinterfork: unable to find clone\n"); | |
122 | return -1; | |
123 | } | |
124 | } | |
125 | ||
126 | if(flags & CLONE_VM) { | |
127 | /* creating a thread, no need to intervene, just pass on the arguments */ | |
128 | retval = plibc_func(fn, child_stack, flags, arg, ptid, tls, ctid); | |
129 | } | |
130 | else { | |
131 | /* creating a real process, we need to intervene */ | |
132 | struct interfork_clone_info info = { fn: fn, arg: arg }; | |
133 | ||
134 | ust_before_fork(&info.fork_info); | |
135 | ||
136 | retval = plibc_func(clone_fn, child_stack, flags, &info, ptid, tls, ctid); | |
137 | ||
138 | /* The child doesn't get here */ | |
139 | ust_after_fork_parent(&info.fork_info); | |
140 | } | |
141 | ||
142 | return retval; | |
143 | } |