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