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