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