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