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