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