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