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