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