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