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