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