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