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