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