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