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