Filter: receive, attach and link empty filter
[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
2691221a
MD
23#include <sys/types.h>
24#include <sys/socket.h>
7fc90dca
MD
25#include <sys/mman.h>
26#include <sys/stat.h>
58d4b2a2
MD
27#include <sys/types.h>
28#include <sys/wait.h>
7fc90dca 29#include <fcntl.h>
2691221a
MD
30#include <unistd.h>
31#include <errno.h>
d9e99d10 32#include <pthread.h>
11ff9c7d
MD
33#include <semaphore.h>
34#include <time.h>
1ea11eab 35#include <assert.h>
e822f505 36#include <signal.h>
95259bd0 37#include <urcu/uatomic.h>
80e2814b 38#include <urcu/futex.h>
c117fb1b 39#include <urcu/compiler.h>
1ea11eab 40
4318ae1b 41#include <lttng/ust-events.h>
4318ae1b 42#include <lttng/ust-abi.h>
4318ae1b 43#include <lttng/ust.h>
44c72f10
MD
44#include <ust-comm.h>
45#include <usterr-signal-safe.h>
46#include "tracepoint-internal.h"
b751f722 47#include "ltt-tracer-core.h"
08114193 48#include "compat.h"
f645cfa7 49#include "../libringbuffer/tlsfixup.h"
edaa1431
MD
50
51/*
52 * Has lttng ust comm constructor been called ?
53 */
54static int initialized;
55
1ea11eab 56/*
17dfb34b
MD
57 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
58 * Held when handling a command, also held by fork() to deal with
59 * removal of threads, and by exit path.
1ea11eab 60 */
1ea11eab
MD
61
62/* Should the ust comm thread quit ? */
63static int lttng_ust_comm_should_quit;
64
11ff9c7d
MD
65/*
66 * Wait for either of these before continuing to the main
67 * program:
68 * - the register_done message from sessiond daemon
69 * (will let the sessiond daemon enable sessions before main
70 * starts.)
71 * - sessiond daemon is not reachable.
72 * - timeout (ensuring applications are resilient to session
73 * daemon problems).
74 */
75static sem_t constructor_wait;
950aab0c
MD
76/*
77 * Doing this for both the global and local sessiond.
78 */
95259bd0 79static int sem_count = { 2 };
11ff9c7d 80
e8508a49
MD
81/*
82 * Counting nesting within lttng-ust. Used to ensure that calling fork()
83 * from liblttng-ust does not execute the pre/post fork handlers.
84 */
85static int __thread lttng_ust_nest_count;
86
1ea11eab
MD
87/*
88 * Info about socket and associated listener thread.
89 */
90struct sock_info {
11ff9c7d 91 const char *name;
1ea11eab 92 pthread_t ust_listener; /* listener thread */
46050b1a 93 int root_handle;
8d20bf54
MD
94 int constructor_sem_posted;
95 int allowed;
44e073f5 96 int global;
7fc90dca
MD
97
98 char sock_path[PATH_MAX];
99 int socket;
100
101 char wait_shm_path[PATH_MAX];
102 char *wait_shm_mmap;
1ea11eab 103};
2691221a
MD
104
105/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 106struct sock_info global_apps = {
11ff9c7d 107 .name = "global",
44e073f5 108 .global = 1,
7fc90dca 109
46050b1a 110 .root_handle = -1,
8d20bf54 111 .allowed = 1,
7fc90dca
MD
112
113 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
114 .socket = -1,
115
116 .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
1ea11eab 117};
2691221a
MD
118
119/* TODO: allow global_apps_sock_path override */
120
1ea11eab 121struct sock_info local_apps = {
11ff9c7d 122 .name = "local",
44e073f5 123 .global = 0,
46050b1a 124 .root_handle = -1,
8d20bf54 125 .allowed = 0, /* Check setuid bit first */
7fc90dca
MD
126
127 .socket = -1,
1ea11eab 128};
2691221a 129
37ed587a
MD
130static int wait_poll_fallback;
131
edaa1431
MD
132extern void ltt_ring_buffer_client_overwrite_init(void);
133extern void ltt_ring_buffer_client_discard_init(void);
134extern void ltt_ring_buffer_metadata_client_init(void);
135extern void ltt_ring_buffer_client_overwrite_exit(void);
136extern void ltt_ring_buffer_client_discard_exit(void);
137extern void ltt_ring_buffer_metadata_client_exit(void);
138
a903623f
MD
139/*
140 * Force a read (imply TLS fixup for dlopen) of TLS variables.
141 */
142static
143void lttng_fixup_nest_count_tls(void)
144{
145 asm volatile ("" : : "m" (lttng_ust_nest_count));
146}
147
2691221a 148static
8d20bf54 149int setup_local_apps(void)
2691221a
MD
150{
151 const char *home_dir;
7fc90dca 152 uid_t uid;
2691221a 153
7fc90dca 154 uid = getuid();
8d20bf54
MD
155 /*
156 * Disallow per-user tracing for setuid binaries.
157 */
7fc90dca 158 if (uid != geteuid()) {
8d20bf54 159 local_apps.allowed = 0;
d0a1ae63 160 return 0;
8d20bf54
MD
161 } else {
162 local_apps.allowed = 1;
163 }
2691221a
MD
164 home_dir = (const char *) getenv("HOME");
165 if (!home_dir)
166 return -ENOENT;
1ea11eab 167 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 168 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
7fc90dca
MD
169 snprintf(local_apps.wait_shm_path, PATH_MAX,
170 DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid);
2691221a
MD
171 return 0;
172}
173
174static
175int register_app_to_sessiond(int socket)
176{
177 ssize_t ret;
178 struct {
e44418f3
MD
179 uint32_t major;
180 uint32_t minor;
2691221a 181 pid_t pid;
5c33bde8 182 pid_t ppid;
2691221a 183 uid_t uid;
83610856 184 gid_t gid;
c117fb1b 185 uint32_t bits_per_long;
2629549e 186 char name[16]; /* process name */
2691221a
MD
187 } reg_msg;
188
e44418f3
MD
189 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
190 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a 191 reg_msg.pid = getpid();
5c33bde8 192 reg_msg.ppid = getppid();
2691221a 193 reg_msg.uid = getuid();
83610856 194 reg_msg.gid = getgid();
c117fb1b 195 reg_msg.bits_per_long = CAA_BITS_PER_LONG;
08114193 196 lttng_ust_getprocname(reg_msg.name);
2691221a 197
57773204 198 ret = ustcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
2691221a
MD
199 if (ret >= 0 && ret != sizeof(reg_msg))
200 return -EIO;
201 return ret;
202}
203
d9e99d10 204static
57773204 205int send_reply(int sock, struct ustcomm_ust_reply *lur)
d9e99d10 206{
9eb62b9c 207 ssize_t len;
d3a492d1 208
57773204 209 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 210 switch (len) {
a4be8962 211 case sizeof(*lur):
d3a492d1
MD
212 DBG("message successfully sent");
213 return 0;
214 case -1:
215 if (errno == ECONNRESET) {
216 printf("remote end closed connection\n");
217 return 0;
218 }
219 return -1;
220 default:
221 printf("incorrect message size: %zd\n", len);
222 return -1;
223 }
224}
225
226static
edaa1431 227int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
228{
229 int ret;
230
edaa1431
MD
231 if (sock_info->constructor_sem_posted)
232 return 0;
233 sock_info->constructor_sem_posted = 1;
56cd7e2f
MD
234 if (uatomic_read(&sem_count) <= 0) {
235 return 0;
236 }
95259bd0
MD
237 ret = uatomic_add_return(&sem_count, -1);
238 if (ret == 0) {
239 ret = sem_post(&constructor_wait);
240 assert(!ret);
241 }
11ff9c7d
MD
242 return 0;
243}
244
245static
246int handle_message(struct sock_info *sock_info,
57773204 247 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 248{
1ea11eab 249 int ret = 0;
b61ce3b2 250 const struct lttng_ust_objd_ops *ops;
57773204 251 struct ustcomm_ust_reply lur;
193183fb 252 int shm_fd, wait_fd;
ef9ff354 253 union ust_args args;
40003310 254 ssize_t len;
1ea11eab 255
17dfb34b 256 ust_lock();
1ea11eab 257
46050b1a
MD
258 memset(&lur, 0, sizeof(lur));
259
1ea11eab 260 if (lttng_ust_comm_should_quit) {
46050b1a 261 ret = -EPERM;
1ea11eab
MD
262 goto end;
263 }
9eb62b9c 264
46050b1a
MD
265 ops = objd_ops(lum->handle);
266 if (!ops) {
267 ret = -ENOENT;
268 goto end;
1ea11eab 269 }
46050b1a
MD
270
271 switch (lum->cmd) {
11ff9c7d
MD
272 case LTTNG_UST_REGISTER_DONE:
273 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 274 ret = handle_register_done(sock_info);
11ff9c7d
MD
275 else
276 ret = -EINVAL;
277 break;
46050b1a
MD
278 case LTTNG_UST_RELEASE:
279 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
280 ret = -EPERM;
281 else
d4419b81 282 ret = lttng_ust_objd_unref(lum->handle);
d9e99d10 283 break;
2d78951a
MD
284 case LTTNG_UST_FILTER:
285 {
286 /* Receive filter data */
287 struct {
288 uint16_t len;
289 uint16_t reloc_offset;
290 char data[FILTER_BYTECODE_MAX_LEN];
291 } filter_data;
292
293 if (lum->u.filter.data_size > 65536) {
294 ERR("Filter data size is too large: %u bytes\n",
295 lum->u.filter.data_size);
296 ret = -EINVAL;
297 goto error;
298 }
299 len = ustcomm_recv_unix_sock(sock, filter_data.data,
300 lum->u.filter.data_size);
301 switch (len) {
302 case 0: /* orderly shutdown */
303 ret = 0;
304 goto error;
305 case -1:
306 DBG("Receive failed from lttng-sessiond with errno %d", errno);
307 if (errno == ECONNRESET) {
308 ERR("%s remote end closed connection\n", sock_info->name);
309 ret = -EINVAL;
310 goto error;
311 }
312 ret = -EINVAL;
313 goto end;
314 default:
315 if (len == lum->u.filter.data_size) {
316 DBG("filter data received\n");
317 break;
318 } else {
319 ERR("incorrect filter data message size: %zd\n", len);
320 ret = -EINVAL;
321 goto end;
322 }
323 }
324 filter_data.len = lum->u.filter.data_size;
325 filter_data.reloc_offset = lum->u.filter.reloc_offset;
326 if (ops->cmd)
327 ret = ops->cmd(lum->handle, lum->cmd,
328 (unsigned long) &filter_data,
329 &args);
330 else
331 ret = -ENOSYS;
332 break;
333 }
d9e99d10 334 default:
46050b1a
MD
335 if (ops->cmd)
336 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354
MD
337 (unsigned long) &lum->u,
338 &args);
46050b1a
MD
339 else
340 ret = -ENOSYS;
341 break;
d9e99d10 342 }
46050b1a 343
1ea11eab 344end:
46050b1a
MD
345 lur.handle = lum->handle;
346 lur.cmd = lum->cmd;
347 lur.ret_val = ret;
348 if (ret >= 0) {
57773204 349 lur.ret_code = USTCOMM_OK;
46050b1a 350 } else {
57773204 351 //lur.ret_code = USTCOMM_SESSION_FAIL;
193183fb 352 lur.ret_code = ret;
46050b1a 353 }
e6ea14c5
MD
354 if (ret >= 0) {
355 switch (lum->cmd) {
356 case LTTNG_UST_STREAM:
357 /*
358 * Special-case reply to send stream info.
359 * Use lum.u output.
360 */
361 lur.u.stream.memory_map_size = *args.stream.memory_map_size;
362 shm_fd = *args.stream.shm_fd;
363 wait_fd = *args.stream.wait_fd;
364 break;
365 case LTTNG_UST_METADATA:
366 case LTTNG_UST_CHANNEL:
367 lur.u.channel.memory_map_size = *args.channel.memory_map_size;
368 shm_fd = *args.channel.shm_fd;
369 wait_fd = *args.channel.wait_fd;
370 break;
371 case LTTNG_UST_TRACER_VERSION:
372 lur.u.version = lum->u.version;
373 break;
374 case LTTNG_UST_TRACEPOINT_LIST_GET:
375 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
376 break;
377 }
381c0f1e 378 }
46050b1a 379 ret = send_reply(sock, &lur);
193183fb
MD
380 if (ret < 0) {
381 perror("error sending reply");
382 goto error;
383 }
46050b1a 384
824f40b8
MD
385 if ((lum->cmd == LTTNG_UST_STREAM
386 || lum->cmd == LTTNG_UST_CHANNEL
387 || lum->cmd == LTTNG_UST_METADATA)
57773204 388 && lur.ret_code == USTCOMM_OK) {
50be2fc4
MD
389 int sendret = 0;
390
381c0f1e 391 /* we also need to send the file descriptors. */
57773204 392 ret = ustcomm_send_fds_unix_sock(sock,
193183fb 393 &shm_fd, &shm_fd,
381c0f1e
MD
394 1, sizeof(int));
395 if (ret < 0) {
396 perror("send shm_fd");
50be2fc4 397 sendret = ret;
381c0f1e 398 }
50be2fc4
MD
399 /*
400 * The sessiond expects 2 file descriptors, even upon
401 * error.
402 */
57773204 403 ret = ustcomm_send_fds_unix_sock(sock,
193183fb 404 &wait_fd, &wait_fd,
381c0f1e
MD
405 1, sizeof(int));
406 if (ret < 0) {
407 perror("send wait_fd");
408 goto error;
409 }
50be2fc4
MD
410 if (sendret) {
411 ret = sendret;
412 goto error;
413 }
381c0f1e 414 }
40003310
MD
415 /*
416 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
417 * after the reply.
418 */
419 if (lur.ret_code == USTCOMM_OK) {
420 switch (lum->cmd) {
421 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
422 len = ustcomm_send_unix_sock(sock,
423 &args.field_list.entry,
424 sizeof(args.field_list.entry));
425 if (len != sizeof(args.field_list.entry)) {
426 ret = -1;
427 goto error;
428 }
429 }
430 }
ef9ff354
MD
431 /*
432 * We still have the memory map reference, and the fds have been
3ad5f707
MD
433 * sent to the sessiond. We can therefore close those fds. Note
434 * that we keep the write side of the wait_fd open, but close
435 * the read side.
ef9ff354
MD
436 */
437 if (lur.ret_code == USTCOMM_OK) {
438 switch (lum->cmd) {
439 case LTTNG_UST_STREAM:
440 if (shm_fd >= 0) {
441 ret = close(shm_fd);
442 if (ret) {
443 PERROR("Error closing stream shm_fd");
444 }
445 *args.stream.shm_fd = -1;
446 }
447 if (wait_fd >= 0) {
448 ret = close(wait_fd);
449 if (ret) {
450 PERROR("Error closing stream wait_fd");
451 }
452 *args.stream.wait_fd = -1;
453 }
454 break;
455 case LTTNG_UST_METADATA:
456 case LTTNG_UST_CHANNEL:
457 if (shm_fd >= 0) {
458 ret = close(shm_fd);
459 if (ret) {
460 PERROR("Error closing channel shm_fd");
461 }
462 *args.channel.shm_fd = -1;
463 }
464 if (wait_fd >= 0) {
465 ret = close(wait_fd);
466 if (ret) {
467 PERROR("Error closing channel wait_fd");
468 }
469 *args.channel.wait_fd = -1;
470 }
471 break;
472 }
473 }
474
381c0f1e 475error:
17dfb34b 476 ust_unlock();
1ea11eab 477 return ret;
d9e99d10
MD
478}
479
46050b1a 480static
efe0de09 481void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
482{
483 int ret;
484
485 if (sock_info->socket != -1) {
e6973a89 486 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 487 if (ret) {
7fc90dca 488 ERR("Error closing apps socket");
46050b1a
MD
489 }
490 sock_info->socket = -1;
491 }
492 if (sock_info->root_handle != -1) {
d4419b81 493 ret = lttng_ust_objd_unref(sock_info->root_handle);
46050b1a
MD
494 if (ret) {
495 ERR("Error unref root handle");
496 }
497 sock_info->root_handle = -1;
498 }
318dfea9 499 sock_info->constructor_sem_posted = 0;
efe0de09
MD
500 /*
501 * wait_shm_mmap is used by listener threads outside of the
502 * ust lock, so we cannot tear it down ourselves, because we
503 * cannot join on these threads. Leave this task to the OS
504 * process exit.
505 */
506 if (!exiting && sock_info->wait_shm_mmap) {
7fc90dca
MD
507 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
508 if (ret) {
509 ERR("Error unmapping wait shm");
510 }
511 sock_info->wait_shm_mmap = NULL;
512 }
513}
514
58d4b2a2 515/*
33bbeb90
MD
516 * Using fork to set umask in the child process (not multi-thread safe).
517 * We deal with the shm_open vs ftruncate race (happening when the
518 * sessiond owns the shm and does not let everybody modify it, to ensure
519 * safety against shm_unlink) by simply letting the mmap fail and
520 * retrying after a few seconds.
521 * For global shm, everybody has rw access to it until the sessiond
522 * starts.
58d4b2a2 523 */
7fc90dca 524static
58d4b2a2 525int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 526{
7fc90dca 527 int wait_shm_fd, ret;
58d4b2a2 528 pid_t pid;
44e073f5 529
58d4b2a2 530 /*
33bbeb90 531 * Try to open read-only.
58d4b2a2 532 */
33bbeb90 533 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
534 if (wait_shm_fd >= 0) {
535 goto end;
536 } else if (wait_shm_fd < 0 && errno != ENOENT) {
537 /*
33bbeb90
MD
538 * Real-only open did not work, and it's not because the
539 * entry was not present. It's a failure that prohibits
540 * using shm.
58d4b2a2 541 */
7fc90dca 542 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 543 goto end;
7fc90dca
MD
544 }
545 /*
58d4b2a2
MD
546 * If the open failed because the file did not exist, try
547 * creating it ourself.
7fc90dca 548 */
e8508a49 549 lttng_ust_nest_count++;
58d4b2a2 550 pid = fork();
e8508a49 551 lttng_ust_nest_count--;
58d4b2a2
MD
552 if (pid > 0) {
553 int status;
554
555 /*
556 * Parent: wait for child to return, in which case the
557 * shared memory map will have been created.
558 */
559 pid = wait(&status);
b7d3cb32 560 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
561 wait_shm_fd = -1;
562 goto end;
7fc90dca 563 }
58d4b2a2
MD
564 /*
565 * Try to open read-only again after creation.
566 */
33bbeb90 567 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
568 if (wait_shm_fd < 0) {
569 /*
570 * Real-only open did not work. It's a failure
571 * that prohibits using shm.
572 */
573 ERR("Error opening shm %s", sock_info->wait_shm_path);
574 goto end;
575 }
576 goto end;
577 } else if (pid == 0) {
578 int create_mode;
579
580 /* Child */
33bbeb90 581 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 582 if (sock_info->global)
33bbeb90 583 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
584 /*
585 * We're alone in a child process, so we can modify the
586 * process-wide umask.
587 */
33bbeb90 588 umask(~create_mode);
58d4b2a2 589 /*
33bbeb90
MD
590 * Try creating shm (or get rw access).
591 * We don't do an exclusive open, because we allow other
592 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
593 */
594 wait_shm_fd = shm_open(sock_info->wait_shm_path,
595 O_RDWR | O_CREAT, create_mode);
596 if (wait_shm_fd >= 0) {
597 ret = ftruncate(wait_shm_fd, mmap_size);
598 if (ret) {
599 PERROR("ftruncate");
600 exit(EXIT_FAILURE);
601 }
58d4b2a2
MD
602 exit(EXIT_SUCCESS);
603 }
33bbeb90
MD
604 /*
605 * For local shm, we need to have rw access to accept
606 * opening it: this means the local sessiond will be
607 * able to wake us up. For global shm, we open it even
608 * if rw access is not granted, because the root.root
609 * sessiond will be able to override all rights and wake
610 * us up.
611 */
612 if (!sock_info->global && errno != EACCES) {
58d4b2a2
MD
613 ERR("Error opening shm %s", sock_info->wait_shm_path);
614 exit(EXIT_FAILURE);
615 }
616 /*
33bbeb90
MD
617 * The shm exists, but we cannot open it RW. Report
618 * success.
58d4b2a2
MD
619 */
620 exit(EXIT_SUCCESS);
621 } else {
622 return -1;
7fc90dca 623 }
58d4b2a2 624end:
33bbeb90
MD
625 if (wait_shm_fd >= 0 && !sock_info->global) {
626 struct stat statbuf;
627
628 /*
629 * Ensure that our user is the owner of the shm file for
630 * local shm. If we do not own the file, it means our
631 * sessiond will not have access to wake us up (there is
632 * probably a rogue process trying to fake our
633 * sessiond). Fallback to polling method in this case.
634 */
635 ret = fstat(wait_shm_fd, &statbuf);
636 if (ret) {
637 PERROR("fstat");
638 goto error_close;
639 }
640 if (statbuf.st_uid != getuid())
641 goto error_close;
642 }
58d4b2a2 643 return wait_shm_fd;
33bbeb90
MD
644
645error_close:
646 ret = close(wait_shm_fd);
647 if (ret) {
648 PERROR("Error closing fd");
649 }
650 return -1;
58d4b2a2
MD
651}
652
653static
654char *get_map_shm(struct sock_info *sock_info)
655{
656 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
657 int wait_shm_fd, ret;
658 char *wait_shm_mmap;
659
660 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
661 if (wait_shm_fd < 0) {
662 goto error;
44e073f5 663 }
7fc90dca
MD
664 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
665 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
666 /* close shm fd immediately after taking the mmap reference */
667 ret = close(wait_shm_fd);
668 if (ret) {
33bbeb90
MD
669 PERROR("Error closing fd");
670 }
671 if (wait_shm_mmap == MAP_FAILED) {
672 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
673 goto error;
7fc90dca
MD
674 }
675 return wait_shm_mmap;
676
677error:
678 return NULL;
679}
680
681static
682void wait_for_sessiond(struct sock_info *sock_info)
683{
efe0de09 684 int ret;
80e2814b 685
7fc90dca
MD
686 ust_lock();
687 if (lttng_ust_comm_should_quit) {
688 goto quit;
689 }
37ed587a
MD
690 if (wait_poll_fallback) {
691 goto error;
692 }
7fc90dca
MD
693 if (!sock_info->wait_shm_mmap) {
694 sock_info->wait_shm_mmap = get_map_shm(sock_info);
695 if (!sock_info->wait_shm_mmap)
696 goto error;
697 }
698 ust_unlock();
699
700 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b
MD
701 /* Wait for futex wakeup */
702 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
703 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
704 FUTEX_WAIT, 0, NULL, NULL, 0);
80e2814b 705 if (ret < 0) {
37ed587a
MD
706 if (errno == EFAULT) {
707 wait_poll_fallback = 1;
a8b870ad 708 DBG(
37ed587a
MD
709"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
710"do not support FUTEX_WAKE on read-only memory mappings correctly. "
711"Please upgrade your kernel "
712"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
713"mainline). LTTng-UST will use polling mode fallback.");
cd27263b
MD
714 if (ust_debug())
715 PERROR("futex");
37ed587a 716 }
80e2814b
MD
717 }
718 }
7fc90dca
MD
719 return;
720
721quit:
722 ust_unlock();
723 return;
724
725error:
726 ust_unlock();
7fc90dca 727 return;
46050b1a
MD
728}
729
1ea11eab
MD
730/*
731 * This thread does not allocate any resource, except within
732 * handle_message, within mutex protection. This mutex protects against
733 * fork and exit.
98bf993f 734 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
735 * is also protected by the mutex.
736 */
d9e99d10
MD
737static
738void *ust_listener_thread(void *arg)
739{
1ea11eab 740 struct sock_info *sock_info = arg;
c0eedf81 741 int sock, ret, prev_connect_failed = 0, has_waited = 0;
d9e99d10 742
9eb62b9c
MD
743 /* Restart trying to connect to the session daemon */
744restart:
c0eedf81
MD
745 if (prev_connect_failed) {
746 /* Wait for sessiond availability with pipe */
747 wait_for_sessiond(sock_info);
748 if (has_waited) {
749 has_waited = 0;
750 /*
751 * Sleep for 5 seconds before retrying after a
752 * sequence of failure / wait / failure. This
753 * deals with a killed or broken session daemon.
754 */
755 sleep(5);
756 }
757 has_waited = 1;
758 prev_connect_failed = 0;
759 }
17dfb34b 760 ust_lock();
1ea11eab
MD
761
762 if (lttng_ust_comm_should_quit) {
17dfb34b 763 ust_unlock();
1ea11eab
MD
764 goto quit;
765 }
9eb62b9c 766
1ea11eab 767 if (sock_info->socket != -1) {
e6973a89 768 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 769 if (ret) {
11ff9c7d 770 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
771 }
772 sock_info->socket = -1;
773 }
46050b1a 774
9eb62b9c 775 /* Register */
57773204 776 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 777 if (ret < 0) {
4d3c9523 778 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
c0eedf81 779 prev_connect_failed = 1;
11ff9c7d
MD
780 /*
781 * If we cannot find the sessiond daemon, don't delay
782 * constructor execution.
783 */
edaa1431 784 ret = handle_register_done(sock_info);
11ff9c7d 785 assert(!ret);
17dfb34b 786 ust_unlock();
1ea11eab 787 goto restart;
46050b1a
MD
788 }
789
790 sock_info->socket = sock = ret;
791
792 /*
793 * Create only one root handle per listener thread for the whole
794 * process lifetime.
795 */
796 if (sock_info->root_handle == -1) {
797 ret = lttng_abi_create_root_handle();
a51070bb 798 if (ret < 0) {
46050b1a 799 ERR("Error creating root handle");
17dfb34b 800 ust_unlock();
46050b1a
MD
801 goto quit;
802 }
803 sock_info->root_handle = ret;
9eb62b9c 804 }
1ea11eab 805
9eb62b9c
MD
806 ret = register_app_to_sessiond(sock);
807 if (ret < 0) {
11ff9c7d 808 ERR("Error registering to %s apps socket", sock_info->name);
c0eedf81 809 prev_connect_failed = 1;
11ff9c7d
MD
810 /*
811 * If we cannot register to the sessiond daemon, don't
812 * delay constructor execution.
813 */
edaa1431 814 ret = handle_register_done(sock_info);
11ff9c7d 815 assert(!ret);
17dfb34b 816 ust_unlock();
9eb62b9c
MD
817 goto restart;
818 }
17dfb34b 819 ust_unlock();
46050b1a 820
d9e99d10
MD
821 for (;;) {
822 ssize_t len;
57773204 823 struct ustcomm_ust_msg lum;
d9e99d10 824
57773204 825 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
826 switch (len) {
827 case 0: /* orderly shutdown */
11ff9c7d 828 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
8236ba10
MD
829 ust_lock();
830 /*
831 * Either sessiond has shutdown or refused us by closing the socket.
832 * In either case, we don't want to delay construction execution,
833 * and we need to wait before retry.
834 */
835 prev_connect_failed = 1;
836 /*
837 * If we cannot register to the sessiond daemon, don't
838 * delay constructor execution.
839 */
840 ret = handle_register_done(sock_info);
841 assert(!ret);
842 ust_unlock();
d9e99d10 843 goto end;
e7723462 844 case sizeof(lum):
d9e99d10 845 DBG("message received\n");
11ff9c7d 846 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 847 if (ret < 0) {
11ff9c7d 848 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
849 }
850 continue;
851 case -1:
8236ba10 852 DBG("Receive failed from lttng-sessiond with errno %d", errno);
d9e99d10 853 if (errno == ECONNRESET) {
11ff9c7d 854 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
855 goto end;
856 }
857 goto end;
858 default:
11ff9c7d 859 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
860 continue;
861 }
862
863 }
864end:
9eb62b9c 865 goto restart; /* try to reconnect */
1ea11eab 866quit:
d9e99d10
MD
867 return NULL;
868}
869
cf12a773
MD
870/*
871 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
872 */
11ff9c7d
MD
873static
874int get_timeout(struct timespec *constructor_timeout)
875{
cf12a773
MD
876 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
877 char *str_delay;
11ff9c7d
MD
878 int ret;
879
69400ac4 880 str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT");
cf12a773
MD
881 if (str_delay) {
882 constructor_delay_ms = strtol(str_delay, NULL, 10);
883 }
884
885 switch (constructor_delay_ms) {
886 case -1:/* fall-through */
887 case 0:
888 return constructor_delay_ms;
889 default:
890 break;
891 }
892
893 /*
894 * If we are unable to find the current time, don't wait.
895 */
896 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
897 if (ret) {
898 return -1;
899 }
95259bd0
MD
900 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
901 constructor_timeout->tv_nsec +=
902 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
903 if (constructor_timeout->tv_nsec >= 1000000000UL) {
904 constructor_timeout->tv_sec++;
905 constructor_timeout->tv_nsec -= 1000000000UL;
906 }
cf12a773 907 return 1;
11ff9c7d 908}
d9e99d10 909
2691221a
MD
910/*
911 * sessiond monitoring thread: monitor presence of global and per-user
912 * sessiond by polling the application common named pipe.
913 */
914/* TODO */
915
edaa1431 916void __attribute__((constructor)) lttng_ust_init(void)
2691221a 917{
11ff9c7d 918 struct timespec constructor_timeout;
ae6a58bf 919 sigset_t sig_all_blocked, orig_parent_mask;
cf12a773 920 int timeout_mode;
2691221a
MD
921 int ret;
922
edaa1431
MD
923 if (uatomic_xchg(&initialized, 1) == 1)
924 return;
925
eddd8d5d
MD
926 /*
927 * Fixup interdependency between TLS fixup mutex (which happens
928 * to be the dynamic linker mutex) and ust_lock, taken within
929 * the ust lock.
930 */
931 lttng_fixup_event_tls();
f645cfa7 932 lttng_fixup_ringbuffer_tls();
4158a15a 933 lttng_fixup_vtid_tls();
a903623f 934 lttng_fixup_nest_count_tls();
eddd8d5d 935
edaa1431
MD
936 /*
937 * We want precise control over the order in which we construct
938 * our sub-libraries vs starting to receive commands from
939 * sessiond (otherwise leading to errors when trying to create
940 * sessiond before the init functions are completed).
941 */
2691221a 942 init_usterr();
edaa1431
MD
943 init_tracepoint();
944 ltt_ring_buffer_metadata_client_init();
945 ltt_ring_buffer_client_overwrite_init();
946 ltt_ring_buffer_client_discard_init();
2691221a 947
cf12a773 948 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 949
95259bd0 950 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
951 assert(!ret);
952
8d20bf54 953 ret = setup_local_apps();
2691221a 954 if (ret) {
8d20bf54 955 ERR("Error setting up to local apps");
2691221a 956 }
ae6a58bf
WP
957
958 /* A new thread created by pthread_create inherits the signal mask
959 * from the parent. To avoid any signal being received by the
960 * listener thread, we block all signals temporarily in the parent,
961 * while we create the listener thread.
962 */
963 sigfillset(&sig_all_blocked);
964 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
965 if (ret) {
d94d802c 966 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
967 }
968
dde70ea0
MD
969 ret = pthread_create(&global_apps.ust_listener, NULL,
970 ust_listener_thread, &global_apps);
d94d802c
MD
971 if (ret) {
972 ERR("pthread_create global: %s", strerror(ret));
973 }
8d20bf54 974 if (local_apps.allowed) {
dde70ea0
MD
975 ret = pthread_create(&local_apps.ust_listener, NULL,
976 ust_listener_thread, &local_apps);
d94d802c
MD
977 if (ret) {
978 ERR("pthread_create local: %s", strerror(ret));
979 }
8d20bf54
MD
980 } else {
981 handle_register_done(&local_apps);
982 }
983
ae6a58bf
WP
984 /* Restore original signal mask in parent */
985 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
986 if (ret) {
d94d802c 987 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
988 }
989
cf12a773
MD
990 switch (timeout_mode) {
991 case 1: /* timeout wait */
95259bd0
MD
992 do {
993 ret = sem_timedwait(&constructor_wait,
994 &constructor_timeout);
995 } while (ret < 0 && errno == EINTR);
cf12a773
MD
996 if (ret < 0 && errno == ETIMEDOUT) {
997 ERR("Timed out waiting for ltt-sessiond");
998 } else {
999 assert(!ret);
1000 }
1001 break;
7b766b16 1002 case -1:/* wait forever */
95259bd0
MD
1003 do {
1004 ret = sem_wait(&constructor_wait);
1005 } while (ret < 0 && errno == EINTR);
11ff9c7d 1006 assert(!ret);
cf12a773 1007 break;
7b766b16 1008 case 0: /* no timeout */
cf12a773 1009 break;
11ff9c7d 1010 }
2691221a
MD
1011}
1012
17dfb34b
MD
1013static
1014void lttng_ust_cleanup(int exiting)
1015{
efe0de09 1016 cleanup_sock_info(&global_apps, exiting);
17dfb34b 1017 if (local_apps.allowed) {
efe0de09 1018 cleanup_sock_info(&local_apps, exiting);
17dfb34b 1019 }
efe0de09
MD
1020 /*
1021 * The teardown in this function all affect data structures
1022 * accessed under the UST lock by the listener thread. This
1023 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1024 * that none of these threads are accessing this data at this
1025 * point.
1026 */
17dfb34b 1027 lttng_ust_abi_exit();
003fedf4 1028 lttng_ust_events_exit();
17dfb34b
MD
1029 ltt_ring_buffer_client_discard_exit();
1030 ltt_ring_buffer_client_overwrite_exit();
1031 ltt_ring_buffer_metadata_client_exit();
1032 exit_tracepoint();
1033 if (!exiting) {
1034 /* Reinitialize values for fork */
1035 sem_count = 2;
1036 lttng_ust_comm_should_quit = 0;
1037 initialized = 0;
1038 }
1039}
1040
edaa1431 1041void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
1042{
1043 int ret;
1044
9eb62b9c
MD
1045 /*
1046 * Using pthread_cancel here because:
1047 * A) we don't want to hang application teardown.
1048 * B) the thread is not allocating any resource.
1049 */
1ea11eab
MD
1050
1051 /*
1052 * Require the communication thread to quit. Synchronize with
1053 * mutexes to ensure it is not in a mutex critical section when
1054 * pthread_cancel is later called.
1055 */
17dfb34b 1056 ust_lock();
1ea11eab 1057 lttng_ust_comm_should_quit = 1;
17dfb34b 1058 ust_unlock();
1ea11eab 1059
f5f94532 1060 /* cancel threads */
1ea11eab 1061 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c 1062 if (ret) {
d94d802c
MD
1063 ERR("Error cancelling global ust listener thread: %s",
1064 strerror(ret));
2691221a 1065 }
8d20bf54
MD
1066 if (local_apps.allowed) {
1067 ret = pthread_cancel(local_apps.ust_listener);
1068 if (ret) {
d94d802c
MD
1069 ERR("Error cancelling local ust listener thread: %s",
1070 strerror(ret));
8d20bf54 1071 }
8d20bf54 1072 }
efe0de09
MD
1073 /*
1074 * Do NOT join threads: use of sys_futex makes it impossible to
1075 * join the threads without using async-cancel, but async-cancel
1076 * is delivered by a signal, which could hit the target thread
1077 * anywhere in its code path, including while the ust_lock() is
1078 * held, causing a deadlock for the other thread. Let the OS
1079 * cleanup the threads if there are stalled in a syscall.
1080 */
17dfb34b 1081 lttng_ust_cleanup(1);
2691221a 1082}
e822f505
MD
1083
1084/*
1085 * We exclude the worker threads across fork and clone (except
1086 * CLONE_VM), because these system calls only keep the forking thread
1087 * running in the child. Therefore, we don't want to call fork or clone
1088 * in the middle of an tracepoint or ust tracing state modification.
1089 * Holding this mutex protects these structures across fork and clone.
1090 */
b728d87e 1091void ust_before_fork(sigset_t *save_sigset)
e822f505
MD
1092{
1093 /*
1094 * Disable signals. This is to avoid that the child intervenes
1095 * before it is properly setup for tracing. It is safer to
1096 * disable all signals, because then we know we are not breaking
1097 * anything by restoring the original mask.
1098 */
1099 sigset_t all_sigs;
1100 int ret;
1101
e8508a49
MD
1102 if (lttng_ust_nest_count)
1103 return;
e822f505
MD
1104 /* Disable signals */
1105 sigfillset(&all_sigs);
b728d87e 1106 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
1107 if (ret == -1) {
1108 PERROR("sigprocmask");
1109 }
17dfb34b 1110 ust_lock();
e822f505
MD
1111 rcu_bp_before_fork();
1112}
1113
b728d87e 1114static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
1115{
1116 int ret;
1117
17dfb34b
MD
1118 DBG("process %d", getpid());
1119 ust_unlock();
e822f505 1120 /* Restore signals */
23c8854a 1121 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
1122 if (ret == -1) {
1123 PERROR("sigprocmask");
1124 }
1125}
1126
b728d87e 1127void ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 1128{
e8508a49
MD
1129 if (lttng_ust_nest_count)
1130 return;
17dfb34b 1131 DBG("process %d", getpid());
e822f505
MD
1132 rcu_bp_after_fork_parent();
1133 /* Release mutexes and reenable signals */
b728d87e 1134 ust_after_fork_common(restore_sigset);
e822f505
MD
1135}
1136
17dfb34b
MD
1137/*
1138 * After fork, in the child, we need to cleanup all the leftover state,
1139 * except the worker thread which already magically disappeared thanks
1140 * to the weird Linux fork semantics. After tyding up, we call
1141 * lttng_ust_init() again to start over as a new PID.
1142 *
1143 * This is meant for forks() that have tracing in the child between the
1144 * fork and following exec call (if there is any).
1145 */
b728d87e 1146void ust_after_fork_child(sigset_t *restore_sigset)
e822f505 1147{
e8508a49
MD
1148 if (lttng_ust_nest_count)
1149 return;
17dfb34b 1150 DBG("process %d", getpid());
e822f505
MD
1151 /* Release urcu mutexes */
1152 rcu_bp_after_fork_child();
17dfb34b 1153 lttng_ust_cleanup(0);
a93bfc45 1154 lttng_context_vtid_reset();
e822f505 1155 /* Release mutexes and reenable signals */
b728d87e 1156 ust_after_fork_common(restore_sigset);
318dfea9 1157 lttng_ust_init();
e822f505 1158}
This page took 0.086248 seconds and 4 git commands to generate.