Document LTTNG_UST_DEBUG_VALGRIND compilation flag
[lttng-ust.git] / liblttng-ust-fork / ustfork.c
CommitLineData
e822f505
MD
1/*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 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
21#include <dlfcn.h>
22#include <unistd.h>
23#include <stdio.h>
df793c55 24#include <signal.h>
616ed36a
PMF
25#include <sched.h>
26#include <stdarg.h>
fbca6b62 27#include "usterr.h"
2d99476b 28
4318ae1b 29#include <lttng/ust.h>
e822f505 30
99912014
MD
31struct user_desc;
32
2d99476b
PMF
33pid_t fork(void)
34{
35 static pid_t (*plibc_func)(void) = NULL;
7f0aeeba 36 sigset_t sigset;
2d99476b
PMF
37 pid_t retval;
38
e822f505 39 if (plibc_func == NULL) {
2d99476b 40 plibc_func = dlsym(RTLD_NEXT, "fork");
e822f505
MD
41 if (plibc_func == NULL) {
42 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
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();
e822f505 50 if (retval == 0) {
df793c55 51 /* child */
7f0aeeba 52 ust_after_fork_child(&sigset);
e822f505 53 } else {
7f0aeeba 54 ust_after_fork_parent(&sigset);
df793c55 55 }
2d99476b
PMF
56 return retval;
57}
97b042a3 58
e822f505 59struct ustfork_clone_info {
616ed36a
PMF
60 int (*fn)(void *);
61 void *arg;
7f0aeeba 62 sigset_t sigset;
616ed36a
PMF
63};
64
65static int clone_fn(void *arg)
66{
e822f505 67 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
616ed36a
PMF
68
69 /* clone is now done and we are in child */
7f0aeeba 70 ust_after_fork_child(&info->sigset);
616ed36a
PMF
71 return info->fn(info->arg);
72}
73
74int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
75{
e822f505
MD
76 static int (*plibc_func)(int (*fn)(void *), void *child_stack,
77 int flags, void *arg, pid_t *ptid,
78 struct user_desc *tls, pid_t *ctid) = NULL;
79 /* var args */
616ed36a
PMF
80 pid_t *ptid;
81 struct user_desc *tls;
82 pid_t *ctid;
e822f505 83 /* end of var args */
616ed36a 84 va_list ap;
e822f505 85 int retval;
616ed36a
PMF
86
87 va_start(ap, arg);
88 ptid = va_arg(ap, pid_t *);
89 tls = va_arg(ap, struct user_desc *);
90 ctid = va_arg(ap, pid_t *);
91 va_end(ap);
92
e822f505 93 if (plibc_func == NULL) {
616ed36a 94 plibc_func = dlsym(RTLD_NEXT, "clone");
e822f505
MD
95 if (plibc_func == NULL) {
96 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
616ed36a
PMF
97 return -1;
98 }
99 }
100
e822f505
MD
101 if (flags & CLONE_VM) {
102 /*
103 * Creating a thread, no need to intervene, just pass on
104 * the arguments.
105 */
106 retval = plibc_func(fn, child_stack, flags, arg, ptid,
107 tls, ctid);
108 } else {
109 /* Creating a real process, we need to intervene. */
110 struct ustfork_clone_info info = { fn: fn, arg: arg };
616ed36a 111
7f0aeeba 112 ust_before_fork(&info.sigset);
e822f505
MD
113 retval = plibc_func(clone_fn, child_stack, flags, &info,
114 ptid, tls, ctid);
115 /* The child doesn't get here. */
7f0aeeba 116 ust_after_fork_parent(&info.sigset);
616ed36a 117 }
616ed36a
PMF
118 return retval;
119}
This page took 0.037986 seconds and 4 git commands to generate.