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