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