Clean-up: sessiond: prepend `the_` to global variable names
[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:
779 return ret_code;
780}
781
917a718d
JG
782/*
783 * Version of setup_lttng_msg() without command header.
784 */
785static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
786 void *payload_buf, size_t payload_len)
787{
788 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
789}
790
917a718d
JG
791/*
792 * Check if the current kernel tracer supports the session rotation feature.
793 * Return 1 if it does, 0 otherwise.
794 */
795static int check_rotate_compatible(void)
796{
797 int ret = 1;
798
412d7227
SM
799 if (the_kernel_tracer_version.major != 2 ||
800 the_kernel_tracer_version.minor < 11) {
917a718d
JG
801 DBG("Kernel tracer version is not compatible with the rotation feature");
802 ret = 0;
803 }
804
805 return ret;
806}
807
808/*
809 * Send data on a unix socket using the liblttsessiondcomm API.
810 *
811 * Return lttcomm error code.
812 */
3a91de3a 813static int send_unix_sock(int sock, struct lttng_payload_view *view)
917a718d 814{
3a91de3a 815 int ret;
fe489250 816 const int fd_count = lttng_payload_view_get_fd_handle_count(view);
3a91de3a 817
917a718d 818 /* Check valid length */
3a91de3a
JG
819 if (view->buffer.size == 0) {
820 ret = -1;
821 goto end;
822 }
823
824 ret = lttcomm_send_unix_sock(
825 sock, view->buffer.data, view->buffer.size);
826 if (ret < 0) {
827 goto end;
917a718d
JG
828 }
829
fe489250 830 if (fd_count > 0) {
700741dc
JG
831 ret = lttcomm_send_payload_view_fds_unix_sock(sock, view);
832 if (ret < 0) {
833 goto end;
fe489250 834 }
3a91de3a
JG
835 }
836
837end:
838 return ret;
917a718d
JG
839}
840
841/*
842 * Process the command requested by the lttng client within the command
843 * context structure. This function make sure that the return structure (llm)
844 * is set and ready for transmission before returning.
845 *
846 * Return any error encountered or 0 for success.
847 *
848 * "sock" is only used for special-case var. len data.
3e3665b8
JG
849 * A command may assume the ownership of the socket, in which case its value
850 * should be set to -1.
917a718d
JG
851 *
852 * Should *NOT* be called with RCU read-side lock held.
853 */
3e3665b8 854static int process_client_msg(struct command_ctx *cmd_ctx, int *sock,
917a718d
JG
855 int *sock_error)
856{
857 int ret = LTTNG_OK;
9124c630
JR
858 bool need_tracing_session = true;
859 bool need_domain;
860 bool need_consumerd;
917a718d 861
19f912db
FD
862 DBG("Processing client command '%s\' (%d)",
863 lttcomm_sessiond_command_str(cmd_ctx->lsm.cmd_type),
864 cmd_ctx->lsm.cmd_type);
917a718d
JG
865
866 assert(!rcu_read_ongoing());
867
868 *sock_error = 0;
869
3a91de3a 870 switch (cmd_ctx->lsm.cmd_type) {
b178f53e 871 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
872 case LTTNG_DESTROY_SESSION:
873 case LTTNG_LIST_SESSIONS:
874 case LTTNG_LIST_DOMAINS:
875 case LTTNG_START_TRACE:
876 case LTTNG_STOP_TRACE:
877 case LTTNG_DATA_PENDING:
878 case LTTNG_SNAPSHOT_ADD_OUTPUT:
879 case LTTNG_SNAPSHOT_DEL_OUTPUT:
880 case LTTNG_SNAPSHOT_LIST_OUTPUT:
881 case LTTNG_SNAPSHOT_RECORD:
882 case LTTNG_SAVE_SESSION:
883 case LTTNG_SET_SESSION_SHM_PATH:
884 case LTTNG_REGENERATE_METADATA:
885 case LTTNG_REGENERATE_STATEDUMP:
917a718d
JG
886 case LTTNG_ROTATE_SESSION:
887 case LTTNG_ROTATION_GET_INFO:
888 case LTTNG_ROTATION_SET_SCHEDULE:
889 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
022349df 890 case LTTNG_CLEAR_SESSION:
fbc9f37d 891 case LTTNG_LIST_TRIGGERS:
9124c630
JR
892 need_domain = false;
893 break;
894 default:
895 need_domain = true;
896 }
897
898 /* Needs a functioning consumerd? */
899 switch (cmd_ctx->lsm.cmd_type) {
900 case LTTNG_REGISTER_TRIGGER:
901 case LTTNG_UNREGISTER_TRIGGER:
902 need_consumerd = false;
917a718d
JG
903 break;
904 default:
9124c630
JR
905 need_consumerd = true;
906 break;
917a718d
JG
907 }
908
412d7227
SM
909 if (the_config.no_kernel && need_domain &&
910 cmd_ctx->lsm.domain.type == LTTNG_DOMAIN_KERNEL) {
917a718d
JG
911 if (!is_root) {
912 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
913 } else {
914 ret = LTTNG_ERR_KERN_NA;
915 }
916 goto error;
917 }
918
919 /* Deny register consumer if we already have a spawned consumer. */
3a91de3a 920 if (cmd_ctx->lsm.cmd_type == LTTNG_REGISTER_CONSUMER) {
412d7227
SM
921 pthread_mutex_lock(&the_kconsumer_data.pid_mutex);
922 if (the_kconsumer_data.pid > 0) {
917a718d 923 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
412d7227 924 pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
917a718d
JG
925 goto error;
926 }
412d7227 927 pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
917a718d
JG
928 }
929
930 /*
931 * Check for command that don't needs to allocate a returned payload. We do
932 * this here so we don't have to make the call for no payload at each
933 * command.
934 */
3a91de3a 935 switch(cmd_ctx->lsm.cmd_type) {
917a718d
JG
936 case LTTNG_LIST_SESSIONS:
937 case LTTNG_LIST_TRACEPOINTS:
938 case LTTNG_LIST_TRACEPOINT_FIELDS:
939 case LTTNG_LIST_DOMAINS:
940 case LTTNG_LIST_CHANNELS:
941 case LTTNG_LIST_EVENTS:
942 case LTTNG_LIST_SYSCALLS:
159b042f
JG
943 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
944 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
945 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
917a718d
JG
946 case LTTNG_DATA_PENDING:
947 case LTTNG_ROTATE_SESSION:
948 case LTTNG_ROTATION_GET_INFO:
9124c630 949 case LTTNG_REGISTER_TRIGGER:
fbc9f37d 950 case LTTNG_LIST_TRIGGERS:
917a718d
JG
951 break;
952 default:
953 /* Setup lttng message with no payload */
954 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
955 if (ret < 0) {
956 /* This label does not try to unlock the session */
957 goto init_setup_error;
958 }
959 }
960
961 /* Commands that DO NOT need a session. */
3a91de3a 962 switch (cmd_ctx->lsm.cmd_type) {
b178f53e 963 case LTTNG_CREATE_SESSION_EXT:
917a718d
JG
964 case LTTNG_LIST_SESSIONS:
965 case LTTNG_LIST_TRACEPOINTS:
966 case LTTNG_LIST_SYSCALLS:
967 case LTTNG_LIST_TRACEPOINT_FIELDS:
968 case LTTNG_SAVE_SESSION:
969 case LTTNG_REGISTER_TRIGGER:
970 case LTTNG_UNREGISTER_TRIGGER:
fbc9f37d 971 case LTTNG_LIST_TRIGGERS:
9124c630 972 need_tracing_session = false;
917a718d
JG
973 break;
974 default:
3a91de3a 975 DBG("Getting session %s by name", cmd_ctx->lsm.session.name);
917a718d
JG
976 /*
977 * We keep the session list lock across _all_ commands
978 * for now, because the per-session lock does not
979 * handle teardown properly.
980 */
981 session_lock_list();
3a91de3a 982 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm.session.name);
917a718d
JG
983 if (cmd_ctx->session == NULL) {
984 ret = LTTNG_ERR_SESS_NOT_FOUND;
985 goto error;
986 } else {
987 /* Acquire lock for the session */
988 session_lock(cmd_ctx->session);
989 }
990 break;
991 }
992
993 /*
994 * Commands that need a valid session but should NOT create one if none
995 * exists. Instead of creating one and destroying it when the command is
996 * handled, process that right before so we save some round trip in useless
997 * code path.
998 */
3a91de3a 999 switch (cmd_ctx->lsm.cmd_type) {
917a718d
JG
1000 case LTTNG_DISABLE_CHANNEL:
1001 case LTTNG_DISABLE_EVENT:
3a91de3a 1002 switch (cmd_ctx->lsm.domain.type) {
917a718d
JG
1003 case LTTNG_DOMAIN_KERNEL:
1004 if (!cmd_ctx->session->kernel_session) {
1005 ret = LTTNG_ERR_NO_CHANNEL;
1006 goto error;
1007 }
1008 break;
1009 case LTTNG_DOMAIN_JUL:
1010 case LTTNG_DOMAIN_LOG4J:
1011 case LTTNG_DOMAIN_PYTHON:
1012 case LTTNG_DOMAIN_UST:
1013 if (!cmd_ctx->session->ust_session) {
1014 ret = LTTNG_ERR_NO_CHANNEL;
1015 goto error;
1016 }
1017 break;
1018 default:
1019 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1020 goto error;
1021 }
1022 default:
1023 break;
1024 }
1025
1026 if (!need_domain) {
1027 goto skip_domain;
1028 }
1029
1030 /*
1031 * Check domain type for specific "pre-action".
1032 */
3a91de3a 1033 switch (cmd_ctx->lsm.domain.type) {
917a718d
JG
1034 case LTTNG_DOMAIN_KERNEL:
1035 if (!is_root) {
1036 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
1037 goto error;
1038 }
1039
7d268848
MD
1040 /* Kernel tracer check */
1041 if (!kernel_tracer_is_initialized()) {
1042 /* Basically, load kernel tracer modules */
1043 ret = init_kernel_tracer();
1044 if (ret != 0) {
1045 goto error;
1046 }
1047 }
1048
917a718d 1049 /* Consumer is in an ERROR state. Report back to client */
412d7227
SM
1050 if (need_consumerd && uatomic_read(&the_kernel_consumerd_state) ==
1051 CONSUMER_ERROR) {
917a718d
JG
1052 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1053 goto error;
1054 }
1055
1056 /* Need a session for kernel command */
1057 if (need_tracing_session) {
1058 if (cmd_ctx->session->kernel_session == NULL) {
1059 ret = create_kernel_session(cmd_ctx->session);
51630bd8 1060 if (ret != LTTNG_OK) {
917a718d
JG
1061 ret = LTTNG_ERR_KERN_SESS_FAIL;
1062 goto error;
1063 }
1064 }
1065
1066 /* Start the kernel consumer daemon */
412d7227
SM
1067 pthread_mutex_lock(&the_kconsumer_data.pid_mutex);
1068 if (the_kconsumer_data.pid == 0 &&
3a91de3a 1069 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
412d7227
SM
1070 pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
1071 ret = start_consumerd(&the_kconsumer_data);
917a718d
JG
1072 if (ret < 0) {
1073 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1074 goto error;
1075 }
412d7227 1076 uatomic_set(&the_kernel_consumerd_state, CONSUMER_STARTED);
917a718d 1077 } else {
412d7227 1078 pthread_mutex_unlock(&the_kconsumer_data.pid_mutex);
917a718d
JG
1079 }
1080
1081 /*
1082 * The consumer was just spawned so we need to add the socket to
1083 * the consumer output of the session if exist.
1084 */
412d7227 1085 ret = consumer_create_socket(&the_kconsumer_data,
917a718d
JG
1086 cmd_ctx->session->kernel_session->consumer);
1087 if (ret < 0) {
1088 goto error;
1089 }
1090 }
1091
1092 break;
1093 case LTTNG_DOMAIN_JUL:
1094 case LTTNG_DOMAIN_LOG4J:
1095 case LTTNG_DOMAIN_PYTHON:
44760c20
JR
1096 if (!agent_tracing_is_enabled()) {
1097 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1098 goto error;
1099 }
1100 /* Fallthrough */
917a718d
JG
1101 case LTTNG_DOMAIN_UST:
1102 {
1103 if (!ust_app_supported()) {
1104 ret = LTTNG_ERR_NO_UST;
1105 goto error;
1106 }
9124c630 1107
917a718d 1108 /* Consumer is in an ERROR state. Report back to client */
412d7227
SM
1109 if (need_consumerd &&
1110 uatomic_read(&the_ust_consumerd_state) ==
1111 CONSUMER_ERROR) {
917a718d
JG
1112 ret = LTTNG_ERR_NO_USTCONSUMERD;
1113 goto error;
1114 }
1115
1116 if (need_tracing_session) {
1117 /* Create UST session if none exist. */
1118 if (cmd_ctx->session->ust_session == NULL) {
1119 ret = create_ust_session(cmd_ctx->session,
3a91de3a 1120 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain));
917a718d
JG
1121 if (ret != LTTNG_OK) {
1122 goto error;
1123 }
1124 }
1125
1126 /* Start the UST consumer daemons */
1127 /* 64-bit */
412d7227
SM
1128 pthread_mutex_lock(&the_ustconsumer64_data.pid_mutex);
1129 if (the_config.consumerd64_bin_path.value &&
1130 the_ustconsumer64_data.pid == 0 &&
3a91de3a 1131 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
412d7227
SM
1132 pthread_mutex_unlock(&the_ustconsumer64_data.pid_mutex);
1133 ret = start_consumerd(&the_ustconsumer64_data);
917a718d
JG
1134 if (ret < 0) {
1135 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
412d7227 1136 uatomic_set(&the_ust_consumerd64_fd, -EINVAL);
917a718d
JG
1137 goto error;
1138 }
1139
412d7227
SM
1140 uatomic_set(&the_ust_consumerd64_fd, the_ustconsumer64_data.cmd_sock);
1141 uatomic_set(&the_ust_consumerd_state, CONSUMER_STARTED);
917a718d 1142 } else {
412d7227 1143 pthread_mutex_unlock(&the_ustconsumer64_data.pid_mutex);
917a718d
JG
1144 }
1145
1146 /*
1147 * Setup socket for consumer 64 bit. No need for atomic access
1148 * since it was set above and can ONLY be set in this thread.
1149 */
412d7227 1150 ret = consumer_create_socket(&the_ustconsumer64_data,
917a718d
JG
1151 cmd_ctx->session->ust_session->consumer);
1152 if (ret < 0) {
1153 goto error;
1154 }
1155
1156 /* 32-bit */
412d7227
SM
1157 pthread_mutex_lock(&the_ustconsumer32_data.pid_mutex);
1158 if (the_config.consumerd32_bin_path.value &&
1159 the_ustconsumer32_data.pid == 0 &&
3a91de3a 1160 cmd_ctx->lsm.cmd_type != LTTNG_REGISTER_CONSUMER) {
412d7227
SM
1161 pthread_mutex_unlock(&the_ustconsumer32_data.pid_mutex);
1162 ret = start_consumerd(&the_ustconsumer32_data);
917a718d
JG
1163 if (ret < 0) {
1164 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
412d7227 1165 uatomic_set(&the_ust_consumerd32_fd, -EINVAL);
917a718d
JG
1166 goto error;
1167 }
1168
412d7227
SM
1169 uatomic_set(&the_ust_consumerd32_fd, the_ustconsumer32_data.cmd_sock);
1170 uatomic_set(&the_ust_consumerd_state, CONSUMER_STARTED);
917a718d 1171 } else {
412d7227 1172 pthread_mutex_unlock(&the_ustconsumer32_data.pid_mutex);
917a718d
JG
1173 }
1174
1175 /*
1176 * Setup socket for consumer 32 bit. No need for atomic access
1177 * since it was set above and can ONLY be set in this thread.
1178 */
412d7227 1179 ret = consumer_create_socket(&the_ustconsumer32_data,
917a718d
JG
1180 cmd_ctx->session->ust_session->consumer);
1181 if (ret < 0) {
1182 goto error;
1183 }
1184 }
1185 break;
1186 }
1187 default:
1188 break;
1189 }
1190skip_domain:
1191
1192 /* Validate consumer daemon state when start/stop trace command */
3a91de3a
JG
1193 if (cmd_ctx->lsm.cmd_type == LTTNG_START_TRACE ||
1194 cmd_ctx->lsm.cmd_type == LTTNG_STOP_TRACE) {
1195 switch (cmd_ctx->lsm.domain.type) {
917a718d
JG
1196 case LTTNG_DOMAIN_NONE:
1197 break;
1198 case LTTNG_DOMAIN_JUL:
1199 case LTTNG_DOMAIN_LOG4J:
1200 case LTTNG_DOMAIN_PYTHON:
1201 case LTTNG_DOMAIN_UST:
412d7227 1202 if (uatomic_read(&the_ust_consumerd_state) != CONSUMER_STARTED) {
917a718d
JG
1203 ret = LTTNG_ERR_NO_USTCONSUMERD;
1204 goto error;
1205 }
1206 break;
1207 case LTTNG_DOMAIN_KERNEL:
412d7227 1208 if (uatomic_read(&the_kernel_consumerd_state) != CONSUMER_STARTED) {
917a718d
JG
1209 ret = LTTNG_ERR_NO_KERNCONSUMERD;
1210 goto error;
1211 }
1212 break;
1213 default:
1214 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1215 goto error;
1216 }
1217 }
1218
1219 /*
d7b377ed 1220 * Check that the UID matches that of the tracing session.
917a718d
JG
1221 * The root user can interact with all sessions.
1222 */
1223 if (need_tracing_session) {
1224 if (!session_access_ok(cmd_ctx->session,
d7b377ed 1225 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds)) ||
917a718d
JG
1226 cmd_ctx->session->destroyed) {
1227 ret = LTTNG_ERR_EPERM;
1228 goto error;
1229 }
1230 }
1231
1232 /*
1233 * Send relayd information to consumer as soon as we have a domain and a
1234 * session defined.
1235 */
1236 if (cmd_ctx->session && need_domain) {
1237 /*
1238 * Setup relayd if not done yet. If the relayd information was already
1239 * sent to the consumer, this call will gracefully return.
1240 */
1241 ret = cmd_setup_relayd(cmd_ctx->session);
1242 if (ret != LTTNG_OK) {
1243 goto error;
1244 }
1245 }
1246
1247 /* Process by command type */
3a91de3a 1248 switch (cmd_ctx->lsm.cmd_type) {
917a718d
JG
1249 case LTTNG_ADD_CONTEXT:
1250 {
1251 /*
1252 * An LTTNG_ADD_CONTEXT command might have a supplementary
1253 * payload if the context being added is an application context.
1254 */
3a91de3a 1255 if (cmd_ctx->lsm.u.context.ctx.ctx ==
917a718d
JG
1256 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1257 char *provider_name = NULL, *context_name = NULL;
1258 size_t provider_name_len =
3a91de3a 1259 cmd_ctx->lsm.u.context.provider_name_len;
917a718d 1260 size_t context_name_len =
3a91de3a 1261 cmd_ctx->lsm.u.context.context_name_len;
917a718d
JG
1262
1263 if (provider_name_len == 0 || context_name_len == 0) {
1264 /*
1265 * Application provider and context names MUST
1266 * be provided.
1267 */
1268 ret = -LTTNG_ERR_INVALID;
1269 goto error;
1270 }
1271
1272 provider_name = zmalloc(provider_name_len + 1);
1273 if (!provider_name) {
1274 ret = -LTTNG_ERR_NOMEM;
1275 goto error;
1276 }
3a91de3a 1277 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name =
917a718d
JG
1278 provider_name;
1279
1280 context_name = zmalloc(context_name_len + 1);
1281 if (!context_name) {
1282 ret = -LTTNG_ERR_NOMEM;
1283 goto error_add_context;
1284 }
3a91de3a 1285 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name =
917a718d
JG
1286 context_name;
1287
3e3665b8 1288 ret = lttcomm_recv_unix_sock(*sock, provider_name,
917a718d
JG
1289 provider_name_len);
1290 if (ret < 0) {
1291 goto error_add_context;
1292 }
1293
3e3665b8 1294 ret = lttcomm_recv_unix_sock(*sock, context_name,
917a718d
JG
1295 context_name_len);
1296 if (ret < 0) {
1297 goto error_add_context;
1298 }
1299 }
1300
1301 /*
1302 * cmd_add_context assumes ownership of the provider and context
1303 * names.
1304 */
1305 ret = cmd_add_context(cmd_ctx->session,
3a91de3a
JG
1306 cmd_ctx->lsm.domain.type,
1307 cmd_ctx->lsm.u.context.channel_name,
1308 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.context.ctx),
412d7227 1309 the_kernel_poll_pipe[1]);
917a718d 1310
3a91de3a
JG
1311 cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
1312 cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
917a718d 1313error_add_context:
3a91de3a
JG
1314 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.provider_name);
1315 free(cmd_ctx->lsm.u.context.ctx.u.app_ctx.ctx_name);
917a718d
JG
1316 if (ret < 0) {
1317 goto error;
1318 }
1319 break;
1320 }
1321 case LTTNG_DISABLE_CHANNEL:
1322 {
3a91de3a
JG
1323 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1324 cmd_ctx->lsm.u.disable.channel_name);
917a718d
JG
1325 break;
1326 }
1327 case LTTNG_DISABLE_EVENT:
1328 {
1329
1330 /*
1331 * FIXME: handle filter; for now we just receive the filter's
1332 * bytecode along with the filter expression which are sent by
1333 * liblttng-ctl and discard them.
1334 *
1335 * This fixes an issue where the client may block while sending
1336 * the filter payload and encounter an error because the session
1337 * daemon closes the socket without ever handling this data.
1338 */
3a91de3a
JG
1339 size_t count = cmd_ctx->lsm.u.disable.expression_len +
1340 cmd_ctx->lsm.u.disable.bytecode_len;
917a718d
JG
1341
1342 if (count) {
1343 char data[LTTNG_FILTER_MAX_LEN];
1344
1345 DBG("Discarding disable event command payload of size %zu", count);
1346 while (count) {
3e3665b8 1347 ret = lttcomm_recv_unix_sock(*sock, data,
917a718d
JG
1348 count > sizeof(data) ? sizeof(data) : count);
1349 if (ret < 0) {
1350 goto error;
1351 }
1352
1353 count -= (size_t) ret;
1354 }
1355 }
3a91de3a
JG
1356 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1357 cmd_ctx->lsm.u.disable.channel_name,
1358 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.disable.event));
917a718d
JG
1359 break;
1360 }
1361 case LTTNG_ENABLE_CHANNEL:
1362 {
3a91de3a
JG
1363 cmd_ctx->lsm.u.channel.chan.attr.extended.ptr =
1364 (struct lttng_channel_extended *) &cmd_ctx->lsm.u.channel.extended;
df4f5a87 1365 ret = cmd_enable_channel(cmd_ctx->session,
3a91de3a
JG
1366 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1367 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.channel.chan),
412d7227 1368 the_kernel_poll_pipe[1]);
917a718d
JG
1369 break;
1370 }
159b042f
JG
1371 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE:
1372 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE:
917a718d 1373 {
159b042f
JG
1374 struct lttng_dynamic_buffer payload;
1375 struct lttng_buffer_view payload_view;
1376 const bool add_value =
3a91de3a 1377 cmd_ctx->lsm.cmd_type ==
159b042f
JG
1378 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE;
1379 const size_t name_len =
3a91de3a 1380 cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
159b042f
JG
1381 .name_len;
1382 const enum lttng_domain_type domain_type =
1383 (enum lttng_domain_type)
3a91de3a 1384 cmd_ctx->lsm.domain.type;
159b042f 1385 const enum lttng_process_attr process_attr =
3a91de3a 1386 (enum lttng_process_attr) cmd_ctx->lsm.u
159b042f
JG
1387 .process_attr_tracker_add_remove_include_value
1388 .process_attr;
1389 const enum lttng_process_attr_value_type value_type =
1390 (enum lttng_process_attr_value_type) cmd_ctx
3a91de3a 1391 ->lsm.u
159b042f
JG
1392 .process_attr_tracker_add_remove_include_value
1393 .value_type;
1394 struct process_attr_value *value;
1395 enum lttng_error_code ret_code;
1434fd36
MJ
1396 long login_name_max;
1397
1398 login_name_max = sysconf(_SC_LOGIN_NAME_MAX);
1399 if (login_name_max < 0) {
1400 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1401 ret = LTTNG_ERR_INVALID;
1402 goto error;
1403 }
159b042f
JG
1404
1405 /* Receive remaining variable length payload if applicable. */
1434fd36 1406 if (name_len > login_name_max) {
159b042f
JG
1407 /*
1408 * POSIX mandates user and group names that are at least
1409 * 8 characters long. Note that although shadow-utils
1410 * (useradd, groupaadd, etc.) use 32 chars as their
1411 * limit (from bits/utmp.h, UT_NAMESIZE),
1412 * LOGIN_NAME_MAX is defined to 256.
1413 */
1434fd36 1414 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
159b042f 1415 add_value ? "addition" : "removal",
1434fd36 1416 name_len, login_name_max);
159b042f 1417 ret = LTTNG_ERR_INVALID;
2d97a006
JR
1418 goto error;
1419 }
1420
159b042f
JG
1421 lttng_dynamic_buffer_init(&payload);
1422 if (name_len != 0) {
1423 /*
1424 * Receive variable payload for user/group name
1425 * arguments.
1426 */
1427 ret = lttng_dynamic_buffer_set_size(&payload, name_len);
1428 if (ret) {
1429 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1430 add_value ? "add" : "remove");
55c9e7ca 1431 ret = LTTNG_ERR_NOMEM;
159b042f 1432 goto error_add_remove_tracker_value;
55c9e7ca 1433 }
159b042f
JG
1434
1435 ret = lttcomm_recv_unix_sock(
1436 *sock, payload.data, name_len);
55c9e7ca 1437 if (ret <= 0) {
159b042f
JG
1438 ERR("Failed to receive payload of %s process attribute tracker value argument",
1439 add_value ? "add" : "remove");
55c9e7ca 1440 *sock_error = 1;
159b042f
JG
1441 ret = LTTNG_ERR_INVALID_PROTOCOL;
1442 goto error_add_remove_tracker_value;
55c9e7ca 1443 }
159b042f 1444 }
2d97a006 1445
159b042f
JG
1446 payload_view = lttng_buffer_view_from_dynamic_buffer(
1447 &payload, 0, name_len);
3e6e0df2
JG
1448 if (name_len > 0 && !lttng_buffer_view_is_valid(&payload_view)) {
1449 ret = LTTNG_ERR_INVALID_PROTOCOL;
1450 goto error_add_remove_tracker_value;
1451 }
1452
159b042f
JG
1453 /*
1454 * Validate the value type and domains are legal for the process
1455 * attribute tracker that is specified and convert the value to
1456 * add/remove to the internal sessiond representation.
1457 */
1458 ret_code = process_attr_value_from_comm(domain_type,
1459 process_attr, value_type,
3a91de3a 1460 &cmd_ctx->lsm.u.process_attr_tracker_add_remove_include_value
159b042f
JG
1461 .integral_value,
1462 &payload_view, &value);
1463 if (ret_code != LTTNG_OK) {
1464 ret = ret_code;
1465 goto error_add_remove_tracker_value;
55c9e7ca 1466 }
159b042f
JG
1467
1468 if (add_value) {
1469 ret = cmd_process_attr_tracker_inclusion_set_add_value(
1470 cmd_ctx->session, domain_type,
1471 process_attr, value);
1472 } else {
1473 ret = cmd_process_attr_tracker_inclusion_set_remove_value(
1474 cmd_ctx->session, domain_type,
1475 process_attr, value);
1476 }
1477 process_attr_value_destroy(value);
1478 error_add_remove_tracker_value:
1479 lttng_dynamic_buffer_reset(&payload);
1480 break;
1481 }
1482 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY:
1483 {
1484 enum lttng_tracking_policy tracking_policy;
1485 const enum lttng_domain_type domain_type =
1486 (enum lttng_domain_type)
3a91de3a 1487 cmd_ctx->lsm.domain.type;
159b042f 1488 const enum lttng_process_attr process_attr =
3a91de3a 1489 (enum lttng_process_attr) cmd_ctx->lsm.u
159b042f
JG
1490 .process_attr_tracker_get_tracking_policy
1491 .process_attr;
1492
1493 ret = cmd_process_attr_tracker_get_tracking_policy(
1494 cmd_ctx->session, domain_type, process_attr,
1495 &tracking_policy);
1496 if (ret != LTTNG_OK) {
55c9e7ca
JR
1497 goto error;
1498 }
2d97a006 1499
159b042f
JG
1500 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
1501 &(uint32_t){tracking_policy}, sizeof(uint32_t));
1502 if (ret < 0) {
1503 ret = LTTNG_ERR_NOMEM;
2d97a006
JR
1504 goto error;
1505 }
159b042f 1506 ret = LTTNG_OK;
917a718d
JG
1507 break;
1508 }
159b042f 1509 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY:
917a718d 1510 {
159b042f 1511 const enum lttng_tracking_policy tracking_policy =
3a91de3a 1512 (enum lttng_tracking_policy) cmd_ctx->lsm.u
159b042f
JG
1513 .process_attr_tracker_set_tracking_policy
1514 .tracking_policy;
1515 const enum lttng_domain_type domain_type =
1516 (enum lttng_domain_type)
3a91de3a 1517 cmd_ctx->lsm.domain.type;
159b042f 1518 const enum lttng_process_attr process_attr =
3a91de3a 1519 (enum lttng_process_attr) cmd_ctx->lsm.u
159b042f
JG
1520 .process_attr_tracker_set_tracking_policy
1521 .process_attr;
1522
1523 ret = cmd_process_attr_tracker_set_tracking_policy(
1524 cmd_ctx->session, domain_type, process_attr,
1525 tracking_policy);
1526 if (ret != LTTNG_OK) {
1527 goto error;
55c9e7ca 1528 }
159b042f
JG
1529 break;
1530 }
1531 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET:
1532 {
1533 struct lttng_process_attr_values *values;
1534 struct lttng_dynamic_buffer reply;
1535 const enum lttng_domain_type domain_type =
1536 (enum lttng_domain_type)
3a91de3a 1537 cmd_ctx->lsm.domain.type;
159b042f 1538 const enum lttng_process_attr process_attr =
3a91de3a 1539 (enum lttng_process_attr) cmd_ctx->lsm.u
159b042f
JG
1540 .process_attr_tracker_get_inclusion_set
1541 .process_attr;
1542
1543 ret = cmd_process_attr_tracker_get_inclusion_set(
1544 cmd_ctx->session, domain_type, process_attr,
1545 &values);
1546 if (ret != LTTNG_OK) {
55c9e7ca
JR
1547 goto error;
1548 }
2d97a006 1549
159b042f
JG
1550 lttng_dynamic_buffer_init(&reply);
1551 ret = lttng_process_attr_values_serialize(values, &reply);
1552 if (ret < 0) {
1553 goto error_tracker_get_inclusion_set;
2d97a006
JR
1554 }
1555
159b042f
JG
1556 ret = setup_lttng_msg_no_cmd_header(
1557 cmd_ctx, reply.data, reply.size);
1558 if (ret < 0) {
1559 ret = LTTNG_ERR_NOMEM;
1560 goto error_tracker_get_inclusion_set;
1561 }
1562 ret = LTTNG_OK;
1563
1564 error_tracker_get_inclusion_set:
1565 lttng_process_attr_values_destroy(values);
1566 lttng_dynamic_buffer_reset(&reply);
917a718d
JG
1567 break;
1568 }
1569 case LTTNG_ENABLE_EVENT:
1570 {
1571 struct lttng_event *ev = NULL;
1572 struct lttng_event_exclusion *exclusion = NULL;
2b00d462 1573 struct lttng_bytecode *bytecode = NULL;
917a718d
JG
1574 char *filter_expression = NULL;
1575
1576 /* Handle exclusion events and receive it from the client. */
3a91de3a
JG
1577 if (cmd_ctx->lsm.u.enable.exclusion_count > 0) {
1578 size_t count = cmd_ctx->lsm.u.enable.exclusion_count;
917a718d
JG
1579
1580 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
1581 (count * LTTNG_SYMBOL_NAME_LEN));
1582 if (!exclusion) {
1583 ret = LTTNG_ERR_EXCLUSION_NOMEM;
1584 goto error;
1585 }
1586
1587 DBG("Receiving var len exclusion event list from client ...");
1588 exclusion->count = count;
3e3665b8 1589 ret = lttcomm_recv_unix_sock(*sock, exclusion->names,
917a718d
JG
1590 count * LTTNG_SYMBOL_NAME_LEN);
1591 if (ret <= 0) {
1592 DBG("Nothing recv() from client var len data... continuing");
1593 *sock_error = 1;
1594 free(exclusion);
1595 ret = LTTNG_ERR_EXCLUSION_INVAL;
1596 goto error;
1597 }
1598 }
1599
1600 /* Get filter expression from client. */
3a91de3a 1601 if (cmd_ctx->lsm.u.enable.expression_len > 0) {
917a718d 1602 size_t expression_len =
3a91de3a 1603 cmd_ctx->lsm.u.enable.expression_len;
917a718d
JG
1604
1605 if (expression_len > LTTNG_FILTER_MAX_LEN) {
1606 ret = LTTNG_ERR_FILTER_INVAL;
1607 free(exclusion);
1608 goto error;
1609 }
1610
1611 filter_expression = zmalloc(expression_len);
1612 if (!filter_expression) {
1613 free(exclusion);
1614 ret = LTTNG_ERR_FILTER_NOMEM;
1615 goto error;
1616 }
1617
1618 /* Receive var. len. data */
1619 DBG("Receiving var len filter's expression from client ...");
3e3665b8 1620 ret = lttcomm_recv_unix_sock(*sock, filter_expression,
917a718d
JG
1621 expression_len);
1622 if (ret <= 0) {
1623 DBG("Nothing recv() from client var len data... continuing");
1624 *sock_error = 1;
1625 free(filter_expression);
1626 free(exclusion);
1627 ret = LTTNG_ERR_FILTER_INVAL;
1628 goto error;
1629 }
1630 }
1631
1632 /* Handle filter and get bytecode from client. */
3a91de3a
JG
1633 if (cmd_ctx->lsm.u.enable.bytecode_len > 0) {
1634 size_t bytecode_len = cmd_ctx->lsm.u.enable.bytecode_len;
917a718d
JG
1635
1636 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1637 ret = LTTNG_ERR_FILTER_INVAL;
1638 free(filter_expression);
1639 free(exclusion);
1640 goto error;
1641 }
1642
1643 bytecode = zmalloc(bytecode_len);
1644 if (!bytecode) {
1645 free(filter_expression);
1646 free(exclusion);
1647 ret = LTTNG_ERR_FILTER_NOMEM;
1648 goto error;
1649 }
1650
1651 /* Receive var. len. data */
1652 DBG("Receiving var len filter's bytecode from client ...");
3e3665b8 1653 ret = lttcomm_recv_unix_sock(*sock, bytecode, bytecode_len);
917a718d
JG
1654 if (ret <= 0) {
1655 DBG("Nothing recv() from client var len data... continuing");
1656 *sock_error = 1;
1657 free(filter_expression);
1658 free(bytecode);
1659 free(exclusion);
1660 ret = LTTNG_ERR_FILTER_INVAL;
1661 goto error;
1662 }
1663
1664 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
1665 free(filter_expression);
1666 free(bytecode);
1667 free(exclusion);
1668 ret = LTTNG_ERR_FILTER_INVAL;
1669 goto error;
1670 }
1671 }
1672
3a91de3a 1673 ev = lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx->lsm.u.enable.event));
917a718d
JG
1674 if (!ev) {
1675 DBG("Failed to copy event: %s",
3a91de3a 1676 cmd_ctx->lsm.u.enable.event.name);
917a718d
JG
1677 free(filter_expression);
1678 free(bytecode);
1679 free(exclusion);
1680 ret = LTTNG_ERR_NOMEM;
1681 goto error;
1682 }
1683
1684
3a91de3a 1685 if (cmd_ctx->lsm.u.enable.userspace_probe_location_len > 0) {
917a718d 1686 /* Expect a userspace probe description. */
3e3665b8 1687 ret = receive_userspace_probe(cmd_ctx, *sock, sock_error, ev);
917a718d
JG
1688 if (ret) {
1689 free(filter_expression);
1690 free(bytecode);
1691 free(exclusion);
1692 lttng_event_destroy(ev);
1693 goto error;
1694 }
1695 }
1696
df4f5a87 1697 ret = cmd_enable_event(cmd_ctx->session,
3a91de3a
JG
1698 ALIGNED_CONST_PTR(cmd_ctx->lsm.domain),
1699 cmd_ctx->lsm.u.enable.channel_name,
917a718d
JG
1700 ev,
1701 filter_expression, bytecode, exclusion,
412d7227 1702 the_kernel_poll_pipe[1]);
917a718d
JG
1703 lttng_event_destroy(ev);
1704 break;
1705 }
1706 case LTTNG_LIST_TRACEPOINTS:
1707 {
1708 struct lttng_event *events;
1709 ssize_t nb_events;
1710
1711 session_lock_list();
3a91de3a 1712 nb_events = cmd_list_tracepoints(cmd_ctx->lsm.domain.type, &events);
917a718d
JG
1713 session_unlock_list();
1714 if (nb_events < 0) {
1715 /* Return value is a negative lttng_error_code. */
1716 ret = -nb_events;
1717 goto error;
1718 }
1719
1720 /*
1721 * Setup lttng message with payload size set to the event list size in
1722 * bytes and then copy list into the llm payload.
1723 */
1724 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1725 sizeof(struct lttng_event) * nb_events);
1726 free(events);
1727
1728 if (ret < 0) {
1729 goto setup_error;
1730 }
1731
1732 ret = LTTNG_OK;
1733 break;
1734 }
1735 case LTTNG_LIST_TRACEPOINT_FIELDS:
1736 {
1737 struct lttng_event_field *fields;
1738 ssize_t nb_fields;
1739
1740 session_lock_list();
3a91de3a 1741 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm.domain.type,
917a718d
JG
1742 &fields);
1743 session_unlock_list();
1744 if (nb_fields < 0) {
1745 /* Return value is a negative lttng_error_code. */
1746 ret = -nb_fields;
1747 goto error;
1748 }
1749
1750 /*
1751 * Setup lttng message with payload size set to the event list size in
1752 * bytes and then copy list into the llm payload.
1753 */
1754 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
1755 sizeof(struct lttng_event_field) * nb_fields);
1756 free(fields);
1757
1758 if (ret < 0) {
1759 goto setup_error;
1760 }
1761
1762 ret = LTTNG_OK;
1763 break;
1764 }
1765 case LTTNG_LIST_SYSCALLS:
1766 {
1767 struct lttng_event *events;
1768 ssize_t nb_events;
1769
1770 nb_events = cmd_list_syscalls(&events);
1771 if (nb_events < 0) {
1772 /* Return value is a negative lttng_error_code. */
1773 ret = -nb_events;
1774 goto error;
1775 }
1776
1777 /*
1778 * Setup lttng message with payload size set to the event list size in
1779 * bytes and then copy list into the llm payload.
1780 */
1781 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
1782 sizeof(struct lttng_event) * nb_events);
1783 free(events);
1784
1785 if (ret < 0) {
1786 goto setup_error;
1787 }
1788
1789 ret = LTTNG_OK;
1790 break;
1791 }
917a718d
JG
1792 case LTTNG_SET_CONSUMER_URI:
1793 {
1794 size_t nb_uri, len;
1795 struct lttng_uri *uris;
1796
3a91de3a 1797 nb_uri = cmd_ctx->lsm.u.uri.size;
917a718d
JG
1798 len = nb_uri * sizeof(struct lttng_uri);
1799
1800 if (nb_uri == 0) {
1801 ret = LTTNG_ERR_INVALID;
1802 goto error;
1803 }
1804
1805 uris = zmalloc(len);
1806 if (uris == NULL) {
1807 ret = LTTNG_ERR_FATAL;
1808 goto error;
1809 }
1810
1811 /* Receive variable len data */
1812 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3e3665b8 1813 ret = lttcomm_recv_unix_sock(*sock, uris, len);
917a718d
JG
1814 if (ret <= 0) {
1815 DBG("No URIs received from client... continuing");
1816 *sock_error = 1;
1817 ret = LTTNG_ERR_SESSION_FAIL;
1818 free(uris);
1819 goto error;
1820 }
1821
1822 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
1823 free(uris);
1824 if (ret != LTTNG_OK) {
1825 goto error;
1826 }
1827
1828
1829 break;
1830 }
1831 case LTTNG_START_TRACE:
1832 {
1833 /*
1834 * On the first start, if we have a kernel session and we have
1835 * enabled time or size-based rotations, we have to make sure
1836 * the kernel tracer supports it.
1837 */
1838 if (!cmd_ctx->session->has_been_started && \
1839 cmd_ctx->session->kernel_session && \
1840 (cmd_ctx->session->rotate_timer_period || \
1841 cmd_ctx->session->rotate_size) && \
1842 !check_rotate_compatible()) {
1843 DBG("Kernel tracer version is not compatible with the rotation feature");
1844 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
1845 goto error;
1846 }
1847 ret = cmd_start_trace(cmd_ctx->session);
1848 break;
1849 }
1850 case LTTNG_STOP_TRACE:
1851 {
1852 ret = cmd_stop_trace(cmd_ctx->session);
1853 break;
1854 }
917a718d
JG
1855 case LTTNG_DESTROY_SESSION:
1856 {
1857 ret = cmd_destroy_session(cmd_ctx->session,
412d7227 1858 the_notification_thread_handle, sock);
917a718d
JG
1859 break;
1860 }
1861 case LTTNG_LIST_DOMAINS:
1862 {
1863 ssize_t nb_dom;
1864 struct lttng_domain *domains = NULL;
1865
1866 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
1867 if (nb_dom < 0) {
1868 /* Return value is a negative lttng_error_code. */
1869 ret = -nb_dom;
1870 goto error;
1871 }
1872
1873 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
1874 nb_dom * sizeof(struct lttng_domain));
1875 free(domains);
1876
1877 if (ret < 0) {
1878 goto setup_error;
1879 }
1880
1881 ret = LTTNG_OK;
1882 break;
1883 }
1884 case LTTNG_LIST_CHANNELS:
1885 {
1886 ssize_t payload_size;
1887 struct lttng_channel *channels = NULL;
1888
3a91de3a 1889 payload_size = cmd_list_channels(cmd_ctx->lsm.domain.type,
917a718d
JG
1890 cmd_ctx->session, &channels);
1891 if (payload_size < 0) {
1892 /* Return value is a negative lttng_error_code. */
1893 ret = -payload_size;
1894 goto error;
1895 }
1896
1897 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
1898 payload_size);
1899 free(channels);
1900
1901 if (ret < 0) {
1902 goto setup_error;
1903 }
1904
1905 ret = LTTNG_OK;
1906 break;
1907 }
1908 case LTTNG_LIST_EVENTS:
1909 {
e368fb43
JG
1910 ssize_t list_ret;
1911 struct lttcomm_event_command_header cmd_header = {};
1912 size_t original_payload_size;
1913 size_t payload_size;
1914
1915 ret = setup_empty_lttng_msg(cmd_ctx);
1916 if (ret) {
1917 ret = LTTNG_ERR_NOMEM;
1918 goto setup_error;
917a718d
JG
1919 }
1920
e368fb43 1921 original_payload_size = cmd_ctx->reply_payload.buffer.size;
917a718d 1922
e368fb43
JG
1923 /* Extended infos are included at the end of the payload. */
1924 list_ret = cmd_list_events(cmd_ctx->lsm.domain.type,
1925 cmd_ctx->session,
1926 cmd_ctx->lsm.u.list.channel_name,
1927 &cmd_ctx->reply_payload);
1928 if (list_ret < 0) {
1929 /* Return value is a negative lttng_error_code. */
1930 ret = -list_ret;
1931 goto error;
917a718d
JG
1932 }
1933
e368fb43
JG
1934 payload_size = cmd_ctx->reply_payload.buffer.size -
1935 sizeof(cmd_header) - original_payload_size;
1936 update_lttng_msg(cmd_ctx, sizeof(cmd_header), payload_size);
1937
917a718d
JG
1938 ret = LTTNG_OK;
1939 break;
1940 }
1941 case LTTNG_LIST_SESSIONS:
1942 {
1943 unsigned int nr_sessions;
1944 void *sessions_payload;
1945 size_t payload_len;
1946
1947 session_lock_list();
1948 nr_sessions = lttng_sessions_count(
1949 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1950 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
b178f53e
JG
1951
1952 payload_len = (sizeof(struct lttng_session) * nr_sessions) +
1953 (sizeof(struct lttng_session_extended) * nr_sessions);
917a718d
JG
1954 sessions_payload = zmalloc(payload_len);
1955
1956 if (!sessions_payload) {
1957 session_unlock_list();
1958 ret = -ENOMEM;
1959 goto setup_error;
1960 }
1961
b178f53e 1962 cmd_list_lttng_sessions(sessions_payload, nr_sessions,
917a718d
JG
1963 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
1964 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
1965 session_unlock_list();
1966
1967 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
1968 payload_len);
1969 free(sessions_payload);
1970
1971 if (ret < 0) {
1972 goto setup_error;
1973 }
1974
1975 ret = LTTNG_OK;
1976 break;
1977 }
1978 case LTTNG_REGISTER_CONSUMER:
1979 {
1980 struct consumer_data *cdata;
1981
3a91de3a 1982 switch (cmd_ctx->lsm.domain.type) {
917a718d 1983 case LTTNG_DOMAIN_KERNEL:
412d7227 1984 cdata = &the_kconsumer_data;
917a718d
JG
1985 break;
1986 default:
1987 ret = LTTNG_ERR_UND;
1988 goto error;
1989 }
1990
3a91de3a
JG
1991 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm.domain.type,
1992 cmd_ctx->lsm.u.reg.path, cdata);
917a718d
JG
1993 break;
1994 }
1995 case LTTNG_DATA_PENDING:
1996 {
1997 int pending_ret;
1998 uint8_t pending_ret_byte;
1999
2000 pending_ret = cmd_data_pending(cmd_ctx->session);
2001
2002 /*
2003 * FIXME
2004 *
2005 * This function may returns 0 or 1 to indicate whether or not
2006 * there is data pending. In case of error, it should return an
2007 * LTTNG_ERR code. However, some code paths may still return
2008 * a nondescript error code, which we handle by returning an
2009 * "unknown" error.
2010 */
2011 if (pending_ret == 0 || pending_ret == 1) {
2012 /*
2013 * ret will be set to LTTNG_OK at the end of
2014 * this function.
2015 */
2016 } else if (pending_ret < 0) {
2017 ret = LTTNG_ERR_UNK;
2018 goto setup_error;
2019 } else {
2020 ret = pending_ret;
2021 goto setup_error;
2022 }
2023
2024 pending_ret_byte = (uint8_t) pending_ret;
2025
2026 /* 1 byte to return whether or not data is pending */
2027 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
2028 &pending_ret_byte, 1);
2029
2030 if (ret < 0) {
2031 goto setup_error;
2032 }
2033
2034 ret = LTTNG_OK;
2035 break;
2036 }
2037 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2038 {
a914e76a 2039 uint32_t snapshot_id;
917a718d
JG
2040 struct lttcomm_lttng_output_id reply;
2041
2042 ret = cmd_snapshot_add_output(cmd_ctx->session,
3a91de3a 2043 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output),
df4f5a87 2044 &snapshot_id);
917a718d
JG
2045 if (ret != LTTNG_OK) {
2046 goto error;
2047 }
a914e76a 2048 reply.id = snapshot_id;
917a718d
JG
2049
2050 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
2051 sizeof(reply));
2052 if (ret < 0) {
2053 goto setup_error;
2054 }
2055
2056 /* Copy output list into message payload */
2057 ret = LTTNG_OK;
2058 break;
2059 }
2060 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2061 {
2062 ret = cmd_snapshot_del_output(cmd_ctx->session,
3a91de3a 2063 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_output.output));
917a718d
JG
2064 break;
2065 }
2066 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2067 {
2068 ssize_t nb_output;
2069 struct lttng_snapshot_output *outputs = NULL;
2070
2071 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
2072 if (nb_output < 0) {
2073 ret = -nb_output;
2074 goto error;
2075 }
2076
2077 assert((nb_output > 0 && outputs) || nb_output == 0);
2078 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
2079 nb_output * sizeof(struct lttng_snapshot_output));
2080 free(outputs);
2081
2082 if (ret < 0) {
2083 goto setup_error;
2084 }
2085
2086 ret = LTTNG_OK;
2087 break;
2088 }
2089 case LTTNG_SNAPSHOT_RECORD:
2090 {
2091 ret = cmd_snapshot_record(cmd_ctx->session,
3a91de3a
JG
2092 ALIGNED_CONST_PTR(cmd_ctx->lsm.u.snapshot_record.output),
2093 cmd_ctx->lsm.u.snapshot_record.wait);
917a718d
JG
2094 break;
2095 }
b178f53e 2096 case LTTNG_CREATE_SESSION_EXT:
917a718d 2097 {
b178f53e
JG
2098 struct lttng_dynamic_buffer payload;
2099 struct lttng_session_descriptor *return_descriptor = NULL;
917a718d 2100
b178f53e 2101 lttng_dynamic_buffer_init(&payload);
3e3665b8 2102 ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor);
b178f53e
JG
2103 if (ret != LTTNG_OK) {
2104 goto error;
917a718d
JG
2105 }
2106
b178f53e
JG
2107 ret = lttng_session_descriptor_serialize(return_descriptor,
2108 &payload);
2109 if (ret) {
2110 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2111 lttng_session_descriptor_destroy(return_descriptor);
2112 ret = LTTNG_ERR_NOMEM;
2113 goto error;
917a718d 2114 }
b178f53e
JG
2115 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data,
2116 payload.size);
2117 if (ret) {
2118 lttng_session_descriptor_destroy(return_descriptor);
2119 ret = LTTNG_ERR_NOMEM;
2120 goto error;
2121 }
2122 lttng_dynamic_buffer_reset(&payload);
2123 lttng_session_descriptor_destroy(return_descriptor);
2124 ret = LTTNG_OK;
917a718d
JG
2125 break;
2126 }
2127 case LTTNG_SAVE_SESSION:
2128 {
3a91de3a 2129 ret = cmd_save_sessions(&cmd_ctx->lsm.u.save_session.attr,
917a718d
JG
2130 &cmd_ctx->creds);
2131 break;
2132 }
2133 case LTTNG_SET_SESSION_SHM_PATH:
2134 {
2135 ret = cmd_set_session_shm_path(cmd_ctx->session,
3a91de3a 2136 cmd_ctx->lsm.u.set_shm_path.shm_path);
917a718d
JG
2137 break;
2138 }
2139 case LTTNG_REGENERATE_METADATA:
2140 {
2141 ret = cmd_regenerate_metadata(cmd_ctx->session);
2142 break;
2143 }
2144 case LTTNG_REGENERATE_STATEDUMP:
2145 {
2146 ret = cmd_regenerate_statedump(cmd_ctx->session);
2147 break;
2148 }
2149 case LTTNG_REGISTER_TRIGGER:
2150 {
746e08d7 2151 struct lttng_trigger *payload_trigger;
242388e4 2152 struct lttng_trigger *return_trigger;
746e08d7
JG
2153 size_t original_reply_payload_size;
2154 size_t reply_payload_size;
2155 const struct lttng_credentials cmd_creds = {
2156 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
2157 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
2158 };
242388e4
JR
2159
2160 ret = setup_empty_lttng_msg(cmd_ctx);
2161 if (ret) {
2162 ret = LTTNG_ERR_NOMEM;
2163 goto setup_error;
2164 }
2165
746e08d7
JG
2166 ret = receive_lttng_trigger(
2167 cmd_ctx, *sock, sock_error, &payload_trigger);
2168 if (ret != LTTNG_OK) {
2169 goto error;
2170 }
2171
2172 original_reply_payload_size = cmd_ctx->reply_payload.buffer.size;
242388e4 2173
746e08d7 2174 ret = cmd_register_trigger(&cmd_creds, payload_trigger,
412d7227
SM
2175 the_notification_thread_handle,
2176 &return_trigger);
242388e4 2177 if (ret != LTTNG_OK) {
746e08d7 2178 lttng_trigger_put(payload_trigger);
242388e4
JR
2179 goto error;
2180 }
2181
2182 ret = lttng_trigger_serialize(return_trigger, &cmd_ctx->reply_payload);
746e08d7
JG
2183 lttng_trigger_put(payload_trigger);
2184 lttng_trigger_put(return_trigger);
242388e4
JR
2185 if (ret) {
2186 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2187 ret = LTTNG_ERR_NOMEM;
242388e4
JR
2188 goto error;
2189 }
2190
746e08d7
JG
2191 reply_payload_size = cmd_ctx->reply_payload.buffer.size -
2192 original_reply_payload_size;
242388e4 2193
746e08d7 2194 update_lttng_msg(cmd_ctx, 0, reply_payload_size);
242388e4
JR
2195
2196 ret = LTTNG_OK;
917a718d
JG
2197 break;
2198 }
2199 case LTTNG_UNREGISTER_TRIGGER:
2200 {
746e08d7
JG
2201 struct lttng_trigger *payload_trigger;
2202 const struct lttng_credentials cmd_creds = {
2203 .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid),
2204 .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid),
2205 };
2206
2207 ret = receive_lttng_trigger(
2208 cmd_ctx, *sock, sock_error, &payload_trigger);
2209 if (ret != LTTNG_OK) {
2210 goto error;
2211 }
2212
2213 ret = cmd_unregister_trigger(&cmd_creds, payload_trigger,
412d7227 2214 the_notification_thread_handle);
746e08d7 2215 lttng_trigger_put(payload_trigger);
917a718d
JG
2216 break;
2217 }
2218 case LTTNG_ROTATE_SESSION:
2219 {
2220 struct lttng_rotate_session_return rotate_return;
2221
2222 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
2223
2224 memset(&rotate_return, 0, sizeof(rotate_return));
2225 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2226 DBG("Kernel tracer version is not compatible with the rotation feature");
2227 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2228 goto error;
2229 }
2230
7fdbed1c 2231 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return,
343defc2
MD
2232 false,
2233 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
917a718d
JG
2234 if (ret < 0) {
2235 ret = -ret;
2236 goto error;
2237 }
2238
2239 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
2240 sizeof(rotate_return));
2241 if (ret < 0) {
2242 ret = -ret;
2243 goto error;
2244 }
2245
2246 ret = LTTNG_OK;
2247 break;
2248 }
2249 case LTTNG_ROTATION_GET_INFO:
2250 {
2251 struct lttng_rotation_get_info_return get_info_return;
2252
2253 memset(&get_info_return, 0, sizeof(get_info_return));
2254 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
3a91de3a 2255 cmd_ctx->lsm.u.get_rotation_info.rotation_id);
917a718d
JG
2256 if (ret < 0) {
2257 ret = -ret;
2258 goto error;
2259 }
2260
2261 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
2262 sizeof(get_info_return));
2263 if (ret < 0) {
2264 ret = -ret;
2265 goto error;
2266 }
2267
2268 ret = LTTNG_OK;
2269 break;
2270 }
2271 case LTTNG_ROTATION_SET_SCHEDULE:
2272 {
2273 bool set_schedule;
2274 enum lttng_rotation_schedule_type schedule_type;
2275 uint64_t value;
2276
2277 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
2278 DBG("Kernel tracer version does not support session rotations");
2279 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
2280 goto error;
2281 }
2282
3a91de3a
JG
2283 set_schedule = cmd_ctx->lsm.u.rotation_set_schedule.set == 1;
2284 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm.u.rotation_set_schedule.type;
2285 value = cmd_ctx->lsm.u.rotation_set_schedule.value;
917a718d 2286
412d7227
SM
2287 ret = cmd_rotation_set_schedule(cmd_ctx->session, set_schedule,
2288 schedule_type, value,
2289 the_notification_thread_handle);
917a718d
JG
2290 if (ret != LTTNG_OK) {
2291 goto error;
2292 }
2293
2294 break;
2295 }
2296 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2297 {
2298 struct lttng_session_list_schedules_return schedules = {
2299 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
2300 .periodic.value = cmd_ctx->session->rotate_timer_period,
2301 .size.set = !!cmd_ctx->session->rotate_size,
2302 .size.value = cmd_ctx->session->rotate_size,
2303 };
2304
2305 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
2306 sizeof(schedules));
2307 if (ret < 0) {
2308 ret = -ret;
2309 goto error;
2310 }
2311
2312 ret = LTTNG_OK;
2313 break;
2314 }
022349df
MD
2315 case LTTNG_CLEAR_SESSION:
2316 {
2317 ret = cmd_clear_session(cmd_ctx->session, sock);
2318 break;
2319 }
fbc9f37d
JR
2320 case LTTNG_LIST_TRIGGERS:
2321 {
2322 struct lttng_triggers *return_triggers = NULL;
2323 size_t original_payload_size;
2324 size_t payload_size;
2325
2326 ret = setup_empty_lttng_msg(cmd_ctx);
2327 if (ret) {
2328 ret = LTTNG_ERR_NOMEM;
2329 goto setup_error;
2330 }
2331
2332 original_payload_size = cmd_ctx->reply_payload.buffer.size;
2333
412d7227
SM
2334 ret = cmd_list_triggers(cmd_ctx, the_notification_thread_handle,
2335 &return_triggers);
fbc9f37d
JR
2336 if (ret != LTTNG_OK) {
2337 goto error;
2338 }
2339
2340 assert(return_triggers);
2341 ret = lttng_triggers_serialize(
2342 return_triggers, &cmd_ctx->reply_payload);
2343 lttng_triggers_destroy(return_triggers);
2344 if (ret) {
2345 ERR("Failed to serialize triggers in reply to `list triggers` command");
2346 ret = LTTNG_ERR_NOMEM;
2347 goto error;
2348 }
2349
2350 payload_size = cmd_ctx->reply_payload.buffer.size -
2351 original_payload_size;
2352
2353 update_lttng_msg(cmd_ctx, 0, payload_size);
2354
2355 ret = LTTNG_OK;
2356 break;
2357 }
917a718d
JG
2358 default:
2359 ret = LTTNG_ERR_UND;
2360 break;
2361 }
2362
2363error:
3a91de3a
JG
2364 if (cmd_ctx->reply_payload.buffer.size == 0) {
2365 DBG("Missing llm header, creating one.");
917a718d
JG
2366 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
2367 goto setup_error;
2368 }
2369 }
2370 /* Set return code */
3a91de3a 2371 ((struct lttcomm_lttng_msg *) (cmd_ctx->reply_payload.buffer.data))->ret_code = ret;
917a718d
JG
2372setup_error:
2373 if (cmd_ctx->session) {
2374 session_unlock(cmd_ctx->session);
2375 session_put(cmd_ctx->session);
3e3665b8 2376 cmd_ctx->session = NULL;
917a718d
JG
2377 }
2378 if (need_tracing_session) {
2379 session_unlock_list();
2380 }
2381init_setup_error:
2382 assert(!rcu_read_ongoing());
2383 return ret;
2384}
2385
2386static int create_client_sock(void)
2387{
2388 int ret, client_sock;
2389 const mode_t old_umask = umask(0);
2390
2391 /* Create client tool unix socket */
412d7227
SM
2392 client_sock = lttcomm_create_unix_sock(
2393 the_config.client_unix_sock_path.value);
917a718d 2394 if (client_sock < 0) {
412d7227
SM
2395 ERR("Create unix sock failed: %s",
2396 the_config.client_unix_sock_path.value);
917a718d
JG
2397 ret = -1;
2398 goto end;
2399 }
2400
2401 /* Set the cloexec flag */
2402 ret = utils_set_fd_cloexec(client_sock);
2403 if (ret < 0) {
2404 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2405 "Continuing but note that the consumer daemon will have a "
2406 "reference to this socket on exec()", client_sock);
2407 }
2408
2409 /* File permission MUST be 660 */
412d7227
SM
2410 ret = chmod(the_config.client_unix_sock_path.value,
2411 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
917a718d 2412 if (ret < 0) {
18972083 2413 ERR("Set file permissions failed: %s",
412d7227 2414 the_config.client_unix_sock_path.value);
917a718d 2415 PERROR("chmod");
18972083
JR
2416 (void) lttcomm_close_unix_sock(client_sock);
2417 ret = -1;
917a718d
JG
2418 goto end;
2419 }
2420 DBG("Created client socket (fd = %i)", client_sock);
2421 ret = client_sock;
2422end:
2423 umask(old_umask);
2424 return ret;
2425}
2426
2427static void cleanup_client_thread(void *data)
2428{
2429 struct lttng_pipe *quit_pipe = data;
2430
2431 lttng_pipe_destroy(quit_pipe);
2432}
2433
6cb45e93
JG
2434static void thread_init_cleanup(void *data)
2435{
2436 set_thread_status(false);
2437}
2438
917a718d
JG
2439/*
2440 * This thread manage all clients request using the unix client socket for
2441 * communication.
2442 */
2443static void *thread_manage_clients(void *data)
2444{
2445 int sock = -1, ret, i, pollfd, err = -1;
2446 int sock_error;
2447 uint32_t revents, nb_fd;
917a718d 2448 struct lttng_poll_event events;
0f68efb6 2449 const int client_sock = thread_state.client_sock;
917a718d
JG
2450 struct lttng_pipe *quit_pipe = data;
2451 const int thread_quit_pipe_fd = lttng_pipe_get_readfd(quit_pipe);
3a91de3a 2452 struct command_ctx cmd_ctx = {};
917a718d
JG
2453
2454 DBG("[thread] Manage client started");
2455
3a91de3a
JG
2456 lttng_payload_init(&cmd_ctx.reply_payload);
2457
917a718d
JG
2458 is_root = (getuid() == 0);
2459
6cb45e93 2460 pthread_cleanup_push(thread_init_cleanup, NULL);
917a718d
JG
2461
2462 rcu_register_thread();
2463
412d7227 2464 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
917a718d
JG
2465
2466 health_code_update();
2467
2468 ret = lttcomm_listen_unix_sock(client_sock);
2469 if (ret < 0) {
2470 goto error_listen;
2471 }
2472
2473 /*
2474 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2475 * more will be added to this poll set.
2476 */
2477 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2478 if (ret < 0) {
2479 goto error_create_poll;
2480 }
2481
2482 /* Add the application registration socket */
2483 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
2484 if (ret < 0) {
2485 goto error;
2486 }
2487
2488 /* Add thread quit pipe */
2489 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLERR);
2490 if (ret < 0) {
2491 goto error;
2492 }
2493
6cb45e93 2494 /* Set state as running. */
0d163d56 2495 set_thread_status(true);
6cb45e93
JG
2496 pthread_cleanup_pop(0);
2497
917a718d
JG
2498 /* This testpoint is after we signal readiness to the parent. */
2499 if (testpoint(sessiond_thread_manage_clients)) {
2500 goto error;
2501 }
2502
2503 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
2504 goto error;
2505 }
2506
2507 health_code_update();
2508
917a718d
JG
2509 while (1) {
2510 const struct cmd_completion_handler *cmd_completion_handler;
2511
3a91de3a
JG
2512 cmd_ctx.creds = (lttng_sock_cred) {
2513 .uid = UINT32_MAX,
2514 .gid = UINT32_MAX,
2515 };
2516 cmd_ctx.session = NULL;
fe489250 2517 lttng_payload_clear(&cmd_ctx.reply_payload);
e368fb43 2518 cmd_ctx.lttng_msg_size = 0;
3a91de3a 2519
917a718d
JG
2520 DBG("Accepting client command ...");
2521
2522 /* Inifinite blocking call, waiting for transmission */
2523 restart:
2524 health_poll_entry();
2525 ret = lttng_poll_wait(&events, -1);
2526 health_poll_exit();
2527 if (ret < 0) {
2528 /*
2529 * Restart interrupted system call.
2530 */
2531 if (errno == EINTR) {
2532 goto restart;
2533 }
2534 goto error;
2535 }
2536
2537 nb_fd = ret;
2538
2539 for (i = 0; i < nb_fd; i++) {
2540 revents = LTTNG_POLL_GETEV(&events, i);
2541 pollfd = LTTNG_POLL_GETFD(&events, i);
2542
2543 health_code_update();
2544
917a718d
JG
2545 if (pollfd == thread_quit_pipe_fd) {
2546 err = 0;
2547 goto exit;
2548 } else {
2549 /* Event on the registration socket */
2550 if (revents & LPOLLIN) {
2551 continue;
2552 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2553 ERR("Client socket poll error");
2554 goto error;
2555 } else {
2556 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2557 goto error;
2558 }
2559 }
2560 }
2561
2562 DBG("Wait for client response");
2563
2564 health_code_update();
2565
2566 sock = lttcomm_accept_unix_sock(client_sock);
2567 if (sock < 0) {
2568 goto error;
2569 }
2570
2571 /*
2572 * Set the CLOEXEC flag. Return code is useless because either way, the
2573 * show must go on.
2574 */
2575 (void) utils_set_fd_cloexec(sock);
2576
2577 /* Set socket option for credentials retrieval */
2578 ret = lttcomm_setsockopt_creds_unix_sock(sock);
2579 if (ret < 0) {
2580 goto error;
2581 }
2582
917a718d
JG
2583 health_code_update();
2584
2585 /*
2586 * Data is received from the lttng client. The struct
2587 * lttcomm_session_msg (lsm) contains the command and data request of
2588 * the client.
2589 */
2590 DBG("Receiving data from client ...");
3a91de3a
JG
2591 ret = lttcomm_recv_creds_unix_sock(sock, &cmd_ctx.lsm,
2592 sizeof(struct lttcomm_session_msg), &cmd_ctx.creds);
2593 if (ret != sizeof(struct lttcomm_session_msg)) {
2594 DBG("Incomplete recv() from client... continuing");
917a718d
JG
2595 ret = close(sock);
2596 if (ret) {
2597 PERROR("close");
2598 }
2599 sock = -1;
917a718d
JG
2600 continue;
2601 }
2602
2603 health_code_update();
2604
2605 // TODO: Validate cmd_ctx including sanity check for
2606 // security purpose.
2607
2608 rcu_thread_online();
2609 /*
2610 * This function dispatch the work to the kernel or userspace tracer
2611 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2612 * informations for the client. The command context struct contains
2613 * everything this function may needs.
2614 */
3a91de3a 2615 ret = process_client_msg(&cmd_ctx, &sock, &sock_error);
917a718d
JG
2616 rcu_thread_offline();
2617 if (ret < 0) {
3e3665b8
JG
2618 if (sock >= 0) {
2619 ret = close(sock);
2620 if (ret) {
2621 PERROR("close");
2622 }
4a76dfd3
JR
2623 }
2624 sock = -1;
917a718d
JG
2625 /*
2626 * TODO: Inform client somehow of the fatal error. At
2627 * this point, ret < 0 means that a zmalloc failed
2628 * (ENOMEM). Error detected but still accept
2629 * command, unless a socket error has been
2630 * detected.
2631 */
917a718d
JG
2632 continue;
2633 }
2634
c7e9ffbd 2635 if (ret < LTTNG_OK || ret >= LTTNG_ERR_NR) {
7e397c55
FD
2636 WARN("Command returned an invalid status code, returning unknown error: "
2637 "command type = %s (%d), ret = %d",
2638 lttcomm_sessiond_command_str(cmd_ctx.lsm.cmd_type),
2639 cmd_ctx.lsm.cmd_type, ret);
c7e9ffbd
JG
2640 ret = LTTNG_ERR_UNK;
2641 }
2642
917a718d
JG
2643 cmd_completion_handler = cmd_pop_completion_handler();
2644 if (cmd_completion_handler) {
2645 enum lttng_error_code completion_code;
2646
2647 completion_code = cmd_completion_handler->run(
2648 cmd_completion_handler->data);
2649 if (completion_code != LTTNG_OK) {
917a718d
JG
2650 continue;
2651 }
2652 }
2653
2654 health_code_update();
2655
3e3665b8 2656 if (sock >= 0) {
3a91de3a
JG
2657 struct lttng_payload_view view =
2658 lttng_payload_view_from_payload(
2659 &cmd_ctx.reply_payload,
2660 0, -1);
e368fb43 2661 struct lttcomm_lttng_msg *llm = (typeof(
3a91de3a
JG
2662 llm)) cmd_ctx.reply_payload.buffer.data;
2663
37f3c202 2664 assert(cmd_ctx.reply_payload.buffer.size >= sizeof(*llm));
3a91de3a
JG
2665 assert(cmd_ctx.lttng_msg_size == cmd_ctx.reply_payload.buffer.size);
2666
fe489250 2667 llm->fd_count = lttng_payload_view_get_fd_handle_count(&view);
e368fb43 2668
3e3665b8 2669 DBG("Sending response (size: %d, retcode: %s (%d))",
3a91de3a
JG
2670 cmd_ctx.lttng_msg_size,
2671 lttng_strerror(-llm->ret_code),
2672 llm->ret_code);
2673 ret = send_unix_sock(sock, &view);
3e3665b8
JG
2674 if (ret < 0) {
2675 ERR("Failed to send data back to client");
2676 }
917a718d 2677
3e3665b8
JG
2678 /* End of transmission */
2679 ret = close(sock);
2680 if (ret) {
2681 PERROR("close");
2682 }
4a76dfd3
JR
2683 }
2684 sock = -1;
917a718d 2685
917a718d
JG
2686 health_code_update();
2687 }
2688
2689exit:
2690error:
2691 if (sock >= 0) {
2692 ret = close(sock);
2693 if (ret) {
2694 PERROR("close");
2695 }
2696 }
2697
2698 lttng_poll_clean(&events);
917a718d
JG
2699
2700error_listen:
2701error_create_poll:
412d7227 2702 unlink(the_config.client_unix_sock_path.value);
0f68efb6
JG
2703 ret = close(client_sock);
2704 if (ret) {
2705 PERROR("close");
917a718d
JG
2706 }
2707
2708 if (err) {
2709 health_error();
2710 ERR("Health error occurred in %s", __func__);
2711 }
2712
412d7227 2713 health_unregister(the_health_sessiond);
917a718d
JG
2714
2715 DBG("Client thread dying");
3a91de3a 2716 lttng_payload_reset(&cmd_ctx.reply_payload);
917a718d 2717 rcu_unregister_thread();
917a718d
JG
2718 return NULL;
2719}
2720
2721static
2722bool shutdown_client_thread(void *thread_data)
2723{
2724 struct lttng_pipe *client_quit_pipe = thread_data;
2725 const int write_fd = lttng_pipe_get_writefd(client_quit_pipe);
2726
2727 return notify_thread_pipe(write_fd) == 1;
2728}
2729
2730struct lttng_thread *launch_client_thread(void)
2731{
6cb45e93 2732 bool thread_running;
917a718d 2733 struct lttng_pipe *client_quit_pipe;
0f68efb6
JG
2734 struct lttng_thread *thread = NULL;
2735 int client_sock_fd = -1;
917a718d 2736
6cb45e93 2737 sem_init(&thread_state.ready, 0, 0);
917a718d
JG
2738 client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
2739 if (!client_quit_pipe) {
2740 goto error;
2741 }
2742
0f68efb6
JG
2743 client_sock_fd = create_client_sock();
2744 if (client_sock_fd < 0) {
2745 goto error;
2746 }
2747
2748 thread_state.client_sock = client_sock_fd;
917a718d
JG
2749 thread = lttng_thread_create("Client management",
2750 thread_manage_clients,
2751 shutdown_client_thread,
2752 cleanup_client_thread,
2753 client_quit_pipe);
2754 if (!thread) {
2755 goto error;
2756 }
0f68efb6
JG
2757 /* The client thread now owns the client sock fd and the quit pipe. */
2758 client_sock_fd = -1;
2759 client_quit_pipe = NULL;
917a718d
JG
2760
2761 /*
2762 * This thread is part of the threads that need to be fully
2763 * initialized before the session daemon is marked as "ready".
2764 */
6cb45e93
JG
2765 thread_running = wait_thread_status();
2766 if (!thread_running) {
0f68efb6 2767 goto error;
6cb45e93 2768 }
917a718d
JG
2769 return thread;
2770error:
0f68efb6
JG
2771 if (client_sock_fd >= 0) {
2772 if (close(client_sock_fd)) {
2773 PERROR("Failed to close client socket");
2774 }
2775 }
2776 lttng_thread_put(thread);
917a718d
JG
2777 cleanup_client_thread(client_quit_pipe);
2778 return NULL;
2779}
This page took 0.158357 seconds and 4 git commands to generate.