Merge branch 'master' into dev
[lttng-ust.git] / liblttng-ust-fork / ustfork.c
1 /*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 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 <dlfcn.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <sched.h>
26 #include <stdarg.h>
27 #include "usterr.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
37 if (plibc_func == NULL) {
38 plibc_func = dlsym(RTLD_NEXT, "fork");
39 if (plibc_func == NULL) {
40 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
41 return -1;
42 }
43 }
44
45 ust_before_fork(&sigset);
46 /* Do the real fork */
47 retval = plibc_func();
48 if (retval == 0) {
49 /* child */
50 ust_after_fork_child(&sigset);
51 } else {
52 ust_after_fork_parent(&sigset);
53 }
54 return retval;
55 }
56
57 #ifdef __linux__
58
59 struct user_desc;
60
61 struct ustfork_clone_info {
62 int (*fn)(void *);
63 void *arg;
64 sigset_t sigset;
65 };
66
67 static int clone_fn(void *arg)
68 {
69 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
70
71 /* clone is now done and we are in child */
72 ust_after_fork_child(&info->sigset);
73 return info->fn(info->arg);
74 }
75
76 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
77 {
78 static int (*plibc_func)(int (*fn)(void *), void *child_stack,
79 int flags, void *arg, pid_t *ptid,
80 struct user_desc *tls, pid_t *ctid) = NULL;
81 /* var args */
82 pid_t *ptid;
83 struct user_desc *tls;
84 pid_t *ctid;
85 /* end of var args */
86 va_list ap;
87 int retval;
88
89 va_start(ap, arg);
90 ptid = va_arg(ap, pid_t *);
91 tls = va_arg(ap, struct user_desc *);
92 ctid = va_arg(ap, pid_t *);
93 va_end(ap);
94
95 if (plibc_func == NULL) {
96 plibc_func = dlsym(RTLD_NEXT, "clone");
97 if (plibc_func == NULL) {
98 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
99 return -1;
100 }
101 }
102
103 if (flags & CLONE_VM) {
104 /*
105 * Creating a thread, no need to intervene, just pass on
106 * the arguments.
107 */
108 retval = plibc_func(fn, child_stack, flags, arg, ptid,
109 tls, ctid);
110 } else {
111 /* Creating a real process, we need to intervene. */
112 struct ustfork_clone_info info = { fn: fn, arg: arg };
113
114 ust_before_fork(&info.sigset);
115 retval = plibc_func(clone_fn, child_stack, flags, &info,
116 ptid, tls, ctid);
117 /* The child doesn't get here. */
118 ust_after_fork_parent(&info.sigset);
119 }
120 return retval;
121 }
122
123 #elif defined (__FreeBSD__)
124
125 pid_t rfork(int flags)
126 {
127 static pid_t (*plibc_func)(void) = NULL;
128 sigset_t sigset;
129 pid_t retval;
130
131 if (plibc_func == NULL) {
132 plibc_func = dlsym(RTLD_NEXT, "rfork");
133 if (plibc_func == NULL) {
134 fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
135 return -1;
136 }
137 }
138
139 ust_before_fork(&sigset);
140 /* Do the real rfork */
141 retval = plibc_func();
142 if (retval == 0) {
143 /* child */
144 ust_after_fork_child(&sigset);
145 } else {
146 ust_after_fork_parent(&sigset);
147 }
148 return retval;
149 }
150
151 /*
152 * On BSD, no need to override vfork, because it runs in the context of
153 * the parent, with parent waiting until execve or exit is executed in
154 * the child.
155 */
156
157 #else
158 #warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
159 #endif
This page took 0.034363 seconds and 5 git commands to generate.