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