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