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