Fix: wait for initial statedump before proceeding to the main program
[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
5e1b7b8b 23#define _GNU_SOURCE
2691221a
MD
24#include <sys/types.h>
25#include <sys/socket.h>
7fc90dca
MD
26#include <sys/mman.h>
27#include <sys/stat.h>
58d4b2a2
MD
28#include <sys/types.h>
29#include <sys/wait.h>
7fc90dca 30#include <fcntl.h>
2691221a
MD
31#include <unistd.h>
32#include <errno.h>
d9e99d10 33#include <pthread.h>
11ff9c7d
MD
34#include <semaphore.h>
35#include <time.h>
1ea11eab 36#include <assert.h>
e822f505 37#include <signal.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 48#include <ust-comm.h>
6548fca4 49#include <ust-fd.h>
44c72f10 50#include <usterr-signal-safe.h>
cd54f6d9 51#include <helper.h>
44c72f10 52#include "tracepoint-internal.h"
7dd08bec 53#include "lttng-tracer-core.h"
08114193 54#include "compat.h"
f645cfa7 55#include "../libringbuffer/tlsfixup.h"
cf73e0fe 56#include "lttng-ust-statedump.h"
f9364363 57#include "clock.h"
5e1b7b8b 58#include "../libringbuffer/getcpu.h"
13efba44 59#include "getenv.h"
edaa1431
MD
60
61/*
62 * Has lttng ust comm constructor been called ?
63 */
64static int initialized;
65
1ea11eab 66/*
17dfb34b
MD
67 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
68 * Held when handling a command, also held by fork() to deal with
69 * removal of threads, and by exit path.
3327ac33
MD
70 *
71 * The UST lock is the centralized mutex across UST tracing control and
72 * probe registration.
73 *
74 * ust_exit_mutex must never nest in ust_mutex.
d58d1454 75 *
4770bd47
MD
76 * ust_fork_mutex must never nest in ust_mutex.
77 *
d58d1454
MD
78 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
79 * counter lazy initialization called by events within the statedump,
80 * which traces while the ust_mutex is held.
4770bd47
MD
81 *
82 * ust_lock nests within the dynamic loader lock (within glibc) because
83 * it is taken within the library constructor.
3327ac33
MD
84 */
85static pthread_mutex_t ust_mutex = PTHREAD_MUTEX_INITIALIZER;
86
d58d1454
MD
87/* Allow nesting the ust_mutex within the same thread. */
88static DEFINE_URCU_TLS(int, ust_mutex_nest);
89
3327ac33
MD
90/*
91 * ust_exit_mutex protects thread_active variable wrt thread exit. It
92 * cannot be done by ust_mutex because pthread_cancel(), which takes an
93 * internal libc lock, cannot nest within ust_mutex.
94 *
95 * It never nests within a ust_mutex.
1ea11eab 96 */
3327ac33 97static pthread_mutex_t ust_exit_mutex = PTHREAD_MUTEX_INITIALIZER;
1ea11eab 98
458d678c
PW
99/*
100 * ust_fork_mutex protects base address statedump tracing against forks. It
101 * prevents the dynamic loader lock to be taken (by base address statedump
102 * tracing) while a fork is happening, thus preventing deadlock issues with
103 * the dynamic loader lock.
104 */
105static pthread_mutex_t ust_fork_mutex = PTHREAD_MUTEX_INITIALIZER;
106
1ea11eab
MD
107/* Should the ust comm thread quit ? */
108static int lttng_ust_comm_should_quit;
109
07b57e5e
MD
110/*
111 * This variable can be tested by applications to check whether
112 * lttng-ust is loaded. They simply have to define their own
113 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
114 * library constructor.
115 */
116int lttng_ust_loaded __attribute__((weak));
117
3327ac33 118/*
d58d1454 119 * Return 0 on success, -1 if should quit.
3327ac33 120 * The lock is taken in both cases.
d58d1454 121 * Signal-safe.
3327ac33
MD
122 */
123int ust_lock(void)
124{
d58d1454 125 sigset_t sig_all_blocked, orig_mask;
e446ad80 126 int ret, oldstate;
d58d1454 127
e446ad80
MD
128 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
129 if (ret) {
130 ERR("pthread_setcancelstate: %s", strerror(ret));
131 }
132 if (oldstate != PTHREAD_CANCEL_ENABLE) {
133 ERR("pthread_setcancelstate: unexpected oldstate");
134 }
d58d1454
MD
135 sigfillset(&sig_all_blocked);
136 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
137 if (ret) {
138 ERR("pthread_sigmask: %s", strerror(ret));
139 }
140 if (!URCU_TLS(ust_mutex_nest)++)
141 pthread_mutex_lock(&ust_mutex);
142 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
143 if (ret) {
144 ERR("pthread_sigmask: %s", strerror(ret));
145 }
3327ac33
MD
146 if (lttng_ust_comm_should_quit) {
147 return -1;
148 } else {
149 return 0;
150 }
151}
152
153/*
154 * ust_lock_nocheck() can be used in constructors/destructors, because
155 * they are already nested within the dynamic loader lock, and therefore
156 * have exclusive access against execution of liblttng-ust destructor.
d58d1454 157 * Signal-safe.
3327ac33
MD
158 */
159void ust_lock_nocheck(void)
160{
d58d1454 161 sigset_t sig_all_blocked, orig_mask;
e446ad80 162 int ret, oldstate;
d58d1454 163
e446ad80
MD
164 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
165 if (ret) {
166 ERR("pthread_setcancelstate: %s", strerror(ret));
167 }
168 if (oldstate != PTHREAD_CANCEL_ENABLE) {
169 ERR("pthread_setcancelstate: unexpected oldstate");
170 }
d58d1454
MD
171 sigfillset(&sig_all_blocked);
172 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
173 if (ret) {
174 ERR("pthread_sigmask: %s", strerror(ret));
175 }
176 if (!URCU_TLS(ust_mutex_nest)++)
177 pthread_mutex_lock(&ust_mutex);
178 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
179 if (ret) {
180 ERR("pthread_sigmask: %s", strerror(ret));
181 }
3327ac33
MD
182}
183
d58d1454
MD
184/*
185 * Signal-safe.
186 */
3327ac33
MD
187void ust_unlock(void)
188{
d58d1454 189 sigset_t sig_all_blocked, orig_mask;
e446ad80 190 int ret, oldstate;
d58d1454
MD
191
192 sigfillset(&sig_all_blocked);
193 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
194 if (ret) {
195 ERR("pthread_sigmask: %s", strerror(ret));
196 }
197 if (!--URCU_TLS(ust_mutex_nest))
198 pthread_mutex_unlock(&ust_mutex);
199 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
200 if (ret) {
201 ERR("pthread_sigmask: %s", strerror(ret));
202 }
e446ad80
MD
203 ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
204 if (ret) {
205 ERR("pthread_setcancelstate: %s", strerror(ret));
206 }
207 if (oldstate != PTHREAD_CANCEL_DISABLE) {
208 ERR("pthread_setcancelstate: unexpected oldstate");
209 }
3327ac33
MD
210}
211
11ff9c7d
MD
212/*
213 * Wait for either of these before continuing to the main
214 * program:
215 * - the register_done message from sessiond daemon
216 * (will let the sessiond daemon enable sessions before main
217 * starts.)
218 * - sessiond daemon is not reachable.
219 * - timeout (ensuring applications are resilient to session
220 * daemon problems).
221 */
222static sem_t constructor_wait;
950aab0c
MD
223/*
224 * Doing this for both the global and local sessiond.
225 */
3f5339b0
GAPG
226enum {
227 sem_count_initial_value = 4,
228};
229
230static int sem_count = sem_count_initial_value;
11ff9c7d 231
e8508a49
MD
232/*
233 * Counting nesting within lttng-ust. Used to ensure that calling fork()
234 * from liblttng-ust does not execute the pre/post fork handlers.
235 */
8c90a710 236static DEFINE_URCU_TLS(int, lttng_ust_nest_count);
e8508a49 237
1ea11eab
MD
238/*
239 * Info about socket and associated listener thread.
240 */
241struct sock_info {
11ff9c7d 242 const char *name;
1ea11eab 243 pthread_t ust_listener; /* listener thread */
46050b1a 244 int root_handle;
3f5339b0 245 int registration_done;
8d20bf54 246 int allowed;
44e073f5 247 int global;
e33f3265 248 int thread_active;
7fc90dca
MD
249
250 char sock_path[PATH_MAX];
251 int socket;
32ce8569 252 int notify_socket;
7fc90dca
MD
253
254 char wait_shm_path[PATH_MAX];
255 char *wait_shm_mmap;
37dddb65
MD
256 /* Keep track of lazy state dump not performed yet. */
257 int statedump_pending;
3f5339b0 258 int initial_statedump_done;
1ea11eab 259};
2691221a
MD
260
261/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 262struct sock_info global_apps = {
11ff9c7d 263 .name = "global",
44e073f5 264 .global = 1,
7fc90dca 265
46050b1a 266 .root_handle = -1,
3f5339b0 267 .registration_done = 0,
2eab9f28 268 .allowed = 0,
e33f3265 269 .thread_active = 0,
7fc90dca 270
32ce8569 271 .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME,
7fc90dca 272 .socket = -1,
32ce8569 273 .notify_socket = -1,
7fc90dca 274
32ce8569 275 .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME,
95c25348 276
37dddb65 277 .statedump_pending = 0,
3f5339b0 278 .initial_statedump_done = 0,
1ea11eab 279};
2691221a
MD
280
281/* TODO: allow global_apps_sock_path override */
282
1ea11eab 283struct sock_info local_apps = {
11ff9c7d 284 .name = "local",
44e073f5 285 .global = 0,
46050b1a 286 .root_handle = -1,
3f5339b0 287 .registration_done = 0,
8d20bf54 288 .allowed = 0, /* Check setuid bit first */
e33f3265 289 .thread_active = 0,
7fc90dca
MD
290
291 .socket = -1,
32ce8569 292 .notify_socket = -1,
95c25348 293
37dddb65 294 .statedump_pending = 0,
3f5339b0 295 .initial_statedump_done = 0,
1ea11eab 296};
2691221a 297
37ed587a
MD
298static int wait_poll_fallback;
299
74d81a6c
MD
300static const char *cmd_name_mapping[] = {
301 [ LTTNG_UST_RELEASE ] = "Release",
302 [ LTTNG_UST_SESSION ] = "Create Session",
303 [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version",
304
305 [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List",
306 [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State",
307 [ LTTNG_UST_REGISTER_DONE ] = "Registration Done",
308 [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List",
309
310 /* Session FD commands */
311 [ LTTNG_UST_CHANNEL ] = "Create Channel",
312 [ LTTNG_UST_SESSION_START ] = "Start Session",
313 [ LTTNG_UST_SESSION_STOP ] = "Stop Session",
314
315 /* Channel FD commands */
316 [ LTTNG_UST_STREAM ] = "Create Stream",
317 [ LTTNG_UST_EVENT ] = "Create Event",
318
319 /* Event and Channel FD commands */
320 [ LTTNG_UST_CONTEXT ] = "Create Context",
321 [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer",
322
323 /* Event, Channel and Session commands */
324 [ LTTNG_UST_ENABLE ] = "Enable",
325 [ LTTNG_UST_DISABLE ] = "Disable",
326
327 /* Tracepoint list commands */
328 [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint",
329 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field",
330
331 /* Event FD commands */
332 [ LTTNG_UST_FILTER ] = "Create Filter",
75582b3a 333 [ LTTNG_UST_EXCLUSION ] = "Add exclusions to event",
74d81a6c
MD
334};
335
ff517991
MD
336static const char *str_timeout;
337static int got_timeout_env;
338
7dd08bec 339extern void lttng_ring_buffer_client_overwrite_init(void);
34a91bdb 340extern void lttng_ring_buffer_client_overwrite_rt_init(void);
7dd08bec 341extern void lttng_ring_buffer_client_discard_init(void);
34a91bdb 342extern void lttng_ring_buffer_client_discard_rt_init(void);
7dd08bec
MD
343extern void lttng_ring_buffer_metadata_client_init(void);
344extern void lttng_ring_buffer_client_overwrite_exit(void);
34a91bdb 345extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
7dd08bec 346extern void lttng_ring_buffer_client_discard_exit(void);
34a91bdb 347extern void lttng_ring_buffer_client_discard_rt_exit(void);
7dd08bec 348extern void lttng_ring_buffer_metadata_client_exit(void);
edaa1431 349
2eab9f28
JR
350static char *get_map_shm(struct sock_info *sock_info);
351
405be658
MD
352ssize_t lttng_ust_read(int fd, void *buf, size_t len)
353{
354 ssize_t ret;
355 size_t copied = 0, to_copy = len;
356
357 do {
358 ret = read(fd, buf + copied, to_copy);
359 if (ret > 0) {
360 copied += ret;
361 to_copy -= ret;
362 }
363 } while ((ret > 0 && to_copy > 0)
364 || (ret < 0 && errno == EINTR));
365 if (ret > 0) {
366 ret = copied;
367 }
368 return ret;
369}
3c6f6263
AM
370/*
371 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
372 * pointer.
373 */
374static
375const char *get_lttng_home_dir(void)
376{
377 const char *val;
378
49254388 379 val = (const char *) lttng_getenv("LTTNG_HOME");
3c6f6263
AM
380 if (val != NULL) {
381 return val;
382 }
49254388 383 return (const char *) lttng_getenv("HOME");
3c6f6263
AM
384}
385
a903623f
MD
386/*
387 * Force a read (imply TLS fixup for dlopen) of TLS variables.
388 */
389static
390void lttng_fixup_nest_count_tls(void)
391{
8c90a710 392 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count)));
a903623f
MD
393}
394
d58d1454
MD
395static
396void lttng_fixup_ust_mutex_nest_tls(void)
397{
398 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest)));
399}
400
1556a549
MD
401/*
402 * Fixup urcu bp TLS.
403 */
404static
405void lttng_fixup_urcu_bp_tls(void)
406{
407 rcu_read_lock();
408 rcu_read_unlock();
409}
410
c362addf
MD
411void lttng_ust_fixup_tls(void)
412{
413 lttng_fixup_urcu_bp_tls();
414 lttng_fixup_ringbuffer_tls();
415 lttng_fixup_vtid_tls();
416 lttng_fixup_nest_count_tls();
417 lttng_fixup_procname_tls();
418 lttng_fixup_ust_mutex_nest_tls();
6548fca4 419 lttng_ust_fixup_fd_tracker_tls();
c362addf
MD
420}
421
32ce8569
MD
422int lttng_get_notify_socket(void *owner)
423{
424 struct sock_info *info = owner;
425
426 return info->notify_socket;
427}
428
74d81a6c
MD
429static
430void print_cmd(int cmd, int handle)
431{
432 const char *cmd_name = "Unknown";
433
fd67a004
MD
434 if (cmd >= 0 && cmd < LTTNG_ARRAY_SIZE(cmd_name_mapping)
435 && cmd_name_mapping[cmd]) {
74d81a6c
MD
436 cmd_name = cmd_name_mapping[cmd];
437 }
fd67a004
MD
438 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
439 cmd_name, cmd,
74d81a6c
MD
440 lttng_ust_obj_get_name(handle), handle);
441}
442
2eab9f28
JR
443static
444int setup_global_apps(void)
445{
446 int ret = 0;
447 assert(!global_apps.wait_shm_mmap);
448
449 global_apps.wait_shm_mmap = get_map_shm(&global_apps);
450 if (!global_apps.wait_shm_mmap) {
451 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
452 global_apps.allowed = 0;
453 ret = -EIO;
454 goto error;
455 }
456
457 global_apps.allowed = 1;
458error:
459 return ret;
460}
2691221a 461static
8d20bf54 462int setup_local_apps(void)
2691221a 463{
2eab9f28 464 int ret = 0;
2691221a 465 const char *home_dir;
7fc90dca 466 uid_t uid;
2691221a 467
2eab9f28
JR
468 assert(!local_apps.wait_shm_mmap);
469
7fc90dca 470 uid = getuid();
8d20bf54
MD
471 /*
472 * Disallow per-user tracing for setuid binaries.
473 */
7fc90dca 474 if (uid != geteuid()) {
9ec6895c 475 assert(local_apps.allowed == 0);
2eab9f28
JR
476 ret = 0;
477 goto end;
8d20bf54 478 }
3c6f6263 479 home_dir = get_lttng_home_dir();
9ec6895c
MD
480 if (!home_dir) {
481 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
482 assert(local_apps.allowed == 0);
2eab9f28
JR
483 ret = -ENOENT;
484 goto end;
9ec6895c
MD
485 }
486 local_apps.allowed = 1;
32ce8569
MD
487 snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s",
488 home_dir,
489 LTTNG_DEFAULT_HOME_RUNDIR,
490 LTTNG_UST_SOCK_FILENAME);
491 snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u",
492 LTTNG_UST_WAIT_FILENAME,
493 uid);
2eab9f28
JR
494
495 local_apps.wait_shm_mmap = get_map_shm(&local_apps);
496 if (!local_apps.wait_shm_mmap) {
497 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
498 local_apps.allowed = 0;
499 ret = -EIO;
500 goto end;
501 }
502end:
503 return ret;
2691221a
MD
504}
505
ff517991 506/*
451d66b2 507 * Get socket timeout, in ms.
28515902 508 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
ff517991
MD
509 */
510static
511long get_timeout(void)
512{
513 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
514
515 if (!got_timeout_env) {
49254388 516 str_timeout = lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
ff517991
MD
517 got_timeout_env = 1;
518 }
519 if (str_timeout)
520 constructor_delay_ms = strtol(str_timeout, NULL, 10);
5cf81d53
MD
521 /* All negative values are considered as "-1". */
522 if (constructor_delay_ms < -1)
523 constructor_delay_ms = -1;
ff517991
MD
524 return constructor_delay_ms;
525}
526
451d66b2 527/* Timeout for notify socket send and recv. */
ff517991
MD
528static
529long get_notify_sock_timeout(void)
530{
531 return get_timeout();
532}
533
451d66b2
MD
534/* Timeout for connecting to cmd and notify sockets. */
535static
536long get_connect_sock_timeout(void)
537{
538 return get_timeout();
539}
540
ff517991 541/*
28515902 542 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
ff517991
MD
543 */
544static
545int get_constructor_timeout(struct timespec *constructor_timeout)
546{
547 long constructor_delay_ms;
548 int ret;
549
550 constructor_delay_ms = get_timeout();
551
552 switch (constructor_delay_ms) {
553 case -1:/* fall-through */
554 case 0:
555 return constructor_delay_ms;
556 default:
557 break;
558 }
559
560 /*
561 * If we are unable to find the current time, don't wait.
562 */
563 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
564 if (ret) {
28515902
JG
565 /* Don't wait. */
566 return 0;
ff517991
MD
567 }
568 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
569 constructor_timeout->tv_nsec +=
570 (constructor_delay_ms % 1000UL) * 1000000UL;
571 if (constructor_timeout->tv_nsec >= 1000000000UL) {
572 constructor_timeout->tv_sec++;
573 constructor_timeout->tv_nsec -= 1000000000UL;
574 }
28515902 575 /* Timeout wait (constructor_delay_ms). */
ff517991
MD
576 return 1;
577}
578
2691221a 579static
32ce8569 580int register_to_sessiond(int socket, enum ustctl_socket_type type)
2691221a 581{
32ce8569
MD
582 return ustcomm_send_reg_msg(socket,
583 type,
584 CAA_BITS_PER_LONG,
585 lttng_alignof(uint8_t) * CHAR_BIT,
586 lttng_alignof(uint16_t) * CHAR_BIT,
587 lttng_alignof(uint32_t) * CHAR_BIT,
588 lttng_alignof(uint64_t) * CHAR_BIT,
589 lttng_alignof(unsigned long) * CHAR_BIT);
2691221a
MD
590}
591
d9e99d10 592static
57773204 593int send_reply(int sock, struct ustcomm_ust_reply *lur)
d9e99d10 594{
9eb62b9c 595 ssize_t len;
d3a492d1 596
57773204 597 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 598 switch (len) {
a4be8962 599 case sizeof(*lur):
d3a492d1
MD
600 DBG("message successfully sent");
601 return 0;
7bc53e94
MD
602 default:
603 if (len == -ECONNRESET) {
604 DBG("remote end closed connection");
d3a492d1
MD
605 return 0;
606 }
7bc53e94
MD
607 if (len < 0)
608 return len;
609 DBG("incorrect message size: %zd", len);
610 return -EINVAL;
d3a492d1
MD
611 }
612}
613
614static
3f5339b0 615void decrement_sem_count(unsigned int count)
11ff9c7d
MD
616{
617 int ret;
618
3f5339b0
GAPG
619 assert(uatomic_read(&sem_count) >= count);
620
56cd7e2f 621 if (uatomic_read(&sem_count) <= 0) {
3f5339b0 622 return;
56cd7e2f 623 }
3f5339b0
GAPG
624
625 ret = uatomic_add_return(&sem_count, -count);
95259bd0
MD
626 if (ret == 0) {
627 ret = sem_post(&constructor_wait);
628 assert(!ret);
629 }
3f5339b0
GAPG
630}
631
632static
633int handle_register_done(struct sock_info *sock_info)
634{
635 if (sock_info->registration_done)
636 return 0;
637 sock_info->registration_done = 1;
638
639 decrement_sem_count(1);
640
641 return 0;
642}
643
644static
645int handle_register_failed(struct sock_info *sock_info)
646{
647 if (sock_info->registration_done)
648 return 0;
649 sock_info->registration_done = 1;
650 sock_info->initial_statedump_done = 1;
651
652 decrement_sem_count(2);
653
11ff9c7d
MD
654 return 0;
655}
656
37dddb65
MD
657/*
658 * Only execute pending statedump after the constructor semaphore has
3f5339b0
GAPG
659 * been posted by the current listener thread. This means statedump will
660 * only be performed after the "registration done" command is received
661 * from this thread's session daemon.
37dddb65
MD
662 *
663 * This ensures we don't run into deadlock issues with the dynamic
664 * loader mutex, which is held while the constructor is called and
665 * waiting on the constructor semaphore. All operations requiring this
666 * dynamic loader lock need to be postponed using this mechanism.
3f5339b0
GAPG
667 *
668 * In a scenario with two session daemons connected to the application,
669 * it is possible that the first listener thread which receives the
670 * registration done command issues its statedump while the dynamic
671 * loader lock is still held by the application constructor waiting on
672 * the semaphore. It will however be allowed to proceed when the
673 * second session daemon sends the registration done command to the
674 * second listener thread. This situation therefore does not produce
675 * a deadlock.
37dddb65
MD
676 */
677static
678void handle_pending_statedump(struct sock_info *sock_info)
679{
3f5339b0 680 if (sock_info->registration_done && sock_info->statedump_pending) {
37dddb65 681 sock_info->statedump_pending = 0;
2932a87f 682 pthread_mutex_lock(&ust_fork_mutex);
37dddb65 683 lttng_handle_pending_statedump(sock_info);
458d678c 684 pthread_mutex_unlock(&ust_fork_mutex);
3f5339b0
GAPG
685
686 if (!sock_info->initial_statedump_done) {
687 sock_info->initial_statedump_done = 1;
688 decrement_sem_count(1);
689 }
37dddb65
MD
690 }
691}
692
11ff9c7d
MD
693static
694int handle_message(struct sock_info *sock_info,
57773204 695 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 696{
1ea11eab 697 int ret = 0;
b61ce3b2 698 const struct lttng_ust_objd_ops *ops;
57773204 699 struct ustcomm_ust_reply lur;
ef9ff354 700 union ust_args args;
8e696cfa 701 char ctxstr[LTTNG_UST_SYM_NAME_LEN]; /* App context string. */
40003310 702 ssize_t len;
1ea11eab 703
46050b1a
MD
704 memset(&lur, 0, sizeof(lur));
705
3327ac33 706 if (ust_lock()) {
74d81a6c 707 ret = -LTTNG_UST_ERR_EXITING;
0dafcd63 708 goto error;
1ea11eab 709 }
9eb62b9c 710
46050b1a
MD
711 ops = objd_ops(lum->handle);
712 if (!ops) {
713 ret = -ENOENT;
0dafcd63 714 goto error;
1ea11eab 715 }
46050b1a
MD
716
717 switch (lum->cmd) {
11ff9c7d
MD
718 case LTTNG_UST_REGISTER_DONE:
719 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 720 ret = handle_register_done(sock_info);
11ff9c7d
MD
721 else
722 ret = -EINVAL;
723 break;
46050b1a
MD
724 case LTTNG_UST_RELEASE:
725 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
726 ret = -EPERM;
727 else
1849ef7c 728 ret = lttng_ust_objd_unref(lum->handle, 1);
d9e99d10 729 break;
2d78951a
MD
730 case LTTNG_UST_FILTER:
731 {
732 /* Receive filter data */
f488575f 733 struct lttng_ust_filter_bytecode_node *bytecode;
2d78951a 734
cd54f6d9 735 if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
7bc53e94 736 ERR("Filter data size is too large: %u bytes",
2d78951a
MD
737 lum->u.filter.data_size);
738 ret = -EINVAL;
739 goto error;
740 }
2734ca65 741
885b1dfd 742 if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
7bc53e94 743 ERR("Filter reloc offset %u is not within data",
2734ca65
CB
744 lum->u.filter.reloc_offset);
745 ret = -EINVAL;
746 goto error;
747 }
748
cd54f6d9
MD
749 bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size);
750 if (!bytecode) {
751 ret = -ENOMEM;
752 goto error;
753 }
f488575f 754 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data,
2d78951a
MD
755 lum->u.filter.data_size);
756 switch (len) {
757 case 0: /* orderly shutdown */
758 ret = 0;
cd54f6d9 759 free(bytecode);
2d78951a 760 goto error;
2d78951a
MD
761 default:
762 if (len == lum->u.filter.data_size) {
7bc53e94 763 DBG("filter data received");
2d78951a 764 break;
7bc53e94
MD
765 } else if (len < 0) {
766 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
767 if (len == -ECONNRESET) {
768 ERR("%s remote end closed connection", sock_info->name);
769 ret = len;
770 free(bytecode);
771 goto error;
772 }
773 ret = len;
eb8bf361 774 free(bytecode);
0dafcd63 775 goto error;
2d78951a 776 } else {
7bc53e94 777 DBG("incorrect filter data message size: %zd", len);
2d78951a 778 ret = -EINVAL;
cd54f6d9 779 free(bytecode);
0dafcd63 780 goto error;
2d78951a
MD
781 }
782 }
f488575f
MD
783 bytecode->bc.len = lum->u.filter.data_size;
784 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
3f6fd224 785 bytecode->bc.seqnum = lum->u.filter.seqnum;
cd54f6d9 786 if (ops->cmd) {
2d78951a 787 ret = ops->cmd(lum->handle, lum->cmd,
cd54f6d9 788 (unsigned long) bytecode,
f59ed768 789 &args, sock_info);
cd54f6d9
MD
790 if (ret) {
791 free(bytecode);
792 }
793 /* don't free bytecode if everything went fine. */
794 } else {
2d78951a 795 ret = -ENOSYS;
cd54f6d9
MD
796 free(bytecode);
797 }
2d78951a
MD
798 break;
799 }
86e36163
JI
800 case LTTNG_UST_EXCLUSION:
801 {
802 /* Receive exclusion names */
803 struct lttng_ust_excluder_node *node;
804 unsigned int count;
805
806 count = lum->u.exclusion.count;
807 if (count == 0) {
808 /* There are no names to read */
809 ret = 0;
810 goto error;
811 }
812 node = zmalloc(sizeof(*node) +
813 count * LTTNG_UST_SYM_NAME_LEN);
814 if (!node) {
815 ret = -ENOMEM;
816 goto error;
817 }
818 node->excluder.count = count;
819 len = ustcomm_recv_unix_sock(sock, node->excluder.names,
820 count * LTTNG_UST_SYM_NAME_LEN);
821 switch (len) {
822 case 0: /* orderly shutdown */
823 ret = 0;
824 free(node);
825 goto error;
826 default:
827 if (len == count * LTTNG_UST_SYM_NAME_LEN) {
828 DBG("Exclusion data received");
829 break;
830 } else if (len < 0) {
831 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
832 if (len == -ECONNRESET) {
833 ERR("%s remote end closed connection", sock_info->name);
834 ret = len;
835 free(node);
836 goto error;
837 }
838 ret = len;
839 free(node);
0dafcd63 840 goto error;
86e36163
JI
841 } else {
842 DBG("Incorrect exclusion data message size: %zd", len);
843 ret = -EINVAL;
844 free(node);
0dafcd63 845 goto error;
86e36163
JI
846 }
847 }
848 if (ops->cmd) {
849 ret = ops->cmd(lum->handle, lum->cmd,
850 (unsigned long) node,
851 &args, sock_info);
852 if (ret) {
853 free(node);
854 }
855 /* Don't free exclusion data if everything went fine. */
856 } else {
857 ret = -ENOSYS;
858 free(node);
859 }
860 break;
861 }
74d81a6c
MD
862 case LTTNG_UST_CHANNEL:
863 {
864 void *chan_data;
ff0f5728 865 int wakeup_fd;
74d81a6c
MD
866
867 len = ustcomm_recv_channel_from_sessiond(sock,
ff0f5728
MD
868 &chan_data, lum->u.channel.len,
869 &wakeup_fd);
74d81a6c
MD
870 switch (len) {
871 case 0: /* orderly shutdown */
872 ret = 0;
873 goto error;
874 default:
875 if (len == lum->u.channel.len) {
876 DBG("channel data received");
877 break;
878 } else if (len < 0) {
879 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
880 if (len == -ECONNRESET) {
881 ERR("%s remote end closed connection", sock_info->name);
882 ret = len;
883 goto error;
884 }
885 ret = len;
0dafcd63 886 goto error;
74d81a6c
MD
887 } else {
888 DBG("incorrect channel data message size: %zd", len);
889 ret = -EINVAL;
0dafcd63 890 goto error;
74d81a6c
MD
891 }
892 }
893 args.channel.chan_data = chan_data;
ff0f5728 894 args.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
895 if (ops->cmd)
896 ret = ops->cmd(lum->handle, lum->cmd,
897 (unsigned long) &lum->u,
898 &args, sock_info);
899 else
900 ret = -ENOSYS;
901 break;
902 }
903 case LTTNG_UST_STREAM:
904 {
905 /* Receive shm_fd, wakeup_fd */
906 ret = ustcomm_recv_stream_from_sessiond(sock,
acafe8d8 907 NULL,
74d81a6c
MD
908 &args.stream.shm_fd,
909 &args.stream.wakeup_fd);
910 if (ret) {
0dafcd63 911 goto error;
74d81a6c 912 }
61973368 913
74d81a6c
MD
914 if (ops->cmd)
915 ret = ops->cmd(lum->handle, lum->cmd,
916 (unsigned long) &lum->u,
917 &args, sock_info);
918 else
919 ret = -ENOSYS;
920 break;
921 }
8e696cfa
MD
922 case LTTNG_UST_CONTEXT:
923 switch (lum->u.context.ctx) {
924 case LTTNG_UST_CONTEXT_APP_CONTEXT:
925 {
926 char *p;
927 size_t ctxlen, recvlen;
928
929 ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1
930 + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len;
931 if (ctxlen >= LTTNG_UST_SYM_NAME_LEN) {
932 ERR("Application context string length size is too large: %zu bytes",
933 ctxlen);
934 ret = -EINVAL;
935 goto error;
936 }
937 strcpy(ctxstr, "$app.");
938 p = &ctxstr[strlen("$app.")];
939 recvlen = ctxlen - strlen("$app.");
940 len = ustcomm_recv_unix_sock(sock, p, recvlen);
941 switch (len) {
942 case 0: /* orderly shutdown */
943 ret = 0;
944 goto error;
945 default:
946 if (len == recvlen) {
947 DBG("app context data received");
948 break;
949 } else if (len < 0) {
950 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
951 if (len == -ECONNRESET) {
952 ERR("%s remote end closed connection", sock_info->name);
953 ret = len;
954 goto error;
955 }
956 ret = len;
957 goto error;
958 } else {
959 DBG("incorrect app context data message size: %zd", len);
960 ret = -EINVAL;
961 goto error;
962 }
963 }
964 /* Put : between provider and ctxname. */
965 p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':';
966 args.app_context.ctxname = ctxstr;
967 break;
968 }
969 default:
970 break;
971 }
972 if (ops->cmd) {
973 ret = ops->cmd(lum->handle, lum->cmd,
974 (unsigned long) &lum->u,
975 &args, sock_info);
976 } else {
977 ret = -ENOSYS;
978 }
979 break;
d9e99d10 980 default:
46050b1a
MD
981 if (ops->cmd)
982 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 983 (unsigned long) &lum->u,
f59ed768 984 &args, sock_info);
46050b1a
MD
985 else
986 ret = -ENOSYS;
987 break;
d9e99d10 988 }
46050b1a 989
46050b1a
MD
990 lur.handle = lum->handle;
991 lur.cmd = lum->cmd;
992 lur.ret_val = ret;
993 if (ret >= 0) {
7bc53e94 994 lur.ret_code = LTTNG_UST_OK;
46050b1a 995 } else {
7bc53e94
MD
996 /*
997 * Use -LTTNG_UST_ERR as wildcard for UST internal
998 * error that are not caused by the transport, except if
999 * we already have a more precise error message to
1000 * report.
1001 */
64b2564e
DG
1002 if (ret > -LTTNG_UST_ERR) {
1003 /* Translate code to UST error. */
1004 switch (ret) {
1005 case -EEXIST:
1006 lur.ret_code = -LTTNG_UST_ERR_EXIST;
1007 break;
1008 case -EINVAL:
1009 lur.ret_code = -LTTNG_UST_ERR_INVAL;
1010 break;
1011 case -ENOENT:
1012 lur.ret_code = -LTTNG_UST_ERR_NOENT;
1013 break;
1014 case -EPERM:
1015 lur.ret_code = -LTTNG_UST_ERR_PERM;
1016 break;
1017 case -ENOSYS:
1018 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
1019 break;
1020 default:
1021 lur.ret_code = -LTTNG_UST_ERR;
1022 break;
1023 }
1024 } else {
7bc53e94 1025 lur.ret_code = ret;
64b2564e 1026 }
46050b1a 1027 }
e6ea14c5
MD
1028 if (ret >= 0) {
1029 switch (lum->cmd) {
e6ea14c5
MD
1030 case LTTNG_UST_TRACER_VERSION:
1031 lur.u.version = lum->u.version;
1032 break;
1033 case LTTNG_UST_TRACEPOINT_LIST_GET:
1034 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
1035 break;
1036 }
381c0f1e 1037 }
74d81a6c 1038 DBG("Return value: %d", lur.ret_val);
4c62d8d1
MD
1039
1040 ust_unlock();
1041
1042 /*
1043 * Performed delayed statedump operations outside of the UST
1044 * lock. We need to take the dynamic loader lock before we take
1045 * the UST lock internally within handle_pending_statedump().
1046 */
1047 handle_pending_statedump(sock_info);
1048
1049 if (ust_lock()) {
1050 ret = -LTTNG_UST_ERR_EXITING;
1051 goto error;
1052 }
1053
46050b1a 1054 ret = send_reply(sock, &lur);
193183fb 1055 if (ret < 0) {
7bc53e94 1056 DBG("error sending reply");
193183fb
MD
1057 goto error;
1058 }
46050b1a 1059
40003310
MD
1060 /*
1061 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1062 * after the reply.
1063 */
7bc53e94 1064 if (lur.ret_code == LTTNG_UST_OK) {
40003310
MD
1065 switch (lum->cmd) {
1066 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1067 len = ustcomm_send_unix_sock(sock,
1068 &args.field_list.entry,
1069 sizeof(args.field_list.entry));
7bc53e94
MD
1070 if (len < 0) {
1071 ret = len;
1072 goto error;
1073 }
40003310 1074 if (len != sizeof(args.field_list.entry)) {
7bc53e94 1075 ret = -EINVAL;
40003310
MD
1076 goto error;
1077 }
1078 }
1079 }
ef9ff354 1080
381c0f1e 1081error:
17dfb34b 1082 ust_unlock();
d9e99d10 1083
37dddb65 1084 return ret;
246be17e
PW
1085}
1086
46050b1a 1087static
efe0de09 1088void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
1089{
1090 int ret;
1091
5b14aab3
MD
1092 if (sock_info->root_handle != -1) {
1093 ret = lttng_ust_objd_unref(sock_info->root_handle, 1);
1094 if (ret) {
1095 ERR("Error unref root handle");
1096 }
1097 sock_info->root_handle = -1;
1098 }
3f5339b0
GAPG
1099 sock_info->registration_done = 0;
1100 sock_info->initial_statedump_done = 0;
5b14aab3
MD
1101
1102 /*
1103 * wait_shm_mmap, socket and notify socket are used by listener
1104 * threads outside of the ust lock, so we cannot tear them down
1105 * ourselves, because we cannot join on these threads. Leave
1106 * responsibility of cleaning up these resources to the OS
1107 * process exit.
1108 */
1109 if (exiting)
1110 return;
1111
46050b1a 1112 if (sock_info->socket != -1) {
e6973a89 1113 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 1114 if (ret) {
32ce8569 1115 ERR("Error closing ust cmd socket");
46050b1a
MD
1116 }
1117 sock_info->socket = -1;
1118 }
32ce8569
MD
1119 if (sock_info->notify_socket != -1) {
1120 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1121 if (ret) {
1122 ERR("Error closing ust notify socket");
1123 }
1124 sock_info->notify_socket = -1;
1125 }
5b14aab3 1126 if (sock_info->wait_shm_mmap) {
172d6b68
MD
1127 long page_size;
1128
1129 page_size = sysconf(_SC_PAGE_SIZE);
2657d1ba
MD
1130 if (page_size <= 0) {
1131 if (!page_size) {
1132 errno = EINVAL;
1133 }
1134 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1135 } else {
172d6b68
MD
1136 ret = munmap(sock_info->wait_shm_mmap, page_size);
1137 if (ret) {
1138 ERR("Error unmapping wait shm");
1139 }
7fc90dca
MD
1140 }
1141 sock_info->wait_shm_mmap = NULL;
1142 }
1143}
1144
58d4b2a2 1145/*
33bbeb90
MD
1146 * Using fork to set umask in the child process (not multi-thread safe).
1147 * We deal with the shm_open vs ftruncate race (happening when the
1148 * sessiond owns the shm and does not let everybody modify it, to ensure
1149 * safety against shm_unlink) by simply letting the mmap fail and
1150 * retrying after a few seconds.
1151 * For global shm, everybody has rw access to it until the sessiond
1152 * starts.
58d4b2a2 1153 */
7fc90dca 1154static
58d4b2a2 1155int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 1156{
7fc90dca 1157 int wait_shm_fd, ret;
58d4b2a2 1158 pid_t pid;
44e073f5 1159
58d4b2a2 1160 /*
33bbeb90 1161 * Try to open read-only.
58d4b2a2 1162 */
33bbeb90 1163 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2 1164 if (wait_shm_fd >= 0) {
7aa76730
MD
1165 int32_t tmp_read;
1166 ssize_t len;
1167 size_t bytes_read = 0;
1168
1169 /*
1170 * Try to read the fd. If unable to do so, try opening
1171 * it in write mode.
1172 */
1173 do {
1174 len = read(wait_shm_fd,
1175 &((char *) &tmp_read)[bytes_read],
1176 sizeof(tmp_read) - bytes_read);
1177 if (len > 0) {
1178 bytes_read += len;
1179 }
1180 } while ((len < 0 && errno == EINTR)
1181 || (len > 0 && bytes_read < sizeof(tmp_read)));
1182 if (bytes_read != sizeof(tmp_read)) {
1183 ret = close(wait_shm_fd);
1184 if (ret) {
1185 ERR("close wait_shm_fd");
1186 }
1187 goto open_write;
1188 }
58d4b2a2
MD
1189 goto end;
1190 } else if (wait_shm_fd < 0 && errno != ENOENT) {
1191 /*
33bbeb90
MD
1192 * Real-only open did not work, and it's not because the
1193 * entry was not present. It's a failure that prohibits
1194 * using shm.
58d4b2a2 1195 */
7fc90dca 1196 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 1197 goto end;
7fc90dca 1198 }
7aa76730
MD
1199
1200open_write:
7fc90dca 1201 /*
7aa76730
MD
1202 * If the open failed because the file did not exist, or because
1203 * the file was not truncated yet, try creating it ourself.
7fc90dca 1204 */
8c90a710 1205 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 1206 pid = fork();
8c90a710 1207 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
1208 if (pid > 0) {
1209 int status;
1210
1211 /*
1212 * Parent: wait for child to return, in which case the
1213 * shared memory map will have been created.
1214 */
1215 pid = wait(&status);
b7d3cb32 1216 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
1217 wait_shm_fd = -1;
1218 goto end;
7fc90dca 1219 }
58d4b2a2
MD
1220 /*
1221 * Try to open read-only again after creation.
1222 */
33bbeb90 1223 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
1224 if (wait_shm_fd < 0) {
1225 /*
1226 * Real-only open did not work. It's a failure
1227 * that prohibits using shm.
1228 */
1229 ERR("Error opening shm %s", sock_info->wait_shm_path);
1230 goto end;
1231 }
1232 goto end;
1233 } else if (pid == 0) {
1234 int create_mode;
1235
1236 /* Child */
33bbeb90 1237 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 1238 if (sock_info->global)
33bbeb90 1239 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
1240 /*
1241 * We're alone in a child process, so we can modify the
1242 * process-wide umask.
1243 */
33bbeb90 1244 umask(~create_mode);
58d4b2a2 1245 /*
33bbeb90
MD
1246 * Try creating shm (or get rw access).
1247 * We don't do an exclusive open, because we allow other
1248 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
1249 */
1250 wait_shm_fd = shm_open(sock_info->wait_shm_path,
1251 O_RDWR | O_CREAT, create_mode);
1252 if (wait_shm_fd >= 0) {
1253 ret = ftruncate(wait_shm_fd, mmap_size);
1254 if (ret) {
1255 PERROR("ftruncate");
b0c1425d 1256 _exit(EXIT_FAILURE);
58d4b2a2 1257 }
b0c1425d 1258 _exit(EXIT_SUCCESS);
58d4b2a2 1259 }
33bbeb90
MD
1260 /*
1261 * For local shm, we need to have rw access to accept
1262 * opening it: this means the local sessiond will be
1263 * able to wake us up. For global shm, we open it even
1264 * if rw access is not granted, because the root.root
1265 * sessiond will be able to override all rights and wake
1266 * us up.
1267 */
1268 if (!sock_info->global && errno != EACCES) {
58d4b2a2 1269 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 1270 _exit(EXIT_FAILURE);
58d4b2a2
MD
1271 }
1272 /*
33bbeb90
MD
1273 * The shm exists, but we cannot open it RW. Report
1274 * success.
58d4b2a2 1275 */
5d3bc5ed 1276 _exit(EXIT_SUCCESS);
58d4b2a2
MD
1277 } else {
1278 return -1;
7fc90dca 1279 }
58d4b2a2 1280end:
33bbeb90
MD
1281 if (wait_shm_fd >= 0 && !sock_info->global) {
1282 struct stat statbuf;
1283
1284 /*
1285 * Ensure that our user is the owner of the shm file for
1286 * local shm. If we do not own the file, it means our
1287 * sessiond will not have access to wake us up (there is
1288 * probably a rogue process trying to fake our
1289 * sessiond). Fallback to polling method in this case.
1290 */
1291 ret = fstat(wait_shm_fd, &statbuf);
1292 if (ret) {
1293 PERROR("fstat");
1294 goto error_close;
1295 }
1296 if (statbuf.st_uid != getuid())
1297 goto error_close;
1298 }
58d4b2a2 1299 return wait_shm_fd;
33bbeb90
MD
1300
1301error_close:
1302 ret = close(wait_shm_fd);
1303 if (ret) {
1304 PERROR("Error closing fd");
1305 }
1306 return -1;
58d4b2a2
MD
1307}
1308
1309static
1310char *get_map_shm(struct sock_info *sock_info)
1311{
172d6b68 1312 long page_size;
58d4b2a2
MD
1313 int wait_shm_fd, ret;
1314 char *wait_shm_mmap;
1315
172d6b68 1316 page_size = sysconf(_SC_PAGE_SIZE);
2657d1ba
MD
1317 if (page_size <= 0) {
1318 if (!page_size) {
1319 errno = EINVAL;
1320 }
1321 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
172d6b68
MD
1322 goto error;
1323 }
1324
6548fca4 1325 lttng_ust_lock_fd_tracker();
172d6b68 1326 wait_shm_fd = get_wait_shm(sock_info, page_size);
58d4b2a2 1327 if (wait_shm_fd < 0) {
6548fca4 1328 lttng_ust_unlock_fd_tracker();
58d4b2a2 1329 goto error;
44e073f5 1330 }
fc9a0ccb
JR
1331
1332 ret = lttng_ust_add_fd_to_tracker(wait_shm_fd);
1333 if (ret < 0) {
1334 ret = close(wait_shm_fd);
1335 if (!ret) {
1336 PERROR("Error closing fd");
1337 }
1338 lttng_ust_unlock_fd_tracker();
1339 goto error;
1340 }
1341
1342 wait_shm_fd = ret;
6548fca4
MD
1343 lttng_ust_unlock_fd_tracker();
1344
172d6b68 1345 wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
7fc90dca 1346 MAP_SHARED, wait_shm_fd, 0);
6548fca4 1347
7fc90dca 1348 /* close shm fd immediately after taking the mmap reference */
6548fca4 1349 lttng_ust_lock_fd_tracker();
7fc90dca 1350 ret = close(wait_shm_fd);
6548fca4
MD
1351 if (!ret) {
1352 lttng_ust_delete_fd_from_tracker(wait_shm_fd);
1353 } else {
33bbeb90
MD
1354 PERROR("Error closing fd");
1355 }
6548fca4
MD
1356 lttng_ust_unlock_fd_tracker();
1357
33bbeb90
MD
1358 if (wait_shm_mmap == MAP_FAILED) {
1359 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1360 goto error;
7fc90dca
MD
1361 }
1362 return wait_shm_mmap;
1363
1364error:
1365 return NULL;
1366}
1367
1368static
1369void wait_for_sessiond(struct sock_info *sock_info)
1370{
2eab9f28 1371 /* Use ust_lock to check if we should quit. */
3327ac33 1372 if (ust_lock()) {
7fc90dca
MD
1373 goto quit;
1374 }
37ed587a
MD
1375 if (wait_poll_fallback) {
1376 goto error;
1377 }
7fc90dca
MD
1378 ust_unlock();
1379
2eab9f28
JR
1380 assert(sock_info->wait_shm_mmap);
1381
7fc90dca 1382 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b 1383 /* Wait for futex wakeup */
ee7fcec8
MD
1384 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap))
1385 goto end_wait;
1386
1387 while (futex_async((int32_t *) sock_info->wait_shm_mmap,
1388 FUTEX_WAIT, 0, NULL, NULL, 0)) {
1389 switch (errno) {
1390 case EWOULDBLOCK:
1391 /* Value already changed. */
1392 goto end_wait;
1393 case EINTR:
1394 /* Retry if interrupted by signal. */
1395 break; /* Get out of switch. */
1396 case EFAULT:
1397 wait_poll_fallback = 1;
1398 DBG(
37ed587a
MD
1399"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1400"do not support FUTEX_WAKE on read-only memory mappings correctly. "
1401"Please upgrade your kernel "
1402"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1403"mainline). LTTng-UST will use polling mode fallback.");
ee7fcec8
MD
1404 if (ust_debug())
1405 PERROR("futex");
1406 goto end_wait;
80e2814b
MD
1407 }
1408 }
ee7fcec8 1409end_wait:
7fc90dca
MD
1410 return;
1411
1412quit:
1413 ust_unlock();
1414 return;
1415
1416error:
1417 ust_unlock();
7fc90dca 1418 return;
46050b1a
MD
1419}
1420
1ea11eab
MD
1421/*
1422 * This thread does not allocate any resource, except within
1423 * handle_message, within mutex protection. This mutex protects against
1424 * fork and exit.
98bf993f 1425 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
1426 * is also protected by the mutex.
1427 */
d9e99d10
MD
1428static
1429void *ust_listener_thread(void *arg)
1430{
1ea11eab 1431 struct sock_info *sock_info = arg;
fc9a0ccb 1432 int sock, ret, prev_connect_failed = 0, has_waited = 0, fd;
ff517991 1433 long timeout;
d9e99d10 1434
c362addf 1435 lttng_ust_fixup_tls();
01f0e40c
RB
1436 /*
1437 * If available, add '-ust' to the end of this thread's
1438 * process name
1439 */
1440 ret = lttng_ust_setustprocname();
1441 if (ret) {
1442 ERR("Unable to set UST process name");
1443 }
1444
9eb62b9c
MD
1445 /* Restart trying to connect to the session daemon */
1446restart:
c0eedf81
MD
1447 if (prev_connect_failed) {
1448 /* Wait for sessiond availability with pipe */
1449 wait_for_sessiond(sock_info);
1450 if (has_waited) {
1451 has_waited = 0;
1452 /*
1453 * Sleep for 5 seconds before retrying after a
1454 * sequence of failure / wait / failure. This
1455 * deals with a killed or broken session daemon.
1456 */
1457 sleep(5);
eacc4aa4
MD
1458 } else {
1459 has_waited = 1;
c0eedf81 1460 }
c0eedf81
MD
1461 prev_connect_failed = 0;
1462 }
9eb62b9c 1463
005aeac5
JR
1464 if (ust_lock()) {
1465 goto quit;
1466 }
1467
1ea11eab 1468 if (sock_info->socket != -1) {
6548fca4 1469 /* FD tracker is updated by ustcomm_close_unix_sock() */
e6973a89 1470 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 1471 if (ret) {
32ce8569
MD
1472 ERR("Error closing %s ust cmd socket",
1473 sock_info->name);
1ea11eab
MD
1474 }
1475 sock_info->socket = -1;
1476 }
32ce8569 1477 if (sock_info->notify_socket != -1) {
6548fca4 1478 /* FD tracker is updated by ustcomm_close_unix_sock() */
32ce8569
MD
1479 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1480 if (ret) {
1481 ERR("Error closing %s ust notify socket",
1482 sock_info->name);
1483 }
1484 sock_info->notify_socket = -1;
1485 }
46050b1a 1486
6548fca4 1487
321f2351
MD
1488 /*
1489 * Register. We need to perform both connect and sending
1490 * registration message before doing the next connect otherwise
1491 * we may reach unix socket connect queue max limits and block
1492 * on the 2nd connect while the session daemon is awaiting the
1493 * first connect registration message.
1494 */
1495 /* Connect cmd socket */
6548fca4 1496 lttng_ust_lock_fd_tracker();
451d66b2
MD
1497 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1498 get_connect_sock_timeout());
321f2351 1499 if (ret < 0) {
6548fca4 1500 lttng_ust_unlock_fd_tracker();
321f2351
MD
1501 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1502 prev_connect_failed = 1;
5b14aab3 1503
e3426ddc 1504 /*
321f2351
MD
1505 * If we cannot find the sessiond daemon, don't delay
1506 * constructor execution.
e3426ddc 1507 */
3f5339b0 1508 ret = handle_register_failed(sock_info);
321f2351
MD
1509 assert(!ret);
1510 ust_unlock();
1511 goto restart;
27fe9f21 1512 }
fc9a0ccb
JR
1513 fd = ret;
1514 ret = lttng_ust_add_fd_to_tracker(fd);
1515 if (ret < 0) {
1516 ret = close(fd);
1517 if (ret) {
1518 PERROR("close on sock_info->socket");
1519 }
1520 ret = -1;
1521 lttng_ust_unlock_fd_tracker();
1522 ust_unlock();
1523 goto quit;
1524 }
1525
321f2351 1526 sock_info->socket = ret;
fc9a0ccb 1527 lttng_ust_unlock_fd_tracker();
27fe9f21 1528
6548fca4
MD
1529 ust_unlock();
1530 /*
1531 * Unlock/relock ust lock because connect is blocking (with
1532 * timeout). Don't delay constructors on the ust lock for too
1533 * long.
1534 */
3327ac33 1535 if (ust_lock()) {
5b14aab3
MD
1536 goto quit;
1537 }
1538
46050b1a
MD
1539 /*
1540 * Create only one root handle per listener thread for the whole
f59ed768
MD
1541 * process lifetime, so we ensure we get ID which is statically
1542 * assigned to the root handle.
46050b1a
MD
1543 */
1544 if (sock_info->root_handle == -1) {
1545 ret = lttng_abi_create_root_handle();
a51070bb 1546 if (ret < 0) {
46050b1a 1547 ERR("Error creating root handle");
46050b1a
MD
1548 goto quit;
1549 }
1550 sock_info->root_handle = ret;
9eb62b9c 1551 }
1ea11eab 1552
32ce8569 1553 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
9eb62b9c 1554 if (ret < 0) {
32ce8569
MD
1555 ERR("Error registering to %s ust cmd socket",
1556 sock_info->name);
c0eedf81 1557 prev_connect_failed = 1;
11ff9c7d
MD
1558 /*
1559 * If we cannot register to the sessiond daemon, don't
1560 * delay constructor execution.
1561 */
3f5339b0 1562 ret = handle_register_failed(sock_info);
11ff9c7d 1563 assert(!ret);
17dfb34b 1564 ust_unlock();
9eb62b9c
MD
1565 goto restart;
1566 }
321f2351
MD
1567
1568 ust_unlock();
6548fca4
MD
1569 /*
1570 * Unlock/relock ust lock because connect is blocking (with
1571 * timeout). Don't delay constructors on the ust lock for too
1572 * long.
1573 */
1574 if (ust_lock()) {
1575 goto quit;
1576 }
321f2351
MD
1577
1578 /* Connect notify socket */
6548fca4 1579 lttng_ust_lock_fd_tracker();
451d66b2
MD
1580 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1581 get_connect_sock_timeout());
321f2351 1582 if (ret < 0) {
6548fca4 1583 lttng_ust_unlock_fd_tracker();
321f2351
MD
1584 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1585 prev_connect_failed = 1;
1586
321f2351
MD
1587 /*
1588 * If we cannot find the sessiond daemon, don't delay
1589 * constructor execution.
1590 */
3f5339b0 1591 ret = handle_register_failed(sock_info);
321f2351
MD
1592 assert(!ret);
1593 ust_unlock();
1594 goto restart;
1595 }
fc9a0ccb
JR
1596
1597 fd = ret;
1598 ret = lttng_ust_add_fd_to_tracker(fd);
1599 if (ret < 0) {
1600 ret = close(fd);
1601 if (ret) {
1602 PERROR("close on sock_info->notify_socket");
1603 }
1604 ret = -1;
1605 lttng_ust_unlock_fd_tracker();
1606 ust_unlock();
1607 goto quit;
1608 }
1609
321f2351 1610 sock_info->notify_socket = ret;
fc9a0ccb 1611 lttng_ust_unlock_fd_tracker();
321f2351 1612
6548fca4
MD
1613 ust_unlock();
1614 /*
1615 * Unlock/relock ust lock because connect is blocking (with
1616 * timeout). Don't delay constructors on the ust lock for too
1617 * long.
1618 */
1619 if (ust_lock()) {
1620 goto quit;
1621 }
1622
321f2351
MD
1623 timeout = get_notify_sock_timeout();
1624 if (timeout >= 0) {
1625 /*
1626 * Give at least 10ms to sessiond to reply to
1627 * notifications.
1628 */
1629 if (timeout < 10)
1630 timeout = 10;
1631 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1632 timeout);
1633 if (ret < 0) {
1634 WARN("Error setting socket receive timeout");
1635 }
1636 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1637 timeout);
1638 if (ret < 0) {
1639 WARN("Error setting socket send timeout");
1640 }
1641 } else if (timeout < -1) {
1642 WARN("Unsupported timeout value %ld", timeout);
1643 }
1644
32ce8569
MD
1645 ret = register_to_sessiond(sock_info->notify_socket,
1646 USTCTL_SOCKET_NOTIFY);
1647 if (ret < 0) {
1648 ERR("Error registering to %s ust notify socket",
1649 sock_info->name);
1650 prev_connect_failed = 1;
1651 /*
1652 * If we cannot register to the sessiond daemon, don't
1653 * delay constructor execution.
1654 */
3f5339b0 1655 ret = handle_register_failed(sock_info);
32ce8569
MD
1656 assert(!ret);
1657 ust_unlock();
1658 goto restart;
1659 }
1660 sock = sock_info->socket;
1661
17dfb34b 1662 ust_unlock();
46050b1a 1663
d9e99d10
MD
1664 for (;;) {
1665 ssize_t len;
57773204 1666 struct ustcomm_ust_msg lum;
d9e99d10 1667
57773204 1668 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
1669 switch (len) {
1670 case 0: /* orderly shutdown */
7dd08bec 1671 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
3327ac33 1672 if (ust_lock()) {
d5e1fea6
MD
1673 goto quit;
1674 }
8236ba10
MD
1675 /*
1676 * Either sessiond has shutdown or refused us by closing the socket.
1677 * In either case, we don't want to delay construction execution,
1678 * and we need to wait before retry.
1679 */
1680 prev_connect_failed = 1;
1681 /*
1682 * If we cannot register to the sessiond daemon, don't
1683 * delay constructor execution.
1684 */
3f5339b0 1685 ret = handle_register_failed(sock_info);
8236ba10
MD
1686 assert(!ret);
1687 ust_unlock();
d9e99d10 1688 goto end;
e7723462 1689 case sizeof(lum):
74d81a6c 1690 print_cmd(lum.cmd, lum.handle);
11ff9c7d 1691 ret = handle_message(sock_info, sock, &lum);
7bc53e94 1692 if (ret) {
0dafcd63
MD
1693 ERR("Error handling message for %s socket",
1694 sock_info->name);
1695 /*
1696 * Close socket if protocol error is
1697 * detected.
1698 */
1699 goto end;
d9e99d10
MD
1700 }
1701 continue;
7bc53e94
MD
1702 default:
1703 if (len < 0) {
1704 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1705 } else {
1706 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1707 }
1708 if (len == -ECONNRESET) {
1709 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
1710 goto end;
1711 }
1712 goto end;
d9e99d10
MD
1713 }
1714
1715 }
1716end:
3327ac33 1717 if (ust_lock()) {
d5e1fea6
MD
1718 goto quit;
1719 }
f59ed768
MD
1720 /* Cleanup socket handles before trying to reconnect */
1721 lttng_ust_objd_table_owner_cleanup(sock_info);
1722 ust_unlock();
9eb62b9c 1723 goto restart; /* try to reconnect */
e33f3265 1724
1ea11eab 1725quit:
e33f3265 1726 ust_unlock();
3327ac33
MD
1727
1728 pthread_mutex_lock(&ust_exit_mutex);
1729 sock_info->thread_active = 0;
1730 pthread_mutex_unlock(&ust_exit_mutex);
d9e99d10
MD
1731 return NULL;
1732}
1733
2594a5b4
MD
1734/*
1735 * Weak symbol to call when the ust malloc wrapper is not loaded.
1736 */
1737__attribute__((weak))
1738void lttng_ust_malloc_wrapper_init(void)
1739{
1740}
1741
2691221a
MD
1742/*
1743 * sessiond monitoring thread: monitor presence of global and per-user
1744 * sessiond by polling the application common named pipe.
1745 */
edaa1431 1746void __attribute__((constructor)) lttng_ust_init(void)
2691221a 1747{
11ff9c7d 1748 struct timespec constructor_timeout;
ae6a58bf 1749 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 1750 pthread_attr_t thread_attr;
cf12a773 1751 int timeout_mode;
2691221a
MD
1752 int ret;
1753
edaa1431
MD
1754 if (uatomic_xchg(&initialized, 1) == 1)
1755 return;
1756
eddd8d5d
MD
1757 /*
1758 * Fixup interdependency between TLS fixup mutex (which happens
1759 * to be the dynamic linker mutex) and ust_lock, taken within
1760 * the ust lock.
1761 */
c362addf 1762 lttng_ust_fixup_tls();
eddd8d5d 1763
07b57e5e
MD
1764 lttng_ust_loaded = 1;
1765
edaa1431
MD
1766 /*
1767 * We want precise control over the order in which we construct
1768 * our sub-libraries vs starting to receive commands from
1769 * sessiond (otherwise leading to errors when trying to create
1770 * sessiond before the init functions are completed).
1771 */
2691221a 1772 init_usterr();
49254388 1773 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
edaa1431 1774 init_tracepoint();
6548fca4 1775 lttng_ust_init_fd_tracker();
f9364363 1776 lttng_ust_clock_init();
5e1b7b8b 1777 lttng_ust_getcpu_init();
cf73e0fe 1778 lttng_ust_statedump_init();
7dd08bec
MD
1779 lttng_ring_buffer_metadata_client_init();
1780 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1781 lttng_ring_buffer_client_overwrite_rt_init();
7dd08bec 1782 lttng_ring_buffer_client_discard_init();
34a91bdb 1783 lttng_ring_buffer_client_discard_rt_init();
d58d1454 1784 lttng_perf_counter_init();
2594a5b4
MD
1785 /*
1786 * Invoke ust malloc wrapper init before starting other threads.
1787 */
1788 lttng_ust_malloc_wrapper_init();
2691221a 1789
ff517991 1790 timeout_mode = get_constructor_timeout(&constructor_timeout);
11ff9c7d 1791
95259bd0 1792 ret = sem_init(&constructor_wait, 0, 0);
8aadb54a
MD
1793 if (ret) {
1794 PERROR("sem_init");
1795 }
11ff9c7d 1796
2eab9f28
JR
1797 ret = setup_global_apps();
1798 if (ret) {
1799 assert(global_apps.allowed == 0);
1800 DBG("global apps setup returned %d", ret);
1801 }
1802
8d20bf54 1803 ret = setup_local_apps();
2691221a 1804 if (ret) {
2eab9f28 1805 assert(local_apps.allowed == 0);
9ec6895c 1806 DBG("local apps setup returned %d", ret);
2691221a 1807 }
ae6a58bf
WP
1808
1809 /* A new thread created by pthread_create inherits the signal mask
1810 * from the parent. To avoid any signal being received by the
1811 * listener thread, we block all signals temporarily in the parent,
1812 * while we create the listener thread.
1813 */
1814 sigfillset(&sig_all_blocked);
1815 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1816 if (ret) {
d94d802c 1817 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1818 }
1819
1879f67f
MG
1820 ret = pthread_attr_init(&thread_attr);
1821 if (ret) {
1822 ERR("pthread_attr_init: %s", strerror(ret));
1823 }
1824 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1825 if (ret) {
1826 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1827 }
1828
2eab9f28
JR
1829 if (global_apps.allowed) {
1830 pthread_mutex_lock(&ust_exit_mutex);
1831 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
1832 ust_listener_thread, &global_apps);
1833 if (ret) {
1834 ERR("pthread_create global: %s", strerror(ret));
1835 }
1836 global_apps.thread_active = 1;
1837 pthread_mutex_unlock(&ust_exit_mutex);
1838 } else {
1839 handle_register_done(&global_apps);
d94d802c 1840 }
e33f3265 1841
8d20bf54 1842 if (local_apps.allowed) {
c0bbbd5a 1843 pthread_mutex_lock(&ust_exit_mutex);
1879f67f 1844 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 1845 ust_listener_thread, &local_apps);
d94d802c
MD
1846 if (ret) {
1847 ERR("pthread_create local: %s", strerror(ret));
1848 }
e33f3265 1849 local_apps.thread_active = 1;
c0bbbd5a 1850 pthread_mutex_unlock(&ust_exit_mutex);
8d20bf54
MD
1851 } else {
1852 handle_register_done(&local_apps);
1853 }
1879f67f
MG
1854 ret = pthread_attr_destroy(&thread_attr);
1855 if (ret) {
1856 ERR("pthread_attr_destroy: %s", strerror(ret));
1857 }
8d20bf54 1858
ae6a58bf
WP
1859 /* Restore original signal mask in parent */
1860 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1861 if (ret) {
d94d802c 1862 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1863 }
1864
cf12a773
MD
1865 switch (timeout_mode) {
1866 case 1: /* timeout wait */
95259bd0
MD
1867 do {
1868 ret = sem_timedwait(&constructor_wait,
1869 &constructor_timeout);
1870 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
1871 if (ret < 0) {
1872 switch (errno) {
1873 case ETIMEDOUT:
1874 ERR("Timed out waiting for lttng-sessiond");
1875 break;
1876 case EINVAL:
1877 PERROR("sem_timedwait");
1878 break;
1879 default:
1880 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1881 strerror(errno));
1882 }
cf12a773
MD
1883 }
1884 break;
7b766b16 1885 case -1:/* wait forever */
95259bd0
MD
1886 do {
1887 ret = sem_wait(&constructor_wait);
1888 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
1889 if (ret < 0) {
1890 switch (errno) {
1891 case EINVAL:
1892 PERROR("sem_wait");
1893 break;
1894 default:
1895 ERR("Unexpected error \"%s\" returned by sem_wait",
1896 strerror(errno));
1897 }
1898 }
cf12a773 1899 break;
7b766b16 1900 case 0: /* no timeout */
cf12a773 1901 break;
11ff9c7d 1902 }
2691221a
MD
1903}
1904
17dfb34b
MD
1905static
1906void lttng_ust_cleanup(int exiting)
1907{
efe0de09 1908 cleanup_sock_info(&global_apps, exiting);
932cfadb 1909 cleanup_sock_info(&local_apps, exiting);
74f98bc9 1910 local_apps.allowed = 0;
2eab9f28 1911 global_apps.allowed = 0;
efe0de09
MD
1912 /*
1913 * The teardown in this function all affect data structures
1914 * accessed under the UST lock by the listener thread. This
1915 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1916 * that none of these threads are accessing this data at this
1917 * point.
1918 */
17dfb34b 1919 lttng_ust_abi_exit();
003fedf4 1920 lttng_ust_events_exit();
d58d1454 1921 lttng_perf_counter_exit();
34a91bdb 1922 lttng_ring_buffer_client_discard_rt_exit();
7dd08bec 1923 lttng_ring_buffer_client_discard_exit();
34a91bdb 1924 lttng_ring_buffer_client_overwrite_rt_exit();
7dd08bec
MD
1925 lttng_ring_buffer_client_overwrite_exit();
1926 lttng_ring_buffer_metadata_client_exit();
cf73e0fe 1927 lttng_ust_statedump_destroy();
17dfb34b
MD
1928 exit_tracepoint();
1929 if (!exiting) {
1930 /* Reinitialize values for fork */
3f5339b0 1931 sem_count = sem_count_initial_value;
17dfb34b
MD
1932 lttng_ust_comm_should_quit = 0;
1933 initialized = 0;
1934 }
1935}
1936
edaa1431 1937void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1938{
1939 int ret;
1940
9eb62b9c
MD
1941 /*
1942 * Using pthread_cancel here because:
1943 * A) we don't want to hang application teardown.
1944 * B) the thread is not allocating any resource.
1945 */
1ea11eab
MD
1946
1947 /*
1948 * Require the communication thread to quit. Synchronize with
1949 * mutexes to ensure it is not in a mutex critical section when
1950 * pthread_cancel is later called.
1951 */
3327ac33 1952 ust_lock_nocheck();
1ea11eab 1953 lttng_ust_comm_should_quit = 1;
3327ac33 1954 ust_unlock();
1ea11eab 1955
3327ac33 1956 pthread_mutex_lock(&ust_exit_mutex);
f5f94532 1957 /* cancel threads */
e33f3265
MD
1958 if (global_apps.thread_active) {
1959 ret = pthread_cancel(global_apps.ust_listener);
1960 if (ret) {
1961 ERR("Error cancelling global ust listener thread: %s",
1962 strerror(ret));
1963 } else {
1964 global_apps.thread_active = 0;
1965 }
2691221a 1966 }
e33f3265 1967 if (local_apps.thread_active) {
8d20bf54
MD
1968 ret = pthread_cancel(local_apps.ust_listener);
1969 if (ret) {
d94d802c
MD
1970 ERR("Error cancelling local ust listener thread: %s",
1971 strerror(ret));
e33f3265
MD
1972 } else {
1973 local_apps.thread_active = 0;
8d20bf54 1974 }
8d20bf54 1975 }
3327ac33 1976 pthread_mutex_unlock(&ust_exit_mutex);
e33f3265 1977
efe0de09
MD
1978 /*
1979 * Do NOT join threads: use of sys_futex makes it impossible to
1980 * join the threads without using async-cancel, but async-cancel
1981 * is delivered by a signal, which could hit the target thread
1982 * anywhere in its code path, including while the ust_lock() is
1983 * held, causing a deadlock for the other thread. Let the OS
1984 * cleanup the threads if there are stalled in a syscall.
1985 */
17dfb34b 1986 lttng_ust_cleanup(1);
2691221a 1987}
e822f505
MD
1988
1989/*
1990 * We exclude the worker threads across fork and clone (except
1991 * CLONE_VM), because these system calls only keep the forking thread
1992 * running in the child. Therefore, we don't want to call fork or clone
1993 * in the middle of an tracepoint or ust tracing state modification.
1994 * Holding this mutex protects these structures across fork and clone.
1995 */
b728d87e 1996void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
1997{
1998 /*
1999 * Disable signals. This is to avoid that the child intervenes
2000 * before it is properly setup for tracing. It is safer to
2001 * disable all signals, because then we know we are not breaking
2002 * anything by restoring the original mask.
2003 */
2004 sigset_t all_sigs;
2005 int ret;
2006
c362addf
MD
2007 /* Fixup lttng-ust TLS. */
2008 lttng_ust_fixup_tls();
2009
8c90a710 2010 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2011 return;
e822f505
MD
2012 /* Disable signals */
2013 sigfillset(&all_sigs);
b728d87e 2014 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
2015 if (ret == -1) {
2016 PERROR("sigprocmask");
2017 }
458d678c
PW
2018
2019 pthread_mutex_lock(&ust_fork_mutex);
2020
3327ac33 2021 ust_lock_nocheck();
e822f505
MD
2022 rcu_bp_before_fork();
2023}
2024
b728d87e 2025static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
2026{
2027 int ret;
2028
17dfb34b
MD
2029 DBG("process %d", getpid());
2030 ust_unlock();
458d678c
PW
2031
2032 pthread_mutex_unlock(&ust_fork_mutex);
2033
e822f505 2034 /* Restore signals */
23c8854a 2035 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
2036 if (ret == -1) {
2037 PERROR("sigprocmask");
2038 }
2039}
2040
b728d87e 2041void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 2042{
8c90a710 2043 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2044 return;
17dfb34b 2045 DBG("process %d", getpid());
e822f505
MD
2046 rcu_bp_after_fork_parent();
2047 /* Release mutexes and reenable signals */
b728d87e 2048 ust_after_fork_common(restore_sigset);
e822f505
MD
2049}
2050
17dfb34b
MD
2051/*
2052 * After fork, in the child, we need to cleanup all the leftover state,
2053 * except the worker thread which already magically disappeared thanks
2054 * to the weird Linux fork semantics. After tyding up, we call
2055 * lttng_ust_init() again to start over as a new PID.
2056 *
2057 * This is meant for forks() that have tracing in the child between the
2058 * fork and following exec call (if there is any).
2059 */
b728d87e 2060void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 2061{
8c90a710 2062 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2063 return;
77971c85 2064 lttng_context_vpid_reset();
8478887d 2065 lttng_context_vtid_reset();
efe79472 2066 lttng_context_procname_reset();
17dfb34b 2067 DBG("process %d", getpid());
e822f505
MD
2068 /* Release urcu mutexes */
2069 rcu_bp_after_fork_child();
17dfb34b 2070 lttng_ust_cleanup(0);
e822f505 2071 /* Release mutexes and reenable signals */
b728d87e 2072 ust_after_fork_common(restore_sigset);
318dfea9 2073 lttng_ust_init();
e822f505 2074}
95c25348 2075
246be17e 2076void lttng_ust_sockinfo_session_enabled(void *owner)
95c25348
PW
2077{
2078 struct sock_info *sock_info = owner;
37dddb65 2079 sock_info->statedump_pending = 1;
95c25348 2080}
This page took 0.147017 seconds and 4 git commands to generate.