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