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