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