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