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