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