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