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