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