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