Commit | Line | Data |
---|---|---|
2691221a MD |
1 | /* |
2 | * lttng-ust-comm.c | |
3 | * | |
4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> | |
5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; only | |
10 | * version 2.1 of the License. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
80e2814b | 22 | #define _LGPL_SOURCE |
2691221a MD |
23 | #include <sys/types.h> |
24 | #include <sys/socket.h> | |
4c1ee94e | 25 | #include <sys/prctl.h> |
7fc90dca MD |
26 | #include <sys/mman.h> |
27 | #include <sys/stat.h> | |
58d4b2a2 MD |
28 | #include <sys/types.h> |
29 | #include <sys/wait.h> | |
7fc90dca | 30 | #include <fcntl.h> |
2691221a MD |
31 | #include <unistd.h> |
32 | #include <errno.h> | |
d9e99d10 | 33 | #include <pthread.h> |
11ff9c7d MD |
34 | #include <semaphore.h> |
35 | #include <time.h> | |
1ea11eab | 36 | #include <assert.h> |
e822f505 | 37 | #include <signal.h> |
95259bd0 | 38 | #include <urcu/uatomic.h> |
80e2814b | 39 | #include <urcu/futex.h> |
c117fb1b | 40 | #include <urcu/compiler.h> |
1ea11eab | 41 | |
4318ae1b | 42 | #include <lttng/ust-events.h> |
4318ae1b | 43 | #include <lttng/ust-abi.h> |
4318ae1b | 44 | #include <lttng/ust.h> |
44c72f10 MD |
45 | #include <ust-comm.h> |
46 | #include <usterr-signal-safe.h> | |
47 | #include "tracepoint-internal.h" | |
b751f722 | 48 | #include "ltt-tracer-core.h" |
f645cfa7 | 49 | #include "../libringbuffer/tlsfixup.h" |
edaa1431 MD |
50 | |
51 | /* | |
52 | * Has lttng ust comm constructor been called ? | |
53 | */ | |
54 | static int initialized; | |
55 | ||
1ea11eab | 56 | /* |
17dfb34b MD |
57 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
58 | * Held when handling a command, also held by fork() to deal with | |
59 | * removal of threads, and by exit path. | |
1ea11eab | 60 | */ |
1ea11eab MD |
61 | |
62 | /* Should the ust comm thread quit ? */ | |
63 | static int lttng_ust_comm_should_quit; | |
64 | ||
11ff9c7d MD |
65 | /* |
66 | * Wait for either of these before continuing to the main | |
67 | * program: | |
68 | * - the register_done message from sessiond daemon | |
69 | * (will let the sessiond daemon enable sessions before main | |
70 | * starts.) | |
71 | * - sessiond daemon is not reachable. | |
72 | * - timeout (ensuring applications are resilient to session | |
73 | * daemon problems). | |
74 | */ | |
75 | static sem_t constructor_wait; | |
950aab0c MD |
76 | /* |
77 | * Doing this for both the global and local sessiond. | |
78 | */ | |
95259bd0 | 79 | static int sem_count = { 2 }; |
11ff9c7d | 80 | |
3b8b68e7 MD |
81 | /* |
82 | * Counting nesting within lttng-ust. Used to ensure that calling fork() | |
83 | * from liblttng-ust does not execute the pre/post fork handlers. | |
84 | */ | |
85 | static int __thread lttng_ust_nest_count; | |
86 | ||
1ea11eab MD |
87 | /* |
88 | * Info about socket and associated listener thread. | |
89 | */ | |
90 | struct sock_info { | |
11ff9c7d | 91 | const char *name; |
1ea11eab | 92 | pthread_t ust_listener; /* listener thread */ |
46050b1a | 93 | int root_handle; |
8d20bf54 MD |
94 | int constructor_sem_posted; |
95 | int allowed; | |
44e073f5 | 96 | int global; |
7fc90dca MD |
97 | |
98 | char sock_path[PATH_MAX]; | |
99 | int socket; | |
100 | ||
101 | char wait_shm_path[PATH_MAX]; | |
102 | char *wait_shm_mmap; | |
1ea11eab | 103 | }; |
2691221a MD |
104 | |
105 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 106 | struct sock_info global_apps = { |
11ff9c7d | 107 | .name = "global", |
44e073f5 | 108 | .global = 1, |
7fc90dca | 109 | |
46050b1a | 110 | .root_handle = -1, |
8d20bf54 | 111 | .allowed = 1, |
7fc90dca MD |
112 | |
113 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, | |
114 | .socket = -1, | |
115 | ||
116 | .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, | |
1ea11eab | 117 | }; |
2691221a MD |
118 | |
119 | /* TODO: allow global_apps_sock_path override */ | |
120 | ||
1ea11eab | 121 | struct sock_info local_apps = { |
11ff9c7d | 122 | .name = "local", |
44e073f5 | 123 | .global = 0, |
46050b1a | 124 | .root_handle = -1, |
8d20bf54 | 125 | .allowed = 0, /* Check setuid bit first */ |
7fc90dca MD |
126 | |
127 | .socket = -1, | |
1ea11eab | 128 | }; |
2691221a | 129 | |
37ed587a MD |
130 | static int wait_poll_fallback; |
131 | ||
edaa1431 MD |
132 | extern void ltt_ring_buffer_client_overwrite_init(void); |
133 | extern void ltt_ring_buffer_client_discard_init(void); | |
134 | extern void ltt_ring_buffer_metadata_client_init(void); | |
135 | extern void ltt_ring_buffer_client_overwrite_exit(void); | |
136 | extern void ltt_ring_buffer_client_discard_exit(void); | |
137 | extern void ltt_ring_buffer_metadata_client_exit(void); | |
138 | ||
e38c8fc1 MD |
139 | /* |
140 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
141 | */ | |
142 | static | |
143 | void lttng_fixup_nest_count_tls(void) | |
144 | { | |
145 | asm volatile ("" : : "m" (lttng_ust_nest_count)); | |
146 | } | |
147 | ||
2691221a | 148 | static |
8d20bf54 | 149 | int setup_local_apps(void) |
2691221a MD |
150 | { |
151 | const char *home_dir; | |
7fc90dca | 152 | uid_t uid; |
2691221a | 153 | |
7fc90dca | 154 | uid = getuid(); |
8d20bf54 MD |
155 | /* |
156 | * Disallow per-user tracing for setuid binaries. | |
157 | */ | |
7fc90dca | 158 | if (uid != geteuid()) { |
8d20bf54 | 159 | local_apps.allowed = 0; |
d0a1ae63 | 160 | return 0; |
8d20bf54 MD |
161 | } else { |
162 | local_apps.allowed = 1; | |
163 | } | |
2691221a MD |
164 | home_dir = (const char *) getenv("HOME"); |
165 | if (!home_dir) | |
166 | return -ENOENT; | |
1ea11eab | 167 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 168 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
7fc90dca MD |
169 | snprintf(local_apps.wait_shm_path, PATH_MAX, |
170 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid); | |
2691221a MD |
171 | return 0; |
172 | } | |
173 | ||
174 | static | |
175 | int register_app_to_sessiond(int socket) | |
176 | { | |
177 | ssize_t ret; | |
4c1ee94e | 178 | int prctl_ret; |
2691221a | 179 | struct { |
e44418f3 MD |
180 | uint32_t major; |
181 | uint32_t minor; | |
2691221a | 182 | pid_t pid; |
5c33bde8 | 183 | pid_t ppid; |
2691221a | 184 | uid_t uid; |
83610856 | 185 | gid_t gid; |
c117fb1b | 186 | uint32_t bits_per_long; |
2629549e | 187 | char name[16]; /* process name */ |
2691221a MD |
188 | } reg_msg; |
189 | ||
e44418f3 MD |
190 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
191 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a | 192 | reg_msg.pid = getpid(); |
5c33bde8 | 193 | reg_msg.ppid = getppid(); |
2691221a | 194 | reg_msg.uid = getuid(); |
83610856 | 195 | reg_msg.gid = getgid(); |
c117fb1b | 196 | reg_msg.bits_per_long = CAA_BITS_PER_LONG; |
4c1ee94e MD |
197 | prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0); |
198 | if (prctl_ret) { | |
199 | ERR("Error executing prctl"); | |
200 | return -errno; | |
201 | } | |
2691221a | 202 | |
57773204 | 203 | ret = ustcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); |
2691221a MD |
204 | if (ret >= 0 && ret != sizeof(reg_msg)) |
205 | return -EIO; | |
206 | return ret; | |
207 | } | |
208 | ||
d9e99d10 | 209 | static |
57773204 | 210 | int send_reply(int sock, struct ustcomm_ust_reply *lur) |
d9e99d10 | 211 | { |
9eb62b9c | 212 | ssize_t len; |
d3a492d1 | 213 | |
57773204 | 214 | len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 215 | switch (len) { |
a4be8962 | 216 | case sizeof(*lur): |
d3a492d1 MD |
217 | DBG("message successfully sent"); |
218 | return 0; | |
219 | case -1: | |
220 | if (errno == ECONNRESET) { | |
221 | printf("remote end closed connection\n"); | |
222 | return 0; | |
223 | } | |
224 | return -1; | |
225 | default: | |
226 | printf("incorrect message size: %zd\n", len); | |
227 | return -1; | |
228 | } | |
229 | } | |
230 | ||
231 | static | |
edaa1431 | 232 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
233 | { |
234 | int ret; | |
235 | ||
edaa1431 MD |
236 | if (sock_info->constructor_sem_posted) |
237 | return 0; | |
238 | sock_info->constructor_sem_posted = 1; | |
56cd7e2f MD |
239 | if (uatomic_read(&sem_count) <= 0) { |
240 | return 0; | |
241 | } | |
95259bd0 MD |
242 | ret = uatomic_add_return(&sem_count, -1); |
243 | if (ret == 0) { | |
244 | ret = sem_post(&constructor_wait); | |
245 | assert(!ret); | |
246 | } | |
11ff9c7d MD |
247 | return 0; |
248 | } | |
249 | ||
250 | static | |
251 | int handle_message(struct sock_info *sock_info, | |
57773204 | 252 | int sock, struct ustcomm_ust_msg *lum) |
d3a492d1 | 253 | { |
1ea11eab | 254 | int ret = 0; |
b61ce3b2 | 255 | const struct lttng_ust_objd_ops *ops; |
57773204 | 256 | struct ustcomm_ust_reply lur; |
193183fb | 257 | int shm_fd, wait_fd; |
ef9ff354 | 258 | union ust_args args; |
1ea11eab | 259 | |
17dfb34b | 260 | ust_lock(); |
1ea11eab | 261 | |
46050b1a MD |
262 | memset(&lur, 0, sizeof(lur)); |
263 | ||
1ea11eab | 264 | if (lttng_ust_comm_should_quit) { |
46050b1a | 265 | ret = -EPERM; |
1ea11eab MD |
266 | goto end; |
267 | } | |
9eb62b9c | 268 | |
46050b1a MD |
269 | ops = objd_ops(lum->handle); |
270 | if (!ops) { | |
271 | ret = -ENOENT; | |
272 | goto end; | |
1ea11eab | 273 | } |
46050b1a MD |
274 | |
275 | switch (lum->cmd) { | |
11ff9c7d MD |
276 | case LTTNG_UST_REGISTER_DONE: |
277 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 278 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
279 | else |
280 | ret = -EINVAL; | |
281 | break; | |
46050b1a MD |
282 | case LTTNG_UST_RELEASE: |
283 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
284 | ret = -EPERM; | |
285 | else | |
d4419b81 | 286 | ret = lttng_ust_objd_unref(lum->handle); |
d9e99d10 MD |
287 | break; |
288 | default: | |
46050b1a MD |
289 | if (ops->cmd) |
290 | ret = ops->cmd(lum->handle, lum->cmd, | |
ef9ff354 MD |
291 | (unsigned long) &lum->u, |
292 | &args); | |
46050b1a MD |
293 | else |
294 | ret = -ENOSYS; | |
295 | break; | |
d9e99d10 | 296 | } |
46050b1a | 297 | |
1ea11eab | 298 | end: |
46050b1a MD |
299 | lur.handle = lum->handle; |
300 | lur.cmd = lum->cmd; | |
301 | lur.ret_val = ret; | |
302 | if (ret >= 0) { | |
57773204 | 303 | lur.ret_code = USTCOMM_OK; |
46050b1a | 304 | } else { |
57773204 | 305 | //lur.ret_code = USTCOMM_SESSION_FAIL; |
193183fb | 306 | lur.ret_code = ret; |
46050b1a | 307 | } |
e6ea14c5 MD |
308 | if (ret >= 0) { |
309 | switch (lum->cmd) { | |
310 | case LTTNG_UST_STREAM: | |
311 | /* | |
312 | * Special-case reply to send stream info. | |
313 | * Use lum.u output. | |
314 | */ | |
315 | lur.u.stream.memory_map_size = *args.stream.memory_map_size; | |
316 | shm_fd = *args.stream.shm_fd; | |
317 | wait_fd = *args.stream.wait_fd; | |
318 | break; | |
319 | case LTTNG_UST_METADATA: | |
320 | case LTTNG_UST_CHANNEL: | |
321 | lur.u.channel.memory_map_size = *args.channel.memory_map_size; | |
322 | shm_fd = *args.channel.shm_fd; | |
323 | wait_fd = *args.channel.wait_fd; | |
324 | break; | |
325 | case LTTNG_UST_TRACER_VERSION: | |
326 | lur.u.version = lum->u.version; | |
327 | break; | |
328 | case LTTNG_UST_TRACEPOINT_LIST_GET: | |
329 | memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint)); | |
330 | break; | |
331 | } | |
381c0f1e | 332 | } |
46050b1a | 333 | ret = send_reply(sock, &lur); |
193183fb MD |
334 | if (ret < 0) { |
335 | perror("error sending reply"); | |
336 | goto error; | |
337 | } | |
46050b1a | 338 | |
824f40b8 MD |
339 | if ((lum->cmd == LTTNG_UST_STREAM |
340 | || lum->cmd == LTTNG_UST_CHANNEL | |
341 | || lum->cmd == LTTNG_UST_METADATA) | |
57773204 | 342 | && lur.ret_code == USTCOMM_OK) { |
5f003977 MD |
343 | int sendret = 0; |
344 | ||
381c0f1e | 345 | /* we also need to send the file descriptors. */ |
57773204 | 346 | ret = ustcomm_send_fds_unix_sock(sock, |
193183fb | 347 | &shm_fd, &shm_fd, |
381c0f1e MD |
348 | 1, sizeof(int)); |
349 | if (ret < 0) { | |
350 | perror("send shm_fd"); | |
5f003977 | 351 | sendret = ret; |
381c0f1e | 352 | } |
5f003977 MD |
353 | /* |
354 | * The sessiond expects 2 file descriptors, even upon | |
355 | * error. | |
356 | */ | |
57773204 | 357 | ret = ustcomm_send_fds_unix_sock(sock, |
193183fb | 358 | &wait_fd, &wait_fd, |
381c0f1e MD |
359 | 1, sizeof(int)); |
360 | if (ret < 0) { | |
361 | perror("send wait_fd"); | |
362 | goto error; | |
363 | } | |
5f003977 MD |
364 | if (sendret) { |
365 | ret = sendret; | |
366 | goto error; | |
367 | } | |
381c0f1e | 368 | } |
ef9ff354 MD |
369 | /* |
370 | * We still have the memory map reference, and the fds have been | |
3ad5f707 MD |
371 | * sent to the sessiond. We can therefore close those fds. Note |
372 | * that we keep the write side of the wait_fd open, but close | |
373 | * the read side. | |
ef9ff354 MD |
374 | */ |
375 | if (lur.ret_code == USTCOMM_OK) { | |
376 | switch (lum->cmd) { | |
377 | case LTTNG_UST_STREAM: | |
378 | if (shm_fd >= 0) { | |
379 | ret = close(shm_fd); | |
380 | if (ret) { | |
381 | PERROR("Error closing stream shm_fd"); | |
382 | } | |
383 | *args.stream.shm_fd = -1; | |
384 | } | |
385 | if (wait_fd >= 0) { | |
386 | ret = close(wait_fd); | |
387 | if (ret) { | |
388 | PERROR("Error closing stream wait_fd"); | |
389 | } | |
390 | *args.stream.wait_fd = -1; | |
391 | } | |
392 | break; | |
393 | case LTTNG_UST_METADATA: | |
394 | case LTTNG_UST_CHANNEL: | |
395 | if (shm_fd >= 0) { | |
396 | ret = close(shm_fd); | |
397 | if (ret) { | |
398 | PERROR("Error closing channel shm_fd"); | |
399 | } | |
400 | *args.channel.shm_fd = -1; | |
401 | } | |
402 | if (wait_fd >= 0) { | |
403 | ret = close(wait_fd); | |
404 | if (ret) { | |
405 | PERROR("Error closing channel wait_fd"); | |
406 | } | |
407 | *args.channel.wait_fd = -1; | |
408 | } | |
409 | break; | |
410 | } | |
411 | } | |
412 | ||
381c0f1e | 413 | error: |
17dfb34b | 414 | ust_unlock(); |
1ea11eab | 415 | return ret; |
d9e99d10 MD |
416 | } |
417 | ||
46050b1a | 418 | static |
efe0de09 | 419 | void cleanup_sock_info(struct sock_info *sock_info, int exiting) |
46050b1a MD |
420 | { |
421 | int ret; | |
422 | ||
423 | if (sock_info->socket != -1) { | |
e6973a89 | 424 | ret = ustcomm_close_unix_sock(sock_info->socket); |
46050b1a | 425 | if (ret) { |
7fc90dca | 426 | ERR("Error closing apps socket"); |
46050b1a MD |
427 | } |
428 | sock_info->socket = -1; | |
429 | } | |
430 | if (sock_info->root_handle != -1) { | |
d4419b81 | 431 | ret = lttng_ust_objd_unref(sock_info->root_handle); |
46050b1a MD |
432 | if (ret) { |
433 | ERR("Error unref root handle"); | |
434 | } | |
435 | sock_info->root_handle = -1; | |
436 | } | |
318dfea9 | 437 | sock_info->constructor_sem_posted = 0; |
efe0de09 MD |
438 | /* |
439 | * wait_shm_mmap is used by listener threads outside of the | |
440 | * ust lock, so we cannot tear it down ourselves, because we | |
441 | * cannot join on these threads. Leave this task to the OS | |
442 | * process exit. | |
443 | */ | |
444 | if (!exiting && sock_info->wait_shm_mmap) { | |
7fc90dca MD |
445 | ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE)); |
446 | if (ret) { | |
447 | ERR("Error unmapping wait shm"); | |
448 | } | |
449 | sock_info->wait_shm_mmap = NULL; | |
450 | } | |
451 | } | |
452 | ||
58d4b2a2 | 453 | /* |
33bbeb90 MD |
454 | * Using fork to set umask in the child process (not multi-thread safe). |
455 | * We deal with the shm_open vs ftruncate race (happening when the | |
456 | * sessiond owns the shm and does not let everybody modify it, to ensure | |
457 | * safety against shm_unlink) by simply letting the mmap fail and | |
458 | * retrying after a few seconds. | |
459 | * For global shm, everybody has rw access to it until the sessiond | |
460 | * starts. | |
58d4b2a2 | 461 | */ |
7fc90dca | 462 | static |
58d4b2a2 | 463 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
7fc90dca | 464 | { |
7fc90dca | 465 | int wait_shm_fd, ret; |
58d4b2a2 | 466 | pid_t pid; |
44e073f5 | 467 | |
58d4b2a2 | 468 | /* |
33bbeb90 | 469 | * Try to open read-only. |
58d4b2a2 | 470 | */ |
33bbeb90 | 471 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
472 | if (wait_shm_fd >= 0) { |
473 | goto end; | |
474 | } else if (wait_shm_fd < 0 && errno != ENOENT) { | |
475 | /* | |
33bbeb90 MD |
476 | * Real-only open did not work, and it's not because the |
477 | * entry was not present. It's a failure that prohibits | |
478 | * using shm. | |
58d4b2a2 | 479 | */ |
7fc90dca | 480 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
58d4b2a2 | 481 | goto end; |
7fc90dca MD |
482 | } |
483 | /* | |
58d4b2a2 MD |
484 | * If the open failed because the file did not exist, try |
485 | * creating it ourself. | |
7fc90dca | 486 | */ |
3b8b68e7 | 487 | lttng_ust_nest_count++; |
58d4b2a2 | 488 | pid = fork(); |
3b8b68e7 | 489 | lttng_ust_nest_count--; |
58d4b2a2 MD |
490 | if (pid > 0) { |
491 | int status; | |
492 | ||
493 | /* | |
494 | * Parent: wait for child to return, in which case the | |
495 | * shared memory map will have been created. | |
496 | */ | |
497 | pid = wait(&status); | |
b7d3cb32 | 498 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
58d4b2a2 MD |
499 | wait_shm_fd = -1; |
500 | goto end; | |
7fc90dca | 501 | } |
58d4b2a2 MD |
502 | /* |
503 | * Try to open read-only again after creation. | |
504 | */ | |
33bbeb90 | 505 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
506 | if (wait_shm_fd < 0) { |
507 | /* | |
508 | * Real-only open did not work. It's a failure | |
509 | * that prohibits using shm. | |
510 | */ | |
511 | ERR("Error opening shm %s", sock_info->wait_shm_path); | |
512 | goto end; | |
513 | } | |
514 | goto end; | |
515 | } else if (pid == 0) { | |
516 | int create_mode; | |
517 | ||
518 | /* Child */ | |
33bbeb90 | 519 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
58d4b2a2 | 520 | if (sock_info->global) |
33bbeb90 | 521 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
58d4b2a2 MD |
522 | /* |
523 | * We're alone in a child process, so we can modify the | |
524 | * process-wide umask. | |
525 | */ | |
33bbeb90 | 526 | umask(~create_mode); |
58d4b2a2 | 527 | /* |
33bbeb90 MD |
528 | * Try creating shm (or get rw access). |
529 | * We don't do an exclusive open, because we allow other | |
530 | * processes to create+ftruncate it concurrently. | |
58d4b2a2 MD |
531 | */ |
532 | wait_shm_fd = shm_open(sock_info->wait_shm_path, | |
533 | O_RDWR | O_CREAT, create_mode); | |
534 | if (wait_shm_fd >= 0) { | |
535 | ret = ftruncate(wait_shm_fd, mmap_size); | |
536 | if (ret) { | |
537 | PERROR("ftruncate"); | |
538 | exit(EXIT_FAILURE); | |
539 | } | |
58d4b2a2 MD |
540 | exit(EXIT_SUCCESS); |
541 | } | |
33bbeb90 MD |
542 | /* |
543 | * For local shm, we need to have rw access to accept | |
544 | * opening it: this means the local sessiond will be | |
545 | * able to wake us up. For global shm, we open it even | |
546 | * if rw access is not granted, because the root.root | |
547 | * sessiond will be able to override all rights and wake | |
548 | * us up. | |
549 | */ | |
550 | if (!sock_info->global && errno != EACCES) { | |
58d4b2a2 MD |
551 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
552 | exit(EXIT_FAILURE); | |
553 | } | |
554 | /* | |
33bbeb90 MD |
555 | * The shm exists, but we cannot open it RW. Report |
556 | * success. | |
58d4b2a2 MD |
557 | */ |
558 | exit(EXIT_SUCCESS); | |
559 | } else { | |
560 | return -1; | |
7fc90dca | 561 | } |
58d4b2a2 | 562 | end: |
33bbeb90 MD |
563 | if (wait_shm_fd >= 0 && !sock_info->global) { |
564 | struct stat statbuf; | |
565 | ||
566 | /* | |
567 | * Ensure that our user is the owner of the shm file for | |
568 | * local shm. If we do not own the file, it means our | |
569 | * sessiond will not have access to wake us up (there is | |
570 | * probably a rogue process trying to fake our | |
571 | * sessiond). Fallback to polling method in this case. | |
572 | */ | |
573 | ret = fstat(wait_shm_fd, &statbuf); | |
574 | if (ret) { | |
575 | PERROR("fstat"); | |
576 | goto error_close; | |
577 | } | |
578 | if (statbuf.st_uid != getuid()) | |
579 | goto error_close; | |
580 | } | |
58d4b2a2 | 581 | return wait_shm_fd; |
33bbeb90 MD |
582 | |
583 | error_close: | |
584 | ret = close(wait_shm_fd); | |
585 | if (ret) { | |
586 | PERROR("Error closing fd"); | |
587 | } | |
588 | return -1; | |
58d4b2a2 MD |
589 | } |
590 | ||
591 | static | |
592 | char *get_map_shm(struct sock_info *sock_info) | |
593 | { | |
594 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
595 | int wait_shm_fd, ret; | |
596 | char *wait_shm_mmap; | |
597 | ||
598 | wait_shm_fd = get_wait_shm(sock_info, mmap_size); | |
599 | if (wait_shm_fd < 0) { | |
600 | goto error; | |
44e073f5 | 601 | } |
7fc90dca MD |
602 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ, |
603 | MAP_SHARED, wait_shm_fd, 0); | |
7fc90dca MD |
604 | /* close shm fd immediately after taking the mmap reference */ |
605 | ret = close(wait_shm_fd); | |
606 | if (ret) { | |
33bbeb90 MD |
607 | PERROR("Error closing fd"); |
608 | } | |
609 | if (wait_shm_mmap == MAP_FAILED) { | |
610 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); | |
611 | goto error; | |
7fc90dca MD |
612 | } |
613 | return wait_shm_mmap; | |
614 | ||
615 | error: | |
616 | return NULL; | |
617 | } | |
618 | ||
619 | static | |
620 | void wait_for_sessiond(struct sock_info *sock_info) | |
621 | { | |
efe0de09 | 622 | int ret; |
80e2814b | 623 | |
7fc90dca MD |
624 | ust_lock(); |
625 | if (lttng_ust_comm_should_quit) { | |
626 | goto quit; | |
627 | } | |
37ed587a MD |
628 | if (wait_poll_fallback) { |
629 | goto error; | |
630 | } | |
7fc90dca MD |
631 | if (!sock_info->wait_shm_mmap) { |
632 | sock_info->wait_shm_mmap = get_map_shm(sock_info); | |
633 | if (!sock_info->wait_shm_mmap) | |
634 | goto error; | |
635 | } | |
636 | ust_unlock(); | |
637 | ||
638 | DBG("Waiting for %s apps sessiond", sock_info->name); | |
80e2814b MD |
639 | /* Wait for futex wakeup */ |
640 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) { | |
641 | ret = futex_async((int32_t *) sock_info->wait_shm_mmap, | |
642 | FUTEX_WAIT, 0, NULL, NULL, 0); | |
80e2814b | 643 | if (ret < 0) { |
37ed587a MD |
644 | if (errno == EFAULT) { |
645 | wait_poll_fallback = 1; | |
a8b870ad | 646 | DBG( |
37ed587a MD |
647 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " |
648 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " | |
649 | "Please upgrade your kernel " | |
650 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " | |
651 | "mainline). LTTng-UST will use polling mode fallback."); | |
cd27263b MD |
652 | if (ust_debug()) |
653 | PERROR("futex"); | |
37ed587a | 654 | } |
80e2814b MD |
655 | } |
656 | } | |
7fc90dca MD |
657 | return; |
658 | ||
659 | quit: | |
660 | ust_unlock(); | |
661 | return; | |
662 | ||
663 | error: | |
664 | ust_unlock(); | |
7fc90dca | 665 | return; |
46050b1a MD |
666 | } |
667 | ||
1ea11eab MD |
668 | /* |
669 | * This thread does not allocate any resource, except within | |
670 | * handle_message, within mutex protection. This mutex protects against | |
671 | * fork and exit. | |
672 | * The other moment it allocates resources is at socket connexion, which | |
673 | * is also protected by the mutex. | |
674 | */ | |
d9e99d10 MD |
675 | static |
676 | void *ust_listener_thread(void *arg) | |
677 | { | |
1ea11eab | 678 | struct sock_info *sock_info = arg; |
c0eedf81 | 679 | int sock, ret, prev_connect_failed = 0, has_waited = 0; |
d9e99d10 | 680 | |
9eb62b9c MD |
681 | /* Restart trying to connect to the session daemon */ |
682 | restart: | |
c0eedf81 MD |
683 | if (prev_connect_failed) { |
684 | /* Wait for sessiond availability with pipe */ | |
685 | wait_for_sessiond(sock_info); | |
686 | if (has_waited) { | |
687 | has_waited = 0; | |
688 | /* | |
689 | * Sleep for 5 seconds before retrying after a | |
690 | * sequence of failure / wait / failure. This | |
691 | * deals with a killed or broken session daemon. | |
692 | */ | |
693 | sleep(5); | |
694 | } | |
695 | has_waited = 1; | |
696 | prev_connect_failed = 0; | |
697 | } | |
17dfb34b | 698 | ust_lock(); |
1ea11eab MD |
699 | |
700 | if (lttng_ust_comm_should_quit) { | |
17dfb34b | 701 | ust_unlock(); |
1ea11eab MD |
702 | goto quit; |
703 | } | |
9eb62b9c | 704 | |
1ea11eab | 705 | if (sock_info->socket != -1) { |
e6973a89 | 706 | ret = ustcomm_close_unix_sock(sock_info->socket); |
1ea11eab | 707 | if (ret) { |
11ff9c7d | 708 | ERR("Error closing %s apps socket", sock_info->name); |
1ea11eab MD |
709 | } |
710 | sock_info->socket = -1; | |
711 | } | |
46050b1a | 712 | |
9eb62b9c | 713 | /* Register */ |
57773204 | 714 | ret = ustcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c | 715 | if (ret < 0) { |
4d3c9523 | 716 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
c0eedf81 | 717 | prev_connect_failed = 1; |
11ff9c7d MD |
718 | /* |
719 | * If we cannot find the sessiond daemon, don't delay | |
720 | * constructor execution. | |
721 | */ | |
edaa1431 | 722 | ret = handle_register_done(sock_info); |
11ff9c7d | 723 | assert(!ret); |
17dfb34b | 724 | ust_unlock(); |
1ea11eab | 725 | goto restart; |
46050b1a MD |
726 | } |
727 | ||
728 | sock_info->socket = sock = ret; | |
729 | ||
730 | /* | |
731 | * Create only one root handle per listener thread for the whole | |
732 | * process lifetime. | |
733 | */ | |
734 | if (sock_info->root_handle == -1) { | |
735 | ret = lttng_abi_create_root_handle(); | |
a51070bb | 736 | if (ret < 0) { |
46050b1a | 737 | ERR("Error creating root handle"); |
17dfb34b | 738 | ust_unlock(); |
46050b1a MD |
739 | goto quit; |
740 | } | |
741 | sock_info->root_handle = ret; | |
9eb62b9c | 742 | } |
1ea11eab | 743 | |
9eb62b9c MD |
744 | ret = register_app_to_sessiond(sock); |
745 | if (ret < 0) { | |
11ff9c7d | 746 | ERR("Error registering to %s apps socket", sock_info->name); |
c0eedf81 | 747 | prev_connect_failed = 1; |
11ff9c7d MD |
748 | /* |
749 | * If we cannot register to the sessiond daemon, don't | |
750 | * delay constructor execution. | |
751 | */ | |
edaa1431 | 752 | ret = handle_register_done(sock_info); |
11ff9c7d | 753 | assert(!ret); |
17dfb34b | 754 | ust_unlock(); |
9eb62b9c MD |
755 | goto restart; |
756 | } | |
17dfb34b | 757 | ust_unlock(); |
46050b1a | 758 | |
d9e99d10 MD |
759 | for (;;) { |
760 | ssize_t len; | |
57773204 | 761 | struct ustcomm_ust_msg lum; |
d9e99d10 | 762 | |
57773204 | 763 | len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
764 | switch (len) { |
765 | case 0: /* orderly shutdown */ | |
11ff9c7d | 766 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
8236ba10 MD |
767 | ust_lock(); |
768 | /* | |
769 | * Either sessiond has shutdown or refused us by closing the socket. | |
770 | * In either case, we don't want to delay construction execution, | |
771 | * and we need to wait before retry. | |
772 | */ | |
773 | prev_connect_failed = 1; | |
774 | /* | |
775 | * If we cannot register to the sessiond daemon, don't | |
776 | * delay constructor execution. | |
777 | */ | |
778 | ret = handle_register_done(sock_info); | |
779 | assert(!ret); | |
780 | ust_unlock(); | |
d9e99d10 | 781 | goto end; |
e7723462 | 782 | case sizeof(lum): |
d9e99d10 | 783 | DBG("message received\n"); |
11ff9c7d | 784 | ret = handle_message(sock_info, sock, &lum); |
2a80c9d8 | 785 | if (ret < 0) { |
11ff9c7d | 786 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
787 | } |
788 | continue; | |
789 | case -1: | |
8236ba10 | 790 | DBG("Receive failed from lttng-sessiond with errno %d", errno); |
d9e99d10 | 791 | if (errno == ECONNRESET) { |
11ff9c7d | 792 | ERR("%s remote end closed connection\n", sock_info->name); |
d9e99d10 MD |
793 | goto end; |
794 | } | |
795 | goto end; | |
796 | default: | |
11ff9c7d | 797 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
d9e99d10 MD |
798 | continue; |
799 | } | |
800 | ||
801 | } | |
802 | end: | |
9eb62b9c | 803 | goto restart; /* try to reconnect */ |
1ea11eab | 804 | quit: |
d9e99d10 MD |
805 | return NULL; |
806 | } | |
807 | ||
cf12a773 MD |
808 | /* |
809 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
810 | */ | |
11ff9c7d MD |
811 | static |
812 | int get_timeout(struct timespec *constructor_timeout) | |
813 | { | |
cf12a773 MD |
814 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
815 | char *str_delay; | |
11ff9c7d MD |
816 | int ret; |
817 | ||
69400ac4 | 818 | str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT"); |
cf12a773 MD |
819 | if (str_delay) { |
820 | constructor_delay_ms = strtol(str_delay, NULL, 10); | |
821 | } | |
822 | ||
823 | switch (constructor_delay_ms) { | |
824 | case -1:/* fall-through */ | |
825 | case 0: | |
826 | return constructor_delay_ms; | |
827 | default: | |
828 | break; | |
829 | } | |
830 | ||
831 | /* | |
832 | * If we are unable to find the current time, don't wait. | |
833 | */ | |
834 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
835 | if (ret) { | |
836 | return -1; | |
837 | } | |
95259bd0 MD |
838 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
839 | constructor_timeout->tv_nsec += | |
840 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
11ff9c7d MD |
841 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
842 | constructor_timeout->tv_sec++; | |
843 | constructor_timeout->tv_nsec -= 1000000000UL; | |
844 | } | |
cf12a773 | 845 | return 1; |
11ff9c7d | 846 | } |
d9e99d10 | 847 | |
2691221a MD |
848 | /* |
849 | * sessiond monitoring thread: monitor presence of global and per-user | |
850 | * sessiond by polling the application common named pipe. | |
851 | */ | |
852 | /* TODO */ | |
853 | ||
edaa1431 | 854 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 855 | { |
11ff9c7d | 856 | struct timespec constructor_timeout; |
e32d20e9 | 857 | sigset_t sig_all_blocked, orig_parent_mask; |
cf12a773 | 858 | int timeout_mode; |
2691221a MD |
859 | int ret; |
860 | ||
edaa1431 MD |
861 | if (uatomic_xchg(&initialized, 1) == 1) |
862 | return; | |
863 | ||
eddd8d5d MD |
864 | /* |
865 | * Fixup interdependency between TLS fixup mutex (which happens | |
866 | * to be the dynamic linker mutex) and ust_lock, taken within | |
867 | * the ust lock. | |
868 | */ | |
869 | lttng_fixup_event_tls(); | |
f645cfa7 | 870 | lttng_fixup_ringbuffer_tls(); |
4158a15a | 871 | lttng_fixup_vtid_tls(); |
e38c8fc1 | 872 | lttng_fixup_nest_count_tls(); |
eddd8d5d | 873 | |
edaa1431 MD |
874 | /* |
875 | * We want precise control over the order in which we construct | |
876 | * our sub-libraries vs starting to receive commands from | |
877 | * sessiond (otherwise leading to errors when trying to create | |
878 | * sessiond before the init functions are completed). | |
879 | */ | |
2691221a | 880 | init_usterr(); |
edaa1431 MD |
881 | init_tracepoint(); |
882 | ltt_ring_buffer_metadata_client_init(); | |
883 | ltt_ring_buffer_client_overwrite_init(); | |
884 | ltt_ring_buffer_client_discard_init(); | |
2691221a | 885 | |
cf12a773 | 886 | timeout_mode = get_timeout(&constructor_timeout); |
11ff9c7d | 887 | |
95259bd0 | 888 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
889 | assert(!ret); |
890 | ||
8d20bf54 | 891 | ret = setup_local_apps(); |
2691221a | 892 | if (ret) { |
8d20bf54 | 893 | ERR("Error setting up to local apps"); |
2691221a | 894 | } |
e32d20e9 WP |
895 | |
896 | /* A new thread created by pthread_create inherits the signal mask | |
897 | * from the parent. To avoid any signal being received by the | |
898 | * listener thread, we block all signals temporarily in the parent, | |
899 | * while we create the listener thread. | |
900 | */ | |
901 | sigfillset(&sig_all_blocked); | |
902 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); | |
903 | if (ret) { | |
e37047b6 | 904 | ERR("pthread_sigmask: %s", strerror(ret)); |
e32d20e9 WP |
905 | } |
906 | ||
dc43c97f MD |
907 | ret = pthread_create(&global_apps.ust_listener, NULL, |
908 | ust_listener_thread, &global_apps); | |
e37047b6 MD |
909 | if (ret) { |
910 | ERR("pthread_create global: %s", strerror(ret)); | |
911 | } | |
8d20bf54 | 912 | if (local_apps.allowed) { |
dc43c97f MD |
913 | ret = pthread_create(&local_apps.ust_listener, NULL, |
914 | ust_listener_thread, &local_apps); | |
e37047b6 MD |
915 | if (ret) { |
916 | ERR("pthread_create local: %s", strerror(ret)); | |
917 | } | |
8d20bf54 MD |
918 | } else { |
919 | handle_register_done(&local_apps); | |
920 | } | |
921 | ||
e32d20e9 WP |
922 | /* Restore original signal mask in parent */ |
923 | ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); | |
924 | if (ret) { | |
e37047b6 | 925 | ERR("pthread_sigmask: %s", strerror(ret)); |
e32d20e9 WP |
926 | } |
927 | ||
cf12a773 MD |
928 | switch (timeout_mode) { |
929 | case 1: /* timeout wait */ | |
95259bd0 MD |
930 | do { |
931 | ret = sem_timedwait(&constructor_wait, | |
932 | &constructor_timeout); | |
933 | } while (ret < 0 && errno == EINTR); | |
cf12a773 MD |
934 | if (ret < 0 && errno == ETIMEDOUT) { |
935 | ERR("Timed out waiting for ltt-sessiond"); | |
936 | } else { | |
937 | assert(!ret); | |
938 | } | |
939 | break; | |
7b766b16 | 940 | case -1:/* wait forever */ |
95259bd0 MD |
941 | do { |
942 | ret = sem_wait(&constructor_wait); | |
943 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 944 | assert(!ret); |
cf12a773 | 945 | break; |
7b766b16 | 946 | case 0: /* no timeout */ |
cf12a773 | 947 | break; |
11ff9c7d | 948 | } |
2691221a MD |
949 | } |
950 | ||
17dfb34b MD |
951 | static |
952 | void lttng_ust_cleanup(int exiting) | |
953 | { | |
efe0de09 | 954 | cleanup_sock_info(&global_apps, exiting); |
17dfb34b | 955 | if (local_apps.allowed) { |
efe0de09 | 956 | cleanup_sock_info(&local_apps, exiting); |
17dfb34b | 957 | } |
efe0de09 MD |
958 | /* |
959 | * The teardown in this function all affect data structures | |
960 | * accessed under the UST lock by the listener thread. This | |
961 | * lock, along with the lttng_ust_comm_should_quit flag, ensure | |
962 | * that none of these threads are accessing this data at this | |
963 | * point. | |
964 | */ | |
17dfb34b | 965 | lttng_ust_abi_exit(); |
003fedf4 | 966 | lttng_ust_events_exit(); |
17dfb34b MD |
967 | ltt_ring_buffer_client_discard_exit(); |
968 | ltt_ring_buffer_client_overwrite_exit(); | |
969 | ltt_ring_buffer_metadata_client_exit(); | |
970 | exit_tracepoint(); | |
971 | if (!exiting) { | |
972 | /* Reinitialize values for fork */ | |
973 | sem_count = 2; | |
974 | lttng_ust_comm_should_quit = 0; | |
975 | initialized = 0; | |
976 | } | |
977 | } | |
978 | ||
edaa1431 | 979 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
980 | { |
981 | int ret; | |
982 | ||
9eb62b9c MD |
983 | /* |
984 | * Using pthread_cancel here because: | |
985 | * A) we don't want to hang application teardown. | |
986 | * B) the thread is not allocating any resource. | |
987 | */ | |
1ea11eab MD |
988 | |
989 | /* | |
990 | * Require the communication thread to quit. Synchronize with | |
991 | * mutexes to ensure it is not in a mutex critical section when | |
992 | * pthread_cancel is later called. | |
993 | */ | |
17dfb34b | 994 | ust_lock(); |
1ea11eab | 995 | lttng_ust_comm_should_quit = 1; |
17dfb34b | 996 | ust_unlock(); |
1ea11eab | 997 | |
f5f94532 | 998 | /* cancel threads */ |
1ea11eab | 999 | ret = pthread_cancel(global_apps.ust_listener); |
9eb62b9c | 1000 | if (ret) { |
e37047b6 MD |
1001 | ERR("Error cancelling global ust listener thread: %s", |
1002 | strerror(ret)); | |
2691221a | 1003 | } |
8d20bf54 MD |
1004 | if (local_apps.allowed) { |
1005 | ret = pthread_cancel(local_apps.ust_listener); | |
1006 | if (ret) { | |
e37047b6 MD |
1007 | ERR("Error cancelling local ust listener thread: %s", |
1008 | strerror(ret)); | |
8d20bf54 | 1009 | } |
8d20bf54 | 1010 | } |
efe0de09 MD |
1011 | /* |
1012 | * Do NOT join threads: use of sys_futex makes it impossible to | |
1013 | * join the threads without using async-cancel, but async-cancel | |
1014 | * is delivered by a signal, which could hit the target thread | |
1015 | * anywhere in its code path, including while the ust_lock() is | |
1016 | * held, causing a deadlock for the other thread. Let the OS | |
1017 | * cleanup the threads if there are stalled in a syscall. | |
1018 | */ | |
17dfb34b | 1019 | lttng_ust_cleanup(1); |
2691221a | 1020 | } |
e822f505 MD |
1021 | |
1022 | /* | |
1023 | * We exclude the worker threads across fork and clone (except | |
1024 | * CLONE_VM), because these system calls only keep the forking thread | |
1025 | * running in the child. Therefore, we don't want to call fork or clone | |
1026 | * in the middle of an tracepoint or ust tracing state modification. | |
1027 | * Holding this mutex protects these structures across fork and clone. | |
1028 | */ | |
b728d87e | 1029 | void ust_before_fork(sigset_t *save_sigset) |
e822f505 MD |
1030 | { |
1031 | /* | |
1032 | * Disable signals. This is to avoid that the child intervenes | |
1033 | * before it is properly setup for tracing. It is safer to | |
1034 | * disable all signals, because then we know we are not breaking | |
1035 | * anything by restoring the original mask. | |
1036 | */ | |
1037 | sigset_t all_sigs; | |
1038 | int ret; | |
1039 | ||
3b8b68e7 MD |
1040 | if (lttng_ust_nest_count) |
1041 | return; | |
e822f505 MD |
1042 | /* Disable signals */ |
1043 | sigfillset(&all_sigs); | |
b728d87e | 1044 | ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); |
e822f505 MD |
1045 | if (ret == -1) { |
1046 | PERROR("sigprocmask"); | |
1047 | } | |
17dfb34b | 1048 | ust_lock(); |
e822f505 MD |
1049 | rcu_bp_before_fork(); |
1050 | } | |
1051 | ||
b728d87e | 1052 | static void ust_after_fork_common(sigset_t *restore_sigset) |
e822f505 MD |
1053 | { |
1054 | int ret; | |
1055 | ||
17dfb34b MD |
1056 | DBG("process %d", getpid()); |
1057 | ust_unlock(); | |
e822f505 | 1058 | /* Restore signals */ |
23c8854a | 1059 | ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL); |
e822f505 MD |
1060 | if (ret == -1) { |
1061 | PERROR("sigprocmask"); | |
1062 | } | |
1063 | } | |
1064 | ||
b728d87e | 1065 | void ust_after_fork_parent(sigset_t *restore_sigset) |
e822f505 | 1066 | { |
3b8b68e7 MD |
1067 | if (lttng_ust_nest_count) |
1068 | return; | |
17dfb34b | 1069 | DBG("process %d", getpid()); |
e822f505 MD |
1070 | rcu_bp_after_fork_parent(); |
1071 | /* Release mutexes and reenable signals */ | |
b728d87e | 1072 | ust_after_fork_common(restore_sigset); |
e822f505 MD |
1073 | } |
1074 | ||
17dfb34b MD |
1075 | /* |
1076 | * After fork, in the child, we need to cleanup all the leftover state, | |
1077 | * except the worker thread which already magically disappeared thanks | |
1078 | * to the weird Linux fork semantics. After tyding up, we call | |
1079 | * lttng_ust_init() again to start over as a new PID. | |
1080 | * | |
1081 | * This is meant for forks() that have tracing in the child between the | |
1082 | * fork and following exec call (if there is any). | |
1083 | */ | |
b728d87e | 1084 | void ust_after_fork_child(sigset_t *restore_sigset) |
e822f505 | 1085 | { |
3b8b68e7 MD |
1086 | if (lttng_ust_nest_count) |
1087 | return; | |
17dfb34b | 1088 | DBG("process %d", getpid()); |
e822f505 MD |
1089 | /* Release urcu mutexes */ |
1090 | rcu_bp_after_fork_child(); | |
17dfb34b | 1091 | lttng_ust_cleanup(0); |
a93bfc45 | 1092 | lttng_context_vtid_reset(); |
e822f505 | 1093 | /* Release mutexes and reenable signals */ |
b728d87e | 1094 | ust_after_fork_common(restore_sigset); |
318dfea9 | 1095 | lttng_ust_init(); |
e822f505 | 1096 | } |