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