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