Wait for the destruction of sessions before tearing down main thread
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <paths.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <urcu/uatomic.h>
39 #include <unistd.h>
40 #include <ctype.h>
41
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
55
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
58 #include "channel.h"
59 #include "cmd.h"
60 #include "consumer.h"
61 #include "context.h"
62 #include "event.h"
63 #include "kernel.h"
64 #include "kernel-consumer.h"
65 #include "modprobe.h"
66 #include "shm.h"
67 #include "ust-ctl.h"
68 #include "ust-consumer.h"
69 #include "utils.h"
70 #include "fd-limit.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
75 #include "save.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
81 #include "agent.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
84 #include "timer.h"
85
86 static const char *help_msg =
87 #ifdef LTTNG_EMBED_HELP
88 #include <lttng-sessiond.8.h>
89 #else
90 NULL
91 #endif
92 ;
93
94 const char *progname;
95 static int lockfile_fd = -1;
96
97 /* Set to 1 when a SIGUSR1 signal is received. */
98 static int recv_child_signal;
99
100 static struct lttng_kernel_tracer_version kernel_tracer_version;
101 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
102
103 /*
104 * Consumer daemon specific control data. Every value not initialized here is
105 * set to 0 by the static definition.
106 */
107 static struct consumer_data kconsumer_data = {
108 .type = LTTNG_CONSUMER_KERNEL,
109 .err_sock = -1,
110 .cmd_sock = -1,
111 .channel_monitor_pipe = -1,
112 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
113 .lock = PTHREAD_MUTEX_INITIALIZER,
114 .cond = PTHREAD_COND_INITIALIZER,
115 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
116 };
117 static struct consumer_data ustconsumer64_data = {
118 .type = LTTNG_CONSUMER64_UST,
119 .err_sock = -1,
120 .cmd_sock = -1,
121 .channel_monitor_pipe = -1,
122 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
123 .lock = PTHREAD_MUTEX_INITIALIZER,
124 .cond = PTHREAD_COND_INITIALIZER,
125 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
126 };
127 static struct consumer_data ustconsumer32_data = {
128 .type = LTTNG_CONSUMER32_UST,
129 .err_sock = -1,
130 .cmd_sock = -1,
131 .channel_monitor_pipe = -1,
132 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
133 .lock = PTHREAD_MUTEX_INITIALIZER,
134 .cond = PTHREAD_COND_INITIALIZER,
135 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
136 };
137
138 /* Command line options */
139 static const struct option long_options[] = {
140 { "client-sock", required_argument, 0, 'c' },
141 { "apps-sock", required_argument, 0, 'a' },
142 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
143 { "kconsumerd-err-sock", required_argument, 0, '\0' },
144 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
145 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
146 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
147 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
148 { "consumerd32-path", required_argument, 0, '\0' },
149 { "consumerd32-libdir", required_argument, 0, '\0' },
150 { "consumerd64-path", required_argument, 0, '\0' },
151 { "consumerd64-libdir", required_argument, 0, '\0' },
152 { "daemonize", no_argument, 0, 'd' },
153 { "background", no_argument, 0, 'b' },
154 { "sig-parent", no_argument, 0, 'S' },
155 { "help", no_argument, 0, 'h' },
156 { "group", required_argument, 0, 'g' },
157 { "version", no_argument, 0, 'V' },
158 { "quiet", no_argument, 0, 'q' },
159 { "verbose", no_argument, 0, 'v' },
160 { "verbose-consumer", no_argument, 0, '\0' },
161 { "no-kernel", no_argument, 0, '\0' },
162 { "pidfile", required_argument, 0, 'p' },
163 { "agent-tcp-port", required_argument, 0, '\0' },
164 { "config", required_argument, 0, 'f' },
165 { "load", required_argument, 0, 'l' },
166 { "kmod-probes", required_argument, 0, '\0' },
167 { "extra-kmod-probes", required_argument, 0, '\0' },
168 { NULL, 0, 0, 0 }
169 };
170
171 /* Command line options to ignore from configuration file */
172 static const char *config_ignore_options[] = { "help", "version", "config" };
173
174 /* Shared between threads */
175 static int dispatch_thread_exit;
176
177 /* Sockets and FDs */
178 static int client_sock = -1;
179 static int apps_sock = -1;
180
181 /*
182 * This pipe is used to inform the thread managing application communication
183 * that a command is queued and ready to be processed.
184 */
185 static int apps_cmd_pipe[2] = { -1, -1 };
186
187 /* Pthread, Mutexes and Semaphores */
188 static pthread_t apps_thread;
189 static pthread_t apps_notify_thread;
190 static pthread_t reg_apps_thread;
191 static pthread_t client_thread;
192 static pthread_t kernel_thread;
193 static pthread_t dispatch_thread;
194 static pthread_t health_thread;
195 static pthread_t ht_cleanup_thread;
196 static pthread_t agent_reg_thread;
197 static pthread_t load_session_thread;
198 static pthread_t notification_thread;
199 static pthread_t rotation_thread;
200 static pthread_t timer_thread;
201
202 /*
203 * UST registration command queue. This queue is tied with a futex and uses a N
204 * wakers / 1 waiter implemented and detailed in futex.c/.h
205 *
206 * The thread_registration_apps and thread_dispatch_ust_registration uses this
207 * queue along with the wait/wake scheme. The thread_manage_apps receives down
208 * the line new application socket and monitors it for any I/O error or clean
209 * close that triggers an unregistration of the application.
210 */
211 static struct ust_cmd_queue ust_cmd_queue;
212
213 static const char *module_proc_lttng = "/proc/lttng";
214
215 /*
216 * Consumer daemon state which is changed when spawning it, killing it or in
217 * case of a fatal error.
218 */
219 enum consumerd_state {
220 CONSUMER_STARTED = 1,
221 CONSUMER_STOPPED = 2,
222 CONSUMER_ERROR = 3,
223 };
224
225 /*
226 * This consumer daemon state is used to validate if a client command will be
227 * able to reach the consumer. If not, the client is informed. For instance,
228 * doing a "lttng start" when the consumer state is set to ERROR will return an
229 * error to the client.
230 *
231 * The following example shows a possible race condition of this scheme:
232 *
233 * consumer thread error happens
234 * client cmd arrives
235 * client cmd checks state -> still OK
236 * consumer thread exit, sets error
237 * client cmd try to talk to consumer
238 * ...
239 *
240 * However, since the consumer is a different daemon, we have no way of making
241 * sure the command will reach it safely even with this state flag. This is why
242 * we consider that up to the state validation during command processing, the
243 * command is safe. After that, we can not guarantee the correctness of the
244 * client request vis-a-vis the consumer.
245 */
246 static enum consumerd_state ust_consumerd_state;
247 static enum consumerd_state kernel_consumerd_state;
248
249 /* Load session thread information to operate. */
250 static struct load_session_thread_data *load_info;
251
252 /*
253 * Section name to look for in the daemon configuration file.
254 */
255 static const char * const config_section_name = "sessiond";
256
257 /* Am I root or not. Set to 1 if the daemon is running as root */
258 static int is_root;
259
260 /* Rotation thread handle. */
261 static struct rotation_thread_handle *rotation_thread_handle;
262
263 /*
264 * Stop all threads by closing the thread quit pipe.
265 */
266 static void stop_threads(void)
267 {
268 int ret;
269
270 /* Stopping all threads */
271 DBG("Terminating all threads");
272 ret = sessiond_notify_quit_pipe();
273 if (ret < 0) {
274 ERR("write error on thread quit pipe");
275 }
276
277 /* Dispatch thread */
278 CMM_STORE_SHARED(dispatch_thread_exit, 1);
279 futex_nto1_wake(&ust_cmd_queue.futex);
280 }
281
282 /*
283 * Close every consumer sockets.
284 */
285 static void close_consumer_sockets(void)
286 {
287 int ret;
288
289 if (kconsumer_data.err_sock >= 0) {
290 ret = close(kconsumer_data.err_sock);
291 if (ret < 0) {
292 PERROR("kernel consumer err_sock close");
293 }
294 }
295 if (ustconsumer32_data.err_sock >= 0) {
296 ret = close(ustconsumer32_data.err_sock);
297 if (ret < 0) {
298 PERROR("UST consumerd32 err_sock close");
299 }
300 }
301 if (ustconsumer64_data.err_sock >= 0) {
302 ret = close(ustconsumer64_data.err_sock);
303 if (ret < 0) {
304 PERROR("UST consumerd64 err_sock close");
305 }
306 }
307 if (kconsumer_data.cmd_sock >= 0) {
308 ret = close(kconsumer_data.cmd_sock);
309 if (ret < 0) {
310 PERROR("kernel consumer cmd_sock close");
311 }
312 }
313 if (ustconsumer32_data.cmd_sock >= 0) {
314 ret = close(ustconsumer32_data.cmd_sock);
315 if (ret < 0) {
316 PERROR("UST consumerd32 cmd_sock close");
317 }
318 }
319 if (ustconsumer64_data.cmd_sock >= 0) {
320 ret = close(ustconsumer64_data.cmd_sock);
321 if (ret < 0) {
322 PERROR("UST consumerd64 cmd_sock close");
323 }
324 }
325 if (kconsumer_data.channel_monitor_pipe >= 0) {
326 ret = close(kconsumer_data.channel_monitor_pipe);
327 if (ret < 0) {
328 PERROR("kernel consumer channel monitor pipe close");
329 }
330 }
331 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
332 ret = close(ustconsumer32_data.channel_monitor_pipe);
333 if (ret < 0) {
334 PERROR("UST consumerd32 channel monitor pipe close");
335 }
336 }
337 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
338 ret = close(ustconsumer64_data.channel_monitor_pipe);
339 if (ret < 0) {
340 PERROR("UST consumerd64 channel monitor pipe close");
341 }
342 }
343 }
344
345 /*
346 * Wait on consumer process termination.
347 *
348 * Need to be called with the consumer data lock held or from a context
349 * ensuring no concurrent access to data (e.g: cleanup).
350 */
351 static void wait_consumer(struct consumer_data *consumer_data)
352 {
353 pid_t ret;
354 int status;
355
356 if (consumer_data->pid <= 0) {
357 return;
358 }
359
360 DBG("Waiting for complete teardown of consumerd (PID: %d)",
361 consumer_data->pid);
362 ret = waitpid(consumer_data->pid, &status, 0);
363 if (ret == -1) {
364 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
365 } else if (!WIFEXITED(status)) {
366 ERR("consumerd termination with error: %d",
367 WEXITSTATUS(ret));
368 }
369 consumer_data->pid = 0;
370 }
371
372 /*
373 * Cleanup the session daemon's data structures.
374 */
375 static void sessiond_cleanup(void)
376 {
377 int ret;
378 struct ltt_session_list *session_list = session_get_list();
379
380 DBG("Cleanup sessiond");
381
382 /*
383 * Close the thread quit pipe. It has already done its job,
384 * since we are now called.
385 */
386 sessiond_close_quit_pipe();
387
388 ret = remove(config.pid_file_path.value);
389 if (ret < 0) {
390 PERROR("remove pidfile %s", config.pid_file_path.value);
391 }
392
393 DBG("Removing sessiond and consumerd content of directory %s",
394 config.rundir.value);
395
396 /* sessiond */
397 DBG("Removing %s", config.pid_file_path.value);
398 (void) unlink(config.pid_file_path.value);
399
400 DBG("Removing %s", config.agent_port_file_path.value);
401 (void) unlink(config.agent_port_file_path.value);
402
403 /* kconsumerd */
404 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
405 (void) unlink(kconsumer_data.err_unix_sock_path);
406
407 DBG("Removing directory %s", config.kconsumerd_path.value);
408 (void) rmdir(config.kconsumerd_path.value);
409
410 /* ust consumerd 32 */
411 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
412 (void) unlink(config.consumerd32_err_unix_sock_path.value);
413
414 DBG("Removing directory %s", config.consumerd32_path.value);
415 (void) rmdir(config.consumerd32_path.value);
416
417 /* ust consumerd 64 */
418 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
419 (void) unlink(config.consumerd64_err_unix_sock_path.value);
420
421 DBG("Removing directory %s", config.consumerd64_path.value);
422 (void) rmdir(config.consumerd64_path.value);
423
424 pthread_mutex_destroy(&session_list->lock);
425
426 wait_consumer(&kconsumer_data);
427 wait_consumer(&ustconsumer64_data);
428 wait_consumer(&ustconsumer32_data);
429
430 DBG("Cleaning up all agent apps");
431 agent_app_ht_clean();
432
433 DBG("Closing all UST sockets");
434 ust_app_clean_list();
435 buffer_reg_destroy_registries();
436
437 if (is_root && !config.no_kernel) {
438 DBG2("Closing kernel fd");
439 if (kernel_tracer_fd >= 0) {
440 ret = close(kernel_tracer_fd);
441 if (ret) {
442 PERROR("close");
443 }
444 }
445 DBG("Unloading kernel modules");
446 modprobe_remove_lttng_all();
447 free(syscall_table);
448 }
449
450 close_consumer_sockets();
451
452 if (load_info) {
453 load_session_destroy_data(load_info);
454 free(load_info);
455 }
456
457 /*
458 * We do NOT rmdir rundir because there are other processes
459 * using it, for instance lttng-relayd, which can start in
460 * parallel with this teardown.
461 */
462 }
463
464 /*
465 * Cleanup the daemon's option data structures.
466 */
467 static void sessiond_cleanup_options(void)
468 {
469 DBG("Cleaning up options");
470
471 sessiond_config_fini(&config);
472
473 run_as_destroy_worker();
474 }
475
476 /*
477 * Send data on a unix socket using the liblttsessiondcomm API.
478 *
479 * Return lttcomm error code.
480 */
481 static int send_unix_sock(int sock, void *buf, size_t len)
482 {
483 /* Check valid length */
484 if (len == 0) {
485 return -1;
486 }
487
488 return lttcomm_send_unix_sock(sock, buf, len);
489 }
490
491 /*
492 * Free memory of a command context structure.
493 */
494 static void clean_command_ctx(struct command_ctx **cmd_ctx)
495 {
496 DBG("Clean command context structure");
497 if (*cmd_ctx) {
498 if ((*cmd_ctx)->llm) {
499 free((*cmd_ctx)->llm);
500 }
501 if ((*cmd_ctx)->lsm) {
502 free((*cmd_ctx)->lsm);
503 }
504 free(*cmd_ctx);
505 *cmd_ctx = NULL;
506 }
507 }
508
509 /*
510 * Notify UST applications using the shm mmap futex.
511 */
512 static int notify_ust_apps(int active)
513 {
514 char *wait_shm_mmap;
515
516 DBG("Notifying applications of session daemon state: %d", active);
517
518 /* See shm.c for this call implying mmap, shm and futex calls */
519 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
520 if (wait_shm_mmap == NULL) {
521 goto error;
522 }
523
524 /* Wake waiting process */
525 futex_wait_update((int32_t *) wait_shm_mmap, active);
526
527 /* Apps notified successfully */
528 return 0;
529
530 error:
531 return -1;
532 }
533
534 /*
535 * Setup the outgoing data buffer for the response (llm) by allocating the
536 * right amount of memory and copying the original information from the lsm
537 * structure.
538 *
539 * Return 0 on success, negative value on error.
540 */
541 static int setup_lttng_msg(struct command_ctx *cmd_ctx,
542 const void *payload_buf, size_t payload_len,
543 const void *cmd_header_buf, size_t cmd_header_len)
544 {
545 int ret = 0;
546 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
547 const size_t cmd_header_offset = header_len;
548 const size_t payload_offset = cmd_header_offset + cmd_header_len;
549 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
550
551 cmd_ctx->llm = zmalloc(total_msg_size);
552
553 if (cmd_ctx->llm == NULL) {
554 PERROR("zmalloc");
555 ret = -ENOMEM;
556 goto end;
557 }
558
559 /* Copy common data */
560 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
561 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
562 cmd_ctx->llm->cmd_header_size = cmd_header_len;
563 cmd_ctx->llm->data_size = payload_len;
564 cmd_ctx->lttng_msg_size = total_msg_size;
565
566 /* Copy command header */
567 if (cmd_header_len) {
568 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
569 cmd_header_len);
570 }
571
572 /* Copy payload */
573 if (payload_len) {
574 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
575 payload_len);
576 }
577
578 end:
579 return ret;
580 }
581
582 /*
583 * Version of setup_lttng_msg() without command header.
584 */
585 static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
586 void *payload_buf, size_t payload_len)
587 {
588 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
589 }
590 /*
591 * Update the kernel poll set of all channel fd available over all tracing
592 * session. Add the wakeup pipe at the end of the set.
593 */
594 static int update_kernel_poll(struct lttng_poll_event *events)
595 {
596 int ret;
597 struct ltt_kernel_channel *channel;
598 struct ltt_session *session;
599 const struct ltt_session_list *session_list = session_get_list();
600
601 DBG("Updating kernel poll set");
602
603 session_lock_list();
604 cds_list_for_each_entry(session, &session_list->head, list) {
605 if (!session_get(session)) {
606 continue;
607 }
608 session_lock(session);
609 if (session->kernel_session == NULL) {
610 session_unlock(session);
611 session_put(session);
612 continue;
613 }
614
615 cds_list_for_each_entry(channel,
616 &session->kernel_session->channel_list.head, list) {
617 /* Add channel fd to the kernel poll set */
618 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
619 if (ret < 0) {
620 session_unlock(session);
621 session_put(session);
622 goto error;
623 }
624 DBG("Channel fd %d added to kernel set", channel->fd);
625 }
626 session_unlock(session);
627 }
628 session_unlock_list();
629
630 return 0;
631
632 error:
633 session_unlock_list();
634 return -1;
635 }
636
637 /*
638 * Find the channel fd from 'fd' over all tracing session. When found, check
639 * for new channel stream and send those stream fds to the kernel consumer.
640 *
641 * Useful for CPU hotplug feature.
642 */
643 static int update_kernel_stream(int fd)
644 {
645 int ret = 0;
646 struct ltt_session *session;
647 struct ltt_kernel_session *ksess;
648 struct ltt_kernel_channel *channel;
649 const struct ltt_session_list *session_list = session_get_list();
650
651 DBG("Updating kernel streams for channel fd %d", fd);
652
653 session_lock_list();
654 cds_list_for_each_entry(session, &session_list->head, list) {
655 if (!session_get(session)) {
656 continue;
657 }
658 session_lock(session);
659 if (session->kernel_session == NULL) {
660 session_unlock(session);
661 session_put(session);
662 continue;
663 }
664 ksess = session->kernel_session;
665
666 cds_list_for_each_entry(channel,
667 &ksess->channel_list.head, list) {
668 struct lttng_ht_iter iter;
669 struct consumer_socket *socket;
670
671 if (channel->fd != fd) {
672 continue;
673 }
674 DBG("Channel found, updating kernel streams");
675 ret = kernel_open_channel_stream(channel);
676 if (ret < 0) {
677 goto error;
678 }
679 /* Update the stream global counter */
680 ksess->stream_count_global += ret;
681
682 /*
683 * Have we already sent fds to the consumer? If yes, it
684 * means that tracing is started so it is safe to send
685 * our updated stream fds.
686 */
687 if (ksess->consumer_fds_sent != 1
688 || ksess->consumer == NULL) {
689 ret = -1;
690 goto error;
691 }
692
693 rcu_read_lock();
694 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
695 &iter.iter, socket, node.node) {
696 pthread_mutex_lock(socket->lock);
697 ret = kernel_consumer_send_channel_streams(socket,
698 channel, ksess,
699 session->output_traces ? 1 : 0);
700 pthread_mutex_unlock(socket->lock);
701 if (ret < 0) {
702 rcu_read_unlock();
703 goto error;
704 }
705 }
706 rcu_read_unlock();
707 }
708 session_unlock(session);
709 session_put(session);
710 }
711 session_unlock_list();
712 return ret;
713
714 error:
715 session_unlock(session);
716 session_put(session);
717 session_unlock_list();
718 return ret;
719 }
720
721 /*
722 * For each tracing session, update newly registered apps. The session list
723 * lock MUST be acquired before calling this.
724 */
725 static void update_ust_app(int app_sock)
726 {
727 struct ltt_session *sess, *stmp;
728 const struct ltt_session_list *session_list = session_get_list();
729
730 /* Consumer is in an ERROR state. Stop any application update. */
731 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
732 /* Stop the update process since the consumer is dead. */
733 return;
734 }
735
736 /* For all tracing session(s) */
737 cds_list_for_each_entry_safe(sess, stmp, &session_list->head, list) {
738 struct ust_app *app;
739
740 if (!session_get(sess)) {
741 continue;
742 }
743 session_lock(sess);
744 if (!sess->ust_session) {
745 goto unlock_session;
746 }
747
748 rcu_read_lock();
749 assert(app_sock >= 0);
750 app = ust_app_find_by_sock(app_sock);
751 if (app == NULL) {
752 /*
753 * Application can be unregistered before so
754 * this is possible hence simply stopping the
755 * update.
756 */
757 DBG3("UST app update failed to find app sock %d",
758 app_sock);
759 goto unlock_rcu;
760 }
761 ust_app_global_update(sess->ust_session, app);
762 unlock_rcu:
763 rcu_read_unlock();
764 unlock_session:
765 session_unlock(sess);
766 session_put(sess);
767 }
768 }
769
770 /*
771 * This thread manage event coming from the kernel.
772 *
773 * Features supported in this thread:
774 * -) CPU Hotplug
775 */
776 static void *thread_manage_kernel(void *data)
777 {
778 int ret, i, pollfd, update_poll_flag = 1, err = -1;
779 uint32_t revents, nb_fd;
780 char tmp;
781 struct lttng_poll_event events;
782
783 DBG("[thread] Thread manage kernel started");
784
785 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
786
787 /*
788 * This first step of the while is to clean this structure which could free
789 * non NULL pointers so initialize it before the loop.
790 */
791 lttng_poll_init(&events);
792
793 if (testpoint(sessiond_thread_manage_kernel)) {
794 goto error_testpoint;
795 }
796
797 health_code_update();
798
799 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
800 goto error_testpoint;
801 }
802
803 while (1) {
804 health_code_update();
805
806 if (update_poll_flag == 1) {
807 /* Clean events object. We are about to populate it again. */
808 lttng_poll_clean(&events);
809
810 ret = sessiond_set_thread_pollset(&events, 2);
811 if (ret < 0) {
812 goto error_poll_create;
813 }
814
815 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
816 if (ret < 0) {
817 goto error;
818 }
819
820 /* This will add the available kernel channel if any. */
821 ret = update_kernel_poll(&events);
822 if (ret < 0) {
823 goto error;
824 }
825 update_poll_flag = 0;
826 }
827
828 DBG("Thread kernel polling");
829
830 /* Poll infinite value of time */
831 restart:
832 health_poll_entry();
833 ret = lttng_poll_wait(&events, -1);
834 DBG("Thread kernel return from poll on %d fds",
835 LTTNG_POLL_GETNB(&events));
836 health_poll_exit();
837 if (ret < 0) {
838 /*
839 * Restart interrupted system call.
840 */
841 if (errno == EINTR) {
842 goto restart;
843 }
844 goto error;
845 } else if (ret == 0) {
846 /* Should not happen since timeout is infinite */
847 ERR("Return value of poll is 0 with an infinite timeout.\n"
848 "This should not have happened! Continuing...");
849 continue;
850 }
851
852 nb_fd = ret;
853
854 for (i = 0; i < nb_fd; i++) {
855 /* Fetch once the poll data */
856 revents = LTTNG_POLL_GETEV(&events, i);
857 pollfd = LTTNG_POLL_GETFD(&events, i);
858
859 health_code_update();
860
861 if (!revents) {
862 /* No activity for this FD (poll implementation). */
863 continue;
864 }
865
866 /* Thread quit pipe has been closed. Killing thread. */
867 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
868 if (ret) {
869 err = 0;
870 goto exit;
871 }
872
873 /* Check for data on kernel pipe */
874 if (revents & LPOLLIN) {
875 if (pollfd == kernel_poll_pipe[0]) {
876 (void) lttng_read(kernel_poll_pipe[0],
877 &tmp, 1);
878 /*
879 * Ret value is useless here, if this pipe gets any actions an
880 * update is required anyway.
881 */
882 update_poll_flag = 1;
883 continue;
884 } else {
885 /*
886 * New CPU detected by the kernel. Adding kernel stream to
887 * kernel session and updating the kernel consumer
888 */
889 ret = update_kernel_stream(pollfd);
890 if (ret < 0) {
891 continue;
892 }
893 break;
894 }
895 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
896 update_poll_flag = 1;
897 continue;
898 } else {
899 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
900 goto error;
901 }
902 }
903 }
904
905 exit:
906 error:
907 lttng_poll_clean(&events);
908 error_poll_create:
909 error_testpoint:
910 utils_close_pipe(kernel_poll_pipe);
911 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
912 if (err) {
913 health_error();
914 ERR("Health error occurred in %s", __func__);
915 WARN("Kernel thread died unexpectedly. "
916 "Kernel tracing can continue but CPU hotplug is disabled.");
917 }
918 health_unregister(health_sessiond);
919 DBG("Kernel thread dying");
920 return NULL;
921 }
922
923 /*
924 * Signal pthread condition of the consumer data that the thread.
925 */
926 static void signal_consumer_condition(struct consumer_data *data, int state)
927 {
928 pthread_mutex_lock(&data->cond_mutex);
929
930 /*
931 * The state is set before signaling. It can be any value, it's the waiter
932 * job to correctly interpret this condition variable associated to the
933 * consumer pthread_cond.
934 *
935 * A value of 0 means that the corresponding thread of the consumer data
936 * was not started. 1 indicates that the thread has started and is ready
937 * for action. A negative value means that there was an error during the
938 * thread bootstrap.
939 */
940 data->consumer_thread_is_ready = state;
941 (void) pthread_cond_signal(&data->cond);
942
943 pthread_mutex_unlock(&data->cond_mutex);
944 }
945
946 /*
947 * This thread manage the consumer error sent back to the session daemon.
948 */
949 static void *thread_manage_consumer(void *data)
950 {
951 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
952 uint32_t revents, nb_fd;
953 enum lttcomm_return_code code;
954 struct lttng_poll_event events;
955 struct consumer_data *consumer_data = data;
956 struct consumer_socket *cmd_socket_wrapper = NULL;
957
958 DBG("[thread] Manage consumer started");
959
960 rcu_register_thread();
961 rcu_thread_online();
962
963 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
964
965 health_code_update();
966
967 /*
968 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
969 * metadata_sock. Nothing more will be added to this poll set.
970 */
971 ret = sessiond_set_thread_pollset(&events, 3);
972 if (ret < 0) {
973 goto error_poll;
974 }
975
976 /*
977 * The error socket here is already in a listening state which was done
978 * just before spawning this thread to avoid a race between the consumer
979 * daemon exec trying to connect and the listen() call.
980 */
981 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
982 if (ret < 0) {
983 goto error;
984 }
985
986 health_code_update();
987
988 /* Infinite blocking call, waiting for transmission */
989 restart:
990 health_poll_entry();
991
992 if (testpoint(sessiond_thread_manage_consumer)) {
993 goto error;
994 }
995
996 ret = lttng_poll_wait(&events, -1);
997 health_poll_exit();
998 if (ret < 0) {
999 /*
1000 * Restart interrupted system call.
1001 */
1002 if (errno == EINTR) {
1003 goto restart;
1004 }
1005 goto error;
1006 }
1007
1008 nb_fd = ret;
1009
1010 for (i = 0; i < nb_fd; i++) {
1011 /* Fetch once the poll data */
1012 revents = LTTNG_POLL_GETEV(&events, i);
1013 pollfd = LTTNG_POLL_GETFD(&events, i);
1014
1015 health_code_update();
1016
1017 if (!revents) {
1018 /* No activity for this FD (poll implementation). */
1019 continue;
1020 }
1021
1022 /* Thread quit pipe has been closed. Killing thread. */
1023 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1024 if (ret) {
1025 err = 0;
1026 goto exit;
1027 }
1028
1029 /* Event on the registration socket */
1030 if (pollfd == consumer_data->err_sock) {
1031 if (revents & LPOLLIN) {
1032 continue;
1033 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1034 ERR("consumer err socket poll error");
1035 goto error;
1036 } else {
1037 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1038 goto error;
1039 }
1040 }
1041 }
1042
1043 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1044 if (sock < 0) {
1045 goto error;
1046 }
1047
1048 /*
1049 * Set the CLOEXEC flag. Return code is useless because either way, the
1050 * show must go on.
1051 */
1052 (void) utils_set_fd_cloexec(sock);
1053
1054 health_code_update();
1055
1056 DBG2("Receiving code from consumer err_sock");
1057
1058 /* Getting status code from kconsumerd */
1059 ret = lttcomm_recv_unix_sock(sock, &code,
1060 sizeof(enum lttcomm_return_code));
1061 if (ret <= 0) {
1062 goto error;
1063 }
1064
1065 health_code_update();
1066 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1067 ERR("consumer error when waiting for SOCK_READY : %s",
1068 lttcomm_get_readable_code(-code));
1069 goto error;
1070 }
1071
1072 /* Connect both command and metadata sockets. */
1073 consumer_data->cmd_sock =
1074 lttcomm_connect_unix_sock(
1075 consumer_data->cmd_unix_sock_path);
1076 consumer_data->metadata_fd =
1077 lttcomm_connect_unix_sock(
1078 consumer_data->cmd_unix_sock_path);
1079 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
1080 PERROR("consumer connect cmd socket");
1081 /* On error, signal condition and quit. */
1082 signal_consumer_condition(consumer_data, -1);
1083 goto error;
1084 }
1085
1086 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1087
1088 /* Create metadata socket lock. */
1089 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1090 if (consumer_data->metadata_sock.lock == NULL) {
1091 PERROR("zmalloc pthread mutex");
1092 goto error;
1093 }
1094 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1095
1096 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1097 DBG("Consumer metadata socket ready (fd: %d)",
1098 consumer_data->metadata_fd);
1099
1100 /*
1101 * Remove the consumerd error sock since we've established a connection.
1102 */
1103 ret = lttng_poll_del(&events, consumer_data->err_sock);
1104 if (ret < 0) {
1105 goto error;
1106 }
1107
1108 /* Add new accepted error socket. */
1109 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1110 if (ret < 0) {
1111 goto error;
1112 }
1113
1114 /* Add metadata socket that is successfully connected. */
1115 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1116 LPOLLIN | LPOLLRDHUP);
1117 if (ret < 0) {
1118 goto error;
1119 }
1120
1121 health_code_update();
1122
1123 /*
1124 * Transfer the write-end of the channel monitoring and rotate pipe
1125 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1126 */
1127 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
1128 if (!cmd_socket_wrapper) {
1129 goto error;
1130 }
1131 cmd_socket_wrapper->lock = &consumer_data->lock;
1132
1133 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
1134 consumer_data->channel_monitor_pipe);
1135 if (ret) {
1136 goto error;
1137 }
1138
1139 /* Discard the socket wrapper as it is no longer needed. */
1140 consumer_destroy_socket(cmd_socket_wrapper);
1141 cmd_socket_wrapper = NULL;
1142
1143 /* The thread is completely initialized, signal that it is ready. */
1144 signal_consumer_condition(consumer_data, 1);
1145
1146 /* Infinite blocking call, waiting for transmission */
1147 restart_poll:
1148 while (1) {
1149 health_code_update();
1150
1151 /* Exit the thread because the thread quit pipe has been triggered. */
1152 if (should_quit) {
1153 /* Not a health error. */
1154 err = 0;
1155 goto exit;
1156 }
1157
1158 health_poll_entry();
1159 ret = lttng_poll_wait(&events, -1);
1160 health_poll_exit();
1161 if (ret < 0) {
1162 /*
1163 * Restart interrupted system call.
1164 */
1165 if (errno == EINTR) {
1166 goto restart_poll;
1167 }
1168 goto error;
1169 }
1170
1171 nb_fd = ret;
1172
1173 for (i = 0; i < nb_fd; i++) {
1174 /* Fetch once the poll data */
1175 revents = LTTNG_POLL_GETEV(&events, i);
1176 pollfd = LTTNG_POLL_GETFD(&events, i);
1177
1178 health_code_update();
1179
1180 if (!revents) {
1181 /* No activity for this FD (poll implementation). */
1182 continue;
1183 }
1184
1185 /*
1186 * Thread quit pipe has been triggered, flag that we should stop
1187 * but continue the current loop to handle potential data from
1188 * consumer.
1189 */
1190 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
1191
1192 if (pollfd == sock) {
1193 /* Event on the consumerd socket */
1194 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1195 && !(revents & LPOLLIN)) {
1196 ERR("consumer err socket second poll error");
1197 goto error;
1198 }
1199 health_code_update();
1200 /* Wait for any kconsumerd error */
1201 ret = lttcomm_recv_unix_sock(sock, &code,
1202 sizeof(enum lttcomm_return_code));
1203 if (ret <= 0) {
1204 ERR("consumer closed the command socket");
1205 goto error;
1206 }
1207
1208 ERR("consumer return code : %s",
1209 lttcomm_get_readable_code(-code));
1210
1211 goto exit;
1212 } else if (pollfd == consumer_data->metadata_fd) {
1213 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1214 && !(revents & LPOLLIN)) {
1215 ERR("consumer err metadata socket second poll error");
1216 goto error;
1217 }
1218 /* UST metadata requests */
1219 ret = ust_consumer_metadata_request(
1220 &consumer_data->metadata_sock);
1221 if (ret < 0) {
1222 ERR("Handling metadata request");
1223 goto error;
1224 }
1225 }
1226 /* No need for an else branch all FDs are tested prior. */
1227 }
1228 health_code_update();
1229 }
1230
1231 exit:
1232 error:
1233 /*
1234 * We lock here because we are about to close the sockets and some other
1235 * thread might be using them so get exclusive access which will abort all
1236 * other consumer command by other threads.
1237 */
1238 pthread_mutex_lock(&consumer_data->lock);
1239
1240 /* Immediately set the consumerd state to stopped */
1241 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1242 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1243 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1244 consumer_data->type == LTTNG_CONSUMER32_UST) {
1245 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1246 } else {
1247 /* Code flow error... */
1248 assert(0);
1249 }
1250
1251 if (consumer_data->err_sock >= 0) {
1252 ret = close(consumer_data->err_sock);
1253 if (ret) {
1254 PERROR("close");
1255 }
1256 consumer_data->err_sock = -1;
1257 }
1258 if (consumer_data->cmd_sock >= 0) {
1259 ret = close(consumer_data->cmd_sock);
1260 if (ret) {
1261 PERROR("close");
1262 }
1263 consumer_data->cmd_sock = -1;
1264 }
1265 if (consumer_data->metadata_sock.fd_ptr &&
1266 *consumer_data->metadata_sock.fd_ptr >= 0) {
1267 ret = close(*consumer_data->metadata_sock.fd_ptr);
1268 if (ret) {
1269 PERROR("close");
1270 }
1271 }
1272 if (sock >= 0) {
1273 ret = close(sock);
1274 if (ret) {
1275 PERROR("close");
1276 }
1277 }
1278
1279 unlink(consumer_data->err_unix_sock_path);
1280 unlink(consumer_data->cmd_unix_sock_path);
1281 pthread_mutex_unlock(&consumer_data->lock);
1282
1283 /* Cleanup metadata socket mutex. */
1284 if (consumer_data->metadata_sock.lock) {
1285 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1286 free(consumer_data->metadata_sock.lock);
1287 }
1288 lttng_poll_clean(&events);
1289
1290 if (cmd_socket_wrapper) {
1291 consumer_destroy_socket(cmd_socket_wrapper);
1292 }
1293 error_poll:
1294 if (err) {
1295 health_error();
1296 ERR("Health error occurred in %s", __func__);
1297 }
1298 health_unregister(health_sessiond);
1299 DBG("consumer thread cleanup completed");
1300
1301 rcu_thread_offline();
1302 rcu_unregister_thread();
1303
1304 return NULL;
1305 }
1306
1307 /*
1308 * This thread receives application command sockets (FDs) on the
1309 * apps_cmd_pipe and waits (polls) on them until they are closed
1310 * or an error occurs.
1311 *
1312 * At that point, it flushes the data (tracing and metadata) associated
1313 * with this application and tears down ust app sessions and other
1314 * associated data structures through ust_app_unregister().
1315 *
1316 * Note that this thread never sends commands to the applications
1317 * through the command sockets; it merely listens for hang-ups
1318 * and errors on those sockets and cleans-up as they occur.
1319 */
1320 static void *thread_manage_apps(void *data)
1321 {
1322 int i, ret, pollfd, err = -1;
1323 ssize_t size_ret;
1324 uint32_t revents, nb_fd;
1325 struct lttng_poll_event events;
1326
1327 DBG("[thread] Manage application started");
1328
1329 rcu_register_thread();
1330 rcu_thread_online();
1331
1332 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1333
1334 if (testpoint(sessiond_thread_manage_apps)) {
1335 goto error_testpoint;
1336 }
1337
1338 health_code_update();
1339
1340 ret = sessiond_set_thread_pollset(&events, 2);
1341 if (ret < 0) {
1342 goto error_poll_create;
1343 }
1344
1345 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1346 if (ret < 0) {
1347 goto error;
1348 }
1349
1350 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1351 goto error;
1352 }
1353
1354 health_code_update();
1355
1356 while (1) {
1357 DBG("Apps thread polling");
1358
1359 /* Inifinite blocking call, waiting for transmission */
1360 restart:
1361 health_poll_entry();
1362 ret = lttng_poll_wait(&events, -1);
1363 DBG("Apps thread return from poll on %d fds",
1364 LTTNG_POLL_GETNB(&events));
1365 health_poll_exit();
1366 if (ret < 0) {
1367 /*
1368 * Restart interrupted system call.
1369 */
1370 if (errno == EINTR) {
1371 goto restart;
1372 }
1373 goto error;
1374 }
1375
1376 nb_fd = ret;
1377
1378 for (i = 0; i < nb_fd; i++) {
1379 /* Fetch once the poll data */
1380 revents = LTTNG_POLL_GETEV(&events, i);
1381 pollfd = LTTNG_POLL_GETFD(&events, i);
1382
1383 health_code_update();
1384
1385 if (!revents) {
1386 /* No activity for this FD (poll implementation). */
1387 continue;
1388 }
1389
1390 /* Thread quit pipe has been closed. Killing thread. */
1391 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1392 if (ret) {
1393 err = 0;
1394 goto exit;
1395 }
1396
1397 /* Inspect the apps cmd pipe */
1398 if (pollfd == apps_cmd_pipe[0]) {
1399 if (revents & LPOLLIN) {
1400 int sock;
1401
1402 /* Empty pipe */
1403 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1404 if (size_ret < sizeof(sock)) {
1405 PERROR("read apps cmd pipe");
1406 goto error;
1407 }
1408
1409 health_code_update();
1410
1411 /*
1412 * Since this is a command socket (write then read),
1413 * we only monitor the error events of the socket.
1414 */
1415 ret = lttng_poll_add(&events, sock,
1416 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1417 if (ret < 0) {
1418 goto error;
1419 }
1420
1421 DBG("Apps with sock %d added to poll set", sock);
1422 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1423 ERR("Apps command pipe error");
1424 goto error;
1425 } else {
1426 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1427 goto error;
1428 }
1429 } else {
1430 /*
1431 * At this point, we know that a registered application made
1432 * the event at poll_wait.
1433 */
1434 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1435 /* Removing from the poll set */
1436 ret = lttng_poll_del(&events, pollfd);
1437 if (ret < 0) {
1438 goto error;
1439 }
1440
1441 /* Socket closed on remote end. */
1442 ust_app_unregister(pollfd);
1443 } else {
1444 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1445 goto error;
1446 }
1447 }
1448
1449 health_code_update();
1450 }
1451 }
1452
1453 exit:
1454 error:
1455 lttng_poll_clean(&events);
1456 error_poll_create:
1457 error_testpoint:
1458 utils_close_pipe(apps_cmd_pipe);
1459 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1460
1461 /*
1462 * We don't clean the UST app hash table here since already registered
1463 * applications can still be controlled so let them be until the session
1464 * daemon dies or the applications stop.
1465 */
1466
1467 if (err) {
1468 health_error();
1469 ERR("Health error occurred in %s", __func__);
1470 }
1471 health_unregister(health_sessiond);
1472 DBG("Application communication apps thread cleanup complete");
1473 rcu_thread_offline();
1474 rcu_unregister_thread();
1475 return NULL;
1476 }
1477
1478 /*
1479 * Send a socket to a thread This is called from the dispatch UST registration
1480 * thread once all sockets are set for the application.
1481 *
1482 * The sock value can be invalid, we don't really care, the thread will handle
1483 * it and make the necessary cleanup if so.
1484 *
1485 * On success, return 0 else a negative value being the errno message of the
1486 * write().
1487 */
1488 static int send_socket_to_thread(int fd, int sock)
1489 {
1490 ssize_t ret;
1491
1492 /*
1493 * It's possible that the FD is set as invalid with -1 concurrently just
1494 * before calling this function being a shutdown state of the thread.
1495 */
1496 if (fd < 0) {
1497 ret = -EBADF;
1498 goto error;
1499 }
1500
1501 ret = lttng_write(fd, &sock, sizeof(sock));
1502 if (ret < sizeof(sock)) {
1503 PERROR("write apps pipe %d", fd);
1504 if (ret < 0) {
1505 ret = -errno;
1506 }
1507 goto error;
1508 }
1509
1510 /* All good. Don't send back the write positive ret value. */
1511 ret = 0;
1512 error:
1513 return (int) ret;
1514 }
1515
1516 /*
1517 * Sanitize the wait queue of the dispatch registration thread meaning removing
1518 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1519 * notify socket is never received.
1520 */
1521 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1522 {
1523 int ret, nb_fd = 0, i;
1524 unsigned int fd_added = 0;
1525 struct lttng_poll_event events;
1526 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1527
1528 assert(wait_queue);
1529
1530 lttng_poll_init(&events);
1531
1532 /* Just skip everything for an empty queue. */
1533 if (!wait_queue->count) {
1534 goto end;
1535 }
1536
1537 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1538 if (ret < 0) {
1539 goto error_create;
1540 }
1541
1542 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1543 &wait_queue->head, head) {
1544 assert(wait_node->app);
1545 ret = lttng_poll_add(&events, wait_node->app->sock,
1546 LPOLLHUP | LPOLLERR);
1547 if (ret < 0) {
1548 goto error;
1549 }
1550
1551 fd_added = 1;
1552 }
1553
1554 if (!fd_added) {
1555 goto end;
1556 }
1557
1558 /*
1559 * Poll but don't block so we can quickly identify the faulty events and
1560 * clean them afterwards from the wait queue.
1561 */
1562 ret = lttng_poll_wait(&events, 0);
1563 if (ret < 0) {
1564 goto error;
1565 }
1566 nb_fd = ret;
1567
1568 for (i = 0; i < nb_fd; i++) {
1569 /* Get faulty FD. */
1570 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1571 int pollfd = LTTNG_POLL_GETFD(&events, i);
1572
1573 if (!revents) {
1574 /* No activity for this FD (poll implementation). */
1575 continue;
1576 }
1577
1578 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1579 &wait_queue->head, head) {
1580 if (pollfd == wait_node->app->sock &&
1581 (revents & (LPOLLHUP | LPOLLERR))) {
1582 cds_list_del(&wait_node->head);
1583 wait_queue->count--;
1584 ust_app_destroy(wait_node->app);
1585 free(wait_node);
1586 /*
1587 * Silence warning of use-after-free in
1588 * cds_list_for_each_entry_safe which uses
1589 * __typeof__(*wait_node).
1590 */
1591 wait_node = NULL;
1592 break;
1593 } else {
1594 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1595 goto error;
1596 }
1597 }
1598 }
1599
1600 if (nb_fd > 0) {
1601 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1602 }
1603
1604 end:
1605 lttng_poll_clean(&events);
1606 return;
1607
1608 error:
1609 lttng_poll_clean(&events);
1610 error_create:
1611 ERR("Unable to sanitize wait queue");
1612 return;
1613 }
1614
1615 /*
1616 * Dispatch request from the registration threads to the application
1617 * communication thread.
1618 */
1619 static void *thread_dispatch_ust_registration(void *data)
1620 {
1621 int ret, err = -1;
1622 struct cds_wfcq_node *node;
1623 struct ust_command *ust_cmd = NULL;
1624 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1625 struct ust_reg_wait_queue wait_queue = {
1626 .count = 0,
1627 };
1628
1629 rcu_register_thread();
1630
1631 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1632
1633 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1634 goto error_testpoint;
1635 }
1636
1637 health_code_update();
1638
1639 CDS_INIT_LIST_HEAD(&wait_queue.head);
1640
1641 DBG("[thread] Dispatch UST command started");
1642
1643 for (;;) {
1644 health_code_update();
1645
1646 /* Atomically prepare the queue futex */
1647 futex_nto1_prepare(&ust_cmd_queue.futex);
1648
1649 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1650 break;
1651 }
1652
1653 do {
1654 struct ust_app *app = NULL;
1655 ust_cmd = NULL;
1656
1657 /*
1658 * Make sure we don't have node(s) that have hung up before receiving
1659 * the notify socket. This is to clean the list in order to avoid
1660 * memory leaks from notify socket that are never seen.
1661 */
1662 sanitize_wait_queue(&wait_queue);
1663
1664 health_code_update();
1665 /* Dequeue command for registration */
1666 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1667 if (node == NULL) {
1668 DBG("Woken up but nothing in the UST command queue");
1669 /* Continue thread execution */
1670 break;
1671 }
1672
1673 ust_cmd = caa_container_of(node, struct ust_command, node);
1674
1675 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1676 " gid:%d sock:%d name:%s (version %d.%d)",
1677 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1678 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1679 ust_cmd->sock, ust_cmd->reg_msg.name,
1680 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1681
1682 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1683 wait_node = zmalloc(sizeof(*wait_node));
1684 if (!wait_node) {
1685 PERROR("zmalloc wait_node dispatch");
1686 ret = close(ust_cmd->sock);
1687 if (ret < 0) {
1688 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1689 }
1690 lttng_fd_put(LTTNG_FD_APPS, 1);
1691 free(ust_cmd);
1692 goto error;
1693 }
1694 CDS_INIT_LIST_HEAD(&wait_node->head);
1695
1696 /* Create application object if socket is CMD. */
1697 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1698 ust_cmd->sock);
1699 if (!wait_node->app) {
1700 ret = close(ust_cmd->sock);
1701 if (ret < 0) {
1702 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1703 }
1704 lttng_fd_put(LTTNG_FD_APPS, 1);
1705 free(wait_node);
1706 free(ust_cmd);
1707 continue;
1708 }
1709 /*
1710 * Add application to the wait queue so we can set the notify
1711 * socket before putting this object in the global ht.
1712 */
1713 cds_list_add(&wait_node->head, &wait_queue.head);
1714 wait_queue.count++;
1715
1716 free(ust_cmd);
1717 /*
1718 * We have to continue here since we don't have the notify
1719 * socket and the application MUST be added to the hash table
1720 * only at that moment.
1721 */
1722 continue;
1723 } else {
1724 /*
1725 * Look for the application in the local wait queue and set the
1726 * notify socket if found.
1727 */
1728 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1729 &wait_queue.head, head) {
1730 health_code_update();
1731 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1732 wait_node->app->notify_sock = ust_cmd->sock;
1733 cds_list_del(&wait_node->head);
1734 wait_queue.count--;
1735 app = wait_node->app;
1736 free(wait_node);
1737 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1738 break;
1739 }
1740 }
1741
1742 /*
1743 * With no application at this stage the received socket is
1744 * basically useless so close it before we free the cmd data
1745 * structure for good.
1746 */
1747 if (!app) {
1748 ret = close(ust_cmd->sock);
1749 if (ret < 0) {
1750 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1751 }
1752 lttng_fd_put(LTTNG_FD_APPS, 1);
1753 }
1754 free(ust_cmd);
1755 }
1756
1757 if (app) {
1758 /*
1759 * @session_lock_list
1760 *
1761 * Lock the global session list so from the register up to the
1762 * registration done message, no thread can see the application
1763 * and change its state.
1764 */
1765 session_lock_list();
1766 rcu_read_lock();
1767
1768 /*
1769 * Add application to the global hash table. This needs to be
1770 * done before the update to the UST registry can locate the
1771 * application.
1772 */
1773 ust_app_add(app);
1774
1775 /* Set app version. This call will print an error if needed. */
1776 (void) ust_app_version(app);
1777
1778 /* Send notify socket through the notify pipe. */
1779 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1780 app->notify_sock);
1781 if (ret < 0) {
1782 rcu_read_unlock();
1783 session_unlock_list();
1784 /*
1785 * No notify thread, stop the UST tracing. However, this is
1786 * not an internal error of the this thread thus setting
1787 * the health error code to a normal exit.
1788 */
1789 err = 0;
1790 goto error;
1791 }
1792
1793 /*
1794 * Update newly registered application with the tracing
1795 * registry info already enabled information.
1796 */
1797 update_ust_app(app->sock);
1798
1799 /*
1800 * Don't care about return value. Let the manage apps threads
1801 * handle app unregistration upon socket close.
1802 */
1803 (void) ust_app_register_done(app);
1804
1805 /*
1806 * Even if the application socket has been closed, send the app
1807 * to the thread and unregistration will take place at that
1808 * place.
1809 */
1810 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1811 if (ret < 0) {
1812 rcu_read_unlock();
1813 session_unlock_list();
1814 /*
1815 * No apps. thread, stop the UST tracing. However, this is
1816 * not an internal error of the this thread thus setting
1817 * the health error code to a normal exit.
1818 */
1819 err = 0;
1820 goto error;
1821 }
1822
1823 rcu_read_unlock();
1824 session_unlock_list();
1825 }
1826 } while (node != NULL);
1827
1828 health_poll_entry();
1829 /* Futex wait on queue. Blocking call on futex() */
1830 futex_nto1_wait(&ust_cmd_queue.futex);
1831 health_poll_exit();
1832 }
1833 /* Normal exit, no error */
1834 err = 0;
1835
1836 error:
1837 /* Clean up wait queue. */
1838 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1839 &wait_queue.head, head) {
1840 cds_list_del(&wait_node->head);
1841 wait_queue.count--;
1842 free(wait_node);
1843 }
1844
1845 /* Empty command queue. */
1846 for (;;) {
1847 /* Dequeue command for registration */
1848 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1849 if (node == NULL) {
1850 break;
1851 }
1852 ust_cmd = caa_container_of(node, struct ust_command, node);
1853 ret = close(ust_cmd->sock);
1854 if (ret < 0) {
1855 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
1856 }
1857 lttng_fd_put(LTTNG_FD_APPS, 1);
1858 free(ust_cmd);
1859 }
1860
1861 error_testpoint:
1862 DBG("Dispatch thread dying");
1863 if (err) {
1864 health_error();
1865 ERR("Health error occurred in %s", __func__);
1866 }
1867 health_unregister(health_sessiond);
1868 rcu_unregister_thread();
1869 return NULL;
1870 }
1871
1872 /*
1873 * This thread manage application registration.
1874 */
1875 static void *thread_registration_apps(void *data)
1876 {
1877 int sock = -1, i, ret, pollfd, err = -1;
1878 uint32_t revents, nb_fd;
1879 struct lttng_poll_event events;
1880 /*
1881 * Get allocated in this thread, enqueued to a global queue, dequeued and
1882 * freed in the manage apps thread.
1883 */
1884 struct ust_command *ust_cmd = NULL;
1885
1886 DBG("[thread] Manage application registration started");
1887
1888 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
1889
1890 if (testpoint(sessiond_thread_registration_apps)) {
1891 goto error_testpoint;
1892 }
1893
1894 ret = lttcomm_listen_unix_sock(apps_sock);
1895 if (ret < 0) {
1896 goto error_listen;
1897 }
1898
1899 /*
1900 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1901 * more will be added to this poll set.
1902 */
1903 ret = sessiond_set_thread_pollset(&events, 2);
1904 if (ret < 0) {
1905 goto error_create_poll;
1906 }
1907
1908 /* Add the application registration socket */
1909 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1910 if (ret < 0) {
1911 goto error_poll_add;
1912 }
1913
1914 /* Notify all applications to register */
1915 ret = notify_ust_apps(1);
1916 if (ret < 0) {
1917 ERR("Failed to notify applications or create the wait shared memory.\n"
1918 "Execution continues but there might be problem for already\n"
1919 "running applications that wishes to register.");
1920 }
1921
1922 while (1) {
1923 DBG("Accepting application registration");
1924
1925 /* Inifinite blocking call, waiting for transmission */
1926 restart:
1927 health_poll_entry();
1928 ret = lttng_poll_wait(&events, -1);
1929 health_poll_exit();
1930 if (ret < 0) {
1931 /*
1932 * Restart interrupted system call.
1933 */
1934 if (errno == EINTR) {
1935 goto restart;
1936 }
1937 goto error;
1938 }
1939
1940 nb_fd = ret;
1941
1942 for (i = 0; i < nb_fd; i++) {
1943 health_code_update();
1944
1945 /* Fetch once the poll data */
1946 revents = LTTNG_POLL_GETEV(&events, i);
1947 pollfd = LTTNG_POLL_GETFD(&events, i);
1948
1949 if (!revents) {
1950 /* No activity for this FD (poll implementation). */
1951 continue;
1952 }
1953
1954 /* Thread quit pipe has been closed. Killing thread. */
1955 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1956 if (ret) {
1957 err = 0;
1958 goto exit;
1959 }
1960
1961 /* Event on the registration socket */
1962 if (pollfd == apps_sock) {
1963 if (revents & LPOLLIN) {
1964 sock = lttcomm_accept_unix_sock(apps_sock);
1965 if (sock < 0) {
1966 goto error;
1967 }
1968
1969 /*
1970 * Set socket timeout for both receiving and ending.
1971 * app_socket_timeout is in seconds, whereas
1972 * lttcomm_setsockopt_rcv_timeout and
1973 * lttcomm_setsockopt_snd_timeout expect msec as
1974 * parameter.
1975 */
1976 if (config.app_socket_timeout >= 0) {
1977 (void) lttcomm_setsockopt_rcv_timeout(sock,
1978 config.app_socket_timeout * 1000);
1979 (void) lttcomm_setsockopt_snd_timeout(sock,
1980 config.app_socket_timeout * 1000);
1981 }
1982
1983 /*
1984 * Set the CLOEXEC flag. Return code is useless because
1985 * either way, the show must go on.
1986 */
1987 (void) utils_set_fd_cloexec(sock);
1988
1989 /* Create UST registration command for enqueuing */
1990 ust_cmd = zmalloc(sizeof(struct ust_command));
1991 if (ust_cmd == NULL) {
1992 PERROR("ust command zmalloc");
1993 ret = close(sock);
1994 if (ret) {
1995 PERROR("close");
1996 }
1997 goto error;
1998 }
1999
2000 /*
2001 * Using message-based transmissions to ensure we don't
2002 * have to deal with partially received messages.
2003 */
2004 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2005 if (ret < 0) {
2006 ERR("Exhausted file descriptors allowed for applications.");
2007 free(ust_cmd);
2008 ret = close(sock);
2009 if (ret) {
2010 PERROR("close");
2011 }
2012 sock = -1;
2013 continue;
2014 }
2015
2016 health_code_update();
2017 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2018 if (ret < 0) {
2019 free(ust_cmd);
2020 /* Close socket of the application. */
2021 ret = close(sock);
2022 if (ret) {
2023 PERROR("close");
2024 }
2025 lttng_fd_put(LTTNG_FD_APPS, 1);
2026 sock = -1;
2027 continue;
2028 }
2029 health_code_update();
2030
2031 ust_cmd->sock = sock;
2032 sock = -1;
2033
2034 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2035 " gid:%d sock:%d name:%s (version %d.%d)",
2036 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2037 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2038 ust_cmd->sock, ust_cmd->reg_msg.name,
2039 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2040
2041 /*
2042 * Lock free enqueue the registration request. The red pill
2043 * has been taken! This apps will be part of the *system*.
2044 */
2045 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
2046
2047 /*
2048 * Wake the registration queue futex. Implicit memory
2049 * barrier with the exchange in cds_wfcq_enqueue.
2050 */
2051 futex_nto1_wake(&ust_cmd_queue.futex);
2052 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2053 ERR("Register apps socket poll error");
2054 goto error;
2055 } else {
2056 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2057 goto error;
2058 }
2059 }
2060 }
2061 }
2062
2063 exit:
2064 error:
2065 /* Notify that the registration thread is gone */
2066 notify_ust_apps(0);
2067
2068 if (apps_sock >= 0) {
2069 ret = close(apps_sock);
2070 if (ret) {
2071 PERROR("close");
2072 }
2073 }
2074 if (sock >= 0) {
2075 ret = close(sock);
2076 if (ret) {
2077 PERROR("close");
2078 }
2079 lttng_fd_put(LTTNG_FD_APPS, 1);
2080 }
2081 unlink(config.apps_unix_sock_path.value);
2082
2083 error_poll_add:
2084 lttng_poll_clean(&events);
2085 error_listen:
2086 error_create_poll:
2087 error_testpoint:
2088 DBG("UST Registration thread cleanup complete");
2089 if (err) {
2090 health_error();
2091 ERR("Health error occurred in %s", __func__);
2092 }
2093 health_unregister(health_sessiond);
2094
2095 return NULL;
2096 }
2097
2098 /*
2099 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2100 * exec or it will fails.
2101 */
2102 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2103 {
2104 int ret, clock_ret;
2105 struct timespec timeout;
2106
2107 /*
2108 * Make sure we set the readiness flag to 0 because we are NOT ready.
2109 * This access to consumer_thread_is_ready does not need to be
2110 * protected by consumer_data.cond_mutex (yet) since the consumer
2111 * management thread has not been started at this point.
2112 */
2113 consumer_data->consumer_thread_is_ready = 0;
2114
2115 /* Setup pthread condition */
2116 ret = pthread_condattr_init(&consumer_data->condattr);
2117 if (ret) {
2118 errno = ret;
2119 PERROR("pthread_condattr_init consumer data");
2120 goto error;
2121 }
2122
2123 /*
2124 * Set the monotonic clock in order to make sure we DO NOT jump in time
2125 * between the clock_gettime() call and the timedwait call. See bug #324
2126 * for a more details and how we noticed it.
2127 */
2128 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2129 if (ret) {
2130 errno = ret;
2131 PERROR("pthread_condattr_setclock consumer data");
2132 goto error;
2133 }
2134
2135 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2136 if (ret) {
2137 errno = ret;
2138 PERROR("pthread_cond_init consumer data");
2139 goto error;
2140 }
2141
2142 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2143 thread_manage_consumer, consumer_data);
2144 if (ret) {
2145 errno = ret;
2146 PERROR("pthread_create consumer");
2147 ret = -1;
2148 goto error;
2149 }
2150
2151 /* We are about to wait on a pthread condition */
2152 pthread_mutex_lock(&consumer_data->cond_mutex);
2153
2154 /* Get time for sem_timedwait absolute timeout */
2155 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
2156 /*
2157 * Set the timeout for the condition timed wait even if the clock gettime
2158 * call fails since we might loop on that call and we want to avoid to
2159 * increment the timeout too many times.
2160 */
2161 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2162
2163 /*
2164 * The following loop COULD be skipped in some conditions so this is why we
2165 * set ret to 0 in order to make sure at least one round of the loop is
2166 * done.
2167 */
2168 ret = 0;
2169
2170 /*
2171 * Loop until the condition is reached or when a timeout is reached. Note
2172 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2173 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2174 * possible. This loop does not take any chances and works with both of
2175 * them.
2176 */
2177 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2178 if (clock_ret < 0) {
2179 PERROR("clock_gettime spawn consumer");
2180 /* Infinite wait for the consumerd thread to be ready */
2181 ret = pthread_cond_wait(&consumer_data->cond,
2182 &consumer_data->cond_mutex);
2183 } else {
2184 ret = pthread_cond_timedwait(&consumer_data->cond,
2185 &consumer_data->cond_mutex, &timeout);
2186 }
2187 }
2188
2189 /* Release the pthread condition */
2190 pthread_mutex_unlock(&consumer_data->cond_mutex);
2191
2192 if (ret != 0) {
2193 errno = ret;
2194 if (ret == ETIMEDOUT) {
2195 int pth_ret;
2196
2197 /*
2198 * Call has timed out so we kill the kconsumerd_thread and return
2199 * an error.
2200 */
2201 ERR("Condition timed out. The consumer thread was never ready."
2202 " Killing it");
2203 pth_ret = pthread_cancel(consumer_data->thread);
2204 if (pth_ret < 0) {
2205 PERROR("pthread_cancel consumer thread");
2206 }
2207 } else {
2208 PERROR("pthread_cond_wait failed consumer thread");
2209 }
2210 /* Caller is expecting a negative value on failure. */
2211 ret = -1;
2212 goto error;
2213 }
2214
2215 pthread_mutex_lock(&consumer_data->pid_mutex);
2216 if (consumer_data->pid == 0) {
2217 ERR("Consumerd did not start");
2218 pthread_mutex_unlock(&consumer_data->pid_mutex);
2219 goto error;
2220 }
2221 pthread_mutex_unlock(&consumer_data->pid_mutex);
2222
2223 return 0;
2224
2225 error:
2226 return ret;
2227 }
2228
2229 /*
2230 * Join consumer thread
2231 */
2232 static int join_consumer_thread(struct consumer_data *consumer_data)
2233 {
2234 void *status;
2235
2236 /* Consumer pid must be a real one. */
2237 if (consumer_data->pid > 0) {
2238 int ret;
2239 ret = kill(consumer_data->pid, SIGTERM);
2240 if (ret) {
2241 PERROR("Error killing consumer daemon");
2242 return ret;
2243 }
2244 return pthread_join(consumer_data->thread, &status);
2245 } else {
2246 return 0;
2247 }
2248 }
2249
2250 /*
2251 * Fork and exec a consumer daemon (consumerd).
2252 *
2253 * Return pid if successful else -1.
2254 */
2255 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2256 {
2257 int ret;
2258 pid_t pid;
2259 const char *consumer_to_use;
2260 const char *verbosity;
2261 struct stat st;
2262
2263 DBG("Spawning consumerd");
2264
2265 pid = fork();
2266 if (pid == 0) {
2267 /*
2268 * Exec consumerd.
2269 */
2270 if (config.verbose_consumer) {
2271 verbosity = "--verbose";
2272 } else if (lttng_opt_quiet) {
2273 verbosity = "--quiet";
2274 } else {
2275 verbosity = "";
2276 }
2277
2278 switch (consumer_data->type) {
2279 case LTTNG_CONSUMER_KERNEL:
2280 /*
2281 * Find out which consumerd to execute. We will first try the
2282 * 64-bit path, then the sessiond's installation directory, and
2283 * fallback on the 32-bit one,
2284 */
2285 DBG3("Looking for a kernel consumer at these locations:");
2286 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
2287 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
2288 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
2289 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
2290 DBG3("Found location #1");
2291 consumer_to_use = config.consumerd64_bin_path.value;
2292 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
2293 DBG3("Found location #2");
2294 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
2295 } else if (config.consumerd32_bin_path.value &&
2296 stat(config.consumerd32_bin_path.value, &st) == 0) {
2297 DBG3("Found location #3");
2298 consumer_to_use = config.consumerd32_bin_path.value;
2299 } else {
2300 DBG("Could not find any valid consumerd executable");
2301 ret = -EINVAL;
2302 goto error;
2303 }
2304 DBG("Using kernel consumer at: %s", consumer_to_use);
2305 (void) execl(consumer_to_use,
2306 "lttng-consumerd", verbosity, "-k",
2307 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2308 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2309 "--group", config.tracing_group_name.value,
2310 NULL);
2311 break;
2312 case LTTNG_CONSUMER64_UST:
2313 {
2314 if (config.consumerd64_lib_dir.value) {
2315 char *tmp;
2316 size_t tmplen;
2317 char *tmpnew;
2318
2319 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2320 if (!tmp) {
2321 tmp = "";
2322 }
2323 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
2324 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2325 if (!tmpnew) {
2326 ret = -ENOMEM;
2327 goto error;
2328 }
2329 strcat(tmpnew, config.consumerd64_lib_dir.value);
2330 if (tmp[0] != '\0') {
2331 strcat(tmpnew, ":");
2332 strcat(tmpnew, tmp);
2333 }
2334 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2335 free(tmpnew);
2336 if (ret) {
2337 ret = -errno;
2338 goto error;
2339 }
2340 }
2341 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
2342 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
2343 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2344 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2345 "--group", config.tracing_group_name.value,
2346 NULL);
2347 break;
2348 }
2349 case LTTNG_CONSUMER32_UST:
2350 {
2351 if (config.consumerd32_lib_dir.value) {
2352 char *tmp;
2353 size_t tmplen;
2354 char *tmpnew;
2355
2356 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2357 if (!tmp) {
2358 tmp = "";
2359 }
2360 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
2361 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2362 if (!tmpnew) {
2363 ret = -ENOMEM;
2364 goto error;
2365 }
2366 strcat(tmpnew, config.consumerd32_lib_dir.value);
2367 if (tmp[0] != '\0') {
2368 strcat(tmpnew, ":");
2369 strcat(tmpnew, tmp);
2370 }
2371 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2372 free(tmpnew);
2373 if (ret) {
2374 ret = -errno;
2375 goto error;
2376 }
2377 }
2378 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
2379 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
2380 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2381 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2382 "--group", config.tracing_group_name.value,
2383 NULL);
2384 break;
2385 }
2386 default:
2387 ERR("unknown consumer type");
2388 errno = 0;
2389 }
2390 if (errno != 0) {
2391 PERROR("Consumer execl()");
2392 }
2393 /* Reaching this point, we got a failure on our execl(). */
2394 exit(EXIT_FAILURE);
2395 } else if (pid > 0) {
2396 ret = pid;
2397 } else {
2398 PERROR("start consumer fork");
2399 ret = -errno;
2400 }
2401 error:
2402 return ret;
2403 }
2404
2405 /*
2406 * Spawn the consumerd daemon and session daemon thread.
2407 */
2408 static int start_consumerd(struct consumer_data *consumer_data)
2409 {
2410 int ret;
2411
2412 /*
2413 * Set the listen() state on the socket since there is a possible race
2414 * between the exec() of the consumer daemon and this call if place in the
2415 * consumer thread. See bug #366 for more details.
2416 */
2417 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2418 if (ret < 0) {
2419 goto error;
2420 }
2421
2422 pthread_mutex_lock(&consumer_data->pid_mutex);
2423 if (consumer_data->pid != 0) {
2424 pthread_mutex_unlock(&consumer_data->pid_mutex);
2425 goto end;
2426 }
2427
2428 ret = spawn_consumerd(consumer_data);
2429 if (ret < 0) {
2430 ERR("Spawning consumerd failed");
2431 pthread_mutex_unlock(&consumer_data->pid_mutex);
2432 goto error;
2433 }
2434
2435 /* Setting up the consumer_data pid */
2436 consumer_data->pid = ret;
2437 DBG2("Consumer pid %d", consumer_data->pid);
2438 pthread_mutex_unlock(&consumer_data->pid_mutex);
2439
2440 DBG2("Spawning consumer control thread");
2441 ret = spawn_consumer_thread(consumer_data);
2442 if (ret < 0) {
2443 ERR("Fatal error spawning consumer control thread");
2444 goto error;
2445 }
2446
2447 end:
2448 return 0;
2449
2450 error:
2451 /* Cleanup already created sockets on error. */
2452 if (consumer_data->err_sock >= 0) {
2453 int err;
2454
2455 err = close(consumer_data->err_sock);
2456 if (err < 0) {
2457 PERROR("close consumer data error socket");
2458 }
2459 }
2460 return ret;
2461 }
2462
2463 /*
2464 * Setup necessary data for kernel tracer action.
2465 */
2466 static int init_kernel_tracer(void)
2467 {
2468 int ret;
2469
2470 /* Modprobe lttng kernel modules */
2471 ret = modprobe_lttng_control();
2472 if (ret < 0) {
2473 goto error;
2474 }
2475
2476 /* Open debugfs lttng */
2477 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2478 if (kernel_tracer_fd < 0) {
2479 DBG("Failed to open %s", module_proc_lttng);
2480 goto error_open;
2481 }
2482
2483 /* Validate kernel version */
2484 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
2485 &kernel_tracer_abi_version);
2486 if (ret < 0) {
2487 goto error_version;
2488 }
2489
2490 ret = modprobe_lttng_data();
2491 if (ret < 0) {
2492 goto error_modules;
2493 }
2494
2495 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
2496 kernel_tracer_fd);
2497 if (ret < 0) {
2498 goto error_modules;
2499 }
2500
2501 if (ret < 1) {
2502 WARN("Kernel tracer does not support buffer monitoring. "
2503 "The monitoring timer of channels in the kernel domain "
2504 "will be set to 0 (disabled).");
2505 }
2506
2507 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2508 return 0;
2509
2510 error_version:
2511 modprobe_remove_lttng_control();
2512 ret = close(kernel_tracer_fd);
2513 if (ret) {
2514 PERROR("close");
2515 }
2516 kernel_tracer_fd = -1;
2517 return LTTNG_ERR_KERN_VERSION;
2518
2519 error_modules:
2520 ret = close(kernel_tracer_fd);
2521 if (ret) {
2522 PERROR("close");
2523 }
2524
2525 error_open:
2526 modprobe_remove_lttng_control();
2527
2528 error:
2529 WARN("No kernel tracer available");
2530 kernel_tracer_fd = -1;
2531 if (!is_root) {
2532 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2533 } else {
2534 return LTTNG_ERR_KERN_NA;
2535 }
2536 }
2537
2538
2539 /*
2540 * Copy consumer output from the tracing session to the domain session. The
2541 * function also applies the right modification on a per domain basis for the
2542 * trace files destination directory.
2543 *
2544 * Should *NOT* be called with RCU read-side lock held.
2545 */
2546 static int copy_session_consumer(int domain, struct ltt_session *session)
2547 {
2548 int ret;
2549 const char *dir_name;
2550 struct consumer_output *consumer;
2551
2552 assert(session);
2553 assert(session->consumer);
2554
2555 switch (domain) {
2556 case LTTNG_DOMAIN_KERNEL:
2557 DBG3("Copying tracing session consumer output in kernel session");
2558 /*
2559 * XXX: We should audit the session creation and what this function
2560 * does "extra" in order to avoid a destroy since this function is used
2561 * in the domain session creation (kernel and ust) only. Same for UST
2562 * domain.
2563 */
2564 if (session->kernel_session->consumer) {
2565 consumer_output_put(session->kernel_session->consumer);
2566 }
2567 session->kernel_session->consumer =
2568 consumer_copy_output(session->consumer);
2569 /* Ease our life a bit for the next part */
2570 consumer = session->kernel_session->consumer;
2571 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2572 break;
2573 case LTTNG_DOMAIN_JUL:
2574 case LTTNG_DOMAIN_LOG4J:
2575 case LTTNG_DOMAIN_PYTHON:
2576 case LTTNG_DOMAIN_UST:
2577 DBG3("Copying tracing session consumer output in UST session");
2578 if (session->ust_session->consumer) {
2579 consumer_output_put(session->ust_session->consumer);
2580 }
2581 session->ust_session->consumer =
2582 consumer_copy_output(session->consumer);
2583 /* Ease our life a bit for the next part */
2584 consumer = session->ust_session->consumer;
2585 dir_name = DEFAULT_UST_TRACE_DIR;
2586 break;
2587 default:
2588 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2589 goto error;
2590 }
2591
2592 /* Append correct directory to subdir */
2593 strncat(consumer->subdir, dir_name,
2594 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2595 DBG3("Copy session consumer subdir %s", consumer->subdir);
2596
2597 ret = LTTNG_OK;
2598
2599 error:
2600 return ret;
2601 }
2602
2603 /*
2604 * Create an UST session and add it to the session ust list.
2605 *
2606 * Should *NOT* be called with RCU read-side lock held.
2607 */
2608 static int create_ust_session(struct ltt_session *session,
2609 struct lttng_domain *domain)
2610 {
2611 int ret;
2612 struct ltt_ust_session *lus = NULL;
2613
2614 assert(session);
2615 assert(domain);
2616 assert(session->consumer);
2617
2618 switch (domain->type) {
2619 case LTTNG_DOMAIN_JUL:
2620 case LTTNG_DOMAIN_LOG4J:
2621 case LTTNG_DOMAIN_PYTHON:
2622 case LTTNG_DOMAIN_UST:
2623 break;
2624 default:
2625 ERR("Unknown UST domain on create session %d", domain->type);
2626 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2627 goto error;
2628 }
2629
2630 DBG("Creating UST session");
2631
2632 lus = trace_ust_create_session(session->id);
2633 if (lus == NULL) {
2634 ret = LTTNG_ERR_UST_SESS_FAIL;
2635 goto error;
2636 }
2637
2638 lus->uid = session->uid;
2639 lus->gid = session->gid;
2640 lus->output_traces = session->output_traces;
2641 lus->snapshot_mode = session->snapshot_mode;
2642 lus->live_timer_interval = session->live_timer;
2643 session->ust_session = lus;
2644 if (session->shm_path[0]) {
2645 strncpy(lus->root_shm_path, session->shm_path,
2646 sizeof(lus->root_shm_path));
2647 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
2648 strncpy(lus->shm_path, session->shm_path,
2649 sizeof(lus->shm_path));
2650 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2651 strncat(lus->shm_path, "/ust",
2652 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2653 }
2654 /* Copy session output to the newly created UST session */
2655 ret = copy_session_consumer(domain->type, session);
2656 if (ret != LTTNG_OK) {
2657 goto error;
2658 }
2659
2660 return LTTNG_OK;
2661
2662 error:
2663 free(lus);
2664 session->ust_session = NULL;
2665 return ret;
2666 }
2667
2668 /*
2669 * Create a kernel tracer session then create the default channel.
2670 */
2671 static int create_kernel_session(struct ltt_session *session)
2672 {
2673 int ret;
2674
2675 DBG("Creating kernel session");
2676
2677 ret = kernel_create_session(session, kernel_tracer_fd);
2678 if (ret < 0) {
2679 ret = LTTNG_ERR_KERN_SESS_FAIL;
2680 goto error;
2681 }
2682
2683 /* Code flow safety */
2684 assert(session->kernel_session);
2685
2686 /* Copy session output to the newly created Kernel session */
2687 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2688 if (ret != LTTNG_OK) {
2689 goto error;
2690 }
2691
2692 session->kernel_session->uid = session->uid;
2693 session->kernel_session->gid = session->gid;
2694 session->kernel_session->output_traces = session->output_traces;
2695 session->kernel_session->snapshot_mode = session->snapshot_mode;
2696
2697 return LTTNG_OK;
2698
2699 error:
2700 trace_kernel_destroy_session(session->kernel_session);
2701 session->kernel_session = NULL;
2702 return ret;
2703 }
2704
2705 /*
2706 * Count number of session permitted by uid/gid.
2707 */
2708 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2709 {
2710 unsigned int i = 0;
2711 struct ltt_session *session;
2712 const struct ltt_session_list *session_list = session_get_list();
2713
2714 DBG("Counting number of available session for UID %d GID %d",
2715 uid, gid);
2716 cds_list_for_each_entry(session, &session_list->head, list) {
2717 if (!session_get(session)) {
2718 continue;
2719 }
2720 session_lock(session);
2721 /* Only count the sessions the user can control. */
2722 if (session_access_ok(session, uid, gid) &&
2723 !session->destroyed) {
2724 i++;
2725 }
2726 session_unlock(session);
2727 session_put(session);
2728 }
2729 return i;
2730 }
2731
2732 static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock,
2733 int *sock_error, struct lttng_event *event)
2734 {
2735 int fd, ret;
2736 struct lttng_userspace_probe_location *probe_location;
2737 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
2738 struct lttng_dynamic_buffer probe_location_buffer;
2739 struct lttng_buffer_view buffer_view;
2740
2741 /*
2742 * Create a buffer to store the serialized version of the probe
2743 * location.
2744 */
2745 lttng_dynamic_buffer_init(&probe_location_buffer);
2746 ret = lttng_dynamic_buffer_set_size(&probe_location_buffer,
2747 cmd_ctx->lsm->u.enable.userspace_probe_location_len);
2748 if (ret) {
2749 ret = LTTNG_ERR_NOMEM;
2750 goto error;
2751 }
2752
2753 /*
2754 * Receive the probe location.
2755 */
2756 ret = lttcomm_recv_unix_sock(sock, probe_location_buffer.data,
2757 probe_location_buffer.size);
2758 if (ret <= 0) {
2759 DBG("Nothing recv() from client var len data... continuing");
2760 *sock_error = 1;
2761 lttng_dynamic_buffer_reset(&probe_location_buffer);
2762 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2763 goto error;
2764 }
2765
2766 buffer_view = lttng_buffer_view_from_dynamic_buffer(
2767 &probe_location_buffer, 0, probe_location_buffer.size);
2768
2769 /*
2770 * Extract the probe location from the serialized version.
2771 */
2772 ret = lttng_userspace_probe_location_create_from_buffer(
2773 &buffer_view, &probe_location);
2774 if (ret < 0) {
2775 WARN("Failed to create a userspace probe location from the received buffer");
2776 lttng_dynamic_buffer_reset( &probe_location_buffer);
2777 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2778 goto error;
2779 }
2780
2781 /*
2782 * Receive the file descriptor to the target binary from the client.
2783 */
2784 DBG("Receiving userspace probe target FD from client ...");
2785 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2786 if (ret <= 0) {
2787 DBG("Nothing recv() from client userspace probe fd... continuing");
2788 *sock_error = 1;
2789 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2790 goto error;
2791 }
2792
2793 /*
2794 * Set the file descriptor received from the client through the unix
2795 * socket in the probe location.
2796 */
2797 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
2798 if (!lookup) {
2799 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2800 goto error;
2801 }
2802
2803 /*
2804 * From the kernel tracer's perspective, all userspace probe event types
2805 * are all the same: a file and an offset.
2806 */
2807 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
2808 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
2809 ret = lttng_userspace_probe_location_function_set_binary_fd(
2810 probe_location, fd);
2811 break;
2812 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
2813 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
2814 probe_location, fd);
2815 break;
2816 default:
2817 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2818 goto error;
2819 }
2820
2821 if (ret) {
2822 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2823 goto error;
2824 }
2825
2826 /* Attach the probe location to the event. */
2827 ret = lttng_event_set_userspace_probe_location(event, probe_location);
2828 if (ret) {
2829 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2830 goto error;
2831 }
2832
2833 lttng_dynamic_buffer_reset(&probe_location_buffer);
2834 error:
2835 return ret;
2836 }
2837
2838 /*
2839 * Check if the current kernel tracer supports the session rotation feature.
2840 * Return 1 if it does, 0 otherwise.
2841 */
2842 static int check_rotate_compatible(void)
2843 {
2844 int ret = 1;
2845
2846 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
2847 DBG("Kernel tracer version is not compatible with the rotation feature");
2848 ret = 0;
2849 }
2850
2851 return ret;
2852 }
2853
2854 /*
2855 * Process the command requested by the lttng client within the command
2856 * context structure. This function make sure that the return structure (llm)
2857 * is set and ready for transmission before returning.
2858 *
2859 * Return any error encountered or 0 for success.
2860 *
2861 * "sock" is only used for special-case var. len data.
2862 *
2863 * Should *NOT* be called with RCU read-side lock held.
2864 */
2865 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2866 int *sock_error)
2867 {
2868 int ret = LTTNG_OK;
2869 int need_tracing_session = 1;
2870 int need_domain;
2871
2872 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2873
2874 assert(!rcu_read_ongoing());
2875
2876 *sock_error = 0;
2877
2878 switch (cmd_ctx->lsm->cmd_type) {
2879 case LTTNG_CREATE_SESSION:
2880 case LTTNG_CREATE_SESSION_SNAPSHOT:
2881 case LTTNG_CREATE_SESSION_LIVE:
2882 case LTTNG_DESTROY_SESSION:
2883 case LTTNG_LIST_SESSIONS:
2884 case LTTNG_LIST_DOMAINS:
2885 case LTTNG_START_TRACE:
2886 case LTTNG_STOP_TRACE:
2887 case LTTNG_DATA_PENDING:
2888 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2889 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2890 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2891 case LTTNG_SNAPSHOT_RECORD:
2892 case LTTNG_SAVE_SESSION:
2893 case LTTNG_SET_SESSION_SHM_PATH:
2894 case LTTNG_REGENERATE_METADATA:
2895 case LTTNG_REGENERATE_STATEDUMP:
2896 case LTTNG_REGISTER_TRIGGER:
2897 case LTTNG_UNREGISTER_TRIGGER:
2898 case LTTNG_ROTATE_SESSION:
2899 case LTTNG_ROTATION_GET_INFO:
2900 case LTTNG_ROTATION_SET_SCHEDULE:
2901 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2902 need_domain = 0;
2903 break;
2904 default:
2905 need_domain = 1;
2906 }
2907
2908 if (config.no_kernel && need_domain
2909 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2910 if (!is_root) {
2911 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2912 } else {
2913 ret = LTTNG_ERR_KERN_NA;
2914 }
2915 goto error;
2916 }
2917
2918 /* Deny register consumer if we already have a spawned consumer. */
2919 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2920 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2921 if (kconsumer_data.pid > 0) {
2922 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2923 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2924 goto error;
2925 }
2926 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2927 }
2928
2929 /*
2930 * Check for command that don't needs to allocate a returned payload. We do
2931 * this here so we don't have to make the call for no payload at each
2932 * command.
2933 */
2934 switch(cmd_ctx->lsm->cmd_type) {
2935 case LTTNG_LIST_SESSIONS:
2936 case LTTNG_LIST_TRACEPOINTS:
2937 case LTTNG_LIST_TRACEPOINT_FIELDS:
2938 case LTTNG_LIST_DOMAINS:
2939 case LTTNG_LIST_CHANNELS:
2940 case LTTNG_LIST_EVENTS:
2941 case LTTNG_LIST_SYSCALLS:
2942 case LTTNG_LIST_TRACKER_PIDS:
2943 case LTTNG_DATA_PENDING:
2944 case LTTNG_ROTATE_SESSION:
2945 case LTTNG_ROTATION_GET_INFO:
2946 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2947 break;
2948 default:
2949 /* Setup lttng message with no payload */
2950 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
2951 if (ret < 0) {
2952 /* This label does not try to unlock the session */
2953 goto init_setup_error;
2954 }
2955 }
2956
2957 /* Commands that DO NOT need a session. */
2958 switch (cmd_ctx->lsm->cmd_type) {
2959 case LTTNG_CREATE_SESSION:
2960 case LTTNG_CREATE_SESSION_SNAPSHOT:
2961 case LTTNG_CREATE_SESSION_LIVE:
2962 case LTTNG_LIST_SESSIONS:
2963 case LTTNG_LIST_TRACEPOINTS:
2964 case LTTNG_LIST_SYSCALLS:
2965 case LTTNG_LIST_TRACEPOINT_FIELDS:
2966 case LTTNG_SAVE_SESSION:
2967 case LTTNG_REGISTER_TRIGGER:
2968 case LTTNG_UNREGISTER_TRIGGER:
2969 need_tracing_session = 0;
2970 break;
2971 default:
2972 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2973 /*
2974 * We keep the session list lock across _all_ commands
2975 * for now, because the per-session lock does not
2976 * handle teardown properly.
2977 */
2978 session_lock_list();
2979 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2980 if (cmd_ctx->session == NULL) {
2981 ret = LTTNG_ERR_SESS_NOT_FOUND;
2982 goto error;
2983 } else {
2984 /* Acquire lock for the session */
2985 session_lock(cmd_ctx->session);
2986 }
2987 break;
2988 }
2989
2990 /*
2991 * Commands that need a valid session but should NOT create one if none
2992 * exists. Instead of creating one and destroying it when the command is
2993 * handled, process that right before so we save some round trip in useless
2994 * code path.
2995 */
2996 switch (cmd_ctx->lsm->cmd_type) {
2997 case LTTNG_DISABLE_CHANNEL:
2998 case LTTNG_DISABLE_EVENT:
2999 switch (cmd_ctx->lsm->domain.type) {
3000 case LTTNG_DOMAIN_KERNEL:
3001 if (!cmd_ctx->session->kernel_session) {
3002 ret = LTTNG_ERR_NO_CHANNEL;
3003 goto error;
3004 }
3005 break;
3006 case LTTNG_DOMAIN_JUL:
3007 case LTTNG_DOMAIN_LOG4J:
3008 case LTTNG_DOMAIN_PYTHON:
3009 case LTTNG_DOMAIN_UST:
3010 if (!cmd_ctx->session->ust_session) {
3011 ret = LTTNG_ERR_NO_CHANNEL;
3012 goto error;
3013 }
3014 break;
3015 default:
3016 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3017 goto error;
3018 }
3019 default:
3020 break;
3021 }
3022
3023 if (!need_domain) {
3024 goto skip_domain;
3025 }
3026
3027 /*
3028 * Check domain type for specific "pre-action".
3029 */
3030 switch (cmd_ctx->lsm->domain.type) {
3031 case LTTNG_DOMAIN_KERNEL:
3032 if (!is_root) {
3033 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3034 goto error;
3035 }
3036
3037 /* Kernel tracer check */
3038 if (kernel_tracer_fd == -1) {
3039 /* Basically, load kernel tracer modules */
3040 ret = init_kernel_tracer();
3041 if (ret != 0) {
3042 goto error;
3043 }
3044 }
3045
3046 /* Consumer is in an ERROR state. Report back to client */
3047 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3048 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3049 goto error;
3050 }
3051
3052 /* Need a session for kernel command */
3053 if (need_tracing_session) {
3054 if (cmd_ctx->session->kernel_session == NULL) {
3055 ret = create_kernel_session(cmd_ctx->session);
3056 if (ret < 0) {
3057 ret = LTTNG_ERR_KERN_SESS_FAIL;
3058 goto error;
3059 }
3060 }
3061
3062 /* Start the kernel consumer daemon */
3063 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3064 if (kconsumer_data.pid == 0 &&
3065 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3066 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3067 ret = start_consumerd(&kconsumer_data);
3068 if (ret < 0) {
3069 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3070 goto error;
3071 }
3072 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3073 } else {
3074 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3075 }
3076
3077 /*
3078 * The consumer was just spawned so we need to add the socket to
3079 * the consumer output of the session if exist.
3080 */
3081 ret = consumer_create_socket(&kconsumer_data,
3082 cmd_ctx->session->kernel_session->consumer);
3083 if (ret < 0) {
3084 goto error;
3085 }
3086 }
3087
3088 break;
3089 case LTTNG_DOMAIN_JUL:
3090 case LTTNG_DOMAIN_LOG4J:
3091 case LTTNG_DOMAIN_PYTHON:
3092 case LTTNG_DOMAIN_UST:
3093 {
3094 if (!ust_app_supported()) {
3095 ret = LTTNG_ERR_NO_UST;
3096 goto error;
3097 }
3098 /* Consumer is in an ERROR state. Report back to client */
3099 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3100 ret = LTTNG_ERR_NO_USTCONSUMERD;
3101 goto error;
3102 }
3103
3104 if (need_tracing_session) {
3105 /* Create UST session if none exist. */
3106 if (cmd_ctx->session->ust_session == NULL) {
3107 ret = create_ust_session(cmd_ctx->session,
3108 &cmd_ctx->lsm->domain);
3109 if (ret != LTTNG_OK) {
3110 goto error;
3111 }
3112 }
3113
3114 /* Start the UST consumer daemons */
3115 /* 64-bit */
3116 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3117 if (config.consumerd64_bin_path.value &&
3118 ustconsumer64_data.pid == 0 &&
3119 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3120 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3121 ret = start_consumerd(&ustconsumer64_data);
3122 if (ret < 0) {
3123 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3124 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3125 goto error;
3126 }
3127
3128 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3129 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3130 } else {
3131 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3132 }
3133
3134 /*
3135 * Setup socket for consumer 64 bit. No need for atomic access
3136 * since it was set above and can ONLY be set in this thread.
3137 */
3138 ret = consumer_create_socket(&ustconsumer64_data,
3139 cmd_ctx->session->ust_session->consumer);
3140 if (ret < 0) {
3141 goto error;
3142 }
3143
3144 /* 32-bit */
3145 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3146 if (config.consumerd32_bin_path.value &&
3147 ustconsumer32_data.pid == 0 &&
3148 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3149 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3150 ret = start_consumerd(&ustconsumer32_data);
3151 if (ret < 0) {
3152 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3153 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3154 goto error;
3155 }
3156
3157 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3158 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3159 } else {
3160 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3161 }
3162
3163 /*
3164 * Setup socket for consumer 32 bit. No need for atomic access
3165 * since it was set above and can ONLY be set in this thread.
3166 */
3167 ret = consumer_create_socket(&ustconsumer32_data,
3168 cmd_ctx->session->ust_session->consumer);
3169 if (ret < 0) {
3170 goto error;
3171 }
3172 }
3173 break;
3174 }
3175 default:
3176 break;
3177 }
3178 skip_domain:
3179
3180 /* Validate consumer daemon state when start/stop trace command */
3181 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3182 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3183 switch (cmd_ctx->lsm->domain.type) {
3184 case LTTNG_DOMAIN_NONE:
3185 break;
3186 case LTTNG_DOMAIN_JUL:
3187 case LTTNG_DOMAIN_LOG4J:
3188 case LTTNG_DOMAIN_PYTHON:
3189 case LTTNG_DOMAIN_UST:
3190 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
3191 ret = LTTNG_ERR_NO_USTCONSUMERD;
3192 goto error;
3193 }
3194 break;
3195 case LTTNG_DOMAIN_KERNEL:
3196 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
3197 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3198 goto error;
3199 }
3200 break;
3201 default:
3202 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3203 goto error;
3204 }
3205 }
3206
3207 /*
3208 * Check that the UID or GID match that of the tracing session.
3209 * The root user can interact with all sessions.
3210 */
3211 if (need_tracing_session) {
3212 if (!session_access_ok(cmd_ctx->session,
3213 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3214 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) ||
3215