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