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