Fix: handle sysconf errors
[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 long page_size;
823
824 page_size = sysconf(_SC_PAGE_SIZE);
825 if (page_size > 0) {
826 ret = munmap(sock_info->wait_shm_mmap, page_size);
827 if (ret) {
828 ERR("Error unmapping wait shm");
829 }
830 }
831 sock_info->wait_shm_mmap = NULL;
832 }
833 }
834
835 /*
836 * Using fork to set umask in the child process (not multi-thread safe).
837 * We deal with the shm_open vs ftruncate race (happening when the
838 * sessiond owns the shm and does not let everybody modify it, to ensure
839 * safety against shm_unlink) by simply letting the mmap fail and
840 * retrying after a few seconds.
841 * For global shm, everybody has rw access to it until the sessiond
842 * starts.
843 */
844 static
845 int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
846 {
847 int wait_shm_fd, ret;
848 pid_t pid;
849
850 /*
851 * Try to open read-only.
852 */
853 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
854 if (wait_shm_fd >= 0) {
855 int32_t tmp_read;
856 ssize_t len;
857 size_t bytes_read = 0;
858
859 /*
860 * Try to read the fd. If unable to do so, try opening
861 * it in write mode.
862 */
863 do {
864 len = read(wait_shm_fd,
865 &((char *) &tmp_read)[bytes_read],
866 sizeof(tmp_read) - bytes_read);
867 if (len > 0) {
868 bytes_read += len;
869 }
870 } while ((len < 0 && errno == EINTR)
871 || (len > 0 && bytes_read < sizeof(tmp_read)));
872 if (bytes_read != sizeof(tmp_read)) {
873 ret = close(wait_shm_fd);
874 if (ret) {
875 ERR("close wait_shm_fd");
876 }
877 goto open_write;
878 }
879 goto end;
880 } else if (wait_shm_fd < 0 && errno != ENOENT) {
881 /*
882 * Real-only open did not work, and it's not because the
883 * entry was not present. It's a failure that prohibits
884 * using shm.
885 */
886 ERR("Error opening shm %s", sock_info->wait_shm_path);
887 goto end;
888 }
889
890 open_write:
891 /*
892 * If the open failed because the file did not exist, or because
893 * the file was not truncated yet, try creating it ourself.
894 */
895 URCU_TLS(lttng_ust_nest_count)++;
896 pid = fork();
897 URCU_TLS(lttng_ust_nest_count)--;
898 if (pid > 0) {
899 int status;
900
901 /*
902 * Parent: wait for child to return, in which case the
903 * shared memory map will have been created.
904 */
905 pid = wait(&status);
906 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
907 wait_shm_fd = -1;
908 goto end;
909 }
910 /*
911 * Try to open read-only again after creation.
912 */
913 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
914 if (wait_shm_fd < 0) {
915 /*
916 * Real-only open did not work. It's a failure
917 * that prohibits using shm.
918 */
919 ERR("Error opening shm %s", sock_info->wait_shm_path);
920 goto end;
921 }
922 goto end;
923 } else if (pid == 0) {
924 int create_mode;
925
926 /* Child */
927 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
928 if (sock_info->global)
929 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
930 /*
931 * We're alone in a child process, so we can modify the
932 * process-wide umask.
933 */
934 umask(~create_mode);
935 /*
936 * Try creating shm (or get rw access).
937 * We don't do an exclusive open, because we allow other
938 * processes to create+ftruncate it concurrently.
939 */
940 wait_shm_fd = shm_open(sock_info->wait_shm_path,
941 O_RDWR | O_CREAT, create_mode);
942 if (wait_shm_fd >= 0) {
943 ret = ftruncate(wait_shm_fd, mmap_size);
944 if (ret) {
945 PERROR("ftruncate");
946 _exit(EXIT_FAILURE);
947 }
948 _exit(EXIT_SUCCESS);
949 }
950 /*
951 * For local shm, we need to have rw access to accept
952 * opening it: this means the local sessiond will be
953 * able to wake us up. For global shm, we open it even
954 * if rw access is not granted, because the root.root
955 * sessiond will be able to override all rights and wake
956 * us up.
957 */
958 if (!sock_info->global && errno != EACCES) {
959 ERR("Error opening shm %s", sock_info->wait_shm_path);
960 _exit(EXIT_FAILURE);
961 }
962 /*
963 * The shm exists, but we cannot open it RW. Report
964 * success.
965 */
966 _exit(EXIT_SUCCESS);
967 } else {
968 return -1;
969 }
970 end:
971 if (wait_shm_fd >= 0 && !sock_info->global) {
972 struct stat statbuf;
973
974 /*
975 * Ensure that our user is the owner of the shm file for
976 * local shm. If we do not own the file, it means our
977 * sessiond will not have access to wake us up (there is
978 * probably a rogue process trying to fake our
979 * sessiond). Fallback to polling method in this case.
980 */
981 ret = fstat(wait_shm_fd, &statbuf);
982 if (ret) {
983 PERROR("fstat");
984 goto error_close;
985 }
986 if (statbuf.st_uid != getuid())
987 goto error_close;
988 }
989 return wait_shm_fd;
990
991 error_close:
992 ret = close(wait_shm_fd);
993 if (ret) {
994 PERROR("Error closing fd");
995 }
996 return -1;
997 }
998
999 static
1000 char *get_map_shm(struct sock_info *sock_info)
1001 {
1002 long page_size;
1003 int wait_shm_fd, ret;
1004 char *wait_shm_mmap;
1005
1006 page_size = sysconf(_SC_PAGE_SIZE);
1007 if (page_size < 0) {
1008 goto error;
1009 }
1010
1011 wait_shm_fd = get_wait_shm(sock_info, page_size);
1012 if (wait_shm_fd < 0) {
1013 goto error;
1014 }
1015 wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
1016 MAP_SHARED, wait_shm_fd, 0);
1017 /* close shm fd immediately after taking the mmap reference */
1018 ret = close(wait_shm_fd);
1019 if (ret) {
1020 PERROR("Error closing fd");
1021 }
1022 if (wait_shm_mmap == MAP_FAILED) {
1023 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1024 goto error;
1025 }
1026 return wait_shm_mmap;
1027
1028 error:
1029 return NULL;
1030 }
1031
1032 static
1033 void wait_for_sessiond(struct sock_info *sock_info)
1034 {
1035 int ret;
1036
1037 if (ust_lock()) {
1038 goto quit;
1039 }
1040 if (wait_poll_fallback) {
1041 goto error;
1042 }
1043 if (!sock_info->wait_shm_mmap) {
1044 sock_info->wait_shm_mmap = get_map_shm(sock_info);
1045 if (!sock_info->wait_shm_mmap)
1046 goto error;
1047 }
1048 ust_unlock();
1049
1050 DBG("Waiting for %s apps sessiond", sock_info->name);
1051 /* Wait for futex wakeup */
1052 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
1053 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
1054 FUTEX_WAIT, 0, NULL, NULL, 0);
1055 if (ret < 0) {
1056 if (errno == EFAULT) {
1057 wait_poll_fallback = 1;
1058 DBG(
1059 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1060 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1061 "Please upgrade your kernel "
1062 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1063 "mainline). LTTng-UST will use polling mode fallback.");
1064 if (ust_debug())
1065 PERROR("futex");
1066 }
1067 }
1068 }
1069 return;
1070
1071 quit:
1072 ust_unlock();
1073 return;
1074
1075 error:
1076 ust_unlock();
1077 return;
1078 }
1079
1080 /*
1081 * This thread does not allocate any resource, except within
1082 * handle_message, within mutex protection. This mutex protects against
1083 * fork and exit.
1084 * The other moment it allocates resources is at socket connection, which
1085 * is also protected by the mutex.
1086 */
1087 static
1088 void *ust_listener_thread(void *arg)
1089 {
1090 struct sock_info *sock_info = arg;
1091 int sock, ret, prev_connect_failed = 0, has_waited = 0;
1092 long timeout;
1093
1094 /* Restart trying to connect to the session daemon */
1095 restart:
1096 if (prev_connect_failed) {
1097 /* Wait for sessiond availability with pipe */
1098 wait_for_sessiond(sock_info);
1099 if (has_waited) {
1100 has_waited = 0;
1101 /*
1102 * Sleep for 5 seconds before retrying after a
1103 * sequence of failure / wait / failure. This
1104 * deals with a killed or broken session daemon.
1105 */
1106 sleep(5);
1107 }
1108 has_waited = 1;
1109 prev_connect_failed = 0;
1110 }
1111
1112 if (sock_info->socket != -1) {
1113 ret = ustcomm_close_unix_sock(sock_info->socket);
1114 if (ret) {
1115 ERR("Error closing %s ust cmd socket",
1116 sock_info->name);
1117 }
1118 sock_info->socket = -1;
1119 }
1120 if (sock_info->notify_socket != -1) {
1121 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1122 if (ret) {
1123 ERR("Error closing %s ust notify socket",
1124 sock_info->name);
1125 }
1126 sock_info->notify_socket = -1;
1127 }
1128
1129 /*
1130 * Register. We need to perform both connect and sending
1131 * registration message before doing the next connect otherwise
1132 * we may reach unix socket connect queue max limits and block
1133 * on the 2nd connect while the session daemon is awaiting the
1134 * first connect registration message.
1135 */
1136 /* Connect cmd socket */
1137 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
1138 if (ret < 0) {
1139 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1140 prev_connect_failed = 1;
1141
1142 if (ust_lock()) {
1143 goto quit;
1144 }
1145
1146 /*
1147 * If we cannot find the sessiond daemon, don't delay
1148 * constructor execution.
1149 */
1150 ret = handle_register_done(sock_info);
1151 assert(!ret);
1152 ust_unlock();
1153 goto restart;
1154 }
1155 sock_info->socket = ret;
1156
1157 if (ust_lock()) {
1158 goto quit;
1159 }
1160
1161 /*
1162 * Create only one root handle per listener thread for the whole
1163 * process lifetime, so we ensure we get ID which is statically
1164 * assigned to the root handle.
1165 */
1166 if (sock_info->root_handle == -1) {
1167 ret = lttng_abi_create_root_handle();
1168 if (ret < 0) {
1169 ERR("Error creating root handle");
1170 goto quit;
1171 }
1172 sock_info->root_handle = ret;
1173 }
1174
1175 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
1176 if (ret < 0) {
1177 ERR("Error registering to %s ust cmd socket",
1178 sock_info->name);
1179 prev_connect_failed = 1;
1180 /*
1181 * If we cannot register to the sessiond daemon, don't
1182 * delay constructor execution.
1183 */
1184 ret = handle_register_done(sock_info);
1185 assert(!ret);
1186 ust_unlock();
1187 goto restart;
1188 }
1189
1190 ust_unlock();
1191
1192 /* Connect notify socket */
1193 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
1194 if (ret < 0) {
1195 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1196 prev_connect_failed = 1;
1197
1198 if (ust_lock()) {
1199 goto quit;
1200 }
1201
1202 /*
1203 * If we cannot find the sessiond daemon, don't delay
1204 * constructor execution.
1205 */
1206 ret = handle_register_done(sock_info);
1207 assert(!ret);
1208 ust_unlock();
1209 goto restart;
1210 }
1211 sock_info->notify_socket = ret;
1212
1213 timeout = get_notify_sock_timeout();
1214 if (timeout >= 0) {
1215 /*
1216 * Give at least 10ms to sessiond to reply to
1217 * notifications.
1218 */
1219 if (timeout < 10)
1220 timeout = 10;
1221 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1222 timeout);
1223 if (ret < 0) {
1224 WARN("Error setting socket receive timeout");
1225 }
1226 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1227 timeout);
1228 if (ret < 0) {
1229 WARN("Error setting socket send timeout");
1230 }
1231 } else if (timeout < -1) {
1232 WARN("Unsupported timeout value %ld", timeout);
1233 }
1234
1235 if (ust_lock()) {
1236 goto quit;
1237 }
1238
1239 ret = register_to_sessiond(sock_info->notify_socket,
1240 USTCTL_SOCKET_NOTIFY);
1241 if (ret < 0) {
1242 ERR("Error registering to %s ust notify socket",
1243 sock_info->name);
1244 prev_connect_failed = 1;
1245 /*
1246 * If we cannot register to the sessiond daemon, don't
1247 * delay constructor execution.
1248 */
1249 ret = handle_register_done(sock_info);
1250 assert(!ret);
1251 ust_unlock();
1252 goto restart;
1253 }
1254 sock = sock_info->socket;
1255
1256 ust_unlock();
1257
1258 for (;;) {
1259 ssize_t len;
1260 struct ustcomm_ust_msg lum;
1261
1262 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
1263 switch (len) {
1264 case 0: /* orderly shutdown */
1265 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
1266 if (ust_lock()) {
1267 goto quit;
1268 }
1269 /*
1270 * Either sessiond has shutdown or refused us by closing the socket.
1271 * In either case, we don't want to delay construction execution,
1272 * and we need to wait before retry.
1273 */
1274 prev_connect_failed = 1;
1275 /*
1276 * If we cannot register to the sessiond daemon, don't
1277 * delay constructor execution.
1278 */
1279 ret = handle_register_done(sock_info);
1280 assert(!ret);
1281 ust_unlock();
1282 goto end;
1283 case sizeof(lum):
1284 print_cmd(lum.cmd, lum.handle);
1285 ret = handle_message(sock_info, sock, &lum);
1286 if (ret) {
1287 ERR("Error handling message for %s socket", sock_info->name);
1288 }
1289 continue;
1290 default:
1291 if (len < 0) {
1292 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1293 } else {
1294 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1295 }
1296 if (len == -ECONNRESET) {
1297 DBG("%s remote end closed connection", sock_info->name);
1298 goto end;
1299 }
1300 goto end;
1301 }
1302
1303 }
1304 end:
1305 if (ust_lock()) {
1306 goto quit;
1307 }
1308 /* Cleanup socket handles before trying to reconnect */
1309 lttng_ust_objd_table_owner_cleanup(sock_info);
1310 ust_unlock();
1311 goto restart; /* try to reconnect */
1312
1313 quit:
1314 ust_unlock();
1315
1316 pthread_mutex_lock(&ust_exit_mutex);
1317 sock_info->thread_active = 0;
1318 pthread_mutex_unlock(&ust_exit_mutex);
1319 return NULL;
1320 }
1321
1322 /*
1323 * Weak symbol to call when the ust malloc wrapper is not loaded.
1324 */
1325 __attribute__((weak))
1326 void lttng_ust_malloc_wrapper_init(void)
1327 {
1328 }
1329
1330 /*
1331 * sessiond monitoring thread: monitor presence of global and per-user
1332 * sessiond by polling the application common named pipe.
1333 */
1334 void __attribute__((constructor)) lttng_ust_init(void)
1335 {
1336 struct timespec constructor_timeout;
1337 sigset_t sig_all_blocked, orig_parent_mask;
1338 pthread_attr_t thread_attr;
1339 int timeout_mode;
1340 int ret;
1341
1342 if (uatomic_xchg(&initialized, 1) == 1)
1343 return;
1344
1345 /*
1346 * Fixup interdependency between TLS fixup mutex (which happens
1347 * to be the dynamic linker mutex) and ust_lock, taken within
1348 * the ust lock.
1349 */
1350 lttng_fixup_ringbuffer_tls();
1351 lttng_fixup_vtid_tls();
1352 lttng_fixup_nest_count_tls();
1353 lttng_fixup_procname_tls();
1354
1355 /*
1356 * We want precise control over the order in which we construct
1357 * our sub-libraries vs starting to receive commands from
1358 * sessiond (otherwise leading to errors when trying to create
1359 * sessiond before the init functions are completed).
1360 */
1361 init_usterr();
1362 init_tracepoint();
1363 lttng_ust_baddr_statedump_init();
1364 lttng_ring_buffer_metadata_client_init();
1365 lttng_ring_buffer_client_overwrite_init();
1366 lttng_ring_buffer_client_overwrite_rt_init();
1367 lttng_ring_buffer_client_discard_init();
1368 lttng_ring_buffer_client_discard_rt_init();
1369 lttng_context_init();
1370 /*
1371 * Invoke ust malloc wrapper init before starting other threads.
1372 */
1373 lttng_ust_malloc_wrapper_init();
1374
1375 timeout_mode = get_constructor_timeout(&constructor_timeout);
1376
1377 ret = sem_init(&constructor_wait, 0, 0);
1378 assert(!ret);
1379
1380 ret = setup_local_apps();
1381 if (ret) {
1382 DBG("local apps setup returned %d", ret);
1383 }
1384
1385 /* A new thread created by pthread_create inherits the signal mask
1386 * from the parent. To avoid any signal being received by the
1387 * listener thread, we block all signals temporarily in the parent,
1388 * while we create the listener thread.
1389 */
1390 sigfillset(&sig_all_blocked);
1391 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1392 if (ret) {
1393 ERR("pthread_sigmask: %s", strerror(ret));
1394 }
1395
1396 ret = pthread_attr_init(&thread_attr);
1397 if (ret) {
1398 ERR("pthread_attr_init: %s", strerror(ret));
1399 }
1400 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1401 if (ret) {
1402 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1403 }
1404
1405 pthread_mutex_lock(&ust_exit_mutex);
1406 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
1407 ust_listener_thread, &global_apps);
1408 if (ret) {
1409 ERR("pthread_create global: %s", strerror(ret));
1410 }
1411 global_apps.thread_active = 1;
1412 pthread_mutex_unlock(&ust_exit_mutex);
1413
1414 if (local_apps.allowed) {
1415 pthread_mutex_lock(&ust_exit_mutex);
1416 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
1417 ust_listener_thread, &local_apps);
1418 if (ret) {
1419 ERR("pthread_create local: %s", strerror(ret));
1420 }
1421 local_apps.thread_active = 1;
1422 pthread_mutex_unlock(&ust_exit_mutex);
1423 } else {
1424 handle_register_done(&local_apps);
1425 }
1426 ret = pthread_attr_destroy(&thread_attr);
1427 if (ret) {
1428 ERR("pthread_attr_destroy: %s", strerror(ret));
1429 }
1430
1431 /* Restore original signal mask in parent */
1432 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1433 if (ret) {
1434 ERR("pthread_sigmask: %s", strerror(ret));
1435 }
1436
1437 switch (timeout_mode) {
1438 case 1: /* timeout wait */
1439 do {
1440 ret = sem_timedwait(&constructor_wait,
1441 &constructor_timeout);
1442 } while (ret < 0 && errno == EINTR);
1443 if (ret < 0 && errno == ETIMEDOUT) {
1444 ERR("Timed out waiting for lttng-sessiond");
1445 } else {
1446 assert(!ret);
1447 }
1448 break;
1449 case -1:/* wait forever */
1450 do {
1451 ret = sem_wait(&constructor_wait);
1452 } while (ret < 0 && errno == EINTR);
1453 assert(!ret);
1454 break;
1455 case 0: /* no timeout */
1456 break;
1457 }
1458 }
1459
1460 static
1461 void lttng_ust_cleanup(int exiting)
1462 {
1463 cleanup_sock_info(&global_apps, exiting);
1464 if (local_apps.allowed) {
1465 cleanup_sock_info(&local_apps, exiting);
1466 }
1467 /*
1468 * The teardown in this function all affect data structures
1469 * accessed under the UST lock by the listener thread. This
1470 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1471 * that none of these threads are accessing this data at this
1472 * point.
1473 */
1474 lttng_ust_abi_exit();
1475 lttng_ust_events_exit();
1476 lttng_context_exit();
1477 lttng_ring_buffer_client_discard_rt_exit();
1478 lttng_ring_buffer_client_discard_exit();
1479 lttng_ring_buffer_client_overwrite_rt_exit();
1480 lttng_ring_buffer_client_overwrite_exit();
1481 lttng_ring_buffer_metadata_client_exit();
1482 lttng_ust_baddr_statedump_destroy();
1483 exit_tracepoint();
1484 if (!exiting) {
1485 /* Reinitialize values for fork */
1486 sem_count = 2;
1487 lttng_ust_comm_should_quit = 0;
1488 initialized = 0;
1489 }
1490 }
1491
1492 void __attribute__((destructor)) lttng_ust_exit(void)
1493 {
1494 int ret;
1495
1496 /*
1497 * Using pthread_cancel here because:
1498 * A) we don't want to hang application teardown.
1499 * B) the thread is not allocating any resource.
1500 */
1501
1502 /*
1503 * Require the communication thread to quit. Synchronize with
1504 * mutexes to ensure it is not in a mutex critical section when
1505 * pthread_cancel is later called.
1506 */
1507 ust_lock_nocheck();
1508 lttng_ust_comm_should_quit = 1;
1509 ust_unlock();
1510
1511 pthread_mutex_lock(&ust_exit_mutex);
1512 /* cancel threads */
1513 if (global_apps.thread_active) {
1514 ret = pthread_cancel(global_apps.ust_listener);
1515 if (ret) {
1516 ERR("Error cancelling global ust listener thread: %s",
1517 strerror(ret));
1518 } else {
1519 global_apps.thread_active = 0;
1520 }
1521 }
1522 if (local_apps.thread_active) {
1523 ret = pthread_cancel(local_apps.ust_listener);
1524 if (ret) {
1525 ERR("Error cancelling local ust listener thread: %s",
1526 strerror(ret));
1527 } else {
1528 local_apps.thread_active = 0;
1529 }
1530 }
1531 pthread_mutex_unlock(&ust_exit_mutex);
1532
1533 /*
1534 * Do NOT join threads: use of sys_futex makes it impossible to
1535 * join the threads without using async-cancel, but async-cancel
1536 * is delivered by a signal, which could hit the target thread
1537 * anywhere in its code path, including while the ust_lock() is
1538 * held, causing a deadlock for the other thread. Let the OS
1539 * cleanup the threads if there are stalled in a syscall.
1540 */
1541 lttng_ust_cleanup(1);
1542 }
1543
1544 /*
1545 * We exclude the worker threads across fork and clone (except
1546 * CLONE_VM), because these system calls only keep the forking thread
1547 * running in the child. Therefore, we don't want to call fork or clone
1548 * in the middle of an tracepoint or ust tracing state modification.
1549 * Holding this mutex protects these structures across fork and clone.
1550 */
1551 void ust_before_fork(sigset_t *save_sigset)
1552 {
1553 /*
1554 * Disable signals. This is to avoid that the child intervenes
1555 * before it is properly setup for tracing. It is safer to
1556 * disable all signals, because then we know we are not breaking
1557 * anything by restoring the original mask.
1558 */
1559 sigset_t all_sigs;
1560 int ret;
1561
1562 if (URCU_TLS(lttng_ust_nest_count))
1563 return;
1564 /* Disable signals */
1565 sigfillset(&all_sigs);
1566 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
1567 if (ret == -1) {
1568 PERROR("sigprocmask");
1569 }
1570 ust_lock_nocheck();
1571 rcu_bp_before_fork();
1572 }
1573
1574 static void ust_after_fork_common(sigset_t *restore_sigset)
1575 {
1576 int ret;
1577
1578 DBG("process %d", getpid());
1579 ust_unlock();
1580 /* Restore signals */
1581 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
1582 if (ret == -1) {
1583 PERROR("sigprocmask");
1584 }
1585 }
1586
1587 void ust_after_fork_parent(sigset_t *restore_sigset)
1588 {
1589 if (URCU_TLS(lttng_ust_nest_count))
1590 return;
1591 DBG("process %d", getpid());
1592 rcu_bp_after_fork_parent();
1593 /* Release mutexes and reenable signals */
1594 ust_after_fork_common(restore_sigset);
1595 }
1596
1597 /*
1598 * After fork, in the child, we need to cleanup all the leftover state,
1599 * except the worker thread which already magically disappeared thanks
1600 * to the weird Linux fork semantics. After tyding up, we call
1601 * lttng_ust_init() again to start over as a new PID.
1602 *
1603 * This is meant for forks() that have tracing in the child between the
1604 * fork and following exec call (if there is any).
1605 */
1606 void ust_after_fork_child(sigset_t *restore_sigset)
1607 {
1608 if (URCU_TLS(lttng_ust_nest_count))
1609 return;
1610 DBG("process %d", getpid());
1611 /* Release urcu mutexes */
1612 rcu_bp_after_fork_child();
1613 lttng_ust_cleanup(0);
1614 lttng_context_vtid_reset();
1615 /* Release mutexes and reenable signals */
1616 ust_after_fork_common(restore_sigset);
1617 lttng_ust_init();
1618 }
1619
1620 void lttng_ust_sockinfo_session_enabled(void *owner)
1621 {
1622 struct sock_info *sock_info = owner;
1623 sock_info->statedump_pending = 1;
1624 }
This page took 0.060515 seconds and 5 git commands to generate.