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