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