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