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