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