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