Fix: don't wait for initial statedump when 0 session active
[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);
c41cdc2a
MD
640 if (!sock_info->statedump_pending) {
641 sock_info->initial_statedump_done = 1;
642 decrement_sem_count(1);
643 }
3f5339b0
GAPG
644
645 return 0;
646}
647
648static
649int handle_register_failed(struct sock_info *sock_info)
650{
651 if (sock_info->registration_done)
652 return 0;
653 sock_info->registration_done = 1;
654 sock_info->initial_statedump_done = 1;
655
656 decrement_sem_count(2);
657
11ff9c7d
MD
658 return 0;
659}
660
37dddb65
MD
661/*
662 * Only execute pending statedump after the constructor semaphore has
3f5339b0
GAPG
663 * been posted by the current listener thread. This means statedump will
664 * only be performed after the "registration done" command is received
665 * from this thread's session daemon.
37dddb65
MD
666 *
667 * This ensures we don't run into deadlock issues with the dynamic
668 * loader mutex, which is held while the constructor is called and
669 * waiting on the constructor semaphore. All operations requiring this
670 * dynamic loader lock need to be postponed using this mechanism.
3f5339b0
GAPG
671 *
672 * In a scenario with two session daemons connected to the application,
673 * it is possible that the first listener thread which receives the
674 * registration done command issues its statedump while the dynamic
675 * loader lock is still held by the application constructor waiting on
676 * the semaphore. It will however be allowed to proceed when the
677 * second session daemon sends the registration done command to the
678 * second listener thread. This situation therefore does not produce
679 * a deadlock.
37dddb65
MD
680 */
681static
682void handle_pending_statedump(struct sock_info *sock_info)
683{
3f5339b0 684 if (sock_info->registration_done && sock_info->statedump_pending) {
37dddb65 685 sock_info->statedump_pending = 0;
2932a87f 686 pthread_mutex_lock(&ust_fork_mutex);
37dddb65 687 lttng_handle_pending_statedump(sock_info);
458d678c 688 pthread_mutex_unlock(&ust_fork_mutex);
3f5339b0
GAPG
689
690 if (!sock_info->initial_statedump_done) {
691 sock_info->initial_statedump_done = 1;
692 decrement_sem_count(1);
693 }
37dddb65
MD
694 }
695}
696
11ff9c7d
MD
697static
698int handle_message(struct sock_info *sock_info,
57773204 699 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 700{
1ea11eab 701 int ret = 0;
b61ce3b2 702 const struct lttng_ust_objd_ops *ops;
57773204 703 struct ustcomm_ust_reply lur;
ef9ff354 704 union ust_args args;
8e696cfa 705 char ctxstr[LTTNG_UST_SYM_NAME_LEN]; /* App context string. */
40003310 706 ssize_t len;
1ea11eab 707
46050b1a
MD
708 memset(&lur, 0, sizeof(lur));
709
3327ac33 710 if (ust_lock()) {
74d81a6c 711 ret = -LTTNG_UST_ERR_EXITING;
0dafcd63 712 goto error;
1ea11eab 713 }
9eb62b9c 714
46050b1a
MD
715 ops = objd_ops(lum->handle);
716 if (!ops) {
717 ret = -ENOENT;
0dafcd63 718 goto error;
1ea11eab 719 }
46050b1a
MD
720
721 switch (lum->cmd) {
11ff9c7d
MD
722 case LTTNG_UST_REGISTER_DONE:
723 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 724 ret = handle_register_done(sock_info);
11ff9c7d
MD
725 else
726 ret = -EINVAL;
727 break;
46050b1a
MD
728 case LTTNG_UST_RELEASE:
729 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
730 ret = -EPERM;
731 else
1849ef7c 732 ret = lttng_ust_objd_unref(lum->handle, 1);
d9e99d10 733 break;
2d78951a
MD
734 case LTTNG_UST_FILTER:
735 {
736 /* Receive filter data */
f488575f 737 struct lttng_ust_filter_bytecode_node *bytecode;
2d78951a 738
cd54f6d9 739 if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
7bc53e94 740 ERR("Filter data size is too large: %u bytes",
2d78951a
MD
741 lum->u.filter.data_size);
742 ret = -EINVAL;
743 goto error;
744 }
2734ca65 745
885b1dfd 746 if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
7bc53e94 747 ERR("Filter reloc offset %u is not within data",
2734ca65
CB
748 lum->u.filter.reloc_offset);
749 ret = -EINVAL;
750 goto error;
751 }
752
cd54f6d9
MD
753 bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size);
754 if (!bytecode) {
755 ret = -ENOMEM;
756 goto error;
757 }
f488575f 758 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data,
2d78951a
MD
759 lum->u.filter.data_size);
760 switch (len) {
761 case 0: /* orderly shutdown */
762 ret = 0;
cd54f6d9 763 free(bytecode);
2d78951a 764 goto error;
2d78951a
MD
765 default:
766 if (len == lum->u.filter.data_size) {
7bc53e94 767 DBG("filter data received");
2d78951a 768 break;
7bc53e94
MD
769 } else if (len < 0) {
770 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
771 if (len == -ECONNRESET) {
772 ERR("%s remote end closed connection", sock_info->name);
773 ret = len;
774 free(bytecode);
775 goto error;
776 }
777 ret = len;
eb8bf361 778 free(bytecode);
0dafcd63 779 goto error;
2d78951a 780 } else {
7bc53e94 781 DBG("incorrect filter data message size: %zd", len);
2d78951a 782 ret = -EINVAL;
cd54f6d9 783 free(bytecode);
0dafcd63 784 goto error;
2d78951a
MD
785 }
786 }
f488575f
MD
787 bytecode->bc.len = lum->u.filter.data_size;
788 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
3f6fd224 789 bytecode->bc.seqnum = lum->u.filter.seqnum;
cd54f6d9 790 if (ops->cmd) {
2d78951a 791 ret = ops->cmd(lum->handle, lum->cmd,
cd54f6d9 792 (unsigned long) bytecode,
f59ed768 793 &args, sock_info);
cd54f6d9
MD
794 if (ret) {
795 free(bytecode);
796 }
797 /* don't free bytecode if everything went fine. */
798 } else {
2d78951a 799 ret = -ENOSYS;
cd54f6d9
MD
800 free(bytecode);
801 }
2d78951a
MD
802 break;
803 }
86e36163
JI
804 case LTTNG_UST_EXCLUSION:
805 {
806 /* Receive exclusion names */
807 struct lttng_ust_excluder_node *node;
808 unsigned int count;
809
810 count = lum->u.exclusion.count;
811 if (count == 0) {
812 /* There are no names to read */
813 ret = 0;
814 goto error;
815 }
816 node = zmalloc(sizeof(*node) +
817 count * LTTNG_UST_SYM_NAME_LEN);
818 if (!node) {
819 ret = -ENOMEM;
820 goto error;
821 }
822 node->excluder.count = count;
823 len = ustcomm_recv_unix_sock(sock, node->excluder.names,
824 count * LTTNG_UST_SYM_NAME_LEN);
825 switch (len) {
826 case 0: /* orderly shutdown */
827 ret = 0;
828 free(node);
829 goto error;
830 default:
831 if (len == count * LTTNG_UST_SYM_NAME_LEN) {
832 DBG("Exclusion data received");
833 break;
834 } else if (len < 0) {
835 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
836 if (len == -ECONNRESET) {
837 ERR("%s remote end closed connection", sock_info->name);
838 ret = len;
839 free(node);
840 goto error;
841 }
842 ret = len;
843 free(node);
0dafcd63 844 goto error;
86e36163
JI
845 } else {
846 DBG("Incorrect exclusion data message size: %zd", len);
847 ret = -EINVAL;
848 free(node);
0dafcd63 849 goto error;
86e36163
JI
850 }
851 }
852 if (ops->cmd) {
853 ret = ops->cmd(lum->handle, lum->cmd,
854 (unsigned long) node,
855 &args, sock_info);
856 if (ret) {
857 free(node);
858 }
859 /* Don't free exclusion data if everything went fine. */
860 } else {
861 ret = -ENOSYS;
862 free(node);
863 }
864 break;
865 }
74d81a6c
MD
866 case LTTNG_UST_CHANNEL:
867 {
868 void *chan_data;
ff0f5728 869 int wakeup_fd;
74d81a6c
MD
870
871 len = ustcomm_recv_channel_from_sessiond(sock,
ff0f5728
MD
872 &chan_data, lum->u.channel.len,
873 &wakeup_fd);
74d81a6c
MD
874 switch (len) {
875 case 0: /* orderly shutdown */
876 ret = 0;
877 goto error;
878 default:
879 if (len == lum->u.channel.len) {
880 DBG("channel data received");
881 break;
882 } else if (len < 0) {
883 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
884 if (len == -ECONNRESET) {
885 ERR("%s remote end closed connection", sock_info->name);
886 ret = len;
887 goto error;
888 }
889 ret = len;
0dafcd63 890 goto error;
74d81a6c
MD
891 } else {
892 DBG("incorrect channel data message size: %zd", len);
893 ret = -EINVAL;
0dafcd63 894 goto error;
74d81a6c
MD
895 }
896 }
897 args.channel.chan_data = chan_data;
ff0f5728 898 args.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
899 if (ops->cmd)
900 ret = ops->cmd(lum->handle, lum->cmd,
901 (unsigned long) &lum->u,
902 &args, sock_info);
903 else
904 ret = -ENOSYS;
905 break;
906 }
907 case LTTNG_UST_STREAM:
908 {
909 /* Receive shm_fd, wakeup_fd */
910 ret = ustcomm_recv_stream_from_sessiond(sock,
acafe8d8 911 NULL,
74d81a6c
MD
912 &args.stream.shm_fd,
913 &args.stream.wakeup_fd);
914 if (ret) {
0dafcd63 915 goto error;
74d81a6c 916 }
61973368 917
74d81a6c
MD
918 if (ops->cmd)
919 ret = ops->cmd(lum->handle, lum->cmd,
920 (unsigned long) &lum->u,
921 &args, sock_info);
922 else
923 ret = -ENOSYS;
924 break;
925 }
8e696cfa
MD
926 case LTTNG_UST_CONTEXT:
927 switch (lum->u.context.ctx) {
928 case LTTNG_UST_CONTEXT_APP_CONTEXT:
929 {
930 char *p;
931 size_t ctxlen, recvlen;
932
933 ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1
934 + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len;
935 if (ctxlen >= LTTNG_UST_SYM_NAME_LEN) {
936 ERR("Application context string length size is too large: %zu bytes",
937 ctxlen);
938 ret = -EINVAL;
939 goto error;
940 }
941 strcpy(ctxstr, "$app.");
942 p = &ctxstr[strlen("$app.")];
943 recvlen = ctxlen - strlen("$app.");
944 len = ustcomm_recv_unix_sock(sock, p, recvlen);
945 switch (len) {
946 case 0: /* orderly shutdown */
947 ret = 0;
948 goto error;
949 default:
950 if (len == recvlen) {
951 DBG("app context data received");
952 break;
953 } else if (len < 0) {
954 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
955 if (len == -ECONNRESET) {
956 ERR("%s remote end closed connection", sock_info->name);
957 ret = len;
958 goto error;
959 }
960 ret = len;
961 goto error;
962 } else {
963 DBG("incorrect app context data message size: %zd", len);
964 ret = -EINVAL;
965 goto error;
966 }
967 }
968 /* Put : between provider and ctxname. */
969 p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':';
970 args.app_context.ctxname = ctxstr;
971 break;
972 }
973 default:
974 break;
975 }
976 if (ops->cmd) {
977 ret = ops->cmd(lum->handle, lum->cmd,
978 (unsigned long) &lum->u,
979 &args, sock_info);
980 } else {
981 ret = -ENOSYS;
982 }
983 break;
d9e99d10 984 default:
46050b1a
MD
985 if (ops->cmd)
986 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 987 (unsigned long) &lum->u,
f59ed768 988 &args, sock_info);
46050b1a
MD
989 else
990 ret = -ENOSYS;
991 break;
d9e99d10 992 }
46050b1a 993
46050b1a
MD
994 lur.handle = lum->handle;
995 lur.cmd = lum->cmd;
996 lur.ret_val = ret;
997 if (ret >= 0) {
7bc53e94 998 lur.ret_code = LTTNG_UST_OK;
46050b1a 999 } else {
7bc53e94
MD
1000 /*
1001 * Use -LTTNG_UST_ERR as wildcard for UST internal
1002 * error that are not caused by the transport, except if
1003 * we already have a more precise error message to
1004 * report.
1005 */
64b2564e
DG
1006 if (ret > -LTTNG_UST_ERR) {
1007 /* Translate code to UST error. */
1008 switch (ret) {
1009 case -EEXIST:
1010 lur.ret_code = -LTTNG_UST_ERR_EXIST;
1011 break;
1012 case -EINVAL:
1013 lur.ret_code = -LTTNG_UST_ERR_INVAL;
1014 break;
1015 case -ENOENT:
1016 lur.ret_code = -LTTNG_UST_ERR_NOENT;
1017 break;
1018 case -EPERM:
1019 lur.ret_code = -LTTNG_UST_ERR_PERM;
1020 break;
1021 case -ENOSYS:
1022 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
1023 break;
1024 default:
1025 lur.ret_code = -LTTNG_UST_ERR;
1026 break;
1027 }
1028 } else {
7bc53e94 1029 lur.ret_code = ret;
64b2564e 1030 }
46050b1a 1031 }
e6ea14c5
MD
1032 if (ret >= 0) {
1033 switch (lum->cmd) {
e6ea14c5
MD
1034 case LTTNG_UST_TRACER_VERSION:
1035 lur.u.version = lum->u.version;
1036 break;
1037 case LTTNG_UST_TRACEPOINT_LIST_GET:
1038 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
1039 break;
1040 }
381c0f1e 1041 }
74d81a6c 1042 DBG("Return value: %d", lur.ret_val);
4c62d8d1
MD
1043
1044 ust_unlock();
1045
1046 /*
1047 * Performed delayed statedump operations outside of the UST
1048 * lock. We need to take the dynamic loader lock before we take
1049 * the UST lock internally within handle_pending_statedump().
1050 */
1051 handle_pending_statedump(sock_info);
1052
1053 if (ust_lock()) {
1054 ret = -LTTNG_UST_ERR_EXITING;
1055 goto error;
1056 }
1057
46050b1a 1058 ret = send_reply(sock, &lur);
193183fb 1059 if (ret < 0) {
7bc53e94 1060 DBG("error sending reply");
193183fb
MD
1061 goto error;
1062 }
46050b1a 1063
40003310
MD
1064 /*
1065 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1066 * after the reply.
1067 */
7bc53e94 1068 if (lur.ret_code == LTTNG_UST_OK) {
40003310
MD
1069 switch (lum->cmd) {
1070 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1071 len = ustcomm_send_unix_sock(sock,
1072 &args.field_list.entry,
1073 sizeof(args.field_list.entry));
7bc53e94
MD
1074 if (len < 0) {
1075 ret = len;
1076 goto error;
1077 }
40003310 1078 if (len != sizeof(args.field_list.entry)) {
7bc53e94 1079 ret = -EINVAL;
40003310
MD
1080 goto error;
1081 }
1082 }
1083 }
ef9ff354 1084
381c0f1e 1085error:
17dfb34b 1086 ust_unlock();
d9e99d10 1087
37dddb65 1088 return ret;
246be17e
PW
1089}
1090
46050b1a 1091static
efe0de09 1092void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
1093{
1094 int ret;
1095
5b14aab3
MD
1096 if (sock_info->root_handle != -1) {
1097 ret = lttng_ust_objd_unref(sock_info->root_handle, 1);
1098 if (ret) {
1099 ERR("Error unref root handle");
1100 }
1101 sock_info->root_handle = -1;
1102 }
3f5339b0
GAPG
1103 sock_info->registration_done = 0;
1104 sock_info->initial_statedump_done = 0;
5b14aab3
MD
1105
1106 /*
1107 * wait_shm_mmap, socket and notify socket are used by listener
1108 * threads outside of the ust lock, so we cannot tear them down
1109 * ourselves, because we cannot join on these threads. Leave
1110 * responsibility of cleaning up these resources to the OS
1111 * process exit.
1112 */
1113 if (exiting)
1114 return;
1115
46050b1a 1116 if (sock_info->socket != -1) {
e6973a89 1117 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 1118 if (ret) {
32ce8569 1119 ERR("Error closing ust cmd socket");
46050b1a
MD
1120 }
1121 sock_info->socket = -1;
1122 }
32ce8569
MD
1123 if (sock_info->notify_socket != -1) {
1124 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1125 if (ret) {
1126 ERR("Error closing ust notify socket");
1127 }
1128 sock_info->notify_socket = -1;
1129 }
5b14aab3 1130 if (sock_info->wait_shm_mmap) {
172d6b68
MD
1131 long page_size;
1132
1133 page_size = sysconf(_SC_PAGE_SIZE);
2657d1ba
MD
1134 if (page_size <= 0) {
1135 if (!page_size) {
1136 errno = EINVAL;
1137 }
1138 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1139 } else {
172d6b68
MD
1140 ret = munmap(sock_info->wait_shm_mmap, page_size);
1141 if (ret) {
1142 ERR("Error unmapping wait shm");
1143 }
7fc90dca
MD
1144 }
1145 sock_info->wait_shm_mmap = NULL;
1146 }
1147}
1148
58d4b2a2 1149/*
33bbeb90
MD
1150 * Using fork to set umask in the child process (not multi-thread safe).
1151 * We deal with the shm_open vs ftruncate race (happening when the
1152 * sessiond owns the shm and does not let everybody modify it, to ensure
1153 * safety against shm_unlink) by simply letting the mmap fail and
1154 * retrying after a few seconds.
1155 * For global shm, everybody has rw access to it until the sessiond
1156 * starts.
58d4b2a2 1157 */
7fc90dca 1158static
58d4b2a2 1159int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 1160{
7fc90dca 1161 int wait_shm_fd, ret;
58d4b2a2 1162 pid_t pid;
44e073f5 1163
58d4b2a2 1164 /*
33bbeb90 1165 * Try to open read-only.
58d4b2a2 1166 */
33bbeb90 1167 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2 1168 if (wait_shm_fd >= 0) {
7aa76730
MD
1169 int32_t tmp_read;
1170 ssize_t len;
1171 size_t bytes_read = 0;
1172
1173 /*
1174 * Try to read the fd. If unable to do so, try opening
1175 * it in write mode.
1176 */
1177 do {
1178 len = read(wait_shm_fd,
1179 &((char *) &tmp_read)[bytes_read],
1180 sizeof(tmp_read) - bytes_read);
1181 if (len > 0) {
1182 bytes_read += len;
1183 }
1184 } while ((len < 0 && errno == EINTR)
1185 || (len > 0 && bytes_read < sizeof(tmp_read)));
1186 if (bytes_read != sizeof(tmp_read)) {
1187 ret = close(wait_shm_fd);
1188 if (ret) {
1189 ERR("close wait_shm_fd");
1190 }
1191 goto open_write;
1192 }
58d4b2a2
MD
1193 goto end;
1194 } else if (wait_shm_fd < 0 && errno != ENOENT) {
1195 /*
33bbeb90
MD
1196 * Real-only open did not work, and it's not because the
1197 * entry was not present. It's a failure that prohibits
1198 * using shm.
58d4b2a2 1199 */
7fc90dca 1200 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 1201 goto end;
7fc90dca 1202 }
7aa76730
MD
1203
1204open_write:
7fc90dca 1205 /*
7aa76730
MD
1206 * If the open failed because the file did not exist, or because
1207 * the file was not truncated yet, try creating it ourself.
7fc90dca 1208 */
8c90a710 1209 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 1210 pid = fork();
8c90a710 1211 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
1212 if (pid > 0) {
1213 int status;
1214
1215 /*
1216 * Parent: wait for child to return, in which case the
1217 * shared memory map will have been created.
1218 */
1219 pid = wait(&status);
b7d3cb32 1220 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
1221 wait_shm_fd = -1;
1222 goto end;
7fc90dca 1223 }
58d4b2a2
MD
1224 /*
1225 * Try to open read-only again after creation.
1226 */
33bbeb90 1227 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
1228 if (wait_shm_fd < 0) {
1229 /*
1230 * Real-only open did not work. It's a failure
1231 * that prohibits using shm.
1232 */
1233 ERR("Error opening shm %s", sock_info->wait_shm_path);
1234 goto end;
1235 }
1236 goto end;
1237 } else if (pid == 0) {
1238 int create_mode;
1239
1240 /* Child */
33bbeb90 1241 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 1242 if (sock_info->global)
33bbeb90 1243 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
1244 /*
1245 * We're alone in a child process, so we can modify the
1246 * process-wide umask.
1247 */
33bbeb90 1248 umask(~create_mode);
58d4b2a2 1249 /*
33bbeb90
MD
1250 * Try creating shm (or get rw access).
1251 * We don't do an exclusive open, because we allow other
1252 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
1253 */
1254 wait_shm_fd = shm_open(sock_info->wait_shm_path,
1255 O_RDWR | O_CREAT, create_mode);
1256 if (wait_shm_fd >= 0) {
1257 ret = ftruncate(wait_shm_fd, mmap_size);
1258 if (ret) {
1259 PERROR("ftruncate");
b0c1425d 1260 _exit(EXIT_FAILURE);
58d4b2a2 1261 }
b0c1425d 1262 _exit(EXIT_SUCCESS);
58d4b2a2 1263 }
33bbeb90
MD
1264 /*
1265 * For local shm, we need to have rw access to accept
1266 * opening it: this means the local sessiond will be
1267 * able to wake us up. For global shm, we open it even
1268 * if rw access is not granted, because the root.root
1269 * sessiond will be able to override all rights and wake
1270 * us up.
1271 */
1272 if (!sock_info->global && errno != EACCES) {
58d4b2a2 1273 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 1274 _exit(EXIT_FAILURE);
58d4b2a2
MD
1275 }
1276 /*
33bbeb90
MD
1277 * The shm exists, but we cannot open it RW. Report
1278 * success.
58d4b2a2 1279 */
5d3bc5ed 1280 _exit(EXIT_SUCCESS);
58d4b2a2
MD
1281 } else {
1282 return -1;
7fc90dca 1283 }
58d4b2a2 1284end:
33bbeb90
MD
1285 if (wait_shm_fd >= 0 && !sock_info->global) {
1286 struct stat statbuf;
1287
1288 /*
1289 * Ensure that our user is the owner of the shm file for
1290 * local shm. If we do not own the file, it means our
1291 * sessiond will not have access to wake us up (there is
1292 * probably a rogue process trying to fake our
1293 * sessiond). Fallback to polling method in this case.
1294 */
1295 ret = fstat(wait_shm_fd, &statbuf);
1296 if (ret) {
1297 PERROR("fstat");
1298 goto error_close;
1299 }
1300 if (statbuf.st_uid != getuid())
1301 goto error_close;
1302 }
58d4b2a2 1303 return wait_shm_fd;
33bbeb90
MD
1304
1305error_close:
1306 ret = close(wait_shm_fd);
1307 if (ret) {
1308 PERROR("Error closing fd");
1309 }
1310 return -1;
58d4b2a2
MD
1311}
1312
1313static
1314char *get_map_shm(struct sock_info *sock_info)
1315{
172d6b68 1316 long page_size;
58d4b2a2
MD
1317 int wait_shm_fd, ret;
1318 char *wait_shm_mmap;
1319
172d6b68 1320 page_size = sysconf(_SC_PAGE_SIZE);
2657d1ba
MD
1321 if (page_size <= 0) {
1322 if (!page_size) {
1323 errno = EINVAL;
1324 }
1325 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
172d6b68
MD
1326 goto error;
1327 }
1328
6548fca4 1329 lttng_ust_lock_fd_tracker();
172d6b68 1330 wait_shm_fd = get_wait_shm(sock_info, page_size);
58d4b2a2 1331 if (wait_shm_fd < 0) {
6548fca4 1332 lttng_ust_unlock_fd_tracker();
58d4b2a2 1333 goto error;
44e073f5 1334 }
fc9a0ccb
JR
1335
1336 ret = lttng_ust_add_fd_to_tracker(wait_shm_fd);
1337 if (ret < 0) {
1338 ret = close(wait_shm_fd);
1339 if (!ret) {
1340 PERROR("Error closing fd");
1341 }
1342 lttng_ust_unlock_fd_tracker();
1343 goto error;
1344 }
1345
1346 wait_shm_fd = ret;
6548fca4
MD
1347 lttng_ust_unlock_fd_tracker();
1348
172d6b68 1349 wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
7fc90dca 1350 MAP_SHARED, wait_shm_fd, 0);
6548fca4 1351
7fc90dca 1352 /* close shm fd immediately after taking the mmap reference */
6548fca4 1353 lttng_ust_lock_fd_tracker();
7fc90dca 1354 ret = close(wait_shm_fd);
6548fca4
MD
1355 if (!ret) {
1356 lttng_ust_delete_fd_from_tracker(wait_shm_fd);
1357 } else {
33bbeb90
MD
1358 PERROR("Error closing fd");
1359 }
6548fca4
MD
1360 lttng_ust_unlock_fd_tracker();
1361
33bbeb90
MD
1362 if (wait_shm_mmap == MAP_FAILED) {
1363 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1364 goto error;
7fc90dca
MD
1365 }
1366 return wait_shm_mmap;
1367
1368error:
1369 return NULL;
1370}
1371
1372static
1373void wait_for_sessiond(struct sock_info *sock_info)
1374{
2eab9f28 1375 /* Use ust_lock to check if we should quit. */
3327ac33 1376 if (ust_lock()) {
7fc90dca
MD
1377 goto quit;
1378 }
37ed587a
MD
1379 if (wait_poll_fallback) {
1380 goto error;
1381 }
7fc90dca
MD
1382 ust_unlock();
1383
2eab9f28
JR
1384 assert(sock_info->wait_shm_mmap);
1385
7fc90dca 1386 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b 1387 /* Wait for futex wakeup */
ee7fcec8
MD
1388 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap))
1389 goto end_wait;
1390
1391 while (futex_async((int32_t *) sock_info->wait_shm_mmap,
1392 FUTEX_WAIT, 0, NULL, NULL, 0)) {
1393 switch (errno) {
1394 case EWOULDBLOCK:
1395 /* Value already changed. */
1396 goto end_wait;
1397 case EINTR:
1398 /* Retry if interrupted by signal. */
1399 break; /* Get out of switch. */
1400 case EFAULT:
1401 wait_poll_fallback = 1;
1402 DBG(
37ed587a
MD
1403"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1404"do not support FUTEX_WAKE on read-only memory mappings correctly. "
1405"Please upgrade your kernel "
1406"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1407"mainline). LTTng-UST will use polling mode fallback.");
ee7fcec8
MD
1408 if (ust_debug())
1409 PERROR("futex");
1410 goto end_wait;
80e2814b
MD
1411 }
1412 }
ee7fcec8 1413end_wait:
7fc90dca
MD
1414 return;
1415
1416quit:
1417 ust_unlock();
1418 return;
1419
1420error:
1421 ust_unlock();
7fc90dca 1422 return;
46050b1a
MD
1423}
1424
1ea11eab
MD
1425/*
1426 * This thread does not allocate any resource, except within
1427 * handle_message, within mutex protection. This mutex protects against
1428 * fork and exit.
98bf993f 1429 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
1430 * is also protected by the mutex.
1431 */
d9e99d10
MD
1432static
1433void *ust_listener_thread(void *arg)
1434{
1ea11eab 1435 struct sock_info *sock_info = arg;
fc9a0ccb 1436 int sock, ret, prev_connect_failed = 0, has_waited = 0, fd;
ff517991 1437 long timeout;
d9e99d10 1438
c362addf 1439 lttng_ust_fixup_tls();
01f0e40c
RB
1440 /*
1441 * If available, add '-ust' to the end of this thread's
1442 * process name
1443 */
1444 ret = lttng_ust_setustprocname();
1445 if (ret) {
1446 ERR("Unable to set UST process name");
1447 }
1448
9eb62b9c
MD
1449 /* Restart trying to connect to the session daemon */
1450restart:
c0eedf81
MD
1451 if (prev_connect_failed) {
1452 /* Wait for sessiond availability with pipe */
1453 wait_for_sessiond(sock_info);
1454 if (has_waited) {
1455 has_waited = 0;
1456 /*
1457 * Sleep for 5 seconds before retrying after a
1458 * sequence of failure / wait / failure. This
1459 * deals with a killed or broken session daemon.
1460 */
1461 sleep(5);
eacc4aa4
MD
1462 } else {
1463 has_waited = 1;
c0eedf81 1464 }
c0eedf81
MD
1465 prev_connect_failed = 0;
1466 }
9eb62b9c 1467
005aeac5
JR
1468 if (ust_lock()) {
1469 goto quit;
1470 }
1471
1ea11eab 1472 if (sock_info->socket != -1) {
6548fca4 1473 /* FD tracker is updated by ustcomm_close_unix_sock() */
e6973a89 1474 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 1475 if (ret) {
32ce8569
MD
1476 ERR("Error closing %s ust cmd socket",
1477 sock_info->name);
1ea11eab
MD
1478 }
1479 sock_info->socket = -1;
1480 }
32ce8569 1481 if (sock_info->notify_socket != -1) {
6548fca4 1482 /* FD tracker is updated by ustcomm_close_unix_sock() */
32ce8569
MD
1483 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1484 if (ret) {
1485 ERR("Error closing %s ust notify socket",
1486 sock_info->name);
1487 }
1488 sock_info->notify_socket = -1;
1489 }
46050b1a 1490
6548fca4 1491
321f2351
MD
1492 /*
1493 * Register. We need to perform both connect and sending
1494 * registration message before doing the next connect otherwise
1495 * we may reach unix socket connect queue max limits and block
1496 * on the 2nd connect while the session daemon is awaiting the
1497 * first connect registration message.
1498 */
1499 /* Connect cmd socket */
6548fca4 1500 lttng_ust_lock_fd_tracker();
451d66b2
MD
1501 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1502 get_connect_sock_timeout());
321f2351 1503 if (ret < 0) {
6548fca4 1504 lttng_ust_unlock_fd_tracker();
321f2351
MD
1505 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1506 prev_connect_failed = 1;
5b14aab3 1507
e3426ddc 1508 /*
321f2351
MD
1509 * If we cannot find the sessiond daemon, don't delay
1510 * constructor execution.
e3426ddc 1511 */
3f5339b0 1512 ret = handle_register_failed(sock_info);
321f2351
MD
1513 assert(!ret);
1514 ust_unlock();
1515 goto restart;
27fe9f21 1516 }
fc9a0ccb
JR
1517 fd = ret;
1518 ret = lttng_ust_add_fd_to_tracker(fd);
1519 if (ret < 0) {
1520 ret = close(fd);
1521 if (ret) {
1522 PERROR("close on sock_info->socket");
1523 }
1524 ret = -1;
1525 lttng_ust_unlock_fd_tracker();
1526 ust_unlock();
1527 goto quit;
1528 }
1529
321f2351 1530 sock_info->socket = ret;
fc9a0ccb 1531 lttng_ust_unlock_fd_tracker();
27fe9f21 1532
6548fca4
MD
1533 ust_unlock();
1534 /*
1535 * Unlock/relock ust lock because connect is blocking (with
1536 * timeout). Don't delay constructors on the ust lock for too
1537 * long.
1538 */
3327ac33 1539 if (ust_lock()) {
5b14aab3
MD
1540 goto quit;
1541 }
1542
46050b1a
MD
1543 /*
1544 * Create only one root handle per listener thread for the whole
f59ed768
MD
1545 * process lifetime, so we ensure we get ID which is statically
1546 * assigned to the root handle.
46050b1a
MD
1547 */
1548 if (sock_info->root_handle == -1) {
1549 ret = lttng_abi_create_root_handle();
a51070bb 1550 if (ret < 0) {
46050b1a 1551 ERR("Error creating root handle");
46050b1a
MD
1552 goto quit;
1553 }
1554 sock_info->root_handle = ret;
9eb62b9c 1555 }
1ea11eab 1556
32ce8569 1557 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
9eb62b9c 1558 if (ret < 0) {
32ce8569
MD
1559 ERR("Error registering to %s ust cmd socket",
1560 sock_info->name);
c0eedf81 1561 prev_connect_failed = 1;
11ff9c7d
MD
1562 /*
1563 * If we cannot register to the sessiond daemon, don't
1564 * delay constructor execution.
1565 */
3f5339b0 1566 ret = handle_register_failed(sock_info);
11ff9c7d 1567 assert(!ret);
17dfb34b 1568 ust_unlock();
9eb62b9c
MD
1569 goto restart;
1570 }
321f2351
MD
1571
1572 ust_unlock();
6548fca4
MD
1573 /*
1574 * Unlock/relock ust lock because connect is blocking (with
1575 * timeout). Don't delay constructors on the ust lock for too
1576 * long.
1577 */
1578 if (ust_lock()) {
1579 goto quit;
1580 }
321f2351
MD
1581
1582 /* Connect notify socket */
6548fca4 1583 lttng_ust_lock_fd_tracker();
451d66b2
MD
1584 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1585 get_connect_sock_timeout());
321f2351 1586 if (ret < 0) {
6548fca4 1587 lttng_ust_unlock_fd_tracker();
321f2351
MD
1588 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1589 prev_connect_failed = 1;
1590
321f2351
MD
1591 /*
1592 * If we cannot find the sessiond daemon, don't delay
1593 * constructor execution.
1594 */
3f5339b0 1595 ret = handle_register_failed(sock_info);
321f2351
MD
1596 assert(!ret);
1597 ust_unlock();
1598 goto restart;
1599 }
fc9a0ccb
JR
1600
1601 fd = ret;
1602 ret = lttng_ust_add_fd_to_tracker(fd);
1603 if (ret < 0) {
1604 ret = close(fd);
1605 if (ret) {
1606 PERROR("close on sock_info->notify_socket");
1607 }
1608 ret = -1;
1609 lttng_ust_unlock_fd_tracker();
1610 ust_unlock();
1611 goto quit;
1612 }
1613
321f2351 1614 sock_info->notify_socket = ret;
fc9a0ccb 1615 lttng_ust_unlock_fd_tracker();
321f2351 1616
6548fca4
MD
1617 ust_unlock();
1618 /*
1619 * Unlock/relock ust lock because connect is blocking (with
1620 * timeout). Don't delay constructors on the ust lock for too
1621 * long.
1622 */
1623 if (ust_lock()) {
1624 goto quit;
1625 }
1626
321f2351
MD
1627 timeout = get_notify_sock_timeout();
1628 if (timeout >= 0) {
1629 /*
1630 * Give at least 10ms to sessiond to reply to
1631 * notifications.
1632 */
1633 if (timeout < 10)
1634 timeout = 10;
1635 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1636 timeout);
1637 if (ret < 0) {
1638 WARN("Error setting socket receive timeout");
1639 }
1640 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1641 timeout);
1642 if (ret < 0) {
1643 WARN("Error setting socket send timeout");
1644 }
1645 } else if (timeout < -1) {
1646 WARN("Unsupported timeout value %ld", timeout);
1647 }
1648
32ce8569
MD
1649 ret = register_to_sessiond(sock_info->notify_socket,
1650 USTCTL_SOCKET_NOTIFY);
1651 if (ret < 0) {
1652 ERR("Error registering to %s ust notify socket",
1653 sock_info->name);
1654 prev_connect_failed = 1;
1655 /*
1656 * If we cannot register to the sessiond daemon, don't
1657 * delay constructor execution.
1658 */
3f5339b0 1659 ret = handle_register_failed(sock_info);
32ce8569
MD
1660 assert(!ret);
1661 ust_unlock();
1662 goto restart;
1663 }
1664 sock = sock_info->socket;
1665
17dfb34b 1666 ust_unlock();
46050b1a 1667
d9e99d10
MD
1668 for (;;) {
1669 ssize_t len;
57773204 1670 struct ustcomm_ust_msg lum;
d9e99d10 1671
57773204 1672 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
1673 switch (len) {
1674 case 0: /* orderly shutdown */
7dd08bec 1675 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
3327ac33 1676 if (ust_lock()) {
d5e1fea6
MD
1677 goto quit;
1678 }
8236ba10
MD
1679 /*
1680 * Either sessiond has shutdown or refused us by closing the socket.
1681 * In either case, we don't want to delay construction execution,
1682 * and we need to wait before retry.
1683 */
1684 prev_connect_failed = 1;
1685 /*
1686 * If we cannot register to the sessiond daemon, don't
1687 * delay constructor execution.
1688 */
3f5339b0 1689 ret = handle_register_failed(sock_info);
8236ba10
MD
1690 assert(!ret);
1691 ust_unlock();
d9e99d10 1692 goto end;
e7723462 1693 case sizeof(lum):
74d81a6c 1694 print_cmd(lum.cmd, lum.handle);
11ff9c7d 1695 ret = handle_message(sock_info, sock, &lum);
7bc53e94 1696 if (ret) {
0dafcd63
MD
1697 ERR("Error handling message for %s socket",
1698 sock_info->name);
1699 /*
1700 * Close socket if protocol error is
1701 * detected.
1702 */
1703 goto end;
d9e99d10
MD
1704 }
1705 continue;
7bc53e94
MD
1706 default:
1707 if (len < 0) {
1708 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1709 } else {
1710 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1711 }
1712 if (len == -ECONNRESET) {
1713 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
1714 goto end;
1715 }
1716 goto end;
d9e99d10
MD
1717 }
1718
1719 }
1720end:
3327ac33 1721 if (ust_lock()) {
d5e1fea6
MD
1722 goto quit;
1723 }
f59ed768
MD
1724 /* Cleanup socket handles before trying to reconnect */
1725 lttng_ust_objd_table_owner_cleanup(sock_info);
1726 ust_unlock();
9eb62b9c 1727 goto restart; /* try to reconnect */
e33f3265 1728
1ea11eab 1729quit:
e33f3265 1730 ust_unlock();
3327ac33
MD
1731
1732 pthread_mutex_lock(&ust_exit_mutex);
1733 sock_info->thread_active = 0;
1734 pthread_mutex_unlock(&ust_exit_mutex);
d9e99d10
MD
1735 return NULL;
1736}
1737
2594a5b4
MD
1738/*
1739 * Weak symbol to call when the ust malloc wrapper is not loaded.
1740 */
1741__attribute__((weak))
1742void lttng_ust_malloc_wrapper_init(void)
1743{
1744}
1745
2691221a
MD
1746/*
1747 * sessiond monitoring thread: monitor presence of global and per-user
1748 * sessiond by polling the application common named pipe.
1749 */
edaa1431 1750void __attribute__((constructor)) lttng_ust_init(void)
2691221a 1751{
11ff9c7d 1752 struct timespec constructor_timeout;
ae6a58bf 1753 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 1754 pthread_attr_t thread_attr;
cf12a773 1755 int timeout_mode;
2691221a
MD
1756 int ret;
1757
edaa1431
MD
1758 if (uatomic_xchg(&initialized, 1) == 1)
1759 return;
1760
eddd8d5d
MD
1761 /*
1762 * Fixup interdependency between TLS fixup mutex (which happens
1763 * to be the dynamic linker mutex) and ust_lock, taken within
1764 * the ust lock.
1765 */
c362addf 1766 lttng_ust_fixup_tls();
eddd8d5d 1767
07b57e5e
MD
1768 lttng_ust_loaded = 1;
1769
edaa1431
MD
1770 /*
1771 * We want precise control over the order in which we construct
1772 * our sub-libraries vs starting to receive commands from
1773 * sessiond (otherwise leading to errors when trying to create
1774 * sessiond before the init functions are completed).
1775 */
2691221a 1776 init_usterr();
49254388 1777 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
edaa1431 1778 init_tracepoint();
6548fca4 1779 lttng_ust_init_fd_tracker();
f9364363 1780 lttng_ust_clock_init();
5e1b7b8b 1781 lttng_ust_getcpu_init();
cf73e0fe 1782 lttng_ust_statedump_init();
7dd08bec
MD
1783 lttng_ring_buffer_metadata_client_init();
1784 lttng_ring_buffer_client_overwrite_init();
34a91bdb 1785 lttng_ring_buffer_client_overwrite_rt_init();
7dd08bec 1786 lttng_ring_buffer_client_discard_init();
34a91bdb 1787 lttng_ring_buffer_client_discard_rt_init();
d58d1454 1788 lttng_perf_counter_init();
2594a5b4
MD
1789 /*
1790 * Invoke ust malloc wrapper init before starting other threads.
1791 */
1792 lttng_ust_malloc_wrapper_init();
2691221a 1793
ff517991 1794 timeout_mode = get_constructor_timeout(&constructor_timeout);
11ff9c7d 1795
95259bd0 1796 ret = sem_init(&constructor_wait, 0, 0);
8aadb54a
MD
1797 if (ret) {
1798 PERROR("sem_init");
1799 }
11ff9c7d 1800
2eab9f28
JR
1801 ret = setup_global_apps();
1802 if (ret) {
1803 assert(global_apps.allowed == 0);
1804 DBG("global apps setup returned %d", ret);
1805 }
1806
8d20bf54 1807 ret = setup_local_apps();
2691221a 1808 if (ret) {
2eab9f28 1809 assert(local_apps.allowed == 0);
9ec6895c 1810 DBG("local apps setup returned %d", ret);
2691221a 1811 }
ae6a58bf
WP
1812
1813 /* A new thread created by pthread_create inherits the signal mask
1814 * from the parent. To avoid any signal being received by the
1815 * listener thread, we block all signals temporarily in the parent,
1816 * while we create the listener thread.
1817 */
1818 sigfillset(&sig_all_blocked);
1819 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1820 if (ret) {
d94d802c 1821 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1822 }
1823
1879f67f
MG
1824 ret = pthread_attr_init(&thread_attr);
1825 if (ret) {
1826 ERR("pthread_attr_init: %s", strerror(ret));
1827 }
1828 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1829 if (ret) {
1830 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1831 }
1832
2eab9f28
JR
1833 if (global_apps.allowed) {
1834 pthread_mutex_lock(&ust_exit_mutex);
1835 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
1836 ust_listener_thread, &global_apps);
1837 if (ret) {
1838 ERR("pthread_create global: %s", strerror(ret));
1839 }
1840 global_apps.thread_active = 1;
1841 pthread_mutex_unlock(&ust_exit_mutex);
1842 } else {
1843 handle_register_done(&global_apps);
d94d802c 1844 }
e33f3265 1845
8d20bf54 1846 if (local_apps.allowed) {
c0bbbd5a 1847 pthread_mutex_lock(&ust_exit_mutex);
1879f67f 1848 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 1849 ust_listener_thread, &local_apps);
d94d802c
MD
1850 if (ret) {
1851 ERR("pthread_create local: %s", strerror(ret));
1852 }
e33f3265 1853 local_apps.thread_active = 1;
c0bbbd5a 1854 pthread_mutex_unlock(&ust_exit_mutex);
8d20bf54
MD
1855 } else {
1856 handle_register_done(&local_apps);
1857 }
1879f67f
MG
1858 ret = pthread_attr_destroy(&thread_attr);
1859 if (ret) {
1860 ERR("pthread_attr_destroy: %s", strerror(ret));
1861 }
8d20bf54 1862
ae6a58bf
WP
1863 /* Restore original signal mask in parent */
1864 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1865 if (ret) {
d94d802c 1866 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
1867 }
1868
cf12a773
MD
1869 switch (timeout_mode) {
1870 case 1: /* timeout wait */
95259bd0
MD
1871 do {
1872 ret = sem_timedwait(&constructor_wait,
1873 &constructor_timeout);
1874 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
1875 if (ret < 0) {
1876 switch (errno) {
1877 case ETIMEDOUT:
1878 ERR("Timed out waiting for lttng-sessiond");
1879 break;
1880 case EINVAL:
1881 PERROR("sem_timedwait");
1882 break;
1883 default:
1884 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1885 strerror(errno));
1886 }
cf12a773
MD
1887 }
1888 break;
7b766b16 1889 case -1:/* wait forever */
95259bd0
MD
1890 do {
1891 ret = sem_wait(&constructor_wait);
1892 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
1893 if (ret < 0) {
1894 switch (errno) {
1895 case EINVAL:
1896 PERROR("sem_wait");
1897 break;
1898 default:
1899 ERR("Unexpected error \"%s\" returned by sem_wait",
1900 strerror(errno));
1901 }
1902 }
cf12a773 1903 break;
7b766b16 1904 case 0: /* no timeout */
cf12a773 1905 break;
11ff9c7d 1906 }
2691221a
MD
1907}
1908
17dfb34b
MD
1909static
1910void lttng_ust_cleanup(int exiting)
1911{
efe0de09 1912 cleanup_sock_info(&global_apps, exiting);
932cfadb 1913 cleanup_sock_info(&local_apps, exiting);
74f98bc9 1914 local_apps.allowed = 0;
2eab9f28 1915 global_apps.allowed = 0;
efe0de09
MD
1916 /*
1917 * The teardown in this function all affect data structures
1918 * accessed under the UST lock by the listener thread. This
1919 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1920 * that none of these threads are accessing this data at this
1921 * point.
1922 */
17dfb34b 1923 lttng_ust_abi_exit();
003fedf4 1924 lttng_ust_events_exit();
d58d1454 1925 lttng_perf_counter_exit();
34a91bdb 1926 lttng_ring_buffer_client_discard_rt_exit();
7dd08bec 1927 lttng_ring_buffer_client_discard_exit();
34a91bdb 1928 lttng_ring_buffer_client_overwrite_rt_exit();
7dd08bec
MD
1929 lttng_ring_buffer_client_overwrite_exit();
1930 lttng_ring_buffer_metadata_client_exit();
cf73e0fe 1931 lttng_ust_statedump_destroy();
17dfb34b
MD
1932 exit_tracepoint();
1933 if (!exiting) {
1934 /* Reinitialize values for fork */
3f5339b0 1935 sem_count = sem_count_initial_value;
17dfb34b
MD
1936 lttng_ust_comm_should_quit = 0;
1937 initialized = 0;
1938 }
1939}
1940
edaa1431 1941void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1942{
1943 int ret;
1944
9eb62b9c
MD
1945 /*
1946 * Using pthread_cancel here because:
1947 * A) we don't want to hang application teardown.
1948 * B) the thread is not allocating any resource.
1949 */
1ea11eab
MD
1950
1951 /*
1952 * Require the communication thread to quit. Synchronize with
1953 * mutexes to ensure it is not in a mutex critical section when
1954 * pthread_cancel is later called.
1955 */
3327ac33 1956 ust_lock_nocheck();
1ea11eab 1957 lttng_ust_comm_should_quit = 1;
3327ac33 1958 ust_unlock();
1ea11eab 1959
3327ac33 1960 pthread_mutex_lock(&ust_exit_mutex);
f5f94532 1961 /* cancel threads */
e33f3265
MD
1962 if (global_apps.thread_active) {
1963 ret = pthread_cancel(global_apps.ust_listener);
1964 if (ret) {
1965 ERR("Error cancelling global ust listener thread: %s",
1966 strerror(ret));
1967 } else {
1968 global_apps.thread_active = 0;
1969 }
2691221a 1970 }
e33f3265 1971 if (local_apps.thread_active) {
8d20bf54
MD
1972 ret = pthread_cancel(local_apps.ust_listener);
1973 if (ret) {
d94d802c
MD
1974 ERR("Error cancelling local ust listener thread: %s",
1975 strerror(ret));
e33f3265
MD
1976 } else {
1977 local_apps.thread_active = 0;
8d20bf54 1978 }
8d20bf54 1979 }
3327ac33 1980 pthread_mutex_unlock(&ust_exit_mutex);
e33f3265 1981
efe0de09
MD
1982 /*
1983 * Do NOT join threads: use of sys_futex makes it impossible to
1984 * join the threads without using async-cancel, but async-cancel
1985 * is delivered by a signal, which could hit the target thread
1986 * anywhere in its code path, including while the ust_lock() is
1987 * held, causing a deadlock for the other thread. Let the OS
1988 * cleanup the threads if there are stalled in a syscall.
1989 */
17dfb34b 1990 lttng_ust_cleanup(1);
2691221a 1991}
e822f505
MD
1992
1993/*
1994 * We exclude the worker threads across fork and clone (except
1995 * CLONE_VM), because these system calls only keep the forking thread
1996 * running in the child. Therefore, we don't want to call fork or clone
1997 * in the middle of an tracepoint or ust tracing state modification.
1998 * Holding this mutex protects these structures across fork and clone.
1999 */
b728d87e 2000void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
2001{
2002 /*
2003 * Disable signals. This is to avoid that the child intervenes
2004 * before it is properly setup for tracing. It is safer to
2005 * disable all signals, because then we know we are not breaking
2006 * anything by restoring the original mask.
2007 */
2008 sigset_t all_sigs;
2009 int ret;
2010
c362addf
MD
2011 /* Fixup lttng-ust TLS. */
2012 lttng_ust_fixup_tls();
2013
8c90a710 2014 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2015 return;
e822f505
MD
2016 /* Disable signals */
2017 sigfillset(&all_sigs);
b728d87e 2018 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
2019 if (ret == -1) {
2020 PERROR("sigprocmask");
2021 }
458d678c
PW
2022
2023 pthread_mutex_lock(&ust_fork_mutex);
2024
3327ac33 2025 ust_lock_nocheck();
e822f505
MD
2026 rcu_bp_before_fork();
2027}
2028
b728d87e 2029static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
2030{
2031 int ret;
2032
17dfb34b
MD
2033 DBG("process %d", getpid());
2034 ust_unlock();
458d678c
PW
2035
2036 pthread_mutex_unlock(&ust_fork_mutex);
2037
e822f505 2038 /* Restore signals */
23c8854a 2039 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
2040 if (ret == -1) {
2041 PERROR("sigprocmask");
2042 }
2043}
2044
b728d87e 2045void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 2046{
8c90a710 2047 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2048 return;
17dfb34b 2049 DBG("process %d", getpid());
e822f505
MD
2050 rcu_bp_after_fork_parent();
2051 /* Release mutexes and reenable signals */
b728d87e 2052 ust_after_fork_common(restore_sigset);
e822f505
MD
2053}
2054
17dfb34b
MD
2055/*
2056 * After fork, in the child, we need to cleanup all the leftover state,
2057 * except the worker thread which already magically disappeared thanks
2058 * to the weird Linux fork semantics. After tyding up, we call
2059 * lttng_ust_init() again to start over as a new PID.
2060 *
2061 * This is meant for forks() that have tracing in the child between the
2062 * fork and following exec call (if there is any).
2063 */
b728d87e 2064void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 2065{
8c90a710 2066 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2067 return;
77971c85 2068 lttng_context_vpid_reset();
8478887d 2069 lttng_context_vtid_reset();
efe79472 2070 lttng_context_procname_reset();
17dfb34b 2071 DBG("process %d", getpid());
e822f505
MD
2072 /* Release urcu mutexes */
2073 rcu_bp_after_fork_child();
17dfb34b 2074 lttng_ust_cleanup(0);
e822f505 2075 /* Release mutexes and reenable signals */
b728d87e 2076 ust_after_fork_common(restore_sigset);
318dfea9 2077 lttng_ust_init();
e822f505 2078}
95c25348 2079
246be17e 2080void lttng_ust_sockinfo_session_enabled(void *owner)
95c25348
PW
2081{
2082 struct sock_info *sock_info = owner;
37dddb65 2083 sock_info->statedump_pending = 1;
95c25348 2084}
This page took 0.15061 seconds and 4 git commands to generate.