Fix: liblttng-ctl comm: lttng_event_context is not packed
[lttng-tools.git] / src / bin / lttng-sessiond / client.c
CommitLineData
917a718d 1/*
a4eb26f0 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
917a718d 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
917a718d 7 *
917a718d
JG
8 */
9
f953b297
JG
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"
917a718d 15#include <common/compat/getenv.h>
f953b297 16#include <common/tracker.h>
917a718d
JG
17#include <common/unix.h>
18#include <common/utils.h>
917a718d 19#include <lttng/event-internal.h>
b178f53e 20#include <lttng/session-descriptor-internal.h>
f953b297
JG
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>
33b9609d 26#include <stdint.h>
f953b297 27#include <sys/stat.h>
917a718d
JG
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"
4ec029ed 37#include "manage-consumer.h"
022349df 38#include "clear.h"
917a718d
JG
39
40static bool is_root;
41
42static struct thread_state {
6cb45e93
JG
43 sem_t ready;
44 bool running;
0f68efb6 45 int client_sock;
6cb45e93
JG
46} thread_state;
47
48static void set_thread_status(bool running)
917a718d 49{
6cb45e93
JG
50 DBG("Marking client thread's state as %s", running ? "running" : "error");
51 thread_state.running = running;
52 sem_post(&thread_state.ready);
917a718d
JG
53}
54
6cb45e93 55static bool wait_thread_status(void)
917a718d 56{
6cb45e93
JG
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");
917a718d 63 }
6cb45e93
JG
64
65 return thread_state.running;
917a718d
JG
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 */
75static 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
99a98ed3 85 free(cmd_ctx->llm);
917a718d
JG
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
113end:
114 return ret;
115}
116
117/*
118 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
4ec029ed 119 * exec or it will fail.
917a718d
JG
120 */
121static int spawn_consumer_thread(struct consumer_data *consumer_data)
122{
4ec029ed 123 return launch_consumer_management_thread(consumer_data) ? 0 : -1;
917a718d
JG
124}
125
126/*
127 * Fork and exec a consumer daemon (consumerd).
128 *
129 * Return pid if successful else -1.
130 */
131static 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) {
cea15630 191 const char *tmp;
917a718d
JG
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) {
cea15630 228 const char *tmp;
917a718d
JG
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 }
277error:
278 return ret;
279}
280
281/*
282 * Spawn the consumerd daemon and session daemon thread.
283 */
284static 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
323end:
324 return 0;
325
326error:
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 */
346static 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 */
b178f53e
JG
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);
917a718d
JG
400 ret = LTTNG_OK;
401
402error:
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 */
411static int create_ust_session(struct ltt_session *session,
df4f5a87 412 const struct lttng_domain *domain)
917a718d
JG
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
465error:
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 */
474static int create_kernel_session(struct ltt_session *session)
475{
476 int ret;
477
478 DBG("Creating kernel session");
479
7d268848 480 ret = kernel_create_session(session);
917a718d
JG
481 if (ret < 0) {
482 ret = LTTNG_ERR_KERN_SESS_FAIL;
5d0a7bcb 483 goto error_create;
917a718d
JG
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;
ee8935c3 499 session->kernel_session->is_live_session = session->live_timer != 0;
917a718d
JG
500
501 return LTTNG_OK;
502
503error:
504 trace_kernel_destroy_session(session->kernel_session);
505 session->kernel_session = NULL;
5d0a7bcb 506error_create:
917a718d
JG
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
917a718d
JG
537/*
538 * Version of setup_lttng_msg() without command header.
539 */
540static 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 */
549static 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 */
568static 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 */
585static 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.
3e3665b8
JG
603 * A command may assume the ownership of the socket, in which case its value
604 * should be set to -1.
917a718d
JG
605 *
606 * Should *NOT* be called with RCU read-side lock held.
607 */
3e3665b8 608static int process_client_msg(struct command_ctx *cmd_ctx, int *sock,
917a718d
JG
609 int *sock_error)
610{
611 int ret = LTTNG_OK;
612 int need_tracing_session = 1;
613 int need_domain;
33b9609d
JR
614 struct lttng_dynamic_buffer payload;
615
616 lttng_dynamic_buffer_init(&payload);
917a718d
JG
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) {
b178f53e 625 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
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:
022349df 646 case LTTNG_CLEAR_SESSION:
917a718d
JG
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:
f953b297
JG
687 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
688 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
689 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
917a718d
JG
690 case LTTNG_DATA_PENDING:
691 case LTTNG_ROTATE_SESSION:
692 case LTTNG_ROTATION_GET_INFO:
917a718d
JG
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) {
b178f53e 705 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
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
7d268848
MD
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
917a718d
JG
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);
51630bd8 800 if (ret != LTTNG_OK) {
917a718d
JG
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,
df4f5a87 852 ALIGNED_CONST_PTR(cmd_ctx->lsm->domain));
917a718d
JG
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 }
922skip_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 {
b583ad83 984 ret = cmd_add_context(cmd_ctx, *sock, kernel_poll_pipe[1]);
917a718d
JG
985 break;
986 }
987 case LTTNG_DISABLE_CHANNEL:
988 {
989 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
990 cmd_ctx->lsm->u.disable.channel_name);
991 break;
992 }
993 case LTTNG_DISABLE_EVENT:
994 {
166fc586 995 ret = cmd_disable_event(cmd_ctx, *sock);
917a718d
JG
996 break;
997 }
998 case LTTNG_ENABLE_CHANNEL:
999 {
33b9609d 1000 ret = cmd_enable_channel(cmd_ctx, *sock, kernel_poll_pipe[1]);
917a718d
JG
1001 break;
1002 }
f953b297
JG
1003 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
1004 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE:
917a718d 1005 {
f953b297
JG
1006 struct lttng_dynamic_buffer payload;
1007 struct lttng_buffer_view payload_view;
1008 const bool add_value =
1009 cmd_ctx->lsm->cmd_type ==
1010 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE;
1011 const size_t name_len =
1012 cmd_ctx->lsm->u.process_attr_tracker_add_remove_include_value
1013 .name_len;
1014 const enum lttng_domain_type domain_type =
1015 (enum lttng_domain_type)
1016 cmd_ctx->lsm->domain.type;
1017 const enum lttng_process_attr process_attr =
1018 (enum lttng_process_attr) cmd_ctx->lsm->u
1019 .process_attr_tracker_add_remove_include_value
1020 .process_attr;
1021 const enum lttng_process_attr_value_type value_type =
1022 (enum lttng_process_attr_value_type) cmd_ctx
1023 ->lsm->u
1024 .process_attr_tracker_add_remove_include_value
1025 .value_type;
1026 struct process_attr_value *value;
1027 enum lttng_error_code ret_code;
1028
1029 /* Receive remaining variable length payload if applicable. */
1030 if (name_len > LOGIN_NAME_MAX) {
1031 /*
1032 * POSIX mandates user and group names that are at least
1033 * 8 characters long. Note that although shadow-utils
1034 * (useradd, groupaadd, etc.) use 32 chars as their
1035 * limit (from bits/utmp.h, UT_NAMESIZE),
1036 * LOGIN_NAME_MAX is defined to 256.
1037 */
1038 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1039 add_value ? "addition" : "removal",
1040 name_len, LOGIN_NAME_MAX);
1041 ret = LTTNG_ERR_INVALID;
2d97a006
JR
1042 goto error;
1043 }
1044
f953b297
JG
1045 lttng_dynamic_buffer_init(&payload);
1046 if (name_len != 0) {
1047 /*
1048 * Receive variable payload for user/group name
1049 * arguments.
1050 */
1051 ret = lttng_dynamic_buffer_set_size(&payload, name_len);
1052 if (ret) {
1053 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1054 add_value ? "add" : "remove");
55c9e7ca 1055 ret = LTTNG_ERR_NOMEM;
f953b297 1056 goto error_add_remove_tracker_value;
55c9e7ca 1057 }
f953b297
JG
1058
1059 ret = lttcomm_recv_unix_sock(
1060 *sock, payload.data, name_len);
55c9e7ca 1061 if (ret <= 0) {
f953b297
JG
1062 ERR("Failed to receive payload of %s process attribute tracker value argument",
1063 add_value ? "add" : "remove");
55c9e7ca 1064 *sock_error = 1;
f953b297
JG
1065 ret = LTTNG_ERR_INVALID_PROTOCOL;
1066 goto error_add_remove_tracker_value;
55c9e7ca 1067 }
f953b297 1068 }
2d97a006 1069
f953b297
JG
1070 payload_view = lttng_buffer_view_from_dynamic_buffer(
1071 &payload, 0, name_len);
1072 /*
1073 * Validate the value type and domains are legal for the process
1074 * attribute tracker that is specified and convert the value to
1075 * add/remove to the internal sessiond representation.
1076 */
1077 ret_code = process_attr_value_from_comm(domain_type,
1078 process_attr, value_type,
1079 &cmd_ctx->lsm->u.process_attr_tracker_add_remove_include_value
1080 .integral_value,
1081 &payload_view, &value);
1082 if (ret_code != LTTNG_OK) {
1083 ret = ret_code;
1084 goto error_add_remove_tracker_value;
55c9e7ca 1085 }
f953b297
JG
1086
1087 if (add_value) {
1088 ret = cmd_process_attr_tracker_inclusion_set_add_value(
1089 cmd_ctx->session, domain_type,
1090 process_attr, value);
1091 } else {
1092 ret = cmd_process_attr_tracker_inclusion_set_remove_value(
1093 cmd_ctx->session, domain_type,
1094 process_attr, value);
1095 }
1096 process_attr_value_destroy(value);
1097 error_add_remove_tracker_value:
1098 lttng_dynamic_buffer_reset(&payload);
1099 break;
1100 }
1101 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
1102 {
1103 enum lttng_tracking_policy tracking_policy;
1104 const enum lttng_domain_type domain_type =
1105 (enum lttng_domain_type)
1106 cmd_ctx->lsm->domain.type;
1107 const enum lttng_process_attr process_attr =
1108 (enum lttng_process_attr) cmd_ctx->lsm->u
1109 .process_attr_tracker_get_tracking_policy
1110 .process_attr;
1111
1112 ret = cmd_process_attr_tracker_get_tracking_policy(
1113 cmd_ctx->session, domain_type, process_attr,
1114 &tracking_policy);
1115 if (ret != LTTNG_OK) {
55c9e7ca
JR
1116 goto error;
1117 }
2d97a006 1118
f953b297
JG
1119 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1120 &(uint32_t){tracking_policy}, sizeof(uint32_t));
1121 if (ret < 0) {
1122 ret = LTTNG_ERR_NOMEM;
2d97a006
JR
1123 goto error;
1124 }
f953b297 1125 ret = LTTNG_OK;
917a718d
JG
1126 break;
1127 }
f953b297 1128 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY:
917a718d 1129 {
f953b297
JG
1130 const enum lttng_tracking_policy tracking_policy =
1131 (enum lttng_tracking_policy) cmd_ctx->lsm->u
1132 .process_attr_tracker_set_tracking_policy
1133 .tracking_policy;
1134 const enum lttng_domain_type domain_type =
1135 (enum lttng_domain_type)
1136 cmd_ctx->lsm->domain.type;
1137 const enum lttng_process_attr process_attr =
1138 (enum lttng_process_attr) cmd_ctx->lsm->u
1139 .process_attr_tracker_set_tracking_policy
1140 .process_attr;
1141
1142 ret = cmd_process_attr_tracker_set_tracking_policy(
1143 cmd_ctx->session, domain_type, process_attr,
1144 tracking_policy);
1145 if (ret != LTTNG_OK) {
1146 goto error;
55c9e7ca 1147 }
f953b297
JG
1148 break;
1149 }
1150 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
1151 {
1152 struct lttng_process_attr_values *values;
1153 struct lttng_dynamic_buffer reply;
1154 const enum lttng_domain_type domain_type =
1155 (enum lttng_domain_type)
1156 cmd_ctx->lsm->domain.type;
1157 const enum lttng_process_attr process_attr =
1158 (enum lttng_process_attr) cmd_ctx->lsm->u
1159 .process_attr_tracker_get_inclusion_set
1160 .process_attr;
1161
1162 ret = cmd_process_attr_tracker_get_inclusion_set(
1163 cmd_ctx->session, domain_type, process_attr,
1164 &values);
1165 if (ret != LTTNG_OK) {
55c9e7ca
JR
1166 goto error;
1167 }
2d97a006 1168
f953b297
JG
1169 lttng_dynamic_buffer_init(&reply);
1170 ret = lttng_process_attr_values_serialize(values, &reply);
1171 if (ret < 0) {
1172 goto error_tracker_get_inclusion_set;
2d97a006
JR
1173 }
1174
f953b297
JG
1175 ret = setup_lttng_msg_no_cmd_header(
1176 cmd_ctx, reply.data, reply.size);
1177 if (ret < 0) {
1178 ret = LTTNG_ERR_NOMEM;
1179 goto error_tracker_get_inclusion_set;
1180 }
1181 ret = LTTNG_OK;
1182
1183 error_tracker_get_inclusion_set:
1184 lttng_process_attr_values_destroy(values);
1185 lttng_dynamic_buffer_reset(&reply);
917a718d
JG
1186 break;
1187 }
1188 case LTTNG_ENABLE_EVENT:
1189 {
166fc586 1190 ret = cmd_enable_event(cmd_ctx, *sock, kernel_poll_pipe[1]);
917a718d
JG
1191 break;
1192 }
1193 case LTTNG_LIST_TRACEPOINTS:
1194 {
166fc586
JR
1195 enum lttng_error_code ret_code;
1196 unsigned int nb_events;
1197 struct lttcomm_list_command_header cmd_header = { 0 };
917a718d
JG
1198
1199 session_lock_list();
166fc586
JR
1200 ret_code = cmd_list_tracepoints(cmd_ctx->lsm->domain.type,
1201 &payload, &nb_events);
917a718d 1202 session_unlock_list();
166fc586
JR
1203 if (ret_code != LTTNG_OK) {
1204 ret = (int) ret_code;
917a718d
JG
1205 goto error;
1206 }
1207
166fc586
JR
1208 if (nb_events > UINT32_MAX) {
1209 ret = LTTNG_ERR_OVERFLOW;
1210 goto error;
1211 }
1212
1213 cmd_header.count = (uint32_t) nb_events;
1214 ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size,
1215 &cmd_header, sizeof(cmd_header));
917a718d
JG
1216
1217 if (ret < 0) {
1218 goto setup_error;
1219 }
1220
1221 ret = LTTNG_OK;
1222 break;
1223 }
1224 case LTTNG_LIST_TRACEPOINT_FIELDS:
1225 {
1226 struct lttng_event_field *fields;
1227 ssize_t nb_fields;
1228
1229 session_lock_list();
1230 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
1231 &fields);
1232 session_unlock_list();
1233 if (nb_fields < 0) {
1234 /* Return value is a negative lttng_error_code. */
1235 ret = -nb_fields;
1236 goto error;
1237 }
1238
1239 /*
1240 * Setup lttng message with payload size set to the event list size in
1241 * bytes and then copy list into the llm payload.
1242 */
1243 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
1244 sizeof(struct lttng_event_field) * nb_fields);
1245 free(fields);
1246
1247 if (ret < 0) {
1248 goto setup_error;
1249 }
1250
1251 ret = LTTNG_OK;
1252 break;
1253 }
1254 case LTTNG_LIST_SYSCALLS:
1255 {
166fc586
JR
1256 enum lttng_error_code ret_code;
1257 uint32_t nb_syscalls;
1258 struct lttcomm_list_command_header cmd_header = { 0 };
917a718d 1259
166fc586
JR
1260 ret_code = cmd_list_syscalls(&payload, &nb_syscalls);
1261 if (ret_code != LTTNG_OK) {
1262 ret = (int) ret_code;
917a718d
JG
1263 goto error;
1264 }
1265
166fc586
JR
1266 if (nb_syscalls > UINT32_MAX) {
1267 ret = LTTNG_ERR_OVERFLOW;
1268 goto error;
1269 }
1270
1271 cmd_header.count = (uint32_t) nb_syscalls;
1272 ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size,
1273 &cmd_header, sizeof(cmd_header));
917a718d
JG
1274
1275 if (ret < 0) {
1276 goto setup_error;
1277 }
1278
1279 ret = LTTNG_OK;
1280 break;
1281 }
917a718d
JG
1282 case LTTNG_SET_CONSUMER_URI:
1283 {
1284 size_t nb_uri, len;
1285 struct lttng_uri *uris;
1286
1287 nb_uri = cmd_ctx->lsm->u.uri.size;
1288 len = nb_uri * sizeof(struct lttng_uri);
1289
1290 if (nb_uri == 0) {
1291 ret = LTTNG_ERR_INVALID;
1292 goto error;
1293 }
1294
1295 uris = zmalloc(len);
1296 if (uris == NULL) {
1297 ret = LTTNG_ERR_FATAL;
1298 goto error;
1299 }
1300
1301 /* Receive variable len data */
1302 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3e3665b8 1303 ret = lttcomm_recv_unix_sock(*sock, uris, len);
917a718d
JG
1304 if (ret <= 0) {
1305 DBG("No URIs received from client... continuing");
1306 *sock_error = 1;
1307 ret = LTTNG_ERR_SESSION_FAIL;
1308 free(uris);
1309 goto error;
1310 }
1311
1312 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
1313 free(uris);
1314 if (ret != LTTNG_OK) {
1315 goto error;
1316 }
1317
1318
1319 break;
1320 }
1321 case LTTNG_START_TRACE:
1322 {
1323 /*
1324 * On the first start, if we have a kernel session and we have
1325 * enabled time or size-based rotations, we have to make sure
1326 * the kernel tracer supports it.
1327 */
1328 if (!cmd_ctx->session->has_been_started && \
1329 cmd_ctx->session->kernel_session && \
1330 (cmd_ctx->session->rotate_timer_period || \
1331 cmd_ctx->session->rotate_size) && \
1332 !check_rotate_compatible()) {
1333 DBG("Kernel tracer version is not compatible with the rotation feature");
1334 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1335 goto error;
1336 }
1337 ret = cmd_start_trace(cmd_ctx->session);
1338 break;
1339 }
1340 case LTTNG_STOP_TRACE:
1341 {
1342 ret = cmd_stop_trace(cmd_ctx->session);
1343 break;
1344 }
917a718d
JG
1345 case LTTNG_DESTROY_SESSION:
1346 {
1347 ret = cmd_destroy_session(cmd_ctx->session,
3e3665b8
JG
1348 notification_thread_handle,
1349 sock);
917a718d
JG
1350 break;
1351 }
1352 case LTTNG_LIST_DOMAINS:
1353 {
1354 ssize_t nb_dom;
1355 struct lttng_domain *domains = NULL;
1356
1357 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
1358 if (nb_dom < 0) {
1359 /* Return value is a negative lttng_error_code. */
1360 ret = -nb_dom;
1361 goto error;
1362 }
1363
1364 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
1365 nb_dom * sizeof(struct lttng_domain));
1366 free(domains);
1367
1368 if (ret < 0) {
1369 goto setup_error;
1370 }
1371
1372 ret = LTTNG_OK;
1373 break;
1374 }
1375 case LTTNG_LIST_CHANNELS:
1376 {
33b9609d
JR
1377 uint32_t nb_channel;
1378 enum lttng_error_code ret_code;
1379 struct lttcomm_list_command_header cmd_header = { 0 };
917a718d 1380
33b9609d
JR
1381 ret_code = cmd_list_channels(cmd_ctx->lsm->domain.type,
1382 cmd_ctx->session, &payload, &nb_channel);
1383 if (ret_code != LTTNG_OK) {
1384 ret = (int) ret_code;
917a718d
JG
1385 goto error;
1386 }
1387
33b9609d
JR
1388 cmd_header.count = nb_channel;
1389 ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size,
1390 &cmd_header, sizeof(cmd_header));
917a718d
JG
1391
1392 if (ret < 0) {
1393 goto setup_error;
1394 }
1395
1396 ret = LTTNG_OK;
1397 break;
1398 }
1399 case LTTNG_LIST_EVENTS:
1400 {
166fc586
JR
1401 enum lttng_error_code ret_code;
1402 unsigned int nb_events;
1403 struct lttcomm_list_command_header cmd_header = { 0 };
1404
1405 ret_code = cmd_list_events(cmd_ctx->lsm->domain.type,
1406 cmd_ctx->session,
1407 cmd_ctx->lsm->u.list.channel_name, &payload,
1408 &nb_events);
1409
1410 if (ret_code != LTTNG_OK) {
1411 ret = (int) ret_code;
917a718d
JG
1412 goto error;
1413 }
1414
166fc586
JR
1415 if (nb_events > UINT32_MAX) {
1416 ret = LTTNG_ERR_OVERFLOW;
1417 goto error;
1418 }
1419
1420 cmd_header.count = (uint32_t) nb_events;
1421 ret = setup_lttng_msg(cmd_ctx, payload.data, payload.size,
1422 &cmd_header, sizeof(cmd_header));
917a718d
JG
1423
1424 if (ret < 0) {
1425 goto setup_error;
1426 }
1427
1428 ret = LTTNG_OK;
1429 break;
1430 }
1431 case LTTNG_LIST_SESSIONS:
1432 {
1433 unsigned int nr_sessions;
1434 void *sessions_payload;
1435 size_t payload_len;
1436
1437 session_lock_list();
1438 nr_sessions = lttng_sessions_count(
1439 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1440 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
b178f53e
JG
1441
1442 payload_len = (sizeof(struct lttng_session) * nr_sessions) +
1443 (sizeof(struct lttng_session_extended) * nr_sessions);
917a718d
JG
1444 sessions_payload = zmalloc(payload_len);
1445
1446 if (!sessions_payload) {
1447 session_unlock_list();
1448 ret = -ENOMEM;
1449 goto setup_error;
1450 }
1451
b178f53e 1452 cmd_list_lttng_sessions(sessions_payload, nr_sessions,
917a718d
JG
1453 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1454 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1455 session_unlock_list();
1456
1457 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
1458 payload_len);
1459 free(sessions_payload);
1460
1461 if (ret < 0) {
1462 goto setup_error;
1463 }
1464
1465 ret = LTTNG_OK;
1466 break;
1467 }
1468 case LTTNG_REGISTER_CONSUMER:
1469 {
1470 struct consumer_data *cdata;
1471
1472 switch (cmd_ctx->lsm->domain.type) {
1473 case LTTNG_DOMAIN_KERNEL:
1474 cdata = &kconsumer_data;
1475 break;
1476 default:
1477 ret = LTTNG_ERR_UND;
1478 goto error;
1479 }
1480
1481 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
1482 cmd_ctx->lsm->u.reg.path, cdata);
1483 break;
1484 }
1485 case LTTNG_DATA_PENDING:
1486 {
1487 int pending_ret;
1488 uint8_t pending_ret_byte;
1489
1490 pending_ret = cmd_data_pending(cmd_ctx->session);
1491
1492 /*
1493 * FIXME
1494 *
1495 * This function may returns 0 or 1 to indicate whether or not
1496 * there is data pending. In case of error, it should return an
1497 * LTTNG_ERR code. However, some code paths may still return
1498 * a nondescript error code, which we handle by returning an
1499 * "unknown" error.
1500 */
1501 if (pending_ret == 0 || pending_ret == 1) {
1502 /*
1503 * ret will be set to LTTNG_OK at the end of
1504 * this function.
1505 */
1506 } else if (pending_ret < 0) {
1507 ret = LTTNG_ERR_UNK;
1508 goto setup_error;
1509 } else {
1510 ret = pending_ret;
1511 goto setup_error;
1512 }
1513
1514 pending_ret_byte = (uint8_t) pending_ret;
1515
1516 /* 1 byte to return whether or not data is pending */
1517 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1518 &pending_ret_byte, 1);
1519
1520 if (ret < 0) {
1521 goto setup_error;
1522 }
1523
1524 ret = LTTNG_OK;
1525 break;
1526 }
1527 case LTTNG_SNAPSHOT_ADD_OUTPUT:
1528 {
a914e76a 1529 uint32_t snapshot_id;
917a718d
JG
1530 struct lttcomm_lttng_output_id reply;
1531
1532 ret = cmd_snapshot_add_output(cmd_ctx->session,
df4f5a87
JG
1533 ALIGNED_CONST_PTR(cmd_ctx->lsm->u.snapshot_output.output),
1534 &snapshot_id);
917a718d
JG
1535 if (ret != LTTNG_OK) {
1536 goto error;
1537 }
a914e76a 1538 reply.id = snapshot_id;
917a718d
JG
1539
1540 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
1541 sizeof(reply));
1542 if (ret < 0) {
1543 goto setup_error;
1544 }
1545
1546 /* Copy output list into message payload */
1547 ret = LTTNG_OK;
1548 break;
1549 }
1550 case LTTNG_SNAPSHOT_DEL_OUTPUT:
1551 {
1552 ret = cmd_snapshot_del_output(cmd_ctx->session,
df4f5a87 1553 ALIGNED_CONST_PTR(cmd_ctx->lsm->u.snapshot_output.output));
917a718d
JG
1554 break;
1555 }
1556 case LTTNG_SNAPSHOT_LIST_OUTPUT:
1557 {
1558 ssize_t nb_output;
1559 struct lttng_snapshot_output *outputs = NULL;
1560
1561 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
1562 if (nb_output < 0) {
1563 ret = -nb_output;
1564 goto error;
1565 }
1566
1567 assert((nb_output > 0 && outputs) || nb_output == 0);
1568 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
1569 nb_output * sizeof(struct lttng_snapshot_output));
1570 free(outputs);
1571
1572 if (ret < 0) {
1573 goto setup_error;
1574 }
1575
1576 ret = LTTNG_OK;
1577 break;
1578 }
1579 case LTTNG_SNAPSHOT_RECORD:
1580 {
1581 ret = cmd_snapshot_record(cmd_ctx->session,
df4f5a87 1582 ALIGNED_CONST_PTR(cmd_ctx->lsm->u.snapshot_record.output),
917a718d
JG
1583 cmd_ctx->lsm->u.snapshot_record.wait);
1584 break;
1585 }
b178f53e 1586 case LTTNG_CREATE_SESSION_EXT:
917a718d 1587 {
b178f53e
JG
1588 struct lttng_dynamic_buffer payload;
1589 struct lttng_session_descriptor *return_descriptor = NULL;
917a718d 1590
b178f53e 1591 lttng_dynamic_buffer_init(&payload);
3e3665b8 1592 ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor);
b178f53e
JG
1593 if (ret != LTTNG_OK) {
1594 goto error;
917a718d
JG
1595 }
1596
b178f53e
JG
1597 ret = lttng_session_descriptor_serialize(return_descriptor,
1598 &payload);
1599 if (ret) {
1600 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1601 lttng_session_descriptor_destroy(return_descriptor);
1602 ret = LTTNG_ERR_NOMEM;
1603 goto error;
917a718d 1604 }
b178f53e
JG
1605 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data,
1606 payload.size);
1607 if (ret) {
1608 lttng_session_descriptor_destroy(return_descriptor);
1609 ret = LTTNG_ERR_NOMEM;
1610 goto error;
1611 }
1612 lttng_dynamic_buffer_reset(&payload);
1613 lttng_session_descriptor_destroy(return_descriptor);
1614 ret = LTTNG_OK;
917a718d
JG
1615 break;
1616 }
1617 case LTTNG_SAVE_SESSION:
1618 {
1619 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
1620 &cmd_ctx->creds);
1621 break;
1622 }
1623 case LTTNG_SET_SESSION_SHM_PATH:
1624 {
1625 ret = cmd_set_session_shm_path(cmd_ctx->session,
1626 cmd_ctx->lsm->u.set_shm_path.shm_path);
1627 break;
1628 }
1629 case LTTNG_REGENERATE_METADATA:
1630 {
1631 ret = cmd_regenerate_metadata(cmd_ctx->session);
1632 break;
1633 }
1634 case LTTNG_REGENERATE_STATEDUMP:
1635 {
1636 ret = cmd_regenerate_statedump(cmd_ctx->session);
1637 break;
1638 }
1639 case LTTNG_REGISTER_TRIGGER:
1640 {
3e3665b8 1641 ret = cmd_register_trigger(cmd_ctx, *sock,
917a718d
JG
1642 notification_thread_handle);
1643 break;
1644 }
1645 case LTTNG_UNREGISTER_TRIGGER:
1646 {
3e3665b8 1647 ret = cmd_unregister_trigger(cmd_ctx, *sock,
917a718d
JG
1648 notification_thread_handle);
1649 break;
1650 }
1651 case LTTNG_ROTATE_SESSION:
1652 {
1653 struct lttng_rotate_session_return rotate_return;
1654
1655 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
1656
1657 memset(&rotate_return, 0, sizeof(rotate_return));
1658 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
1659 DBG("Kernel tracer version is not compatible with the rotation feature");
1660 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1661 goto error;
1662 }
1663
7fdbed1c 1664 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return,
343defc2
MD
1665 false,
1666 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
917a718d
JG
1667 if (ret < 0) {
1668 ret = -ret;
1669 goto error;
1670 }
1671
1672 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
1673 sizeof(rotate_return));
1674 if (ret < 0) {
1675 ret = -ret;
1676 goto error;
1677 }
1678
1679 ret = LTTNG_OK;
1680 break;
1681 }
1682 case LTTNG_ROTATION_GET_INFO:
1683 {
1684 struct lttng_rotation_get_info_return get_info_return;
1685
1686 memset(&get_info_return, 0, sizeof(get_info_return));
1687 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
1688 cmd_ctx->lsm->u.get_rotation_info.rotation_id);
1689 if (ret < 0) {
1690 ret = -ret;
1691 goto error;
1692 }
1693
1694 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
1695 sizeof(get_info_return));
1696 if (ret < 0) {
1697 ret = -ret;
1698 goto error;
1699 }
1700
1701 ret = LTTNG_OK;
1702 break;
1703 }
1704 case LTTNG_ROTATION_SET_SCHEDULE:
1705 {
1706 bool set_schedule;
1707 enum lttng_rotation_schedule_type schedule_type;
1708 uint64_t value;
1709
1710 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
1711 DBG("Kernel tracer version does not support session rotations");
1712 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1713 goto error;
1714 }
1715
1716 set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1;
1717 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type;
1718 value = cmd_ctx->lsm->u.rotation_set_schedule.value;
1719
1720 ret = cmd_rotation_set_schedule(cmd_ctx->session,
1721 set_schedule,
1722 schedule_type,
1723 value,
1724 notification_thread_handle);
1725 if (ret != LTTNG_OK) {
1726 goto error;
1727 }
1728
1729 break;
1730 }
1731 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
1732 {
1733 struct lttng_session_list_schedules_return schedules = {
1734 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
1735 .periodic.value = cmd_ctx->session->rotate_timer_period,
1736 .size.set = !!cmd_ctx->session->rotate_size,
1737 .size.value = cmd_ctx->session->rotate_size,
1738 };
1739
1740 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
1741 sizeof(schedules));
1742 if (ret < 0) {
1743 ret = -ret;
1744 goto error;
1745 }
1746
1747 ret = LTTNG_OK;
1748 break;
1749 }
022349df
MD
1750 case LTTNG_CLEAR_SESSION:
1751 {
1752 ret = cmd_clear_session(cmd_ctx->session, sock);
1753 break;
1754 }
917a718d
JG
1755 default:
1756 ret = LTTNG_ERR_UND;
1757 break;
1758 }
1759
1760error:
1761 if (cmd_ctx->llm == NULL) {
1762 DBG("Missing llm structure. Allocating one.");
1763 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
1764 goto setup_error;
1765 }
1766 }
1767 /* Set return code */
1768 cmd_ctx->llm->ret_code = ret;
1769setup_error:
1770 if (cmd_ctx->session) {
1771 session_unlock(cmd_ctx->session);
1772 session_put(cmd_ctx->session);
3e3665b8 1773 cmd_ctx->session = NULL;
917a718d
JG
1774 }
1775 if (need_tracing_session) {
1776 session_unlock_list();
1777 }
1778init_setup_error:
1779 assert(!rcu_read_ongoing());
33b9609d 1780 lttng_dynamic_buffer_reset(&payload);
917a718d
JG
1781 return ret;
1782}
1783
1784static int create_client_sock(void)
1785{
1786 int ret, client_sock;
1787 const mode_t old_umask = umask(0);
1788
1789 /* Create client tool unix socket */
1790 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
1791 if (client_sock < 0) {
1792 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
1793 ret = -1;
1794 goto end;
1795 }
1796
1797 /* Set the cloexec flag */
1798 ret = utils_set_fd_cloexec(client_sock);
1799 if (ret < 0) {
1800 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1801 "Continuing but note that the consumer daemon will have a "
1802 "reference to this socket on exec()", client_sock);
1803 }
1804
1805 /* File permission MUST be 660 */
1806 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1807 if (ret < 0) {
18972083
JR
1808 ERR("Set file permissions failed: %s",
1809 config.client_unix_sock_path.value);
917a718d 1810 PERROR("chmod");
18972083
JR
1811 (void) lttcomm_close_unix_sock(client_sock);
1812 ret = -1;
917a718d
JG
1813 goto end;
1814 }
1815 DBG("Created client socket (fd = %i)", client_sock);
1816 ret = client_sock;
1817end:
1818 umask(old_umask);
1819 return ret;
1820}
1821
1822static void cleanup_client_thread(void *data)
1823{
1824 struct lttng_pipe *quit_pipe = data;
1825
1826 lttng_pipe_destroy(quit_pipe);
1827}
1828
6cb45e93
JG
1829static void thread_init_cleanup(void *data)
1830{
1831 set_thread_status(false);
1832}
1833
917a718d
JG
1834/*
1835 * This thread manage all clients request using the unix client socket for
1836 * communication.
1837 */
1838static void *thread_manage_clients(void *data)
1839{
1840 int sock = -1, ret, i, pollfd, err = -1;
1841 int sock_error;
1842 uint32_t revents, nb_fd;
1843 struct command_ctx *cmd_ctx = NULL;
1844 struct lttng_poll_event events;
0f68efb6 1845 const int client_sock = thread_state.client_sock;
917a718d
JG
1846 struct lttng_pipe *quit_pipe = data;
1847 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
1848
1849 DBG("[thread] Manage client started");
1850
1851 is_root = (getuid() == 0);
1852
6cb45e93 1853 pthread_cleanup_push(thread_init_cleanup, NULL);
917a718d
JG
1854
1855 rcu_register_thread();
1856
1857 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
1858
1859 health_code_update();
1860
1861 ret = lttcomm_listen_unix_sock(client_sock);
1862 if (ret < 0) {
1863 goto error_listen;
1864 }
1865
1866 /*
1867 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
1868 * more will be added to this poll set.
1869 */
1870 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
1871 if (ret < 0) {
1872 goto error_create_poll;
1873 }
1874
1875 /* Add the application registration socket */
1876 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
1877 if (ret < 0) {
1878 goto error;
1879 }
1880
1881 /* Add thread quit pipe */
1882 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
1883 if (ret < 0) {
1884 goto error;
1885 }
1886
6cb45e93
JG
1887 /* Set state as running. */
1888 set_thread_status(true);
1889 pthread_cleanup_pop(0);
1890
917a718d
JG
1891 /* This testpoint is after we signal readiness to the parent. */
1892 if (testpoint(sessiond_thread_manage_clients)) {
1893 goto error;
1894 }
1895
1896 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
1897 goto error;
1898 }
1899
1900 health_code_update();
1901
917a718d
JG
1902 while (1) {
1903 const struct cmd_completion_handler *cmd_completion_handler;
1904
1905 DBG("Accepting client command ...");
1906
1907 /* Inifinite blocking call, waiting for transmission */
1908 restart:
1909 health_poll_entry();
1910 ret = lttng_poll_wait(&events, -1);
1911 health_poll_exit();
1912 if (ret < 0) {
1913 /*
1914 * Restart interrupted system call.
1915 */
1916 if (errno == EINTR) {
1917 goto restart;
1918 }
1919 goto error;
1920 }
1921
1922 nb_fd = ret;
1923
1924 for (i = 0; i < nb_fd; i++) {
1925 revents = LTTNG_POLL_GETEV(&events, i);
1926 pollfd = LTTNG_POLL_GETFD(&events, i);
1927
1928 health_code_update();
1929
917a718d
JG
1930 if (pollfd == thread_quit_pipe_fd) {
1931 err = 0;
1932 goto exit;
1933 } else {
1934 /* Event on the registration socket */
1935 if (revents & LPOLLIN) {
1936 continue;
1937 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1938 ERR("Client socket poll error");
1939 goto error;
1940 } else {
1941 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1942 goto error;
1943 }
1944 }
1945 }
1946
1947 DBG("Wait for client response");
1948
1949 health_code_update();
1950
1951 sock = lttcomm_accept_unix_sock(client_sock);
1952 if (sock < 0) {
1953 goto error;
1954 }
1955
1956 /*
1957 * Set the CLOEXEC flag. Return code is useless because either way, the
1958 * show must go on.
1959 */
1960 (void) utils_set_fd_cloexec(sock);
1961
1962 /* Set socket option for credentials retrieval */
1963 ret = lttcomm_setsockopt_creds_unix_sock(sock);
1964 if (ret < 0) {
1965 goto error;
1966 }
1967
1968 /* Allocate context command to process the client request */
1969 cmd_ctx = zmalloc(sizeof(struct command_ctx));
1970 if (cmd_ctx == NULL) {
1971 PERROR("zmalloc cmd_ctx");
1972 goto error;
1973 }
1974
1975 /* Allocate data buffer for reception */
1976 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
1977 if (cmd_ctx->lsm == NULL) {
1978 PERROR("zmalloc cmd_ctx->lsm");
1979 goto error;
1980 }
1981
1982 cmd_ctx->llm = NULL;
1983 cmd_ctx->session = NULL;
1984
1985 health_code_update();
1986
1987 /*
1988 * Data is received from the lttng client. The struct
1989 * lttcomm_session_msg (lsm) contains the command and data request of
1990 * the client.
1991 */
1992 DBG("Receiving data from client ...");
1993 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
1994 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
1995 if (ret <= 0) {
1996 DBG("Nothing recv() from client... continuing");
1997 ret = close(sock);
1998 if (ret) {
1999 PERROR("close");
2000 }
2001 sock = -1;
2002 clean_command_ctx(&cmd_ctx);
2003 continue;
2004 }
2005
2006 health_code_update();
2007
2008 // TODO: Validate cmd_ctx including sanity check for
2009 // security purpose.
2010
2011 rcu_thread_online();
2012 /*
2013 * This function dispatch the work to the kernel or userspace tracer
2014 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2015 * informations for the client. The command context struct contains
2016 * everything this function may needs.
2017 */
3e3665b8 2018 ret = process_client_msg(cmd_ctx, &sock, &sock_error);
917a718d
JG
2019 rcu_thread_offline();
2020 if (ret < 0) {
3e3665b8
JG
2021 if (sock >= 0) {
2022 ret = close(sock);
2023 if (ret) {
2024 PERROR("close");
2025 }
2026 }
2027 sock = -1;
917a718d
JG
2028 /*
2029 * TODO: Inform client somehow of the fatal error. At
2030 * this point, ret < 0 means that a zmalloc failed
2031 * (ENOMEM). Error detected but still accept
2032 * command, unless a socket error has been
2033 * detected.
2034 */
2035 clean_command_ctx(&cmd_ctx);
2036 continue;
2037 }
2038
2039 cmd_completion_handler = cmd_pop_completion_handler();
2040 if (cmd_completion_handler) {
2041 enum lttng_error_code completion_code;
2042
2043 completion_code = cmd_completion_handler->run(
2044 cmd_completion_handler->data);
2045 if (completion_code != LTTNG_OK) {
2046 clean_command_ctx(&cmd_ctx);
2047 continue;
2048 }
2049 }
2050
2051 health_code_update();
2052
3e3665b8
JG
2053 if (sock >= 0) {
2054 DBG("Sending response (size: %d, retcode: %s (%d))",
2055 cmd_ctx->lttng_msg_size,
2056 lttng_strerror(-cmd_ctx->llm->ret_code),
2057 cmd_ctx->llm->ret_code);
2058 ret = send_unix_sock(sock, cmd_ctx->llm,
2059 cmd_ctx->lttng_msg_size);
2060 if (ret < 0) {
2061 ERR("Failed to send data back to client");
2062 }
917a718d 2063
3e3665b8
JG
2064 /* End of transmission */
2065 ret = close(sock);
2066 if (ret) {
2067 PERROR("close");
2068 }
2069 }
2070 sock = -1;
917a718d
JG
2071
2072 clean_command_ctx(&cmd_ctx);
2073
2074 health_code_update();
2075 }
2076
2077exit:
2078error:
2079 if (sock >= 0) {
2080 ret = close(sock);
2081 if (ret) {
2082 PERROR("close");
2083 }
2084 }
2085
2086 lttng_poll_clean(&events);
2087 clean_command_ctx(&cmd_ctx);
2088
2089error_listen:
2090error_create_poll:
2091 unlink(config.client_unix_sock_path.value);
0f68efb6
JG
2092 ret = close(client_sock);
2093 if (ret) {
2094 PERROR("close");
917a718d
JG
2095 }
2096
2097 if (err) {
2098 health_error();
2099 ERR("Health error occurred in %s", __func__);
2100 }
2101
2102 health_unregister(health_sessiond);
2103
2104 DBG("Client thread dying");
2105
2106 rcu_unregister_thread();
917a718d
JG
2107 return NULL;
2108}
2109
2110static
2111bool shutdown_client_thread(void *thread_data)
2112{
2113 struct lttng_pipe *client_quit_pipe = thread_data;
2114 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2115
2116 return notify_thread_pipe(write_fd) == 1;
2117}
2118
2119struct lttng_thread *launch_client_thread(void)
2120{
6cb45e93 2121 bool thread_running;
917a718d 2122 struct lttng_pipe *client_quit_pipe;
0f68efb6
JG
2123 struct lttng_thread *thread = NULL;
2124 int client_sock_fd = -1;
917a718d 2125
6cb45e93 2126 sem_init(&thread_state.ready, 0, 0);
917a718d
JG
2127 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2128 if (!client_quit_pipe) {
2129 goto error;
2130 }
2131
0f68efb6
JG
2132 client_sock_fd = create_client_sock();
2133 if (client_sock_fd < 0) {
2134 goto error;
2135 }
2136
2137 thread_state.client_sock = client_sock_fd;
917a718d
JG
2138 thread = lttng_thread_create("Client management",
2139 thread_manage_clients,
2140 shutdown_client_thread,
2141 cleanup_client_thread,
2142 client_quit_pipe);
2143 if (!thread) {
2144 goto error;
2145 }
0f68efb6
JG
2146 /* The client thread now owns the client sock fd and the quit pipe. */
2147 client_sock_fd = -1;
2148 client_quit_pipe = NULL;
917a718d
JG
2149
2150 /*
2151 * This thread is part of the threads that need to be fully
2152 * initialized before the session daemon is marked as "ready".
2153 */
6cb45e93
JG
2154 thread_running = wait_thread_status();
2155 if (!thread_running) {
0f68efb6 2156 goto error;
6cb45e93 2157 }
917a718d
JG
2158 return thread;
2159error:
0f68efb6
JG
2160 if (client_sock_fd >= 0) {
2161 if (close(client_sock_fd)) {
2162 PERROR("Failed to close client socket");
2163 }
2164 }
2165 lttng_thread_put(thread);
917a718d
JG
2166 cleanup_client_thread(client_quit_pipe);
2167 return NULL;
2168}
This page took 0.12566 seconds and 5 git commands to generate.