Create stream files relative to a stream's current trace chunk
[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.
709 *
710 * Should *NOT* be called with RCU read-side lock held.
711 */
712static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
713 int *sock_error)
714{
715 int ret = LTTNG_OK;
716 int need_tracing_session = 1;
717 int need_domain;
718
719 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
720
721 assert(!rcu_read_ongoing());
722
723 *sock_error = 0;
724
725 switch (cmd_ctx->lsm->cmd_type) {
b178f53e 726 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
727 case LTTNG_DESTROY_SESSION:
728 case LTTNG_LIST_SESSIONS:
729 case LTTNG_LIST_DOMAINS:
730 case LTTNG_START_TRACE:
731 case LTTNG_STOP_TRACE:
732 case LTTNG_DATA_PENDING:
733 case LTTNG_SNAPSHOT_ADD_OUTPUT:
734 case LTTNG_SNAPSHOT_DEL_OUTPUT:
735 case LTTNG_SNAPSHOT_LIST_OUTPUT:
736 case LTTNG_SNAPSHOT_RECORD:
737 case LTTNG_SAVE_SESSION:
738 case LTTNG_SET_SESSION_SHM_PATH:
739 case LTTNG_REGENERATE_METADATA:
740 case LTTNG_REGENERATE_STATEDUMP:
741 case LTTNG_REGISTER_TRIGGER:
742 case LTTNG_UNREGISTER_TRIGGER:
743 case LTTNG_ROTATE_SESSION:
744 case LTTNG_ROTATION_GET_INFO:
745 case LTTNG_ROTATION_SET_SCHEDULE:
746 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
747 need_domain = 0;
748 break;
749 default:
750 need_domain = 1;
751 }
752
753 if (config.no_kernel && need_domain
754 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
755 if (!is_root) {
756 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
757 } else {
758 ret = LTTNG_ERR_KERN_NA;
759 }
760 goto error;
761 }
762
763 /* Deny register consumer if we already have a spawned consumer. */
764 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
765 pthread_mutex_lock(&kconsumer_data.pid_mutex);
766 if (kconsumer_data.pid > 0) {
767 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
768 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
769 goto error;
770 }
771 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
772 }
773
774 /*
775 * Check for command that don't needs to allocate a returned payload. We do
776 * this here so we don't have to make the call for no payload at each
777 * command.
778 */
779 switch(cmd_ctx->lsm->cmd_type) {
780 case LTTNG_LIST_SESSIONS:
781 case LTTNG_LIST_TRACEPOINTS:
782 case LTTNG_LIST_TRACEPOINT_FIELDS:
783 case LTTNG_LIST_DOMAINS:
784 case LTTNG_LIST_CHANNELS:
785 case LTTNG_LIST_EVENTS:
786 case LTTNG_LIST_SYSCALLS:
787 case LTTNG_LIST_TRACKER_PIDS:
788 case LTTNG_DATA_PENDING:
789 case LTTNG_ROTATE_SESSION:
790 case LTTNG_ROTATION_GET_INFO:
791 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
792 break;
793 default:
794 /* Setup lttng message with no payload */
795 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
796 if (ret < 0) {
797 /* This label does not try to unlock the session */
798 goto init_setup_error;
799 }
800 }
801
802 /* Commands that DO NOT need a session. */
803 switch (cmd_ctx->lsm->cmd_type) {
b178f53e 804 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
805 case LTTNG_LIST_SESSIONS:
806 case LTTNG_LIST_TRACEPOINTS:
807 case LTTNG_LIST_SYSCALLS:
808 case LTTNG_LIST_TRACEPOINT_FIELDS:
809 case LTTNG_SAVE_SESSION:
810 case LTTNG_REGISTER_TRIGGER:
811 case LTTNG_UNREGISTER_TRIGGER:
812 need_tracing_session = 0;
813 break;
814 default:
815 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
816 /*
817 * We keep the session list lock across _all_ commands
818 * for now, because the per-session lock does not
819 * handle teardown properly.
820 */
821 session_lock_list();
822 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
823 if (cmd_ctx->session == NULL) {
824 ret = LTTNG_ERR_SESS_NOT_FOUND;
825 goto error;
826 } else {
827 /* Acquire lock for the session */
828 session_lock(cmd_ctx->session);
829 }
830 break;
831 }
832
833 /*
834 * Commands that need a valid session but should NOT create one if none
835 * exists. Instead of creating one and destroying it when the command is
836 * handled, process that right before so we save some round trip in useless
837 * code path.
838 */
839 switch (cmd_ctx->lsm->cmd_type) {
840 case LTTNG_DISABLE_CHANNEL:
841 case LTTNG_DISABLE_EVENT:
842 switch (cmd_ctx->lsm->domain.type) {
843 case LTTNG_DOMAIN_KERNEL:
844 if (!cmd_ctx->session->kernel_session) {
845 ret = LTTNG_ERR_NO_CHANNEL;
846 goto error;
847 }
848 break;
849 case LTTNG_DOMAIN_JUL:
850 case LTTNG_DOMAIN_LOG4J:
851 case LTTNG_DOMAIN_PYTHON:
852 case LTTNG_DOMAIN_UST:
853 if (!cmd_ctx->session->ust_session) {
854 ret = LTTNG_ERR_NO_CHANNEL;
855 goto error;
856 }
857 break;
858 default:
859 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
860 goto error;
861 }
862 default:
863 break;
864 }
865
866 if (!need_domain) {
867 goto skip_domain;
868 }
869
870 /*
871 * Check domain type for specific "pre-action".
872 */
873 switch (cmd_ctx->lsm->domain.type) {
874 case LTTNG_DOMAIN_KERNEL:
875 if (!is_root) {
876 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
877 goto error;
878 }
879
880 /* Consumer is in an ERROR state. Report back to client */
881 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
882 ret = LTTNG_ERR_NO_KERNCONSUMERD;
883 goto error;
884 }
885
886 /* Need a session for kernel command */
887 if (need_tracing_session) {
888 if (cmd_ctx->session->kernel_session == NULL) {
889 ret = create_kernel_session(cmd_ctx->session);
51630bd8 890 if (ret != LTTNG_OK) {
917a718d
JG
891 ret = LTTNG_ERR_KERN_SESS_FAIL;
892 goto error;
893 }
894 }
895
896 /* Start the kernel consumer daemon */
897 pthread_mutex_lock(&kconsumer_data.pid_mutex);
898 if (kconsumer_data.pid == 0 &&
899 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
900 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
901 ret = start_consumerd(&kconsumer_data);
902 if (ret < 0) {
903 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
904 goto error;
905 }
906 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
907 } else {
908 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
909 }
910
911 /*
912 * The consumer was just spawned so we need to add the socket to
913 * the consumer output of the session if exist.
914 */
915 ret = consumer_create_socket(&kconsumer_data,
916 cmd_ctx->session->kernel_session->consumer);
917 if (ret < 0) {
918 goto error;
919 }
920 }
921
922 break;
923 case LTTNG_DOMAIN_JUL:
924 case LTTNG_DOMAIN_LOG4J:
925 case LTTNG_DOMAIN_PYTHON:
926 case LTTNG_DOMAIN_UST:
927 {
928 if (!ust_app_supported()) {
929 ret = LTTNG_ERR_NO_UST;
930 goto error;
931 }
932 /* Consumer is in an ERROR state. Report back to client */
933 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
934 ret = LTTNG_ERR_NO_USTCONSUMERD;
935 goto error;
936 }
937
938 if (need_tracing_session) {
939 /* Create UST session if none exist. */
940 if (cmd_ctx->session->ust_session == NULL) {
941 ret = create_ust_session(cmd_ctx->session,
942 &cmd_ctx->lsm->domain);
943 if (ret != LTTNG_OK) {
944 goto error;
945 }
946 }
947
948 /* Start the UST consumer daemons */
949 /* 64-bit */
950 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
951 if (config.consumerd64_bin_path.value &&
952 ustconsumer64_data.pid == 0 &&
953 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
954 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
955 ret = start_consumerd(&ustconsumer64_data);
956 if (ret < 0) {
957 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
958 uatomic_set(&ust_consumerd64_fd, -EINVAL);
959 goto error;
960 }
961
962 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
963 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
964 } else {
965 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
966 }
967
968 /*
969 * Setup socket for consumer 64 bit. No need for atomic access
970 * since it was set above and can ONLY be set in this thread.
971 */
972 ret = consumer_create_socket(&ustconsumer64_data,
973 cmd_ctx->session->ust_session->consumer);
974 if (ret < 0) {
975 goto error;
976 }
977
978 /* 32-bit */
979 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
980 if (config.consumerd32_bin_path.value &&
981 ustconsumer32_data.pid == 0 &&
982 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
983 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
984 ret = start_consumerd(&ustconsumer32_data);
985 if (ret < 0) {
986 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
987 uatomic_set(&ust_consumerd32_fd, -EINVAL);
988 goto error;
989 }
990
991 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
992 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
993 } else {
994 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
995 }
996
997 /*
998 * Setup socket for consumer 32 bit. No need for atomic access
999 * since it was set above and can ONLY be set in this thread.
1000 */
1001 ret = consumer_create_socket(&ustconsumer32_data,
1002 cmd_ctx->session->ust_session->consumer);
1003 if (ret < 0) {
1004 goto error;
1005 }
1006 }
1007 break;
1008 }
1009 default:
1010 break;
1011 }
1012skip_domain:
1013
1014 /* Validate consumer daemon state when start/stop trace command */
1015 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
1016 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
1017 switch (cmd_ctx->lsm->domain.type) {
1018 case LTTNG_DOMAIN_NONE:
1019 break;
1020 case LTTNG_DOMAIN_JUL:
1021 case LTTNG_DOMAIN_LOG4J:
1022 case LTTNG_DOMAIN_PYTHON:
1023 case LTTNG_DOMAIN_UST:
1024 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
1025 ret = LTTNG_ERR_NO_USTCONSUMERD;
1026 goto error;
1027 }
1028 break;
1029 case LTTNG_DOMAIN_KERNEL:
1030 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
1031 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1032 goto error;
1033 }
1034 break;
1035 default:
1036 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1037 goto error;
1038 }
1039 }
1040
1041 /*
1042 * Check that the UID or GID match that of the tracing session.
1043 * The root user can interact with all sessions.
1044 */
1045 if (need_tracing_session) {
1046 if (!session_access_ok(cmd_ctx->session,
1047 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1048 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) ||
1049 cmd_ctx->session->destroyed) {
1050 ret = LTTNG_ERR_EPERM;
1051 goto error;
1052 }
1053 }
1054
1055 /*
1056 * Send relayd information to consumer as soon as we have a domain and a
1057 * session defined.
1058 */
1059 if (cmd_ctx->session && need_domain) {
1060 /*
1061 * Setup relayd if not done yet. If the relayd information was already
1062 * sent to the consumer, this call will gracefully return.
1063 */
1064 ret = cmd_setup_relayd(cmd_ctx->session);
1065 if (ret != LTTNG_OK) {
1066 goto error;
1067 }
1068 }
1069
1070 /* Process by command type */
1071 switch (cmd_ctx->lsm->cmd_type) {
1072 case LTTNG_ADD_CONTEXT:
1073 {
1074 /*
1075 * An LTTNG_ADD_CONTEXT command might have a supplementary
1076 * payload if the context being added is an application context.
1077 */
1078 if (cmd_ctx->lsm->u.context.ctx.ctx ==
1079 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1080 char *provider_name = NULL, *context_name = NULL;
1081 size_t provider_name_len =
1082 cmd_ctx->lsm->u.context.provider_name_len;
1083 size_t context_name_len =
1084 cmd_ctx->lsm->u.context.context_name_len;
1085
1086 if (provider_name_len == 0 || context_name_len == 0) {
1087 /*
1088 * Application provider and context names MUST
1089 * be provided.
1090 */
1091 ret = -LTTNG_ERR_INVALID;
1092 goto error;
1093 }
1094
1095 provider_name = zmalloc(provider_name_len + 1);
1096 if (!provider_name) {
1097 ret = -LTTNG_ERR_NOMEM;
1098 goto error;
1099 }
1100 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
1101 provider_name;
1102
1103 context_name = zmalloc(context_name_len + 1);
1104 if (!context_name) {
1105 ret = -LTTNG_ERR_NOMEM;
1106 goto error_add_context;
1107 }
1108 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
1109 context_name;
1110
1111 ret = lttcomm_recv_unix_sock(sock, provider_name,
1112 provider_name_len);
1113 if (ret < 0) {
1114 goto error_add_context;
1115 }
1116
1117 ret = lttcomm_recv_unix_sock(sock, context_name,
1118 context_name_len);
1119 if (ret < 0) {
1120 goto error_add_context;
1121 }
1122 }
1123
1124 /*
1125 * cmd_add_context assumes ownership of the provider and context
1126 * names.
1127 */
1128 ret = cmd_add_context(cmd_ctx->session,
1129 cmd_ctx->lsm->domain.type,
1130 cmd_ctx->lsm->u.context.channel_name,
1131 &cmd_ctx->lsm->u.context.ctx,
1132 kernel_poll_pipe[1]);
1133
1134 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
1135 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
1136error_add_context:
1137 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
1138 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
1139 if (ret < 0) {
1140 goto error;
1141 }
1142 break;
1143 }
1144 case LTTNG_DISABLE_CHANNEL:
1145 {
1146 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
1147 cmd_ctx->lsm->u.disable.channel_name);
1148 break;
1149 }
1150 case LTTNG_DISABLE_EVENT:
1151 {
1152
1153 /*
1154 * FIXME: handle filter; for now we just receive the filter's
1155 * bytecode along with the filter expression which are sent by
1156 * liblttng-ctl and discard them.
1157 *
1158 * This fixes an issue where the client may block while sending
1159 * the filter payload and encounter an error because the session
1160 * daemon closes the socket without ever handling this data.
1161 */
1162 size_t count = cmd_ctx->lsm->u.disable.expression_len +
1163 cmd_ctx->lsm->u.disable.bytecode_len;
1164
1165 if (count) {
1166 char data[LTTNG_FILTER_MAX_LEN];
1167
1168 DBG("Discarding disable event command payload of size %zu", count);
1169 while (count) {
1170 ret = lttcomm_recv_unix_sock(sock, data,
1171 count > sizeof(data) ? sizeof(data) : count);
1172 if (ret < 0) {
1173 goto error;
1174 }
1175
1176 count -= (size_t) ret;
1177 }
1178 }
1179 /* FIXME: passing packed structure to non-packed pointer */
1180 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
1181 cmd_ctx->lsm->u.disable.channel_name,
1182 &cmd_ctx->lsm->u.disable.event);
1183 break;
1184 }
1185 case LTTNG_ENABLE_CHANNEL:
1186 {
1187 cmd_ctx->lsm->u.channel.chan.attr.extended.ptr =
1188 (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended;
1189 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
1190 &cmd_ctx->lsm->u.channel.chan,
1191 kernel_poll_pipe[1]);
1192 break;
1193 }
1194 case LTTNG_TRACK_PID:
1195 {
1196 ret = cmd_track_pid(cmd_ctx->session,
1197 cmd_ctx->lsm->domain.type,
1198 cmd_ctx->lsm->u.pid_tracker.pid);
1199 break;
1200 }
1201 case LTTNG_UNTRACK_PID:
1202 {
1203 ret = cmd_untrack_pid(cmd_ctx->session,
1204 cmd_ctx->lsm->domain.type,
1205 cmd_ctx->lsm->u.pid_tracker.pid);
1206 break;
1207 }
1208 case LTTNG_ENABLE_EVENT:
1209 {
1210 struct lttng_event *ev = NULL;
1211 struct lttng_event_exclusion *exclusion = NULL;
1212 struct lttng_filter_bytecode *bytecode = NULL;
1213 char *filter_expression = NULL;
1214
1215 /* Handle exclusion events and receive it from the client. */
1216 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
1217 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
1218
1219 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
1220 (count * LTTNG_SYMBOL_NAME_LEN));
1221 if (!exclusion) {
1222 ret = LTTNG_ERR_EXCLUSION_NOMEM;
1223 goto error;
1224 }
1225
1226 DBG("Receiving var len exclusion event list from client ...");
1227 exclusion->count = count;
1228 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
1229 count * LTTNG_SYMBOL_NAME_LEN);
1230 if (ret <= 0) {
1231 DBG("Nothing recv() from client var len data... continuing");
1232 *sock_error = 1;
1233 free(exclusion);
1234 ret = LTTNG_ERR_EXCLUSION_INVAL;
1235 goto error;
1236 }
1237 }
1238
1239 /* Get filter expression from client. */
1240 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
1241 size_t expression_len =
1242 cmd_ctx->lsm->u.enable.expression_len;
1243
1244 if (expression_len > LTTNG_FILTER_MAX_LEN) {
1245 ret = LTTNG_ERR_FILTER_INVAL;
1246 free(exclusion);
1247 goto error;
1248 }
1249
1250 filter_expression = zmalloc(expression_len);
1251 if (!filter_expression) {
1252 free(exclusion);
1253 ret = LTTNG_ERR_FILTER_NOMEM;
1254 goto error;
1255 }
1256
1257 /* Receive var. len. data */
1258 DBG("Receiving var len filter's expression from client ...");
1259 ret = lttcomm_recv_unix_sock(sock, filter_expression,
1260 expression_len);
1261 if (ret <= 0) {
1262 DBG("Nothing recv() from client var len data... continuing");
1263 *sock_error = 1;
1264 free(filter_expression);
1265 free(exclusion);
1266 ret = LTTNG_ERR_FILTER_INVAL;
1267 goto error;
1268 }
1269 }
1270
1271 /* Handle filter and get bytecode from client. */
1272 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
1273 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
1274
1275 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1276 ret = LTTNG_ERR_FILTER_INVAL;
1277 free(filter_expression);
1278 free(exclusion);
1279 goto error;
1280 }
1281
1282 bytecode = zmalloc(bytecode_len);
1283 if (!bytecode) {
1284 free(filter_expression);
1285 free(exclusion);
1286 ret = LTTNG_ERR_FILTER_NOMEM;
1287 goto error;
1288 }
1289
1290 /* Receive var. len. data */
1291 DBG("Receiving var len filter's bytecode from client ...");
1292 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
1293 if (ret <= 0) {
1294 DBG("Nothing recv() from client var len data... continuing");
1295 *sock_error = 1;
1296 free(filter_expression);
1297 free(bytecode);
1298 free(exclusion);
1299 ret = LTTNG_ERR_FILTER_INVAL;
1300 goto error;
1301 }
1302
1303 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
1304 free(filter_expression);
1305 free(bytecode);
1306 free(exclusion);
1307 ret = LTTNG_ERR_FILTER_INVAL;
1308 goto error;
1309 }
1310 }
1311
1312 ev = lttng_event_copy(&cmd_ctx->lsm->u.enable.event);
1313 if (!ev) {
1314 DBG("Failed to copy event: %s",
1315 cmd_ctx->lsm->u.enable.event.name);
1316 free(filter_expression);
1317 free(bytecode);
1318 free(exclusion);
1319 ret = LTTNG_ERR_NOMEM;
1320 goto error;
1321 }
1322
1323
1324 if (cmd_ctx->lsm->u.enable.userspace_probe_location_len > 0) {
1325 /* Expect a userspace probe description. */
1326 ret = receive_userspace_probe(cmd_ctx, sock, sock_error, ev);
1327 if (ret) {
1328 free(filter_expression);
1329 free(bytecode);
1330 free(exclusion);
1331 lttng_event_destroy(ev);
1332 goto error;
1333 }
1334 }
1335
1336 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
1337 cmd_ctx->lsm->u.enable.channel_name,
1338 ev,
1339 filter_expression, bytecode, exclusion,
1340 kernel_poll_pipe[1]);
1341 lttng_event_destroy(ev);
1342 break;
1343 }
1344 case LTTNG_LIST_TRACEPOINTS:
1345 {
1346 struct lttng_event *events;
1347 ssize_t nb_events;
1348
1349 session_lock_list();
1350 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
1351 session_unlock_list();
1352 if (nb_events < 0) {
1353 /* Return value is a negative lttng_error_code. */
1354 ret = -nb_events;
1355 goto error;
1356 }
1357
1358 /*
1359 * Setup lttng message with payload size set to the event list size in
1360 * bytes and then copy list into the llm payload.
1361 */
1362 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1363 sizeof(struct lttng_event) * nb_events);
1364 free(events);
1365
1366 if (ret < 0) {
1367 goto setup_error;
1368 }
1369
1370 ret = LTTNG_OK;
1371 break;
1372 }
1373 case LTTNG_LIST_TRACEPOINT_FIELDS:
1374 {
1375 struct lttng_event_field *fields;
1376 ssize_t nb_fields;
1377
1378 session_lock_list();
1379 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
1380 &fields);
1381 session_unlock_list();
1382 if (nb_fields < 0) {
1383 /* Return value is a negative lttng_error_code. */
1384 ret = -nb_fields;
1385 goto error;
1386 }
1387
1388 /*
1389 * Setup lttng message with payload size set to the event list size in
1390 * bytes and then copy list into the llm payload.
1391 */
1392 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
1393 sizeof(struct lttng_event_field) * nb_fields);
1394 free(fields);
1395
1396 if (ret < 0) {
1397 goto setup_error;
1398 }
1399
1400 ret = LTTNG_OK;
1401 break;
1402 }
1403 case LTTNG_LIST_SYSCALLS:
1404 {
1405 struct lttng_event *events;
1406 ssize_t nb_events;
1407
1408 nb_events = cmd_list_syscalls(&events);
1409 if (nb_events < 0) {
1410 /* Return value is a negative lttng_error_code. */
1411 ret = -nb_events;
1412 goto error;
1413 }
1414
1415 /*
1416 * Setup lttng message with payload size set to the event list size in
1417 * bytes and then copy list into the llm payload.
1418 */
1419 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1420 sizeof(struct lttng_event) * nb_events);
1421 free(events);
1422
1423 if (ret < 0) {
1424 goto setup_error;
1425 }
1426
1427 ret = LTTNG_OK;
1428 break;
1429 }
1430 case LTTNG_LIST_TRACKER_PIDS:
1431 {
1432 int32_t *pids = NULL;
1433 ssize_t nr_pids;
1434
1435 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
1436 cmd_ctx->lsm->domain.type, &pids);
1437 if (nr_pids < 0) {
1438 /* Return value is a negative lttng_error_code. */
1439 ret = -nr_pids;
1440 goto error;
1441 }
1442
1443 /*
1444 * Setup lttng message with payload size set to the event list size in
1445 * bytes and then copy list into the llm payload.
1446 */
1447 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
1448 sizeof(int32_t) * nr_pids);
1449 free(pids);
1450
1451 if (ret < 0) {
1452 goto setup_error;
1453 }
1454
1455 ret = LTTNG_OK;
1456 break;
1457 }
1458 case LTTNG_SET_CONSUMER_URI:
1459 {
1460 size_t nb_uri, len;
1461 struct lttng_uri *uris;
1462
1463 nb_uri = cmd_ctx->lsm->u.uri.size;
1464 len = nb_uri * sizeof(struct lttng_uri);
1465
1466 if (nb_uri == 0) {
1467 ret = LTTNG_ERR_INVALID;
1468 goto error;
1469 }
1470
1471 uris = zmalloc(len);
1472 if (uris == NULL) {
1473 ret = LTTNG_ERR_FATAL;
1474 goto error;
1475 }
1476
1477 /* Receive variable len data */
1478 DBG("Receiving %zu URI(s) from client ...", nb_uri);
1479 ret = lttcomm_recv_unix_sock(sock, uris, len);
1480 if (ret <= 0) {
1481 DBG("No URIs received from client... continuing");
1482 *sock_error = 1;
1483 ret = LTTNG_ERR_SESSION_FAIL;
1484 free(uris);
1485 goto error;
1486 }
1487
1488 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
1489 free(uris);
1490 if (ret != LTTNG_OK) {
1491 goto error;
1492 }
1493
1494
1495 break;
1496 }
1497 case LTTNG_START_TRACE:
1498 {
1499 /*
1500 * On the first start, if we have a kernel session and we have
1501 * enabled time or size-based rotations, we have to make sure
1502 * the kernel tracer supports it.
1503 */
1504 if (!cmd_ctx->session->has_been_started && \
1505 cmd_ctx->session->kernel_session && \
1506 (cmd_ctx->session->rotate_timer_period || \
1507 cmd_ctx->session->rotate_size) && \
1508 !check_rotate_compatible()) {
1509 DBG("Kernel tracer version is not compatible with the rotation feature");
1510 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1511 goto error;
1512 }
1513 ret = cmd_start_trace(cmd_ctx->session);
1514 break;
1515 }
1516 case LTTNG_STOP_TRACE:
1517 {
1518 ret = cmd_stop_trace(cmd_ctx->session);
1519 break;
1520 }
917a718d
JG
1521 case LTTNG_DESTROY_SESSION:
1522 {
1523 ret = cmd_destroy_session(cmd_ctx->session,
1524 notification_thread_handle);
1525 break;
1526 }
1527 case LTTNG_LIST_DOMAINS:
1528 {
1529 ssize_t nb_dom;
1530 struct lttng_domain *domains = NULL;
1531
1532 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
1533 if (nb_dom < 0) {
1534 /* Return value is a negative lttng_error_code. */
1535 ret = -nb_dom;
1536 goto error;
1537 }
1538
1539 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
1540 nb_dom * sizeof(struct lttng_domain));
1541 free(domains);
1542
1543 if (ret < 0) {
1544 goto setup_error;
1545 }
1546
1547 ret = LTTNG_OK;
1548 break;
1549 }
1550 case LTTNG_LIST_CHANNELS:
1551 {
1552 ssize_t payload_size;
1553 struct lttng_channel *channels = NULL;
1554
1555 payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type,
1556 cmd_ctx->session, &channels);
1557 if (payload_size < 0) {
1558 /* Return value is a negative lttng_error_code. */
1559 ret = -payload_size;
1560 goto error;
1561 }
1562
1563 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
1564 payload_size);
1565 free(channels);
1566
1567 if (ret < 0) {
1568 goto setup_error;
1569 }
1570
1571 ret = LTTNG_OK;
1572 break;
1573 }
1574 case LTTNG_LIST_EVENTS:
1575 {
1576 ssize_t nb_event;
1577 struct lttng_event *events = NULL;
1578 struct lttcomm_event_command_header cmd_header;
1579 size_t total_size;
1580
1581 memset(&cmd_header, 0, sizeof(cmd_header));
1582 /* Extended infos are included at the end of events */
1583 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type,
1584 cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name,
1585 &events, &total_size);
1586
1587 if (nb_event < 0) {
1588 /* Return value is a negative lttng_error_code. */
1589 ret = -nb_event;
1590 goto error;
1591 }
1592
1593 cmd_header.nb_events = nb_event;
1594 ret = setup_lttng_msg(cmd_ctx, events, total_size,
1595 &cmd_header, sizeof(cmd_header));
1596 free(events);
1597
1598 if (ret < 0) {
1599 goto setup_error;
1600 }
1601
1602 ret = LTTNG_OK;
1603 break;
1604 }
1605 case LTTNG_LIST_SESSIONS:
1606 {
1607 unsigned int nr_sessions;
1608 void *sessions_payload;
1609 size_t payload_len;
1610
1611 session_lock_list();
1612 nr_sessions = lttng_sessions_count(
1613 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1614 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
b178f53e
JG
1615
1616 payload_len = (sizeof(struct lttng_session) * nr_sessions) +
1617 (sizeof(struct lttng_session_extended) * nr_sessions);
917a718d
JG
1618 sessions_payload = zmalloc(payload_len);
1619
1620 if (!sessions_payload) {
1621 session_unlock_list();
1622 ret = -ENOMEM;
1623 goto setup_error;
1624 }
1625
b178f53e 1626 cmd_list_lttng_sessions(sessions_payload, nr_sessions,
917a718d
JG
1627 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1628 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1629 session_unlock_list();
1630
1631 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
1632 payload_len);
1633 free(sessions_payload);
1634
1635 if (ret < 0) {
1636 goto setup_error;
1637 }
1638
1639 ret = LTTNG_OK;
1640 break;
1641 }
1642 case LTTNG_REGISTER_CONSUMER:
1643 {
1644 struct consumer_data *cdata;
1645
1646 switch (cmd_ctx->lsm->domain.type) {
1647 case LTTNG_DOMAIN_KERNEL:
1648 cdata = &kconsumer_data;
1649 break;
1650 default:
1651 ret = LTTNG_ERR_UND;
1652 goto error;
1653 }
1654
1655 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
1656 cmd_ctx->lsm->u.reg.path, cdata);
1657 break;
1658 }
1659 case LTTNG_DATA_PENDING:
1660 {
1661 int pending_ret;
1662 uint8_t pending_ret_byte;
1663
1664 pending_ret = cmd_data_pending(cmd_ctx->session);
1665
1666 /*
1667 * FIXME
1668 *
1669 * This function may returns 0 or 1 to indicate whether or not
1670 * there is data pending. In case of error, it should return an
1671 * LTTNG_ERR code. However, some code paths may still return
1672 * a nondescript error code, which we handle by returning an
1673 * "unknown" error.
1674 */
1675 if (pending_ret == 0 || pending_ret == 1) {
1676 /*
1677 * ret will be set to LTTNG_OK at the end of
1678 * this function.
1679 */
1680 } else if (pending_ret < 0) {
1681 ret = LTTNG_ERR_UNK;
1682 goto setup_error;
1683 } else {
1684 ret = pending_ret;
1685 goto setup_error;
1686 }
1687
1688 pending_ret_byte = (uint8_t) pending_ret;
1689
1690 /* 1 byte to return whether or not data is pending */
1691 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1692 &pending_ret_byte, 1);
1693
1694 if (ret < 0) {
1695 goto setup_error;
1696 }
1697
1698 ret = LTTNG_OK;
1699 break;
1700 }
1701 case LTTNG_SNAPSHOT_ADD_OUTPUT:
1702 {
1703 struct lttcomm_lttng_output_id reply;
1704
1705 ret = cmd_snapshot_add_output(cmd_ctx->session,
1706 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
1707 if (ret != LTTNG_OK) {
1708 goto error;
1709 }
1710
1711 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
1712 sizeof(reply));
1713 if (ret < 0) {
1714 goto setup_error;
1715 }
1716
1717 /* Copy output list into message payload */
1718 ret = LTTNG_OK;
1719 break;
1720 }
1721 case LTTNG_SNAPSHOT_DEL_OUTPUT:
1722 {
1723 ret = cmd_snapshot_del_output(cmd_ctx->session,
1724 &cmd_ctx->lsm->u.snapshot_output.output);
1725 break;
1726 }
1727 case LTTNG_SNAPSHOT_LIST_OUTPUT:
1728 {
1729 ssize_t nb_output;
1730 struct lttng_snapshot_output *outputs = NULL;
1731
1732 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
1733 if (nb_output < 0) {
1734 ret = -nb_output;
1735 goto error;
1736 }
1737
1738 assert((nb_output > 0 && outputs) || nb_output == 0);
1739 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
1740 nb_output * sizeof(struct lttng_snapshot_output));
1741 free(outputs);
1742
1743 if (ret < 0) {
1744 goto setup_error;
1745 }
1746
1747 ret = LTTNG_OK;
1748 break;
1749 }
1750 case LTTNG_SNAPSHOT_RECORD:
1751 {
1752 ret = cmd_snapshot_record(cmd_ctx->session,
1753 &cmd_ctx->lsm->u.snapshot_record.output,
1754 cmd_ctx->lsm->u.snapshot_record.wait);
1755 break;
1756 }
b178f53e 1757 case LTTNG_CREATE_SESSION_EXT:
917a718d 1758 {
b178f53e
JG
1759 struct lttng_dynamic_buffer payload;
1760 struct lttng_session_descriptor *return_descriptor = NULL;
917a718d 1761
b178f53e
JG
1762 lttng_dynamic_buffer_init(&payload);
1763 ret = cmd_create_session(cmd_ctx, sock, &return_descriptor);
1764 if (ret != LTTNG_OK) {
1765 goto error;
917a718d
JG
1766 }
1767
b178f53e
JG
1768 ret = lttng_session_descriptor_serialize(return_descriptor,
1769 &payload);
1770 if (ret) {
1771 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1772 lttng_session_descriptor_destroy(return_descriptor);
1773 ret = LTTNG_ERR_NOMEM;
1774 goto error;
917a718d 1775 }
b178f53e
JG
1776 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data,
1777 payload.size);
1778 if (ret) {
1779 lttng_session_descriptor_destroy(return_descriptor);
1780 ret = LTTNG_ERR_NOMEM;
1781 goto error;
1782 }
1783 lttng_dynamic_buffer_reset(&payload);
1784 lttng_session_descriptor_destroy(return_descriptor);
1785 ret = LTTNG_OK;
917a718d
JG
1786 break;
1787 }
1788 case LTTNG_SAVE_SESSION:
1789 {
1790 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
1791 &cmd_ctx->creds);
1792 break;
1793 }
1794 case LTTNG_SET_SESSION_SHM_PATH:
1795 {
1796 ret = cmd_set_session_shm_path(cmd_ctx->session,
1797 cmd_ctx->lsm->u.set_shm_path.shm_path);
1798 break;
1799 }
1800 case LTTNG_REGENERATE_METADATA:
1801 {
1802 ret = cmd_regenerate_metadata(cmd_ctx->session);
1803 break;
1804 }
1805 case LTTNG_REGENERATE_STATEDUMP:
1806 {
1807 ret = cmd_regenerate_statedump(cmd_ctx->session);
1808 break;
1809 }
1810 case LTTNG_REGISTER_TRIGGER:
1811 {
1812 ret = cmd_register_trigger(cmd_ctx, sock,
1813 notification_thread_handle);
1814 break;
1815 }
1816 case LTTNG_UNREGISTER_TRIGGER:
1817 {
1818 ret = cmd_unregister_trigger(cmd_ctx, sock,
1819 notification_thread_handle);
1820 break;
1821 }
1822 case LTTNG_ROTATE_SESSION:
1823 {
1824 struct lttng_rotate_session_return rotate_return;
1825
1826 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
1827
1828 memset(&rotate_return, 0, sizeof(rotate_return));
1829 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
1830 DBG("Kernel tracer version is not compatible with the rotation feature");
1831 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1832 goto error;
1833 }
1834
1835 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return);
1836 if (ret < 0) {
1837 ret = -ret;
1838 goto error;
1839 }
1840
1841 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
1842 sizeof(rotate_return));
1843 if (ret < 0) {
1844 ret = -ret;
1845 goto error;
1846 }
1847
1848 ret = LTTNG_OK;
1849 break;
1850 }
1851 case LTTNG_ROTATION_GET_INFO:
1852 {
1853 struct lttng_rotation_get_info_return get_info_return;
1854
1855 memset(&get_info_return, 0, sizeof(get_info_return));
1856 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
1857 cmd_ctx->lsm->u.get_rotation_info.rotation_id);
1858 if (ret < 0) {
1859 ret = -ret;
1860 goto error;
1861 }
1862
1863 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
1864 sizeof(get_info_return));
1865 if (ret < 0) {
1866 ret = -ret;
1867 goto error;
1868 }
1869
1870 ret = LTTNG_OK;
1871 break;
1872 }
1873 case LTTNG_ROTATION_SET_SCHEDULE:
1874 {
1875 bool set_schedule;
1876 enum lttng_rotation_schedule_type schedule_type;
1877 uint64_t value;
1878
1879 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
1880 DBG("Kernel tracer version does not support session rotations");
1881 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1882 goto error;
1883 }
1884
1885 set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1;
1886 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type;
1887 value = cmd_ctx->lsm->u.rotation_set_schedule.value;
1888
1889 ret = cmd_rotation_set_schedule(cmd_ctx->session,
1890 set_schedule,
1891 schedule_type,
1892 value,
1893 notification_thread_handle);
1894 if (ret != LTTNG_OK) {
1895 goto error;
1896 }
1897
1898 break;
1899 }
1900 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
1901 {
1902 struct lttng_session_list_schedules_return schedules = {
1903 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
1904 .periodic.value = cmd_ctx->session->rotate_timer_period,
1905 .size.set = !!cmd_ctx->session->rotate_size,
1906 .size.value = cmd_ctx->session->rotate_size,
1907 };
1908
1909 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
1910 sizeof(schedules));
1911 if (ret < 0) {
1912 ret = -ret;
1913 goto error;
1914 }
1915
1916 ret = LTTNG_OK;
1917 break;
1918 }
1919 default:
1920 ret = LTTNG_ERR_UND;
1921 break;
1922 }
1923
1924error:
1925 if (cmd_ctx->llm == NULL) {
1926 DBG("Missing llm structure. Allocating one.");
1927 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
1928 goto setup_error;
1929 }
1930 }
1931 /* Set return code */
1932 cmd_ctx->llm->ret_code = ret;
1933setup_error:
1934 if (cmd_ctx->session) {
1935 session_unlock(cmd_ctx->session);
1936 session_put(cmd_ctx->session);
1937 }
1938 if (need_tracing_session) {
1939 session_unlock_list();
1940 }
1941init_setup_error:
1942 assert(!rcu_read_ongoing());
1943 return ret;
1944}
1945
1946static int create_client_sock(void)
1947{
1948 int ret, client_sock;
1949 const mode_t old_umask = umask(0);
1950
1951 /* Create client tool unix socket */
1952 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
1953 if (client_sock < 0) {
1954 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
1955 ret = -1;
1956 goto end;
1957 }
1958
1959 /* Set the cloexec flag */
1960 ret = utils_set_fd_cloexec(client_sock);
1961 if (ret < 0) {
1962 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1963 "Continuing but note that the consumer daemon will have a "
1964 "reference to this socket on exec()", client_sock);
1965 }
1966
1967 /* File permission MUST be 660 */
1968 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1969 if (ret < 0) {
1970 ERR("Set file permissions failed: %s", config.client_unix_sock_path.value);
1971 PERROR("chmod");
1972 goto end;
1973 }
1974 DBG("Created client socket (fd = %i)", client_sock);
1975 ret = client_sock;
1976end:
1977 umask(old_umask);
1978 return ret;
1979}
1980
1981static void cleanup_client_thread(void *data)
1982{
1983 struct lttng_pipe *quit_pipe = data;
1984
1985 lttng_pipe_destroy(quit_pipe);
1986}
1987
6cb45e93
JG
1988static void thread_init_cleanup(void *data)
1989{
1990 set_thread_status(false);
1991}
1992
917a718d
JG
1993/*
1994 * This thread manage all clients request using the unix client socket for
1995 * communication.
1996 */
1997static void *thread_manage_clients(void *data)
1998{
1999 int sock = -1, ret, i, pollfd, err = -1;
2000 int sock_error;
2001 uint32_t revents, nb_fd;
2002 struct command_ctx *cmd_ctx = NULL;
2003 struct lttng_poll_event events;
2004 int client_sock = -1;
2005 struct lttng_pipe *quit_pipe = data;
2006 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
2007
2008 DBG("[thread] Manage client started");
2009
2010 is_root = (getuid() == 0);
2011
6cb45e93 2012 pthread_cleanup_push(thread_init_cleanup, NULL);
917a718d
JG
2013 client_sock = create_client_sock();
2014 if (client_sock < 0) {
2015 goto error_listen;
2016 }
2017
2018 rcu_register_thread();
2019
2020 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
2021
2022 health_code_update();
2023
2024 ret = lttcomm_listen_unix_sock(client_sock);
2025 if (ret < 0) {
2026 goto error_listen;
2027 }
2028
2029 /*
2030 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2031 * more will be added to this poll set.
2032 */
2033 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2034 if (ret < 0) {
2035 goto error_create_poll;
2036 }
2037
2038 /* Add the application registration socket */
2039 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
2040 if (ret < 0) {
2041 goto error;
2042 }
2043
2044 /* Add thread quit pipe */
2045 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
2046 if (ret < 0) {
2047 goto error;
2048 }
2049
6cb45e93
JG
2050 /* Set state as running. */
2051 set_thread_status(true);
2052 pthread_cleanup_pop(0);
2053
917a718d
JG
2054 /* This testpoint is after we signal readiness to the parent. */
2055 if (testpoint(sessiond_thread_manage_clients)) {
2056 goto error;
2057 }
2058
2059 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
2060 goto error;
2061 }
2062
2063 health_code_update();
2064
917a718d
JG
2065 while (1) {
2066 const struct cmd_completion_handler *cmd_completion_handler;
2067
2068 DBG("Accepting client command ...");
2069
2070 /* Inifinite blocking call, waiting for transmission */
2071 restart:
2072 health_poll_entry();
2073 ret = lttng_poll_wait(&events, -1);
2074 health_poll_exit();
2075 if (ret < 0) {
2076 /*
2077 * Restart interrupted system call.
2078 */
2079 if (errno == EINTR) {
2080 goto restart;
2081 }
2082 goto error;
2083 }
2084
2085 nb_fd = ret;
2086
2087 for (i = 0; i < nb_fd; i++) {
2088 revents = LTTNG_POLL_GETEV(&events, i);
2089 pollfd = LTTNG_POLL_GETFD(&events, i);
2090
2091 health_code_update();
2092
917a718d
JG
2093 if (pollfd == thread_quit_pipe_fd) {
2094 err = 0;
2095 goto exit;
2096 } else {
2097 /* Event on the registration socket */
2098 if (revents & LPOLLIN) {
2099 continue;
2100 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2101 ERR("Client socket poll error");
2102 goto error;
2103 } else {
2104 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2105 goto error;
2106 }
2107 }
2108 }
2109
2110 DBG("Wait for client response");
2111
2112 health_code_update();
2113
2114 sock = lttcomm_accept_unix_sock(client_sock);
2115 if (sock < 0) {
2116 goto error;
2117 }
2118
2119 /*
2120 * Set the CLOEXEC flag. Return code is useless because either way, the
2121 * show must go on.
2122 */
2123 (void) utils_set_fd_cloexec(sock);
2124
2125 /* Set socket option for credentials retrieval */
2126 ret = lttcomm_setsockopt_creds_unix_sock(sock);
2127 if (ret < 0) {
2128 goto error;
2129 }
2130
2131 /* Allocate context command to process the client request */
2132 cmd_ctx = zmalloc(sizeof(struct command_ctx));
2133 if (cmd_ctx == NULL) {
2134 PERROR("zmalloc cmd_ctx");
2135 goto error;
2136 }
2137
2138 /* Allocate data buffer for reception */
2139 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
2140 if (cmd_ctx->lsm == NULL) {
2141 PERROR("zmalloc cmd_ctx->lsm");
2142 goto error;
2143 }
2144
2145 cmd_ctx->llm = NULL;
2146 cmd_ctx->session = NULL;
2147
2148 health_code_update();
2149
2150 /*
2151 * Data is received from the lttng client. The struct
2152 * lttcomm_session_msg (lsm) contains the command and data request of
2153 * the client.
2154 */
2155 DBG("Receiving data from client ...");
2156 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
2157 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
2158 if (ret <= 0) {
2159 DBG("Nothing recv() from client... continuing");
2160 ret = close(sock);
2161 if (ret) {
2162 PERROR("close");
2163 }
2164 sock = -1;
2165 clean_command_ctx(&cmd_ctx);
2166 continue;
2167 }
2168
2169 health_code_update();
2170
2171 // TODO: Validate cmd_ctx including sanity check for
2172 // security purpose.
2173
2174 rcu_thread_online();
2175 /*
2176 * This function dispatch the work to the kernel or userspace tracer
2177 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2178 * informations for the client. The command context struct contains
2179 * everything this function may needs.
2180 */
2181 ret = process_client_msg(cmd_ctx, sock, &sock_error);
2182 rcu_thread_offline();
2183 if (ret < 0) {
2184 ret = close(sock);
2185 if (ret) {
2186 PERROR("close");
2187 }
2188 sock = -1;
2189 /*
2190 * TODO: Inform client somehow of the fatal error. At
2191 * this point, ret < 0 means that a zmalloc failed
2192 * (ENOMEM). Error detected but still accept
2193 * command, unless a socket error has been
2194 * detected.
2195 */
2196 clean_command_ctx(&cmd_ctx);
2197 continue;
2198 }
2199
2200 cmd_completion_handler = cmd_pop_completion_handler();
2201 if (cmd_completion_handler) {
2202 enum lttng_error_code completion_code;
2203
2204 completion_code = cmd_completion_handler->run(
2205 cmd_completion_handler->data);
2206 if (completion_code != LTTNG_OK) {
2207 clean_command_ctx(&cmd_ctx);
2208 continue;
2209 }
2210 }
2211
2212 health_code_update();
2213
2214 DBG("Sending response (size: %d, retcode: %s (%d))",
2215 cmd_ctx->lttng_msg_size,
2216 lttng_strerror(-cmd_ctx->llm->ret_code),
2217 cmd_ctx->llm->ret_code);
2218 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2219 if (ret < 0) {
2220 ERR("Failed to send data back to client");
2221 }
2222
2223 /* End of transmission */
2224 ret = close(sock);
2225 if (ret) {
2226 PERROR("close");
2227 }
2228 sock = -1;
2229
2230 clean_command_ctx(&cmd_ctx);
2231
2232 health_code_update();
2233 }
2234
2235exit:
2236error:
2237 if (sock >= 0) {
2238 ret = close(sock);
2239 if (ret) {
2240 PERROR("close");
2241 }
2242 }
2243
2244 lttng_poll_clean(&events);
2245 clean_command_ctx(&cmd_ctx);
2246
2247error_listen:
2248error_create_poll:
2249 unlink(config.client_unix_sock_path.value);
2250 if (client_sock >= 0) {
2251 ret = close(client_sock);
2252 if (ret) {
2253 PERROR("close");
2254 }
2255 }
2256
2257 if (err) {
2258 health_error();
2259 ERR("Health error occurred in %s", __func__);
2260 }
2261
2262 health_unregister(health_sessiond);
2263
2264 DBG("Client thread dying");
2265
2266 rcu_unregister_thread();
917a718d
JG
2267 return NULL;
2268}
2269
2270static
2271bool shutdown_client_thread(void *thread_data)
2272{
2273 struct lttng_pipe *client_quit_pipe = thread_data;
2274 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2275
2276 return notify_thread_pipe(write_fd) == 1;
2277}
2278
2279struct lttng_thread *launch_client_thread(void)
2280{
6cb45e93 2281 bool thread_running;
917a718d
JG
2282 struct lttng_pipe *client_quit_pipe;
2283 struct lttng_thread *thread;
2284
6cb45e93 2285 sem_init(&thread_state.ready, 0, 0);
917a718d
JG
2286 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2287 if (!client_quit_pipe) {
2288 goto error;
2289 }
2290
2291 thread = lttng_thread_create("Client management",
2292 thread_manage_clients,
2293 shutdown_client_thread,
2294 cleanup_client_thread,
2295 client_quit_pipe);
2296 if (!thread) {
2297 goto error;
2298 }
2299
2300 /*
2301 * This thread is part of the threads that need to be fully
2302 * initialized before the session daemon is marked as "ready".
2303 */
6cb45e93
JG
2304 thread_running = wait_thread_status();
2305 if (!thread_running) {
2306 lttng_thread_put(thread);
2307 thread = NULL;
2308 }
917a718d
JG
2309 return thread;
2310error:
2311 cleanup_client_thread(client_quit_pipe);
2312 return NULL;
2313}
This page took 0.114588 seconds and 5 git commands to generate.