2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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
21 #include <lttng/ust-dlfcn.h>
29 #include <lttng/ust.h>
33 static pid_t (*plibc_func
)(void) = NULL
;
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");
46 ust_before_fork(&sigset
);
47 /* Do the real fork */
48 retval
= plibc_func();
51 ust_after_fork_child(&sigset
);
53 ust_after_fork_parent(&sigset
);
58 int daemon(int nochdir
, int noclose
)
60 static int (*plibc_func
)(int nochdir
, int noclose
) = NULL
;
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");
73 ust_before_fork(&sigset
);
74 /* Do the real daemon call */
75 retval
= plibc_func(nochdir
, noclose
);
77 /* child, parent called _exit() directly */
78 ust_after_fork_child(&sigset
);
80 /* on error in the parent */
81 ust_after_fork_parent(&sigset
);
90 struct ustfork_clone_info
{
96 static int clone_fn(void *arg
)
98 struct ustfork_clone_info
*info
= (struct ustfork_clone_info
*) arg
;
100 /* clone is now done and we are in child */
101 ust_after_fork_child(&info
->sigset
);
102 return info
->fn(info
->arg
);
105 int clone(int (*fn
)(void *), void *child_stack
, int flags
, void *arg
, ...)
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
;
112 struct user_desc
*tls
;
114 /* end of var args */
119 ptid
= va_arg(ap
, pid_t
*);
120 tls
= va_arg(ap
, struct user_desc
*);
121 ctid
= va_arg(ap
, pid_t
*);
124 if (plibc_func
== NULL
) {
125 plibc_func
= dlsym(RTLD_NEXT
, "clone");
126 if (plibc_func
== NULL
) {
127 fprintf(stderr
, "libustfork: unable to find \"clone\" symbol.\n");
133 if (flags
& CLONE_VM
) {
135 * Creating a thread, no need to intervene, just pass on
138 retval
= plibc_func(fn
, child_stack
, flags
, arg
, ptid
,
141 /* Creating a real process, we need to intervene. */
142 struct ustfork_clone_info info
= { .fn
= fn
, .arg
= arg
};
144 ust_before_fork(&info
.sigset
);
145 retval
= plibc_func(clone_fn
, child_stack
, flags
, &info
,
147 /* The child doesn't get here. */
148 ust_after_fork_parent(&info
.sigset
);
153 #elif defined (__FreeBSD__)
155 pid_t
rfork(int flags
)
157 static pid_t (*plibc_func
)(void) = NULL
;
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");
170 ust_before_fork(&sigset
);
171 /* Do the real rfork */
172 retval
= plibc_func();
175 ust_after_fork_child(&sigset
);
177 ust_after_fork_parent(&sigset
);
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
189 #warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
This page took 0.034629 seconds and 5 git commands to generate.