b943e248eb983d2edb3a0b18e2baf171a89811ef
[lttng-ust.git] / liblttng-ust-fork / ustfork.c
1 /*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 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 struct user_desc;
32
33 pid_t fork(void)
34 {
35 static pid_t (*plibc_func)(void) = NULL;
36 sigset_t sigset;
37 pid_t retval;
38
39 if (plibc_func == NULL) {
40 plibc_func = dlsym(RTLD_NEXT, "fork");
41 if (plibc_func == NULL) {
42 fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
43 return -1;
44 }
45 }
46
47 ust_before_fork(&sigset);
48 /* Do the real fork */
49 retval = plibc_func();
50 if (retval == 0) {
51 /* child */
52 ust_after_fork_child(&sigset);
53 } else {
54 ust_after_fork_parent(&sigset);
55 }
56 return retval;
57 }
58
59 int daemon(int nochdir, int noclose)
60 {
61 static int (*plibc_func)(int nochdir, int noclose) = NULL;
62 sigset_t sigset;
63 int retval;
64
65 if (plibc_func == NULL) {
66 plibc_func = dlsym(RTLD_NEXT, "daemon");
67 if (plibc_func == NULL) {
68 fprintf(stderr, "libustfork: unable to find \"daemon\" symbol\n");
69 errno = ENOSYS;
70 return -1;
71 }
72 }
73
74 ust_before_fork(&sigset);
75 /* Do the real daemon call */
76 retval = plibc_func(nochdir, noclose);
77 if (retval == 0) {
78 /* child, parent called _exit() directly */
79 ust_after_fork_child(&sigset);
80 } else {
81 /* on error in the parent */
82 ust_after_fork_parent(&sigset);
83 }
84 return retval;
85 }
86
87 struct ustfork_clone_info {
88 int (*fn)(void *);
89 void *arg;
90 sigset_t sigset;
91 };
92
93 static int clone_fn(void *arg)
94 {
95 struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
96
97 /* clone is now done and we are in child */
98 ust_after_fork_child(&info->sigset);
99 return info->fn(info->arg);
100 }
101
102 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
103 {
104 static int (*plibc_func)(int (*fn)(void *), void *child_stack,
105 int flags, void *arg, pid_t *ptid,
106 struct user_desc *tls, pid_t *ctid) = NULL;
107 /* var args */
108 pid_t *ptid;
109 struct user_desc *tls;
110 pid_t *ctid;
111 /* end of var args */
112 va_list ap;
113 int retval;
114
115 va_start(ap, arg);
116 ptid = va_arg(ap, pid_t *);
117 tls = va_arg(ap, struct user_desc *);
118 ctid = va_arg(ap, pid_t *);
119 va_end(ap);
120
121 if (plibc_func == NULL) {
122 plibc_func = dlsym(RTLD_NEXT, "clone");
123 if (plibc_func == NULL) {
124 fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
125 return -1;
126 }
127 }
128
129 if (flags & CLONE_VM) {
130 /*
131 * Creating a thread, no need to intervene, just pass on
132 * the arguments.
133 */
134 retval = plibc_func(fn, child_stack, flags, arg, ptid,
135 tls, ctid);
136 } else {
137 /* Creating a real process, we need to intervene. */
138 struct ustfork_clone_info info = { .fn = fn, .arg = arg };
139
140 ust_before_fork(&info.sigset);
141 retval = plibc_func(clone_fn, child_stack, flags, &info,
142 ptid, tls, ctid);
143 /* The child doesn't get here. */
144 ust_after_fork_parent(&info.sigset);
145 }
146 return retval;
147 }
This page took 0.032041 seconds and 3 git commands to generate.