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