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