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