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