Fix: liblttng-ust process startup hang when sessiond is stopped
[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;
eb8bf361 446 free(bytecode);
7bc53e94 447 goto end;
2d78951a 448 } else {
7bc53e94 449 DBG("incorrect filter data message size: %zd", len);
2d78951a 450 ret = -EINVAL;
cd54f6d9 451 free(bytecode);
2d78951a
MD
452 goto end;
453 }
454 }
f488575f
MD
455 bytecode->bc.len = lum->u.filter.data_size;
456 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
3f6fd224 457 bytecode->bc.seqnum = lum->u.filter.seqnum;
cd54f6d9 458 if (ops->cmd) {
2d78951a 459 ret = ops->cmd(lum->handle, lum->cmd,
cd54f6d9 460 (unsigned long) bytecode,
f59ed768 461 &args, sock_info);
cd54f6d9
MD
462 if (ret) {
463 free(bytecode);
464 }
465 /* don't free bytecode if everything went fine. */
466 } else {
2d78951a 467 ret = -ENOSYS;
cd54f6d9
MD
468 free(bytecode);
469 }
2d78951a
MD
470 break;
471 }
74d81a6c
MD
472 case LTTNG_UST_CHANNEL:
473 {
474 void *chan_data;
ff0f5728 475 int wakeup_fd;
74d81a6c
MD
476
477 len = ustcomm_recv_channel_from_sessiond(sock,
ff0f5728
MD
478 &chan_data, lum->u.channel.len,
479 &wakeup_fd);
74d81a6c
MD
480 switch (len) {
481 case 0: /* orderly shutdown */
482 ret = 0;
483 goto error;
484 default:
485 if (len == lum->u.channel.len) {
486 DBG("channel data received");
487 break;
488 } else if (len < 0) {
489 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
490 if (len == -ECONNRESET) {
491 ERR("%s remote end closed connection", sock_info->name);
492 ret = len;
493 goto error;
494 }
495 ret = len;
496 goto end;
497 } else {
498 DBG("incorrect channel data message size: %zd", len);
499 ret = -EINVAL;
500 goto end;
501 }
502 }
503 args.channel.chan_data = chan_data;
ff0f5728 504 args.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
505 if (ops->cmd)
506 ret = ops->cmd(lum->handle, lum->cmd,
507 (unsigned long) &lum->u,
508 &args, sock_info);
509 else
510 ret = -ENOSYS;
511 break;
512 }
513 case LTTNG_UST_STREAM:
514 {
515 /* Receive shm_fd, wakeup_fd */
516 ret = ustcomm_recv_stream_from_sessiond(sock,
517 &lum->u.stream.len,
518 &args.stream.shm_fd,
519 &args.stream.wakeup_fd);
520 if (ret) {
521 goto end;
522 }
523 if (ops->cmd)
524 ret = ops->cmd(lum->handle, lum->cmd,
525 (unsigned long) &lum->u,
526 &args, sock_info);
527 else
528 ret = -ENOSYS;
529 break;
530 }
d9e99d10 531 default:
46050b1a
MD
532 if (ops->cmd)
533 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 534 (unsigned long) &lum->u,
f59ed768 535 &args, sock_info);
46050b1a
MD
536 else
537 ret = -ENOSYS;
538 break;
d9e99d10 539 }
46050b1a 540
1ea11eab 541end:
46050b1a
MD
542 lur.handle = lum->handle;
543 lur.cmd = lum->cmd;
544 lur.ret_val = ret;
545 if (ret >= 0) {
7bc53e94 546 lur.ret_code = LTTNG_UST_OK;
46050b1a 547 } else {
7bc53e94
MD
548 /*
549 * Use -LTTNG_UST_ERR as wildcard for UST internal
550 * error that are not caused by the transport, except if
551 * we already have a more precise error message to
552 * report.
553 */
64b2564e
DG
554 if (ret > -LTTNG_UST_ERR) {
555 /* Translate code to UST error. */
556 switch (ret) {
557 case -EEXIST:
558 lur.ret_code = -LTTNG_UST_ERR_EXIST;
559 break;
560 case -EINVAL:
561 lur.ret_code = -LTTNG_UST_ERR_INVAL;
562 break;
563 case -ENOENT:
564 lur.ret_code = -LTTNG_UST_ERR_NOENT;
565 break;
566 case -EPERM:
567 lur.ret_code = -LTTNG_UST_ERR_PERM;
568 break;
569 case -ENOSYS:
570 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
571 break;
572 default:
573 lur.ret_code = -LTTNG_UST_ERR;
574 break;
575 }
576 } else {
7bc53e94 577 lur.ret_code = ret;
64b2564e 578 }
46050b1a 579 }
e6ea14c5
MD
580 if (ret >= 0) {
581 switch (lum->cmd) {
e6ea14c5
MD
582 case LTTNG_UST_TRACER_VERSION:
583 lur.u.version = lum->u.version;
584 break;
585 case LTTNG_UST_TRACEPOINT_LIST_GET:
586 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
587 break;
588 }
381c0f1e 589 }
74d81a6c 590 DBG("Return value: %d", lur.ret_val);
46050b1a 591 ret = send_reply(sock, &lur);
193183fb 592 if (ret < 0) {
7bc53e94 593 DBG("error sending reply");
193183fb
MD
594 goto error;
595 }
46050b1a 596
40003310
MD
597 /*
598 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
599 * after the reply.
600 */
7bc53e94 601 if (lur.ret_code == LTTNG_UST_OK) {
40003310
MD
602 switch (lum->cmd) {
603 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
604 len = ustcomm_send_unix_sock(sock,
605 &args.field_list.entry,
606 sizeof(args.field_list.entry));
7bc53e94
MD
607 if (len < 0) {
608 ret = len;
609 goto error;
610 }
40003310 611 if (len != sizeof(args.field_list.entry)) {
7bc53e94 612 ret = -EINVAL;
40003310
MD
613 goto error;
614 }
615 }
616 }
ef9ff354 617
381c0f1e 618error:
17dfb34b 619 ust_unlock();
1ea11eab 620 return ret;
d9e99d10
MD
621}
622
46050b1a 623static
efe0de09 624void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
625{
626 int ret;
627
844a1607
MD
628 if (sock_info->root_handle != -1) {
629 ret = lttng_ust_objd_unref(sock_info->root_handle, 1);
630 if (ret) {
631 ERR("Error unref root handle");
632 }
633 sock_info->root_handle = -1;
634 }
635 sock_info->constructor_sem_posted = 0;
636
637 /*
638 * wait_shm_mmap, socket and notify socket are used by listener
639 * threads outside of the ust lock, so we cannot tear them down
640 * ourselves, because we cannot join on these threads. Leave
641 * responsibility of cleaning up these resources to the OS
642 * process exit.
643 */
644 if (exiting)
645 return;
646
46050b1a 647 if (sock_info->socket != -1) {
e6973a89 648 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 649 if (ret) {
32ce8569 650 ERR("Error closing ust cmd socket");
46050b1a
MD
651 }
652 sock_info->socket = -1;
653 }
32ce8569
MD
654 if (sock_info->notify_socket != -1) {
655 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
656 if (ret) {
657 ERR("Error closing ust notify socket");
658 }
659 sock_info->notify_socket = -1;
660 }
844a1607 661 if (sock_info->wait_shm_mmap) {
7fc90dca
MD
662 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
663 if (ret) {
664 ERR("Error unmapping wait shm");
665 }
666 sock_info->wait_shm_mmap = NULL;
667 }
668}
669
58d4b2a2 670/*
33bbeb90
MD
671 * Using fork to set umask in the child process (not multi-thread safe).
672 * We deal with the shm_open vs ftruncate race (happening when the
673 * sessiond owns the shm and does not let everybody modify it, to ensure
674 * safety against shm_unlink) by simply letting the mmap fail and
675 * retrying after a few seconds.
676 * For global shm, everybody has rw access to it until the sessiond
677 * starts.
58d4b2a2 678 */
7fc90dca 679static
58d4b2a2 680int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 681{
7fc90dca 682 int wait_shm_fd, ret;
58d4b2a2 683 pid_t pid;
44e073f5 684
58d4b2a2 685 /*
33bbeb90 686 * Try to open read-only.
58d4b2a2 687 */
33bbeb90 688 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
689 if (wait_shm_fd >= 0) {
690 goto end;
691 } else if (wait_shm_fd < 0 && errno != ENOENT) {
692 /*
33bbeb90
MD
693 * Real-only open did not work, and it's not because the
694 * entry was not present. It's a failure that prohibits
695 * using shm.
58d4b2a2 696 */
7fc90dca 697 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 698 goto end;
7fc90dca
MD
699 }
700 /*
58d4b2a2
MD
701 * If the open failed because the file did not exist, try
702 * creating it ourself.
7fc90dca 703 */
8c90a710 704 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 705 pid = fork();
8c90a710 706 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
707 if (pid > 0) {
708 int status;
709
710 /*
711 * Parent: wait for child to return, in which case the
712 * shared memory map will have been created.
713 */
714 pid = wait(&status);
b7d3cb32 715 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
716 wait_shm_fd = -1;
717 goto end;
7fc90dca 718 }
58d4b2a2
MD
719 /*
720 * Try to open read-only again after creation.
721 */
33bbeb90 722 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
723 if (wait_shm_fd < 0) {
724 /*
725 * Real-only open did not work. It's a failure
726 * that prohibits using shm.
727 */
728 ERR("Error opening shm %s", sock_info->wait_shm_path);
729 goto end;
730 }
731 goto end;
732 } else if (pid == 0) {
733 int create_mode;
734
735 /* Child */
33bbeb90 736 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 737 if (sock_info->global)
33bbeb90 738 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
739 /*
740 * We're alone in a child process, so we can modify the
741 * process-wide umask.
742 */
33bbeb90 743 umask(~create_mode);
58d4b2a2 744 /*
33bbeb90
MD
745 * Try creating shm (or get rw access).
746 * We don't do an exclusive open, because we allow other
747 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
748 */
749 wait_shm_fd = shm_open(sock_info->wait_shm_path,
750 O_RDWR | O_CREAT, create_mode);
751 if (wait_shm_fd >= 0) {
752 ret = ftruncate(wait_shm_fd, mmap_size);
753 if (ret) {
754 PERROR("ftruncate");
b0c1425d 755 _exit(EXIT_FAILURE);
58d4b2a2 756 }
b0c1425d 757 _exit(EXIT_SUCCESS);
58d4b2a2 758 }
33bbeb90
MD
759 /*
760 * For local shm, we need to have rw access to accept
761 * opening it: this means the local sessiond will be
762 * able to wake us up. For global shm, we open it even
763 * if rw access is not granted, because the root.root
764 * sessiond will be able to override all rights and wake
765 * us up.
766 */
767 if (!sock_info->global && errno != EACCES) {
58d4b2a2 768 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 769 _exit(EXIT_FAILURE);
58d4b2a2
MD
770 }
771 /*
33bbeb90
MD
772 * The shm exists, but we cannot open it RW. Report
773 * success.
58d4b2a2 774 */
5d3bc5ed 775 _exit(EXIT_SUCCESS);
58d4b2a2
MD
776 } else {
777 return -1;
7fc90dca 778 }
58d4b2a2 779end:
33bbeb90
MD
780 if (wait_shm_fd >= 0 && !sock_info->global) {
781 struct stat statbuf;
782
783 /*
784 * Ensure that our user is the owner of the shm file for
785 * local shm. If we do not own the file, it means our
786 * sessiond will not have access to wake us up (there is
787 * probably a rogue process trying to fake our
788 * sessiond). Fallback to polling method in this case.
789 */
790 ret = fstat(wait_shm_fd, &statbuf);
791 if (ret) {
792 PERROR("fstat");
793 goto error_close;
794 }
795 if (statbuf.st_uid != getuid())
796 goto error_close;
797 }
58d4b2a2 798 return wait_shm_fd;
33bbeb90
MD
799
800error_close:
801 ret = close(wait_shm_fd);
802 if (ret) {
803 PERROR("Error closing fd");
804 }
805 return -1;
58d4b2a2
MD
806}
807
808static
809char *get_map_shm(struct sock_info *sock_info)
810{
811 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
812 int wait_shm_fd, ret;
813 char *wait_shm_mmap;
814
815 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
816 if (wait_shm_fd < 0) {
817 goto error;
44e073f5 818 }
7fc90dca
MD
819 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
820 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
821 /* close shm fd immediately after taking the mmap reference */
822 ret = close(wait_shm_fd);
823 if (ret) {
33bbeb90
MD
824 PERROR("Error closing fd");
825 }
826 if (wait_shm_mmap == MAP_FAILED) {
827 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
828 goto error;
7fc90dca
MD
829 }
830 return wait_shm_mmap;
831
832error:
833 return NULL;
834}
835
836static
837void wait_for_sessiond(struct sock_info *sock_info)
838{
efe0de09 839 int ret;
80e2814b 840
7fc90dca
MD
841 ust_lock();
842 if (lttng_ust_comm_should_quit) {
843 goto quit;
844 }
37ed587a
MD
845 if (wait_poll_fallback) {
846 goto error;
847 }
7fc90dca
MD
848 if (!sock_info->wait_shm_mmap) {
849 sock_info->wait_shm_mmap = get_map_shm(sock_info);
850 if (!sock_info->wait_shm_mmap)
851 goto error;
852 }
853 ust_unlock();
854
855 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b
MD
856 /* Wait for futex wakeup */
857 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
858 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
859 FUTEX_WAIT, 0, NULL, NULL, 0);
80e2814b 860 if (ret < 0) {
37ed587a
MD
861 if (errno == EFAULT) {
862 wait_poll_fallback = 1;
a8b870ad 863 DBG(
37ed587a
MD
864"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
865"do not support FUTEX_WAKE on read-only memory mappings correctly. "
866"Please upgrade your kernel "
867"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
868"mainline). LTTng-UST will use polling mode fallback.");
cd27263b
MD
869 if (ust_debug())
870 PERROR("futex");
37ed587a 871 }
80e2814b
MD
872 }
873 }
7fc90dca
MD
874 return;
875
876quit:
877 ust_unlock();
878 return;
879
880error:
881 ust_unlock();
7fc90dca 882 return;
46050b1a
MD
883}
884
1ea11eab
MD
885/*
886 * This thread does not allocate any resource, except within
887 * handle_message, within mutex protection. This mutex protects against
888 * fork and exit.
98bf993f 889 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
890 * is also protected by the mutex.
891 */
d9e99d10
MD
892static
893void *ust_listener_thread(void *arg)
894{
1ea11eab 895 struct sock_info *sock_info = arg;
c0eedf81 896 int sock, ret, prev_connect_failed = 0, has_waited = 0;
32ce8569
MD
897 int open_sock[2];
898 int i;
ff517991 899 long timeout;
d9e99d10 900
9eb62b9c
MD
901 /* Restart trying to connect to the session daemon */
902restart:
c0eedf81
MD
903 if (prev_connect_failed) {
904 /* Wait for sessiond availability with pipe */
905 wait_for_sessiond(sock_info);
906 if (has_waited) {
907 has_waited = 0;
908 /*
909 * Sleep for 5 seconds before retrying after a
910 * sequence of failure / wait / failure. This
911 * deals with a killed or broken session daemon.
912 */
913 sleep(5);
914 }
915 has_waited = 1;
916 prev_connect_failed = 0;
917 }
9eb62b9c 918
1ea11eab 919 if (sock_info->socket != -1) {
e6973a89 920 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 921 if (ret) {
32ce8569
MD
922 ERR("Error closing %s ust cmd socket",
923 sock_info->name);
1ea11eab
MD
924 }
925 sock_info->socket = -1;
926 }
32ce8569
MD
927 if (sock_info->notify_socket != -1) {
928 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
929 if (ret) {
930 ERR("Error closing %s ust notify socket",
931 sock_info->name);
932 }
933 sock_info->notify_socket = -1;
934 }
46050b1a 935
9eb62b9c 936 /* Register */
32ce8569
MD
937 for (i = 0; i < 2; i++) {
938 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
939 if (ret < 0) {
940 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
941 prev_connect_failed = 1;
844a1607
MD
942
943 ust_lock();
944
945 if (lttng_ust_comm_should_quit) {
946 goto quit;
947 }
948
32ce8569
MD
949 /*
950 * If we cannot find the sessiond daemon, don't delay
951 * constructor execution.
952 */
953 ret = handle_register_done(sock_info);
954 assert(!ret);
955 ust_unlock();
956 goto restart;
957 }
958 open_sock[i] = ret;
46050b1a
MD
959 }
960
32ce8569
MD
961 sock_info->socket = open_sock[0];
962 sock_info->notify_socket = open_sock[1];
46050b1a 963
27fe9f21 964 timeout = get_notify_sock_timeout();
e3426ddc
MD
965 if (timeout >= 0) {
966 /*
967 * Give at least 10ms to sessiond to reply to
968 * notifications.
969 */
970 if (timeout < 10)
971 timeout = 10;
27fe9f21
MD
972 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
973 timeout);
974 if (ret < 0) {
975 WARN("Error setting socket receive timeout");
976 }
977 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
978 timeout);
979 if (ret < 0) {
980 WARN("Error setting socket send timeout");
981 }
e3426ddc
MD
982 } else if (timeout < -1) {
983 WARN("Unsuppoorted timeout value %ld", timeout);
27fe9f21
MD
984 }
985
844a1607
MD
986 ust_lock();
987
988 if (lttng_ust_comm_should_quit) {
989 goto quit;
990 }
991
46050b1a
MD
992 /*
993 * Create only one root handle per listener thread for the whole
f59ed768
MD
994 * process lifetime, so we ensure we get ID which is statically
995 * assigned to the root handle.
46050b1a
MD
996 */
997 if (sock_info->root_handle == -1) {
998 ret = lttng_abi_create_root_handle();
a51070bb 999 if (ret < 0) {
46050b1a 1000 ERR("Error creating root handle");
46050b1a
MD
1001 goto quit;
1002 }
1003 sock_info->root_handle = ret;
9eb62b9c 1004 }
1ea11eab 1005
32ce8569 1006 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
9eb62b9c 1007 if (ret < 0) {
32ce8569
MD
1008 ERR("Error registering to %s ust cmd socket",
1009 sock_info->name);
c0eedf81 1010 prev_connect_failed = 1;
11ff9c7d
MD
1011 /*
1012 * If we cannot register to the sessiond daemon, don't
1013 * delay constructor execution.
1014 */
edaa1431 1015 ret = handle_register_done(sock_info);
11ff9c7d 1016 assert(!ret);
17dfb34b 1017 ust_unlock();
9eb62b9c
MD
1018 goto restart;
1019 }
32ce8569
MD
1020 ret = register_to_sessiond(sock_info->notify_socket,
1021 USTCTL_SOCKET_NOTIFY);
1022 if (ret < 0) {
1023 ERR("Error registering to %s ust notify socket",
1024 sock_info->name);
1025 prev_connect_failed = 1;
1026 /*
1027 * If we cannot register to the sessiond daemon, don't
1028 * delay constructor execution.
1029 */
1030 ret = handle_register_done(sock_info);
1031 assert(!ret);
1032 ust_unlock();
1033 goto restart;
1034 }
1035 sock = sock_info->socket;
1036
17dfb34b 1037 ust_unlock();
46050b1a 1038
d9e99d10
MD
1039 for (;;) {
1040 ssize_t len;
57773204 1041 struct ustcomm_ust_msg lum;
d9e99d10 1042
57773204 1043 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
1044 switch (len) {
1045 case 0: /* orderly shutdown */
7dd08bec 1046 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
8236ba10 1047 ust_lock();
d5e1fea6 1048 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
1049 goto quit;
1050 }
8236ba10
MD
1051 /*
1052 * Either sessiond has shutdown or refused us by closing the socket.
1053 * In either case, we don't want to delay construction execution,
1054 * and we need to wait before retry.
1055 */
1056 prev_connect_failed = 1;
1057 /*
1058 * If we cannot register to the sessiond daemon, don't
1059 * delay constructor execution.
1060 */
1061 ret = handle_register_done(sock_info);
1062 assert(!ret);
1063 ust_unlock();
d9e99d10 1064 goto end;
e7723462 1065 case sizeof(lum):
74d81a6c 1066 print_cmd(lum.cmd, lum.handle);
11ff9c7d 1067 ret = handle_message(sock_info, sock, &lum);
7bc53e94 1068 if (ret) {
11ff9c7d 1069 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
1070 }
1071 continue;
7bc53e94
MD
1072 default:
1073 if (len < 0) {
1074 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1075 } else {
1076 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1077 }
1078 if (len == -ECONNRESET) {
1079 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
1080 goto end;
1081 }
1082 goto end;
d9e99d10
MD
1083 }
1084
1085 }
1086end:
f59ed768 1087 ust_lock();
d5e1fea6 1088 if (lttng_ust_comm_should_quit) {
d5e1fea6
MD
1089 goto quit;
1090 }
f59ed768
MD
1091 /* Cleanup socket handles before trying to reconnect */
1092 lttng_ust_objd_table_owner_cleanup(sock_info);
1093 ust_unlock();
9eb62b9c 1094 goto restart; /* try to reconnect */
e33f3265 1095
1ea11eab 1096quit:
e33f3265
MD
1097 sock_info->thread_active = 0;
1098 ust_unlock();
d9e99d10
MD
1099 return NULL;
1100}
1101
2691221a
MD
1102/*
1103 * sessiond monitoring thread: monitor presence of global and per-user
1104 * sessiond by polling the application common named pipe.
1105 */
edaa1431 1106void __attribute__((constructor)) lttng_ust_init(void)
2691221a 1107{
11ff9c7d 1108 struct timespec constructor_timeout;
ae6a58bf 1109 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 1110 pthread_attr_t thread_attr;
cf12a773 1111 int timeout_mode;
2691221a
MD
1112 int ret;
1113
edaa1431
MD
1114 if (uatomic_xchg(&initialized, 1) == 1)
1115 return;
1116
eddd8d5d
MD
1117 /*
1118 * Fixup interdependency between TLS fixup mutex (which happens
1119 * to be the dynamic linker mutex) and ust_lock, taken within
1120 * the ust lock.
1121 */
f645cfa7 1122 lttng_fixup_ringbuffer_tls();
4158a15a 1123 lttng_fixup_vtid_tls();
a903623f 1124 lttng_fixup_nest_count_tls();
009745db 1125 lttng_fixup_procname_tls();
eddd8d5d 1126
edaa1431
MD
1127 /*
1128 * We want precise control over the order in which we construct
1129 * our sub-libraries vs starting to receive commands from
1130 * sessiond (otherwise leading to errors when trying to create
1131 * sessiond before the init functions are completed).
1132 */
2691221a 1133 init_usterr();
edaa1431 1134 init_tracepoint();
7dd08bec
MD
1135 lttng_ring_buffer_metadata_client_init();
1136 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1137 lttng_ring_buffer_client_overwrite_rt_init();
7dd08bec 1138 lttng_ring_buffer_client_discard_init();
34a91bdb 1139 lttng_ring_buffer_client_discard_rt_init();
a0a3bef9 1140 lttng_context_init();
2691221a 1141
ff517991 1142 timeout_mode = get_constructor_timeout(&constructor_timeout);
11ff9c7d 1143
95259bd0 1144 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
1145 assert(!ret);
1146
8d20bf54 1147 ret = setup_local_apps();
2691221a 1148 if (ret) {
9ec6895c 1149 DBG("local apps setup returned %d", ret);
2691221a 1150 }
ae6a58bf
WP
1151
1152 /* A new thread created by pthread_create inherits the signal mask
1153 * from the parent. To avoid any signal being received by the
1154 * listener thread, we block all signals temporarily in the parent,
1155 * while we create the listener thread.
1156 */
1157 sigfillset(&sig_all_blocked);
1158 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1159 if (ret) {
d94d802c 1160 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1161 }
1162
1879f67f
MG
1163 ret = pthread_attr_init(&thread_attr);
1164 if (ret) {
1165 ERR("pthread_attr_init: %s", strerror(ret));
1166 }
1167 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1168 if (ret) {
1169 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1170 }
1171
e33f3265 1172 ust_lock();
1879f67f 1173 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
dde70ea0 1174 ust_listener_thread, &global_apps);
d94d802c
MD
1175 if (ret) {
1176 ERR("pthread_create global: %s", strerror(ret));
1177 }
e33f3265
MD
1178 global_apps.thread_active = 1;
1179 ust_unlock();
1180
8d20bf54 1181 if (local_apps.allowed) {
e33f3265 1182 ust_lock();
1879f67f 1183 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 1184 ust_listener_thread, &local_apps);
d94d802c
MD
1185 if (ret) {
1186 ERR("pthread_create local: %s", strerror(ret));
1187 }
e33f3265
MD
1188 local_apps.thread_active = 1;
1189 ust_unlock();
8d20bf54
MD
1190 } else {
1191 handle_register_done(&local_apps);
1192 }
1879f67f
MG
1193 ret = pthread_attr_destroy(&thread_attr);
1194 if (ret) {
1195 ERR("pthread_attr_destroy: %s", strerror(ret));
1196 }
8d20bf54 1197
ae6a58bf
WP
1198 /* Restore original signal mask in parent */
1199 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1200 if (ret) {
d94d802c 1201 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1202 }
1203
cf12a773
MD
1204 switch (timeout_mode) {
1205 case 1: /* timeout wait */
95259bd0
MD
1206 do {
1207 ret = sem_timedwait(&constructor_wait,
1208 &constructor_timeout);
1209 } while (ret < 0 && errno == EINTR);
cf12a773 1210 if (ret < 0 && errno == ETIMEDOUT) {
7dd08bec 1211 ERR("Timed out waiting for lttng-sessiond");
cf12a773
MD
1212 } else {
1213 assert(!ret);
1214 }
1215 break;
7b766b16 1216 case -1:/* wait forever */
95259bd0
MD
1217 do {
1218 ret = sem_wait(&constructor_wait);
1219 } while (ret < 0 && errno == EINTR);
11ff9c7d 1220 assert(!ret);
cf12a773 1221 break;
7b766b16 1222 case 0: /* no timeout */
cf12a773 1223 break;
11ff9c7d 1224 }
2691221a
MD
1225}
1226
17dfb34b
MD
1227static
1228void lttng_ust_cleanup(int exiting)
1229{
efe0de09 1230 cleanup_sock_info(&global_apps, exiting);
17dfb34b 1231 if (local_apps.allowed) {
efe0de09 1232 cleanup_sock_info(&local_apps, exiting);
17dfb34b 1233 }
efe0de09
MD
1234 /*
1235 * The teardown in this function all affect data structures
1236 * accessed under the UST lock by the listener thread. This
1237 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1238 * that none of these threads are accessing this data at this
1239 * point.
1240 */
17dfb34b 1241 lttng_ust_abi_exit();
003fedf4 1242 lttng_ust_events_exit();
a0a3bef9 1243 lttng_context_exit();
34a91bdb 1244 lttng_ring_buffer_client_discard_rt_exit();
7dd08bec 1245 lttng_ring_buffer_client_discard_exit();
34a91bdb 1246 lttng_ring_buffer_client_overwrite_rt_exit();
7dd08bec
MD
1247 lttng_ring_buffer_client_overwrite_exit();
1248 lttng_ring_buffer_metadata_client_exit();
17dfb34b
MD
1249 exit_tracepoint();
1250 if (!exiting) {
1251 /* Reinitialize values for fork */
1252 sem_count = 2;
1253 lttng_ust_comm_should_quit = 0;
1254 initialized = 0;
1255 }
1256}
1257
edaa1431 1258void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1259{
1260 int ret;
1261
9eb62b9c
MD
1262 /*
1263 * Using pthread_cancel here because:
1264 * A) we don't want to hang application teardown.
1265 * B) the thread is not allocating any resource.
1266 */
1ea11eab
MD
1267
1268 /*
1269 * Require the communication thread to quit. Synchronize with
1270 * mutexes to ensure it is not in a mutex critical section when
1271 * pthread_cancel is later called.
1272 */
17dfb34b 1273 ust_lock();
1ea11eab 1274 lttng_ust_comm_should_quit = 1;
1ea11eab 1275
f5f94532 1276 /* cancel threads */
e33f3265
MD
1277 if (global_apps.thread_active) {
1278 ret = pthread_cancel(global_apps.ust_listener);
1279 if (ret) {
1280 ERR("Error cancelling global ust listener thread: %s",
1281 strerror(ret));
1282 } else {
1283 global_apps.thread_active = 0;
1284 }
2691221a 1285 }
e33f3265 1286 if (local_apps.thread_active) {
8d20bf54
MD
1287 ret = pthread_cancel(local_apps.ust_listener);
1288 if (ret) {
d94d802c
MD
1289 ERR("Error cancelling local ust listener thread: %s",
1290 strerror(ret));
e33f3265
MD
1291 } else {
1292 local_apps.thread_active = 0;
8d20bf54 1293 }
8d20bf54 1294 }
e33f3265
MD
1295 ust_unlock();
1296
efe0de09
MD
1297 /*
1298 * Do NOT join threads: use of sys_futex makes it impossible to
1299 * join the threads without using async-cancel, but async-cancel
1300 * is delivered by a signal, which could hit the target thread
1301 * anywhere in its code path, including while the ust_lock() is
1302 * held, causing a deadlock for the other thread. Let the OS
1303 * cleanup the threads if there are stalled in a syscall.
1304 */
17dfb34b 1305 lttng_ust_cleanup(1);
2691221a 1306}
e822f505
MD
1307
1308/*
1309 * We exclude the worker threads across fork and clone (except
1310 * CLONE_VM), because these system calls only keep the forking thread
1311 * running in the child. Therefore, we don't want to call fork or clone
1312 * in the middle of an tracepoint or ust tracing state modification.
1313 * Holding this mutex protects these structures across fork and clone.
1314 */
b728d87e 1315void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
1316{
1317 /*
1318 * Disable signals. This is to avoid that the child intervenes
1319 * before it is properly setup for tracing. It is safer to
1320 * disable all signals, because then we know we are not breaking
1321 * anything by restoring the original mask.
1322 */
1323 sigset_t all_sigs;
1324 int ret;
1325
8c90a710 1326 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1327 return;
e822f505
MD
1328 /* Disable signals */
1329 sigfillset(&all_sigs);
b728d87e 1330 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
1331 if (ret == -1) {
1332 PERROR("sigprocmask");
1333 }
17dfb34b 1334 ust_lock();
e822f505
MD
1335 rcu_bp_before_fork();
1336}
1337
b728d87e 1338static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
1339{
1340 int ret;
1341
17dfb34b
MD
1342 DBG("process %d", getpid());
1343 ust_unlock();
e822f505 1344 /* Restore signals */
23c8854a 1345 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
1346 if (ret == -1) {
1347 PERROR("sigprocmask");
1348 }
1349}
1350
b728d87e 1351void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 1352{
8c90a710 1353 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1354 return;
17dfb34b 1355 DBG("process %d", getpid());
e822f505
MD
1356 rcu_bp_after_fork_parent();
1357 /* Release mutexes and reenable signals */
b728d87e 1358 ust_after_fork_common(restore_sigset);
e822f505
MD
1359}
1360
17dfb34b
MD
1361/*
1362 * After fork, in the child, we need to cleanup all the leftover state,
1363 * except the worker thread which already magically disappeared thanks
1364 * to the weird Linux fork semantics. After tyding up, we call
1365 * lttng_ust_init() again to start over as a new PID.
1366 *
1367 * This is meant for forks() that have tracing in the child between the
1368 * fork and following exec call (if there is any).
1369 */
b728d87e 1370void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 1371{
8c90a710 1372 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 1373 return;
17dfb34b 1374 DBG("process %d", getpid());
e822f505
MD
1375 /* Release urcu mutexes */
1376 rcu_bp_after_fork_child();
17dfb34b 1377 lttng_ust_cleanup(0);
a93bfc45 1378 lttng_context_vtid_reset();
e822f505 1379 /* Release mutexes and reenable signals */
b728d87e 1380 ust_after_fork_common(restore_sigset);
318dfea9 1381 lttng_ust_init();
e822f505 1382}
This page took 0.102138 seconds and 4 git commands to generate.