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