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