Revert "Fix linker library order"
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
CommitLineData
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>
7fc90dca
MD
25#include <sys/mman.h>
26#include <sys/stat.h>
58d4b2a2
MD
27#include <sys/types.h>
28#include <sys/wait.h>
7fc90dca 29#include <fcntl.h>
2691221a
MD
30#include <unistd.h>
31#include <errno.h>
d9e99d10 32#include <pthread.h>
11ff9c7d
MD
33#include <semaphore.h>
34#include <time.h>
1ea11eab 35#include <assert.h>
e822f505 36#include <signal.h>
95259bd0 37#include <urcu/uatomic.h>
80e2814b 38#include <urcu/futex.h>
c117fb1b 39#include <urcu/compiler.h>
1ea11eab 40
4318ae1b 41#include <lttng/ust-events.h>
4318ae1b 42#include <lttng/ust-abi.h>
4318ae1b 43#include <lttng/ust.h>
7bc53e94 44#include <lttng/ust-error.h>
74d81a6c 45#include <lttng/ust-ctl.h>
8c90a710 46#include <urcu/tls-compat.h>
44c72f10
MD
47#include <ust-comm.h>
48#include <usterr-signal-safe.h>
cd54f6d9 49#include <helper.h>
44c72f10 50#include "tracepoint-internal.h"
7dd08bec 51#include "lttng-tracer-core.h"
08114193 52#include "compat.h"
f645cfa7 53#include "../libringbuffer/tlsfixup.h"
edaa1431
MD
54
55/*
56 * Has lttng ust comm constructor been called ?
57 */
58static int initialized;
59
1ea11eab 60/*
17dfb34b
MD
61 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
62 * Held when handling a command, also held by fork() to deal with
63 * removal of threads, and by exit path.
1ea11eab 64 */
1ea11eab
MD
65
66/* Should the ust comm thread quit ? */
67static int lttng_ust_comm_should_quit;
68
11ff9c7d
MD
69/*
70 * Wait for either of these before continuing to the main
71 * program:
72 * - the register_done message from sessiond daemon
73 * (will let the sessiond daemon enable sessions before main
74 * starts.)
75 * - sessiond daemon is not reachable.
76 * - timeout (ensuring applications are resilient to session
77 * daemon problems).
78 */
79static sem_t constructor_wait;
950aab0c
MD
80/*
81 * Doing this for both the global and local sessiond.
82 */
95259bd0 83static int sem_count = { 2 };
11ff9c7d 84
e8508a49
MD
85/*
86 * Counting nesting within lttng-ust. Used to ensure that calling fork()
87 * from liblttng-ust does not execute the pre/post fork handlers.
88 */
8c90a710 89static DEFINE_URCU_TLS(int, lttng_ust_nest_count);
e8508a49 90
1ea11eab
MD
91/*
92 * Info about socket and associated listener thread.
93 */
94struct sock_info {
11ff9c7d 95 const char *name;
1ea11eab 96 pthread_t ust_listener; /* listener thread */
46050b1a 97 int root_handle;
8d20bf54
MD
98 int constructor_sem_posted;
99 int allowed;
44e073f5 100 int global;
e33f3265 101 int thread_active;
7fc90dca
MD
102
103 char sock_path[PATH_MAX];
104 int socket;
32ce8569 105 int notify_socket;
7fc90dca
MD
106
107 char wait_shm_path[PATH_MAX];
108 char *wait_shm_mmap;
1ea11eab 109};
2691221a
MD
110
111/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 112struct sock_info global_apps = {
11ff9c7d 113 .name = "global",
44e073f5 114 .global = 1,
7fc90dca 115
46050b1a 116 .root_handle = -1,
8d20bf54 117 .allowed = 1,
e33f3265 118 .thread_active = 0,
7fc90dca 119
32ce8569 120 .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME,
7fc90dca 121 .socket = -1,
32ce8569 122 .notify_socket = -1,
7fc90dca 123
32ce8569 124 .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME,
1ea11eab 125};
2691221a
MD
126
127/* TODO: allow global_apps_sock_path override */
128
1ea11eab 129struct sock_info local_apps = {
11ff9c7d 130 .name = "local",
44e073f5 131 .global = 0,
46050b1a 132 .root_handle = -1,
8d20bf54 133 .allowed = 0, /* Check setuid bit first */
e33f3265 134 .thread_active = 0,
7fc90dca
MD
135
136 .socket = -1,
32ce8569 137 .notify_socket = -1,
1ea11eab 138};
2691221a 139
37ed587a
MD
140static int wait_poll_fallback;
141
74d81a6c
MD
142static const char *cmd_name_mapping[] = {
143 [ LTTNG_UST_RELEASE ] = "Release",
144 [ LTTNG_UST_SESSION ] = "Create Session",
145 [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version",
146
147 [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List",
148 [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State",
149 [ LTTNG_UST_REGISTER_DONE ] = "Registration Done",
150 [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List",
151
152 /* Session FD commands */
153 [ LTTNG_UST_CHANNEL ] = "Create Channel",
154 [ LTTNG_UST_SESSION_START ] = "Start Session",
155 [ LTTNG_UST_SESSION_STOP ] = "Stop Session",
156
157 /* Channel FD commands */
158 [ LTTNG_UST_STREAM ] = "Create Stream",
159 [ LTTNG_UST_EVENT ] = "Create Event",
160
161 /* Event and Channel FD commands */
162 [ LTTNG_UST_CONTEXT ] = "Create Context",
163 [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer",
164
165 /* Event, Channel and Session commands */
166 [ LTTNG_UST_ENABLE ] = "Enable",
167 [ LTTNG_UST_DISABLE ] = "Disable",
168
169 /* Tracepoint list commands */
170 [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint",
171 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field",
172
173 /* Event FD commands */
174 [ LTTNG_UST_FILTER ] = "Create Filter",
175};
176
ff517991
MD
177static const char *str_timeout;
178static int got_timeout_env;
179
7dd08bec 180extern void lttng_ring_buffer_client_overwrite_init(void);
34a91bdb 181extern void lttng_ring_buffer_client_overwrite_rt_init(void);
7dd08bec 182extern void lttng_ring_buffer_client_discard_init(void);
34a91bdb 183extern void lttng_ring_buffer_client_discard_rt_init(void);
7dd08bec
MD
184extern void lttng_ring_buffer_metadata_client_init(void);
185extern void lttng_ring_buffer_client_overwrite_exit(void);
34a91bdb 186extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
7dd08bec 187extern void lttng_ring_buffer_client_discard_exit(void);
34a91bdb 188extern void lttng_ring_buffer_client_discard_rt_exit(void);
7dd08bec 189extern void lttng_ring_buffer_metadata_client_exit(void);
edaa1431 190
a903623f
MD
191/*
192 * Force a read (imply TLS fixup for dlopen) of TLS variables.
193 */
194static
195void lttng_fixup_nest_count_tls(void)
196{
8c90a710 197 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count)));
a903623f
MD
198}
199
32ce8569
MD
200int lttng_get_notify_socket(void *owner)
201{
202 struct sock_info *info = owner;
203
204 return info->notify_socket;
205}
206
74d81a6c
MD
207static
208void print_cmd(int cmd, int handle)
209{
210 const char *cmd_name = "Unknown";
211
212 if (cmd_name_mapping[cmd]) {
213 cmd_name = cmd_name_mapping[cmd];
214 }
215 DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name,
216 lttng_ust_obj_get_name(handle), handle);
217}
218
2691221a 219static
8d20bf54 220int setup_local_apps(void)
2691221a
MD
221{
222 const char *home_dir;
7fc90dca 223 uid_t uid;
2691221a 224
7fc90dca 225 uid = getuid();
8d20bf54
MD
226 /*
227 * Disallow per-user tracing for setuid binaries.
228 */
7fc90dca 229 if (uid != geteuid()) {
9ec6895c 230 assert(local_apps.allowed == 0);
d0a1ae63 231 return 0;
8d20bf54 232 }
2691221a 233 home_dir = (const char *) getenv("HOME");
9ec6895c
MD
234 if (!home_dir) {
235 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
236 assert(local_apps.allowed == 0);
2691221a 237 return -ENOENT;
9ec6895c
MD
238 }
239 local_apps.allowed = 1;
32ce8569
MD
240 snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s",
241 home_dir,
242 LTTNG_DEFAULT_HOME_RUNDIR,
243 LTTNG_UST_SOCK_FILENAME);
244 snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u",
245 LTTNG_UST_WAIT_FILENAME,
246 uid);
2691221a
MD
247 return 0;
248}
249
ff517991
MD
250/*
251 * Get notify_sock timeout, in ms.
252 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
253 */
254static
255long get_timeout(void)
256{
257 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
258
259 if (!got_timeout_env) {
260 str_timeout = getenv("LTTNG_UST_REGISTER_TIMEOUT");
261 got_timeout_env = 1;
262 }
263 if (str_timeout)
264 constructor_delay_ms = strtol(str_timeout, NULL, 10);
265 return constructor_delay_ms;
266}
267
268static
269long get_notify_sock_timeout(void)
270{
271 return get_timeout();
272}
273
274/*
275 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
276 */
277static
278int get_constructor_timeout(struct timespec *constructor_timeout)
279{
280 long constructor_delay_ms;
281 int ret;
282
283 constructor_delay_ms = get_timeout();
284
285 switch (constructor_delay_ms) {
286 case -1:/* fall-through */
287 case 0:
288 return constructor_delay_ms;
289 default:
290 break;
291 }
292
293 /*
294 * If we are unable to find the current time, don't wait.
295 */
296 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
297 if (ret) {
298 return -1;
299 }
300 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
301 constructor_timeout->tv_nsec +=
302 (constructor_delay_ms % 1000UL) * 1000000UL;
303 if (constructor_timeout->tv_nsec >= 1000000000UL) {
304 constructor_timeout->tv_sec++;
305 constructor_timeout->tv_nsec -= 1000000000UL;
306 }
307 return 1;
308}
309
2691221a 310static
32ce8569 311int register_to_sessiond(int socket, enum ustctl_socket_type type)
2691221a 312{
32ce8569
MD
313 return ustcomm_send_reg_msg(socket,
314 type,
315 CAA_BITS_PER_LONG,
316 lttng_alignof(uint8_t) * CHAR_BIT,
317 lttng_alignof(uint16_t) * CHAR_BIT,
318 lttng_alignof(uint32_t) * CHAR_BIT,
319 lttng_alignof(uint64_t) * CHAR_BIT,
320 lttng_alignof(unsigned long) * CHAR_BIT);
2691221a
MD
321}
322
d9e99d10 323static
57773204 324int send_reply(int sock, struct ustcomm_ust_reply *lur)
d9e99d10 325{
9eb62b9c 326 ssize_t len;
d3a492d1 327
57773204 328 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 329 switch (len) {
a4be8962 330 case sizeof(*lur):
d3a492d1
MD
331 DBG("message successfully sent");
332 return 0;
7bc53e94
MD
333 default:
334 if (len == -ECONNRESET) {
335 DBG("remote end closed connection");
d3a492d1
MD
336 return 0;
337 }
7bc53e94
MD
338 if (len < 0)
339 return len;
340 DBG("incorrect message size: %zd", len);
341 return -EINVAL;
d3a492d1
MD
342 }
343}
344
345static
edaa1431 346int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
347{
348 int ret;
349
edaa1431
MD
350 if (sock_info->constructor_sem_posted)
351 return 0;
352 sock_info->constructor_sem_posted = 1;
56cd7e2f
MD
353 if (uatomic_read(&sem_count) <= 0) {
354 return 0;
355 }
95259bd0
MD
356 ret = uatomic_add_return(&sem_count, -1);
357 if (ret == 0) {
358 ret = sem_post(&constructor_wait);
359 assert(!ret);
360 }
11ff9c7d
MD
361 return 0;
362}
363
364static
365int handle_message(struct sock_info *sock_info,
57773204 366 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 367{
1ea11eab 368 int ret = 0;
b61ce3b2 369 const struct lttng_ust_objd_ops *ops;
57773204 370 struct ustcomm_ust_reply lur;
ef9ff354 371 union ust_args args;
40003310 372 ssize_t len;
1ea11eab 373
17dfb34b 374 ust_lock();
1ea11eab 375
46050b1a
MD
376 memset(&lur, 0, sizeof(lur));
377
1ea11eab 378 if (lttng_ust_comm_should_quit) {
74d81a6c 379 ret = -LTTNG_UST_ERR_EXITING;
1ea11eab
MD
380 goto end;
381 }
9eb62b9c 382
46050b1a
MD
383 ops = objd_ops(lum->handle);
384 if (!ops) {
385 ret = -ENOENT;
386 goto end;
1ea11eab 387 }
46050b1a
MD
388
389 switch (lum->cmd) {
11ff9c7d
MD
390 case LTTNG_UST_REGISTER_DONE:
391 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 392 ret = handle_register_done(sock_info);
11ff9c7d
MD
393 else
394 ret = -EINVAL;
395 break;
46050b1a
MD
396 case LTTNG_UST_RELEASE:
397 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
398 ret = -EPERM;
399 else
1849ef7c 400 ret = lttng_ust_objd_unref(lum->handle, 1);
d9e99d10 401 break;
2d78951a
MD
402 case LTTNG_UST_FILTER:
403 {
404 /* Receive filter data */
f488575f 405 struct lttng_ust_filter_bytecode_node *bytecode;
2d78951a 406
cd54f6d9 407 if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
7bc53e94 408 ERR("Filter data size is too large: %u bytes",
2d78951a
MD
409 lum->u.filter.data_size);
410 ret = -EINVAL;
411 goto error;
412 }
2734ca65 413
885b1dfd 414 if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
7bc53e94 415 ERR("Filter reloc offset %u is not within data",
2734ca65
CB
416 lum->u.filter.reloc_offset);
417 ret = -EINVAL;
418 goto error;
419 }
420
cd54f6d9
MD
421 bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size);
422 if (!bytecode) {
423 ret = -ENOMEM;
424 goto error;
425 }
f488575f 426 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data,
2d78951a
MD
427 lum->u.filter.data_size);
428 switch (len) {
429 case 0: /* orderly shutdown */
430 ret = 0;
cd54f6d9 431 free(bytecode);
2d78951a 432 goto error;
2d78951a
MD
433 default:
434 if (len == lum->u.filter.data_size) {
7bc53e94 435 DBG("filter data received");
2d78951a 436 break;
7bc53e94
MD
437 } else if (len < 0) {
438 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
439 if (len == -ECONNRESET) {
440 ERR("%s remote end closed connection", sock_info->name);
441 ret = len;
442 free(bytecode);
443 goto error;
444 }
445 ret = len;
446 goto end;
2d78951a 447 } else {
7bc53e94 448 DBG("incorrect filter data message size: %zd", len);
2d78951a 449 ret = -EINVAL;
cd54f6d9 450 free(bytecode);
2d78951a
MD
451 goto end;
452 }
453 }
f488575f
MD
454 bytecode->bc.len = lum->u.filter.data_size;
455 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
3f6fd224 456 bytecode->bc.seqnum = lum->u.filter.seqnum;
cd54f6d9 457 if (ops->cmd) {
2d78951a 458 ret = ops->cmd(lum->handle, lum->cmd,
cd54f6d9 459 (unsigned long) bytecode,
f59ed768 460 &args, sock_info);
cd54f6d9
MD
461 if (ret) {
462 free(bytecode);
463 }
464 /* don't free bytecode if everything went fine. */
465 } else {
2d78951a 466 ret = -ENOSYS;
cd54f6d9
MD
467 free(bytecode);
468 }
2d78951a
MD
469 break;
470 }
74d81a6c
MD
471 case LTTNG_UST_CHANNEL:
472 {
473 void *chan_data;
ff0f5728 474 int wakeup_fd;
74d81a6c
MD
475
476 len = ustcomm_recv_channel_from_sessiond(sock,
ff0f5728
MD
477 &chan_data, lum->u.channel.len,
478 &wakeup_fd);
74d81a6c
MD
479 switch (len) {
480 case 0: /* orderly shutdown */
481 ret = 0;
482 goto error;
483 default:
484 if (len == lum->u.channel.len) {
485 DBG("channel data received");
486 break;
487 } else if (len < 0) {
488 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
489 if (len == -ECONNRESET) {
490 ERR("%s remote end closed connection", sock_info->name);
491 ret = len;
492 goto error;
493 }
494 ret = len;
495 goto end;
496 } else {
497 DBG("incorrect channel data message size: %zd", len);
498 ret = -EINVAL;
499 goto end;
500 }
501 }
502 args.channel.chan_data = chan_data;
ff0f5728 503 args.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
504 if (ops->cmd)
505 ret = ops->cmd(lum->handle, lum->cmd,
506 (unsigned long) &lum->u,
507 &args, sock_info);
508 else
509 ret = -ENOSYS;
510 break;
511 }
512 case LTTNG_UST_STREAM:
513 {
514 /* Receive shm_fd, wakeup_fd */
515 ret = ustcomm_recv_stream_from_sessiond(sock,
516 &lum->u.stream.len,
517 &args.stream.shm_fd,
518 &args.stream.wakeup_fd);
519 if (ret) {
520 goto end;
521 }
522 if (ops->cmd)
523 ret = ops->cmd(lum->handle, lum->cmd,
524 (unsigned long) &lum->u,
525 &args, sock_info);
526 else
527 ret = -ENOSYS;
528 break;
529 }
d9e99d10 530 default:
46050b1a
MD
531 if (ops->cmd)
532 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 533 (unsigned long) &lum->u,
f59ed768 534 &args, sock_info);
46050b1a
MD
535 else
536 ret = -ENOSYS;
537 break;
d9e99d10 538 }
46050b1a 539
1ea11eab 540end:
46050b1a
MD
541 lur.handle = lum->handle;
542 lur.cmd = lum->cmd;
543 lur.ret_val = ret;
544 if (ret >= 0) {
7bc53e94 545 lur.ret_code = LTTNG_UST_OK;
46050b1a 546 } else {
7bc53e94
MD
547 /*
548 * Use -LTTNG_UST_ERR as wildcard for UST internal
549 * error that are not caused by the transport, except if
550 * we already have a more precise error message to
551 * report.
552 */
64b2564e
DG
553 if (ret > -LTTNG_UST_ERR) {
554 /* Translate code to UST error. */
555 switch (ret) {
556 case -EEXIST:
557 lur.ret_code = -LTTNG_UST_ERR_EXIST;
558 break;
559 case -EINVAL:
560 lur.ret_code = -LTTNG_UST_ERR_INVAL;
561 break;
562 case -ENOENT:
563 lur.ret_code = -LTTNG_UST_ERR_NOENT;
564 break;
565 case -EPERM:
566 lur.ret_code = -LTTNG_UST_ERR_PERM;
567 break;
568 case -ENOSYS:
569 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
570 break;
571 default:
572 lur.ret_code = -LTTNG_UST_ERR;
573 break;
574 }
575 } else {
7bc53e94 576 lur.ret_code = ret;
64b2564e 577 }
46050b1a 578 }
e6ea14c5
MD
579 if (ret >= 0) {
580 switch (lum->cmd) {
e6ea14c5
MD
581 case LTTNG_UST_TRACER_VERSION:
582 lur.u.version = lum->u.version;
583 break;
584 case LTTNG_UST_TRACEPOINT_LIST_GET:
585 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
586 break;
587 }
381c0f1e 588 }
74d81a6c 589 DBG("Return value: %d", lur.ret_val);
46050b1a 590 ret = send_reply(sock, &lur);
193183fb 591 if (ret < 0) {
7bc53e94 592 DBG("error sending reply");
193183fb
MD
593 goto error;
594 }
46050b1a 595
40003310
MD
596 /*
597 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
598 * after the reply.
599 */
7bc53e94 600 if (lur.ret_code == LTTNG_UST_OK) {
40003310
MD
601 switch (lum->cmd) {
602 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
603 len = ustcomm_send_unix_sock(sock,
604 &args.field_list.entry,
605 sizeof(args.field_list.entry));
7bc53e94
MD
606 if (len < 0) {
607 ret = len;
608 goto error;
609 }
40003310 610 if (len != sizeof(args.field_list.entry)) {
7bc53e94 611 ret = -EINVAL;
40003310
MD
612 goto error;
613 }
614 }
615 }
ef9ff354 616
381c0f1e 617error:
17dfb34b 618 ust_unlock();
1ea11eab 619 return ret;
d9e99d10
MD
620}
621
46050b1a 622static
efe0de09 623void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
624{
625 int ret;
626
627 if (sock_info->socket != -1) {
e6973a89 628 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 629 if (ret) {
32ce8569 630 ERR("Error closing ust cmd socket");
46050b1a
MD
631 }
632 sock_info->socket = -1;
633 }
32ce8569
MD
634 if (sock_info->notify_socket != -1) {
635 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
636 if (ret) {
637 ERR("Error closing ust notify socket");
638 }
639 sock_info->notify_socket = -1;
640 }
46050b1a 641 if (sock_info->root_handle != -1) {
1849ef7c 642 ret = lttng_ust_objd_unref(sock_info->root_handle, 1);
46050b1a
MD
643 if (ret) {
644 ERR("Error unref root handle");
645 }
646 sock_info->root_handle = -1;
647 }
318dfea9 648 sock_info->constructor_sem_posted = 0;
efe0de09
MD
649 /*
650 * wait_shm_mmap is used by listener threads outside of the
651 * ust lock, so we cannot tear it down ourselves, because we
652 * cannot join on these threads. Leave this task to the OS
653 * process exit.
654 */
655 if (!exiting && sock_info->wait_shm_mmap) {
7fc90dca
MD
656 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
657 if (ret) {
658 ERR("Error unmapping wait shm");
659 }
660 sock_info->wait_shm_mmap = NULL;
661 }
662}
663
58d4b2a2 664/*
33bbeb90
MD
665 * Using fork to set umask in the child process (not multi-thread safe).
666 * We deal with the shm_open vs ftruncate race (happening when the
667 * sessiond owns the shm and does not let everybody modify it, to ensure
668 * safety against shm_unlink) by simply letting the mmap fail and
669 * retrying after a few seconds.
670 * For global shm, everybody has rw access to it until the sessiond
671 * starts.
58d4b2a2 672 */
7fc90dca 673static
58d4b2a2 674int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 675{
7fc90dca 676 int wait_shm_fd, ret;
58d4b2a2 677 pid_t pid;
44e073f5 678
58d4b2a2 679 /*
33bbeb90 680 * Try to open read-only.
58d4b2a2 681 */
33bbeb90 682 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
683 if (wait_shm_fd >= 0) {
684 goto end;
685 } else if (wait_shm_fd < 0 && errno != ENOENT) {
686 /*
33bbeb90
MD
687 * Real-only open did not work, and it's not because the
688 * entry was not present. It's a failure that prohibits
689 * using shm.
58d4b2a2 690 */
7fc90dca 691 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 692 goto end;
7fc90dca
MD
693 }
694 /*
58d4b2a2
MD
695 * If the open failed because the file did not exist, try
696 * creating it ourself.
7fc90dca 697 */
8c90a710 698 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 699 pid = fork();
8c90a710 700 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
701 if (pid > 0) {
702 int status;
703
704 /*
705 * Parent: wait for child to return, in which case the
706 * shared memory map will have been created.
707 */
708 pid = wait(&status);
b7d3cb32 709 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
710 wait_shm_fd = -1;
711 goto end;
7fc90dca 712 }
58d4b2a2
MD
713 /*
714 * Try to open read-only again after creation.
715 */
33bbeb90 716 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
717 if (wait_shm_fd < 0) {
718 /*
719 * Real-only open did not work. It's a failure
720 * that prohibits using shm.
721 */
722 ERR("Error opening shm %s", sock_info->wait_shm_path);
723 goto end;
724 }
725 goto end;
726 } else if (pid == 0) {
727 int create_mode;
728
729 /* Child */
33bbeb90 730 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 731 if (sock_info->global)
33bbeb90 732 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
733 /*
734 * We're alone in a child process, so we can modify the
735 * process-wide umask.
736 */
33bbeb90 737 umask(~create_mode);
58d4b2a2 738 /*
33bbeb90
MD
739 * Try creating shm (or get rw access).
740 * We don't do an exclusive open, because we allow other
741 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
742 */
743 wait_shm_fd = shm_open(sock_info->wait_shm_path,
744 O_RDWR | O_CREAT, create_mode);
745 if (wait_shm_fd >= 0) {
746 ret = ftruncate(wait_shm_fd, mmap_size);
747 if (ret) {
748 PERROR("ftruncate");
b0c1425d 749 _exit(EXIT_FAILURE);
58d4b2a2 750 }
b0c1425d 751 _exit(EXIT_SUCCESS);
58d4b2a2 752 }
33bbeb90
MD
753 /*
754 * For local shm, we need to have rw access to accept
755 * opening it: this means the local sessiond will be
756 * able to wake us up. For global shm, we open it even
757 * if rw access is not granted, because the root.root
758 * sessiond will be able to override all rights and wake
759 * us up.
760 */
761 if (!sock_info->global && errno != EACCES) {
58d4b2a2 762 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 763 _exit(EXIT_FAILURE);
58d4b2a2
MD
764 }
765 /*
33bbeb90
MD
766 * The shm exists, but we cannot open it RW. Report
767 * success.
58d4b2a2 768 */
5d3bc5ed 769 _exit(EXIT_SUCCESS);
58d4b2a2
MD
770 } else {
771 return -1;
7fc90dca 772 }
58d4b2a2 773end:
33bbeb90
MD
774 if (wait_shm_fd >= 0 && !sock_info->global) {
775 struct stat statbuf;
776
777 /*
778 * Ensure that our user is the owner of the shm file for
779 * local shm. If we do not own the file, it means our
780 * sessiond will not have access to wake us up (there is
781 * probably a rogue process trying to fake our
782 * sessiond). Fallback to polling method in this case.
783 */
784 ret = fstat(wait_shm_fd, &statbuf);
785 if (ret) {
786 PERROR("fstat");
787 goto error_close;
788 }
789 if (statbuf.st_uid != getuid())
790 goto error_close;
791 }
58d4b2a2 792 return wait_shm_fd;
33bbeb90
MD
793
794error_close:
795 ret = close(wait_shm_fd);
796 if (ret) {
797 PERROR("Error closing fd");
798 }
799 return -1;
58d4b2a2
MD
800}
801
802static
803char *get_map_shm(struct sock_info *sock_info)
804{
805 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
806 int wait_shm_fd, ret;
807 char *wait_shm_mmap;
808
809 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
810 if (wait_shm_fd < 0) {
811 goto error;
44e073f5 812 }
7fc90dca
MD
813 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
814 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
815 /* close shm fd immediately after taking the mmap reference */
816 ret = close(wait_shm_fd);
817 if (ret) {
33bbeb90
MD
818 PERROR("Error closing fd");
819 }
820 if (wait_shm_mmap == MAP_FAILED) {
821 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
822 goto error;
7fc90dca
MD
823 }
824 return wait_shm_mmap;
825
826error:
827 return NULL;
828}
829
830static
831void wait_for_sessiond(struct sock_info *sock_info)
832{
efe0de09 833 int ret;
80e2814b 834
7fc90dca
MD
835 ust_lock();
836 if (lttng_ust_comm_should_quit) {
837 goto quit;
838 }
37ed587a
MD
839 if (wait_poll_fallback) {
840 goto error;
841 }
7fc90dca
MD
842 if (!sock_info->wait_shm_mmap) {
843 sock_info->wait_shm_mmap = get_map_shm(sock_info);
844 if (!sock_info->wait_shm_mmap)
845 goto error;
846 }
847 ust_unlock();
848
849 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b
MD
850 /* Wait for futex wakeup */
851 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
852 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
853 FUTEX_WAIT, 0, NULL, NULL, 0);
80e2814b 854 if (ret < 0) {
37ed587a
MD
855 if (errno == EFAULT) {
856 wait_poll_fallback = 1;
a8b870ad 857 DBG(
37ed587a
MD
858"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
859"do not support FUTEX_WAKE on read-only memory mappings correctly. "
860"Please upgrade your kernel "
861"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
862"mainline). LTTng-UST will use polling mode fallback.");
cd27263b
MD
863 if (ust_debug())
864 PERROR("futex");
37ed587a 865 }
80e2814b
MD
866 }
867 }
7fc90dca
MD
868 return;
869
870quit:
871 ust_unlock();
872 return;
873
874error:
875 ust_unlock();
7fc90dca 876 return;
46050b1a
MD
877}
878
1ea11eab
MD
879/*
880 * This thread does not allocate any resource, except within
881 * handle_message, within mutex protection. This mutex protects against
882 * fork and exit.
98bf993f 883 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
884 * is also protected by the mutex.
885 */
d9e99d10
MD
886static
887void *ust_listener_thread(void *arg)
888{
1ea11eab 889 struct sock_info *sock_info = arg;
c0eedf81 890 int sock, ret, prev_connect_failed = 0, has_waited = 0;
32ce8569
MD
891 int open_sock[2];
892 int i;
ff517991 893 long timeout;
d9e99d10 894
9eb62b9c
MD
895 /* Restart trying to connect to the session daemon */
896restart:
c0eedf81
MD
897 if (prev_connect_failed) {
898 /* Wait for sessiond availability with pipe */
899 wait_for_sessiond(sock_info);
900 if (has_waited) {
901 has_waited = 0;
902 /*
903 * Sleep for 5 seconds before retrying after a
904 * sequence of failure / wait / failure. This
905 * deals with a killed or broken session daemon.
906 */
907 sleep(5);
908 }
909 has_waited = 1;
910 prev_connect_failed = 0;
911 }
17dfb34b 912 ust_lock();
1ea11eab
MD
913
914 if (lttng_ust_comm_should_quit) {
1ea11eab
MD
915 goto quit;
916 }
9eb62b9c 917
1ea11eab 918 if (sock_info->socket != -1) {
e6973a89 919 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 920 if (ret) {
32ce8569
MD
921 ERR("Error closing %s ust cmd socket",
922 sock_info->name);
1ea11eab
MD
923 }
924 sock_info->socket = -1;
925 }
32ce8569
MD
926 if (sock_info->notify_socket != -1) {
927 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
928 if (ret) {
929 ERR("Error closing %s ust notify socket",
930 sock_info->name);
931 }
932 sock_info->notify_socket = -1;
933 }
46050b1a 934
9eb62b9c 935 /* Register */
32ce8569
MD
936 for (i = 0; i < 2; i++) {
937 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
938 if (ret < 0) {
939 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
940 prev_connect_failed = 1;
941 /*
942 * If we cannot find the sessiond daemon, don't delay
943 * constructor execution.
944 */
945 ret = handle_register_done(sock_info);
946 assert(!ret);
947 ust_unlock();
948 goto restart;
949 }
950 open_sock[i] = ret;
46050b1a
MD
951 }
952
32ce8569
MD
953 sock_info->socket = open_sock[0];
954 sock_info->notify_socket = open_sock[1];
46050b1a 955
27fe9f21 956 timeout = get_notify_sock_timeout();
e3426ddc
MD
957 if (timeout >= 0) {
958 /*
959 * Give at least 10ms to sessiond to reply to
960 * notifications.
961 */
962 if (timeout < 10)
963 timeout = 10;
27fe9f21
MD
964 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
965 timeout);
966 if (ret < 0) {
967 WARN("Error setting socket receive timeout");
968 }
969 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
970 timeout);
971 if (ret < 0) {
972 WARN("Error setting socket send timeout");
973 }
e3426ddc
MD
974 } else if (timeout < -1) {
975 WARN("Unsuppoorted timeout value %ld", timeout);
27fe9f21
MD
976 }
977
46050b1a
MD
978 /*
979 * Create only one root handle per listener thread for the whole
f59ed768
MD
980 * process lifetime, so we ensure we get ID which is statically
981 * assigned to the root handle.
46050b1a
MD
982 */
983 if (sock_info->root_handle == -1) {
984 ret = lttng_abi_create_root_handle();
a51070bb 985 if (ret < 0) {
46050b1a 986 ERR("Error creating root handle");
46050b1a
MD
987 goto quit;
988 }
989 sock_info->root_handle = ret;
9eb62b9c 990 }
1ea11eab 991
32ce8569 992 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
9eb62b9c 993 if (ret < 0) {
32ce8569
MD
994 ERR("Error registering to %s ust cmd socket",
995 sock_info->name);
c0eedf81 996 prev_connect_failed = 1;
11ff9c7d
MD
997 /*
998 * If we cannot register to the sessiond daemon, don't
999 * delay constructor execution.
1000 */
edaa1431 1001 ret = handle_register_done(sock_info);
11ff9c7d 1002 assert(!ret);
17dfb34b 1003 ust_unlock();
9eb62b9c
MD
1004 goto restart;
1005 }
32ce8569
MD
1006 ret = register_to_sessiond(sock_info->notify_socket,
1007 USTCTL_SOCKET_NOTIFY);
1008 if (ret < 0) {
1009 ERR("Error registering to %s ust notify socket",
1010 sock_info->name);
1011 prev_connect_failed = 1;
1012 /*
1013 * If we cannot register to the sessiond daemon, don't
1014 * delay constructor execution.
1015 */
1016 ret = handle_register_done(sock_info);
1017 assert(!ret);
1018 ust_unlock();
1019 goto restart;
1020 }
1021 sock = sock_info->socket;
1022
17dfb34b 1023 ust_unlock();
46050b1a 1024
d9e99d10
MD
1025 for (;;) {
1026 ssize_t len;
57773204 1027 struct ustcomm_ust_msg lum;
d9e99d10 1028
57773204 1029 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
1030 switch (len) {
1031 case 0: /* orderly shutdown */
7dd08bec 1032 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
8236ba10 1033 ust_lock();
d5e1fea6 1034 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
1035 goto quit;
1036 }
8236ba10
MD
1037 /*
1038 * Either sessiond has shutdown or refused us by closing the socket.
1039 * In either case, we don't want to delay construction execution,
1040 * and we need to wait before retry.
1041 */
1042 prev_connect_failed = 1;
1043 /*
1044 * If we cannot register to the sessiond daemon, don't
1045 * delay constructor execution.
1046 */
1047 ret = handle_register_done(sock_info);
1048 assert(!ret);
1049 ust_unlock();
d9e99d10 1050 goto end;
e7723462 1051 case sizeof(lum):
74d81a6c 1052 print_cmd(lum.cmd, lum.handle);
11ff9c7d 1053 ret = handle_message(sock_info, sock, &lum);
7bc53e94 1054 if (ret) {
11ff9c7d 1055 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
1056 }
1057 continue;
7bc53e94
MD
1058 default:
1059 if (len < 0) {
1060 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1061 } else {
1062 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1063 }
1064 if (len == -ECONNRESET) {
1065 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
1066 goto end;
1067 }
1068 goto end;
d9e99d10
MD
1069 }
1070
1071 }
1072end:
f59ed768 1073 ust_lock();
d5e1fea6 1074 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
1075 goto quit;
1076 }
f59ed768
MD
1077 /* Cleanup socket handles before trying to reconnect */
1078 lttng_ust_objd_table_owner_cleanup(sock_info);
1079 ust_unlock();
9eb62b9c 1080 goto restart; /* try to reconnect */
e33f3265 1081
1ea11eab 1082quit:
e33f3265
MD
1083 sock_info->thread_active = 0;
1084 ust_unlock();
d9e99d10
MD
1085 return NULL;
1086}
1087
2691221a
MD
1088/*
1089 * sessiond monitoring thread: monitor presence of global and per-user
1090 * sessiond by polling the application common named pipe.
1091 */
edaa1431 1092void __attribute__((constructor)) lttng_ust_init(void)
2691221a 1093{
11ff9c7d 1094 struct timespec constructor_timeout;
ae6a58bf 1095 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 1096 pthread_attr_t thread_attr;
cf12a773 1097 int timeout_mode;
2691221a
MD
1098 int ret;
1099
edaa1431
MD
1100 if (uatomic_xchg(&initialized, 1) == 1)
1101 return;
1102
eddd8d5d
MD
1103 /*
1104 * Fixup interdependency between TLS fixup mutex (which happens
1105 * to be the dynamic linker mutex) and ust_lock, taken within
1106 * the ust lock.
1107 */
f645cfa7 1108 lttng_fixup_ringbuffer_tls();
4158a15a 1109 lttng_fixup_vtid_tls();
a903623f 1110 lttng_fixup_nest_count_tls();
009745db 1111 lttng_fixup_procname_tls();
eddd8d5d 1112
edaa1431
MD
1113 /*
1114 * We want precise control over the order in which we construct
1115 * our sub-libraries vs starting to receive commands from
1116 * sessiond (otherwise leading to errors when trying to create
1117 * sessiond before the init functions are completed).
1118 */
2691221a 1119 init_usterr();
edaa1431 1120 init_tracepoint();
7dd08bec
MD
1121 lttng_ring_buffer_metadata_client_init();
1122 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1123 lttng_ring_buffer_client_overwrite_rt_init();
7dd08bec 1124 lttng_ring_buffer_client_discard_init();
34a91bdb 1125 lttng_ring_buffer_client_discard_rt_init();
a0a3bef9 1126 lttng_context_init();
2691221a 1127
ff517991 1128 timeout_mode = get_constructor_timeout(&constructor_timeout);
11ff9c7d 1129
95259bd0 1130 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
1131 assert(!ret);
1132
8d20bf54 1133 ret = setup_local_apps();
2691221a 1134 if (ret) {
9ec6895c 1135 DBG("local apps setup returned %d", ret);
2691221a 1136 }
ae6a58bf
WP
1137
1138 /* A new thread created by pthread_create inherits the signal mask
1139 * from the parent. To avoid any signal being received by the
1140 * listener thread, we block all signals temporarily in the parent,
1141 * while we create the listener thread.
1142 */
1143 sigfillset(&sig_all_blocked);
1144 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1145 if (ret) {
d94d802c 1146 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1147 }
1148
1879f67f
MG
1149 ret = pthread_attr_init(&thread_attr);
1150 if (ret) {
1151 ERR("pthread_attr_init: %s", strerror(ret));
1152 }
1153 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1154 if (ret) {
1155 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1156 }
1157
e33f3265 1158 ust_lock();
1879f67f 1159 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
dde70ea0 1160 ust_listener_thread, &global_apps);
d94d802c
MD
1161 if (ret) {
1162 ERR("pthread_create global: %s", strerror(ret));
1163 }
e33f3265
MD
1164 global_apps.thread_active = 1;
1165 ust_unlock();
1166
8d20bf54 1167 if (local_apps.allowed) {
e33f3265 1168 ust_lock();
1879f67f 1169 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 1170 ust_listener_thread, &local_apps);
d94d802c
MD
1171 if (ret) {
1172 ERR("pthread_create local: %s", strerror(ret));
1173 }
e33f3265
MD
1174 local_apps.thread_active = 1;
1175 ust_unlock();
8d20bf54
MD
1176 } else {
1177 handle_register_done(&local_apps);
1178 }
1879f67f
MG
1179 ret = pthread_attr_destroy(&thread_attr);
1180 if (ret) {
1181 ERR("pthread_attr_destroy: %s", strerror(ret));
1182 }
8d20bf54 1183
ae6a58bf
WP
1184 /* Restore original signal mask in parent */
1185 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1186 if (ret) {
d94d802c 1187 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1188 }
1189
cf12a773
MD
1190 switch (timeout_mode) {
1191 case 1: /* timeout wait */
95259bd0
MD
1192 do {
1193 ret = sem_timedwait(&constructor_wait,
1194 &constructor_timeout);
1195 } while (ret < 0 && errno == EINTR);
cf12a773 1196 if (ret < 0 && errno == ETIMEDOUT) {
7dd08bec 1197 ERR("Timed out waiting for lttng-sessiond");
cf12a773
MD
1198 } else {
1199 assert(!ret);
1200 }
1201 break;
7b766b16 1202 case -1:/* wait forever */
95259bd0
MD
1203 do {
1204 ret = sem_wait(&constructor_wait);
1205 } while (ret < 0 && errno == EINTR);
11ff9c7d 1206 assert(!ret);
cf12a773 1207 break;
7b766b16 1208 case 0: /* no timeout */
cf12a773 1209 break;
11ff9c7d 1210 }
2691221a
MD
1211}
1212
17dfb34b
MD
1213static
1214void lttng_ust_cleanup(int exiting)
1215{
efe0de09 1216 cleanup_sock_info(&global_apps, exiting);
17dfb34b 1217 if (local_apps.allowed) {
efe0de09 1218 cleanup_sock_info(&local_apps, exiting);
17dfb34b 1219 }
efe0de09
MD
1220 /*
1221 * The teardown in this function all affect data structures
1222 * accessed under the UST lock by the listener thread. This
1223 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1224 * that none of these threads are accessing this data at this
1225 * point.
1226 */
17dfb34b 1227 lttng_ust_abi_exit();
003fedf4 1228 lttng_ust_events_exit();
a0a3bef9 1229 lttng_context_exit();
34a91bdb 1230 lttng_ring_buffer_client_discard_rt_exit();
7dd08bec 1231 lttng_ring_buffer_client_discard_exit();
34a91bdb 1232 lttng_ring_buffer_client_overwrite_rt_exit();
7dd08bec
MD
1233 lttng_ring_buffer_client_overwrite_exit();
1234 lttng_ring_buffer_metadata_client_exit();
17dfb34b
MD
1235 exit_tracepoint();
1236 if (!exiting) {
1237 /* Reinitialize values for fork */
1238 sem_count = 2;
1239 lttng_ust_comm_should_quit = 0;
1240 initialized = 0;
1241 }
1242}
1243
edaa1431 1244void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1245{
1246 int ret;
1247
9eb62b9c
MD
1248 /*
1249 * Using pthread_cancel here because:
1250 * A) we don't want to hang application teardown.
1251 * B) the thread is not allocating any resource.
1252 */
1ea11eab
MD
1253
1254 /*
1255 * Require the communication thread to quit. Synchronize with
1256 * mutexes to ensure it is not in a mutex critical section when
1257 * pthread_cancel is later called.
1258 */
17dfb34b 1259 ust_lock();
1ea11eab 1260 lttng_ust_comm_should_quit = 1;
1ea11eab 1261
f5f94532 1262 /* cancel threads */
e33f3265
MD
1263 if (global_apps.thread_active) {
1264 ret = pthread_cancel(global_apps.ust_listener);
1265 if (ret) {
1266 ERR("Error cancelling global ust listener thread: %s",
1267 strerror(ret));
1268 } else {
1269 global_apps.thread_active = 0;
1270 }
2691221a 1271 }
e33f3265 1272 if (local_apps.thread_active) {
8d20bf54
MD
1273 ret = pthread_cancel(local_apps.ust_listener);
1274 if (ret) {
d94d802c
MD
1275 ERR("Error cancelling local ust listener thread: %s",
1276 strerror(ret));
e33f3265
MD
1277 } else {
1278 local_apps.thread_active = 0;
8d20bf54 1279 }
8d20bf54 1280 }
e33f3265
MD
1281 ust_unlock();
1282
efe0de09
MD
1283 /*
1284 * Do NOT join threads: use of sys_futex makes it impossible to
1285 * join the threads without using async-cancel, but async-cancel
1286 * is delivered by a signal, which could hit the target thread
1287 * anywhere in its code path, including while the ust_lock() is
1288 * held, causing a deadlock for the other thread. Let the OS
1289 * cleanup the threads if there are stalled in a syscall.
1290 */
17dfb34b 1291 lttng_ust_cleanup(1);
2691221a 1292}
e822f505
MD
1293
1294/*
1295 * We exclude the worker threads across fork and clone (except
1296 * CLONE_VM), because these system calls only keep the forking thread
1297 * running in the child. Therefore, we don't want to call fork or clone
1298 * in the middle of an tracepoint or ust tracing state modification.
1299 * Holding this mutex protects these structures across fork and clone.
1300 */
b728d87e 1301void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
1302{
1303 /*
1304 * Disable signals. This is to avoid that the child intervenes
1305 * before it is properly setup for tracing. It is safer to
1306 * disable all signals, because then we know we are not breaking
1307 * anything by restoring the original mask.
1308 */
1309 sigset_t all_sigs;
1310 int ret;
1311
8c90a710 1312 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1313 return;
e822f505
MD
1314 /* Disable signals */
1315 sigfillset(&all_sigs);
b728d87e 1316 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
1317 if (ret == -1) {
1318 PERROR("sigprocmask");
1319 }
17dfb34b 1320 ust_lock();
e822f505
MD
1321 rcu_bp_before_fork();
1322}
1323
b728d87e 1324static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
1325{
1326 int ret;
1327
17dfb34b
MD
1328 DBG("process %d", getpid());
1329 ust_unlock();
e822f505 1330 /* Restore signals */
23c8854a 1331 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
1332 if (ret == -1) {
1333 PERROR("sigprocmask");
1334 }
1335}
1336
b728d87e 1337void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 1338{
8c90a710 1339 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1340 return;
17dfb34b 1341 DBG("process %d", getpid());
e822f505
MD
1342 rcu_bp_after_fork_parent();
1343 /* Release mutexes and reenable signals */
b728d87e 1344 ust_after_fork_common(restore_sigset);
e822f505
MD
1345}
1346
17dfb34b
MD
1347/*
1348 * After fork, in the child, we need to cleanup all the leftover state,
1349 * except the worker thread which already magically disappeared thanks
1350 * to the weird Linux fork semantics. After tyding up, we call
1351 * lttng_ust_init() again to start over as a new PID.
1352 *
1353 * This is meant for forks() that have tracing in the child between the
1354 * fork and following exec call (if there is any).
1355 */
b728d87e 1356void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 1357{
8c90a710 1358 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1359 return;
17dfb34b 1360 DBG("process %d", getpid());
e822f505
MD
1361 /* Release urcu mutexes */
1362 rcu_bp_after_fork_child();
17dfb34b 1363 lttng_ust_cleanup(0);
a93bfc45 1364 lttng_context_vtid_reset();
e822f505 1365 /* Release mutexes and reenable signals */
b728d87e 1366 ust_after_fork_common(restore_sigset);
318dfea9 1367 lttng_ust_init();
e822f505 1368}
This page took 0.102932 seconds and 4 git commands to generate.