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