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