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