Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[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 #elif defined (__FreeBSD__)
164
165 pid_t rfork(int flags)
166 {
167 static pid_t (*plibc_func)(void) = NULL;
168 sigset_t sigset;
169 pid_t retval;
170 int saved_errno;
171
172 if (plibc_func == NULL) {
173 plibc_func = dlsym(RTLD_NEXT, "rfork");
174 if (plibc_func == NULL) {
175 fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
176 errno = ENOSYS;
177 return -1;
178 }
179 }
180
181 ust_before_fork(&sigset);
182 /* Do the real rfork */
183 retval = plibc_func();
184 saved_errno = errno;
185 if (retval == 0) {
186 /* child */
187 ust_after_fork_child(&sigset);
188 } else {
189 ust_after_fork_parent(&sigset);
190 }
191 errno = saved_errno;
192 return retval;
193 }
194
195 /*
196 * On BSD, no need to override vfork, because it runs in the context of
197 * the parent, with parent waiting until execve or exit is executed in
198 * the child.
199 */
200
201 #else
202 #warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
203 #endif
This page took 0.03306 seconds and 4 git commands to generate.