Fix: Don't override user variables within the build system
[lttng-ust.git] / liblttng-ust-fork / ustfork.c
CommitLineData
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
31pid_t fork(void)
32{
33 static pid_t (*plibc_func)(void) = NULL;
7f0aeeba 34 sigset_t sigset;
2d99476b
PMF
35 pid_t retval;
36
e822f505 37 if (plibc_func == NULL) {
2d99476b 38 plibc_func = dlsym(RTLD_NEXT, "fork");
e822f505
MD
39 if (plibc_func == NULL) {
40 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
3678c8aa 41 errno = ENOSYS;
2c10b7fd 42 return -1;
2d99476b
PMF
43 }
44 }
45
7f0aeeba 46 ust_before_fork(&sigset);
df793c55 47 /* Do the real fork */
2d99476b 48 retval = plibc_func();
e822f505 49 if (retval == 0) {
df793c55 50 /* child */
7f0aeeba 51 ust_after_fork_child(&sigset);
e822f505 52 } else {
7f0aeeba 53 ust_after_fork_parent(&sigset);
df793c55 54 }
2d99476b
PMF
55 return retval;
56}
97b042a3 57
2f6150e9
MD
58int daemon(int nochdir, int noclose)
59{
60 static int (*plibc_func)(int nochdir, int noclose) = NULL;
61 sigset_t sigset;
62 int retval;
63
64 if (plibc_func == NULL) {
65 plibc_func = dlsym(RTLD_NEXT, "daemon");
66 if (plibc_func == NULL) {
67 fprintf(stderr, "libustfork: unable to find \"daemon\" symbol\n");
68 errno = ENOSYS;
69 return -1;
70 }
71 }
72
73 ust_before_fork(&sigset);
74 /* Do the real daemon call */
75 retval = plibc_func(nochdir, noclose);
76 if (retval == 0) {
77 /* child, parent called _exit() directly */
78 ust_after_fork_child(&sigset);
79 } else {
80 /* on error in the parent */
81 ust_after_fork_parent(&sigset);
82 }
83 return retval;
84}
85
939c89cb
MD
86#ifdef __linux__
87
88struct user_desc;
89
e822f505 90struct ustfork_clone_info {
616ed36a
PMF
91 int (*fn)(void *);
92 void *arg;
7f0aeeba 93 sigset_t sigset;
616ed36a
PMF
94};
95
96static int clone_fn(void *arg)
97{
e822f505 98 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
616ed36a
PMF
99
100 /* clone is now done and we are in child */
7f0aeeba 101 ust_after_fork_child(&info->sigset);
616ed36a
PMF
102 return info->fn(info->arg);
103}
104
105int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
106{
e822f505
MD
107 static int (*plibc_func)(int (*fn)(void *), void *child_stack,
108 int flags, void *arg, pid_t *ptid,
109 struct user_desc *tls, pid_t *ctid) = NULL;
110 /* var args */
616ed36a
PMF
111 pid_t *ptid;
112 struct user_desc *tls;
113 pid_t *ctid;
e822f505 114 /* end of var args */
616ed36a 115 va_list ap;
e822f505 116 int retval;
616ed36a
PMF
117
118 va_start(ap, arg);
119 ptid = va_arg(ap, pid_t *);
120 tls = va_arg(ap, struct user_desc *);
121 ctid = va_arg(ap, pid_t *);
122 va_end(ap);
123
e822f505 124 if (plibc_func == NULL) {
616ed36a 125 plibc_func = dlsym(RTLD_NEXT, "clone");
e822f505
MD
126 if (plibc_func == NULL) {
127 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
3678c8aa 128 errno = ENOSYS;
616ed36a
PMF
129 return -1;
130 }
131 }
132
e822f505
MD
133 if (flags & CLONE_VM) {
134 /*
135 * Creating a thread, no need to intervene, just pass on
136 * the arguments.
137 */
138 retval = plibc_func(fn, child_stack, flags, arg, ptid,
139 tls, ctid);
140 } else {
141 /* Creating a real process, we need to intervene. */
3fd2b913 142 struct ustfork_clone_info info = { .fn = fn, .arg = arg };
616ed36a 143
7f0aeeba 144 ust_before_fork(&info.sigset);
e822f505
MD
145 retval = plibc_func(clone_fn, child_stack, flags, &info,
146 ptid, tls, ctid);
147 /* The child doesn't get here. */
7f0aeeba 148 ust_after_fork_parent(&info.sigset);
616ed36a 149 }
616ed36a
PMF
150 return retval;
151}
939c89cb
MD
152
153#elif defined (__FreeBSD__)
154
155pid_t rfork(int flags)
156{
157 static pid_t (*plibc_func)(void) = NULL;
158 sigset_t sigset;
159 pid_t retval;
160
161 if (plibc_func == NULL) {
162 plibc_func = dlsym(RTLD_NEXT, "rfork");
163 if (plibc_func == NULL) {
164 fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
3678c8aa 165 errno = ENOSYS;
939c89cb
MD
166 return -1;
167 }
168 }
169
170 ust_before_fork(&sigset);
171 /* Do the real rfork */
172 retval = plibc_func();
173 if (retval == 0) {
174 /* child */
175 ust_after_fork_child(&sigset);
176 } else {
177 ust_after_fork_parent(&sigset);
178 }
179 return retval;
180}
181
182/*
183 * On BSD, no need to override vfork, because it runs in the context of
184 * the parent, with parent waiting until execve or exit is executed in
185 * the child.
186 */
187
188#else
189#warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
190#endif
This page took 0.037807 seconds and 4 git commands to generate.