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