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