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