Fix: sessiond: leak of trigger on registration error
[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;
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 goto end;
759 }
760 }
761
762 *_trigger = trigger;
763 ret_code = LTTNG_OK;
764
765 end:
766 return ret_code;
767 }
768
769 /*
770 * Version of setup_lttng_msg() without command header.
771 */
772 static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
773 void *payload_buf, size_t payload_len)
774 {
775 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
776 }
777
778 /*
779 * Check if the current kernel tracer supports the session rotation feature.
780 * Return 1 if it does, 0 otherwise.
781 */
782 static int check_rotate_compatible(void)
783 {
784 int ret = 1;
785
786 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
787 DBG("Kernel tracer version is not compatible with the rotation feature");
788 ret = 0;
789 }
790
791 return ret;
792 }
793
794 /*
795 * Send data on a unix socket using the liblttsessiondcomm API.
796 *
797 * Return lttcomm error code.
798 */
799 static int send_unix_sock(int sock, struct lttng_payload_view *view)
800 {
801 int ret;
802 const int fd_count = lttng_payload_view_get_fd_handle_count(view);
803
804 /* Check valid length */
805 if (view->buffer.size == 0) {
806 ret = -1;
807 goto end;
808 }
809
810 ret = lttcomm_send_unix_sock(
811 sock, view->buffer.data, view->buffer.size);
812 if (ret < 0) {
813 goto end;
814 }
815
816 if (fd_count > 0) {
817 ret = lttcomm_send_payload_view_fds_unix_sock(sock, view);
818 if (ret < 0) {
819 goto end;
820 }
821 }
822
823 end:
824 return ret;
825 }
826
827 /*
828 * Process the command requested by the lttng client within the command
829 * context structure. This function make sure that the return structure (llm)
830 * is set and ready for transmission before returning.
831 *
832 * Return any error encountered or 0 for success.
833 *
834 * "sock" is only used for special-case var. len data.
835 * A command may assume the ownership of the socket, in which case its value
836 * should be set to -1.
837 *
838 * Should *NOT* be called with RCU read-side lock held.
839 */
840 static int process_client_msg(struct command_ctx *cmd_ctx, int *sock,
841 int *sock_error)
842 {
843 int ret = LTTNG_OK;
844 bool need_tracing_session = true;
845 bool need_domain;
846 bool need_consumerd;
847
848 DBG("Processing client command '%s\' (%d)",
849 lttcomm_sessiond_command_str(cmd_ctx->lsm.cmd_type),
850 cmd_ctx->lsm.cmd_type);
851
852 assert(!rcu_read_ongoing());
853
854 *sock_error = 0;
855
856 switch (cmd_ctx->lsm.cmd_type) {
857 case LTTNG_CREATE_SESSION_EXT:
858 case LTTNG_DESTROY_SESSION:
859 case LTTNG_LIST_SESSIONS:
860 case LTTNG_LIST_DOMAINS:
861 case LTTNG_START_TRACE:
862 case LTTNG_STOP_TRACE:
863 case LTTNG_DATA_PENDING:
864 case LTTNG_SNAPSHOT_ADD_OUTPUT:
865 case LTTNG_SNAPSHOT_DEL_OUTPUT:
866 case LTTNG_SNAPSHOT_LIST_OUTPUT:
867 case LTTNG_SNAPSHOT_RECORD:
868 case LTTNG_SAVE_SESSION:
869 case LTTNG_SET_SESSION_SHM_PATH:
870 case LTTNG_REGENERATE_METADATA:
871 case LTTNG_REGENERATE_STATEDUMP:
872 case LTTNG_ROTATE_SESSION:
873 case LTTNG_ROTATION_GET_INFO:
874 case LTTNG_ROTATION_SET_SCHEDULE:
875 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
876 case LTTNG_CLEAR_SESSION:
877 case LTTNG_LIST_TRIGGERS:
878 need_domain = false;
879 break;
880 default:
881 need_domain = true;
882 }
883
884 /* Needs a functioning consumerd? */
885 switch (cmd_ctx->lsm.cmd_type) {
886 case LTTNG_REGISTER_TRIGGER:
887 case LTTNG_UNREGISTER_TRIGGER:
888 need_consumerd = false;
889 break;
890 default:
891 need_consumerd = true;
892 break;
893 }
894
895 if (config.no_kernel && need_domain
896 && cmd_ctx->lsm.domain.type == LTTNG_DOMAIN_KERNEL) {
897 if (!is_root) {
898 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
899 } else {
900 ret = LTTNG_ERR_KERN_NA;
901 }
902 goto error;
903 }
904
905 /* Deny register consumer if we already have a spawned consumer. */
906 if (cmd_ctx->lsm.cmd_type == LTTNG_REGISTER_CONSUMER) {
907 pthread_mutex_lock(&kconsumer_data.pid_mutex);
908 if (kconsumer_data.pid > 0) {
909 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
910 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
911 goto error;
912 }
913 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
914 }
915
916 /*
917 * Check for command that don't needs to allocate a returned payload. We do
918 * this here so we don't have to make the call for no payload at each
919 * command.
920 */
921 switch(cmd_ctx->lsm.cmd_type) {
922 case LTTNG_LIST_SESSIONS:
923 case LTTNG_LIST_TRACEPOINTS:
924 case LTTNG_LIST_TRACEPOINT_FIELDS:
925 case LTTNG_LIST_DOMAINS:
926 case LTTNG_LIST_CHANNELS:
927 case LTTNG_LIST_EVENTS:
928 case LTTNG_LIST_SYSCALLS:
929 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
930 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
931 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
932 case LTTNG_DATA_PENDING:
933 case LTTNG_ROTATE_SESSION:
934 case LTTNG_ROTATION_GET_INFO:
935 case LTTNG_REGISTER_TRIGGER:
936 case LTTNG_LIST_TRIGGERS:
937 break;
938 default:
939 /* Setup lttng message with no payload */
940 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
941 if (ret < 0) {
942 /* This label does not try to unlock the session */
943 goto init_setup_error;
944 }
945 }
946
947 /* Commands that DO NOT need a session. */
948 switch (cmd_ctx->lsm.cmd_type) {
949 case LTTNG_CREATE_SESSION_EXT:
950 case LTTNG_LIST_SESSIONS:
951 case LTTNG_LIST_TRACEPOINTS:
952 case LTTNG_LIST_SYSCALLS:
953 case LTTNG_LIST_TRACEPOINT_FIELDS:
954 case LTTNG_SAVE_SESSION:
955 case LTTNG_REGISTER_TRIGGER:
956 case LTTNG_UNREGISTER_TRIGGER:
957 case LTTNG_LIST_TRIGGERS:
958 need_tracing_session = false;
959 break;
960 default:
961 DBG("Getting session %s by name", cmd_ctx->lsm.session.name);
962 /*
963 * We keep the session list lock across _all_ commands
964 * for now, because the per-session lock does not
965 * handle teardown properly.
966 */
967 session_lock_list();
968 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm.session.name);
969 if (cmd_ctx->session == NULL) {
970 ret = LTTNG_ERR_SESS_NOT_FOUND;
971 goto error;
972 } else {
973 /* Acquire lock for the session */
974 session_lock(cmd_ctx->session);
975 }
976 break;
977 }
978
979 /*
980 * Commands that need a valid session but should NOT create one if none
981 * exists. Instead of creating one and destroying it when the command is
982 * handled, process that right before so we save some round trip in useless
983 * code path.
984 */
985 switch (cmd_ctx->lsm.cmd_type) {
986 case LTTNG_DISABLE_CHANNEL:
987 case LTTNG_DISABLE_EVENT:
988 switch (cmd_ctx->lsm.domain.type) {
989 case LTTNG_DOMAIN_KERNEL:
990 if (!cmd_ctx->session->kernel_session) {
991 ret = LTTNG_ERR_NO_CHANNEL;
992 goto error;
993 }
994 break;
995 case LTTNG_DOMAIN_JUL:
996 case LTTNG_DOMAIN_LOG4J:
997 case LTTNG_DOMAIN_PYTHON:
998 case LTTNG_DOMAIN_UST:
999 if (!cmd_ctx->session->ust_session) {
1000 ret = LTTNG_ERR_NO_CHANNEL;
1001 goto error;
1002 }
1003 break;
1004 default:
1005 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1006 goto error;
1007 }
1008 default:
1009 break;
1010 }
1011
1012 if (!need_domain) {
1013 goto skip_domain;
1014 }
1015
1016 /*
1017 * Check domain type for specific "pre-action".
1018 */
1019 switch (cmd_ctx->lsm.domain.type) {
1020 case LTTNG_DOMAIN_KERNEL:
1021 if (!is_root) {
1022 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
1023 goto error;
1024 }
1025
1026 /* Kernel tracer check */
1027 if (!kernel_tracer_is_initialized()) {
1028 /* Basically, load kernel tracer modules */
1029 ret = init_kernel_tracer();
1030 if (ret != 0) {
1031 goto error;
1032 }
1033 }
1034
1035 /* Consumer is in an ERROR state. Report back to client */
1036 if (need_consumerd && uatomic_read(&kernel_consumerd_state) ==
1037 CONSUMER_ERROR) {
1038 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1039 goto error;
1040 }
1041
1042 /* Need a session for kernel command */
1043 if (need_tracing_session) {
1044 if (cmd_ctx->session->kernel_session == NULL) {
1045 ret = create_kernel_session(cmd_ctx->session);
1046 if (ret != LTTNG_OK) {
1047 ret = LTTNG_ERR_KERN_SESS_FAIL;
1048 goto error;
1049 }
1050 }
1051
1052 /* Start the kernel consumer daemon */
1053 pthread_mutex_lock(&kconsumer_data.pid_mutex);
1054 if (kconsumer_data.pid == 0 &&
1055 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1056 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
1057 ret = start_consumerd(&kconsumer_data);
1058 if (ret < 0) {
1059 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1060 goto error;
1061 }
1062 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
1063 } else {
1064 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
1065 }
1066
1067 /*
1068 * The consumer was just spawned so we need to add the socket to
1069 * the consumer output of the session if exist.
1070 */
1071 ret = consumer_create_socket(&kconsumer_data,
1072 cmd_ctx->session->kernel_session->consumer);
1073 if (ret < 0) {
1074 goto error;
1075 }
1076 }
1077
1078 break;
1079 case LTTNG_DOMAIN_JUL:
1080 case LTTNG_DOMAIN_LOG4J:
1081 case LTTNG_DOMAIN_PYTHON:
1082 if (!agent_tracing_is_enabled()) {
1083 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1084 goto error;
1085 }
1086 /* Fallthrough */
1087 case LTTNG_DOMAIN_UST:
1088 {
1089 if (!ust_app_supported()) {
1090 ret = LTTNG_ERR_NO_UST;
1091 goto error;
1092 }
1093
1094 /* Consumer is in an ERROR state. Report back to client */
1095 if (need_consumerd && uatomic_read(&ust_consumerd_state) ==
1096 CONSUMER_ERROR) {
1097 ret = LTTNG_ERR_NO_USTCONSUMERD;
1098 goto error;
1099 }
1100
1101 if (need_tracing_session) {
1102 /* Create UST session if none exist. */
1103 if (cmd_ctx->session->ust_session == NULL) {
1104 ret = create_ust_session(cmd_ctx->session,
1105 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain));
1106 if (ret != LTTNG_OK) {
1107 goto error;
1108 }
1109 }
1110
1111 /* Start the UST consumer daemons */
1112 /* 64-bit */
1113 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
1114 if (config.consumerd64_bin_path.value &&
1115 ustconsumer64_data.pid == 0 &&
1116 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1117 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1118 ret = start_consumerd(&ustconsumer64_data);
1119 if (ret < 0) {
1120 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
1121 uatomic_set(&ust_consumerd64_fd, -EINVAL);
1122 goto error;
1123 }
1124
1125 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
1126 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1127 } else {
1128 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
1129 }
1130
1131 /*
1132 * Setup socket for consumer 64 bit. No need for atomic access
1133 * since it was set above and can ONLY be set in this thread.
1134 */
1135 ret = consumer_create_socket(&ustconsumer64_data,
1136 cmd_ctx->session->ust_session->consumer);
1137 if (ret < 0) {
1138 goto error;
1139 }
1140
1141 /* 32-bit */
1142 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
1143 if (config.consumerd32_bin_path.value &&
1144 ustconsumer32_data.pid == 0 &&
1145 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
1146 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1147 ret = start_consumerd(&ustconsumer32_data);
1148 if (ret < 0) {
1149 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
1150 uatomic_set(&ust_consumerd32_fd, -EINVAL);
1151 goto error;
1152 }
1153
1154 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
1155 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
1156 } else {
1157 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
1158 }
1159
1160 /*
1161 * Setup socket for consumer 32 bit. No need for atomic access
1162 * since it was set above and can ONLY be set in this thread.
1163 */
1164 ret = consumer_create_socket(&ustconsumer32_data,
1165 cmd_ctx->session->ust_session->consumer);
1166 if (ret < 0) {
1167 goto error;
1168 }
1169 }
1170 break;
1171 }
1172 default:
1173 break;
1174 }
1175 skip_domain:
1176
1177 /* Validate consumer daemon state when start/stop trace command */
1178 if (cmd_ctx->lsm.cmd_type == LTTNG_START_TRACE ||
1179 cmd_ctx->lsm.cmd_type == LTTNG_STOP_TRACE) {
1180 switch (cmd_ctx->lsm.domain.type) {
1181 case LTTNG_DOMAIN_NONE:
1182 break;
1183 case LTTNG_DOMAIN_JUL:
1184 case LTTNG_DOMAIN_LOG4J:
1185 case LTTNG_DOMAIN_PYTHON:
1186 case LTTNG_DOMAIN_UST:
1187 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
1188 ret = LTTNG_ERR_NO_USTCONSUMERD;
1189 goto error;
1190 }
1191 break;
1192 case LTTNG_DOMAIN_KERNEL:
1193 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
1194 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1195 goto error;
1196 }
1197 break;
1198 default:
1199 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1200 goto error;
1201 }
1202 }
1203
1204 /*
1205 * Check that the UID matches that of the tracing session.
1206 * The root user can interact with all sessions.
1207 */
1208 if (need_tracing_session) {
1209 if (!session_access_ok(cmd_ctx->session,
1210 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds)) ||
1211 cmd_ctx->session->destroyed) {
1212 ret = LTTNG_ERR_EPERM;
1213 goto error;
1214 }
1215 }
1216
1217 /*
1218 * Send relayd information to consumer as soon as we have a domain and a
1219 * session defined.
1220 */
1221 if (cmd_ctx->session && need_domain) {
1222 /*
1223 * Setup relayd if not done yet. If the relayd information was already
1224 * sent to the consumer, this call will gracefully return.
1225 */
1226 ret = cmd_setup_relayd(cmd_ctx->session);
1227 if (ret != LTTNG_OK) {
1228 goto error;
1229 }
1230 }
1231
1232 /* Process by command type */
1233 switch (cmd_ctx->lsm.cmd_type) {
1234 case LTTNG_ADD_CONTEXT:
1235 {
1236 /*
1237 * An LTTNG_ADD_CONTEXT command might have a supplementary
1238 * payload if the context being added is an application context.
1239 */
1240 if (cmd_ctx->lsm.u.context.ctx.ctx ==
1241 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1242 char *provider_name = NULL, *context_name = NULL;
1243 size_t provider_name_len =
1244 cmd_ctx->lsm.u.context.provider_name_len;
1245 size_t context_name_len =
1246 cmd_ctx->lsm.u.context.context_name_len;
1247
1248 if (provider_name_len == 0 || context_name_len == 0) {
1249 /*
1250 * Application provider and context names MUST
1251 * be provided.
1252 */
1253 ret = -LTTNG_ERR_INVALID;
1254 goto error;
1255 }
1256
1257 provider_name = zmalloc(provider_name_len + 1);
1258 if (!provider_name) {
1259 ret = -LTTNG_ERR_NOMEM;
1260 goto error;
1261 }
1262 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name =
1263 provider_name;
1264
1265 context_name = zmalloc(context_name_len + 1);
1266 if (!context_name) {
1267 ret = -LTTNG_ERR_NOMEM;
1268 goto error_add_context;
1269 }
1270 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name =
1271 context_name;
1272
1273 ret = lttcomm_recv_unix_sock(*sock, provider_name,
1274 provider_name_len);
1275 if (ret < 0) {
1276 goto error_add_context;
1277 }
1278
1279 ret = lttcomm_recv_unix_sock(*sock, context_name,
1280 context_name_len);
1281 if (ret < 0) {
1282 goto error_add_context;
1283 }
1284 }
1285
1286 /*
1287 * cmd_add_context assumes ownership of the provider and context
1288 * names.
1289 */
1290 ret = cmd_add_context(cmd_ctx->session,
1291 cmd_ctx->lsm.domain.type,
1292 cmd_ctx->lsm.u.context.channel_name,
1293 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.context.ctx),
1294 kernel_poll_pipe[1]);
1295
1296 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
1297 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
1298 error_add_context:
1299 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name);
1300 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name);
1301 if (ret < 0) {
1302 goto error;
1303 }
1304 break;
1305 }
1306 case LTTNG_DISABLE_CHANNEL:
1307 {
1308 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1309 cmd_ctx->lsm.u.disable.channel_name);
1310 break;
1311 }
1312 case LTTNG_DISABLE_EVENT:
1313 {
1314
1315 /*
1316 * FIXME: handle filter; for now we just receive the filter's
1317 * bytecode along with the filter expression which are sent by
1318 * liblttng-ctl and discard them.
1319 *
1320 * This fixes an issue where the client may block while sending
1321 * the filter payload and encounter an error because the session
1322 * daemon closes the socket without ever handling this data.
1323 */
1324 size_t count = cmd_ctx->lsm.u.disable.expression_len +
1325 cmd_ctx->lsm.u.disable.bytecode_len;
1326
1327 if (count) {
1328 char data[LTTNG_FILTER_MAX_LEN];
1329
1330 DBG("Discarding disable event command payload of size %zu", count);
1331 while (count) {
1332 ret = lttcomm_recv_unix_sock(*sock, data,
1333 count > sizeof(data) ? sizeof(data) : count);
1334 if (ret < 0) {
1335 goto error;
1336 }
1337
1338 count -= (size_t) ret;
1339 }
1340 }
1341 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1342 cmd_ctx->lsm.u.disable.channel_name,
1343 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.disable.event));
1344 break;
1345 }
1346 case LTTNG_ENABLE_CHANNEL:
1347 {
1348 cmd_ctx->lsm.u.channel.chan.attr.extended.ptr =
1349 (struct lttng_channel_extended *) &cmd_ctx->lsm.u.channel.extended;
1350 ret = cmd_enable_channel(cmd_ctx->session,
1351 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1352 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.channel.chan),
1353 kernel_poll_pipe[1]);
1354 break;
1355 }
1356 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
1357 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE:
1358 {
1359 struct lttng_dynamic_buffer payload;
1360 struct lttng_buffer_view payload_view;
1361 const bool add_value =
1362 cmd_ctx->lsm.cmd_type ==
1363 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE;
1364 const size_t name_len =
1365 cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
1366 .name_len;
1367 const enum lttng_domain_type domain_type =
1368 (enum lttng_domain_type)
1369 cmd_ctx->lsm.domain.type;
1370 const enum lttng_process_attr process_attr =
1371 (enum lttng_process_attr) cmd_ctx->lsm.u
1372 .process_attr_tracker_add_remove_include_value
1373 .process_attr;
1374 const enum lttng_process_attr_value_type value_type =
1375 (enum lttng_process_attr_value_type) cmd_ctx
1376 ->lsm.u
1377 .process_attr_tracker_add_remove_include_value
1378 .value_type;
1379 struct process_attr_value *value;
1380 enum lttng_error_code ret_code;
1381 long login_name_max;
1382
1383 login_name_max = sysconf(_SC_LOGIN_NAME_MAX);
1384 if (login_name_max < 0) {
1385 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1386 ret = LTTNG_ERR_INVALID;
1387 goto error;
1388 }
1389
1390 /* Receive remaining variable length payload if applicable. */
1391 if (name_len > login_name_max) {
1392 /*
1393 * POSIX mandates user and group names that are at least
1394 * 8 characters long. Note that although shadow-utils
1395 * (useradd, groupaadd, etc.) use 32 chars as their
1396 * limit (from bits/utmp.h, UT_NAMESIZE),
1397 * LOGIN_NAME_MAX is defined to 256.
1398 */
1399 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1400 add_value ? "addition" : "removal",
1401 name_len, login_name_max);
1402 ret = LTTNG_ERR_INVALID;
1403 goto error;
1404 }
1405
1406 lttng_dynamic_buffer_init(&payload);
1407 if (name_len != 0) {
1408 /*
1409 * Receive variable payload for user/group name
1410 * arguments.
1411 */
1412 ret = lttng_dynamic_buffer_set_size(&payload, name_len);
1413 if (ret) {
1414 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1415 add_value ? "add" : "remove");
1416 ret = LTTNG_ERR_NOMEM;
1417 goto error_add_remove_tracker_value;
1418 }
1419
1420 ret = lttcomm_recv_unix_sock(
1421 *sock, payload.data, name_len);
1422 if (ret <= 0) {
1423 ERR("Failed to receive payload of %s process attribute tracker value argument",
1424 add_value ? "add" : "remove");
1425 *sock_error = 1;
1426 ret = LTTNG_ERR_INVALID_PROTOCOL;
1427 goto error_add_remove_tracker_value;
1428 }
1429 }
1430
1431 payload_view = lttng_buffer_view_from_dynamic_buffer(
1432 &payload, 0, name_len);
1433 if (name_len > 0 && !lttng_buffer_view_is_valid(&payload_view)) {
1434 ret = LTTNG_ERR_INVALID_PROTOCOL;
1435 goto error_add_remove_tracker_value;
1436 }
1437
1438 /*
1439 * Validate the value type and domains are legal for the process
1440 * attribute tracker that is specified and convert the value to
1441 * add/remove to the internal sessiond representation.
1442 */
1443 ret_code = process_attr_value_from_comm(domain_type,
1444 process_attr, value_type,
1445 &cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
1446 .integral_value,
1447 &payload_view, &value);
1448 if (ret_code != LTTNG_OK) {
1449 ret = ret_code;
1450 goto error_add_remove_tracker_value;
1451 }
1452
1453 if (add_value) {
1454 ret = cmd_process_attr_tracker_inclusion_set_add_value(
1455 cmd_ctx->session, domain_type,
1456 process_attr, value);
1457 } else {
1458 ret = cmd_process_attr_tracker_inclusion_set_remove_value(
1459 cmd_ctx->session, domain_type,
1460 process_attr, value);
1461 }
1462 process_attr_value_destroy(value);
1463 error_add_remove_tracker_value:
1464 lttng_dynamic_buffer_reset(&payload);
1465 break;
1466 }
1467 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
1468 {
1469 enum lttng_tracking_policy tracking_policy;
1470 const enum lttng_domain_type domain_type =
1471 (enum lttng_domain_type)
1472 cmd_ctx->lsm.domain.type;
1473 const enum lttng_process_attr process_attr =
1474 (enum lttng_process_attr) cmd_ctx->lsm.u
1475 .process_attr_tracker_get_tracking_policy
1476 .process_attr;
1477
1478 ret = cmd_process_attr_tracker_get_tracking_policy(
1479 cmd_ctx->session, domain_type, process_attr,
1480 &tracking_policy);
1481 if (ret != LTTNG_OK) {
1482 goto error;
1483 }
1484
1485 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1486 &(uint32_t){tracking_policy}, sizeof(uint32_t));
1487 if (ret < 0) {
1488 ret = LTTNG_ERR_NOMEM;
1489 goto error;
1490 }
1491 ret = LTTNG_OK;
1492 break;
1493 }
1494 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY:
1495 {
1496 const enum lttng_tracking_policy tracking_policy =
1497 (enum lttng_tracking_policy) cmd_ctx->lsm.u
1498 .process_attr_tracker_set_tracking_policy
1499 .tracking_policy;
1500 const enum lttng_domain_type domain_type =
1501 (enum lttng_domain_type)
1502 cmd_ctx->lsm.domain.type;
1503 const enum lttng_process_attr process_attr =
1504 (enum lttng_process_attr) cmd_ctx->lsm.u
1505 .process_attr_tracker_set_tracking_policy
1506 .process_attr;
1507
1508 ret = cmd_process_attr_tracker_set_tracking_policy(
1509 cmd_ctx->session, domain_type, process_attr,
1510 tracking_policy);
1511 if (ret != LTTNG_OK) {
1512 goto error;
1513 }
1514 break;
1515 }
1516 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
1517 {
1518 struct lttng_process_attr_values *values;
1519 struct lttng_dynamic_buffer reply;
1520 const enum lttng_domain_type domain_type =
1521 (enum lttng_domain_type)
1522 cmd_ctx->lsm.domain.type;
1523 const enum lttng_process_attr process_attr =
1524 (enum lttng_process_attr) cmd_ctx->lsm.u
1525 .process_attr_tracker_get_inclusion_set
1526 .process_attr;
1527
1528 ret = cmd_process_attr_tracker_get_inclusion_set(
1529 cmd_ctx->session, domain_type, process_attr,
1530 &values);
1531 if (ret != LTTNG_OK) {
1532 goto error;
1533 }
1534
1535 lttng_dynamic_buffer_init(&reply);
1536 ret = lttng_process_attr_values_serialize(values, &reply);
1537 if (ret < 0) {
1538 goto error_tracker_get_inclusion_set;
1539 }
1540
1541 ret = setup_lttng_msg_no_cmd_header(
1542 cmd_ctx, reply.data, reply.size);
1543 if (ret < 0) {
1544 ret = LTTNG_ERR_NOMEM;
1545 goto error_tracker_get_inclusion_set;
1546 }
1547 ret = LTTNG_OK;
1548
1549 error_tracker_get_inclusion_set:
1550 lttng_process_attr_values_destroy(values);
1551 lttng_dynamic_buffer_reset(&reply);
1552 break;
1553 }
1554 case LTTNG_ENABLE_EVENT:
1555 {
1556 struct lttng_event *ev = NULL;
1557 struct lttng_event_exclusion *exclusion = NULL;
1558 struct lttng_filter_bytecode *bytecode = NULL;
1559 char *filter_expression = NULL;
1560
1561 /* Handle exclusion events and receive it from the client. */
1562 if (cmd_ctx->lsm.u.enable.exclusion_count > 0) {
1563 size_t count = cmd_ctx->lsm.u.enable.exclusion_count;
1564
1565 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
1566 (count * LTTNG_SYMBOL_NAME_LEN));
1567 if (!exclusion) {
1568 ret = LTTNG_ERR_EXCLUSION_NOMEM;
1569 goto error;
1570 }
1571
1572 DBG("Receiving var len exclusion event list from client ...");
1573 exclusion->count = count;
1574 ret = lttcomm_recv_unix_sock(*sock, exclusion->names,
1575 count * LTTNG_SYMBOL_NAME_LEN);
1576 if (ret <= 0) {
1577 DBG("Nothing recv() from client var len data... continuing");
1578 *sock_error = 1;
1579 free(exclusion);
1580 ret = LTTNG_ERR_EXCLUSION_INVAL;
1581 goto error;
1582 }
1583 }
1584
1585 /* Get filter expression from client. */
1586 if (cmd_ctx->lsm.u.enable.expression_len > 0) {
1587 size_t expression_len =
1588 cmd_ctx->lsm.u.enable.expression_len;
1589
1590 if (expression_len > LTTNG_FILTER_MAX_LEN) {
1591 ret = LTTNG_ERR_FILTER_INVAL;
1592 free(exclusion);
1593 goto error;
1594 }
1595
1596 filter_expression = zmalloc(expression_len);
1597 if (!filter_expression) {
1598 free(exclusion);
1599 ret = LTTNG_ERR_FILTER_NOMEM;
1600 goto error;
1601 }
1602
1603 /* Receive var. len. data */
1604 DBG("Receiving var len filter's expression from client ...");
1605 ret = lttcomm_recv_unix_sock(*sock, filter_expression,
1606 expression_len);
1607 if (ret <= 0) {
1608 DBG("Nothing recv() from client var len data... continuing");
1609 *sock_error = 1;
1610 free(filter_expression);
1611 free(exclusion);
1612 ret = LTTNG_ERR_FILTER_INVAL;
1613 goto error;
1614 }
1615 }
1616
1617 /* Handle filter and get bytecode from client. */
1618 if (cmd_ctx->lsm.u.enable.bytecode_len > 0) {
1619 size_t bytecode_len = cmd_ctx->lsm.u.enable.bytecode_len;
1620
1621 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1622 ret = LTTNG_ERR_FILTER_INVAL;
1623 free(filter_expression);
1624 free(exclusion);
1625 goto error;
1626 }
1627
1628 bytecode = zmalloc(bytecode_len);
1629 if (!bytecode) {
1630 free(filter_expression);
1631 free(exclusion);
1632 ret = LTTNG_ERR_FILTER_NOMEM;
1633 goto error;
1634 }
1635
1636 /* Receive var. len. data */
1637 DBG("Receiving var len filter's bytecode from client ...");
1638 ret = lttcomm_recv_unix_sock(*sock, bytecode, bytecode_len);
1639 if (ret <= 0) {
1640 DBG("Nothing recv() from client var len data... continuing");
1641 *sock_error = 1;
1642 free(filter_expression);
1643 free(bytecode);
1644 free(exclusion);
1645 ret = LTTNG_ERR_FILTER_INVAL;
1646 goto error;
1647 }
1648
1649 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
1650 free(filter_expression);
1651 free(bytecode);
1652 free(exclusion);
1653 ret = LTTNG_ERR_FILTER_INVAL;
1654 goto error;
1655 }
1656 }
1657
1658 ev = lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx->lsm.u.enable.event));
1659 if (!ev) {
1660 DBG("Failed to copy event: %s",
1661 cmd_ctx->lsm.u.enable.event.name);
1662 free(filter_expression);
1663 free(bytecode);
1664 free(exclusion);
1665 ret = LTTNG_ERR_NOMEM;
1666 goto error;
1667 }
1668
1669
1670 if (cmd_ctx->lsm.u.enable.userspace_probe_location_len > 0) {
1671 /* Expect a userspace probe description. */
1672 ret = receive_userspace_probe(cmd_ctx, *sock, sock_error, ev);
1673 if (ret) {
1674 free(filter_expression);
1675 free(bytecode);
1676 free(exclusion);
1677 lttng_event_destroy(ev);
1678 goto error;
1679 }
1680 }
1681
1682 ret = cmd_enable_event(cmd_ctx->session,
1683 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1684 cmd_ctx->lsm.u.enable.channel_name,
1685 ev,
1686 filter_expression, bytecode, exclusion,
1687 kernel_poll_pipe[1]);
1688 lttng_event_destroy(ev);
1689 break;
1690 }
1691 case LTTNG_LIST_TRACEPOINTS:
1692 {
1693 struct lttng_event *events;
1694 ssize_t nb_events;
1695
1696 session_lock_list();
1697 nb_events = cmd_list_tracepoints(cmd_ctx->lsm.domain.type, &events);
1698 session_unlock_list();
1699 if (nb_events < 0) {
1700 /* Return value is a negative lttng_error_code. */
1701 ret = -nb_events;
1702 goto error;
1703 }
1704
1705 /*
1706 * Setup lttng message with payload size set to the event list size in
1707 * bytes and then copy list into the llm payload.
1708 */
1709 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1710 sizeof(struct lttng_event) * nb_events);
1711 free(events);
1712
1713 if (ret < 0) {
1714 goto setup_error;
1715 }
1716
1717 ret = LTTNG_OK;
1718 break;
1719 }
1720 case LTTNG_LIST_TRACEPOINT_FIELDS:
1721 {
1722 struct lttng_event_field *fields;
1723 ssize_t nb_fields;
1724
1725 session_lock_list();
1726 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm.domain.type,
1727 &fields);
1728 session_unlock_list();
1729 if (nb_fields < 0) {
1730 /* Return value is a negative lttng_error_code. */
1731 ret = -nb_fields;
1732 goto error;
1733 }
1734
1735 /*
1736 * Setup lttng message with payload size set to the event list size in
1737 * bytes and then copy list into the llm payload.
1738 */
1739 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
1740 sizeof(struct lttng_event_field) * nb_fields);
1741 free(fields);
1742
1743 if (ret < 0) {
1744 goto setup_error;
1745 }
1746
1747 ret = LTTNG_OK;
1748 break;
1749 }
1750 case LTTNG_LIST_SYSCALLS:
1751 {
1752 struct lttng_event *events;
1753 ssize_t nb_events;
1754
1755 nb_events = cmd_list_syscalls(&events);
1756 if (nb_events < 0) {
1757 /* Return value is a negative lttng_error_code. */
1758 ret = -nb_events;
1759 goto error;
1760 }
1761
1762 /*
1763 * Setup lttng message with payload size set to the event list size in
1764 * bytes and then copy list into the llm payload.
1765 */
1766 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1767 sizeof(struct lttng_event) * nb_events);
1768 free(events);
1769
1770 if (ret < 0) {
1771 goto setup_error;
1772 }
1773
1774 ret = LTTNG_OK;
1775 break;
1776 }
1777 case LTTNG_SET_CONSUMER_URI:
1778 {
1779 size_t nb_uri, len;
1780 struct lttng_uri *uris;
1781
1782 nb_uri = cmd_ctx->lsm.u.uri.size;
1783 len = nb_uri * sizeof(struct lttng_uri);
1784
1785 if (nb_uri == 0) {
1786 ret = LTTNG_ERR_INVALID;
1787 goto error;
1788 }
1789
1790 uris = zmalloc(len);
1791 if (uris == NULL) {
1792 ret = LTTNG_ERR_FATAL;
1793 goto error;
1794 }
1795
1796 /* Receive variable len data */
1797 DBG("Receiving %zu URI(s) from client ...", nb_uri);
1798 ret = lttcomm_recv_unix_sock(*sock, uris, len);
1799 if (ret <= 0) {
1800 DBG("No URIs received from client... continuing");
1801 *sock_error = 1;
1802 ret = LTTNG_ERR_SESSION_FAIL;
1803 free(uris);
1804 goto error;
1805 }
1806
1807 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
1808 free(uris);
1809 if (ret != LTTNG_OK) {
1810 goto error;
1811 }
1812
1813
1814 break;
1815 }
1816 case LTTNG_START_TRACE:
1817 {
1818 /*
1819 * On the first start, if we have a kernel session and we have
1820 * enabled time or size-based rotations, we have to make sure
1821 * the kernel tracer supports it.
1822 */
1823 if (!cmd_ctx->session->has_been_started && \
1824 cmd_ctx->session->kernel_session && \
1825 (cmd_ctx->session->rotate_timer_period || \
1826 cmd_ctx->session->rotate_size) && \
1827 !check_rotate_compatible()) {
1828 DBG("Kernel tracer version is not compatible with the rotation feature");
1829 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1830 goto error;
1831 }
1832 ret = cmd_start_trace(cmd_ctx->session);
1833 break;
1834 }
1835 case LTTNG_STOP_TRACE:
1836 {
1837 ret = cmd_stop_trace(cmd_ctx->session);
1838 break;
1839 }
1840 case LTTNG_DESTROY_SESSION:
1841 {
1842 ret = cmd_destroy_session(cmd_ctx->session,
1843 notification_thread_handle,
1844 sock);
1845 break;
1846 }
1847 case LTTNG_LIST_DOMAINS:
1848 {
1849 ssize_t nb_dom;
1850 struct lttng_domain *domains = NULL;
1851
1852 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
1853 if (nb_dom < 0) {
1854 /* Return value is a negative lttng_error_code. */
1855 ret = -nb_dom;
1856 goto error;
1857 }
1858
1859 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
1860 nb_dom * sizeof(struct lttng_domain));
1861 free(domains);
1862
1863 if (ret < 0) {
1864 goto setup_error;
1865 }
1866
1867 ret = LTTNG_OK;
1868 break;
1869 }
1870 case LTTNG_LIST_CHANNELS:
1871 {
1872 ssize_t payload_size;
1873 struct lttng_channel *channels = NULL;
1874
1875 payload_size = cmd_list_channels(cmd_ctx->lsm.domain.type,
1876 cmd_ctx->session, &channels);
1877 if (payload_size < 0) {
1878 /* Return value is a negative lttng_error_code. */
1879 ret = -payload_size;
1880 goto error;
1881 }
1882
1883 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
1884 payload_size);
1885 free(channels);
1886
1887 if (ret < 0) {
1888 goto setup_error;
1889 }
1890
1891 ret = LTTNG_OK;
1892 break;
1893 }
1894 case LTTNG_LIST_EVENTS:
1895 {
1896 ssize_t list_ret;
1897 struct lttcomm_event_command_header cmd_header = {};
1898 size_t original_payload_size;
1899 size_t payload_size;
1900
1901 ret = setup_empty_lttng_msg(cmd_ctx);
1902 if (ret) {
1903 ret = LTTNG_ERR_NOMEM;
1904 goto setup_error;
1905 }
1906
1907 original_payload_size = cmd_ctx->reply_payload.buffer.size;
1908
1909 /* Extended infos are included at the end of the payload. */
1910 list_ret = cmd_list_events(cmd_ctx->lsm.domain.type,
1911 cmd_ctx->session,
1912 cmd_ctx->lsm.u.list.channel_name,
1913 &cmd_ctx->reply_payload);
1914 if (list_ret < 0) {
1915 /* Return value is a negative lttng_error_code. */
1916 ret = -list_ret;
1917 goto error;
1918 }
1919
1920 payload_size = cmd_ctx->reply_payload.buffer.size -
1921 sizeof(cmd_header) - original_payload_size;
1922 update_lttng_msg(cmd_ctx, sizeof(cmd_header), payload_size);
1923
1924 ret = LTTNG_OK;
1925 break;
1926 }
1927 case LTTNG_LIST_SESSIONS:
1928 {
1929 unsigned int nr_sessions;
1930 void *sessions_payload;
1931 size_t payload_len;
1932
1933 session_lock_list();
1934 nr_sessions = lttng_sessions_count(
1935 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1936 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1937
1938 payload_len = (sizeof(struct lttng_session) * nr_sessions) +
1939 (sizeof(struct lttng_session_extended) * nr_sessions);
1940 sessions_payload = zmalloc(payload_len);
1941
1942 if (!sessions_payload) {
1943 session_unlock_list();
1944 ret = -ENOMEM;
1945 goto setup_error;
1946 }
1947
1948 cmd_list_lttng_sessions(sessions_payload, nr_sessions,
1949 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1950 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1951 session_unlock_list();
1952
1953 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
1954 payload_len);
1955 free(sessions_payload);
1956
1957 if (ret < 0) {
1958 goto setup_error;
1959 }
1960
1961 ret = LTTNG_OK;
1962 break;
1963 }
1964 case LTTNG_REGISTER_CONSUMER:
1965 {
1966 struct consumer_data *cdata;
1967
1968 switch (cmd_ctx->lsm.domain.type) {
1969 case LTTNG_DOMAIN_KERNEL:
1970 cdata = &kconsumer_data;
1971 break;
1972 default:
1973 ret = LTTNG_ERR_UND;
1974 goto error;
1975 }
1976
1977 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1978 cmd_ctx->lsm.u.reg.path, cdata);
1979 break;
1980 }
1981 case LTTNG_DATA_PENDING:
1982 {
1983 int pending_ret;
1984 uint8_t pending_ret_byte;
1985
1986 pending_ret = cmd_data_pending(cmd_ctx->session);
1987
1988 /*
1989 * FIXME
1990 *
1991 * This function may returns 0 or 1 to indicate whether or not
1992 * there is data pending. In case of error, it should return an
1993 * LTTNG_ERR code. However, some code paths may still return
1994 * a nondescript error code, which we handle by returning an
1995 * "unknown" error.
1996 */
1997 if (pending_ret == 0 || pending_ret == 1) {
1998 /*
1999 * ret will be set to LTTNG_OK at the end of
2000 * this function.
2001 */
2002 } else if (pending_ret < 0) {
2003 ret = LTTNG_ERR_UNK;
2004 goto setup_error;
2005 } else {
2006 ret = pending_ret;
2007 goto setup_error;
2008 }
2009
2010 pending_ret_byte = (uint8_t) pending_ret;
2011
2012 /* 1 byte to return whether or not data is pending */
2013 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
2014 &pending_ret_byte, 1);
2015
2016 if (ret < 0) {
2017 goto setup_error;
2018 }
2019
2020 ret = LTTNG_OK;
2021 break;
2022 }
2023 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2024 {
2025 uint32_t snapshot_id;
2026 struct lttcomm_lttng_output_id reply;
2027
2028 ret = cmd_snapshot_add_output(cmd_ctx->session,
2029 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output),
2030 &snapshot_id);
2031 if (ret != LTTNG_OK) {
2032 goto error;
2033 }
2034 reply.id = snapshot_id;
2035
2036 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
2037 sizeof(reply));
2038 if (ret < 0) {
2039 goto setup_error;
2040 }
2041
2042 /* Copy output list into message payload */
2043 ret = LTTNG_OK;
2044 break;
2045 }
2046 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2047 {
2048 ret = cmd_snapshot_del_output(cmd_ctx->session,
2049 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output));
2050 break;
2051 }
2052 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2053 {
2054 ssize_t nb_output;
2055 struct lttng_snapshot_output *outputs = NULL;
2056
2057 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
2058 if (nb_output < 0) {
2059 ret = -nb_output;
2060 goto error;
2061 }
2062
2063 assert((nb_output > 0 && outputs) || nb_output == 0);
2064 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
2065 nb_output * sizeof(struct lttng_snapshot_output));
2066 free(outputs);
2067
2068 if (ret < 0) {
2069 goto setup_error;
2070 }
2071
2072 ret = LTTNG_OK;
2073 break;
2074 }
2075 case LTTNG_SNAPSHOT_RECORD:
2076 {
2077 ret = cmd_snapshot_record(cmd_ctx->session,
2078 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_record.output),
2079 cmd_ctx->lsm.u.snapshot_record.wait);
2080 break;
2081 }
2082 case LTTNG_CREATE_SESSION_EXT:
2083 {
2084 struct lttng_dynamic_buffer payload;
2085 struct lttng_session_descriptor *return_descriptor = NULL;
2086
2087 lttng_dynamic_buffer_init(&payload);
2088 ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor);
2089 if (ret != LTTNG_OK) {
2090 goto error;
2091 }
2092
2093 ret = lttng_session_descriptor_serialize(return_descriptor,
2094 &payload);
2095 if (ret) {
2096 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2097 lttng_session_descriptor_destroy(return_descriptor);
2098 ret = LTTNG_ERR_NOMEM;
2099 goto error;
2100 }
2101 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data,
2102 payload.size);
2103 if (ret) {
2104 lttng_session_descriptor_destroy(return_descriptor);
2105 ret = LTTNG_ERR_NOMEM;
2106 goto error;
2107 }
2108 lttng_dynamic_buffer_reset(&payload);
2109 lttng_session_descriptor_destroy(return_descriptor);
2110 ret = LTTNG_OK;
2111 break;
2112 }
2113 case LTTNG_SAVE_SESSION:
2114 {
2115 ret = cmd_save_sessions(&cmd_ctx->lsm.u.save_session.attr,
2116 &cmd_ctx->creds);
2117 break;
2118 }
2119 case LTTNG_SET_SESSION_SHM_PATH:
2120 {
2121 ret = cmd_set_session_shm_path(cmd_ctx->session,
2122 cmd_ctx->lsm.u.set_shm_path.shm_path);
2123 break;
2124 }
2125 case LTTNG_REGENERATE_METADATA:
2126 {
2127 ret = cmd_regenerate_metadata(cmd_ctx->session);
2128 break;
2129 }
2130 case LTTNG_REGENERATE_STATEDUMP:
2131 {
2132 ret = cmd_regenerate_statedump(cmd_ctx->session);
2133 break;
2134 }
2135 case LTTNG_REGISTER_TRIGGER:
2136 {
2137 struct lttng_trigger *payload_trigger;
2138 struct lttng_trigger *return_trigger;
2139 size_t original_reply_payload_size;
2140 size_t reply_payload_size;
2141 const struct lttng_credentials cmd_creds = {
2142 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
2143 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
2144 };
2145
2146 ret = setup_empty_lttng_msg(cmd_ctx);
2147 if (ret) {
2148 ret = LTTNG_ERR_NOMEM;
2149 goto setup_error;
2150 }
2151
2152 ret = receive_lttng_trigger(
2153 cmd_ctx, *sock, sock_error, &payload_trigger);
2154 if (ret != LTTNG_OK) {
2155 goto error;
2156 }
2157
2158 original_reply_payload_size = cmd_ctx->reply_payload.buffer.size;
2159
2160 ret = cmd_register_trigger(&cmd_creds, payload_trigger,
2161 notification_thread_handle, &return_trigger);
2162 if (ret != LTTNG_OK) {
2163 lttng_trigger_put(payload_trigger);
2164 goto error;
2165 }
2166
2167 ret = lttng_trigger_serialize(return_trigger, &cmd_ctx->reply_payload);
2168 lttng_trigger_put(payload_trigger);
2169 lttng_trigger_put(return_trigger);
2170 if (ret) {
2171 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2172 ret = LTTNG_ERR_NOMEM;
2173 goto error;
2174 }
2175
2176 reply_payload_size = cmd_ctx->reply_payload.buffer.size -
2177 original_reply_payload_size;
2178
2179 update_lttng_msg(cmd_ctx, 0, reply_payload_size);
2180
2181 ret = LTTNG_OK;
2182 break;
2183 }
2184 case LTTNG_UNREGISTER_TRIGGER:
2185 {
2186 struct lttng_trigger *payload_trigger;
2187 const struct lttng_credentials cmd_creds = {
2188 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
2189 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
2190 };
2191
2192 ret = receive_lttng_trigger(
2193 cmd_ctx, *sock, sock_error, &payload_trigger);
2194 if (ret != LTTNG_OK) {
2195 goto error;
2196 }
2197
2198 ret = cmd_unregister_trigger(&cmd_creds, payload_trigger,
2199 notification_thread_handle);
2200 lttng_trigger_put(payload_trigger);
2201 break;
2202 }
2203 case LTTNG_ROTATE_SESSION:
2204 {
2205 struct lttng_rotate_session_return rotate_return;
2206
2207 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
2208
2209 memset(&rotate_return, 0, sizeof(rotate_return));
2210 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2211 DBG("Kernel tracer version is not compatible with the rotation feature");
2212 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2213 goto error;
2214 }
2215
2216 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return,
2217 false,
2218 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
2219 if (ret < 0) {
2220 ret = -ret;
2221 goto error;
2222 }
2223
2224 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
2225 sizeof(rotate_return));
2226 if (ret < 0) {
2227 ret = -ret;
2228 goto error;
2229 }
2230
2231 ret = LTTNG_OK;
2232 break;
2233 }
2234 case LTTNG_ROTATION_GET_INFO:
2235 {
2236 struct lttng_rotation_get_info_return get_info_return;
2237
2238 memset(&get_info_return, 0, sizeof(get_info_return));
2239 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
2240 cmd_ctx->lsm.u.get_rotation_info.rotation_id);
2241 if (ret < 0) {
2242 ret = -ret;
2243 goto error;
2244 }
2245
2246 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
2247 sizeof(get_info_return));
2248 if (ret < 0) {
2249 ret = -ret;
2250 goto error;
2251 }
2252
2253 ret = LTTNG_OK;
2254 break;
2255 }
2256 case LTTNG_ROTATION_SET_SCHEDULE:
2257 {
2258 bool set_schedule;
2259 enum lttng_rotation_schedule_type schedule_type;
2260 uint64_t value;
2261
2262 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2263 DBG("Kernel tracer version does not support session rotations");
2264 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2265 goto error;
2266 }
2267
2268 set_schedule = cmd_ctx->lsm.u.rotation_set_schedule.set == 1;
2269 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm.u.rotation_set_schedule.type;
2270 value = cmd_ctx->lsm.u.rotation_set_schedule.value;
2271
2272 ret = cmd_rotation_set_schedule(cmd_ctx->session,
2273 set_schedule,
2274 schedule_type,
2275 value,
2276 notification_thread_handle);
2277 if (ret != LTTNG_OK) {
2278 goto error;
2279 }
2280
2281 break;
2282 }
2283 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2284 {
2285 struct lttng_session_list_schedules_return schedules = {
2286 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
2287 .periodic.value = cmd_ctx->session->rotate_timer_period,
2288 .size.set = !!cmd_ctx->session->rotate_size,
2289 .size.value = cmd_ctx->session->rotate_size,
2290 };
2291
2292 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
2293 sizeof(schedules));
2294 if (ret < 0) {
2295 ret = -ret;
2296 goto error;
2297 }
2298
2299 ret = LTTNG_OK;
2300 break;
2301 }
2302 case LTTNG_CLEAR_SESSION:
2303 {
2304 ret = cmd_clear_session(cmd_ctx->session, sock);
2305 break;
2306 }
2307 case LTTNG_LIST_TRIGGERS:
2308 {
2309 struct lttng_triggers *return_triggers = NULL;
2310 size_t original_payload_size;
2311 size_t payload_size;
2312
2313 ret = setup_empty_lttng_msg(cmd_ctx);
2314 if (ret) {
2315 ret = LTTNG_ERR_NOMEM;
2316 goto setup_error;
2317 }
2318
2319 original_payload_size = cmd_ctx->reply_payload.buffer.size;
2320
2321 ret = cmd_list_triggers(cmd_ctx,
2322 notification_thread_handle, &return_triggers);
2323 if (ret != LTTNG_OK) {
2324 goto error;
2325 }
2326
2327 assert(return_triggers);
2328 ret = lttng_triggers_serialize(
2329 return_triggers, &cmd_ctx->reply_payload);
2330 lttng_triggers_destroy(return_triggers);
2331 if (ret) {
2332 ERR("Failed to serialize triggers in reply to `list triggers` command");
2333 ret = LTTNG_ERR_NOMEM;
2334 goto error;
2335 }
2336
2337 payload_size = cmd_ctx->reply_payload.buffer.size -
2338 original_payload_size;
2339
2340 update_lttng_msg(cmd_ctx, 0, payload_size);
2341
2342 ret = LTTNG_OK;
2343 break;
2344 }
2345 default:
2346 ret = LTTNG_ERR_UND;
2347 break;
2348 }
2349
2350 error:
2351 if (cmd_ctx->reply_payload.buffer.size == 0) {
2352 DBG("Missing llm header, creating one.");
2353 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
2354 goto setup_error;
2355 }
2356 }
2357 /* Set return code */
2358 ((struct lttcomm_lttng_msg *) (cmd_ctx->reply_payload.buffer.data))->ret_code = ret;
2359 setup_error:
2360 if (cmd_ctx->session) {
2361 session_unlock(cmd_ctx->session);
2362 session_put(cmd_ctx->session);
2363 cmd_ctx->session = NULL;
2364 }
2365 if (need_tracing_session) {
2366 session_unlock_list();
2367 }
2368 init_setup_error:
2369 assert(!rcu_read_ongoing());
2370 return ret;
2371 }
2372
2373 static int create_client_sock(void)
2374 {
2375 int ret, client_sock;
2376 const mode_t old_umask = umask(0);
2377
2378 /* Create client tool unix socket */
2379 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
2380 if (client_sock < 0) {
2381 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
2382 ret = -1;
2383 goto end;
2384 }
2385
2386 /* Set the cloexec flag */
2387 ret = utils_set_fd_cloexec(client_sock);
2388 if (ret < 0) {
2389 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2390 "Continuing but note that the consumer daemon will have a "
2391 "reference to this socket on exec()", client_sock);
2392 }
2393
2394 /* File permission MUST be 660 */
2395 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2396 if (ret < 0) {
2397 ERR("Set file permissions failed: %s",
2398 config.client_unix_sock_path.value);
2399 PERROR("chmod");
2400 (void) lttcomm_close_unix_sock(client_sock);
2401 ret = -1;
2402 goto end;
2403 }
2404 DBG("Created client socket (fd = %i)", client_sock);
2405 ret = client_sock;
2406 end:
2407 umask(old_umask);
2408 return ret;
2409 }
2410
2411 static void cleanup_client_thread(void *data)
2412 {
2413 struct lttng_pipe *quit_pipe = data;
2414
2415 lttng_pipe_destroy(quit_pipe);
2416 }
2417
2418 static void thread_init_cleanup(void *data)
2419 {
2420 set_thread_status(false);
2421 }
2422
2423 /*
2424 * This thread manage all clients request using the unix client socket for
2425 * communication.
2426 */
2427 static void *thread_manage_clients(void *data)
2428 {
2429 int sock = -1, ret, i, pollfd, err = -1;
2430 int sock_error;
2431 uint32_t revents, nb_fd;
2432 struct lttng_poll_event events;
2433 const int client_sock = thread_state.client_sock;
2434 struct lttng_pipe *quit_pipe = data;
2435 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
2436 struct command_ctx cmd_ctx = {};
2437
2438 DBG("[thread] Manage client started");
2439
2440 lttng_payload_init(&cmd_ctx.reply_payload);
2441
2442 is_root = (getuid() == 0);
2443
2444 pthread_cleanup_push(thread_init_cleanup, NULL);
2445
2446 rcu_register_thread();
2447
2448 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
2449
2450 health_code_update();
2451
2452 ret = lttcomm_listen_unix_sock(client_sock);
2453 if (ret < 0) {
2454 goto error_listen;
2455 }
2456
2457 /*
2458 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2459 * more will be added to this poll set.
2460 */
2461 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2462 if (ret < 0) {
2463 goto error_create_poll;
2464 }
2465
2466 /* Add the application registration socket */
2467 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
2468 if (ret < 0) {
2469 goto error;
2470 }
2471
2472 /* Add thread quit pipe */
2473 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
2474 if (ret < 0) {
2475 goto error;
2476 }
2477
2478 /* Set state as running. */
2479 set_thread_status(true);
2480 pthread_cleanup_pop(0);
2481
2482 /* This testpoint is after we signal readiness to the parent. */
2483 if (testpoint(sessiond_thread_manage_clients)) {
2484 goto error;
2485 }
2486
2487 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
2488 goto error;
2489 }
2490
2491 health_code_update();
2492
2493 while (1) {
2494 const struct cmd_completion_handler *cmd_completion_handler;
2495
2496 cmd_ctx.creds = (lttng_sock_cred) {
2497 .uid = UINT32_MAX,
2498 .gid = UINT32_MAX,
2499 };
2500 cmd_ctx.session = NULL;
2501 lttng_payload_clear(&cmd_ctx.reply_payload);
2502 cmd_ctx.lttng_msg_size = 0;
2503
2504 DBG("Accepting client command ...");
2505
2506 /* Inifinite blocking call, waiting for transmission */
2507 restart:
2508 health_poll_entry();
2509 ret = lttng_poll_wait(&events, -1);
2510 health_poll_exit();
2511 if (ret < 0) {
2512 /*
2513 * Restart interrupted system call.
2514 */
2515 if (errno == EINTR) {
2516 goto restart;
2517 }
2518 goto error;
2519 }
2520
2521 nb_fd = ret;
2522
2523 for (i = 0; i < nb_fd; i++) {
2524 revents = LTTNG_POLL_GETEV(&events, i);
2525 pollfd = LTTNG_POLL_GETFD(&events, i);
2526
2527 health_code_update();
2528
2529 if (pollfd == thread_quit_pipe_fd) {
2530 err = 0;
2531 goto exit;
2532 } else {
2533 /* Event on the registration socket */
2534 if (revents & LPOLLIN) {
2535 continue;
2536 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2537 ERR("Client socket poll error");
2538 goto error;
2539 } else {
2540 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2541 goto error;
2542 }
2543 }
2544 }
2545
2546 DBG("Wait for client response");
2547
2548 health_code_update();
2549
2550 sock = lttcomm_accept_unix_sock(client_sock);
2551 if (sock < 0) {
2552 goto error;
2553 }
2554
2555 /*
2556 * Set the CLOEXEC flag. Return code is useless because either way, the
2557 * show must go on.
2558 */
2559 (void) utils_set_fd_cloexec(sock);
2560
2561 /* Set socket option for credentials retrieval */
2562 ret = lttcomm_setsockopt_creds_unix_sock(sock);
2563 if (ret < 0) {
2564 goto error;
2565 }
2566
2567 health_code_update();
2568
2569 /*
2570 * Data is received from the lttng client. The struct
2571 * lttcomm_session_msg (lsm) contains the command and data request of
2572 * the client.
2573 */
2574 DBG("Receiving data from client ...");
2575 ret = lttcomm_recv_creds_unix_sock(sock, &cmd_ctx.lsm,
2576 sizeof(struct lttcomm_session_msg), &cmd_ctx.creds);
2577 if (ret != sizeof(struct lttcomm_session_msg)) {
2578 DBG("Incomplete recv() from client... continuing");
2579 ret = close(sock);
2580 if (ret) {
2581 PERROR("close");
2582 }
2583 sock = -1;
2584 continue;
2585 }
2586
2587 health_code_update();
2588
2589 // TODO: Validate cmd_ctx including sanity check for
2590 // security purpose.
2591
2592 rcu_thread_online();
2593 /*
2594 * This function dispatch the work to the kernel or userspace tracer
2595 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2596 * informations for the client. The command context struct contains
2597 * everything this function may needs.
2598 */
2599 ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
2600 rcu_thread_offline();
2601 if (ret < 0) {
2602 if (sock >= 0) {
2603 ret = close(sock);
2604 if (ret) {
2605 PERROR("close");
2606 }
2607 }
2608 sock = -1;
2609 /*
2610 * TODO: Inform client somehow of the fatal error. At
2611 * this point, ret < 0 means that a zmalloc failed
2612 * (ENOMEM). Error detected but still accept
2613 * command, unless a socket error has been
2614 * detected.
2615 */
2616 continue;
2617 }
2618
2619 if (ret < LTTNG_OK || ret >= LTTNG_ERR_NR) {
2620 WARN("Command returned an invalid status code, returning unknown error: command type = %d, ret = %d", cmd_ctx.lsm.cmd_type, ret);
2621 ret = LTTNG_ERR_UNK;
2622 }
2623
2624 cmd_completion_handler = cmd_pop_completion_handler();
2625 if (cmd_completion_handler) {
2626 enum lttng_error_code completion_code;
2627
2628 completion_code = cmd_completion_handler->run(
2629 cmd_completion_handler->data);
2630 if (completion_code != LTTNG_OK) {
2631 continue;
2632 }
2633 }
2634
2635 health_code_update();
2636
2637 if (sock >= 0) {
2638 struct lttng_payload_view view =
2639 lttng_payload_view_from_payload(
2640 &cmd_ctx.reply_payload,
2641 0, -1);
2642 struct lttcomm_lttng_msg *llm = (typeof(
2643 llm)) cmd_ctx.reply_payload.buffer.data;
2644
2645 assert(cmd_ctx.reply_payload.buffer.size >= sizeof(*llm));
2646 assert(cmd_ctx.lttng_msg_size == cmd_ctx.reply_payload.buffer.size);
2647
2648 llm->fd_count = lttng_payload_view_get_fd_handle_count(&view);
2649
2650 DBG("Sending response (size: %d, retcode: %s (%d))",
2651 cmd_ctx.lttng_msg_size,
2652 lttng_strerror(-llm->ret_code),
2653 llm->ret_code);
2654 ret = send_unix_sock(sock, &view);
2655 if (ret < 0) {
2656 ERR("Failed to send data back to client");
2657 }
2658
2659 /* End of transmission */
2660 ret = close(sock);
2661 if (ret) {
2662 PERROR("close");
2663 }
2664 }
2665 sock = -1;
2666
2667 health_code_update();
2668 }
2669
2670 exit:
2671 error:
2672 if (sock >= 0) {
2673 ret = close(sock);
2674 if (ret) {
2675 PERROR("close");
2676 }
2677 }
2678
2679 lttng_poll_clean(&events);
2680
2681 error_listen:
2682 error_create_poll:
2683 unlink(config.client_unix_sock_path.value);
2684 ret = close(client_sock);
2685 if (ret) {
2686 PERROR("close");
2687 }
2688
2689 if (err) {
2690 health_error();
2691 ERR("Health error occurred in %s", __func__);
2692 }
2693
2694 health_unregister(health_sessiond);
2695
2696 DBG("Client thread dying");
2697 lttng_payload_reset(&cmd_ctx.reply_payload);
2698 rcu_unregister_thread();
2699 return NULL;
2700 }
2701
2702 static
2703 bool shutdown_client_thread(void *thread_data)
2704 {
2705 struct lttng_pipe *client_quit_pipe = thread_data;
2706 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2707
2708 return notify_thread_pipe(write_fd) == 1;
2709 }
2710
2711 struct lttng_thread *launch_client_thread(void)
2712 {
2713 bool thread_running;
2714 struct lttng_pipe *client_quit_pipe;
2715 struct lttng_thread *thread = NULL;
2716 int client_sock_fd = -1;
2717
2718 sem_init(&thread_state.ready, 0, 0);
2719 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2720 if (!client_quit_pipe) {
2721 goto error;
2722 }
2723
2724 client_sock_fd = create_client_sock();
2725 if (client_sock_fd < 0) {
2726 goto error;
2727 }
2728
2729 thread_state.client_sock = client_sock_fd;
2730 thread = lttng_thread_create("Client management",
2731 thread_manage_clients,
2732 shutdown_client_thread,
2733 cleanup_client_thread,
2734 client_quit_pipe);
2735 if (!thread) {
2736 goto error;
2737 }
2738 /* The client thread now owns the client sock fd and the quit pipe. */
2739 client_sock_fd = -1;
2740 client_quit_pipe = NULL;
2741
2742 /*
2743 * This thread is part of the threads that need to be fully
2744 * initialized before the session daemon is marked as "ready".
2745 */
2746 thread_running = wait_thread_status();
2747 if (!thread_running) {
2748 goto error;
2749 }
2750 return thread;
2751 error:
2752 if (client_sock_fd >= 0) {
2753 if (close(client_sock_fd)) {
2754 PERROR("Failed to close client socket");
2755 }
2756 }
2757 lttng_thread_put(thread);
2758 cleanup_client_thread(client_quit_pipe);
2759 return NULL;
2760 }
This page took 0.133755 seconds and 4 git commands to generate.