Add userspace namespace contexts
[lttng-ust.git] / liblttng-ust-fork / ustfork.c
1 /*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
7 * License as published by the Free Software Foundation; version 2.1 of
8 * the License.
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
21 #include <lttng/ust-dlfcn.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <sched.h>
26 #include <stdarg.h>
27 #include <errno.h>
28
29 #include <lttng/ust.h>
30
31 pid_t fork(void)
32 {
33 static pid_t (*plibc_func)(void) = NULL;
34 sigset_t sigset;
35 pid_t retval;
36 int saved_errno;
37
38 if (plibc_func == NULL) {
39 plibc_func = dlsym(RTLD_NEXT, "fork");
40 if (plibc_func == NULL) {
41 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
42 errno = ENOSYS;
43 return -1;
44 }
45 }
46
47 ust_before_fork(&sigset);
48 /* Do the real fork */
49 retval = plibc_func();
50 saved_errno = errno;
51 if (retval == 0) {
52 /* child */
53 ust_after_fork_child(&sigset);
54 } else {
55 ust_after_fork_parent(&sigset);
56 }
57 errno = saved_errno;
58 return retval;
59 }
60
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;
66 int saved_errno;
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);
80 saved_errno = errno;
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 }
88 errno = saved_errno;
89 return retval;
90 }
91
92 #ifdef __linux__
93
94 struct user_desc;
95
96 struct ustfork_clone_info {
97 int (*fn)(void *);
98 void *arg;
99 sigset_t sigset;
100 };
101
102 static int clone_fn(void *arg)
103 {
104 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
105
106 /* clone is now done and we are in child */
107 ust_after_fork_child(&info->sigset);
108 return info->fn(info->arg);
109 }
110
111 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
112 {
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 */
117 pid_t *ptid;
118 struct user_desc *tls;
119 pid_t *ctid;
120 /* end of var args */
121 va_list ap;
122 int retval;
123 int saved_errno;
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
131 if (plibc_func == NULL) {
132 plibc_func = dlsym(RTLD_NEXT, "clone");
133 if (plibc_func == NULL) {
134 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
135 errno = ENOSYS;
136 return -1;
137 }
138 }
139
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);
147 saved_errno = errno;
148 } else {
149 /* Creating a real process, we need to intervene. */
150 struct ustfork_clone_info info = { .fn = fn, .arg = arg };
151
152 ust_before_fork(&info.sigset);
153 retval = plibc_func(clone_fn, child_stack, flags, &info,
154 ptid, tls, ctid);
155 saved_errno = errno;
156 /* The child doesn't get here. */
157 ust_after_fork_parent(&info.sigset);
158 }
159 errno = saved_errno;
160 return retval;
161 }
162
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
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;
220 int saved_errno;
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");
226 errno = ENOSYS;
227 return -1;
228 }
229 }
230
231 ust_before_fork(&sigset);
232 /* Do the real rfork */
233 retval = plibc_func();
234 saved_errno = errno;
235 if (retval == 0) {
236 /* child */
237 ust_after_fork_child(&sigset);
238 } else {
239 ust_after_fork_parent(&sigset);
240 }
241 errno = saved_errno;
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
This page took 0.033338 seconds and 4 git commands to generate.