payload: use fd_handle instead of raw file descriptors
[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 int i;
739 struct lttng_dynamic_array raw_fds;
740
741 /*
742 * Never holds ownership of the FDs; this is just used
743 * to put the FDs in a contiguous array.
744 */
745 lttng_dynamic_array_init(&raw_fds, sizeof(int), NULL);
746
747 for (i = 0; i < fd_count; i++) {
748 struct fd_handle *handle =
749 lttng_payload_view_pop_fd_handle(view);
750 const int raw_fd = fd_handle_get_fd(handle);
751
752 ret = lttng_dynamic_array_add_element(&raw_fds, &raw_fd);
753 fd_handle_put(handle);
754 if (ret) {
755 lttng_dynamic_array_reset(&raw_fds);
756 goto end;
757 }
758 }
759
760 ret = lttcomm_send_fds_unix_sock(sock,
761 (const int *) raw_fds.buffer.data,
762 fd_count);
763 lttng_dynamic_array_reset(&raw_fds);
764 }
765
766 end:
767 return ret;
768 }
769
770 /*
771 * Process the command requested by the lttng client within the command
772 * context structure. This function make sure that the return structure (llm)
773 * is set and ready for transmission before returning.
774 *
775 * Return any error encountered or 0 for success.
776 *
777 * "sock" is only used for special-case var. len data.
778 * A command may assume the ownership of the socket, in which case its value
779 * should be set to -1.
780 *
781 * Should *NOT* be called with RCU read-side lock held.
782 */
783 static int process_client_msg(struct command_ctx *cmd_ctx, int *sock,
784 int *sock_error)
785 {
786 int ret = LTTNG_OK;
787 int need_tracing_session = 1;
788 int need_domain;
789
790 DBG("Processing client command %d", cmd_ctx->lsm.cmd_type);
791
792 assert(!rcu_read_ongoing());
793
794 *sock_error = 0;
795
796 switch (cmd_ctx->lsm.cmd_type) {
797 case LTTNG_CREATE_SESSION_EXT:
798 case LTTNG_DESTROY_SESSION:
799 case LTTNG_LIST_SESSIONS:
800 case LTTNG_LIST_DOMAINS:
801 case LTTNG_START_TRACE:
802 case LTTNG_STOP_TRACE:
803 case LTTNG_DATA_PENDING:
804 case LTTNG_SNAPSHOT_ADD_OUTPUT:
805 case LTTNG_SNAPSHOT_DEL_OUTPUT:
806 case LTTNG_SNAPSHOT_LIST_OUTPUT:
807 case LTTNG_SNAPSHOT_RECORD:
808 case LTTNG_SAVE_SESSION:
809 case LTTNG_SET_SESSION_SHM_PATH:
810 case LTTNG_REGENERATE_METADATA:
811 case LTTNG_REGENERATE_STATEDUMP:
812 case LTTNG_REGISTER_TRIGGER:
813 case LTTNG_UNREGISTER_TRIGGER:
814 case LTTNG_ROTATE_SESSION:
815 case LTTNG_ROTATION_GET_INFO:
816 case LTTNG_ROTATION_SET_SCHEDULE:
817 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
818 case LTTNG_CLEAR_SESSION:
819 need_domain = 0;
820 break;
821 default:
822 need_domain = 1;
823 }
824
825 if (config.no_kernel && need_domain
826 && cmd_ctx->lsm.domain.type == LTTNG_DOMAIN_KERNEL) {
827 if (!is_root) {
828 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
829 } else {
830 ret = LTTNG_ERR_KERN_NA;
831 }
832 goto error;
833 }
834
835 /* Deny register consumer if we already have a spawned consumer. */
836 if (cmd_ctx->lsm.cmd_type == LTTNG_REGISTER_CONSUMER) {
837 pthread_mutex_lock(&kconsumer_data.pid_mutex);
838 if (kconsumer_data.pid > 0) {
839 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
840 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
841 goto error;
842 }
843 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
844 }
845
846 /*
847 * Check for command that don't needs to allocate a returned payload. We do
848 * this here so we don't have to make the call for no payload at each
849 * command.
850 */
851 switch(cmd_ctx->lsm.cmd_type) {
852 case LTTNG_LIST_SESSIONS:
853 case LTTNG_LIST_TRACEPOINTS:
854 case LTTNG_LIST_TRACEPOINT_FIELDS:
855 case LTTNG_LIST_DOMAINS:
856 case LTTNG_LIST_CHANNELS:
857 case LTTNG_LIST_EVENTS:
858 case LTTNG_LIST_SYSCALLS:
859 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
860 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
861 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
862 case LTTNG_DATA_PENDING:
863 case LTTNG_ROTATE_SESSION:
864 case LTTNG_ROTATION_GET_INFO:
865 break;
866 default:
867 /* Setup lttng message with no payload */
868 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
869 if (ret < 0) {
870 /* This label does not try to unlock the session */
871 goto init_setup_error;
872 }
873 }
874
875 /* Commands that DO NOT need a session. */
876 switch (cmd_ctx->lsm.cmd_type) {
877 case LTTNG_CREATE_SESSION_EXT:
878 case LTTNG_LIST_SESSIONS:
879 case LTTNG_LIST_TRACEPOINTS:
880 case LTTNG_LIST_SYSCALLS:
881 case LTTNG_LIST_TRACEPOINT_FIELDS:
882 case LTTNG_SAVE_SESSION:
883 case LTTNG_REGISTER_TRIGGER:
884 case LTTNG_UNREGISTER_TRIGGER:
885 need_tracing_session = 0;
886 break;
887 default:
888 DBG("Getting session %s by name", cmd_ctx->lsm.session.name);
889 /*
890 * We keep the session list lock across _all_ commands
891 * for now, because the per-session lock does not
892 * handle teardown properly.
893 */
894 session_lock_list();
895 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm.session.name);
896 if (cmd_ctx->session == NULL) {
897 ret = LTTNG_ERR_SESS_NOT_FOUND;
898 goto error;
899 } else {
900 /* Acquire lock for the session */
901 session_lock(cmd_ctx->session);
902 }
903 break;
904 }
905
906 /*
907 * Commands that need a valid session but should NOT create one if none
908 * exists. Instead of creating one and destroying it when the command is
909 * handled, process that right before so we save some round trip in useless
910 * code path.
911 */
912 switch (cmd_ctx->lsm.cmd_type) {
913 case LTTNG_DISABLE_CHANNEL:
914 case LTTNG_DISABLE_EVENT:
915 switch (cmd_ctx->lsm.domain.type) {
916 case LTTNG_DOMAIN_KERNEL:
917 if (!cmd_ctx->session->kernel_session) {
918 ret = LTTNG_ERR_NO_CHANNEL;
919 goto error;
920 }
921 break;
922 case LTTNG_DOMAIN_JUL:
923 case LTTNG_DOMAIN_LOG4J:
924 case LTTNG_DOMAIN_PYTHON:
925 case LTTNG_DOMAIN_UST:
926 if (!cmd_ctx->session->ust_session) {
927 ret = LTTNG_ERR_NO_CHANNEL;
928 goto error;
929 }
930 break;
931 default:
932 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
933 goto error;
934 }
935 default:
936 break;
937 }
938
939 if (!need_domain) {
940 goto skip_domain;
941 }
942
943 /*
944 * Check domain type for specific "pre-action".
945 */
946 switch (cmd_ctx->lsm.domain.type) {
947 case LTTNG_DOMAIN_KERNEL:
948 if (!is_root) {
949 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
950 goto error;
951 }
952
953 /* Kernel tracer check */
954 if (!kernel_tracer_is_initialized()) {
955 /* Basically, load kernel tracer modules */
956 ret = init_kernel_tracer();
957 if (ret != 0) {
958 goto error;
959 }
960 }
961
962 /* Consumer is in an ERROR state. Report back to client */
963 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
964 ret = LTTNG_ERR_NO_KERNCONSUMERD;
965 goto error;
966 }
967
968 /* Need a session for kernel command */
969 if (need_tracing_session) {
970 if (cmd_ctx->session->kernel_session == NULL) {
971 ret = create_kernel_session(cmd_ctx->session);
972 if (ret != LTTNG_OK) {
973 ret = LTTNG_ERR_KERN_SESS_FAIL;
974 goto error;
975 }
976 }
977
978 /* Start the kernel consumer daemon */
979 pthread_mutex_lock(&kconsumer_data.pid_mutex);
980 if (kconsumer_data.pid == 0 &&
981 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
982 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
983 ret = start_consumerd(&kconsumer_data);
984 if (ret < 0) {
985 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
986 goto error;
987 }
988 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
989 } else {
990 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
991 }
992
993 /*
994 * The consumer was just spawned so we need to add the socket to
995 * the consumer output of the session if exist.
996 */
997 ret = consumer_create_socket(&kconsumer_data,
998 cmd_ctx->session->kernel_session->consumer);
999 if (ret < 0) {
1000 goto error;
1001 }
1002 }
1003
1004 break;
1005 case LTTNG_DOMAIN_JUL:
1006 case LTTNG_DOMAIN_LOG4J:
1007 case LTTNG_DOMAIN_PYTHON:
1008 case LTTNG_DOMAIN_UST:
1009 {
1010 if (!ust_app_supported()) {
1011 ret = LTTNG_ERR_NO_UST;
1012 goto error;
1013 }
1014 /* Consumer is in an ERROR state. Report back to client */
1015 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
1016 ret = LTTNG_ERR_NO_USTCONSUMERD;
1017 goto error;
1018 }
1019
1020 if (need_tracing_session) {
1021 /* Create UST session if none exist. */
1022 if (cmd_ctx->session->ust_session == NULL) {
1023 ret = create_ust_session(cmd_ctx->session,
1024 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain));
1025 if (ret != LTTNG_OK) {
1026 goto error;
1027 }
1028 }
1029
1030 /* Start the UST consumer daemons */
1031 /* 64-bit */
1032 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
1033 if (config.consumerd64_bin_path.value &&
1034 ustconsumer64_data.pid == 0 &&
1035 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1036 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1037 ret = start_consumerd(&ustconsumer64_data);
1038 if (ret < 0) {
1039 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
1040 uatomic_set(&ust_consumerd64_fd, -EINVAL);
1041 goto error;
1042 }
1043
1044 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
1045 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1046 } else {
1047 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1048 }
1049
1050 /*
1051 * Setup socket for consumer 64 bit. No need for atomic access
1052 * since it was set above and can ONLY be set in this thread.
1053 */
1054 ret = consumer_create_socket(&ustconsumer64_data,
1055 cmd_ctx->session->ust_session->consumer);
1056 if (ret < 0) {
1057 goto error;
1058 }
1059
1060 /* 32-bit */
1061 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
1062 if (config.consumerd32_bin_path.value &&
1063 ustconsumer32_data.pid == 0 &&
1064 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1065 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1066 ret = start_consumerd(&ustconsumer32_data);
1067 if (ret < 0) {
1068 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
1069 uatomic_set(&ust_consumerd32_fd, -EINVAL);
1070 goto error;
1071 }
1072
1073 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
1074 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1075 } else {
1076 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1077 }
1078
1079 /*
1080 * Setup socket for consumer 32 bit. No need for atomic access
1081 * since it was set above and can ONLY be set in this thread.
1082 */
1083 ret = consumer_create_socket(&ustconsumer32_data,
1084 cmd_ctx->session->ust_session->consumer);
1085 if (ret < 0) {
1086 goto error;
1087 }
1088 }
1089 break;
1090 }
1091 default:
1092 break;
1093 }
1094 skip_domain:
1095
1096 /* Validate consumer daemon state when start/stop trace command */
1097 if (cmd_ctx->lsm.cmd_type == LTTNG_START_TRACE ||
1098 cmd_ctx->lsm.cmd_type == LTTNG_STOP_TRACE) {
1099 switch (cmd_ctx->lsm.domain.type) {
1100 case LTTNG_DOMAIN_NONE:
1101 break;
1102 case LTTNG_DOMAIN_JUL:
1103 case LTTNG_DOMAIN_LOG4J:
1104 case LTTNG_DOMAIN_PYTHON:
1105 case LTTNG_DOMAIN_UST:
1106 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
1107 ret = LTTNG_ERR_NO_USTCONSUMERD;
1108 goto error;
1109 }
1110 break;
1111 case LTTNG_DOMAIN_KERNEL:
1112 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
1113 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1114 goto error;
1115 }
1116 break;
1117 default:
1118 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1119 goto error;
1120 }
1121 }
1122
1123 /*
1124 * Check that the UID or GID match that of the tracing session.
1125 * The root user can interact with all sessions.
1126 */
1127 if (need_tracing_session) {
1128 if (!session_access_ok(cmd_ctx->session,
1129 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1130 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) ||
1131 cmd_ctx->session->destroyed) {
1132 ret = LTTNG_ERR_EPERM;
1133 goto error;
1134 }
1135 }
1136
1137 /*
1138 * Send relayd information to consumer as soon as we have a domain and a
1139 * session defined.
1140 */
1141 if (cmd_ctx->session && need_domain) {
1142 /*
1143 * Setup relayd if not done yet. If the relayd information was already
1144 * sent to the consumer, this call will gracefully return.
1145 */
1146 ret = cmd_setup_relayd(cmd_ctx->session);
1147 if (ret != LTTNG_OK) {
1148 goto error;
1149 }
1150 }
1151
1152 /* Process by command type */
1153 switch (cmd_ctx->lsm.cmd_type) {
1154 case LTTNG_ADD_CONTEXT:
1155 {
1156 /*
1157 * An LTTNG_ADD_CONTEXT command might have a supplementary
1158 * payload if the context being added is an application context.
1159 */
1160 if (cmd_ctx->lsm.u.context.ctx.ctx ==
1161 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1162 char *provider_name = NULL, *context_name = NULL;
1163 size_t provider_name_len =
1164 cmd_ctx->lsm.u.context.provider_name_len;
1165 size_t context_name_len =
1166 cmd_ctx->lsm.u.context.context_name_len;
1167
1168 if (provider_name_len == 0 || context_name_len == 0) {
1169 /*
1170 * Application provider and context names MUST
1171 * be provided.
1172 */
1173 ret = -LTTNG_ERR_INVALID;
1174 goto error;
1175 }
1176
1177 provider_name = zmalloc(provider_name_len + 1);
1178 if (!provider_name) {
1179 ret = -LTTNG_ERR_NOMEM;
1180 goto error;
1181 }
1182 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name =
1183 provider_name;
1184
1185 context_name = zmalloc(context_name_len + 1);
1186 if (!context_name) {
1187 ret = -LTTNG_ERR_NOMEM;
1188 goto error_add_context;
1189 }
1190 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name =
1191 context_name;
1192
1193 ret = lttcomm_recv_unix_sock(*sock, provider_name,
1194 provider_name_len);
1195 if (ret < 0) {
1196 goto error_add_context;
1197 }
1198
1199 ret = lttcomm_recv_unix_sock(*sock, context_name,
1200 context_name_len);
1201 if (ret < 0) {
1202 goto error_add_context;
1203 }
1204 }
1205
1206 /*
1207 * cmd_add_context assumes ownership of the provider and context
1208 * names.
1209 */
1210 ret = cmd_add_context(cmd_ctx->session,
1211 cmd_ctx->lsm.domain.type,
1212 cmd_ctx->lsm.u.context.channel_name,
1213 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.context.ctx),
1214 kernel_poll_pipe[1]);
1215
1216 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
1217 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
1218 error_add_context:
1219 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name);
1220 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name);
1221 if (ret < 0) {
1222 goto error;
1223 }
1224 break;
1225 }
1226 case LTTNG_DISABLE_CHANNEL:
1227 {
1228 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1229 cmd_ctx->lsm.u.disable.channel_name);
1230 break;
1231 }
1232 case LTTNG_DISABLE_EVENT:
1233 {
1234
1235 /*
1236 * FIXME: handle filter; for now we just receive the filter's
1237 * bytecode along with the filter expression which are sent by
1238 * liblttng-ctl and discard them.
1239 *
1240 * This fixes an issue where the client may block while sending
1241 * the filter payload and encounter an error because the session
1242 * daemon closes the socket without ever handling this data.
1243 */
1244 size_t count = cmd_ctx->lsm.u.disable.expression_len +
1245 cmd_ctx->lsm.u.disable.bytecode_len;
1246
1247 if (count) {
1248 char data[LTTNG_FILTER_MAX_LEN];
1249
1250 DBG("Discarding disable event command payload of size %zu", count);
1251 while (count) {
1252 ret = lttcomm_recv_unix_sock(*sock, data,
1253 count > sizeof(data) ? sizeof(data) : count);
1254 if (ret < 0) {
1255 goto error;
1256 }
1257
1258 count -= (size_t) ret;
1259 }
1260 }
1261 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1262 cmd_ctx->lsm.u.disable.channel_name,
1263 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.disable.event));
1264 break;
1265 }
1266 case LTTNG_ENABLE_CHANNEL:
1267 {
1268 cmd_ctx->lsm.u.channel.chan.attr.extended.ptr =
1269 (struct lttng_channel_extended *) &cmd_ctx->lsm.u.channel.extended;
1270 ret = cmd_enable_channel(cmd_ctx->session,
1271 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1272 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.channel.chan),
1273 kernel_poll_pipe[1]);
1274 break;
1275 }
1276 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
1277 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE:
1278 {
1279 struct lttng_dynamic_buffer payload;
1280 struct lttng_buffer_view payload_view;
1281 const bool add_value =
1282 cmd_ctx->lsm.cmd_type ==
1283 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE;
1284 const size_t name_len =
1285 cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
1286 .name_len;
1287 const enum lttng_domain_type domain_type =
1288 (enum lttng_domain_type)
1289 cmd_ctx->lsm.domain.type;
1290 const enum lttng_process_attr process_attr =
1291 (enum lttng_process_attr) cmd_ctx->lsm.u
1292 .process_attr_tracker_add_remove_include_value
1293 .process_attr;
1294 const enum lttng_process_attr_value_type value_type =
1295 (enum lttng_process_attr_value_type) cmd_ctx
1296 ->lsm.u
1297 .process_attr_tracker_add_remove_include_value
1298 .value_type;
1299 struct process_attr_value *value;
1300 enum lttng_error_code ret_code;
1301
1302 /* Receive remaining variable length payload if applicable. */
1303 if (name_len > LOGIN_NAME_MAX) {
1304 /*
1305 * POSIX mandates user and group names that are at least
1306 * 8 characters long. Note that although shadow-utils
1307 * (useradd, groupaadd, etc.) use 32 chars as their
1308 * limit (from bits/utmp.h, UT_NAMESIZE),
1309 * LOGIN_NAME_MAX is defined to 256.
1310 */
1311 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1312 add_value ? "addition" : "removal",
1313 name_len, LOGIN_NAME_MAX);
1314 ret = LTTNG_ERR_INVALID;
1315 goto error;
1316 }
1317
1318 lttng_dynamic_buffer_init(&payload);
1319 if (name_len != 0) {
1320 /*
1321 * Receive variable payload for user/group name
1322 * arguments.
1323 */
1324 ret = lttng_dynamic_buffer_set_size(&payload, name_len);
1325 if (ret) {
1326 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1327 add_value ? "add" : "remove");
1328 ret = LTTNG_ERR_NOMEM;
1329 goto error_add_remove_tracker_value;
1330 }
1331
1332 ret = lttcomm_recv_unix_sock(
1333 *sock, payload.data, name_len);
1334 if (ret <= 0) {
1335 ERR("Failed to receive payload of %s process attribute tracker value argument",
1336 add_value ? "add" : "remove");
1337 *sock_error = 1;
1338 ret = LTTNG_ERR_INVALID_PROTOCOL;
1339 goto error_add_remove_tracker_value;
1340 }
1341 }
1342
1343 payload_view = lttng_buffer_view_from_dynamic_buffer(
1344 &payload, 0, name_len);
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 ret = cmd_register_trigger(cmd_ctx, *sock,
2045 notification_thread_handle);
2046 break;
2047 }
2048 case LTTNG_UNREGISTER_TRIGGER:
2049 {
2050 ret = cmd_unregister_trigger(cmd_ctx, *sock,
2051 notification_thread_handle);
2052 break;
2053 }
2054 case LTTNG_ROTATE_SESSION:
2055 {
2056 struct lttng_rotate_session_return rotate_return;
2057
2058 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
2059
2060 memset(&rotate_return, 0, sizeof(rotate_return));
2061 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2062 DBG("Kernel tracer version is not compatible with the rotation feature");
2063 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2064 goto error;
2065 }
2066
2067 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return,
2068 false,
2069 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
2070 if (ret < 0) {
2071 ret = -ret;
2072 goto error;
2073 }
2074
2075 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
2076 sizeof(rotate_return));
2077 if (ret < 0) {
2078 ret = -ret;
2079 goto error;
2080 }
2081
2082 ret = LTTNG_OK;
2083 break;
2084 }
2085 case LTTNG_ROTATION_GET_INFO:
2086 {
2087 struct lttng_rotation_get_info_return get_info_return;
2088
2089 memset(&get_info_return, 0, sizeof(get_info_return));
2090 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
2091 cmd_ctx->lsm.u.get_rotation_info.rotation_id);
2092 if (ret < 0) {
2093 ret = -ret;
2094 goto error;
2095 }
2096
2097 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
2098 sizeof(get_info_return));
2099 if (ret < 0) {
2100 ret = -ret;
2101 goto error;
2102 }
2103
2104 ret = LTTNG_OK;
2105 break;
2106 }
2107 case LTTNG_ROTATION_SET_SCHEDULE:
2108 {
2109 bool set_schedule;
2110 enum lttng_rotation_schedule_type schedule_type;
2111 uint64_t value;
2112
2113 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2114 DBG("Kernel tracer version does not support session rotations");
2115 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2116 goto error;
2117 }
2118
2119 set_schedule = cmd_ctx->lsm.u.rotation_set_schedule.set == 1;
2120 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm.u.rotation_set_schedule.type;
2121 value = cmd_ctx->lsm.u.rotation_set_schedule.value;
2122
2123 ret = cmd_rotation_set_schedule(cmd_ctx->session,
2124 set_schedule,
2125 schedule_type,
2126 value,
2127 notification_thread_handle);
2128 if (ret != LTTNG_OK) {
2129 goto error;
2130 }
2131
2132 break;
2133 }
2134 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2135 {
2136 struct lttng_session_list_schedules_return schedules = {
2137 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
2138 .periodic.value = cmd_ctx->session->rotate_timer_period,
2139 .size.set = !!cmd_ctx->session->rotate_size,
2140 .size.value = cmd_ctx->session->rotate_size,
2141 };
2142
2143 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
2144 sizeof(schedules));
2145 if (ret < 0) {
2146 ret = -ret;
2147 goto error;
2148 }
2149
2150 ret = LTTNG_OK;
2151 break;
2152 }
2153 case LTTNG_CLEAR_SESSION:
2154 {
2155 ret = cmd_clear_session(cmd_ctx->session, sock);
2156 break;
2157 }
2158 default:
2159 ret = LTTNG_ERR_UND;
2160 break;
2161 }
2162
2163 error:
2164 if (cmd_ctx->reply_payload.buffer.size == 0) {
2165 DBG("Missing llm header, creating one.");
2166 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
2167 goto setup_error;
2168 }
2169 }
2170 /* Set return code */
2171 ((struct lttcomm_lttng_msg *) (cmd_ctx->reply_payload.buffer.data))->ret_code = ret;
2172 setup_error:
2173 if (cmd_ctx->session) {
2174 session_unlock(cmd_ctx->session);
2175 session_put(cmd_ctx->session);
2176 cmd_ctx->session = NULL;
2177 }
2178 if (need_tracing_session) {
2179 session_unlock_list();
2180 }
2181 init_setup_error:
2182 assert(!rcu_read_ongoing());
2183 return ret;
2184 }
2185
2186 static int create_client_sock(void)
2187 {
2188 int ret, client_sock;
2189 const mode_t old_umask = umask(0);
2190
2191 /* Create client tool unix socket */
2192 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
2193 if (client_sock < 0) {
2194 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
2195 ret = -1;
2196 goto end;
2197 }
2198
2199 /* Set the cloexec flag */
2200 ret = utils_set_fd_cloexec(client_sock);
2201 if (ret < 0) {
2202 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2203 "Continuing but note that the consumer daemon will have a "
2204 "reference to this socket on exec()", client_sock);
2205 }
2206
2207 /* File permission MUST be 660 */
2208 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2209 if (ret < 0) {
2210 ERR("Set file permissions failed: %s",
2211 config.client_unix_sock_path.value);
2212 PERROR("chmod");
2213 (void) lttcomm_close_unix_sock(client_sock);
2214 ret = -1;
2215 goto end;
2216 }
2217 DBG("Created client socket (fd = %i)", client_sock);
2218 ret = client_sock;
2219 end:
2220 umask(old_umask);
2221 return ret;
2222 }
2223
2224 static void cleanup_client_thread(void *data)
2225 {
2226 struct lttng_pipe *quit_pipe = data;
2227
2228 lttng_pipe_destroy(quit_pipe);
2229 }
2230
2231 static void thread_init_cleanup(void *data)
2232 {
2233 set_thread_status(false);
2234 }
2235
2236 /*
2237 * This thread manage all clients request using the unix client socket for
2238 * communication.
2239 */
2240 static void *thread_manage_clients(void *data)
2241 {
2242 int sock = -1, ret, i, pollfd, err = -1;
2243 int sock_error;
2244 uint32_t revents, nb_fd;
2245 struct lttng_poll_event events;
2246 const int client_sock = thread_state.client_sock;
2247 struct lttng_pipe *quit_pipe = data;
2248 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
2249 struct command_ctx cmd_ctx = {};
2250
2251 DBG("[thread] Manage client started");
2252
2253 lttng_payload_init(&cmd_ctx.reply_payload);
2254
2255 is_root = (getuid() == 0);
2256
2257 pthread_cleanup_push(thread_init_cleanup, NULL);
2258
2259 rcu_register_thread();
2260
2261 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
2262
2263 health_code_update();
2264
2265 ret = lttcomm_listen_unix_sock(client_sock);
2266 if (ret < 0) {
2267 goto error_listen;
2268 }
2269
2270 /*
2271 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2272 * more will be added to this poll set.
2273 */
2274 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2275 if (ret < 0) {
2276 goto error_create_poll;
2277 }
2278
2279 /* Add the application registration socket */
2280 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
2281 if (ret < 0) {
2282 goto error;
2283 }
2284
2285 /* Add thread quit pipe */
2286 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
2287 if (ret < 0) {
2288 goto error;
2289 }
2290
2291 /* Set state as running. */
2292 set_thread_status(true);
2293 pthread_cleanup_pop(0);
2294
2295 /* This testpoint is after we signal readiness to the parent. */
2296 if (testpoint(sessiond_thread_manage_clients)) {
2297 goto error;
2298 }
2299
2300 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
2301 goto error;
2302 }
2303
2304 health_code_update();
2305
2306 while (1) {
2307 const struct cmd_completion_handler *cmd_completion_handler;
2308
2309 cmd_ctx.creds = (lttng_sock_cred) {
2310 .uid = UINT32_MAX,
2311 .gid = UINT32_MAX,
2312 };
2313 cmd_ctx.session = NULL;
2314 lttng_payload_clear(&cmd_ctx.reply_payload);
2315 cmd_ctx.lttng_msg_size = 0;
2316
2317 DBG("Accepting client command ...");
2318
2319 /* Inifinite blocking call, waiting for transmission */
2320 restart:
2321 health_poll_entry();
2322 ret = lttng_poll_wait(&events, -1);
2323 health_poll_exit();
2324 if (ret < 0) {
2325 /*
2326 * Restart interrupted system call.
2327 */
2328 if (errno == EINTR) {
2329 goto restart;
2330 }
2331 goto error;
2332 }
2333
2334 nb_fd = ret;
2335
2336 for (i = 0; i < nb_fd; i++) {
2337 revents = LTTNG_POLL_GETEV(&events, i);
2338 pollfd = LTTNG_POLL_GETFD(&events, i);
2339
2340 health_code_update();
2341
2342 if (pollfd == thread_quit_pipe_fd) {
2343 err = 0;
2344 goto exit;
2345 } else {
2346 /* Event on the registration socket */
2347 if (revents & LPOLLIN) {
2348 continue;
2349 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2350 ERR("Client socket poll error");
2351 goto error;
2352 } else {
2353 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2354 goto error;
2355 }
2356 }
2357 }
2358
2359 DBG("Wait for client response");
2360
2361 health_code_update();
2362
2363 sock = lttcomm_accept_unix_sock(client_sock);
2364 if (sock < 0) {
2365 goto error;
2366 }
2367
2368 /*
2369 * Set the CLOEXEC flag. Return code is useless because either way, the
2370 * show must go on.
2371 */
2372 (void) utils_set_fd_cloexec(sock);
2373
2374 /* Set socket option for credentials retrieval */
2375 ret = lttcomm_setsockopt_creds_unix_sock(sock);
2376 if (ret < 0) {
2377 goto error;
2378 }
2379
2380 health_code_update();
2381
2382 /*
2383 * Data is received from the lttng client. The struct
2384 * lttcomm_session_msg (lsm) contains the command and data request of
2385 * the client.
2386 */
2387 DBG("Receiving data from client ...");
2388 ret = lttcomm_recv_creds_unix_sock(sock, &cmd_ctx.lsm,
2389 sizeof(struct lttcomm_session_msg), &cmd_ctx.creds);
2390 if (ret != sizeof(struct lttcomm_session_msg)) {
2391 DBG("Incomplete recv() from client... continuing");
2392 ret = close(sock);
2393 if (ret) {
2394 PERROR("close");
2395 }
2396 sock = -1;
2397 continue;
2398 }
2399
2400 health_code_update();
2401
2402 // TODO: Validate cmd_ctx including sanity check for
2403 // security purpose.
2404
2405 rcu_thread_online();
2406 /*
2407 * This function dispatch the work to the kernel or userspace tracer
2408 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2409 * informations for the client. The command context struct contains
2410 * everything this function may needs.
2411 */
2412 ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
2413 rcu_thread_offline();
2414 if (ret < 0) {
2415 if (sock >= 0) {
2416 ret = close(sock);
2417 if (ret) {
2418 PERROR("close");
2419 }
2420 }
2421 sock = -1;
2422 /*
2423 * TODO: Inform client somehow of the fatal error. At
2424 * this point, ret < 0 means that a zmalloc failed
2425 * (ENOMEM). Error detected but still accept
2426 * command, unless a socket error has been
2427 * detected.
2428 */
2429 continue;
2430 }
2431
2432 cmd_completion_handler = cmd_pop_completion_handler();
2433 if (cmd_completion_handler) {
2434 enum lttng_error_code completion_code;
2435
2436 completion_code = cmd_completion_handler->run(
2437 cmd_completion_handler->data);
2438 if (completion_code != LTTNG_OK) {
2439 continue;
2440 }
2441 }
2442
2443 health_code_update();
2444
2445 if (sock >= 0) {
2446 struct lttng_payload_view view =
2447 lttng_payload_view_from_payload(
2448 &cmd_ctx.reply_payload,
2449 0, -1);
2450 struct lttcomm_lttng_msg *llm = (typeof(
2451 llm)) cmd_ctx.reply_payload.buffer.data;
2452
2453 assert(cmd_ctx.reply_payload.buffer.size >=
2454 sizeof(llm));
2455 assert(cmd_ctx.lttng_msg_size == cmd_ctx.reply_payload.buffer.size);
2456
2457 llm->fd_count = lttng_payload_view_get_fd_handle_count(&view);
2458
2459 DBG("Sending response (size: %d, retcode: %s (%d))",
2460 cmd_ctx.lttng_msg_size,
2461 lttng_strerror(-llm->ret_code),
2462 llm->ret_code);
2463 ret = send_unix_sock(sock, &view);
2464 if (ret < 0) {
2465 ERR("Failed to send data back to client");
2466 }
2467
2468 /* End of transmission */
2469 ret = close(sock);
2470 if (ret) {
2471 PERROR("close");
2472 }
2473 }
2474 sock = -1;
2475
2476 health_code_update();
2477 }
2478
2479 exit:
2480 error:
2481 if (sock >= 0) {
2482 ret = close(sock);
2483 if (ret) {
2484 PERROR("close");
2485 }
2486 }
2487
2488 lttng_poll_clean(&events);
2489
2490 error_listen:
2491 error_create_poll:
2492 unlink(config.client_unix_sock_path.value);
2493 ret = close(client_sock);
2494 if (ret) {
2495 PERROR("close");
2496 }
2497
2498 if (err) {
2499 health_error();
2500 ERR("Health error occurred in %s", __func__);
2501 }
2502
2503 health_unregister(health_sessiond);
2504
2505 DBG("Client thread dying");
2506 lttng_payload_reset(&cmd_ctx.reply_payload);
2507 rcu_unregister_thread();
2508 return NULL;
2509 }
2510
2511 static
2512 bool shutdown_client_thread(void *thread_data)
2513 {
2514 struct lttng_pipe *client_quit_pipe = thread_data;
2515 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2516
2517 return notify_thread_pipe(write_fd) == 1;
2518 }
2519
2520 struct lttng_thread *launch_client_thread(void)
2521 {
2522 bool thread_running;
2523 struct lttng_pipe *client_quit_pipe;
2524 struct lttng_thread *thread = NULL;
2525 int client_sock_fd = -1;
2526
2527 sem_init(&thread_state.ready, 0, 0);
2528 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2529 if (!client_quit_pipe) {
2530 goto error;
2531 }
2532
2533 client_sock_fd = create_client_sock();
2534 if (client_sock_fd < 0) {
2535 goto error;
2536 }
2537
2538 thread_state.client_sock = client_sock_fd;
2539 thread = lttng_thread_create("Client management",
2540 thread_manage_clients,
2541 shutdown_client_thread,
2542 cleanup_client_thread,
2543 client_quit_pipe);
2544 if (!thread) {
2545 goto error;
2546 }
2547 /* The client thread now owns the client sock fd and the quit pipe. */
2548 client_sock_fd = -1;
2549 client_quit_pipe = NULL;
2550
2551 /*
2552 * This thread is part of the threads that need to be fully
2553 * initialized before the session daemon is marked as "ready".
2554 */
2555 thread_running = wait_thread_status();
2556 if (!thread_running) {
2557 goto error;
2558 }
2559 return thread;
2560 error:
2561 if (client_sock_fd >= 0) {
2562 if (close(client_sock_fd)) {
2563 PERROR("Failed to close client socket");
2564 }
2565 }
2566 lttng_thread_put(thread);
2567 cleanup_client_thread(client_quit_pipe);
2568 return NULL;
2569 }
This page took 0.117569 seconds and 4 git commands to generate.