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