ustfork: set errno to ENOSYS if symbol lookup fails
[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 <dlfcn.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <sched.h>
26 #include <stdarg.h>
27 #include "usterr.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
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");
41 errno = ENOSYS;
42 return -1;
43 }
44 }
45
46 ust_before_fork(&sigset);
47 /* Do the real fork */
48 retval = plibc_func();
49 if (retval == 0) {
50 /* child */
51 ust_after_fork_child(&sigset);
52 } else {
53 ust_after_fork_parent(&sigset);
54 }
55 return retval;
56 }
57
58 #ifdef __linux__
59
60 struct user_desc;
61
62 struct ustfork_clone_info {
63 int (*fn)(void *);
64 void *arg;
65 sigset_t sigset;
66 };
67
68 static int clone_fn(void *arg)
69 {
70 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
71
72 /* clone is now done and we are in child */
73 ust_after_fork_child(&info->sigset);
74 return info->fn(info->arg);
75 }
76
77 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
78 {
79 static int (*plibc_func)(int (*fn)(void *), void *child_stack,
80 int flags, void *arg, pid_t *ptid,
81 struct user_desc *tls, pid_t *ctid) = NULL;
82 /* var args */
83 pid_t *ptid;
84 struct user_desc *tls;
85 pid_t *ctid;
86 /* end of var args */
87 va_list ap;
88 int retval;
89
90 va_start(ap, arg);
91 ptid = va_arg(ap, pid_t *);
92 tls = va_arg(ap, struct user_desc *);
93 ctid = va_arg(ap, pid_t *);
94 va_end(ap);
95
96 if (plibc_func == NULL) {
97 plibc_func = dlsym(RTLD_NEXT, "clone");
98 if (plibc_func == NULL) {
99 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
100 errno = ENOSYS;
101 return -1;
102 }
103 }
104
105 if (flags & CLONE_VM) {
106 /*
107 * Creating a thread, no need to intervene, just pass on
108 * the arguments.
109 */
110 retval = plibc_func(fn, child_stack, flags, arg, ptid,
111 tls, ctid);
112 } else {
113 /* Creating a real process, we need to intervene. */
114 struct ustfork_clone_info info = { fn = fn, arg = arg };
115
116 ust_before_fork(&info.sigset);
117 retval = plibc_func(clone_fn, child_stack, flags, &info,
118 ptid, tls, ctid);
119 /* The child doesn't get here. */
120 ust_after_fork_parent(&info.sigset);
121 }
122 return retval;
123 }
124
125 #elif defined (__FreeBSD__)
126
127 pid_t rfork(int flags)
128 {
129 static pid_t (*plibc_func)(void) = NULL;
130 sigset_t sigset;
131 pid_t retval;
132
133 if (plibc_func == NULL) {
134 plibc_func = dlsym(RTLD_NEXT, "rfork");
135 if (plibc_func == NULL) {
136 fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
137 errno = ENOSYS;
138 return -1;
139 }
140 }
141
142 ust_before_fork(&sigset);
143 /* Do the real rfork */
144 retval = plibc_func();
145 if (retval == 0) {
146 /* child */
147 ust_after_fork_child(&sigset);
148 } else {
149 ust_after_fork_parent(&sigset);
150 }
151 return retval;
152 }
153
154 /*
155 * On BSD, no need to override vfork, because it runs in the context of
156 * the parent, with parent waiting until execve or exit is executed in
157 * the child.
158 */
159
160 #else
161 #warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
162 #endif
This page took 0.033306 seconds and 5 git commands to generate.