Fix: ustfork: save and restore errno in syscall wrappers
[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 35 pid_t retval;
111902ab 36 int saved_errno;
2d99476b 37
e822f505 38 if (plibc_func == NULL) {
2d99476b 39 plibc_func = dlsym(RTLD_NEXT, "fork");
e822f505
MD
40 if (plibc_func == NULL) {
41 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
3678c8aa 42 errno = ENOSYS;
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();
111902ab 50 saved_errno = errno;
e822f505 51 if (retval == 0) {
df793c55 52 /* child */
7f0aeeba 53 ust_after_fork_child(&sigset);
e822f505 54 } else {
7f0aeeba 55 ust_after_fork_parent(&sigset);
df793c55 56 }
111902ab 57 errno = saved_errno;
2d99476b
PMF
58 return retval;
59}
97b042a3 60
2f6150e9
MD
61int daemon(int nochdir, int noclose)
62{
63 static int (*plibc_func)(int nochdir, int noclose) = NULL;
64 sigset_t sigset;
65 int retval;
111902ab 66 int saved_errno;
2f6150e9
MD
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);
111902ab 80 saved_errno = errno;
2f6150e9
MD
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 }
111902ab 88 errno = saved_errno;
2f6150e9
MD
89 return retval;
90}
91
939c89cb
MD
92#ifdef __linux__
93
94struct user_desc;
95
e822f505 96struct ustfork_clone_info {
616ed36a
PMF
97 int (*fn)(void *);
98 void *arg;
7f0aeeba 99 sigset_t sigset;
616ed36a
PMF
100};
101
102static int clone_fn(void *arg)
103{
e822f505 104 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
616ed36a
PMF
105
106 /* clone is now done and we are in child */
7f0aeeba 107 ust_after_fork_child(&info->sigset);
616ed36a
PMF
108 return info->fn(info->arg);
109}
110
111int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
112{
e822f505
MD
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 */
616ed36a
PMF
117 pid_t *ptid;
118 struct user_desc *tls;
119 pid_t *ctid;
e822f505 120 /* end of var args */
616ed36a 121 va_list ap;
e822f505 122 int retval;
111902ab 123 int saved_errno;
616ed36a
PMF
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
e822f505 131 if (plibc_func == NULL) {
616ed36a 132 plibc_func = dlsym(RTLD_NEXT, "clone");
e822f505
MD
133 if (plibc_func == NULL) {
134 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
3678c8aa 135 errno = ENOSYS;
616ed36a
PMF
136 return -1;
137 }
138 }
139
e822f505
MD
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);
111902ab 147 saved_errno = errno;
e822f505
MD
148 } else {
149 /* Creating a real process, we need to intervene. */
3fd2b913 150 struct ustfork_clone_info info = { .fn = fn, .arg = arg };
616ed36a 151
7f0aeeba 152 ust_before_fork(&info.sigset);
e822f505
MD
153 retval = plibc_func(clone_fn, child_stack, flags, &info,
154 ptid, tls, ctid);
111902ab 155 saved_errno = errno;
e822f505 156 /* The child doesn't get here. */
7f0aeeba 157 ust_after_fork_parent(&info.sigset);
616ed36a 158 }
111902ab 159 errno = saved_errno;
616ed36a
PMF
160 return retval;
161}
939c89cb
MD
162
163#elif defined (__FreeBSD__)
164
165pid_t rfork(int flags)
166{
167 static pid_t (*plibc_func)(void) = NULL;
168 sigset_t sigset;
169 pid_t retval;
111902ab 170 int saved_errno;
939c89cb
MD
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");
3678c8aa 176 errno = ENOSYS;
939c89cb
MD
177 return -1;
178 }
179 }
180
181 ust_before_fork(&sigset);
182 /* Do the real rfork */
183 retval = plibc_func();
111902ab 184 saved_errno = errno;
939c89cb
MD
185 if (retval == 0) {
186 /* child */
187 ust_after_fork_child(&sigset);
188 } else {
189 ust_after_fork_parent(&sigset);
190 }
111902ab 191 errno = saved_errno;
939c89cb
MD
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.038728 seconds and 4 git commands to generate.