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