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