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