Commit | Line | Data |
---|---|---|
e822f505 MD |
1 | /* |
2 | * Copyright (C) 2009 Pierre-Marc Fournier | |
3678c8aa | 3 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2d99476b PMF |
4 | * |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
e822f505 MD |
7 | * License as published by the Free Software Foundation; version 2.1 of |
8 | * the License. | |
2d99476b PMF |
9 | * |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
f02baefb | 21 | #include <lttng/ust-dlfcn.h> |
2d99476b PMF |
22 | #include <unistd.h> |
23 | #include <stdio.h> | |
df793c55 | 24 | #include <signal.h> |
616ed36a PMF |
25 | #include <sched.h> |
26 | #include <stdarg.h> | |
eb2b066f | 27 | #include <errno.h> |
2d99476b | 28 | |
4318ae1b | 29 | #include <lttng/ust.h> |
e822f505 | 30 | |
2d99476b PMF |
31 | pid_t fork(void) |
32 | { | |
33 | static pid_t (*plibc_func)(void) = NULL; | |
7f0aeeba | 34 | sigset_t sigset; |
2d99476b | 35 | pid_t retval; |
111902ab | 36 | int saved_errno; |
2d99476b | 37 | |
e822f505 | 38 | if (plibc_func == NULL) { |
2d99476b | 39 | plibc_func = dlsym(RTLD_NEXT, "fork"); |
e822f505 MD |
40 | if (plibc_func == NULL) { |
41 | fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n"); | |
3678c8aa | 42 | errno = ENOSYS; |
2c10b7fd | 43 | return -1; |
2d99476b PMF |
44 | } |
45 | } | |
46 | ||
7f0aeeba | 47 | ust_before_fork(&sigset); |
df793c55 | 48 | /* Do the real fork */ |
2d99476b | 49 | retval = plibc_func(); |
111902ab | 50 | saved_errno = errno; |
e822f505 | 51 | if (retval == 0) { |
df793c55 | 52 | /* child */ |
7f0aeeba | 53 | ust_after_fork_child(&sigset); |
e822f505 | 54 | } else { |
7f0aeeba | 55 | ust_after_fork_parent(&sigset); |
df793c55 | 56 | } |
111902ab | 57 | errno = saved_errno; |
2d99476b PMF |
58 | return retval; |
59 | } | |
97b042a3 | 60 | |
2f6150e9 MD |
61 | int daemon(int nochdir, int noclose) |
62 | { | |
63 | static int (*plibc_func)(int nochdir, int noclose) = NULL; | |
64 | sigset_t sigset; | |
65 | int retval; | |
111902ab | 66 | int saved_errno; |
2f6150e9 MD |
67 | |
68 | if (plibc_func == NULL) { | |
69 | plibc_func = dlsym(RTLD_NEXT, "daemon"); | |
70 | if (plibc_func == NULL) { | |
71 | fprintf(stderr, "libustfork: unable to find \"daemon\" symbol\n"); | |
72 | errno = ENOSYS; | |
73 | return -1; | |
74 | } | |
75 | } | |
76 | ||
77 | ust_before_fork(&sigset); | |
78 | /* Do the real daemon call */ | |
79 | retval = plibc_func(nochdir, noclose); | |
111902ab | 80 | saved_errno = errno; |
2f6150e9 MD |
81 | if (retval == 0) { |
82 | /* child, parent called _exit() directly */ | |
83 | ust_after_fork_child(&sigset); | |
84 | } else { | |
85 | /* on error in the parent */ | |
86 | ust_after_fork_parent(&sigset); | |
87 | } | |
111902ab | 88 | errno = saved_errno; |
2f6150e9 MD |
89 | return retval; |
90 | } | |
91 | ||
939c89cb MD |
92 | #ifdef __linux__ |
93 | ||
94 | struct user_desc; | |
95 | ||
e822f505 | 96 | struct ustfork_clone_info { |
616ed36a PMF |
97 | int (*fn)(void *); |
98 | void *arg; | |
7f0aeeba | 99 | sigset_t sigset; |
616ed36a PMF |
100 | }; |
101 | ||
102 | static int clone_fn(void *arg) | |
103 | { | |
e822f505 | 104 | struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg; |
616ed36a PMF |
105 | |
106 | /* clone is now done and we are in child */ | |
7f0aeeba | 107 | ust_after_fork_child(&info->sigset); |
616ed36a PMF |
108 | return info->fn(info->arg); |
109 | } | |
110 | ||
111 | int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...) | |
112 | { | |
e822f505 MD |
113 | static int (*plibc_func)(int (*fn)(void *), void *child_stack, |
114 | int flags, void *arg, pid_t *ptid, | |
115 | struct user_desc *tls, pid_t *ctid) = NULL; | |
116 | /* var args */ | |
616ed36a PMF |
117 | pid_t *ptid; |
118 | struct user_desc *tls; | |
119 | pid_t *ctid; | |
e822f505 | 120 | /* end of var args */ |
616ed36a | 121 | va_list ap; |
e822f505 | 122 | int retval; |
111902ab | 123 | int saved_errno; |
616ed36a PMF |
124 | |
125 | va_start(ap, arg); | |
126 | ptid = va_arg(ap, pid_t *); | |
127 | tls = va_arg(ap, struct user_desc *); | |
128 | ctid = va_arg(ap, pid_t *); | |
129 | va_end(ap); | |
130 | ||
e822f505 | 131 | if (plibc_func == NULL) { |
616ed36a | 132 | plibc_func = dlsym(RTLD_NEXT, "clone"); |
e822f505 MD |
133 | if (plibc_func == NULL) { |
134 | fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n"); | |
3678c8aa | 135 | errno = ENOSYS; |
616ed36a PMF |
136 | return -1; |
137 | } | |
138 | } | |
139 | ||
e822f505 MD |
140 | if (flags & CLONE_VM) { |
141 | /* | |
142 | * Creating a thread, no need to intervene, just pass on | |
143 | * the arguments. | |
144 | */ | |
145 | retval = plibc_func(fn, child_stack, flags, arg, ptid, | |
146 | tls, ctid); | |
111902ab | 147 | saved_errno = errno; |
e822f505 MD |
148 | } else { |
149 | /* Creating a real process, we need to intervene. */ | |
3fd2b913 | 150 | struct ustfork_clone_info info = { .fn = fn, .arg = arg }; |
616ed36a | 151 | |
7f0aeeba | 152 | ust_before_fork(&info.sigset); |
e822f505 MD |
153 | retval = plibc_func(clone_fn, child_stack, flags, &info, |
154 | ptid, tls, ctid); | |
111902ab | 155 | saved_errno = errno; |
e822f505 | 156 | /* The child doesn't get here. */ |
7f0aeeba | 157 | ust_after_fork_parent(&info.sigset); |
616ed36a | 158 | } |
111902ab | 159 | errno = saved_errno; |
616ed36a PMF |
160 | return retval; |
161 | } | |
939c89cb | 162 | |
735bef47 MJ |
163 | int setns(int fd, int nstype) |
164 | { | |
165 | static int (*plibc_func)(int fd, int nstype) = NULL; | |
166 | int retval; | |
167 | int saved_errno; | |
168 | ||
169 | if (plibc_func == NULL) { | |
170 | plibc_func = dlsym(RTLD_NEXT, "setns"); | |
171 | if (plibc_func == NULL) { | |
172 | fprintf(stderr, "libustfork: unable to find \"setns\" symbol\n"); | |
173 | errno = ENOSYS; | |
174 | return -1; | |
175 | } | |
176 | } | |
177 | ||
178 | /* Do the real setns */ | |
179 | retval = plibc_func(fd, nstype); | |
180 | saved_errno = errno; | |
181 | ||
182 | ust_after_setns(); | |
183 | ||
184 | errno = saved_errno; | |
185 | return retval; | |
186 | } | |
187 | ||
188 | int unshare(int flags) | |
189 | { | |
190 | static int (*plibc_func)(int flags) = NULL; | |
191 | int retval; | |
192 | int saved_errno; | |
193 | ||
194 | if (plibc_func == NULL) { | |
195 | plibc_func = dlsym(RTLD_NEXT, "unshare"); | |
196 | if (plibc_func == NULL) { | |
197 | fprintf(stderr, "libustfork: unable to find \"unshare\" symbol\n"); | |
198 | errno = ENOSYS; | |
199 | return -1; | |
200 | } | |
201 | } | |
202 | ||
203 | /* Do the real setns */ | |
204 | retval = plibc_func(flags); | |
205 | saved_errno = errno; | |
206 | ||
207 | ust_after_unshare(); | |
208 | ||
209 | errno = saved_errno; | |
210 | return retval; | |
211 | } | |
212 | ||
939c89cb MD |
213 | #elif defined (__FreeBSD__) |
214 | ||
215 | pid_t rfork(int flags) | |
216 | { | |
217 | static pid_t (*plibc_func)(void) = NULL; | |
218 | sigset_t sigset; | |
219 | pid_t retval; | |
111902ab | 220 | int saved_errno; |
939c89cb MD |
221 | |
222 | if (plibc_func == NULL) { | |
223 | plibc_func = dlsym(RTLD_NEXT, "rfork"); | |
224 | if (plibc_func == NULL) { | |
225 | fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n"); | |
3678c8aa | 226 | errno = ENOSYS; |
939c89cb MD |
227 | return -1; |
228 | } | |
229 | } | |
230 | ||
231 | ust_before_fork(&sigset); | |
232 | /* Do the real rfork */ | |
233 | retval = plibc_func(); | |
111902ab | 234 | saved_errno = errno; |
939c89cb MD |
235 | if (retval == 0) { |
236 | /* child */ | |
237 | ust_after_fork_child(&sigset); | |
238 | } else { | |
239 | ust_after_fork_parent(&sigset); | |
240 | } | |
111902ab | 241 | errno = saved_errno; |
939c89cb MD |
242 | return retval; |
243 | } | |
244 | ||
245 | /* | |
246 | * On BSD, no need to override vfork, because it runs in the context of | |
247 | * the parent, with parent waiting until execve or exit is executed in | |
248 | * the child. | |
249 | */ | |
250 | ||
251 | #else | |
252 | #warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete." | |
253 | #endif |