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