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