faf5d579fde6fbb7affa61267babdce675f6d844
[lttng-tools.git] / src / bin / lttng-sessiond / client.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #include "common/buffer-view.h"
11 #include "common/compat/socket.h"
12 #include "common/dynamic-buffer.h"
13 #include "common/dynamic-array.h"
14 #include "common/payload.h"
15 #include "common/payload-view.h"
16 #include "common/fd-handle.h"
17 #include "common/sessiond-comm/sessiond-comm.h"
18 #include "common/payload.h"
19 #include "common/payload-view.h"
20 #include "lttng/lttng-error.h"
21 #include "lttng/tracker.h"
22 #include <common/compat/getenv.h>
23 #include <common/tracker.h>
24 #include <common/unix.h>
25 #include <common/utils.h>
26 #include <lttng/event-internal.h>
27 #include <lttng/session-descriptor-internal.h>
28 #include <lttng/session-internal.h>
29 #include <lttng/userspace-probe-internal.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35
36 #include "client.h"
37 #include "lttng-sessiond.h"
38 #include "cmd.h"
39 #include "kernel.h"
40 #include "save.h"
41 #include "health-sessiond.h"
42 #include "testpoint.h"
43 #include "utils.h"
44 #include "manage-consumer.h"
45 #include "clear.h"
46
47 static bool is_root;
48
49 static struct thread_state {
50 sem_t ready;
51 bool running;
52 int client_sock;
53 } thread_state;
54
55 static void set_thread_status(bool running)
56 {
57 DBG("Marking client thread's state as %s", running ? "running" : "error");
58 thread_state.running = running;
59 sem_post(&thread_state.ready);
60 }
61
62 static bool wait_thread_status(void)
63 {
64 DBG("Waiting for client thread to be ready");
65 sem_wait(&thread_state.ready);
66 if (thread_state.running) {
67 DBG("Client thread is ready");
68 } else {
69 ERR("Initialization of client thread failed");
70 }
71
72 return thread_state.running;
73 }
74
75 /*
76 * Setup the outgoing data buffer for the response (llm) by allocating the
77 * right amount of memory and copying the original information from the lsm
78 * structure.
79 *
80 * Return 0 on success, negative value on error.
81 */
82 static int setup_lttng_msg(struct command_ctx *cmd_ctx,
83 const void *payload_buf, size_t payload_len,
84 const void *cmd_header_buf, size_t cmd_header_len)
85 {
86 int ret = 0;
87 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
88 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
89 const struct lttcomm_lttng_msg llm = {
90 .cmd_type = cmd_ctx->lsm.cmd_type,
91 .pid = cmd_ctx->lsm.domain.attr.pid,
92 .cmd_header_size = cmd_header_len,
93 .data_size = payload_len,
94 };
95
96 ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
97 if (ret) {
98 goto end;
99 }
100
101 lttng_dynamic_pointer_array_clear(&cmd_ctx->reply_payload._fd_handles);
102
103 cmd_ctx->lttng_msg_size = total_msg_size;
104
105 /* Append reply header. */
106 ret = lttng_dynamic_buffer_append(
107 &cmd_ctx->reply_payload.buffer, &llm, sizeof(llm));
108 if (ret) {
109 goto end;
110 }
111
112 /* Append command header. */
113 if (cmd_header_len) {
114 ret = lttng_dynamic_buffer_append(
115 &cmd_ctx->reply_payload.buffer, cmd_header_buf,
116 cmd_header_len);
117 if (ret) {
118 goto end;
119 }
120 }
121
122 /* Append payload. */
123 if (payload_len) {
124 ret = lttng_dynamic_buffer_append(
125 &cmd_ctx->reply_payload.buffer, payload_buf,
126 payload_len);
127 if (ret) {
128 goto end;
129 }
130 }
131
132 end:
133 return ret;
134 }
135
136 static int setup_empty_lttng_msg(struct command_ctx *cmd_ctx)
137 {
138 int ret;
139 const struct lttcomm_lttng_msg llm = {};
140
141 ret = lttng_dynamic_buffer_set_size(&cmd_ctx->reply_payload.buffer, 0);
142 if (ret) {
143 goto end;
144 }
145
146 /* Append place-holder reply header. */
147 ret = lttng_dynamic_buffer_append(
148 &cmd_ctx->reply_payload.buffer, &llm, sizeof(llm));
149 if (ret) {
150 goto end;
151 }
152
153 cmd_ctx->lttng_msg_size = sizeof(llm);
154 end:
155 return ret;
156 }
157
158 static void update_lttng_msg(struct command_ctx *cmd_ctx, size_t cmd_header_len,
159 size_t payload_len)
160 {
161 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
162 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
163 const struct lttcomm_lttng_msg llm = {
164 .cmd_type = cmd_ctx->lsm.cmd_type,
165 .pid = cmd_ctx->lsm.domain.attr.pid,
166 .cmd_header_size = cmd_header_len,
167 .data_size = payload_len,
168 };
169 struct lttcomm_lttng_msg *p_llm;
170
171 assert(cmd_ctx->reply_payload.buffer.size >= sizeof(llm));
172
173 p_llm = (typeof(p_llm)) cmd_ctx->reply_payload.buffer.data;
174
175 /* Update existing header. */
176 memcpy(p_llm, &llm, sizeof(llm));
177
178 cmd_ctx->lttng_msg_size = total_msg_size;
179 }
180
181 /*
182 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
183 * exec or it will fail.
184 */
185 static int spawn_consumer_thread(struct consumer_data *consumer_data)
186 {
187 return launch_consumer_management_thread(consumer_data) ? 0 : -1;
188 }
189
190 /*
191 * Fork and exec a consumer daemon (consumerd).
192 *
193 * Return pid if successful else -1.
194 */
195 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
196 {
197 int ret;
198 pid_t pid;
199 const char *consumer_to_use;
200 const char *verbosity;
201 struct stat st;
202
203 DBG("Spawning consumerd");
204
205 pid = fork();
206 if (pid == 0) {
207 /*
208 * Exec consumerd.
209 */
210 if (config.verbose_consumer) {
211 verbosity = "--verbose";
212 } else if (lttng_opt_quiet) {
213 verbosity = "--quiet";
214 } else {
215 verbosity = "";
216 }
217
218 switch (consumer_data->type) {
219 case LTTNG_CONSUMER_KERNEL:
220 /*
221 * Find out which consumerd to execute. We will first try the
222 * 64-bit path, then the sessiond's installation directory, and
223 * fallback on the 32-bit one,
224 */
225 DBG3("Looking for a kernel consumer at these locations:");
226 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
227 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
228 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
229 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
230 DBG3("Found location #1");
231 consumer_to_use = config.consumerd64_bin_path.value;
232 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
233 DBG3("Found location #2");
234 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
235 } else if (config.consumerd32_bin_path.value &&
236 stat(config.consumerd32_bin_path.value, &st) == 0) {
237 DBG3("Found location #3");
238 consumer_to_use = config.consumerd32_bin_path.value;
239 } else {
240 DBG("Could not find any valid consumerd executable");
241 ret = -EINVAL;
242 goto error;
243 }
244 DBG("Using kernel consumer at: %s", consumer_to_use);
245 (void) execl(consumer_to_use,
246 "lttng-consumerd", verbosity, "-k",
247 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
248 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
249 "--group", config.tracing_group_name.value,
250 NULL);
251 break;
252 case LTTNG_CONSUMER64_UST:
253 {
254 if (config.consumerd64_lib_dir.value) {
255 const char *tmp;
256 size_t tmplen;
257 char *tmpnew;
258
259 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
260 if (!tmp) {
261 tmp = "";
262 }
263 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
264 tmpnew = zmalloc(tmplen + 1 /* \0 */);
265 if (!tmpnew) {
266 ret = -ENOMEM;
267 goto error;
268 }
269 strcat(tmpnew, config.consumerd64_lib_dir.value);
270 if (tmp[0] != '\0') {
271 strcat(tmpnew, ":");
272 strcat(tmpnew, tmp);
273 }
274 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
275 free(tmpnew);
276 if (ret) {
277 ret = -errno;
278 goto error;
279 }
280 }
281 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
282 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
283 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
284 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
285 "--group", config.tracing_group_name.value,
286 NULL);
287 break;
288 }
289 case LTTNG_CONSUMER32_UST:
290 {
291 if (config.consumerd32_lib_dir.value) {
292 const char *tmp;
293 size_t tmplen;
294 char *tmpnew;
295
296 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
297 if (!tmp) {
298 tmp = "";
299 }
300 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
301 tmpnew = zmalloc(tmplen + 1 /* \0 */);
302 if (!tmpnew) {
303 ret = -ENOMEM;
304 goto error;
305 }
306 strcat(tmpnew, config.consumerd32_lib_dir.value);
307 if (tmp[0] != '\0') {
308 strcat(tmpnew, ":");
309 strcat(tmpnew, tmp);
310 }
311 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
312 free(tmpnew);
313 if (ret) {
314 ret = -errno;
315 goto error;
316 }
317 }
318 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
319 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
320 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
321 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
322 "--group", config.tracing_group_name.value,
323 NULL);
324 break;
325 }
326 default:
327 ERR("unknown consumer type");
328 errno = 0;
329 }
330 if (errno != 0) {
331 PERROR("Consumer execl()");
332 }
333 /* Reaching this point, we got a failure on our execl(). */
334 exit(EXIT_FAILURE);
335 } else if (pid > 0) {
336 ret = pid;
337 } else {
338 PERROR("start consumer fork");
339 ret = -errno;
340 }
341 error:
342 return ret;
343 }
344
345 /*
346 * Spawn the consumerd daemon and session daemon thread.
347 */
348 static int start_consumerd(struct consumer_data *consumer_data)
349 {
350 int ret;
351
352 /*
353 * Set the listen() state on the socket since there is a possible race
354 * between the exec() of the consumer daemon and this call if place in the
355 * consumer thread. See bug #366 for more details.
356 */
357 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
358 if (ret < 0) {
359 goto error;
360 }
361
362 pthread_mutex_lock(&consumer_data->pid_mutex);
363 if (consumer_data->pid != 0) {
364 pthread_mutex_unlock(&consumer_data->pid_mutex);
365 goto end;
366 }
367
368 ret = spawn_consumerd(consumer_data);
369 if (ret < 0) {
370 ERR("Spawning consumerd failed");
371 pthread_mutex_unlock(&consumer_data->pid_mutex);
372 goto error;
373 }
374
375 /* Setting up the consumer_data pid */
376 consumer_data->pid = ret;
377 DBG2("Consumer pid %d", consumer_data->pid);
378 pthread_mutex_unlock(&consumer_data->pid_mutex);
379
380 DBG2("Spawning consumer control thread");
381 ret = spawn_consumer_thread(consumer_data);
382 if (ret < 0) {
383 ERR("Fatal error spawning consumer control thread");
384 goto error;
385 }
386
387 end:
388 return 0;
389
390 error:
391 /* Cleanup already created sockets on error. */
392 if (consumer_data->err_sock >= 0) {
393 int err;
394
395 err = close(consumer_data->err_sock);
396 if (err < 0) {
397 PERROR("close consumer data error socket");
398 }
399 }
400 return ret;
401 }
402
403 /*
404 * Copy consumer output from the tracing session to the domain session. The
405 * function also applies the right modification on a per domain basis for the
406 * trace files destination directory.
407 *
408 * Should *NOT* be called with RCU read-side lock held.
409 */
410 static int copy_session_consumer(int domain, struct ltt_session *session)
411 {
412 int ret;
413 const char *dir_name;
414 struct consumer_output *consumer;
415
416 assert(session);
417 assert(session->consumer);
418
419 switch (domain) {
420 case LTTNG_DOMAIN_KERNEL:
421 DBG3("Copying tracing session consumer output in kernel session");
422 /*
423 * XXX: We should audit the session creation and what this function
424 * does "extra" in order to avoid a destroy since this function is used
425 * in the domain session creation (kernel and ust) only. Same for UST
426 * domain.
427 */
428 if (session->kernel_session->consumer) {
429 consumer_output_put(session->kernel_session->consumer);
430 }
431 session->kernel_session->consumer =
432 consumer_copy_output(session->consumer);
433 /* Ease our life a bit for the next part */
434 consumer = session->kernel_session->consumer;
435 dir_name = DEFAULT_KERNEL_TRACE_DIR;
436 break;
437 case LTTNG_DOMAIN_JUL:
438 case LTTNG_DOMAIN_LOG4J:
439 case LTTNG_DOMAIN_PYTHON:
440 case LTTNG_DOMAIN_UST:
441 DBG3("Copying tracing session consumer output in UST session");
442 if (session->ust_session->consumer) {
443 consumer_output_put(session->ust_session->consumer);
444 }
445 session->ust_session->consumer =
446 consumer_copy_output(session->consumer);
447 /* Ease our life a bit for the next part */
448 consumer = session->ust_session->consumer;
449 dir_name = DEFAULT_UST_TRACE_DIR;
450 break;
451 default:
452 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
453 goto error;
454 }
455
456 /* Append correct directory to subdir */
457 ret = lttng_strncpy(consumer->domain_subdir, dir_name,
458 sizeof(consumer->domain_subdir));
459 if (ret) {
460 ret = LTTNG_ERR_UNK;
461 goto error;
462 }
463 DBG3("Copy session consumer subdir %s", consumer->domain_subdir);
464 ret = LTTNG_OK;
465
466 error:
467 return ret;
468 }
469
470 /*
471 * Create an UST session and add it to the session ust list.
472 *
473 * Should *NOT* be called with RCU read-side lock held.
474 */
475 static int create_ust_session(struct ltt_session *session,
476 const struct lttng_domain *domain)
477 {
478 int ret;
479 struct ltt_ust_session *lus = NULL;
480
481 assert(session);
482 assert(domain);
483 assert(session->consumer);
484
485 switch (domain->type) {
486 case LTTNG_DOMAIN_JUL:
487 case LTTNG_DOMAIN_LOG4J:
488 case LTTNG_DOMAIN_PYTHON:
489 case LTTNG_DOMAIN_UST:
490 break;
491 default:
492 ERR("Unknown UST domain on create session %d", domain->type);
493 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
494 goto error;
495 }
496
497 DBG("Creating UST session");
498
499 lus = trace_ust_create_session(session->id);
500 if (lus == NULL) {
501 ret = LTTNG_ERR_UST_SESS_FAIL;
502 goto error;
503 }
504
505 lus->uid = session->uid;
506 lus->gid = session->gid;
507 lus->output_traces = session->output_traces;
508 lus->snapshot_mode = session->snapshot_mode;
509 lus->live_timer_interval = session->live_timer;
510 session->ust_session = lus;
511 if (session->shm_path[0]) {
512 strncpy(lus->root_shm_path, session->shm_path,
513 sizeof(lus->root_shm_path));
514 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
515 strncpy(lus->shm_path, session->shm_path,
516 sizeof(lus->shm_path));
517 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
518 strncat(lus->shm_path, "/ust",
519 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
520 }
521 /* Copy session output to the newly created UST session */
522 ret = copy_session_consumer(domain->type, session);
523 if (ret != LTTNG_OK) {
524 goto error;
525 }
526
527 return LTTNG_OK;
528
529 error:
530 free(lus);
531 session->ust_session = NULL;
532 return ret;
533 }
534
535 /*
536 * Create a kernel tracer session then create the default channel.
537 */
538 static int create_kernel_session(struct ltt_session *session)
539 {
540 int ret;
541
542 DBG("Creating kernel session");
543
544 ret = kernel_create_session(session);
545 if (ret < 0) {
546 ret = LTTNG_ERR_KERN_SESS_FAIL;
547 goto error_create;
548 }
549
550 /* Code flow safety */
551 assert(session->kernel_session);
552
553 /* Copy session output to the newly created Kernel session */
554 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
555 if (ret != LTTNG_OK) {
556 goto error;
557 }
558
559 session->kernel_session->uid = session->uid;
560 session->kernel_session->gid = session->gid;
561 session->kernel_session->output_traces = session->output_traces;
562 session->kernel_session->snapshot_mode = session->snapshot_mode;
563 session->kernel_session->is_live_session = session->live_timer != 0;
564
565 return LTTNG_OK;
566
567 error:
568 trace_kernel_destroy_session(session->kernel_session);
569 session->kernel_session = NULL;
570 error_create:
571 return ret;
572 }
573
574 /*
575 * Count number of session permitted by uid/gid.
576 */
577 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
578 {
579 unsigned int i = 0;
580 struct ltt_session *session;
581 const struct ltt_session_list *session_list = session_get_list();
582
583 DBG("Counting number of available session for UID %d", uid);
584 cds_list_for_each_entry(session, &session_list->head, list) {
585 if (!session_get(session)) {
586 continue;
587 }
588 session_lock(session);
589 /* Only count the sessions the user can control. */
590 if (session_access_ok(session, uid) &&
591 !session->destroyed) {
592 i++;
593 }
594 session_unlock(session);
595 session_put(session);
596 }
597 return i;
598 }
599
600 static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock,
601 int *sock_error, struct lttng_event *event)
602 {
603 int fd = -1, ret;
604 struct lttng_userspace_probe_location *probe_location;
605 struct lttng_payload probe_location_payload;
606 struct fd_handle *handle = NULL;
607
608 /*
609 * Create a payload to store the serialized version of the probe
610 * location.
611 */
612 lttng_payload_init(&probe_location_payload);
613
614 ret = lttng_dynamic_buffer_set_size(&probe_location_payload.buffer,
615 cmd_ctx->lsm.u.enable.userspace_probe_location_len);
616 if (ret) {
617 ret = LTTNG_ERR_NOMEM;
618 goto error;
619 }
620
621 /*
622 * Receive the probe location.
623 */
624 ret = lttcomm_recv_unix_sock(sock, probe_location_payload.buffer.data,
625 probe_location_payload.buffer.size);
626 if (ret <= 0) {
627 DBG("Nothing recv() from client var len data... continuing");
628 *sock_error = 1;
629 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
630 goto error;
631 }
632
633 /*
634 * Receive the file descriptor to the target binary from the client.
635 */
636 DBG("Receiving userspace probe target FD from client ...");
637 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
638 if (ret <= 0) {
639 DBG("Nothing recv() from client userspace probe fd... continuing");
640 *sock_error = 1;
641 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
642 goto error;
643 }
644
645 handle = fd_handle_create(fd);
646 if (!handle) {
647 ret = LTTNG_ERR_NOMEM;
648 goto error;
649 }
650
651 /* Transferred to the handle. */
652 fd = -1;
653
654 ret = lttng_payload_push_fd_handle(&probe_location_payload, handle);
655 if (ret) {
656 ERR("Failed to add userspace probe file descriptor to payload");
657 ret = LTTNG_ERR_NOMEM;
658 goto error;
659 }
660
661 fd_handle_put(handle);
662 handle = NULL;
663
664 {
665 struct lttng_payload_view view = lttng_payload_view_from_payload(
666 &probe_location_payload, 0, -1);
667
668 /* Extract the probe location from the serialized version. */
669 ret = lttng_userspace_probe_location_create_from_payload(
670 &view, &probe_location);
671 }
672 if (ret < 0) {
673 WARN("Failed to create a userspace probe location from the received buffer");
674 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
675 goto error;
676 }
677
678 /* Attach the probe location to the event. */
679 ret = lttng_event_set_userspace_probe_location(event, probe_location);
680 if (ret) {
681 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
682 goto error;
683 }
684
685 error:
686 if (fd >= 0) {
687 if (close(fd)) {
688 PERROR("Failed to close userspace probe location binary fd");
689 }
690 }
691
692 fd_handle_put(handle);
693 lttng_payload_reset(&probe_location_payload);
694 return ret;
695 }
696
697 /*
698 * Version of setup_lttng_msg() without command header.
699 */
700 static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
701 void *payload_buf, size_t payload_len)
702 {
703 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
704 }
705
706 /*
707 * Check if the current kernel tracer supports the session rotation feature.
708 * Return 1 if it does, 0 otherwise.
709 */
710 static int check_rotate_compatible(void)
711 {
712 int ret = 1;
713
714 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
715 DBG("Kernel tracer version is not compatible with the rotation feature");
716 ret = 0;
717 }
718
719 return ret;
720 }
721
722 /*
723 * Send data on a unix socket using the liblttsessiondcomm API.
724 *
725 * Return lttcomm error code.
726 */
727 static int send_unix_sock(int sock, struct lttng_payload_view *view)
728 {
729 int ret;
730 const int fd_count = lttng_payload_view_get_fd_handle_count(view);
731
732 /* Check valid length */
733 if (view->buffer.size == 0) {
734 ret = -1;
735 goto end;
736 }
737
738 ret = lttcomm_send_unix_sock(
739 sock, view->buffer.data, view->buffer.size);
740 if (ret < 0) {
741 goto end;
742 }
743
744 if (fd_count > 0) {
745 ret = lttcomm_send_payload_view_fds_unix_sock(sock, view);
746 if (ret < 0) {
747 goto end;
748 }
749 }
750
751 end:
752 return ret;
753 }
754
755 /*
756 * Process the command requested by the lttng client within the command
757 * context structure. This function make sure that the return structure (llm)
758 * is set and ready for transmission before returning.
759 *
760 * Return any error encountered or 0 for success.
761 *
762 * "sock" is only used for special-case var. len data.
763 * A command may assume the ownership of the socket, in which case its value
764 * should be set to -1.
765 *
766 * Should *NOT* be called with RCU read-side lock held.
767 */
768 static int process_client_msg(struct command_ctx *cmd_ctx, int *sock,
769 int *sock_error)
770 {
771 int ret = LTTNG_OK;
772 int need_tracing_session = 1;
773 int need_domain;
774
775 DBG("Processing client command %d", cmd_ctx->lsm.cmd_type);
776
777 assert(!rcu_read_ongoing());
778
779 *sock_error = 0;
780
781 switch (cmd_ctx->lsm.cmd_type) {
782 case LTTNG_CREATE_SESSION_EXT:
783 case LTTNG_DESTROY_SESSION:
784 case LTTNG_LIST_SESSIONS:
785 case LTTNG_LIST_DOMAINS:
786 case LTTNG_START_TRACE:
787 case LTTNG_STOP_TRACE:
788 case LTTNG_DATA_PENDING:
789 case LTTNG_SNAPSHOT_ADD_OUTPUT:
790 case LTTNG_SNAPSHOT_DEL_OUTPUT:
791 case LTTNG_SNAPSHOT_LIST_OUTPUT:
792 case LTTNG_SNAPSHOT_RECORD:
793 case LTTNG_SAVE_SESSION:
794 case LTTNG_SET_SESSION_SHM_PATH:
795 case LTTNG_REGENERATE_METADATA:
796 case LTTNG_REGENERATE_STATEDUMP:
797 case LTTNG_REGISTER_TRIGGER:
798 case LTTNG_UNREGISTER_TRIGGER:
799 case LTTNG_ROTATE_SESSION:
800 case LTTNG_ROTATION_GET_INFO:
801 case LTTNG_ROTATION_SET_SCHEDULE:
802 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
803 case LTTNG_CLEAR_SESSION:
804 case LTTNG_LIST_TRIGGERS:
805 need_domain = 0;
806 break;
807 default:
808 need_domain = 1;
809 }
810
811 if (config.no_kernel && need_domain
812 && cmd_ctx->lsm.domain.type == LTTNG_DOMAIN_KERNEL) {
813 if (!is_root) {
814 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
815 } else {
816 ret = LTTNG_ERR_KERN_NA;
817 }
818 goto error;
819 }
820
821 /* Deny register consumer if we already have a spawned consumer. */
822 if (cmd_ctx->lsm.cmd_type == LTTNG_REGISTER_CONSUMER) {
823 pthread_mutex_lock(&kconsumer_data.pid_mutex);
824 if (kconsumer_data.pid > 0) {
825 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
826 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
827 goto error;
828 }
829 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
830 }
831
832 /*
833 * Check for command that don't needs to allocate a returned payload. We do
834 * this here so we don't have to make the call for no payload at each
835 * command.
836 */
837 switch(cmd_ctx->lsm.cmd_type) {
838 case LTTNG_LIST_SESSIONS:
839 case LTTNG_LIST_TRACEPOINTS:
840 case LTTNG_LIST_TRACEPOINT_FIELDS:
841 case LTTNG_LIST_DOMAINS:
842 case LTTNG_LIST_CHANNELS:
843 case LTTNG_LIST_EVENTS:
844 case LTTNG_LIST_SYSCALLS:
845 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
846 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
847 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
848 case LTTNG_DATA_PENDING:
849 case LTTNG_ROTATE_SESSION:
850 case LTTNG_ROTATION_GET_INFO:
851 case LTTNG_LIST_TRIGGERS:
852 break;
853 default:
854 /* Setup lttng message with no payload */
855 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
856 if (ret < 0) {
857 /* This label does not try to unlock the session */
858 goto init_setup_error;
859 }
860 }
861
862 /* Commands that DO NOT need a session. */
863 switch (cmd_ctx->lsm.cmd_type) {
864 case LTTNG_CREATE_SESSION_EXT:
865 case LTTNG_LIST_SESSIONS:
866 case LTTNG_LIST_TRACEPOINTS:
867 case LTTNG_LIST_SYSCALLS:
868 case LTTNG_LIST_TRACEPOINT_FIELDS:
869 case LTTNG_SAVE_SESSION:
870 case LTTNG_REGISTER_TRIGGER:
871 case LTTNG_UNREGISTER_TRIGGER:
872 case LTTNG_LIST_TRIGGERS:
873 need_tracing_session = 0;
874 break;
875 default:
876 DBG("Getting session %s by name", cmd_ctx->lsm.session.name);
877 /*
878 * We keep the session list lock across _all_ commands
879 * for now, because the per-session lock does not
880 * handle teardown properly.
881 */
882 session_lock_list();
883 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm.session.name);
884 if (cmd_ctx->session == NULL) {
885 ret = LTTNG_ERR_SESS_NOT_FOUND;
886 goto error;
887 } else {
888 /* Acquire lock for the session */
889 session_lock(cmd_ctx->session);
890 }
891 break;
892 }
893
894 /*
895 * Commands that need a valid session but should NOT create one if none
896 * exists. Instead of creating one and destroying it when the command is
897 * handled, process that right before so we save some round trip in useless
898 * code path.
899 */
900 switch (cmd_ctx->lsm.cmd_type) {
901 case LTTNG_DISABLE_CHANNEL:
902 case LTTNG_DISABLE_EVENT:
903 switch (cmd_ctx->lsm.domain.type) {
904 case LTTNG_DOMAIN_KERNEL:
905 if (!cmd_ctx->session->kernel_session) {
906 ret = LTTNG_ERR_NO_CHANNEL;
907 goto error;
908 }
909 break;
910 case LTTNG_DOMAIN_JUL:
911 case LTTNG_DOMAIN_LOG4J:
912 case LTTNG_DOMAIN_PYTHON:
913 case LTTNG_DOMAIN_UST:
914 if (!cmd_ctx->session->ust_session) {
915 ret = LTTNG_ERR_NO_CHANNEL;
916 goto error;
917 }
918 break;
919 default:
920 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
921 goto error;
922 }
923 default:
924 break;
925 }
926
927 if (!need_domain) {
928 goto skip_domain;
929 }
930
931 /*
932 * Check domain type for specific "pre-action".
933 */
934 switch (cmd_ctx->lsm.domain.type) {
935 case LTTNG_DOMAIN_KERNEL:
936 if (!is_root) {
937 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
938 goto error;
939 }
940
941 /* Kernel tracer check */
942 if (!kernel_tracer_is_initialized()) {
943 /* Basically, load kernel tracer modules */
944 ret = init_kernel_tracer();
945 if (ret != 0) {
946 goto error;
947 }
948 }
949
950 /* Consumer is in an ERROR state. Report back to client */
951 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
952 ret = LTTNG_ERR_NO_KERNCONSUMERD;
953 goto error;
954 }
955
956 /* Need a session for kernel command */
957 if (need_tracing_session) {
958 if (cmd_ctx->session->kernel_session == NULL) {
959 ret = create_kernel_session(cmd_ctx->session);
960 if (ret != LTTNG_OK) {
961 ret = LTTNG_ERR_KERN_SESS_FAIL;
962 goto error;
963 }
964 }
965
966 /* Start the kernel consumer daemon */
967 pthread_mutex_lock(&kconsumer_data.pid_mutex);
968 if (kconsumer_data.pid == 0 &&
969 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
970 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
971 ret = start_consumerd(&kconsumer_data);
972 if (ret < 0) {
973 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
974 goto error;
975 }
976 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
977 } else {
978 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
979 }
980
981 /*
982 * The consumer was just spawned so we need to add the socket to
983 * the consumer output of the session if exist.
984 */
985 ret = consumer_create_socket(&kconsumer_data,
986 cmd_ctx->session->kernel_session->consumer);
987 if (ret < 0) {
988 goto error;
989 }
990 }
991
992 break;
993 case LTTNG_DOMAIN_JUL:
994 case LTTNG_DOMAIN_LOG4J:
995 case LTTNG_DOMAIN_PYTHON:
996 case LTTNG_DOMAIN_UST:
997 {
998 if (!ust_app_supported()) {
999 ret = LTTNG_ERR_NO_UST;
1000 goto error;
1001 }
1002 /* Consumer is in an ERROR state. Report back to client */
1003 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
1004 ret = LTTNG_ERR_NO_USTCONSUMERD;
1005 goto error;
1006 }
1007
1008 if (need_tracing_session) {
1009 /* Create UST session if none exist. */
1010 if (cmd_ctx->session->ust_session == NULL) {
1011 ret = create_ust_session(cmd_ctx->session,
1012 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain));
1013 if (ret != LTTNG_OK) {
1014 goto error;
1015 }
1016 }
1017
1018 /* Start the UST consumer daemons */
1019 /* 64-bit */
1020 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
1021 if (config.consumerd64_bin_path.value &&
1022 ustconsumer64_data.pid == 0 &&
1023 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1024 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1025 ret = start_consumerd(&ustconsumer64_data);
1026 if (ret < 0) {
1027 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
1028 uatomic_set(&ust_consumerd64_fd, -EINVAL);
1029 goto error;
1030 }
1031
1032 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
1033 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1034 } else {
1035 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1036 }
1037
1038 /*
1039 * Setup socket for consumer 64 bit. No need for atomic access
1040 * since it was set above and can ONLY be set in this thread.
1041 */
1042 ret = consumer_create_socket(&ustconsumer64_data,
1043 cmd_ctx->session->ust_session->consumer);
1044 if (ret < 0) {
1045 goto error;
1046 }
1047
1048 /* 32-bit */
1049 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
1050 if (config.consumerd32_bin_path.value &&
1051 ustconsumer32_data.pid == 0 &&
1052 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1053 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1054 ret = start_consumerd(&ustconsumer32_data);
1055 if (ret < 0) {
1056 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
1057 uatomic_set(&ust_consumerd32_fd, -EINVAL);
1058 goto error;
1059 }
1060
1061 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
1062 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1063 } else {
1064 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1065 }
1066
1067 /*
1068 * Setup socket for consumer 32 bit. No need for atomic access
1069 * since it was set above and can ONLY be set in this thread.
1070 */
1071 ret = consumer_create_socket(&ustconsumer32_data,
1072 cmd_ctx->session->ust_session->consumer);
1073 if (ret < 0) {
1074 goto error;
1075 }
1076 }
1077 break;
1078 }
1079 default:
1080 break;
1081 }
1082 skip_domain:
1083
1084 /* Validate consumer daemon state when start/stop trace command */
1085 if (cmd_ctx->lsm.cmd_type == LTTNG_START_TRACE ||
1086 cmd_ctx->lsm.cmd_type == LTTNG_STOP_TRACE) {
1087 switch (cmd_ctx->lsm.domain.type) {
1088 case LTTNG_DOMAIN_NONE:
1089 break;
1090 case LTTNG_DOMAIN_JUL:
1091 case LTTNG_DOMAIN_LOG4J:
1092 case LTTNG_DOMAIN_PYTHON:
1093 case LTTNG_DOMAIN_UST:
1094 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
1095 ret = LTTNG_ERR_NO_USTCONSUMERD;
1096 goto error;
1097 }
1098 break;
1099 case LTTNG_DOMAIN_KERNEL:
1100 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
1101 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1102 goto error;
1103 }
1104 break;
1105 default:
1106 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1107 goto error;
1108 }
1109 }
1110
1111 /*
1112 * Check that the UID matches that of the tracing session.
1113 * The root user can interact with all sessions.
1114 */
1115 if (need_tracing_session) {
1116 if (!session_access_ok(cmd_ctx->session,
1117 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds)) ||
1118 cmd_ctx->session->destroyed) {
1119 ret = LTTNG_ERR_EPERM;
1120 goto error;
1121 }
1122 }
1123
1124 /*
1125 * Send relayd information to consumer as soon as we have a domain and a
1126 * session defined.
1127 */
1128 if (cmd_ctx->session && need_domain) {
1129 /*
1130 * Setup relayd if not done yet. If the relayd information was already
1131 * sent to the consumer, this call will gracefully return.
1132 */
1133 ret = cmd_setup_relayd(cmd_ctx->session);
1134 if (ret != LTTNG_OK) {
1135 goto error;
1136 }
1137 }
1138
1139 /* Process by command type */
1140 switch (cmd_ctx->lsm.cmd_type) {
1141 case LTTNG_ADD_CONTEXT:
1142 {
1143 /*
1144 * An LTTNG_ADD_CONTEXT command might have a supplementary
1145 * payload if the context being added is an application context.
1146 */
1147 if (cmd_ctx->lsm.u.context.ctx.ctx ==
1148 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1149 char *provider_name = NULL, *context_name = NULL;
1150 size_t provider_name_len =
1151 cmd_ctx->lsm.u.context.provider_name_len;
1152 size_t context_name_len =
1153 cmd_ctx->lsm.u.context.context_name_len;
1154
1155 if (provider_name_len == 0 || context_name_len == 0) {
1156 /*
1157 * Application provider and context names MUST
1158 * be provided.
1159 */
1160 ret = -LTTNG_ERR_INVALID;
1161 goto error;
1162 }
1163
1164 provider_name = zmalloc(provider_name_len + 1);
1165 if (!provider_name) {
1166 ret = -LTTNG_ERR_NOMEM;
1167 goto error;
1168 }
1169 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name =
1170 provider_name;
1171
1172 context_name = zmalloc(context_name_len + 1);
1173 if (!context_name) {
1174 ret = -LTTNG_ERR_NOMEM;
1175 goto error_add_context;
1176 }
1177 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name =
1178 context_name;
1179
1180 ret = lttcomm_recv_unix_sock(*sock, provider_name,
1181 provider_name_len);
1182 if (ret < 0) {
1183 goto error_add_context;
1184 }
1185
1186 ret = lttcomm_recv_unix_sock(*sock, context_name,
1187 context_name_len);
1188 if (ret < 0) {
1189 goto error_add_context;
1190 }
1191 }
1192
1193 /*
1194 * cmd_add_context assumes ownership of the provider and context
1195 * names.
1196 */
1197 ret = cmd_add_context(cmd_ctx->session,
1198 cmd_ctx->lsm.domain.type,
1199 cmd_ctx->lsm.u.context.channel_name,
1200 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.context.ctx),
1201 kernel_poll_pipe[1]);
1202
1203 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
1204 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
1205 error_add_context:
1206 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name);
1207 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name);
1208 if (ret < 0) {
1209 goto error;
1210 }
1211 break;
1212 }
1213 case LTTNG_DISABLE_CHANNEL:
1214 {
1215 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1216 cmd_ctx->lsm.u.disable.channel_name);
1217 break;
1218 }
1219 case LTTNG_DISABLE_EVENT:
1220 {
1221
1222 /*
1223 * FIXME: handle filter; for now we just receive the filter's
1224 * bytecode along with the filter expression which are sent by
1225 * liblttng-ctl and discard them.
1226 *
1227 * This fixes an issue where the client may block while sending
1228 * the filter payload and encounter an error because the session
1229 * daemon closes the socket without ever handling this data.
1230 */
1231 size_t count = cmd_ctx->lsm.u.disable.expression_len +
1232 cmd_ctx->lsm.u.disable.bytecode_len;
1233
1234 if (count) {
1235 char data[LTTNG_FILTER_MAX_LEN];
1236
1237 DBG("Discarding disable event command payload of size %zu", count);
1238 while (count) {
1239 ret = lttcomm_recv_unix_sock(*sock, data,
1240 count > sizeof(data) ? sizeof(data) : count);
1241 if (ret < 0) {
1242 goto error;
1243 }
1244
1245 count -= (size_t) ret;
1246 }
1247 }
1248 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1249 cmd_ctx->lsm.u.disable.channel_name,
1250 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.disable.event));
1251 break;
1252 }
1253 case LTTNG_ENABLE_CHANNEL:
1254 {
1255 cmd_ctx->lsm.u.channel.chan.attr.extended.ptr =
1256 (struct lttng_channel_extended *) &cmd_ctx->lsm.u.channel.extended;
1257 ret = cmd_enable_channel(cmd_ctx->session,
1258 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1259 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.channel.chan),
1260 kernel_poll_pipe[1]);
1261 break;
1262 }
1263 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
1264 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE:
1265 {
1266 struct lttng_dynamic_buffer payload;
1267 struct lttng_buffer_view payload_view;
1268 const bool add_value =
1269 cmd_ctx->lsm.cmd_type ==
1270 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE;
1271 const size_t name_len =
1272 cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
1273 .name_len;
1274 const enum lttng_domain_type domain_type =
1275 (enum lttng_domain_type)
1276 cmd_ctx->lsm.domain.type;
1277 const enum lttng_process_attr process_attr =
1278 (enum lttng_process_attr) cmd_ctx->lsm.u
1279 .process_attr_tracker_add_remove_include_value
1280 .process_attr;
1281 const enum lttng_process_attr_value_type value_type =
1282 (enum lttng_process_attr_value_type) cmd_ctx
1283 ->lsm.u
1284 .process_attr_tracker_add_remove_include_value
1285 .value_type;
1286 struct process_attr_value *value;
1287 enum lttng_error_code ret_code;
1288 long login_name_max;
1289
1290 login_name_max = sysconf(_SC_LOGIN_NAME_MAX);
1291 if (login_name_max < 0) {
1292 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1293 ret = LTTNG_ERR_INVALID;
1294 goto error;
1295 }
1296
1297 /* Receive remaining variable length payload if applicable. */
1298 if (name_len > login_name_max) {
1299 /*
1300 * POSIX mandates user and group names that are at least
1301 * 8 characters long. Note that although shadow-utils
1302 * (useradd, groupaadd, etc.) use 32 chars as their
1303 * limit (from bits/utmp.h, UT_NAMESIZE),
1304 * LOGIN_NAME_MAX is defined to 256.
1305 */
1306 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1307 add_value ? "addition" : "removal",
1308 name_len, login_name_max);
1309 ret = LTTNG_ERR_INVALID;
1310 goto error;
1311 }
1312
1313 lttng_dynamic_buffer_init(&payload);
1314 if (name_len != 0) {
1315 /*
1316 * Receive variable payload for user/group name
1317 * arguments.
1318 */
1319 ret = lttng_dynamic_buffer_set_size(&payload, name_len);
1320 if (ret) {
1321 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1322 add_value ? "add" : "remove");
1323 ret = LTTNG_ERR_NOMEM;
1324 goto error_add_remove_tracker_value;
1325 }
1326
1327 ret = lttcomm_recv_unix_sock(
1328 *sock, payload.data, name_len);
1329 if (ret <= 0) {
1330 ERR("Failed to receive payload of %s process attribute tracker value argument",
1331 add_value ? "add" : "remove");
1332 *sock_error = 1;
1333 ret = LTTNG_ERR_INVALID_PROTOCOL;
1334 goto error_add_remove_tracker_value;
1335 }
1336 }
1337
1338 payload_view = lttng_buffer_view_from_dynamic_buffer(
1339 &payload, 0, name_len);
1340 if (name_len > 0 && !lttng_buffer_view_is_valid(&payload_view)) {
1341 ret = LTTNG_ERR_INVALID_PROTOCOL;
1342 goto error_add_remove_tracker_value;
1343 }
1344
1345 /*
1346 * Validate the value type and domains are legal for the process
1347 * attribute tracker that is specified and convert the value to
1348 * add/remove to the internal sessiond representation.
1349 */
1350 ret_code = process_attr_value_from_comm(domain_type,
1351 process_attr, value_type,
1352 &cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
1353 .integral_value,
1354 &payload_view, &value);
1355 if (ret_code != LTTNG_OK) {
1356 ret = ret_code;
1357 goto error_add_remove_tracker_value;
1358 }
1359
1360 if (add_value) {
1361 ret = cmd_process_attr_tracker_inclusion_set_add_value(
1362 cmd_ctx->session, domain_type,
1363 process_attr, value);
1364 } else {
1365 ret = cmd_process_attr_tracker_inclusion_set_remove_value(
1366 cmd_ctx->session, domain_type,
1367 process_attr, value);
1368 }
1369 process_attr_value_destroy(value);
1370 error_add_remove_tracker_value:
1371 lttng_dynamic_buffer_reset(&payload);
1372 break;
1373 }
1374 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
1375 {
1376 enum lttng_tracking_policy tracking_policy;
1377 const enum lttng_domain_type domain_type =
1378 (enum lttng_domain_type)
1379 cmd_ctx->lsm.domain.type;
1380 const enum lttng_process_attr process_attr =
1381 (enum lttng_process_attr) cmd_ctx->lsm.u
1382 .process_attr_tracker_get_tracking_policy
1383 .process_attr;
1384
1385 ret = cmd_process_attr_tracker_get_tracking_policy(
1386 cmd_ctx->session, domain_type, process_attr,
1387 &tracking_policy);
1388 if (ret != LTTNG_OK) {
1389 goto error;
1390 }
1391
1392 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1393 &(uint32_t){tracking_policy}, sizeof(uint32_t));
1394 if (ret < 0) {
1395 ret = LTTNG_ERR_NOMEM;
1396 goto error;
1397 }
1398 ret = LTTNG_OK;
1399 break;
1400 }
1401 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY:
1402 {
1403 const enum lttng_tracking_policy tracking_policy =
1404 (enum lttng_tracking_policy) cmd_ctx->lsm.u
1405 .process_attr_tracker_set_tracking_policy
1406 .tracking_policy;
1407 const enum lttng_domain_type domain_type =
1408 (enum lttng_domain_type)
1409 cmd_ctx->lsm.domain.type;
1410 const enum lttng_process_attr process_attr =
1411 (enum lttng_process_attr) cmd_ctx->lsm.u
1412 .process_attr_tracker_set_tracking_policy
1413 .process_attr;
1414
1415 ret = cmd_process_attr_tracker_set_tracking_policy(
1416 cmd_ctx->session, domain_type, process_attr,
1417 tracking_policy);
1418 if (ret != LTTNG_OK) {
1419 goto error;
1420 }
1421 break;
1422 }
1423 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
1424 {
1425 struct lttng_process_attr_values *values;
1426 struct lttng_dynamic_buffer reply;
1427 const enum lttng_domain_type domain_type =
1428 (enum lttng_domain_type)
1429 cmd_ctx->lsm.domain.type;
1430 const enum lttng_process_attr process_attr =
1431 (enum lttng_process_attr) cmd_ctx->lsm.u
1432 .process_attr_tracker_get_inclusion_set
1433 .process_attr;
1434
1435 ret = cmd_process_attr_tracker_get_inclusion_set(
1436 cmd_ctx->session, domain_type, process_attr,
1437 &values);
1438 if (ret != LTTNG_OK) {
1439 goto error;
1440 }
1441
1442 lttng_dynamic_buffer_init(&reply);
1443 ret = lttng_process_attr_values_serialize(values, &reply);
1444 if (ret < 0) {
1445 goto error_tracker_get_inclusion_set;
1446 }
1447
1448 ret = setup_lttng_msg_no_cmd_header(
1449 cmd_ctx, reply.data, reply.size);
1450 if (ret < 0) {
1451 ret = LTTNG_ERR_NOMEM;
1452 goto error_tracker_get_inclusion_set;
1453 }
1454 ret = LTTNG_OK;
1455
1456 error_tracker_get_inclusion_set:
1457 lttng_process_attr_values_destroy(values);
1458 lttng_dynamic_buffer_reset(&reply);
1459 break;
1460 }
1461 case LTTNG_ENABLE_EVENT:
1462 {
1463 struct lttng_event *ev = NULL;
1464 struct lttng_event_exclusion *exclusion = NULL;
1465 struct lttng_filter_bytecode *bytecode = NULL;
1466 char *filter_expression = NULL;
1467
1468 /* Handle exclusion events and receive it from the client. */
1469 if (cmd_ctx->lsm.u.enable.exclusion_count > 0) {
1470 size_t count = cmd_ctx->lsm.u.enable.exclusion_count;
1471
1472 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
1473 (count * LTTNG_SYMBOL_NAME_LEN));
1474 if (!exclusion) {
1475 ret = LTTNG_ERR_EXCLUSION_NOMEM;
1476 goto error;
1477 }
1478
1479 DBG("Receiving var len exclusion event list from client ...");
1480 exclusion->count = count;
1481 ret = lttcomm_recv_unix_sock(*sock, exclusion->names,
1482 count * LTTNG_SYMBOL_NAME_LEN);
1483 if (ret <= 0) {
1484 DBG("Nothing recv() from client var len data... continuing");
1485 *sock_error = 1;
1486 free(exclusion);
1487 ret = LTTNG_ERR_EXCLUSION_INVAL;
1488 goto error;
1489 }
1490 }
1491
1492 /* Get filter expression from client. */
1493 if (cmd_ctx->lsm.u.enable.expression_len > 0) {
1494 size_t expression_len =
1495 cmd_ctx->lsm.u.enable.expression_len;
1496
1497 if (expression_len > LTTNG_FILTER_MAX_LEN) {
1498 ret = LTTNG_ERR_FILTER_INVAL;
1499 free(exclusion);
1500 goto error;
1501 }
1502
1503 filter_expression = zmalloc(expression_len);
1504 if (!filter_expression) {
1505 free(exclusion);
1506 ret = LTTNG_ERR_FILTER_NOMEM;
1507 goto error;
1508 }
1509
1510 /* Receive var. len. data */
1511 DBG("Receiving var len filter's expression from client ...");
1512 ret = lttcomm_recv_unix_sock(*sock, filter_expression,
1513 expression_len);
1514 if (ret <= 0) {
1515 DBG("Nothing recv() from client var len data... continuing");
1516 *sock_error = 1;
1517 free(filter_expression);
1518 free(exclusion);
1519 ret = LTTNG_ERR_FILTER_INVAL;
1520 goto error;
1521 }
1522 }
1523
1524 /* Handle filter and get bytecode from client. */
1525 if (cmd_ctx->lsm.u.enable.bytecode_len > 0) {
1526 size_t bytecode_len = cmd_ctx->lsm.u.enable.bytecode_len;
1527
1528 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1529 ret = LTTNG_ERR_FILTER_INVAL;
1530 free(filter_expression);
1531 free(exclusion);
1532 goto error;
1533 }
1534
1535 bytecode = zmalloc(bytecode_len);
1536 if (!bytecode) {
1537 free(filter_expression);
1538 free(exclusion);
1539 ret = LTTNG_ERR_FILTER_NOMEM;
1540 goto error;
1541 }
1542
1543 /* Receive var. len. data */
1544 DBG("Receiving var len filter's bytecode from client ...");
1545 ret = lttcomm_recv_unix_sock(*sock, bytecode, bytecode_len);
1546 if (ret <= 0) {
1547 DBG("Nothing recv() from client var len data... continuing");
1548 *sock_error = 1;
1549 free(filter_expression);
1550 free(bytecode);
1551 free(exclusion);
1552 ret = LTTNG_ERR_FILTER_INVAL;
1553 goto error;
1554 }
1555
1556 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
1557 free(filter_expression);
1558 free(bytecode);
1559 free(exclusion);
1560 ret = LTTNG_ERR_FILTER_INVAL;
1561 goto error;
1562 }
1563 }
1564
1565 ev = lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx->lsm.u.enable.event));
1566 if (!ev) {
1567 DBG("Failed to copy event: %s",
1568 cmd_ctx->lsm.u.enable.event.name);
1569 free(filter_expression);
1570 free(bytecode);
1571 free(exclusion);
1572 ret = LTTNG_ERR_NOMEM;
1573 goto error;
1574 }
1575
1576
1577 if (cmd_ctx->lsm.u.enable.userspace_probe_location_len > 0) {
1578 /* Expect a userspace probe description. */
1579 ret = receive_userspace_probe(cmd_ctx, *sock, sock_error, ev);
1580 if (ret) {
1581 free(filter_expression);
1582 free(bytecode);
1583 free(exclusion);
1584 lttng_event_destroy(ev);
1585 goto error;
1586 }
1587 }
1588
1589 ret = cmd_enable_event(cmd_ctx->session,
1590 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1591 cmd_ctx->lsm.u.enable.channel_name,
1592 ev,
1593 filter_expression, bytecode, exclusion,
1594 kernel_poll_pipe[1]);
1595 lttng_event_destroy(ev);
1596 break;
1597 }
1598 case LTTNG_LIST_TRACEPOINTS:
1599 {
1600 struct lttng_event *events;
1601 ssize_t nb_events;
1602
1603 session_lock_list();
1604 nb_events = cmd_list_tracepoints(cmd_ctx->lsm.domain.type, &events);
1605 session_unlock_list();
1606 if (nb_events < 0) {
1607 /* Return value is a negative lttng_error_code. */
1608 ret = -nb_events;
1609 goto error;
1610 }
1611
1612 /*
1613 * Setup lttng message with payload size set to the event list size in
1614 * bytes and then copy list into the llm payload.
1615 */
1616 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1617 sizeof(struct lttng_event) * nb_events);
1618 free(events);
1619
1620 if (ret < 0) {
1621 goto setup_error;
1622 }
1623
1624 ret = LTTNG_OK;
1625 break;
1626 }
1627 case LTTNG_LIST_TRACEPOINT_FIELDS:
1628 {
1629 struct lttng_event_field *fields;
1630 ssize_t nb_fields;
1631
1632 session_lock_list();
1633 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm.domain.type,
1634 &fields);
1635 session_unlock_list();
1636 if (nb_fields < 0) {
1637 /* Return value is a negative lttng_error_code. */
1638 ret = -nb_fields;
1639 goto error;
1640 }
1641
1642 /*
1643 * Setup lttng message with payload size set to the event list size in
1644 * bytes and then copy list into the llm payload.
1645 */
1646 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
1647 sizeof(struct lttng_event_field) * nb_fields);
1648 free(fields);
1649
1650 if (ret < 0) {
1651 goto setup_error;
1652 }
1653
1654 ret = LTTNG_OK;
1655 break;
1656 }
1657 case LTTNG_LIST_SYSCALLS:
1658 {
1659 struct lttng_event *events;
1660 ssize_t nb_events;
1661
1662 nb_events = cmd_list_syscalls(&events);
1663 if (nb_events < 0) {
1664 /* Return value is a negative lttng_error_code. */
1665 ret = -nb_events;
1666 goto error;
1667 }
1668
1669 /*
1670 * Setup lttng message with payload size set to the event list size in
1671 * bytes and then copy list into the llm payload.
1672 */
1673 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1674 sizeof(struct lttng_event) * nb_events);
1675 free(events);
1676
1677 if (ret < 0) {
1678 goto setup_error;
1679 }
1680
1681 ret = LTTNG_OK;
1682 break;
1683 }
1684 case LTTNG_SET_CONSUMER_URI:
1685 {
1686 size_t nb_uri, len;
1687 struct lttng_uri *uris;
1688
1689 nb_uri = cmd_ctx->lsm.u.uri.size;
1690 len = nb_uri * sizeof(struct lttng_uri);
1691
1692 if (nb_uri == 0) {
1693 ret = LTTNG_ERR_INVALID;
1694 goto error;
1695 }
1696
1697 uris = zmalloc(len);
1698 if (uris == NULL) {
1699 ret = LTTNG_ERR_FATAL;
1700 goto error;
1701 }
1702
1703 /* Receive variable len data */
1704 DBG("Receiving %zu URI(s) from client ...", nb_uri);
1705 ret = lttcomm_recv_unix_sock(*sock, uris, len);
1706 if (ret <= 0) {
1707 DBG("No URIs received from client... continuing");
1708 *sock_error = 1;
1709 ret = LTTNG_ERR_SESSION_FAIL;
1710 free(uris);
1711 goto error;
1712 }
1713
1714 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
1715 free(uris);
1716 if (ret != LTTNG_OK) {
1717 goto error;
1718 }
1719
1720
1721 break;
1722 }
1723 case LTTNG_START_TRACE:
1724 {
1725 /*
1726 * On the first start, if we have a kernel session and we have
1727 * enabled time or size-based rotations, we have to make sure
1728 * the kernel tracer supports it.
1729 */
1730 if (!cmd_ctx->session->has_been_started && \
1731 cmd_ctx->session->kernel_session && \
1732 (cmd_ctx->session->rotate_timer_period || \
1733 cmd_ctx->session->rotate_size) && \
1734 !check_rotate_compatible()) {
1735 DBG("Kernel tracer version is not compatible with the rotation feature");
1736 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1737 goto error;
1738 }
1739 ret = cmd_start_trace(cmd_ctx->session);
1740 break;
1741 }
1742 case LTTNG_STOP_TRACE:
1743 {
1744 ret = cmd_stop_trace(cmd_ctx->session);
1745 break;
1746 }
1747 case LTTNG_DESTROY_SESSION:
1748 {
1749 ret = cmd_destroy_session(cmd_ctx->session,
1750 notification_thread_handle,
1751 sock);
1752 break;
1753 }
1754 case LTTNG_LIST_DOMAINS:
1755 {
1756 ssize_t nb_dom;
1757 struct lttng_domain *domains = NULL;
1758
1759 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
1760 if (nb_dom < 0) {
1761 /* Return value is a negative lttng_error_code. */
1762 ret = -nb_dom;
1763 goto error;
1764 }
1765
1766 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
1767 nb_dom * sizeof(struct lttng_domain));
1768 free(domains);
1769
1770 if (ret < 0) {
1771 goto setup_error;
1772 }
1773
1774 ret = LTTNG_OK;
1775 break;
1776 }
1777 case LTTNG_LIST_CHANNELS:
1778 {
1779 ssize_t payload_size;
1780 struct lttng_channel *channels = NULL;
1781
1782 payload_size = cmd_list_channels(cmd_ctx->lsm.domain.type,
1783 cmd_ctx->session, &channels);
1784 if (payload_size < 0) {
1785 /* Return value is a negative lttng_error_code. */
1786 ret = -payload_size;
1787 goto error;
1788 }
1789
1790 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
1791 payload_size);
1792 free(channels);
1793
1794 if (ret < 0) {
1795 goto setup_error;
1796 }
1797
1798 ret = LTTNG_OK;
1799 break;
1800 }
1801 case LTTNG_LIST_EVENTS:
1802 {
1803 ssize_t list_ret;
1804 struct lttcomm_event_command_header cmd_header = {};
1805 size_t original_payload_size;
1806 size_t payload_size;
1807
1808 ret = setup_empty_lttng_msg(cmd_ctx);
1809 if (ret) {
1810 ret = LTTNG_ERR_NOMEM;
1811 goto setup_error;
1812 }
1813
1814 original_payload_size = cmd_ctx->reply_payload.buffer.size;
1815
1816 /* Extended infos are included at the end of the payload. */
1817 list_ret = cmd_list_events(cmd_ctx->lsm.domain.type,
1818 cmd_ctx->session,
1819 cmd_ctx->lsm.u.list.channel_name,
1820 &cmd_ctx->reply_payload);
1821 if (list_ret < 0) {
1822 /* Return value is a negative lttng_error_code. */
1823 ret = -list_ret;
1824 goto error;
1825 }
1826
1827 payload_size = cmd_ctx->reply_payload.buffer.size -
1828 sizeof(cmd_header) - original_payload_size;
1829 update_lttng_msg(cmd_ctx, sizeof(cmd_header), payload_size);
1830
1831 ret = LTTNG_OK;
1832 break;
1833 }
1834 case LTTNG_LIST_SESSIONS:
1835 {
1836 unsigned int nr_sessions;
1837 void *sessions_payload;
1838 size_t payload_len;
1839
1840 session_lock_list();
1841 nr_sessions = lttng_sessions_count(
1842 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1843 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1844
1845 payload_len = (sizeof(struct lttng_session) * nr_sessions) +
1846 (sizeof(struct lttng_session_extended) * nr_sessions);
1847 sessions_payload = zmalloc(payload_len);
1848
1849 if (!sessions_payload) {
1850 session_unlock_list();
1851 ret = -ENOMEM;
1852 goto setup_error;
1853 }
1854
1855 cmd_list_lttng_sessions(sessions_payload, nr_sessions,
1856 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1857 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1858 session_unlock_list();
1859
1860 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
1861 payload_len);
1862 free(sessions_payload);
1863
1864 if (ret < 0) {
1865 goto setup_error;
1866 }
1867
1868 ret = LTTNG_OK;
1869 break;
1870 }
1871 case LTTNG_REGISTER_CONSUMER:
1872 {
1873 struct consumer_data *cdata;
1874
1875 switch (cmd_ctx->lsm.domain.type) {
1876 case LTTNG_DOMAIN_KERNEL:
1877 cdata = &kconsumer_data;
1878 break;
1879 default:
1880 ret = LTTNG_ERR_UND;
1881 goto error;
1882 }
1883
1884 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1885 cmd_ctx->lsm.u.reg.path, cdata);
1886 break;
1887 }
1888 case LTTNG_DATA_PENDING:
1889 {
1890 int pending_ret;
1891 uint8_t pending_ret_byte;
1892
1893 pending_ret = cmd_data_pending(cmd_ctx->session);
1894
1895 /*
1896 * FIXME
1897 *
1898 * This function may returns 0 or 1 to indicate whether or not
1899 * there is data pending. In case of error, it should return an
1900 * LTTNG_ERR code. However, some code paths may still return
1901 * a nondescript error code, which we handle by returning an
1902 * "unknown" error.
1903 */
1904 if (pending_ret == 0 || pending_ret == 1) {
1905 /*
1906 * ret will be set to LTTNG_OK at the end of
1907 * this function.
1908 */
1909 } else if (pending_ret < 0) {
1910 ret = LTTNG_ERR_UNK;
1911 goto setup_error;
1912 } else {
1913 ret = pending_ret;
1914 goto setup_error;
1915 }
1916
1917 pending_ret_byte = (uint8_t) pending_ret;
1918
1919 /* 1 byte to return whether or not data is pending */
1920 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1921 &pending_ret_byte, 1);
1922
1923 if (ret < 0) {
1924 goto setup_error;
1925 }
1926
1927 ret = LTTNG_OK;
1928 break;
1929 }
1930 case LTTNG_SNAPSHOT_ADD_OUTPUT:
1931 {
1932 uint32_t snapshot_id;
1933 struct lttcomm_lttng_output_id reply;
1934
1935 ret = cmd_snapshot_add_output(cmd_ctx->session,
1936 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output),
1937 &snapshot_id);
1938 if (ret != LTTNG_OK) {
1939 goto error;
1940 }
1941 reply.id = snapshot_id;
1942
1943 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
1944 sizeof(reply));
1945 if (ret < 0) {
1946 goto setup_error;
1947 }
1948
1949 /* Copy output list into message payload */
1950 ret = LTTNG_OK;
1951 break;
1952 }
1953 case LTTNG_SNAPSHOT_DEL_OUTPUT:
1954 {
1955 ret = cmd_snapshot_del_output(cmd_ctx->session,
1956 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output));
1957 break;
1958 }
1959 case LTTNG_SNAPSHOT_LIST_OUTPUT:
1960 {
1961 ssize_t nb_output;
1962 struct lttng_snapshot_output *outputs = NULL;
1963
1964 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
1965 if (nb_output < 0) {
1966 ret = -nb_output;
1967 goto error;
1968 }
1969
1970 assert((nb_output > 0 && outputs) || nb_output == 0);
1971 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
1972 nb_output * sizeof(struct lttng_snapshot_output));
1973 free(outputs);
1974
1975 if (ret < 0) {
1976 goto setup_error;
1977 }
1978
1979 ret = LTTNG_OK;
1980 break;
1981 }
1982 case LTTNG_SNAPSHOT_RECORD:
1983 {
1984 ret = cmd_snapshot_record(cmd_ctx->session,
1985 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_record.output),
1986 cmd_ctx->lsm.u.snapshot_record.wait);
1987 break;
1988 }
1989 case LTTNG_CREATE_SESSION_EXT:
1990 {
1991 struct lttng_dynamic_buffer payload;
1992 struct lttng_session_descriptor *return_descriptor = NULL;
1993
1994 lttng_dynamic_buffer_init(&payload);
1995 ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor);
1996 if (ret != LTTNG_OK) {
1997 goto error;
1998 }
1999
2000 ret = lttng_session_descriptor_serialize(return_descriptor,
2001 &payload);
2002 if (ret) {
2003 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2004 lttng_session_descriptor_destroy(return_descriptor);
2005 ret = LTTNG_ERR_NOMEM;
2006 goto error;
2007 }
2008 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data,
2009 payload.size);
2010 if (ret) {
2011 lttng_session_descriptor_destroy(return_descriptor);
2012 ret = LTTNG_ERR_NOMEM;
2013 goto error;
2014 }
2015 lttng_dynamic_buffer_reset(&payload);
2016 lttng_session_descriptor_destroy(return_descriptor);
2017 ret = LTTNG_OK;
2018 break;
2019 }
2020 case LTTNG_SAVE_SESSION:
2021 {
2022 ret = cmd_save_sessions(&cmd_ctx->lsm.u.save_session.attr,
2023 &cmd_ctx->creds);
2024 break;
2025 }
2026 case LTTNG_SET_SESSION_SHM_PATH:
2027 {
2028 ret = cmd_set_session_shm_path(cmd_ctx->session,
2029 cmd_ctx->lsm.u.set_shm_path.shm_path);
2030 break;
2031 }
2032 case LTTNG_REGENERATE_METADATA:
2033 {
2034 ret = cmd_regenerate_metadata(cmd_ctx->session);
2035 break;
2036 }
2037 case LTTNG_REGENERATE_STATEDUMP:
2038 {
2039 ret = cmd_regenerate_statedump(cmd_ctx->session);
2040 break;
2041 }
2042 case LTTNG_REGISTER_TRIGGER:
2043 {
2044 struct lttng_trigger *return_trigger;
2045 size_t original_payload_size;
2046 size_t payload_size;
2047
2048 ret = setup_empty_lttng_msg(cmd_ctx);
2049 if (ret) {
2050 ret = LTTNG_ERR_NOMEM;
2051 goto setup_error;
2052 }
2053
2054 original_payload_size = cmd_ctx->reply_payload.buffer.size;
2055
2056 ret = cmd_register_trigger(cmd_ctx, *sock,
2057 notification_thread_handle, &return_trigger);
2058 if (ret != LTTNG_OK) {
2059 goto error;
2060 }
2061
2062 ret = lttng_trigger_serialize(return_trigger, &cmd_ctx->reply_payload);
2063 if (ret) {
2064 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2065 ret = LTTNG_ERR_NOMEM;
2066 lttng_trigger_destroy(return_trigger);
2067 goto error;
2068 }
2069
2070 lttng_trigger_destroy(return_trigger);
2071 return_trigger = NULL;
2072
2073 payload_size = cmd_ctx->reply_payload.buffer.size -
2074 original_payload_size;
2075
2076 update_lttng_msg(cmd_ctx, 0, payload_size);
2077
2078 ret = LTTNG_OK;
2079 break;
2080 }
2081 case LTTNG_UNREGISTER_TRIGGER:
2082 {
2083 ret = cmd_unregister_trigger(cmd_ctx, *sock,
2084 notification_thread_handle);
2085 break;
2086 }
2087 case LTTNG_ROTATE_SESSION:
2088 {
2089 struct lttng_rotate_session_return rotate_return;
2090
2091 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
2092
2093 memset(&rotate_return, 0, sizeof(rotate_return));
2094 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2095 DBG("Kernel tracer version is not compatible with the rotation feature");
2096 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2097 goto error;
2098 }
2099
2100 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return,
2101 false,
2102 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
2103 if (ret < 0) {
2104 ret = -ret;
2105 goto error;
2106 }
2107
2108 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
2109 sizeof(rotate_return));
2110 if (ret < 0) {
2111 ret = -ret;
2112 goto error;
2113 }
2114
2115 ret = LTTNG_OK;
2116 break;
2117 }
2118 case LTTNG_ROTATION_GET_INFO:
2119 {
2120 struct lttng_rotation_get_info_return get_info_return;
2121
2122 memset(&get_info_return, 0, sizeof(get_info_return));
2123 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
2124 cmd_ctx->lsm.u.get_rotation_info.rotation_id);
2125 if (ret < 0) {
2126 ret = -ret;
2127 goto error;
2128 }
2129
2130 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
2131 sizeof(get_info_return));
2132 if (ret < 0) {
2133 ret = -ret;
2134 goto error;
2135 }
2136
2137 ret = LTTNG_OK;
2138 break;
2139 }
2140 case LTTNG_ROTATION_SET_SCHEDULE:
2141 {
2142 bool set_schedule;
2143 enum lttng_rotation_schedule_type schedule_type;
2144 uint64_t value;
2145
2146 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2147 DBG("Kernel tracer version does not support session rotations");
2148 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2149 goto error;
2150 }
2151
2152 set_schedule = cmd_ctx->lsm.u.rotation_set_schedule.set == 1;
2153 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm.u.rotation_set_schedule.type;
2154 value = cmd_ctx->lsm.u.rotation_set_schedule.value;
2155
2156 ret = cmd_rotation_set_schedule(cmd_ctx->session,
2157 set_schedule,
2158 schedule_type,
2159 value,
2160 notification_thread_handle);
2161 if (ret != LTTNG_OK) {
2162 goto error;
2163 }
2164
2165 break;
2166 }
2167 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2168 {
2169 struct lttng_session_list_schedules_return schedules = {
2170 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
2171 .periodic.value = cmd_ctx->session->rotate_timer_period,
2172 .size.set = !!cmd_ctx->session->rotate_size,
2173 .size.value = cmd_ctx->session->rotate_size,
2174 };
2175
2176 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
2177 sizeof(schedules));
2178 if (ret < 0) {
2179 ret = -ret;
2180 goto error;
2181 }
2182
2183 ret = LTTNG_OK;
2184 break;
2185 }
2186 case LTTNG_CLEAR_SESSION:
2187 {
2188 ret = cmd_clear_session(cmd_ctx->session, sock);
2189 break;
2190 }
2191 case LTTNG_LIST_TRIGGERS:
2192 {
2193 struct lttng_triggers *return_triggers = NULL;
2194 size_t original_payload_size;
2195 size_t payload_size;
2196
2197 ret = setup_empty_lttng_msg(cmd_ctx);
2198 if (ret) {
2199 ret = LTTNG_ERR_NOMEM;
2200 goto setup_error;
2201 }
2202
2203 original_payload_size = cmd_ctx->reply_payload.buffer.size;
2204
2205 ret = cmd_list_triggers(cmd_ctx,
2206 notification_thread_handle, &return_triggers);
2207 if (ret != LTTNG_OK) {
2208 goto error;
2209 }
2210
2211 assert(return_triggers);
2212 ret = lttng_triggers_serialize(
2213 return_triggers, &cmd_ctx->reply_payload);
2214 lttng_triggers_destroy(return_triggers);
2215 if (ret) {
2216 ERR("Failed to serialize triggers in reply to `list triggers` command");
2217 ret = LTTNG_ERR_NOMEM;
2218 goto error;
2219 }
2220
2221 payload_size = cmd_ctx->reply_payload.buffer.size -
2222 original_payload_size;
2223
2224 update_lttng_msg(cmd_ctx, 0, payload_size);
2225
2226 ret = LTTNG_OK;
2227 break;
2228 }
2229 default:
2230 ret = LTTNG_ERR_UND;
2231 break;
2232 }
2233
2234 error:
2235 if (cmd_ctx->reply_payload.buffer.size == 0) {
2236 DBG("Missing llm header, creating one.");
2237 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
2238 goto setup_error;
2239 }
2240 }
2241 /* Set return code */
2242 ((struct lttcomm_lttng_msg *) (cmd_ctx->reply_payload.buffer.data))->ret_code = ret;
2243 setup_error:
2244 if (cmd_ctx->session) {
2245 session_unlock(cmd_ctx->session);
2246 session_put(cmd_ctx->session);
2247 cmd_ctx->session = NULL;
2248 }
2249 if (need_tracing_session) {
2250 session_unlock_list();
2251 }
2252 init_setup_error:
2253 assert(!rcu_read_ongoing());
2254 return ret;
2255 }
2256
2257 static int create_client_sock(void)
2258 {
2259 int ret, client_sock;
2260 const mode_t old_umask = umask(0);
2261
2262 /* Create client tool unix socket */
2263 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
2264 if (client_sock < 0) {
2265 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
2266 ret = -1;
2267 goto end;
2268 }
2269
2270 /* Set the cloexec flag */
2271 ret = utils_set_fd_cloexec(client_sock);
2272 if (ret < 0) {
2273 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2274 "Continuing but note that the consumer daemon will have a "
2275 "reference to this socket on exec()", client_sock);
2276 }
2277
2278 /* File permission MUST be 660 */
2279 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2280 if (ret < 0) {
2281 ERR("Set file permissions failed: %s",
2282 config.client_unix_sock_path.value);
2283 PERROR("chmod");
2284 (void) lttcomm_close_unix_sock(client_sock);
2285 ret = -1;
2286 goto end;
2287 }
2288 DBG("Created client socket (fd = %i)", client_sock);
2289 ret = client_sock;
2290 end:
2291 umask(old_umask);
2292 return ret;
2293 }
2294
2295 static void cleanup_client_thread(void *data)
2296 {
2297 struct lttng_pipe *quit_pipe = data;
2298
2299 lttng_pipe_destroy(quit_pipe);
2300 }
2301
2302 static void thread_init_cleanup(void *data)
2303 {
2304 set_thread_status(false);
2305 }
2306
2307 /*
2308 * This thread manage all clients request using the unix client socket for
2309 * communication.
2310 */
2311 static void *thread_manage_clients(void *data)
2312 {
2313 int sock = -1, ret, i, pollfd, err = -1;
2314 int sock_error;
2315 uint32_t revents, nb_fd;
2316 struct lttng_poll_event events;
2317 const int client_sock = thread_state.client_sock;
2318 struct lttng_pipe *quit_pipe = data;
2319 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
2320 struct command_ctx cmd_ctx = {};
2321
2322 DBG("[thread] Manage client started");
2323
2324 lttng_payload_init(&cmd_ctx.reply_payload);
2325
2326 is_root = (getuid() == 0);
2327
2328 pthread_cleanup_push(thread_init_cleanup, NULL);
2329
2330 rcu_register_thread();
2331
2332 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
2333
2334 health_code_update();
2335
2336 ret = lttcomm_listen_unix_sock(client_sock);
2337 if (ret < 0) {
2338 goto error_listen;
2339 }
2340
2341 /*
2342 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2343 * more will be added to this poll set.
2344 */
2345 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2346 if (ret < 0) {
2347 goto error_create_poll;
2348 }
2349
2350 /* Add the application registration socket */
2351 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
2352 if (ret < 0) {
2353 goto error;
2354 }
2355
2356 /* Add thread quit pipe */
2357 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
2358 if (ret < 0) {
2359 goto error;
2360 }
2361
2362 /* Set state as running. */
2363 set_thread_status(true);
2364 pthread_cleanup_pop(0);
2365
2366 /* This testpoint is after we signal readiness to the parent. */
2367 if (testpoint(sessiond_thread_manage_clients)) {
2368 goto error;
2369 }
2370
2371 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
2372 goto error;
2373 }
2374
2375 health_code_update();
2376
2377 while (1) {
2378 const struct cmd_completion_handler *cmd_completion_handler;
2379
2380 cmd_ctx.creds = (lttng_sock_cred) {
2381 .uid = UINT32_MAX,
2382 .gid = UINT32_MAX,
2383 };
2384 cmd_ctx.session = NULL;
2385 lttng_payload_clear(&cmd_ctx.reply_payload);
2386 cmd_ctx.lttng_msg_size = 0;
2387
2388 DBG("Accepting client command ...");
2389
2390 /* Inifinite blocking call, waiting for transmission */
2391 restart:
2392 health_poll_entry();
2393 ret = lttng_poll_wait(&events, -1);
2394 health_poll_exit();
2395 if (ret < 0) {
2396 /*
2397 * Restart interrupted system call.
2398 */
2399 if (errno == EINTR) {
2400 goto restart;
2401 }
2402 goto error;
2403 }
2404
2405 nb_fd = ret;
2406
2407 for (i = 0; i < nb_fd; i++) {
2408 revents = LTTNG_POLL_GETEV(&events, i);
2409 pollfd = LTTNG_POLL_GETFD(&events, i);
2410
2411 health_code_update();
2412
2413 if (pollfd == thread_quit_pipe_fd) {
2414 err = 0;
2415 goto exit;
2416 } else {
2417 /* Event on the registration socket */
2418 if (revents & LPOLLIN) {
2419 continue;
2420 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2421 ERR("Client socket poll error");
2422 goto error;
2423 } else {
2424 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2425 goto error;
2426 }
2427 }
2428 }
2429
2430 DBG("Wait for client response");
2431
2432 health_code_update();
2433
2434 sock = lttcomm_accept_unix_sock(client_sock);
2435 if (sock < 0) {
2436 goto error;
2437 }
2438
2439 /*
2440 * Set the CLOEXEC flag. Return code is useless because either way, the
2441 * show must go on.
2442 */
2443 (void) utils_set_fd_cloexec(sock);
2444
2445 /* Set socket option for credentials retrieval */
2446 ret = lttcomm_setsockopt_creds_unix_sock(sock);
2447 if (ret < 0) {
2448 goto error;
2449 }
2450
2451 health_code_update();
2452
2453 /*
2454 * Data is received from the lttng client. The struct
2455 * lttcomm_session_msg (lsm) contains the command and data request of
2456 * the client.
2457 */
2458 DBG("Receiving data from client ...");
2459 ret = lttcomm_recv_creds_unix_sock(sock, &cmd_ctx.lsm,
2460 sizeof(struct lttcomm_session_msg), &cmd_ctx.creds);
2461 if (ret != sizeof(struct lttcomm_session_msg)) {
2462 DBG("Incomplete recv() from client... continuing");
2463 ret = close(sock);
2464 if (ret) {
2465 PERROR("close");
2466 }
2467 sock = -1;
2468 continue;
2469 }
2470
2471 health_code_update();
2472
2473 // TODO: Validate cmd_ctx including sanity check for
2474 // security purpose.
2475
2476 rcu_thread_online();
2477 /*
2478 * This function dispatch the work to the kernel or userspace tracer
2479 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2480 * informations for the client. The command context struct contains
2481 * everything this function may needs.
2482 */
2483 ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
2484 rcu_thread_offline();
2485 if (ret < 0) {
2486 if (sock >= 0) {
2487 ret = close(sock);
2488 if (ret) {
2489 PERROR("close");
2490 }
2491 }
2492 sock = -1;
2493 /*
2494 * TODO: Inform client somehow of the fatal error. At
2495 * this point, ret < 0 means that a zmalloc failed
2496 * (ENOMEM). Error detected but still accept
2497 * command, unless a socket error has been
2498 * detected.
2499 */
2500 continue;
2501 }
2502
2503 cmd_completion_handler = cmd_pop_completion_handler();
2504 if (cmd_completion_handler) {
2505 enum lttng_error_code completion_code;
2506
2507 completion_code = cmd_completion_handler->run(
2508 cmd_completion_handler->data);
2509 if (completion_code != LTTNG_OK) {
2510 continue;
2511 }
2512 }
2513
2514 health_code_update();
2515
2516 if (sock >= 0) {
2517 struct lttng_payload_view view =
2518 lttng_payload_view_from_payload(
2519 &cmd_ctx.reply_payload,
2520 0, -1);
2521 struct lttcomm_lttng_msg *llm = (typeof(
2522 llm)) cmd_ctx.reply_payload.buffer.data;
2523
2524 assert(cmd_ctx.reply_payload.buffer.size >=
2525 sizeof(llm));
2526 assert(cmd_ctx.lttng_msg_size == cmd_ctx.reply_payload.buffer.size);
2527
2528 llm->fd_count = lttng_payload_view_get_fd_handle_count(&view);
2529
2530 DBG("Sending response (size: %d, retcode: %s (%d))",
2531 cmd_ctx.lttng_msg_size,
2532 lttng_strerror(-llm->ret_code),
2533 llm->ret_code);
2534 ret = send_unix_sock(sock, &view);
2535 if (ret < 0) {
2536 ERR("Failed to send data back to client");
2537 }
2538
2539 /* End of transmission */
2540 ret = close(sock);
2541 if (ret) {
2542 PERROR("close");
2543 }
2544 }
2545 sock = -1;
2546
2547 health_code_update();
2548 }
2549
2550 exit:
2551 error:
2552 if (sock >= 0) {
2553 ret = close(sock);
2554 if (ret) {
2555 PERROR("close");
2556 }
2557 }
2558
2559 lttng_poll_clean(&events);
2560
2561 error_listen:
2562 error_create_poll:
2563 unlink(config.client_unix_sock_path.value);
2564 ret = close(client_sock);
2565 if (ret) {
2566 PERROR("close");
2567 }
2568
2569 if (err) {
2570 health_error();
2571 ERR("Health error occurred in %s", __func__);
2572 }
2573
2574 health_unregister(health_sessiond);
2575
2576 DBG("Client thread dying");
2577 lttng_payload_reset(&cmd_ctx.reply_payload);
2578 rcu_unregister_thread();
2579 return NULL;
2580 }
2581
2582 static
2583 bool shutdown_client_thread(void *thread_data)
2584 {
2585 struct lttng_pipe *client_quit_pipe = thread_data;
2586 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2587
2588 return notify_thread_pipe(write_fd) == 1;
2589 }
2590
2591 struct lttng_thread *launch_client_thread(void)
2592 {
2593 bool thread_running;
2594 struct lttng_pipe *client_quit_pipe;
2595 struct lttng_thread *thread = NULL;
2596 int client_sock_fd = -1;
2597
2598 sem_init(&thread_state.ready, 0, 0);
2599 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2600 if (!client_quit_pipe) {
2601 goto error;
2602 }
2603
2604 client_sock_fd = create_client_sock();
2605 if (client_sock_fd < 0) {
2606 goto error;
2607 }
2608
2609 thread_state.client_sock = client_sock_fd;
2610 thread = lttng_thread_create("Client management",
2611 thread_manage_clients,
2612 shutdown_client_thread,
2613 cleanup_client_thread,
2614 client_quit_pipe);
2615 if (!thread) {
2616 goto error;
2617 }
2618 /* The client thread now owns the client sock fd and the quit pipe. */
2619 client_sock_fd = -1;
2620 client_quit_pipe = NULL;
2621
2622 /*
2623 * This thread is part of the threads that need to be fully
2624 * initialized before the session daemon is marked as "ready".
2625 */
2626 thread_running = wait_thread_status();
2627 if (!thread_running) {
2628 goto error;
2629 }
2630 return thread;
2631 error:
2632 if (client_sock_fd >= 0) {
2633 if (close(client_sock_fd)) {
2634 PERROR("Failed to close client socket");
2635 }
2636 }
2637 lttng_thread_put(thread);
2638 cleanup_client_thread(client_quit_pipe);
2639 return NULL;
2640 }
This page took 0.124261 seconds and 4 git commands to generate.