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