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