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