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