| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include "agent-thread.hpp" |
| 11 | #include "agent.hpp" |
| 12 | #include "buffer-registry.hpp" |
| 13 | #include "channel.hpp" |
| 14 | #include "cmd.hpp" |
| 15 | #include "consumer.hpp" |
| 16 | #include "event-notifier-error-accounting.hpp" |
| 17 | #include "event.hpp" |
| 18 | #include "health-sessiond.hpp" |
| 19 | #include "kernel-consumer.hpp" |
| 20 | #include "kernel.hpp" |
| 21 | #include "lttng-sessiond.hpp" |
| 22 | #include "lttng-syscall.hpp" |
| 23 | #include "notification-thread-commands.hpp" |
| 24 | #include "notification-thread.hpp" |
| 25 | #include "rotate.hpp" |
| 26 | #include "rotation-thread.hpp" |
| 27 | #include "session.hpp" |
| 28 | #include "timer.hpp" |
| 29 | #include "tracker.hpp" |
| 30 | #include "utils.hpp" |
| 31 | |
| 32 | #include <common/buffer-view.hpp> |
| 33 | #include <common/common.hpp> |
| 34 | #include <common/compat/string.hpp> |
| 35 | #include <common/defaults.hpp> |
| 36 | #include <common/dynamic-buffer.hpp> |
| 37 | #include <common/kernel-ctl/kernel-ctl.hpp> |
| 38 | #include <common/payload-view.hpp> |
| 39 | #include <common/payload.hpp> |
| 40 | #include <common/relayd/relayd.hpp> |
| 41 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 42 | #include <common/string-utils/string-utils.hpp> |
| 43 | #include <common/trace-chunk.hpp> |
| 44 | #include <common/utils.hpp> |
| 45 | |
| 46 | #include <lttng/action/action-internal.hpp> |
| 47 | #include <lttng/action/action.h> |
| 48 | #include <lttng/channel-internal.hpp> |
| 49 | #include <lttng/channel.h> |
| 50 | #include <lttng/condition/condition-internal.hpp> |
| 51 | #include <lttng/condition/condition.h> |
| 52 | #include <lttng/condition/event-rule-matches-internal.hpp> |
| 53 | #include <lttng/condition/event-rule-matches.h> |
| 54 | #include <lttng/error-query-internal.hpp> |
| 55 | #include <lttng/event-internal.hpp> |
| 56 | #include <lttng/event-rule/event-rule-internal.hpp> |
| 57 | #include <lttng/event-rule/event-rule.h> |
| 58 | #include <lttng/location-internal.hpp> |
| 59 | #include <lttng/lttng-error.h> |
| 60 | #include <lttng/rotate-internal.hpp> |
| 61 | #include <lttng/session-descriptor-internal.hpp> |
| 62 | #include <lttng/session-internal.hpp> |
| 63 | #include <lttng/tracker.h> |
| 64 | #include <lttng/trigger/trigger-internal.hpp> |
| 65 | #include <lttng/userspace-probe-internal.hpp> |
| 66 | |
| 67 | #include <algorithm> |
| 68 | #include <inttypes.h> |
| 69 | #include <stdio.h> |
| 70 | #include <sys/stat.h> |
| 71 | #include <urcu/list.h> |
| 72 | #include <urcu/uatomic.h> |
| 73 | |
| 74 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
| 75 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 |
| 76 | |
| 77 | namespace lsu = lttng::sessiond::ust; |
| 78 | |
| 79 | static enum lttng_error_code wait_on_path(void *path); |
| 80 | |
| 81 | namespace { |
| 82 | struct cmd_destroy_session_reply_context { |
| 83 | int reply_sock_fd; |
| 84 | bool implicit_rotation_on_destroy; |
| 85 | /* |
| 86 | * Indicates whether or not an error occurred while launching the |
| 87 | * destruction of a session. |
| 88 | */ |
| 89 | enum lttng_error_code destruction_status; |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * Command completion handler that is used by the destroy command |
| 94 | * when a session that has a non-default shm_path is being destroyed. |
| 95 | * |
| 96 | * See comment in cmd_destroy_session() for the rationale. |
| 97 | */ |
| 98 | struct destroy_completion_handler { |
| 99 | struct cmd_completion_handler handler; |
| 100 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; |
| 101 | } destroy_completion_handler = { |
| 102 | .handler = { .run = wait_on_path, .data = destroy_completion_handler.shm_path }, |
| 103 | .shm_path = { 0 }, |
| 104 | }; |
| 105 | |
| 106 | /* |
| 107 | * Used to keep a unique index for each relayd socket created where this value |
| 108 | * is associated with streams on the consumer so it can match the right relayd |
| 109 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
| 110 | * held. |
| 111 | */ |
| 112 | pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
| 113 | uint64_t relayd_net_seq_idx; |
| 114 | } /* namespace */ |
| 115 | |
| 116 | static struct cmd_completion_handler *current_completion_handler; |
| 117 | static int validate_ust_event_name(const char *); |
| 118 | static int cmd_enable_event_internal(struct ltt_session *session, |
| 119 | const struct lttng_domain *domain, |
| 120 | char *channel_name, |
| 121 | struct lttng_event *event, |
| 122 | char *filter_expression, |
| 123 | struct lttng_bytecode *filter, |
| 124 | struct lttng_event_exclusion *exclusion, |
| 125 | int wpipe); |
| 126 | static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session, |
| 127 | const struct lttng_domain *domain, |
| 128 | const struct lttng_channel *_attr, |
| 129 | int wpipe); |
| 130 | |
| 131 | /* |
| 132 | * Create a session path used by list_lttng_sessions for the case that the |
| 133 | * session consumer is on the network. |
| 134 | */ |
| 135 | static int build_network_session_path(char *dst, size_t size, struct ltt_session *session) |
| 136 | { |
| 137 | int ret, kdata_port, udata_port; |
| 138 | struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr; |
| 139 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; |
| 140 | |
| 141 | LTTNG_ASSERT(session); |
| 142 | LTTNG_ASSERT(dst); |
| 143 | |
| 144 | memset(tmp_urls, 0, sizeof(tmp_urls)); |
| 145 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); |
| 146 | |
| 147 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; |
| 148 | |
| 149 | if (session->kernel_session && session->kernel_session->consumer) { |
| 150 | kuri = &session->kernel_session->consumer->dst.net.control; |
| 151 | kdata_port = session->kernel_session->consumer->dst.net.data.port; |
| 152 | } |
| 153 | |
| 154 | if (session->ust_session && session->ust_session->consumer) { |
| 155 | uuri = &session->ust_session->consumer->dst.net.control; |
| 156 | udata_port = session->ust_session->consumer->dst.net.data.port; |
| 157 | } |
| 158 | |
| 159 | if (uuri == nullptr && kuri == nullptr) { |
| 160 | uri = &session->consumer->dst.net.control; |
| 161 | kdata_port = session->consumer->dst.net.data.port; |
| 162 | } else if (kuri && uuri) { |
| 163 | ret = uri_compare(kuri, uuri); |
| 164 | if (ret) { |
| 165 | /* Not Equal */ |
| 166 | uri = kuri; |
| 167 | /* Build uuri URL string */ |
| 168 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); |
| 169 | if (ret < 0) { |
| 170 | goto error; |
| 171 | } |
| 172 | } else { |
| 173 | uri = kuri; |
| 174 | } |
| 175 | } else if (kuri && uuri == nullptr) { |
| 176 | uri = kuri; |
| 177 | } else if (uuri && kuri == nullptr) { |
| 178 | uri = uuri; |
| 179 | } |
| 180 | |
| 181 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); |
| 182 | if (ret < 0) { |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Do we have a UST url set. If yes, this means we have both kernel and UST |
| 188 | * to print. |
| 189 | */ |
| 190 | if (*tmp_uurl != '\0') { |
| 191 | ret = snprintf(dst, |
| 192 | size, |
| 193 | "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
| 194 | tmp_urls, |
| 195 | kdata_port, |
| 196 | tmp_uurl, |
| 197 | udata_port); |
| 198 | } else { |
| 199 | int dport; |
| 200 | if (kuri || (!kuri && !uuri)) { |
| 201 | dport = kdata_port; |
| 202 | } else { |
| 203 | /* No kernel URI, use the UST port. */ |
| 204 | dport = udata_port; |
| 205 | } |
| 206 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); |
| 207 | } |
| 208 | |
| 209 | error: |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Get run-time attributes if the session has been started (discarded events, |
| 215 | * lost packets). |
| 216 | */ |
| 217 | static int get_kernel_runtime_stats(struct ltt_session *session, |
| 218 | struct ltt_kernel_channel *kchan, |
| 219 | uint64_t *discarded_events, |
| 220 | uint64_t *lost_packets) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | if (!session->has_been_started) { |
| 225 | ret = 0; |
| 226 | *discarded_events = 0; |
| 227 | *lost_packets = 0; |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | ret = consumer_get_discarded_events( |
| 232 | session->id, kchan->key, session->kernel_session->consumer, discarded_events); |
| 233 | if (ret < 0) { |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | ret = consumer_get_lost_packets( |
| 238 | session->id, kchan->key, session->kernel_session->consumer, lost_packets); |
| 239 | if (ret < 0) { |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Get run-time attributes if the session has been started (discarded events, |
| 249 | * lost packets). |
| 250 | */ |
| 251 | static int get_ust_runtime_stats(struct ltt_session *session, |
| 252 | struct ltt_ust_channel *uchan, |
| 253 | uint64_t *discarded_events, |
| 254 | uint64_t *lost_packets) |
| 255 | { |
| 256 | int ret; |
| 257 | struct ltt_ust_session *usess; |
| 258 | |
| 259 | if (!discarded_events || !lost_packets) { |
| 260 | ret = -1; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | usess = session->ust_session; |
| 265 | LTTNG_ASSERT(discarded_events); |
| 266 | LTTNG_ASSERT(lost_packets); |
| 267 | |
| 268 | if (!usess || !session->has_been_started) { |
| 269 | *discarded_events = 0; |
| 270 | *lost_packets = 0; |
| 271 | ret = 0; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 276 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, |
| 277 | &usess->buffer_reg_uid_list, |
| 278 | usess->consumer, |
| 279 | uchan->id, |
| 280 | uchan->attr.overwrite, |
| 281 | discarded_events, |
| 282 | lost_packets); |
| 283 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { |
| 284 | ret = ust_app_pid_get_channel_runtime_stats(usess, |
| 285 | uchan, |
| 286 | usess->consumer, |
| 287 | uchan->attr.overwrite, |
| 288 | discarded_events, |
| 289 | lost_packets); |
| 290 | if (ret < 0) { |
| 291 | goto end; |
| 292 | } |
| 293 | *discarded_events += uchan->per_pid_closed_app_discarded; |
| 294 | *lost_packets += uchan->per_pid_closed_app_lost; |
| 295 | } else { |
| 296 | ERR("Unsupported buffer type"); |
| 297 | abort(); |
| 298 | ret = -1; |
| 299 | goto end; |
| 300 | } |
| 301 | |
| 302 | end: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Create a list of agent domain events. |
| 308 | * |
| 309 | * Return number of events in list on success or else a negative value. |
| 310 | */ |
| 311 | static enum lttng_error_code list_lttng_agent_events(struct agent *agt, |
| 312 | struct lttng_payload *reply_payload, |
| 313 | unsigned int *nb_events) |
| 314 | { |
| 315 | enum lttng_error_code ret_code; |
| 316 | int ret = 0; |
| 317 | unsigned int local_nb_events = 0; |
| 318 | struct agent_event *event; |
| 319 | struct lttng_ht_iter iter; |
| 320 | unsigned long agent_event_count; |
| 321 | |
| 322 | assert(agt); |
| 323 | assert(reply_payload); |
| 324 | |
| 325 | DBG3("Listing agent events"); |
| 326 | |
| 327 | rcu_read_lock(); |
| 328 | agent_event_count = lttng_ht_get_count(agt->events); |
| 329 | if (agent_event_count == 0) { |
| 330 | /* Early exit. */ |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | if (agent_event_count > UINT_MAX) { |
| 335 | ret_code = LTTNG_ERR_OVERFLOW; |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | local_nb_events = (unsigned int) agent_event_count; |
| 340 | |
| 341 | cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) { |
| 342 | struct lttng_event *tmp_event = lttng_event_create(); |
| 343 | |
| 344 | if (!tmp_event) { |
| 345 | ret_code = LTTNG_ERR_NOMEM; |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | if (lttng_strncpy(tmp_event->name, event->name, sizeof(tmp_event->name))) { |
| 350 | lttng_event_destroy(tmp_event); |
| 351 | ret_code = LTTNG_ERR_FATAL; |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | tmp_event->name[sizeof(tmp_event->name) - 1] = '\0'; |
| 356 | tmp_event->enabled = !!event->enabled_count; |
| 357 | tmp_event->loglevel = event->loglevel_value; |
| 358 | tmp_event->loglevel_type = event->loglevel_type; |
| 359 | |
| 360 | ret = lttng_event_serialize( |
| 361 | tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload); |
| 362 | lttng_event_destroy(tmp_event); |
| 363 | if (ret) { |
| 364 | ret_code = LTTNG_ERR_FATAL; |
| 365 | goto error; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | end: |
| 370 | ret_code = LTTNG_OK; |
| 371 | *nb_events = local_nb_events; |
| 372 | error: |
| 373 | rcu_read_unlock(); |
| 374 | return ret_code; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Create a list of ust global domain events. |
| 379 | */ |
| 380 | static enum lttng_error_code list_lttng_ust_global_events(char *channel_name, |
| 381 | struct ltt_ust_domain_global *ust_global, |
| 382 | struct lttng_payload *reply_payload, |
| 383 | unsigned int *nb_events) |
| 384 | { |
| 385 | enum lttng_error_code ret_code; |
| 386 | int ret; |
| 387 | struct lttng_ht_iter iter; |
| 388 | struct lttng_ht_node_str *node; |
| 389 | struct ltt_ust_channel *uchan; |
| 390 | struct ltt_ust_event *uevent; |
| 391 | unsigned long channel_event_count; |
| 392 | unsigned int local_nb_events = 0; |
| 393 | |
| 394 | assert(reply_payload); |
| 395 | assert(nb_events); |
| 396 | |
| 397 | DBG("Listing UST global events for channel %s", channel_name); |
| 398 | |
| 399 | rcu_read_lock(); |
| 400 | |
| 401 | lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter); |
| 402 | node = lttng_ht_iter_get_node_str(&iter); |
| 403 | if (node == nullptr) { |
| 404 | ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 405 | goto error; |
| 406 | } |
| 407 | |
| 408 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); |
| 409 | |
| 410 | channel_event_count = lttng_ht_get_count(uchan->events); |
| 411 | if (channel_event_count == 0) { |
| 412 | /* Early exit. */ |
| 413 | ret_code = LTTNG_OK; |
| 414 | goto end; |
| 415 | } |
| 416 | |
| 417 | if (channel_event_count > UINT_MAX) { |
| 418 | ret_code = LTTNG_ERR_OVERFLOW; |
| 419 | goto error; |
| 420 | } |
| 421 | |
| 422 | local_nb_events = (unsigned int) channel_event_count; |
| 423 | |
| 424 | DBG3("Listing UST global %d events", *nb_events); |
| 425 | |
| 426 | cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) { |
| 427 | struct lttng_event *tmp_event = nullptr; |
| 428 | |
| 429 | if (uevent->internal) { |
| 430 | /* This event should remain hidden from clients */ |
| 431 | local_nb_events--; |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | tmp_event = lttng_event_create(); |
| 436 | if (!tmp_event) { |
| 437 | ret_code = LTTNG_ERR_NOMEM; |
| 438 | goto error; |
| 439 | } |
| 440 | |
| 441 | if (lttng_strncpy(tmp_event->name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN)) { |
| 442 | ret_code = LTTNG_ERR_FATAL; |
| 443 | lttng_event_destroy(tmp_event); |
| 444 | goto error; |
| 445 | } |
| 446 | |
| 447 | tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 448 | tmp_event->enabled = uevent->enabled; |
| 449 | |
| 450 | switch (uevent->attr.instrumentation) { |
| 451 | case LTTNG_UST_ABI_TRACEPOINT: |
| 452 | tmp_event->type = LTTNG_EVENT_TRACEPOINT; |
| 453 | break; |
| 454 | case LTTNG_UST_ABI_PROBE: |
| 455 | tmp_event->type = LTTNG_EVENT_PROBE; |
| 456 | break; |
| 457 | case LTTNG_UST_ABI_FUNCTION: |
| 458 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | tmp_event->loglevel = uevent->attr.loglevel; |
| 463 | switch (uevent->attr.loglevel_type) { |
| 464 | case LTTNG_UST_ABI_LOGLEVEL_ALL: |
| 465 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 466 | break; |
| 467 | case LTTNG_UST_ABI_LOGLEVEL_RANGE: |
| 468 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 469 | break; |
| 470 | case LTTNG_UST_ABI_LOGLEVEL_SINGLE: |
| 471 | tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 472 | break; |
| 473 | } |
| 474 | if (uevent->filter) { |
| 475 | tmp_event->filter = 1; |
| 476 | } |
| 477 | if (uevent->exclusion) { |
| 478 | tmp_event->exclusion = 1; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * We do not care about the filter bytecode and the fd from the |
| 483 | * userspace_probe_location. |
| 484 | */ |
| 485 | ret = lttng_event_serialize(tmp_event, |
| 486 | uevent->exclusion ? uevent->exclusion->count : 0, |
| 487 | uevent->exclusion ? (char **) uevent->exclusion->names : |
| 488 | nullptr, |
| 489 | uevent->filter_expression, |
| 490 | 0, |
| 491 | nullptr, |
| 492 | reply_payload); |
| 493 | lttng_event_destroy(tmp_event); |
| 494 | if (ret) { |
| 495 | ret_code = LTTNG_ERR_FATAL; |
| 496 | goto error; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | end: |
| 501 | /* nb_events is already set at this point. */ |
| 502 | ret_code = LTTNG_OK; |
| 503 | *nb_events = local_nb_events; |
| 504 | error: |
| 505 | rcu_read_unlock(); |
| 506 | return ret_code; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Fill lttng_event array of all kernel events in the channel. |
| 511 | */ |
| 512 | static enum lttng_error_code list_lttng_kernel_events(char *channel_name, |
| 513 | struct ltt_kernel_session *kernel_session, |
| 514 | struct lttng_payload *reply_payload, |
| 515 | unsigned int *nb_events) |
| 516 | { |
| 517 | enum lttng_error_code ret_code; |
| 518 | int ret; |
| 519 | struct ltt_kernel_event *event; |
| 520 | struct ltt_kernel_channel *kchan; |
| 521 | |
| 522 | assert(reply_payload); |
| 523 | |
| 524 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); |
| 525 | if (kchan == nullptr) { |
| 526 | ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 527 | goto end; |
| 528 | } |
| 529 | |
| 530 | *nb_events = kchan->event_count; |
| 531 | |
| 532 | DBG("Listing events for channel %s", kchan->channel->name); |
| 533 | |
| 534 | if (*nb_events == 0) { |
| 535 | ret_code = LTTNG_OK; |
| 536 | goto end; |
| 537 | } |
| 538 | |
| 539 | /* Kernel channels */ |
| 540 | cds_list_for_each_entry (event, &kchan->events_list.head, list) { |
| 541 | struct lttng_event *tmp_event = lttng_event_create(); |
| 542 | |
| 543 | if (!tmp_event) { |
| 544 | ret_code = LTTNG_ERR_NOMEM; |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 548 | if (lttng_strncpy(tmp_event->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) { |
| 549 | lttng_event_destroy(tmp_event); |
| 550 | ret_code = LTTNG_ERR_FATAL; |
| 551 | goto end; |
| 552 | } |
| 553 | |
| 554 | tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 555 | tmp_event->enabled = event->enabled; |
| 556 | tmp_event->filter = (unsigned char) !!event->filter_expression; |
| 557 | |
| 558 | switch (event->event->instrumentation) { |
| 559 | case LTTNG_KERNEL_ABI_TRACEPOINT: |
| 560 | tmp_event->type = LTTNG_EVENT_TRACEPOINT; |
| 561 | break; |
| 562 | case LTTNG_KERNEL_ABI_KRETPROBE: |
| 563 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 564 | memcpy(&tmp_event->attr.probe, |
| 565 | &event->event->u.kprobe, |
| 566 | sizeof(struct lttng_kernel_abi_kprobe)); |
| 567 | break; |
| 568 | case LTTNG_KERNEL_ABI_KPROBE: |
| 569 | tmp_event->type = LTTNG_EVENT_PROBE; |
| 570 | memcpy(&tmp_event->attr.probe, |
| 571 | &event->event->u.kprobe, |
| 572 | sizeof(struct lttng_kernel_abi_kprobe)); |
| 573 | break; |
| 574 | case LTTNG_KERNEL_ABI_UPROBE: |
| 575 | tmp_event->type = LTTNG_EVENT_USERSPACE_PROBE; |
| 576 | break; |
| 577 | case LTTNG_KERNEL_ABI_FUNCTION: |
| 578 | tmp_event->type = LTTNG_EVENT_FUNCTION; |
| 579 | memcpy(&(tmp_event->attr.ftrace), |
| 580 | &event->event->u.ftrace, |
| 581 | sizeof(struct lttng_kernel_abi_function)); |
| 582 | break; |
| 583 | case LTTNG_KERNEL_ABI_NOOP: |
| 584 | tmp_event->type = LTTNG_EVENT_NOOP; |
| 585 | break; |
| 586 | case LTTNG_KERNEL_ABI_SYSCALL: |
| 587 | tmp_event->type = LTTNG_EVENT_SYSCALL; |
| 588 | break; |
| 589 | case LTTNG_KERNEL_ABI_ALL: |
| 590 | /* fall-through. */ |
| 591 | default: |
| 592 | abort(); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (event->userspace_probe_location) { |
| 597 | struct lttng_userspace_probe_location *location_copy = |
| 598 | lttng_userspace_probe_location_copy( |
| 599 | event->userspace_probe_location); |
| 600 | |
| 601 | if (!location_copy) { |
| 602 | lttng_event_destroy(tmp_event); |
| 603 | ret_code = LTTNG_ERR_NOMEM; |
| 604 | goto end; |
| 605 | } |
| 606 | |
| 607 | ret = lttng_event_set_userspace_probe_location(tmp_event, location_copy); |
| 608 | if (ret) { |
| 609 | lttng_event_destroy(tmp_event); |
| 610 | lttng_userspace_probe_location_destroy(location_copy); |
| 611 | ret_code = LTTNG_ERR_INVALID; |
| 612 | goto end; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | ret = lttng_event_serialize( |
| 617 | tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload); |
| 618 | lttng_event_destroy(tmp_event); |
| 619 | if (ret) { |
| 620 | ret_code = LTTNG_ERR_FATAL; |
| 621 | goto end; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | ret_code = LTTNG_OK; |
| 626 | end: |
| 627 | return ret_code; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Add URI so the consumer output object. Set the correct path depending on the |
| 632 | * domain adding the default trace directory. |
| 633 | */ |
| 634 | static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session, |
| 635 | struct consumer_output *consumer, |
| 636 | struct lttng_uri *uri, |
| 637 | enum lttng_domain_type domain) |
| 638 | { |
| 639 | int ret; |
| 640 | enum lttng_error_code ret_code = LTTNG_OK; |
| 641 | |
| 642 | LTTNG_ASSERT(uri); |
| 643 | |
| 644 | if (consumer == nullptr) { |
| 645 | DBG("No consumer detected. Don't add URI. Stopping."); |
| 646 | ret_code = LTTNG_ERR_NO_CONSUMER; |
| 647 | goto error; |
| 648 | } |
| 649 | |
| 650 | switch (domain) { |
| 651 | case LTTNG_DOMAIN_KERNEL: |
| 652 | ret = lttng_strncpy(consumer->domain_subdir, |
| 653 | DEFAULT_KERNEL_TRACE_DIR, |
| 654 | sizeof(consumer->domain_subdir)); |
| 655 | break; |
| 656 | case LTTNG_DOMAIN_UST: |
| 657 | ret = lttng_strncpy(consumer->domain_subdir, |
| 658 | DEFAULT_UST_TRACE_DIR, |
| 659 | sizeof(consumer->domain_subdir)); |
| 660 | break; |
| 661 | default: |
| 662 | /* |
| 663 | * This case is possible is we try to add the URI to the global |
| 664 | * tracing session consumer object which in this case there is |
| 665 | * no subdir. |
| 666 | */ |
| 667 | memset(consumer->domain_subdir, 0, sizeof(consumer->domain_subdir)); |
| 668 | ret = 0; |
| 669 | } |
| 670 | if (ret) { |
| 671 | ERR("Failed to initialize consumer output domain subdirectory"); |
| 672 | ret_code = LTTNG_ERR_FATAL; |
| 673 | goto error; |
| 674 | } |
| 675 | |
| 676 | switch (uri->dtype) { |
| 677 | case LTTNG_DST_IPV4: |
| 678 | case LTTNG_DST_IPV6: |
| 679 | DBG2("Setting network URI to consumer"); |
| 680 | |
| 681 | if (consumer->type == CONSUMER_DST_NET) { |
| 682 | if ((uri->stype == LTTNG_STREAM_CONTROL && |
| 683 | consumer->dst.net.control_isset) || |
| 684 | (uri->stype == LTTNG_STREAM_DATA && consumer->dst.net.data_isset)) { |
| 685 | ret_code = LTTNG_ERR_URL_EXIST; |
| 686 | goto error; |
| 687 | } |
| 688 | } else { |
| 689 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
| 690 | } |
| 691 | |
| 692 | /* Set URI into consumer output object */ |
| 693 | ret = consumer_set_network_uri(session, consumer, uri); |
| 694 | if (ret < 0) { |
| 695 | ret_code = (lttng_error_code) -ret; |
| 696 | goto error; |
| 697 | } else if (ret == 1) { |
| 698 | /* |
| 699 | * URI was the same in the consumer so we do not append the subdir |
| 700 | * again so to not duplicate output dir. |
| 701 | */ |
| 702 | ret_code = LTTNG_OK; |
| 703 | goto error; |
| 704 | } |
| 705 | break; |
| 706 | case LTTNG_DST_PATH: |
| 707 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
| 708 | ret_code = LTTNG_ERR_INVALID; |
| 709 | goto error; |
| 710 | } |
| 711 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); |
| 712 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
| 713 | |
| 714 | ret = lttng_strncpy(consumer->dst.session_root_path, |
| 715 | uri->dst.path, |
| 716 | sizeof(consumer->dst.session_root_path)); |
| 717 | if (ret) { |
| 718 | ret_code = LTTNG_ERR_FATAL; |
| 719 | goto error; |
| 720 | } |
| 721 | consumer->type = CONSUMER_DST_LOCAL; |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | ret_code = LTTNG_OK; |
| 726 | error: |
| 727 | return ret_code; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Init tracing by creating trace directory and sending fds kernel consumer. |
| 732 | */ |
| 733 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
| 734 | { |
| 735 | int ret = 0; |
| 736 | struct lttng_ht_iter iter; |
| 737 | struct consumer_socket *socket; |
| 738 | |
| 739 | LTTNG_ASSERT(session); |
| 740 | |
| 741 | rcu_read_lock(); |
| 742 | |
| 743 | if (session->consumer_fds_sent == 0 && session->consumer != nullptr) { |
| 744 | cds_lfht_for_each_entry ( |
| 745 | session->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 746 | pthread_mutex_lock(socket->lock); |
| 747 | ret = kernel_consumer_send_session(socket, session); |
| 748 | pthread_mutex_unlock(socket->lock); |
| 749 | if (ret < 0) { |
| 750 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 751 | goto error; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | error: |
| 757 | rcu_read_unlock(); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Create a socket to the relayd using the URI. |
| 763 | * |
| 764 | * On success, the relayd_sock pointer is set to the created socket. |
| 765 | * Else, it remains untouched and an LTTng error code is returned. |
| 766 | */ |
| 767 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
| 768 | struct lttcomm_relayd_sock **relayd_sock, |
| 769 | struct consumer_output *consumer) |
| 770 | { |
| 771 | int ret; |
| 772 | enum lttng_error_code status = LTTNG_OK; |
| 773 | struct lttcomm_relayd_sock *rsock; |
| 774 | |
| 775 | rsock = lttcomm_alloc_relayd_sock( |
| 776 | uri, RELAYD_VERSION_COMM_MAJOR, RELAYD_VERSION_COMM_MINOR); |
| 777 | if (!rsock) { |
| 778 | status = LTTNG_ERR_FATAL; |
| 779 | goto error; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Connect to relayd so we can proceed with a session creation. This call |
| 784 | * can possibly block for an arbitrary amount of time to set the health |
| 785 | * state to be in poll execution. |
| 786 | */ |
| 787 | health_poll_entry(); |
| 788 | ret = relayd_connect(rsock); |
| 789 | health_poll_exit(); |
| 790 | if (ret < 0) { |
| 791 | ERR("Unable to reach lttng-relayd"); |
| 792 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 793 | goto free_sock; |
| 794 | } |
| 795 | |
| 796 | /* Create socket for control stream. */ |
| 797 | if (uri->stype == LTTNG_STREAM_CONTROL) { |
| 798 | uint64_t result_flags; |
| 799 | |
| 800 | DBG3("Creating relayd stream socket from URI"); |
| 801 | |
| 802 | /* Check relayd version */ |
| 803 | ret = relayd_version_check(rsock); |
| 804 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
| 805 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 806 | goto close_sock; |
| 807 | } else if (ret < 0) { |
| 808 | ERR("Unable to reach lttng-relayd"); |
| 809 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 810 | goto close_sock; |
| 811 | } |
| 812 | consumer->relay_major_version = rsock->major; |
| 813 | consumer->relay_minor_version = rsock->minor; |
| 814 | ret = relayd_get_configuration(rsock, 0, &result_flags); |
| 815 | if (ret < 0) { |
| 816 | ERR("Unable to get relayd configuration"); |
| 817 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
| 818 | goto close_sock; |
| 819 | } |
| 820 | if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) { |
| 821 | consumer->relay_allows_clear = true; |
| 822 | } |
| 823 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
| 824 | DBG3("Creating relayd data socket from URI"); |
| 825 | } else { |
| 826 | /* Command is not valid */ |
| 827 | ERR("Relayd invalid stream type: %d", uri->stype); |
| 828 | status = LTTNG_ERR_INVALID; |
| 829 | goto close_sock; |
| 830 | } |
| 831 | |
| 832 | *relayd_sock = rsock; |
| 833 | |
| 834 | return status; |
| 835 | |
| 836 | close_sock: |
| 837 | /* The returned value is not useful since we are on an error path. */ |
| 838 | (void) relayd_close(rsock); |
| 839 | free_sock: |
| 840 | free(rsock); |
| 841 | error: |
| 842 | return status; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Connect to the relayd using URI and send the socket to the right consumer. |
| 847 | * |
| 848 | * The consumer socket lock must be held by the caller. |
| 849 | * |
| 850 | * Returns LTTNG_OK on success or an LTTng error code on failure. |
| 851 | */ |
| 852 | static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id, |
| 853 | struct lttng_uri *relayd_uri, |
| 854 | struct consumer_output *consumer, |
| 855 | struct consumer_socket *consumer_sock, |
| 856 | const char *session_name, |
| 857 | const char *hostname, |
| 858 | const char *base_path, |
| 859 | int session_live_timer, |
| 860 | const uint64_t *current_chunk_id, |
| 861 | time_t session_creation_time, |
| 862 | bool session_name_contains_creation_time) |
| 863 | { |
| 864 | int ret; |
| 865 | struct lttcomm_relayd_sock *rsock = nullptr; |
| 866 | enum lttng_error_code status; |
| 867 | |
| 868 | /* Connect to relayd and make version check if uri is the control. */ |
| 869 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
| 870 | if (status != LTTNG_OK) { |
| 871 | goto relayd_comm_error; |
| 872 | } |
| 873 | LTTNG_ASSERT(rsock); |
| 874 | |
| 875 | /* Set the network sequence index if not set. */ |
| 876 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
| 877 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 878 | /* |
| 879 | * Increment net_seq_idx because we are about to transfer the |
| 880 | * new relayd socket to the consumer. |
| 881 | * Assign unique key so the consumer can match streams. |
| 882 | */ |
| 883 | consumer->net_seq_index = ++relayd_net_seq_idx; |
| 884 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 885 | } |
| 886 | |
| 887 | /* Send relayd socket to consumer. */ |
| 888 | ret = consumer_send_relayd_socket(consumer_sock, |
| 889 | rsock, |
| 890 | consumer, |
| 891 | relayd_uri->stype, |
| 892 | session_id, |
| 893 | session_name, |
| 894 | hostname, |
| 895 | base_path, |
| 896 | session_live_timer, |
| 897 | current_chunk_id, |
| 898 | session_creation_time, |
| 899 | session_name_contains_creation_time); |
| 900 | if (ret < 0) { |
| 901 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
| 902 | goto close_sock; |
| 903 | } |
| 904 | |
| 905 | /* Flag that the corresponding socket was sent. */ |
| 906 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { |
| 907 | consumer_sock->control_sock_sent = 1; |
| 908 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
| 909 | consumer_sock->data_sock_sent = 1; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * Close socket which was dup on the consumer side. The session daemon does |
| 914 | * NOT keep track of the relayd socket(s) once transfer to the consumer. |
| 915 | */ |
| 916 | |
| 917 | close_sock: |
| 918 | if (status != LTTNG_OK) { |
| 919 | /* |
| 920 | * The consumer output for this session should not be used anymore |
| 921 | * since the relayd connection failed thus making any tracing or/and |
| 922 | * streaming not usable. |
| 923 | */ |
| 924 | consumer->enabled = 0; |
| 925 | } |
| 926 | (void) relayd_close(rsock); |
| 927 | free(rsock); |
| 928 | |
| 929 | relayd_comm_error: |
| 930 | return status; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Send both relayd sockets to a specific consumer and domain. This is a |
| 935 | * helper function to facilitate sending the information to the consumer for a |
| 936 | * session. |
| 937 | * |
| 938 | * The consumer socket lock must be held by the caller. |
| 939 | * |
| 940 | * Returns LTTNG_OK, or an LTTng error code on failure. |
| 941 | */ |
| 942 | static enum lttng_error_code send_consumer_relayd_sockets(unsigned int session_id, |
| 943 | struct consumer_output *consumer, |
| 944 | struct consumer_socket *sock, |
| 945 | const char *session_name, |
| 946 | const char *hostname, |
| 947 | const char *base_path, |
| 948 | int session_live_timer, |
| 949 | const uint64_t *current_chunk_id, |
| 950 | time_t session_creation_time, |
| 951 | bool session_name_contains_creation_time) |
| 952 | { |
| 953 | enum lttng_error_code status = LTTNG_OK; |
| 954 | |
| 955 | LTTNG_ASSERT(consumer); |
| 956 | LTTNG_ASSERT(sock); |
| 957 | |
| 958 | /* Sending control relayd socket. */ |
| 959 | if (!sock->control_sock_sent) { |
| 960 | status = send_consumer_relayd_socket(session_id, |
| 961 | &consumer->dst.net.control, |
| 962 | consumer, |
| 963 | sock, |
| 964 | session_name, |
| 965 | hostname, |
| 966 | base_path, |
| 967 | session_live_timer, |
| 968 | current_chunk_id, |
| 969 | session_creation_time, |
| 970 | session_name_contains_creation_time); |
| 971 | if (status != LTTNG_OK) { |
| 972 | goto error; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /* Sending data relayd socket. */ |
| 977 | if (!sock->data_sock_sent) { |
| 978 | status = send_consumer_relayd_socket(session_id, |
| 979 | &consumer->dst.net.data, |
| 980 | consumer, |
| 981 | sock, |
| 982 | session_name, |
| 983 | hostname, |
| 984 | base_path, |
| 985 | session_live_timer, |
| 986 | current_chunk_id, |
| 987 | session_creation_time, |
| 988 | session_name_contains_creation_time); |
| 989 | if (status != LTTNG_OK) { |
| 990 | goto error; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | error: |
| 995 | return status; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Setup relayd connections for a tracing session. First creates the socket to |
| 1000 | * the relayd and send them to the right domain consumer. Consumer type MUST be |
| 1001 | * network. |
| 1002 | */ |
| 1003 | int cmd_setup_relayd(struct ltt_session *session) |
| 1004 | { |
| 1005 | int ret = LTTNG_OK; |
| 1006 | struct ltt_ust_session *usess; |
| 1007 | struct ltt_kernel_session *ksess; |
| 1008 | struct consumer_socket *socket; |
| 1009 | struct lttng_ht_iter iter; |
| 1010 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
| 1011 | |
| 1012 | LTTNG_ASSERT(session); |
| 1013 | |
| 1014 | usess = session->ust_session; |
| 1015 | ksess = session->kernel_session; |
| 1016 | |
| 1017 | DBG("Setting relayd for session %s", session->name); |
| 1018 | |
| 1019 | rcu_read_lock(); |
| 1020 | if (session->current_trace_chunk) { |
| 1021 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( |
| 1022 | session->current_trace_chunk, ¤t_chunk_id.value); |
| 1023 | |
| 1024 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1025 | current_chunk_id.is_set = true; |
| 1026 | } else { |
| 1027 | ERR("Failed to get current trace chunk id"); |
| 1028 | ret = LTTNG_ERR_UNK; |
| 1029 | goto error; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET && |
| 1034 | usess->consumer->enabled) { |
| 1035 | /* For each consumer socket, send relayd sockets */ |
| 1036 | cds_lfht_for_each_entry ( |
| 1037 | usess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1038 | pthread_mutex_lock(socket->lock); |
| 1039 | ret = send_consumer_relayd_sockets( |
| 1040 | session->id, |
| 1041 | usess->consumer, |
| 1042 | socket, |
| 1043 | session->name, |
| 1044 | session->hostname, |
| 1045 | session->base_path, |
| 1046 | session->live_timer, |
| 1047 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 1048 | session->creation_time, |
| 1049 | session->name_contains_creation_time); |
| 1050 | pthread_mutex_unlock(socket->lock); |
| 1051 | if (ret != LTTNG_OK) { |
| 1052 | goto error; |
| 1053 | } |
| 1054 | /* Session is now ready for network streaming. */ |
| 1055 | session->net_handle = 1; |
| 1056 | } |
| 1057 | session->consumer->relay_major_version = usess->consumer->relay_major_version; |
| 1058 | session->consumer->relay_minor_version = usess->consumer->relay_minor_version; |
| 1059 | session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear; |
| 1060 | } |
| 1061 | |
| 1062 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET && |
| 1063 | ksess->consumer->enabled) { |
| 1064 | cds_lfht_for_each_entry ( |
| 1065 | ksess->consumer->socks->ht, &iter.iter, socket, node.node) { |
| 1066 | pthread_mutex_lock(socket->lock); |
| 1067 | ret = send_consumer_relayd_sockets( |
| 1068 | session->id, |
| 1069 | ksess->consumer, |
| 1070 | socket, |
| 1071 | session->name, |
| 1072 | session->hostname, |
| 1073 | session->base_path, |
| 1074 | session->live_timer, |
| 1075 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 1076 | session->creation_time, |
| 1077 | session->name_contains_creation_time); |
| 1078 | pthread_mutex_unlock(socket->lock); |
| 1079 | if (ret != LTTNG_OK) { |
| 1080 | goto error; |
| 1081 | } |
| 1082 | /* Session is now ready for network streaming. */ |
| 1083 | session->net_handle = 1; |
| 1084 | } |
| 1085 | session->consumer->relay_major_version = ksess->consumer->relay_major_version; |
| 1086 | session->consumer->relay_minor_version = ksess->consumer->relay_minor_version; |
| 1087 | session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear; |
| 1088 | } |
| 1089 | |
| 1090 | error: |
| 1091 | rcu_read_unlock(); |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Start a kernel session by opening all necessary streams. |
| 1097 | */ |
| 1098 | int start_kernel_session(struct ltt_kernel_session *ksess) |
| 1099 | { |
| 1100 | int ret; |
| 1101 | struct ltt_kernel_channel *kchan; |
| 1102 | |
| 1103 | /* Open kernel metadata */ |
| 1104 | if (ksess->metadata == nullptr && ksess->output_traces) { |
| 1105 | ret = kernel_open_metadata(ksess); |
| 1106 | if (ret < 0) { |
| 1107 | ret = LTTNG_ERR_KERN_META_FAIL; |
| 1108 | goto error; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* Open kernel metadata stream */ |
| 1113 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
| 1114 | ret = kernel_open_metadata_stream(ksess); |
| 1115 | if (ret < 0) { |
| 1116 | ERR("Kernel create metadata stream failed"); |
| 1117 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 1118 | goto error; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* For each channel */ |
| 1123 | cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) { |
| 1124 | if (kchan->stream_count == 0) { |
| 1125 | ret = kernel_open_channel_stream(kchan); |
| 1126 | if (ret < 0) { |
| 1127 | ret = LTTNG_ERR_KERN_STREAM_FAIL; |
| 1128 | goto error; |
| 1129 | } |
| 1130 | /* Update the stream global counter */ |
| 1131 | ksess->stream_count_global += ret; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /* Setup kernel consumer socket and send fds to it */ |
| 1136 | ret = init_kernel_tracing(ksess); |
| 1137 | if (ret != 0) { |
| 1138 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 1139 | goto error; |
| 1140 | } |
| 1141 | |
| 1142 | /* This start the kernel tracing */ |
| 1143 | ret = kernel_start_session(ksess); |
| 1144 | if (ret < 0) { |
| 1145 | ret = LTTNG_ERR_KERN_START_FAIL; |
| 1146 | goto error; |
| 1147 | } |
| 1148 | |
| 1149 | /* Quiescent wait after starting trace */ |
| 1150 | kernel_wait_quiescent(); |
| 1151 | |
| 1152 | ksess->active = 1; |
| 1153 | |
| 1154 | ret = LTTNG_OK; |
| 1155 | |
| 1156 | error: |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | int stop_kernel_session(struct ltt_kernel_session *ksess) |
| 1161 | { |
| 1162 | struct ltt_kernel_channel *kchan; |
| 1163 | bool error_occurred = false; |
| 1164 | int ret; |
| 1165 | |
| 1166 | if (!ksess || !ksess->active) { |
| 1167 | return LTTNG_OK; |
| 1168 | } |
| 1169 | DBG("Stopping kernel tracing"); |
| 1170 | |
| 1171 | ret = kernel_stop_session(ksess); |
| 1172 | if (ret < 0) { |
| 1173 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
| 1174 | goto error; |
| 1175 | } |
| 1176 | |
| 1177 | kernel_wait_quiescent(); |
| 1178 | |
| 1179 | /* Flush metadata after stopping (if exists) */ |
| 1180 | if (ksess->metadata_stream_fd >= 0) { |
| 1181 | ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd); |
| 1182 | if (ret < 0) { |
| 1183 | ERR("Kernel metadata flush failed"); |
| 1184 | error_occurred = true; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* Flush all buffers after stopping */ |
| 1189 | cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) { |
| 1190 | ret = kernel_flush_buffer(kchan); |
| 1191 | if (ret < 0) { |
| 1192 | ERR("Kernel flush buffer error"); |
| 1193 | error_occurred = true; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | ksess->active = 0; |
| 1198 | if (error_occurred) { |
| 1199 | ret = LTTNG_ERR_UNK; |
| 1200 | } else { |
| 1201 | ret = LTTNG_OK; |
| 1202 | } |
| 1203 | error: |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
| 1209 | */ |
| 1210 | int cmd_disable_channel(struct ltt_session *session, |
| 1211 | enum lttng_domain_type domain, |
| 1212 | char *channel_name) |
| 1213 | { |
| 1214 | int ret; |
| 1215 | struct ltt_ust_session *usess; |
| 1216 | |
| 1217 | usess = session->ust_session; |
| 1218 | |
| 1219 | rcu_read_lock(); |
| 1220 | |
| 1221 | switch (domain) { |
| 1222 | case LTTNG_DOMAIN_KERNEL: |
| 1223 | { |
| 1224 | ret = channel_kernel_disable(session->kernel_session, channel_name); |
| 1225 | if (ret != LTTNG_OK) { |
| 1226 | goto error; |
| 1227 | } |
| 1228 | |
| 1229 | kernel_wait_quiescent(); |
| 1230 | break; |
| 1231 | } |
| 1232 | case LTTNG_DOMAIN_UST: |
| 1233 | { |
| 1234 | struct ltt_ust_channel *uchan; |
| 1235 | struct lttng_ht *chan_ht; |
| 1236 | |
| 1237 | chan_ht = usess->domain_global.channels; |
| 1238 | |
| 1239 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 1240 | if (uchan == nullptr) { |
| 1241 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1242 | goto error; |
| 1243 | } |
| 1244 | |
| 1245 | ret = channel_ust_disable(usess, uchan); |
| 1246 | if (ret != LTTNG_OK) { |
| 1247 | goto error; |
| 1248 | } |
| 1249 | break; |
| 1250 | } |
| 1251 | default: |
| 1252 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1253 | goto error; |
| 1254 | } |
| 1255 | |
| 1256 | ret = LTTNG_OK; |
| 1257 | |
| 1258 | error: |
| 1259 | rcu_read_unlock(); |
| 1260 | return ret; |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. |
| 1265 | * |
| 1266 | * The wpipe arguments is used as a notifier for the kernel thread. |
| 1267 | */ |
| 1268 | int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe) |
| 1269 | { |
| 1270 | int ret; |
| 1271 | size_t channel_len; |
| 1272 | ssize_t sock_recv_len; |
| 1273 | struct lttng_channel *channel = nullptr; |
| 1274 | struct lttng_buffer_view view; |
| 1275 | struct lttng_dynamic_buffer channel_buffer; |
| 1276 | const struct lttng_domain command_domain = cmd_ctx->lsm.domain; |
| 1277 | |
| 1278 | lttng_dynamic_buffer_init(&channel_buffer); |
| 1279 | channel_len = (size_t) cmd_ctx->lsm.u.channel.length; |
| 1280 | ret = lttng_dynamic_buffer_set_size(&channel_buffer, channel_len); |
| 1281 | if (ret) { |
| 1282 | ret = LTTNG_ERR_NOMEM; |
| 1283 | goto end; |
| 1284 | } |
| 1285 | |
| 1286 | sock_recv_len = lttcomm_recv_unix_sock(sock, channel_buffer.data, channel_len); |
| 1287 | if (sock_recv_len < 0 || sock_recv_len != channel_len) { |
| 1288 | ERR("Failed to receive \"enable channel\" command payload"); |
| 1289 | ret = LTTNG_ERR_INVALID; |
| 1290 | goto end; |
| 1291 | } |
| 1292 | |
| 1293 | view = lttng_buffer_view_from_dynamic_buffer(&channel_buffer, 0, channel_len); |
| 1294 | if (!lttng_buffer_view_is_valid(&view)) { |
| 1295 | ret = LTTNG_ERR_INVALID; |
| 1296 | goto end; |
| 1297 | } |
| 1298 | |
| 1299 | if (lttng_channel_create_from_buffer(&view, &channel) != channel_len) { |
| 1300 | ERR("Invalid channel payload received in \"enable channel\" command"); |
| 1301 | ret = LTTNG_ERR_INVALID; |
| 1302 | goto end; |
| 1303 | } |
| 1304 | |
| 1305 | ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe); |
| 1306 | |
| 1307 | end: |
| 1308 | lttng_dynamic_buffer_reset(&channel_buffer); |
| 1309 | lttng_channel_destroy(channel); |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session, |
| 1314 | const struct lttng_domain *domain, |
| 1315 | const struct lttng_channel *_attr, |
| 1316 | int wpipe) |
| 1317 | { |
| 1318 | enum lttng_error_code ret_code; |
| 1319 | struct ltt_ust_session *usess = session->ust_session; |
| 1320 | struct lttng_ht *chan_ht; |
| 1321 | size_t len; |
| 1322 | struct lttng_channel *attr = nullptr; |
| 1323 | |
| 1324 | LTTNG_ASSERT(session); |
| 1325 | LTTNG_ASSERT(_attr); |
| 1326 | LTTNG_ASSERT(domain); |
| 1327 | |
| 1328 | attr = lttng_channel_copy(_attr); |
| 1329 | if (!attr) { |
| 1330 | ret_code = LTTNG_ERR_NOMEM; |
| 1331 | goto end; |
| 1332 | } |
| 1333 | |
| 1334 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
| 1335 | |
| 1336 | /* Validate channel name */ |
| 1337 | if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) { |
| 1338 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1339 | goto end; |
| 1340 | } |
| 1341 | |
| 1342 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
| 1343 | |
| 1344 | rcu_read_lock(); |
| 1345 | |
| 1346 | /* |
| 1347 | * If the session is a live session, remove the switch timer, the |
| 1348 | * live timer does the same thing but sends also synchronisation |
| 1349 | * beacons for inactive streams. |
| 1350 | */ |
| 1351 | if (session->live_timer > 0) { |
| 1352 | attr->attr.live_timer_interval = session->live_timer; |
| 1353 | attr->attr.switch_timer_interval = 0; |
| 1354 | } |
| 1355 | |
| 1356 | /* Check for feature support */ |
| 1357 | switch (domain->type) { |
| 1358 | case LTTNG_DOMAIN_KERNEL: |
| 1359 | { |
| 1360 | if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) { |
| 1361 | /* Sampling position of buffer is not supported */ |
| 1362 | WARN("Kernel tracer does not support buffer monitoring. " |
| 1363 | "Setting the monitor interval timer to 0 " |
| 1364 | "(disabled) for channel '%s' of session '%s'", |
| 1365 | attr->name, |
| 1366 | session->name); |
| 1367 | lttng_channel_set_monitor_timer_interval(attr, 0); |
| 1368 | } |
| 1369 | break; |
| 1370 | } |
| 1371 | case LTTNG_DOMAIN_UST: |
| 1372 | break; |
| 1373 | case LTTNG_DOMAIN_JUL: |
| 1374 | case LTTNG_DOMAIN_LOG4J: |
| 1375 | case LTTNG_DOMAIN_PYTHON: |
| 1376 | if (!agent_tracing_is_enabled()) { |
| 1377 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); |
| 1378 | ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED; |
| 1379 | goto error; |
| 1380 | } |
| 1381 | break; |
| 1382 | default: |
| 1383 | ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1384 | goto error; |
| 1385 | } |
| 1386 | |
| 1387 | switch (domain->type) { |
| 1388 | case LTTNG_DOMAIN_KERNEL: |
| 1389 | { |
| 1390 | struct ltt_kernel_channel *kchan; |
| 1391 | |
| 1392 | kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session); |
| 1393 | if (kchan == nullptr) { |
| 1394 | /* |
| 1395 | * Don't try to create a channel if the session has been started at |
| 1396 | * some point in time before. The tracer does not allow it. |
| 1397 | */ |
| 1398 | if (session->has_been_started) { |
| 1399 | ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1400 | goto error; |
| 1401 | } |
| 1402 | |
| 1403 | if (session->snapshot.nb_output > 0 || session->snapshot_mode) { |
| 1404 | /* Enforce mmap output for snapshot sessions. */ |
| 1405 | attr->attr.output = LTTNG_EVENT_MMAP; |
| 1406 | } |
| 1407 | ret_code = channel_kernel_create(session->kernel_session, attr, wpipe); |
| 1408 | if (attr->name[0] != '\0') { |
| 1409 | session->kernel_session->has_non_default_channel = 1; |
| 1410 | } |
| 1411 | } else { |
| 1412 | ret_code = channel_kernel_enable(session->kernel_session, kchan); |
| 1413 | } |
| 1414 | |
| 1415 | if (ret_code != LTTNG_OK) { |
| 1416 | goto error; |
| 1417 | } |
| 1418 | |
| 1419 | kernel_wait_quiescent(); |
| 1420 | break; |
| 1421 | } |
| 1422 | case LTTNG_DOMAIN_UST: |
| 1423 | case LTTNG_DOMAIN_JUL: |
| 1424 | case LTTNG_DOMAIN_LOG4J: |
| 1425 | case LTTNG_DOMAIN_PYTHON: |
| 1426 | { |
| 1427 | struct ltt_ust_channel *uchan; |
| 1428 | |
| 1429 | /* |
| 1430 | * FIXME |
| 1431 | * |
| 1432 | * Current agent implementation limitations force us to allow |
| 1433 | * only one channel at once in "agent" subdomains. Each |
| 1434 | * subdomain has a default channel name which must be strictly |
| 1435 | * adhered to. |
| 1436 | */ |
| 1437 | if (domain->type == LTTNG_DOMAIN_JUL) { |
| 1438 | if (strncmp(attr->name, |
| 1439 | DEFAULT_JUL_CHANNEL_NAME, |
| 1440 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1441 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1442 | goto error; |
| 1443 | } |
| 1444 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { |
| 1445 | if (strncmp(attr->name, |
| 1446 | DEFAULT_LOG4J_CHANNEL_NAME, |
| 1447 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1448 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1449 | goto error; |
| 1450 | } |
| 1451 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { |
| 1452 | if (strncmp(attr->name, |
| 1453 | DEFAULT_PYTHON_CHANNEL_NAME, |
| 1454 | LTTNG_SYMBOL_NAME_LEN - 1) != 0) { |
| 1455 | ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME; |
| 1456 | goto error; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | chan_ht = usess->domain_global.channels; |
| 1461 | |
| 1462 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); |
| 1463 | if (uchan == nullptr) { |
| 1464 | /* |
| 1465 | * Don't try to create a channel if the session has been started at |
| 1466 | * some point in time before. The tracer does not allow it. |
| 1467 | */ |
| 1468 | if (session->has_been_started) { |
| 1469 | ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1470 | goto error; |
| 1471 | } |
| 1472 | |
| 1473 | ret_code = channel_ust_create(usess, attr, domain->buf_type); |
| 1474 | if (attr->name[0] != '\0') { |
| 1475 | usess->has_non_default_channel = 1; |
| 1476 | } |
| 1477 | } else { |
| 1478 | ret_code = channel_ust_enable(usess, uchan); |
| 1479 | } |
| 1480 | break; |
| 1481 | } |
| 1482 | default: |
| 1483 | ret_code = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 1484 | goto error; |
| 1485 | } |
| 1486 | |
| 1487 | if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { |
| 1488 | session->has_non_mmap_channel = true; |
| 1489 | } |
| 1490 | error: |
| 1491 | rcu_read_unlock(); |
| 1492 | end: |
| 1493 | lttng_channel_destroy(attr); |
| 1494 | return ret_code; |
| 1495 | } |
| 1496 | |
| 1497 | enum lttng_error_code |
| 1498 | cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session, |
| 1499 | enum lttng_domain_type domain, |
| 1500 | enum lttng_process_attr process_attr, |
| 1501 | enum lttng_tracking_policy *policy) |
| 1502 | { |
| 1503 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1504 | const struct process_attr_tracker *tracker; |
| 1505 | |
| 1506 | switch (domain) { |
| 1507 | case LTTNG_DOMAIN_KERNEL: |
| 1508 | if (!session->kernel_session) { |
| 1509 | ret_code = LTTNG_ERR_INVALID; |
| 1510 | goto end; |
| 1511 | } |
| 1512 | tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr); |
| 1513 | break; |
| 1514 | case LTTNG_DOMAIN_UST: |
| 1515 | if (!session->ust_session) { |
| 1516 | ret_code = LTTNG_ERR_INVALID; |
| 1517 | goto end; |
| 1518 | } |
| 1519 | tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr); |
| 1520 | break; |
| 1521 | default: |
| 1522 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1523 | goto end; |
| 1524 | } |
| 1525 | if (tracker) { |
| 1526 | *policy = process_attr_tracker_get_tracking_policy(tracker); |
| 1527 | } else { |
| 1528 | ret_code = LTTNG_ERR_INVALID; |
| 1529 | } |
| 1530 | end: |
| 1531 | return ret_code; |
| 1532 | } |
| 1533 | |
| 1534 | enum lttng_error_code |
| 1535 | cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session, |
| 1536 | enum lttng_domain_type domain, |
| 1537 | enum lttng_process_attr process_attr, |
| 1538 | enum lttng_tracking_policy policy) |
| 1539 | { |
| 1540 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1541 | |
| 1542 | switch (policy) { |
| 1543 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: |
| 1544 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: |
| 1545 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: |
| 1546 | break; |
| 1547 | default: |
| 1548 | ret_code = LTTNG_ERR_INVALID; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | |
| 1552 | switch (domain) { |
| 1553 | case LTTNG_DOMAIN_KERNEL: |
| 1554 | if (!session->kernel_session) { |
| 1555 | ret_code = LTTNG_ERR_INVALID; |
| 1556 | goto end; |
| 1557 | } |
| 1558 | ret_code = kernel_process_attr_tracker_set_tracking_policy( |
| 1559 | session->kernel_session, process_attr, policy); |
| 1560 | break; |
| 1561 | case LTTNG_DOMAIN_UST: |
| 1562 | if (!session->ust_session) { |
| 1563 | ret_code = LTTNG_ERR_INVALID; |
| 1564 | goto end; |
| 1565 | } |
| 1566 | ret_code = trace_ust_process_attr_tracker_set_tracking_policy( |
| 1567 | session->ust_session, process_attr, policy); |
| 1568 | break; |
| 1569 | default: |
| 1570 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1571 | break; |
| 1572 | } |
| 1573 | end: |
| 1574 | return ret_code; |
| 1575 | } |
| 1576 | |
| 1577 | enum lttng_error_code |
| 1578 | cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session, |
| 1579 | enum lttng_domain_type domain, |
| 1580 | enum lttng_process_attr process_attr, |
| 1581 | const struct process_attr_value *value) |
| 1582 | { |
| 1583 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1584 | |
| 1585 | switch (domain) { |
| 1586 | case LTTNG_DOMAIN_KERNEL: |
| 1587 | if (!session->kernel_session) { |
| 1588 | ret_code = LTTNG_ERR_INVALID; |
| 1589 | goto end; |
| 1590 | } |
| 1591 | ret_code = kernel_process_attr_tracker_inclusion_set_add_value( |
| 1592 | session->kernel_session, process_attr, value); |
| 1593 | break; |
| 1594 | case LTTNG_DOMAIN_UST: |
| 1595 | if (!session->ust_session) { |
| 1596 | ret_code = LTTNG_ERR_INVALID; |
| 1597 | goto end; |
| 1598 | } |
| 1599 | ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value( |
| 1600 | session->ust_session, process_attr, value); |
| 1601 | break; |
| 1602 | default: |
| 1603 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1604 | break; |
| 1605 | } |
| 1606 | end: |
| 1607 | return ret_code; |
| 1608 | } |
| 1609 | |
| 1610 | enum lttng_error_code |
| 1611 | cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session, |
| 1612 | enum lttng_domain_type domain, |
| 1613 | enum lttng_process_attr process_attr, |
| 1614 | const struct process_attr_value *value) |
| 1615 | { |
| 1616 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1617 | |
| 1618 | switch (domain) { |
| 1619 | case LTTNG_DOMAIN_KERNEL: |
| 1620 | if (!session->kernel_session) { |
| 1621 | ret_code = LTTNG_ERR_INVALID; |
| 1622 | goto end; |
| 1623 | } |
| 1624 | ret_code = kernel_process_attr_tracker_inclusion_set_remove_value( |
| 1625 | session->kernel_session, process_attr, value); |
| 1626 | break; |
| 1627 | case LTTNG_DOMAIN_UST: |
| 1628 | if (!session->ust_session) { |
| 1629 | ret_code = LTTNG_ERR_INVALID; |
| 1630 | goto end; |
| 1631 | } |
| 1632 | ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value( |
| 1633 | session->ust_session, process_attr, value); |
| 1634 | break; |
| 1635 | default: |
| 1636 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1637 | break; |
| 1638 | } |
| 1639 | end: |
| 1640 | return ret_code; |
| 1641 | } |
| 1642 | |
| 1643 | enum lttng_error_code |
| 1644 | cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session, |
| 1645 | enum lttng_domain_type domain, |
| 1646 | enum lttng_process_attr process_attr, |
| 1647 | struct lttng_process_attr_values **values) |
| 1648 | { |
| 1649 | enum lttng_error_code ret_code = LTTNG_OK; |
| 1650 | const struct process_attr_tracker *tracker; |
| 1651 | enum process_attr_tracker_status status; |
| 1652 | |
| 1653 | switch (domain) { |
| 1654 | case LTTNG_DOMAIN_KERNEL: |
| 1655 | if (!session->kernel_session) { |
| 1656 | ret_code = LTTNG_ERR_INVALID; |
| 1657 | goto end; |
| 1658 | } |
| 1659 | tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr); |
| 1660 | break; |
| 1661 | case LTTNG_DOMAIN_UST: |
| 1662 | if (!session->ust_session) { |
| 1663 | ret_code = LTTNG_ERR_INVALID; |
| 1664 | goto end; |
| 1665 | } |
| 1666 | tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr); |
| 1667 | break; |
| 1668 | default: |
| 1669 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; |
| 1670 | goto end; |
| 1671 | } |
| 1672 | |
| 1673 | if (!tracker) { |
| 1674 | ret_code = LTTNG_ERR_INVALID; |
| 1675 | goto end; |
| 1676 | } |
| 1677 | |
| 1678 | status = process_attr_tracker_get_inclusion_set(tracker, values); |
| 1679 | switch (status) { |
| 1680 | case PROCESS_ATTR_TRACKER_STATUS_OK: |
| 1681 | ret_code = LTTNG_OK; |
| 1682 | break; |
| 1683 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: |
| 1684 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; |
| 1685 | break; |
| 1686 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: |
| 1687 | ret_code = LTTNG_ERR_NOMEM; |
| 1688 | break; |
| 1689 | default: |
| 1690 | ret_code = LTTNG_ERR_UNK; |
| 1691 | break; |
| 1692 | } |
| 1693 | |
| 1694 | end: |
| 1695 | return ret_code; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Command LTTNG_DISABLE_EVENT processed by the client thread. |
| 1700 | */ |
| 1701 | int cmd_disable_event(struct command_ctx *cmd_ctx, |
| 1702 | struct lttng_event *event, |
| 1703 | char *filter_expression, |
| 1704 | struct lttng_bytecode *bytecode, |
| 1705 | struct lttng_event_exclusion *exclusion) |
| 1706 | { |
| 1707 | int ret; |
| 1708 | const char *event_name; |
| 1709 | const struct ltt_session *session = cmd_ctx->session; |
| 1710 | const char *channel_name = cmd_ctx->lsm.u.disable.channel_name; |
| 1711 | const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type; |
| 1712 | |
| 1713 | DBG("Disable event command for event \'%s\'", event->name); |
| 1714 | |
| 1715 | /* |
| 1716 | * Filter and exclusions are simply not handled by the |
| 1717 | * disable event command at this time. |
| 1718 | * |
| 1719 | * FIXME |
| 1720 | */ |
| 1721 | (void) filter_expression; |
| 1722 | (void) exclusion; |
| 1723 | |
| 1724 | /* Ignore the presence of filter or exclusion for the event */ |
| 1725 | event->filter = 0; |
| 1726 | event->exclusion = 0; |
| 1727 | |
| 1728 | event_name = event->name; |
| 1729 | |
| 1730 | /* Error out on unhandled search criteria */ |
| 1731 | if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid || |
| 1732 | event->filter || event->exclusion) { |
| 1733 | ret = LTTNG_ERR_UNK; |
| 1734 | goto error; |
| 1735 | } |
| 1736 | |
| 1737 | rcu_read_lock(); |
| 1738 | |
| 1739 | switch (domain) { |
| 1740 | case LTTNG_DOMAIN_KERNEL: |
| 1741 | { |
| 1742 | struct ltt_kernel_channel *kchan; |
| 1743 | struct ltt_kernel_session *ksess; |
| 1744 | |
| 1745 | ksess = session->kernel_session; |
| 1746 | |
| 1747 | /* |
| 1748 | * If a non-default channel has been created in the |
| 1749 | * session, explicitely require that -c chan_name needs |
| 1750 | * to be provided. |
| 1751 | */ |
| 1752 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { |
| 1753 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1754 | goto error_unlock; |
| 1755 | } |
| 1756 | |
| 1757 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
| 1758 | if (kchan == nullptr) { |
| 1759 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 1760 | goto error_unlock; |
| 1761 | } |
| 1762 | |
| 1763 | switch (event->type) { |
| 1764 | case LTTNG_EVENT_ALL: |
| 1765 | case LTTNG_EVENT_TRACEPOINT: |
| 1766 | case LTTNG_EVENT_SYSCALL: |
| 1767 | case LTTNG_EVENT_PROBE: |
| 1768 | case LTTNG_EVENT_FUNCTION: |
| 1769 | case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */ |
| 1770 | if (event_name[0] == '\0') { |
| 1771 | ret = event_kernel_disable_event(kchan, nullptr, event->type); |
| 1772 | } else { |
| 1773 | ret = event_kernel_disable_event(kchan, event_name, event->type); |
| 1774 | } |
| 1775 | if (ret != LTTNG_OK) { |
| 1776 | goto error_unlock; |
| 1777 | } |
| 1778 | break; |
| 1779 | default: |
| 1780 | ret = LTTNG_ERR_UNK; |
| 1781 | goto error_unlock; |
| 1782 | } |
| 1783 | |
| 1784 | kernel_wait_quiescent(); |
| 1785 | break; |
| 1786 | } |
| 1787 | case LTTNG_DOMAIN_UST: |
| 1788 | { |
| 1789 | struct ltt_ust_channel *uchan; |
| 1790 | struct ltt_ust_session *usess; |
| 1791 | |
| 1792 | usess = session->ust_session; |
| 1793 | |
| 1794 | if (validate_ust_event_name(event_name)) { |
| 1795 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
| 1796 | goto error_unlock; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * If a non-default channel has been created in the |
| 1801 | * session, explicitly require that -c chan_name needs |
| 1802 | * to be provided. |
| 1803 | */ |
| 1804 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 1805 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 1806 | goto error_unlock; |
| 1807 | } |
| 1808 | |
| 1809 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name); |
| 1810 | if (uchan == nullptr) { |
| 1811 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1812 | goto error_unlock; |
| 1813 | } |
| 1814 | |
| 1815 | switch (event->type) { |
| 1816 | case LTTNG_EVENT_ALL: |
| 1817 | /* |
| 1818 | * An empty event name means that everything |
| 1819 | * should be disabled. |
| 1820 | */ |
| 1821 | if (event->name[0] == '\0') { |
| 1822 | ret = event_ust_disable_all_tracepoints(usess, uchan); |
| 1823 | } else { |
| 1824 | ret = event_ust_disable_tracepoint(usess, uchan, event_name); |
| 1825 | } |
| 1826 | if (ret != LTTNG_OK) { |
| 1827 | goto error_unlock; |
| 1828 | } |
| 1829 | break; |
| 1830 | default: |
| 1831 | ret = LTTNG_ERR_UNK; |
| 1832 | goto error_unlock; |
| 1833 | } |
| 1834 | |
| 1835 | DBG3("Disable UST event %s in channel %s completed", event_name, channel_name); |
| 1836 | break; |
| 1837 | } |
| 1838 | case LTTNG_DOMAIN_LOG4J: |
| 1839 | case LTTNG_DOMAIN_JUL: |
| 1840 | case LTTNG_DOMAIN_PYTHON: |
| 1841 | { |
| 1842 | struct agent *agt; |
| 1843 | struct ltt_ust_session *usess = session->ust_session; |
| 1844 | |
| 1845 | LTTNG_ASSERT(usess); |
| 1846 | |
| 1847 | switch (event->type) { |
| 1848 | case LTTNG_EVENT_ALL: |
| 1849 | break; |
| 1850 | default: |
| 1851 | ret = LTTNG_ERR_UNK; |
| 1852 | goto error_unlock; |
| 1853 | } |
| 1854 | |
| 1855 | agt = trace_ust_find_agent(usess, domain); |
| 1856 | if (!agt) { |
| 1857 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; |
| 1858 | goto error_unlock; |
| 1859 | } |
| 1860 | /* |
| 1861 | * An empty event name means that everything |
| 1862 | * should be disabled. |
| 1863 | */ |
| 1864 | if (event->name[0] == '\0') { |
| 1865 | ret = event_agent_disable_all(usess, agt); |
| 1866 | } else { |
| 1867 | ret = event_agent_disable(usess, agt, event_name); |
| 1868 | } |
| 1869 | if (ret != LTTNG_OK) { |
| 1870 | goto error_unlock; |
| 1871 | } |
| 1872 | |
| 1873 | break; |
| 1874 | } |
| 1875 | default: |
| 1876 | ret = LTTNG_ERR_UND; |
| 1877 | goto error_unlock; |
| 1878 | } |
| 1879 | |
| 1880 | ret = LTTNG_OK; |
| 1881 | |
| 1882 | error_unlock: |
| 1883 | rcu_read_unlock(); |
| 1884 | error: |
| 1885 | free(exclusion); |
| 1886 | free(bytecode); |
| 1887 | free(filter_expression); |
| 1888 | return ret; |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | * Command LTTNG_ADD_CONTEXT processed by the client thread. |
| 1893 | */ |
| 1894 | int cmd_add_context(struct command_ctx *cmd_ctx, |
| 1895 | const struct lttng_event_context *event_context, |
| 1896 | int kwpipe) |
| 1897 | { |
| 1898 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
| 1899 | const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type; |
| 1900 | const struct ltt_session *session = cmd_ctx->session; |
| 1901 | const char *channel_name = cmd_ctx->lsm.u.context.channel_name; |
| 1902 | |
| 1903 | /* |
| 1904 | * Don't try to add a context if the session has been started at |
| 1905 | * some point in time before. The tracer does not allow it and would |
| 1906 | * result in a corrupted trace. |
| 1907 | */ |
| 1908 | if (cmd_ctx->session->has_been_started) { |
| 1909 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 1910 | goto end; |
| 1911 | } |
| 1912 | |
| 1913 | switch (domain) { |
| 1914 | case LTTNG_DOMAIN_KERNEL: |
| 1915 | LTTNG_ASSERT(session->kernel_session); |
| 1916 | |
| 1917 | if (session->kernel_session->channel_count == 0) { |
| 1918 | /* Create default channel */ |
| 1919 | ret = channel_kernel_create(session->kernel_session, nullptr, kwpipe); |
| 1920 | if (ret != LTTNG_OK) { |
| 1921 | goto error; |
| 1922 | } |
| 1923 | chan_kern_created = 1; |
| 1924 | } |
| 1925 | /* Add kernel context to kernel tracer */ |
| 1926 | ret = context_kernel_add(session->kernel_session, event_context, channel_name); |
| 1927 | if (ret != LTTNG_OK) { |
| 1928 | goto error; |
| 1929 | } |
| 1930 | break; |
| 1931 | case LTTNG_DOMAIN_JUL: |
| 1932 | case LTTNG_DOMAIN_LOG4J: |
| 1933 | { |
| 1934 | /* |
| 1935 | * Validate channel name. |
| 1936 | * If no channel name is given and the domain is JUL or LOG4J, |
| 1937 | * set it to the appropriate domain-specific channel name. If |
| 1938 | * a name is provided but does not match the expexted channel |
| 1939 | * name, return an error. |
| 1940 | */ |
| 1941 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && |
| 1942 | strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME) != 0) { |
| 1943 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1944 | goto error; |
| 1945 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && |
| 1946 | strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME) != 0) { |
| 1947 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 1948 | goto error; |
| 1949 | } |
| 1950 | } |
| 1951 | /* fall through */ |
| 1952 | case LTTNG_DOMAIN_UST: |
| 1953 | { |
| 1954 | struct ltt_ust_session *usess = session->ust_session; |
| 1955 | unsigned int chan_count; |
| 1956 | |
| 1957 | LTTNG_ASSERT(usess); |
| 1958 | |
| 1959 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
| 1960 | if (chan_count == 0) { |
| 1961 | struct lttng_channel *attr; |
| 1962 | /* Create default channel */ |
| 1963 | attr = channel_new_default_attr(domain, usess->buffer_type); |
| 1964 | if (attr == nullptr) { |
| 1965 | ret = LTTNG_ERR_FATAL; |
| 1966 | goto error; |
| 1967 | } |
| 1968 | |
| 1969 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
| 1970 | if (ret != LTTNG_OK) { |
| 1971 | free(attr); |
| 1972 | goto error; |
| 1973 | } |
| 1974 | channel_attr_destroy(attr); |
| 1975 | chan_ust_created = 1; |
| 1976 | } |
| 1977 | |
| 1978 | ret = context_ust_add(usess, domain, event_context, channel_name); |
| 1979 | if (ret != LTTNG_OK) { |
| 1980 | goto error; |
| 1981 | } |
| 1982 | break; |
| 1983 | } |
| 1984 | default: |
| 1985 | ret = LTTNG_ERR_UND; |
| 1986 | goto error; |
| 1987 | } |
| 1988 | |
| 1989 | ret = LTTNG_OK; |
| 1990 | goto end; |
| 1991 | |
| 1992 | error: |
| 1993 | if (chan_kern_created) { |
| 1994 | struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name( |
| 1995 | DEFAULT_CHANNEL_NAME, session->kernel_session); |
| 1996 | /* Created previously, this should NOT fail. */ |
| 1997 | LTTNG_ASSERT(kchan); |
| 1998 | kernel_destroy_channel(kchan); |
| 1999 | } |
| 2000 | |
| 2001 | if (chan_ust_created) { |
| 2002 | struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name( |
| 2003 | session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME); |
| 2004 | /* Created previously, this should NOT fail. */ |
| 2005 | LTTNG_ASSERT(uchan); |
| 2006 | /* Remove from the channel list of the session. */ |
| 2007 | trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan); |
| 2008 | trace_ust_destroy_channel(uchan); |
| 2009 | } |
| 2010 | end: |
| 2011 | return ret; |
| 2012 | } |
| 2013 | |
| 2014 | static inline bool name_starts_with(const char *name, const char *prefix) |
| 2015 | { |
| 2016 | const size_t max_cmp_len = std::min(strlen(prefix), (size_t) LTTNG_SYMBOL_NAME_LEN); |
| 2017 | |
| 2018 | return !strncmp(name, prefix, max_cmp_len); |
| 2019 | } |
| 2020 | |
| 2021 | /* Perform userspace-specific event name validation */ |
| 2022 | static int validate_ust_event_name(const char *name) |
| 2023 | { |
| 2024 | int ret = 0; |
| 2025 | |
| 2026 | if (!name) { |
| 2027 | ret = -1; |
| 2028 | goto end; |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * Check name against all internal UST event component namespaces used |
| 2033 | * by the agents. |
| 2034 | */ |
| 2035 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || |
| 2036 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || |
| 2037 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { |
| 2038 | ret = -1; |
| 2039 | } |
| 2040 | |
| 2041 | end: |
| 2042 | return ret; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Internal version of cmd_enable_event() with a supplemental |
| 2047 | * "internal_event" flag which is used to enable internal events which should |
| 2048 | * be hidden from clients. Such events are used in the agent implementation to |
| 2049 | * enable the events through which all "agent" events are funeled. |
| 2050 | */ |
| 2051 | static int _cmd_enable_event(struct ltt_session *session, |
| 2052 | const struct lttng_domain *domain, |
| 2053 | char *channel_name, |
| 2054 | struct lttng_event *event, |
| 2055 | char *filter_expression, |
| 2056 | struct lttng_bytecode *filter, |
| 2057 | struct lttng_event_exclusion *exclusion, |
| 2058 | int wpipe, |
| 2059 | bool internal_event) |
| 2060 | { |
| 2061 | int ret = 0, channel_created = 0; |
| 2062 | struct lttng_channel *attr = nullptr; |
| 2063 | |
| 2064 | LTTNG_ASSERT(session); |
| 2065 | LTTNG_ASSERT(event); |
| 2066 | LTTNG_ASSERT(channel_name); |
| 2067 | |
| 2068 | /* If we have a filter, we must have its filter expression */ |
| 2069 | LTTNG_ASSERT(!(!!filter_expression ^ !!filter)); |
| 2070 | |
| 2071 | /* Normalize event name as a globbing pattern */ |
| 2072 | strutils_normalize_star_glob_pattern(event->name); |
| 2073 | |
| 2074 | /* Normalize exclusion names as globbing patterns */ |
| 2075 | if (exclusion) { |
| 2076 | size_t i; |
| 2077 | |
| 2078 | for (i = 0; i < exclusion->count; i++) { |
| 2079 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); |
| 2080 | |
| 2081 | strutils_normalize_star_glob_pattern(name); |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | DBG("Enable event command for event \'%s\'", event->name); |
| 2086 | |
| 2087 | rcu_read_lock(); |
| 2088 | |
| 2089 | switch (domain->type) { |
| 2090 | case LTTNG_DOMAIN_KERNEL: |
| 2091 | { |
| 2092 | struct ltt_kernel_channel *kchan; |
| 2093 | |
| 2094 | /* |
| 2095 | * If a non-default channel has been created in the |
| 2096 | * session, explicitely require that -c chan_name needs |
| 2097 | * to be provided. |
| 2098 | */ |
| 2099 | if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') { |
| 2100 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 2101 | goto error; |
| 2102 | } |
| 2103 | |
| 2104 | kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session); |
| 2105 | if (kchan == nullptr) { |
| 2106 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL); |
| 2107 | if (attr == nullptr) { |
| 2108 | ret = LTTNG_ERR_FATAL; |
| 2109 | goto error; |
| 2110 | } |
| 2111 | if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) { |
| 2112 | ret = LTTNG_ERR_INVALID; |
| 2113 | goto error; |
| 2114 | } |
| 2115 | |
| 2116 | ret = cmd_enable_channel_internal(session, domain, attr, wpipe); |
| 2117 | if (ret != LTTNG_OK) { |
| 2118 | goto error; |
| 2119 | } |
| 2120 | channel_created = 1; |
| 2121 | } |
| 2122 | |
| 2123 | /* Get the newly created kernel channel pointer */ |
| 2124 | kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session); |
| 2125 | if (kchan == nullptr) { |
| 2126 | /* This sould not happen... */ |
| 2127 | ret = LTTNG_ERR_FATAL; |
| 2128 | goto error; |
| 2129 | } |
| 2130 | |
| 2131 | switch (event->type) { |
| 2132 | case LTTNG_EVENT_ALL: |
| 2133 | { |
| 2134 | char *filter_expression_a = nullptr; |
| 2135 | struct lttng_bytecode *filter_a = nullptr; |
| 2136 | |
| 2137 | /* |
| 2138 | * We need to duplicate filter_expression and filter, |
| 2139 | * because ownership is passed to first enable |
| 2140 | * event. |
| 2141 | */ |
| 2142 | if (filter_expression) { |
| 2143 | filter_expression_a = strdup(filter_expression); |
| 2144 | if (!filter_expression_a) { |
| 2145 | ret = LTTNG_ERR_FATAL; |
| 2146 | goto error; |
| 2147 | } |
| 2148 | } |
| 2149 | if (filter) { |
| 2150 | filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len); |
| 2151 | if (!filter_a) { |
| 2152 | free(filter_expression_a); |
| 2153 | ret = LTTNG_ERR_FATAL; |
| 2154 | goto error; |
| 2155 | } |
| 2156 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); |
| 2157 | } |
| 2158 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
| 2159 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2160 | /* We have passed ownership */ |
| 2161 | filter_expression = nullptr; |
| 2162 | filter = nullptr; |
| 2163 | if (ret != LTTNG_OK) { |
| 2164 | if (channel_created) { |
| 2165 | /* Let's not leak a useless channel. */ |
| 2166 | kernel_destroy_channel(kchan); |
| 2167 | } |
| 2168 | free(filter_expression_a); |
| 2169 | free(filter_a); |
| 2170 | goto error; |
| 2171 | } |
| 2172 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ |
| 2173 | ret = event_kernel_enable_event( |
| 2174 | kchan, event, filter_expression_a, filter_a); |
| 2175 | /* We have passed ownership */ |
| 2176 | filter_expression_a = nullptr; |
| 2177 | filter_a = nullptr; |
| 2178 | if (ret != LTTNG_OK) { |
| 2179 | goto error; |
| 2180 | } |
| 2181 | break; |
| 2182 | } |
| 2183 | case LTTNG_EVENT_PROBE: |
| 2184 | case LTTNG_EVENT_USERSPACE_PROBE: |
| 2185 | case LTTNG_EVENT_FUNCTION: |
| 2186 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 2187 | case LTTNG_EVENT_TRACEPOINT: |
| 2188 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2189 | /* We have passed ownership */ |
| 2190 | filter_expression = nullptr; |
| 2191 | filter = nullptr; |
| 2192 | if (ret != LTTNG_OK) { |
| 2193 | if (channel_created) { |
| 2194 | /* Let's not leak a useless channel. */ |
| 2195 | kernel_destroy_channel(kchan); |
| 2196 | } |
| 2197 | goto error; |
| 2198 | } |
| 2199 | break; |
| 2200 | case LTTNG_EVENT_SYSCALL: |
| 2201 | ret = event_kernel_enable_event(kchan, event, filter_expression, filter); |
| 2202 | /* We have passed ownership */ |
| 2203 | filter_expression = nullptr; |
| 2204 | filter = nullptr; |
| 2205 | if (ret != LTTNG_OK) { |
| 2206 | goto error; |
| 2207 | } |
| 2208 | break; |
| 2209 | default: |
| 2210 | ret = LTTNG_ERR_UNK; |
| 2211 | goto error; |
| 2212 | } |
| 2213 | |
| 2214 | kernel_wait_quiescent(); |
| 2215 | break; |
| 2216 | } |
| 2217 | case LTTNG_DOMAIN_UST: |
| 2218 | { |
| 2219 | struct ltt_ust_channel *uchan; |
| 2220 | struct ltt_ust_session *usess = session->ust_session; |
| 2221 | |
| 2222 | LTTNG_ASSERT(usess); |
| 2223 | |
| 2224 | /* |
| 2225 | * If a non-default channel has been created in the |
| 2226 | * session, explicitely require that -c chan_name needs |
| 2227 | * to be provided. |
| 2228 | */ |
| 2229 | if (usess->has_non_default_channel && channel_name[0] == '\0') { |
| 2230 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; |
| 2231 | goto error; |
| 2232 | } |
| 2233 | |
| 2234 | /* Get channel from global UST domain */ |
| 2235 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name); |
| 2236 | if (uchan == nullptr) { |
| 2237 | /* Create default channel */ |
| 2238 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type); |
| 2239 | if (attr == nullptr) { |
| 2240 | ret = LTTNG_ERR_FATAL; |
| 2241 | goto error; |
| 2242 | } |
| 2243 | if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) { |
| 2244 | ret = LTTNG_ERR_INVALID; |
| 2245 | goto error; |
| 2246 | } |
| 2247 | |
| 2248 | ret = cmd_enable_channel_internal(session, domain, attr, wpipe); |
| 2249 | if (ret != LTTNG_OK) { |
| 2250 | goto error; |
| 2251 | } |
| 2252 | |
| 2253 | /* Get the newly created channel reference back */ |
| 2254 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
| 2255 | channel_name); |
| 2256 | LTTNG_ASSERT(uchan); |
| 2257 | } |
| 2258 | |
| 2259 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
| 2260 | /* |
| 2261 | * Don't allow users to add UST events to channels which |
| 2262 | * are assigned to a userspace subdomain (JUL, Log4J, |
| 2263 | * Python, etc.). |
| 2264 | */ |
| 2265 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; |
| 2266 | goto error; |
| 2267 | } |
| 2268 | |
| 2269 | if (!internal_event) { |
| 2270 | /* |
| 2271 | * Ensure the event name is not reserved for internal |
| 2272 | * use. |
| 2273 | */ |
| 2274 | ret = validate_ust_event_name(event->name); |
| 2275 | if (ret) { |
| 2276 | WARN("Userspace event name %s failed validation.", event->name); |
| 2277 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
| 2278 | goto error; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | /* At this point, the session and channel exist on the tracer */ |
| 2283 | ret = event_ust_enable_tracepoint( |
| 2284 | usess, uchan, event, filter_expression, filter, exclusion, internal_event); |
| 2285 | /* We have passed ownership */ |
| 2286 | filter_expression = nullptr; |
| 2287 | filter = nullptr; |
| 2288 | exclusion = nullptr; |
| 2289 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
| 2290 | goto already_enabled; |
| 2291 | } else if (ret != LTTNG_OK) { |
| 2292 | goto error; |
| 2293 | } |
| 2294 | break; |
| 2295 | } |
| 2296 | case LTTNG_DOMAIN_LOG4J: |
| 2297 | case LTTNG_DOMAIN_JUL: |
| 2298 | case LTTNG_DOMAIN_PYTHON: |
| 2299 | { |
| 2300 | const char *default_event_name, *default_chan_name; |
| 2301 | struct agent *agt; |
| 2302 | struct lttng_event uevent; |
| 2303 | struct lttng_domain tmp_dom; |
| 2304 | struct ltt_ust_session *usess = session->ust_session; |
| 2305 | |
| 2306 | LTTNG_ASSERT(usess); |
| 2307 | |
| 2308 | if (!agent_tracing_is_enabled()) { |
| 2309 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); |
| 2310 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; |
| 2311 | goto error; |
| 2312 | } |
| 2313 | |
| 2314 | agt = trace_ust_find_agent(usess, domain->type); |
| 2315 | if (!agt) { |
| 2316 | agt = agent_create(domain->type); |
| 2317 | if (!agt) { |
| 2318 | ret = LTTNG_ERR_NOMEM; |
| 2319 | goto error; |
| 2320 | } |
| 2321 | agent_add(agt, usess->agents); |
| 2322 | } |
| 2323 | |
| 2324 | /* Create the default tracepoint. */ |
| 2325 | memset(&uevent, 0, sizeof(uevent)); |
| 2326 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
| 2327 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 2328 | default_event_name = event_get_default_agent_ust_name(domain->type); |
| 2329 | if (!default_event_name) { |
| 2330 | ret = LTTNG_ERR_FATAL; |
| 2331 | goto error; |
| 2332 | } |
| 2333 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
| 2334 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
| 2335 | |
| 2336 | /* |
| 2337 | * The domain type is changed because we are about to enable the |
| 2338 | * default channel and event for the JUL domain that are hardcoded. |
| 2339 | * This happens in the UST domain. |
| 2340 | */ |
| 2341 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); |
| 2342 | tmp_dom.type = LTTNG_DOMAIN_UST; |
| 2343 | |
| 2344 | switch (domain->type) { |
| 2345 | case LTTNG_DOMAIN_LOG4J: |
| 2346 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
| 2347 | break; |
| 2348 | case LTTNG_DOMAIN_JUL: |
| 2349 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
| 2350 | break; |
| 2351 | case LTTNG_DOMAIN_PYTHON: |
| 2352 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; |
| 2353 | break; |
| 2354 | default: |
| 2355 | /* The switch/case we are in makes this impossible */ |
| 2356 | abort(); |
| 2357 | } |
| 2358 | |
| 2359 | { |
| 2360 | char *filter_expression_copy = nullptr; |
| 2361 | struct lttng_bytecode *filter_copy = nullptr; |
| 2362 | |
| 2363 | if (filter) { |
| 2364 | const size_t filter_size = |
| 2365 | sizeof(struct lttng_bytecode) + filter->len; |
| 2366 | |
| 2367 | filter_copy = zmalloc<lttng_bytecode>(filter_size); |
| 2368 | if (!filter_copy) { |
| 2369 | ret = LTTNG_ERR_NOMEM; |
| 2370 | goto error; |
| 2371 | } |
| 2372 | memcpy(filter_copy, filter, filter_size); |
| 2373 | |
| 2374 | filter_expression_copy = strdup(filter_expression); |
| 2375 | if (!filter_expression) { |
| 2376 | ret = LTTNG_ERR_NOMEM; |
| 2377 | } |
| 2378 | |
| 2379 | if (!filter_expression_copy || !filter_copy) { |
| 2380 | free(filter_expression_copy); |
| 2381 | free(filter_copy); |
| 2382 | goto error; |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | ret = cmd_enable_event_internal(session, |
| 2387 | &tmp_dom, |
| 2388 | (char *) default_chan_name, |
| 2389 | &uevent, |
| 2390 | filter_expression_copy, |
| 2391 | filter_copy, |
| 2392 | nullptr, |
| 2393 | wpipe); |
| 2394 | } |
| 2395 | |
| 2396 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
| 2397 | goto already_enabled; |
| 2398 | } else if (ret != LTTNG_OK) { |
| 2399 | goto error; |
| 2400 | } |
| 2401 | |
| 2402 | /* The wild card * means that everything should be enabled. */ |
| 2403 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { |
| 2404 | ret = event_agent_enable_all(usess, agt, event, filter, filter_expression); |
| 2405 | } else { |
| 2406 | ret = event_agent_enable(usess, agt, event, filter, filter_expression); |
| 2407 | } |
| 2408 | filter = nullptr; |
| 2409 | filter_expression = nullptr; |
| 2410 | if (ret != LTTNG_OK) { |
| 2411 | goto error; |
| 2412 | } |
| 2413 | |
| 2414 | break; |
| 2415 | } |
| 2416 | default: |
| 2417 | ret = LTTNG_ERR_UND; |
| 2418 | goto error; |
| 2419 | } |
| 2420 | |
| 2421 | ret = LTTNG_OK; |
| 2422 | |
| 2423 | already_enabled: |
| 2424 | error: |
| 2425 | free(filter_expression); |
| 2426 | free(filter); |
| 2427 | free(exclusion); |
| 2428 | channel_attr_destroy(attr); |
| 2429 | rcu_read_unlock(); |
| 2430 | return ret; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * Command LTTNG_ENABLE_EVENT processed by the client thread. |
| 2435 | * We own filter, exclusion, and filter_expression. |
| 2436 | */ |
| 2437 | int cmd_enable_event(struct command_ctx *cmd_ctx, |
| 2438 | struct lttng_event *event, |
| 2439 | char *filter_expression, |
| 2440 | struct lttng_event_exclusion *exclusion, |
| 2441 | struct lttng_bytecode *bytecode, |
| 2442 | int wpipe) |
| 2443 | { |
| 2444 | int ret; |
| 2445 | /* |
| 2446 | * Copied to ensure proper alignment since 'lsm' is a packed structure. |
| 2447 | */ |
| 2448 | const lttng_domain command_domain = cmd_ctx->lsm.domain; |
| 2449 | |
| 2450 | /* |
| 2451 | * The ownership of the following parameters is transferred to |
| 2452 | * _cmd_enable_event: |
| 2453 | * |
| 2454 | * - filter_expression, |
| 2455 | * - bytecode, |
| 2456 | * - exclusion |
| 2457 | */ |
| 2458 | ret = _cmd_enable_event(cmd_ctx->session, |
| 2459 | &command_domain, |
| 2460 | cmd_ctx->lsm.u.enable.channel_name, |
| 2461 | event, |
| 2462 | filter_expression, |
| 2463 | bytecode, |
| 2464 | exclusion, |
| 2465 | wpipe, |
| 2466 | false); |
| 2467 | filter_expression = nullptr; |
| 2468 | bytecode = nullptr; |
| 2469 | exclusion = nullptr; |
| 2470 | return ret; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * Enable an event which is internal to LTTng. An internal should |
| 2475 | * never be made visible to clients and are immune to checks such as |
| 2476 | * reserved names. |
| 2477 | */ |
| 2478 | static int cmd_enable_event_internal(struct ltt_session *session, |
| 2479 | const struct lttng_domain *domain, |
| 2480 | char *channel_name, |
| 2481 | struct lttng_event *event, |
| 2482 | char *filter_expression, |
| 2483 | struct lttng_bytecode *filter, |
| 2484 | struct lttng_event_exclusion *exclusion, |
| 2485 | int wpipe) |
| 2486 | { |
| 2487 | return _cmd_enable_event(session, |
| 2488 | domain, |
| 2489 | channel_name, |
| 2490 | event, |
| 2491 | filter_expression, |
| 2492 | filter, |
| 2493 | exclusion, |
| 2494 | wpipe, |
| 2495 | true); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. |
| 2500 | */ |
| 2501 | enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain, |
| 2502 | struct lttng_payload *reply_payload) |
| 2503 | { |
| 2504 | enum lttng_error_code ret_code; |
| 2505 | int ret; |
| 2506 | ssize_t i, nb_events = 0; |
| 2507 | struct lttng_event *events = nullptr; |
| 2508 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2509 | size_t reply_command_header_offset; |
| 2510 | |
| 2511 | assert(reply_payload); |
| 2512 | |
| 2513 | /* Reserve space for command reply header. */ |
| 2514 | reply_command_header_offset = reply_payload->buffer.size; |
| 2515 | ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer, |
| 2516 | reply_command_header_offset + |
| 2517 | sizeof(struct lttcomm_list_command_header)); |
| 2518 | if (ret) { |
| 2519 | ret_code = LTTNG_ERR_NOMEM; |
| 2520 | goto error; |
| 2521 | } |
| 2522 | |
| 2523 | switch (domain) { |
| 2524 | case LTTNG_DOMAIN_KERNEL: |
| 2525 | nb_events = kernel_list_events(&events); |
| 2526 | if (nb_events < 0) { |
| 2527 | ret_code = LTTNG_ERR_KERN_LIST_FAIL; |
| 2528 | goto error; |
| 2529 | } |
| 2530 | break; |
| 2531 | case LTTNG_DOMAIN_UST: |
| 2532 | nb_events = ust_app_list_events(&events); |
| 2533 | if (nb_events < 0) { |
| 2534 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2535 | goto error; |
| 2536 | } |
| 2537 | break; |
| 2538 | case LTTNG_DOMAIN_LOG4J: |
| 2539 | case LTTNG_DOMAIN_JUL: |
| 2540 | case LTTNG_DOMAIN_PYTHON: |
| 2541 | nb_events = agent_list_events(&events, domain); |
| 2542 | if (nb_events < 0) { |
| 2543 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2544 | goto error; |
| 2545 | } |
| 2546 | break; |
| 2547 | default: |
| 2548 | ret_code = LTTNG_ERR_UND; |
| 2549 | goto error; |
| 2550 | } |
| 2551 | |
| 2552 | for (i = 0; i < nb_events; i++) { |
| 2553 | ret = lttng_event_serialize( |
| 2554 | &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload); |
| 2555 | if (ret) { |
| 2556 | ret_code = LTTNG_ERR_NOMEM; |
| 2557 | goto error; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | if (nb_events > UINT32_MAX) { |
| 2562 | ERR("Tracepoint count would overflow the tracepoint listing command's reply"); |
| 2563 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2564 | goto error; |
| 2565 | } |
| 2566 | |
| 2567 | /* Update command reply header. */ |
| 2568 | reply_command_header.count = (uint32_t) nb_events; |
| 2569 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 2570 | &reply_command_header, |
| 2571 | sizeof(reply_command_header)); |
| 2572 | |
| 2573 | ret_code = LTTNG_OK; |
| 2574 | error: |
| 2575 | free(events); |
| 2576 | return ret_code; |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. |
| 2581 | */ |
| 2582 | enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
| 2583 | struct lttng_payload *reply) |
| 2584 | { |
| 2585 | enum lttng_error_code ret_code; |
| 2586 | int ret; |
| 2587 | unsigned int i, nb_fields; |
| 2588 | struct lttng_event_field *fields = nullptr; |
| 2589 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2590 | size_t reply_command_header_offset; |
| 2591 | |
| 2592 | assert(reply); |
| 2593 | |
| 2594 | /* Reserve space for command reply header. */ |
| 2595 | reply_command_header_offset = reply->buffer.size; |
| 2596 | ret = lttng_dynamic_buffer_set_size(&reply->buffer, |
| 2597 | reply_command_header_offset + |
| 2598 | sizeof(struct lttcomm_list_command_header)); |
| 2599 | if (ret) { |
| 2600 | ret_code = LTTNG_ERR_NOMEM; |
| 2601 | goto error; |
| 2602 | } |
| 2603 | |
| 2604 | switch (domain) { |
| 2605 | case LTTNG_DOMAIN_UST: |
| 2606 | ret = ust_app_list_event_fields(&fields); |
| 2607 | if (ret < 0) { |
| 2608 | ret_code = LTTNG_ERR_UST_LIST_FAIL; |
| 2609 | goto error; |
| 2610 | } |
| 2611 | |
| 2612 | break; |
| 2613 | case LTTNG_DOMAIN_KERNEL: |
| 2614 | default: /* fall-through */ |
| 2615 | ret_code = LTTNG_ERR_UND; |
| 2616 | goto error; |
| 2617 | } |
| 2618 | |
| 2619 | nb_fields = ret; |
| 2620 | |
| 2621 | for (i = 0; i < nb_fields; i++) { |
| 2622 | ret = lttng_event_field_serialize(&fields[i], reply); |
| 2623 | if (ret) { |
| 2624 | ret_code = LTTNG_ERR_NOMEM; |
| 2625 | goto error; |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | if (nb_fields > UINT32_MAX) { |
| 2630 | ERR("Tracepoint field count would overflow the tracepoint field listing command's reply"); |
| 2631 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2632 | goto error; |
| 2633 | } |
| 2634 | |
| 2635 | /* Update command reply header. */ |
| 2636 | reply_command_header.count = (uint32_t) nb_fields; |
| 2637 | |
| 2638 | memcpy(reply->buffer.data + reply_command_header_offset, |
| 2639 | &reply_command_header, |
| 2640 | sizeof(reply_command_header)); |
| 2641 | |
| 2642 | ret_code = LTTNG_OK; |
| 2643 | |
| 2644 | error: |
| 2645 | free(fields); |
| 2646 | return ret_code; |
| 2647 | } |
| 2648 | |
| 2649 | enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload) |
| 2650 | { |
| 2651 | enum lttng_error_code ret_code; |
| 2652 | ssize_t nb_events, i; |
| 2653 | int ret; |
| 2654 | struct lttng_event *events = nullptr; |
| 2655 | struct lttcomm_list_command_header reply_command_header = {}; |
| 2656 | size_t reply_command_header_offset; |
| 2657 | |
| 2658 | assert(reply_payload); |
| 2659 | |
| 2660 | /* Reserve space for command reply header. */ |
| 2661 | reply_command_header_offset = reply_payload->buffer.size; |
| 2662 | ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer, |
| 2663 | reply_command_header_offset + |
| 2664 | sizeof(struct lttcomm_list_command_header)); |
| 2665 | if (ret) { |
| 2666 | ret_code = LTTNG_ERR_NOMEM; |
| 2667 | goto end; |
| 2668 | } |
| 2669 | |
| 2670 | nb_events = syscall_table_list(&events); |
| 2671 | if (nb_events < 0) { |
| 2672 | ret_code = (enum lttng_error_code) - nb_events; |
| 2673 | goto end; |
| 2674 | } |
| 2675 | |
| 2676 | for (i = 0; i < nb_events; i++) { |
| 2677 | ret = lttng_event_serialize( |
| 2678 | &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload); |
| 2679 | if (ret) { |
| 2680 | ret_code = LTTNG_ERR_NOMEM; |
| 2681 | goto end; |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | if (nb_events > UINT32_MAX) { |
| 2686 | ERR("Syscall count would overflow the syscall listing command's reply"); |
| 2687 | ret_code = LTTNG_ERR_OVERFLOW; |
| 2688 | goto end; |
| 2689 | } |
| 2690 | |
| 2691 | /* Update command reply header. */ |
| 2692 | reply_command_header.count = (uint32_t) nb_events; |
| 2693 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 2694 | &reply_command_header, |
| 2695 | sizeof(reply_command_header)); |
| 2696 | |
| 2697 | ret_code = LTTNG_OK; |
| 2698 | end: |
| 2699 | free(events); |
| 2700 | return ret_code; |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * Command LTTNG_START_TRACE processed by the client thread. |
| 2705 | * |
| 2706 | * Called with session mutex held. |
| 2707 | */ |
| 2708 | int cmd_start_trace(struct ltt_session *session) |
| 2709 | { |
| 2710 | enum lttng_error_code ret; |
| 2711 | unsigned long nb_chan = 0; |
| 2712 | struct ltt_kernel_session *ksession; |
| 2713 | struct ltt_ust_session *usess; |
| 2714 | const bool session_rotated_after_last_stop = session->rotated_after_last_stop; |
| 2715 | const bool session_cleared_after_last_stop = session->cleared_after_last_stop; |
| 2716 | |
| 2717 | LTTNG_ASSERT(session); |
| 2718 | |
| 2719 | /* Ease our life a bit ;) */ |
| 2720 | ksession = session->kernel_session; |
| 2721 | usess = session->ust_session; |
| 2722 | |
| 2723 | /* Is the session already started? */ |
| 2724 | if (session->active) { |
| 2725 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 2726 | /* Perform nothing */ |
| 2727 | goto end; |
| 2728 | } |
| 2729 | |
| 2730 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING && |
| 2731 | !session->current_trace_chunk) { |
| 2732 | /* |
| 2733 | * A rotation was launched while the session was stopped and |
| 2734 | * it has not been completed yet. It is not possible to start |
| 2735 | * the session since starting the session here would require a |
| 2736 | * rotation from "NULL" to a new trace chunk. That rotation |
| 2737 | * would overlap with the ongoing rotation, which is not |
| 2738 | * supported. |
| 2739 | */ |
| 2740 | WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing", |
| 2741 | session->name); |
| 2742 | ret = LTTNG_ERR_ROTATION_PENDING; |
| 2743 | goto error; |
| 2744 | } |
| 2745 | |
| 2746 | /* |
| 2747 | * Starting a session without channel is useless since after that it's not |
| 2748 | * possible to enable channel thus inform the client. |
| 2749 | */ |
| 2750 | if (usess && usess->domain_global.channels) { |
| 2751 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); |
| 2752 | } |
| 2753 | if (ksession) { |
| 2754 | nb_chan += ksession->channel_count; |
| 2755 | } |
| 2756 | if (!nb_chan) { |
| 2757 | ret = LTTNG_ERR_NO_CHANNEL; |
| 2758 | goto error; |
| 2759 | } |
| 2760 | |
| 2761 | session->active = 1; |
| 2762 | session->rotated_after_last_stop = false; |
| 2763 | session->cleared_after_last_stop = false; |
| 2764 | if (session->output_traces && !session->current_trace_chunk) { |
| 2765 | if (!session->has_been_started) { |
| 2766 | struct lttng_trace_chunk *trace_chunk; |
| 2767 | |
| 2768 | DBG("Creating initial trace chunk of session \"%s\"", session->name); |
| 2769 | trace_chunk = |
| 2770 | session_create_new_trace_chunk(session, nullptr, nullptr, nullptr); |
| 2771 | if (!trace_chunk) { |
| 2772 | ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 2773 | goto error; |
| 2774 | } |
| 2775 | LTTNG_ASSERT(!session->current_trace_chunk); |
| 2776 | ret = (lttng_error_code) session_set_trace_chunk( |
| 2777 | session, trace_chunk, nullptr); |
| 2778 | lttng_trace_chunk_put(trace_chunk); |
| 2779 | if (ret) { |
| 2780 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 2781 | goto error; |
| 2782 | } |
| 2783 | } else { |
| 2784 | DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk", |
| 2785 | session->name); |
| 2786 | /* |
| 2787 | * Rotate existing streams into the new chunk. |
| 2788 | * This is a "quiet" rotation has no client has |
| 2789 | * explicitly requested this operation. |
| 2790 | * |
| 2791 | * There is also no need to wait for the rotation |
| 2792 | * to complete as it will happen immediately. No data |
| 2793 | * was produced as the session was stopped, so the |
| 2794 | * rotation should happen on reception of the command. |
| 2795 | */ |
| 2796 | ret = (lttng_error_code) cmd_rotate_session( |
| 2797 | session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); |
| 2798 | if (ret != LTTNG_OK) { |
| 2799 | goto error; |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | /* Kernel tracing */ |
| 2805 | if (ksession != nullptr) { |
| 2806 | DBG("Start kernel tracing session %s", session->name); |
| 2807 | ret = (lttng_error_code) start_kernel_session(ksession); |
| 2808 | if (ret != LTTNG_OK) { |
| 2809 | goto error; |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | /* Flag session that trace should start automatically */ |
| 2814 | if (usess) { |
| 2815 | int int_ret = ust_app_start_trace_all(usess); |
| 2816 | |
| 2817 | if (int_ret < 0) { |
| 2818 | ret = LTTNG_ERR_UST_START_FAIL; |
| 2819 | goto error; |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | /* |
| 2824 | * Open a packet in every stream of the session to ensure that viewers |
| 2825 | * can correctly identify the boundaries of the periods during which |
| 2826 | * tracing was active for this session. |
| 2827 | */ |
| 2828 | ret = session_open_packets(session); |
| 2829 | if (ret != LTTNG_OK) { |
| 2830 | goto error; |
| 2831 | } |
| 2832 | |
| 2833 | /* |
| 2834 | * Clear the flag that indicates that a rotation was done while the |
| 2835 | * session was stopped. |
| 2836 | */ |
| 2837 | session->rotated_after_last_stop = false; |
| 2838 | |
| 2839 | if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) { |
| 2840 | int int_ret = timer_session_rotation_schedule_timer_start( |
| 2841 | session, session->rotate_timer_period); |
| 2842 | |
| 2843 | if (int_ret < 0) { |
| 2844 | ERR("Failed to enable rotate timer"); |
| 2845 | ret = LTTNG_ERR_UNK; |
| 2846 | goto error; |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | ret = LTTNG_OK; |
| 2851 | |
| 2852 | error: |
| 2853 | if (ret == LTTNG_OK) { |
| 2854 | /* Flag this after a successful start. */ |
| 2855 | session->has_been_started |= 1; |
| 2856 | } else { |
| 2857 | session->active = 0; |
| 2858 | /* Restore initial state on error. */ |
| 2859 | session->rotated_after_last_stop = session_rotated_after_last_stop; |
| 2860 | session->cleared_after_last_stop = session_cleared_after_last_stop; |
| 2861 | } |
| 2862 | end: |
| 2863 | return ret; |
| 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | * Command LTTNG_STOP_TRACE processed by the client thread. |
| 2868 | */ |
| 2869 | int cmd_stop_trace(struct ltt_session *session) |
| 2870 | { |
| 2871 | int ret; |
| 2872 | struct ltt_kernel_session *ksession; |
| 2873 | struct ltt_ust_session *usess; |
| 2874 | |
| 2875 | LTTNG_ASSERT(session); |
| 2876 | |
| 2877 | DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
| 2878 | /* Short cut */ |
| 2879 | ksession = session->kernel_session; |
| 2880 | usess = session->ust_session; |
| 2881 | |
| 2882 | /* Session is not active. Skip everything and inform the client. */ |
| 2883 | if (!session->active) { |
| 2884 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
| 2885 | goto error; |
| 2886 | } |
| 2887 | |
| 2888 | ret = stop_kernel_session(ksession); |
| 2889 | if (ret != LTTNG_OK) { |
| 2890 | goto error; |
| 2891 | } |
| 2892 | |
| 2893 | if (usess && usess->active) { |
| 2894 | ret = ust_app_stop_trace_all(usess); |
| 2895 | if (ret < 0) { |
| 2896 | ret = LTTNG_ERR_UST_STOP_FAIL; |
| 2897 | goto error; |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
| 2902 | /* Flag inactive after a successful stop. */ |
| 2903 | session->active = 0; |
| 2904 | ret = LTTNG_OK; |
| 2905 | |
| 2906 | error: |
| 2907 | return ret; |
| 2908 | } |
| 2909 | |
| 2910 | /* |
| 2911 | * Set the base_path of the session only if subdir of a control uris is set. |
| 2912 | * Return LTTNG_OK on success, otherwise LTTNG_ERR_*. |
| 2913 | */ |
| 2914 | static int |
| 2915 | set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris) |
| 2916 | { |
| 2917 | int ret; |
| 2918 | size_t i; |
| 2919 | |
| 2920 | for (i = 0; i < nb_uri; i++) { |
| 2921 | if (uris[i].stype != LTTNG_STREAM_CONTROL || uris[i].subdir[0] == '\0') { |
| 2922 | /* Not interested in these URIs */ |
| 2923 | continue; |
| 2924 | } |
| 2925 | |
| 2926 | if (session->base_path != nullptr) { |
| 2927 | free(session->base_path); |
| 2928 | session->base_path = nullptr; |
| 2929 | } |
| 2930 | |
| 2931 | /* Set session base_path */ |
| 2932 | session->base_path = strdup(uris[i].subdir); |
| 2933 | if (!session->base_path) { |
| 2934 | PERROR("Failed to copy base path \"%s\" to session \"%s\"", |
| 2935 | uris[i].subdir, |
| 2936 | session->name); |
| 2937 | ret = LTTNG_ERR_NOMEM; |
| 2938 | goto error; |
| 2939 | } |
| 2940 | DBG2("Setting base path \"%s\" for session \"%s\"", |
| 2941 | session->base_path, |
| 2942 | session->name); |
| 2943 | } |
| 2944 | ret = LTTNG_OK; |
| 2945 | error: |
| 2946 | return ret; |
| 2947 | } |
| 2948 | |
| 2949 | /* |
| 2950 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. |
| 2951 | */ |
| 2952 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris) |
| 2953 | { |
| 2954 | int ret, i; |
| 2955 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 2956 | struct ltt_ust_session *usess = session->ust_session; |
| 2957 | |
| 2958 | LTTNG_ASSERT(session); |
| 2959 | LTTNG_ASSERT(uris); |
| 2960 | LTTNG_ASSERT(nb_uri > 0); |
| 2961 | |
| 2962 | /* Can't set consumer URI if the session is active. */ |
| 2963 | if (session->active) { |
| 2964 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
| 2965 | goto error; |
| 2966 | } |
| 2967 | |
| 2968 | /* |
| 2969 | * Set the session base path if any. This is done inside |
| 2970 | * cmd_set_consumer_uri to preserve backward compatibility of the |
| 2971 | * previous session creation api vs the session descriptor api. |
| 2972 | */ |
| 2973 | ret = set_session_base_path_from_uris(session, nb_uri, uris); |
| 2974 | if (ret != LTTNG_OK) { |
| 2975 | goto error; |
| 2976 | } |
| 2977 | |
| 2978 | /* Set the "global" consumer URIs */ |
| 2979 | for (i = 0; i < nb_uri; i++) { |
| 2980 | ret = add_uri_to_consumer(session, session->consumer, &uris[i], LTTNG_DOMAIN_NONE); |
| 2981 | if (ret != LTTNG_OK) { |
| 2982 | goto error; |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | /* Set UST session URIs */ |
| 2987 | if (session->ust_session) { |
| 2988 | for (i = 0; i < nb_uri; i++) { |
| 2989 | ret = add_uri_to_consumer(session, |
| 2990 | session->ust_session->consumer, |
| 2991 | &uris[i], |
| 2992 | LTTNG_DOMAIN_UST); |
| 2993 | if (ret != LTTNG_OK) { |
| 2994 | goto error; |
| 2995 | } |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* Set kernel session URIs */ |
| 3000 | if (session->kernel_session) { |
| 3001 | for (i = 0; i < nb_uri; i++) { |
| 3002 | ret = add_uri_to_consumer(session, |
| 3003 | session->kernel_session->consumer, |
| 3004 | &uris[i], |
| 3005 | LTTNG_DOMAIN_KERNEL); |
| 3006 | if (ret != LTTNG_OK) { |
| 3007 | goto error; |
| 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | * Make sure to set the session in output mode after we set URI since a |
| 3014 | * session can be created without URL (thus flagged in no output mode). |
| 3015 | */ |
| 3016 | session->output_traces = 1; |
| 3017 | if (ksess) { |
| 3018 | ksess->output_traces = 1; |
| 3019 | } |
| 3020 | |
| 3021 | if (usess) { |
| 3022 | usess->output_traces = 1; |
| 3023 | } |
| 3024 | |
| 3025 | /* All good! */ |
| 3026 | ret = LTTNG_OK; |
| 3027 | |
| 3028 | error: |
| 3029 | return ret; |
| 3030 | } |
| 3031 | |
| 3032 | static enum lttng_error_code |
| 3033 | set_session_output_from_descriptor(struct ltt_session *session, |
| 3034 | const struct lttng_session_descriptor *descriptor) |
| 3035 | { |
| 3036 | int ret; |
| 3037 | enum lttng_error_code ret_code = LTTNG_OK; |
| 3038 | enum lttng_session_descriptor_type session_type = |
| 3039 | lttng_session_descriptor_get_type(descriptor); |
| 3040 | enum lttng_session_descriptor_output_type output_type = |
| 3041 | lttng_session_descriptor_get_output_type(descriptor); |
| 3042 | struct lttng_uri uris[2] = {}; |
| 3043 | size_t uri_count = 0; |
| 3044 | |
| 3045 | switch (output_type) { |
| 3046 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: |
| 3047 | goto end; |
| 3048 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: |
| 3049 | lttng_session_descriptor_get_local_output_uri(descriptor, &uris[0]); |
| 3050 | uri_count = 1; |
| 3051 | break; |
| 3052 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: |
| 3053 | lttng_session_descriptor_get_network_output_uris(descriptor, &uris[0], &uris[1]); |
| 3054 | uri_count = 2; |
| 3055 | break; |
| 3056 | default: |
| 3057 | ret_code = LTTNG_ERR_INVALID; |
| 3058 | goto end; |
| 3059 | } |
| 3060 | |
| 3061 | switch (session_type) { |
| 3062 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: |
| 3063 | { |
| 3064 | struct snapshot_output *new_output = nullptr; |
| 3065 | |
| 3066 | new_output = snapshot_output_alloc(); |
| 3067 | if (!new_output) { |
| 3068 | ret_code = LTTNG_ERR_NOMEM; |
| 3069 | goto end; |
| 3070 | } |
| 3071 | |
| 3072 | ret = snapshot_output_init_with_uri(session, |
| 3073 | DEFAULT_SNAPSHOT_MAX_SIZE, |
| 3074 | nullptr, |
| 3075 | uris, |
| 3076 | uri_count, |
| 3077 | session->consumer, |
| 3078 | new_output, |
| 3079 | &session->snapshot); |
| 3080 | if (ret < 0) { |
| 3081 | ret_code = (ret == -ENOMEM) ? LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; |
| 3082 | snapshot_output_destroy(new_output); |
| 3083 | goto end; |
| 3084 | } |
| 3085 | snapshot_add_output(&session->snapshot, new_output); |
| 3086 | break; |
| 3087 | } |
| 3088 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: |
| 3089 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: |
| 3090 | { |
| 3091 | ret_code = (lttng_error_code) cmd_set_consumer_uri(session, uri_count, uris); |
| 3092 | break; |
| 3093 | } |
| 3094 | default: |
| 3095 | ret_code = LTTNG_ERR_INVALID; |
| 3096 | goto end; |
| 3097 | } |
| 3098 | end: |
| 3099 | return ret_code; |
| 3100 | } |
| 3101 | |
| 3102 | static enum lttng_error_code |
| 3103 | cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor, |
| 3104 | const lttng_sock_cred *creds, |
| 3105 | const char *home_path) |
| 3106 | { |
| 3107 | int ret; |
| 3108 | enum lttng_error_code ret_code; |
| 3109 | const char *session_name; |
| 3110 | struct ltt_session *new_session = nullptr; |
| 3111 | enum lttng_session_descriptor_status descriptor_status; |
| 3112 | |
| 3113 | session_lock_list(); |
| 3114 | if (home_path) { |
| 3115 | if (*home_path != '/') { |
| 3116 | ERR("Home path provided by client is not absolute"); |
| 3117 | ret_code = LTTNG_ERR_INVALID; |
| 3118 | goto end; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | descriptor_status = lttng_session_descriptor_get_session_name(descriptor, &session_name); |
| 3123 | switch (descriptor_status) { |
| 3124 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: |
| 3125 | break; |
| 3126 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: |
| 3127 | session_name = nullptr; |
| 3128 | break; |
| 3129 | default: |
| 3130 | ret_code = LTTNG_ERR_INVALID; |
| 3131 | goto end; |
| 3132 | } |
| 3133 | |
| 3134 | ret_code = session_create(session_name, creds->uid, creds->gid, &new_session); |
| 3135 | if (ret_code != LTTNG_OK) { |
| 3136 | goto end; |
| 3137 | } |
| 3138 | |
| 3139 | ret_code = notification_thread_command_add_session(the_notification_thread_handle, |
| 3140 | new_session->id, |
| 3141 | new_session->name, |
| 3142 | new_session->uid, |
| 3143 | new_session->gid); |
| 3144 | if (ret_code != LTTNG_OK) { |
| 3145 | goto end; |
| 3146 | } |
| 3147 | |
| 3148 | /* Announce the session's destruction to the notification thread when it is destroyed. */ |
| 3149 | ret = session_add_destroy_notifier( |
| 3150 | new_session, |
| 3151 | [](const struct ltt_session *session, void *user_data __attribute__((unused))) { |
| 3152 | (void) notification_thread_command_remove_session( |
| 3153 | the_notification_thread_handle, session->id); |
| 3154 | }, |
| 3155 | nullptr); |
| 3156 | if (ret) { |
| 3157 | PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s", |
| 3158 | new_session->name); |
| 3159 | ret = LTTNG_ERR_NOMEM; |
| 3160 | goto end; |
| 3161 | } |
| 3162 | |
| 3163 | if (!session_name) { |
| 3164 | ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name); |
| 3165 | if (ret) { |
| 3166 | ret_code = LTTNG_ERR_SESSION_FAIL; |
| 3167 | goto end; |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | if (!lttng_session_descriptor_is_output_destination_initialized(descriptor)) { |
| 3172 | /* |
| 3173 | * Only include the session's creation time in the output |
| 3174 | * destination if the name of the session itself was |
| 3175 | * not auto-generated. |
| 3176 | */ |
| 3177 | ret_code = lttng_session_descriptor_set_default_output( |
| 3178 | descriptor, |
| 3179 | session_name ? &new_session->creation_time : nullptr, |
| 3180 | home_path); |
| 3181 | if (ret_code != LTTNG_OK) { |
| 3182 | goto end; |
| 3183 | } |
| 3184 | } else { |
| 3185 | new_session->has_user_specified_directory = |
| 3186 | lttng_session_descriptor_has_output_directory(descriptor); |
| 3187 | } |
| 3188 | |
| 3189 | switch (lttng_session_descriptor_get_type(descriptor)) { |
| 3190 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: |
| 3191 | new_session->snapshot_mode = 1; |
| 3192 | break; |
| 3193 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: |
| 3194 | new_session->live_timer = |
| 3195 | lttng_session_descriptor_live_get_timer_interval(descriptor); |
| 3196 | break; |
| 3197 | default: |
| 3198 | break; |
| 3199 | } |
| 3200 | |
| 3201 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
| 3202 | if (ret_code != LTTNG_OK) { |
| 3203 | goto end; |
| 3204 | } |
| 3205 | new_session->consumer->enabled = 1; |
| 3206 | ret_code = LTTNG_OK; |
| 3207 | end: |
| 3208 | /* Release reference provided by the session_create function. */ |
| 3209 | session_put(new_session); |
| 3210 | if (ret_code != LTTNG_OK && new_session) { |
| 3211 | /* Release the global reference on error. */ |
| 3212 | session_destroy(new_session); |
| 3213 | } |
| 3214 | session_unlock_list(); |
| 3215 | return ret_code; |
| 3216 | } |
| 3217 | |
| 3218 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, |
| 3219 | int sock, |
| 3220 | struct lttng_session_descriptor **return_descriptor) |
| 3221 | { |
| 3222 | int ret; |
| 3223 | size_t payload_size; |
| 3224 | struct lttng_dynamic_buffer payload; |
| 3225 | struct lttng_buffer_view home_dir_view; |
| 3226 | struct lttng_buffer_view session_descriptor_view; |
| 3227 | struct lttng_session_descriptor *session_descriptor = nullptr; |
| 3228 | enum lttng_error_code ret_code; |
| 3229 | |
| 3230 | lttng_dynamic_buffer_init(&payload); |
| 3231 | if (cmd_ctx->lsm.u.create_session.home_dir_size >= LTTNG_PATH_MAX) { |
| 3232 | ret_code = LTTNG_ERR_INVALID; |
| 3233 | goto error; |
| 3234 | } |
| 3235 | if (cmd_ctx->lsm.u.create_session.session_descriptor_size > |
| 3236 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { |
| 3237 | ret_code = LTTNG_ERR_INVALID; |
| 3238 | goto error; |
| 3239 | } |
| 3240 | |
| 3241 | payload_size = cmd_ctx->lsm.u.create_session.home_dir_size + |
| 3242 | cmd_ctx->lsm.u.create_session.session_descriptor_size; |
| 3243 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); |
| 3244 | if (ret) { |
| 3245 | ret_code = LTTNG_ERR_NOMEM; |
| 3246 | goto error; |
| 3247 | } |
| 3248 | |
| 3249 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
| 3250 | if (ret <= 0) { |
| 3251 | ERR("Reception of session descriptor failed, aborting."); |
| 3252 | ret_code = LTTNG_ERR_SESSION_FAIL; |
| 3253 | goto error; |
| 3254 | } |
| 3255 | |
| 3256 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
| 3257 | &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size); |
| 3258 | if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 && |
| 3259 | !lttng_buffer_view_is_valid(&home_dir_view)) { |
| 3260 | ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory"); |
| 3261 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; |
| 3262 | goto error; |
| 3263 | } |
| 3264 | |
| 3265 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( |
| 3266 | &payload, |
| 3267 | cmd_ctx->lsm.u.create_session.home_dir_size, |
| 3268 | cmd_ctx->lsm.u.create_session.session_descriptor_size); |
| 3269 | if (!lttng_buffer_view_is_valid(&session_descriptor_view)) { |
| 3270 | ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor"); |
| 3271 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; |
| 3272 | goto error; |
| 3273 | } |
| 3274 | |
| 3275 | ret = lttng_session_descriptor_create_from_buffer(&session_descriptor_view, |
| 3276 | &session_descriptor); |
| 3277 | if (ret < 0) { |
| 3278 | ERR("Failed to create session descriptor from payload of \"create session\" command"); |
| 3279 | ret_code = LTTNG_ERR_INVALID; |
| 3280 | goto error; |
| 3281 | } |
| 3282 | |
| 3283 | /* |
| 3284 | * Sets the descriptor's auto-generated properties (name, output) if |
| 3285 | * needed. |
| 3286 | */ |
| 3287 | ret_code = cmd_create_session_from_descriptor(session_descriptor, |
| 3288 | &cmd_ctx->creds, |
| 3289 | home_dir_view.size ? home_dir_view.data : |
| 3290 | nullptr); |
| 3291 | if (ret_code != LTTNG_OK) { |
| 3292 | goto error; |
| 3293 | } |
| 3294 | |
| 3295 | ret_code = LTTNG_OK; |
| 3296 | *return_descriptor = session_descriptor; |
| 3297 | session_descriptor = nullptr; |
| 3298 | error: |
| 3299 | lttng_dynamic_buffer_reset(&payload); |
| 3300 | lttng_session_descriptor_destroy(session_descriptor); |
| 3301 | return ret_code; |
| 3302 | } |
| 3303 | |
| 3304 | static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context) |
| 3305 | { |
| 3306 | int ret; |
| 3307 | ssize_t comm_ret; |
| 3308 | const struct cmd_destroy_session_reply_context *reply_context = |
| 3309 | (cmd_destroy_session_reply_context *) _reply_context; |
| 3310 | struct lttng_dynamic_buffer payload; |
| 3311 | struct lttcomm_session_destroy_command_header cmd_header; |
| 3312 | struct lttng_trace_archive_location *location = nullptr; |
| 3313 | struct lttcomm_lttng_msg llm = { |
| 3314 | .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION, |
| 3315 | .ret_code = reply_context->destruction_status, |
| 3316 | .pid = UINT32_MAX, |
| 3317 | .cmd_header_size = sizeof(struct lttcomm_session_destroy_command_header), |
| 3318 | .data_size = 0, |
| 3319 | .fd_count = 0, |
| 3320 | }; |
| 3321 | size_t payload_size_before_location; |
| 3322 | |
| 3323 | lttng_dynamic_buffer_init(&payload); |
| 3324 | |
| 3325 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); |
| 3326 | if (ret) { |
| 3327 | ERR("Failed to append session destruction message"); |
| 3328 | goto error; |
| 3329 | } |
| 3330 | |
| 3331 | cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ? |
| 3332 | session->rotation_state : |
| 3333 | LTTNG_ROTATION_STATE_NO_ROTATION); |
| 3334 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, sizeof(cmd_header)); |
| 3335 | if (ret) { |
| 3336 | ERR("Failed to append session destruction command header"); |
| 3337 | goto error; |
| 3338 | } |
| 3339 | |
| 3340 | if (!reply_context->implicit_rotation_on_destroy) { |
| 3341 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", |
| 3342 | session->name); |
| 3343 | goto send_reply; |
| 3344 | } |
| 3345 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { |
| 3346 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", |
| 3347 | session->name); |
| 3348 | goto send_reply; |
| 3349 | } |
| 3350 | |
| 3351 | location = session_get_trace_archive_location(session); |
| 3352 | if (!location) { |
| 3353 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", |
| 3354 | session->name); |
| 3355 | goto error; |
| 3356 | } |
| 3357 | |
| 3358 | payload_size_before_location = payload.size; |
| 3359 | comm_ret = lttng_trace_archive_location_serialize(location, &payload); |
| 3360 | lttng_trace_archive_location_put(location); |
| 3361 | if (comm_ret < 0) { |
| 3362 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", |
| 3363 | session->name); |
| 3364 | goto error; |
| 3365 | } |
| 3366 | /* Update the message to indicate the location's length. */ |
| 3367 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = |
| 3368 | payload.size - payload_size_before_location; |
| 3369 | send_reply: |
| 3370 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, payload.data, payload.size); |
| 3371 | if (comm_ret != (ssize_t) payload.size) { |
| 3372 | ERR("Failed to send result of the destruction of session \"%s\" to client", |
| 3373 | session->name); |
| 3374 | } |
| 3375 | error: |
| 3376 | ret = close(reply_context->reply_sock_fd); |
| 3377 | if (ret) { |
| 3378 | PERROR("Failed to close client socket in deferred session destroy reply"); |
| 3379 | } |
| 3380 | lttng_dynamic_buffer_reset(&payload); |
| 3381 | free(_reply_context); |
| 3382 | } |
| 3383 | |
| 3384 | /* |
| 3385 | * Command LTTNG_DESTROY_SESSION processed by the client thread. |
| 3386 | * |
| 3387 | * Called with session lock held. |
| 3388 | */ |
| 3389 | int cmd_destroy_session(struct ltt_session *session, |
| 3390 | struct notification_thread_handle *notification_thread_handle, |
| 3391 | int *sock_fd) |
| 3392 | { |
| 3393 | int ret; |
| 3394 | enum lttng_error_code destruction_last_error = LTTNG_OK; |
| 3395 | struct cmd_destroy_session_reply_context *reply_context = nullptr; |
| 3396 | |
| 3397 | if (sock_fd) { |
| 3398 | reply_context = zmalloc<cmd_destroy_session_reply_context>(); |
| 3399 | if (!reply_context) { |
| 3400 | ret = LTTNG_ERR_NOMEM; |
| 3401 | goto end; |
| 3402 | } |
| 3403 | |
| 3404 | reply_context->reply_sock_fd = *sock_fd; |
| 3405 | } |
| 3406 | |
| 3407 | /* Safety net */ |
| 3408 | LTTNG_ASSERT(session); |
| 3409 | |
| 3410 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id); |
| 3411 | if (session->active) { |
| 3412 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", |
| 3413 | session->name); |
| 3414 | ret = cmd_stop_trace(session); |
| 3415 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
| 3416 | /* Carry on with the destruction of the session. */ |
| 3417 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", |
| 3418 | session->name, |
| 3419 | lttng_strerror(-ret)); |
| 3420 | destruction_last_error = (lttng_error_code) ret; |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | if (session->rotation_schedule_timer_enabled) { |
| 3425 | if (timer_session_rotation_schedule_timer_stop(session)) { |
| 3426 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", |
| 3427 | session->name); |
| 3428 | destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR; |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | if (session->rotate_size) { |
| 3433 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); |
| 3434 | session->rotate_size = 0; |
| 3435 | } |
| 3436 | |
| 3437 | if (session->rotated && session->current_trace_chunk && session->output_traces) { |
| 3438 | /* |
| 3439 | * Perform a last rotation on destruction if rotations have |
| 3440 | * occurred during the session's lifetime. |
| 3441 | */ |
| 3442 | ret = cmd_rotate_session( |
| 3443 | session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); |
| 3444 | if (ret != LTTNG_OK) { |
| 3445 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", |
| 3446 | session->name, |
| 3447 | lttng_strerror(-ret)); |
| 3448 | destruction_last_error = (lttng_error_code) -ret; |
| 3449 | } |
| 3450 | if (reply_context) { |
| 3451 | reply_context->implicit_rotation_on_destroy = true; |
| 3452 | } |
| 3453 | } else if (session->has_been_started && session->current_trace_chunk) { |
| 3454 | /* |
| 3455 | * The user has not triggered a session rotation. However, to |
| 3456 | * ensure all data has been consumed, the session is rotated |
| 3457 | * to a 'null' trace chunk before it is destroyed. |
| 3458 | * |
| 3459 | * This is a "quiet" rotation meaning that no notification is |
| 3460 | * emitted and no renaming of the current trace chunk takes |
| 3461 | * place. |
| 3462 | */ |
| 3463 | ret = cmd_rotate_session( |
| 3464 | session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); |
| 3465 | /* |
| 3466 | * Rotation operations may not be supported by the kernel |
| 3467 | * tracer. Hence, do not consider this implicit rotation as |
| 3468 | * a session destruction error. The library has already stopped |
| 3469 | * the session and waited for pending data; there is nothing |
| 3470 | * left to do but complete the destruction of the session. |
| 3471 | */ |
| 3472 | if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) { |
| 3473 | ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s", |
| 3474 | session->name, |
| 3475 | lttng_strerror(ret)); |
| 3476 | destruction_last_error = (lttng_error_code) -ret; |
| 3477 | } |
| 3478 | } |
| 3479 | |
| 3480 | if (session->shm_path[0]) { |
| 3481 | /* |
| 3482 | * When a session is created with an explicit shm_path, |
| 3483 | * the consumer daemon will create its shared memory files |
| 3484 | * at that location and will *not* unlink them. This is normal |
| 3485 | * as the intention of that feature is to make it possible |
| 3486 | * to retrieve the content of those files should a crash occur. |
| 3487 | * |
| 3488 | * To ensure the content of those files can be used, the |
| 3489 | * sessiond daemon will replicate the content of the metadata |
| 3490 | * cache in a metadata file. |
| 3491 | * |
| 3492 | * On clean-up, it is expected that the consumer daemon will |
| 3493 | * unlink the shared memory files and that the session daemon |
| 3494 | * will unlink the metadata file. Then, the session's directory |
| 3495 | * in the shm path can be removed. |
| 3496 | * |
| 3497 | * Unfortunately, a flaw in the design of the sessiond's and |
| 3498 | * consumerd's tear down of channels makes it impossible to |
| 3499 | * determine when the sessiond _and_ the consumerd have both |
| 3500 | * destroyed their representation of a channel. For one, the |
| 3501 | * unlinking, close, and rmdir happen in deferred 'call_rcu' |
| 3502 | * callbacks in both daemons. |
| 3503 | * |
| 3504 | * However, it is also impossible for the sessiond to know when |
| 3505 | * the consumer daemon is done destroying its channel(s) since |
| 3506 | * it occurs as a reaction to the closing of the channel's file |
| 3507 | * descriptor. There is no resulting communication initiated |
| 3508 | * from the consumerd to the sessiond to confirm that the |
| 3509 | * operation is completed (and was successful). |
| 3510 | * |
| 3511 | * Until this is all fixed, the session daemon checks for the |
| 3512 | * removal of the session's shm path which makes it possible |
| 3513 | * to safely advertise a session as having been destroyed. |
| 3514 | * |
| 3515 | * Prior to this fix, it was not possible to reliably save |
| 3516 | * a session making use of the --shm-path option, destroy it, |
| 3517 | * and load it again. This is because the creation of the |
| 3518 | * session would fail upon seeing the session's shm path |
| 3519 | * already in existence. |
| 3520 | * |
| 3521 | * Note that none of the error paths in the check for the |
| 3522 | * directory's existence return an error. This is normal |
| 3523 | * as there isn't much that can be done. The session will |
| 3524 | * be destroyed properly, except that we can't offer the |
| 3525 | * guarantee that the same session can be re-created. |
| 3526 | */ |
| 3527 | current_completion_handler = &destroy_completion_handler.handler; |
| 3528 | ret = lttng_strncpy(destroy_completion_handler.shm_path, |
| 3529 | session->shm_path, |
| 3530 | sizeof(destroy_completion_handler.shm_path)); |
| 3531 | LTTNG_ASSERT(!ret); |
| 3532 | } |
| 3533 | |
| 3534 | /* |
| 3535 | * The session is destroyed. However, note that the command context |
| 3536 | * still holds a reference to the session, thus delaying its destruction |
| 3537 | * _at least_ up to the point when that reference is released. |
| 3538 | */ |
| 3539 | session_destroy(session); |
| 3540 | if (reply_context) { |
| 3541 | reply_context->destruction_status = destruction_last_error; |
| 3542 | ret = session_add_destroy_notifier( |
| 3543 | session, cmd_destroy_session_reply, (void *) reply_context); |
| 3544 | if (ret) { |
| 3545 | ret = LTTNG_ERR_FATAL; |
| 3546 | goto end; |
| 3547 | } else { |
| 3548 | *sock_fd = -1; |
| 3549 | } |
| 3550 | } |
| 3551 | ret = LTTNG_OK; |
| 3552 | end: |
| 3553 | return ret; |
| 3554 | } |
| 3555 | |
| 3556 | /* |
| 3557 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. |
| 3558 | */ |
| 3559 | int cmd_register_consumer(struct ltt_session *session, |
| 3560 | enum lttng_domain_type domain, |
| 3561 | const char *sock_path, |
| 3562 | struct consumer_data *cdata) |
| 3563 | { |
| 3564 | int ret, sock; |
| 3565 | struct consumer_socket *socket = nullptr; |
| 3566 | |
| 3567 | LTTNG_ASSERT(session); |
| 3568 | LTTNG_ASSERT(cdata); |
| 3569 | LTTNG_ASSERT(sock_path); |
| 3570 | |
| 3571 | switch (domain) { |
| 3572 | case LTTNG_DOMAIN_KERNEL: |
| 3573 | { |
| 3574 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3575 | |
| 3576 | LTTNG_ASSERT(ksess); |
| 3577 | |
| 3578 | /* Can't register a consumer if there is already one */ |
| 3579 | if (ksess->consumer_fds_sent != 0) { |
| 3580 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
| 3581 | goto error; |
| 3582 | } |
| 3583 | |
| 3584 | sock = lttcomm_connect_unix_sock(sock_path); |
| 3585 | if (sock < 0) { |
| 3586 | ret = LTTNG_ERR_CONNECT_FAIL; |
| 3587 | goto error; |
| 3588 | } |
| 3589 | cdata->cmd_sock = sock; |
| 3590 | |
| 3591 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
| 3592 | if (socket == nullptr) { |
| 3593 | ret = close(sock); |
| 3594 | if (ret < 0) { |
| 3595 | PERROR("close register consumer"); |
| 3596 | } |
| 3597 | cdata->cmd_sock = -1; |
| 3598 | ret = LTTNG_ERR_FATAL; |
| 3599 | goto error; |
| 3600 | } |
| 3601 | |
| 3602 | socket->lock = zmalloc<pthread_mutex_t>(); |
| 3603 | if (socket->lock == nullptr) { |
| 3604 | PERROR("zmalloc pthread mutex"); |
| 3605 | ret = LTTNG_ERR_FATAL; |
| 3606 | goto error; |
| 3607 | } |
| 3608 | |
| 3609 | pthread_mutex_init(socket->lock, nullptr); |
| 3610 | socket->registered = 1; |
| 3611 | |
| 3612 | rcu_read_lock(); |
| 3613 | consumer_add_socket(socket, ksess->consumer); |
| 3614 | rcu_read_unlock(); |
| 3615 | |
| 3616 | pthread_mutex_lock(&cdata->pid_mutex); |
| 3617 | cdata->pid = -1; |
| 3618 | pthread_mutex_unlock(&cdata->pid_mutex); |
| 3619 | |
| 3620 | break; |
| 3621 | } |
| 3622 | default: |
| 3623 | /* TODO: Userspace tracing */ |
| 3624 | ret = LTTNG_ERR_UND; |
| 3625 | goto error; |
| 3626 | } |
| 3627 | |
| 3628 | return LTTNG_OK; |
| 3629 | |
| 3630 | error: |
| 3631 | if (socket) { |
| 3632 | consumer_destroy_socket(socket); |
| 3633 | } |
| 3634 | return ret; |
| 3635 | } |
| 3636 | |
| 3637 | /* |
| 3638 | * Command LTTNG_LIST_DOMAINS processed by the client thread. |
| 3639 | */ |
| 3640 | ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains) |
| 3641 | { |
| 3642 | int ret, index = 0; |
| 3643 | ssize_t nb_dom = 0; |
| 3644 | struct agent *agt; |
| 3645 | struct lttng_ht_iter iter; |
| 3646 | |
| 3647 | if (session->kernel_session != nullptr) { |
| 3648 | DBG3("Listing domains found kernel domain"); |
| 3649 | nb_dom++; |
| 3650 | } |
| 3651 | |
| 3652 | if (session->ust_session != nullptr) { |
| 3653 | DBG3("Listing domains found UST global domain"); |
| 3654 | nb_dom++; |
| 3655 | |
| 3656 | rcu_read_lock(); |
| 3657 | cds_lfht_for_each_entry ( |
| 3658 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3659 | if (agt->being_used) { |
| 3660 | nb_dom++; |
| 3661 | } |
| 3662 | } |
| 3663 | rcu_read_unlock(); |
| 3664 | } |
| 3665 | |
| 3666 | if (!nb_dom) { |
| 3667 | goto end; |
| 3668 | } |
| 3669 | |
| 3670 | *domains = calloc<lttng_domain>(nb_dom); |
| 3671 | if (*domains == nullptr) { |
| 3672 | ret = LTTNG_ERR_FATAL; |
| 3673 | goto error; |
| 3674 | } |
| 3675 | |
| 3676 | if (session->kernel_session != nullptr) { |
| 3677 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; |
| 3678 | |
| 3679 | /* Kernel session buffer type is always GLOBAL */ |
| 3680 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; |
| 3681 | |
| 3682 | index++; |
| 3683 | } |
| 3684 | |
| 3685 | if (session->ust_session != nullptr) { |
| 3686 | (*domains)[index].type = LTTNG_DOMAIN_UST; |
| 3687 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
| 3688 | index++; |
| 3689 | |
| 3690 | rcu_read_lock(); |
| 3691 | cds_lfht_for_each_entry ( |
| 3692 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3693 | if (agt->being_used) { |
| 3694 | (*domains)[index].type = agt->domain; |
| 3695 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
| 3696 | index++; |
| 3697 | } |
| 3698 | } |
| 3699 | rcu_read_unlock(); |
| 3700 | } |
| 3701 | end: |
| 3702 | return nb_dom; |
| 3703 | |
| 3704 | error: |
| 3705 | /* Return negative value to differentiate return code */ |
| 3706 | return -ret; |
| 3707 | } |
| 3708 | |
| 3709 | /* |
| 3710 | * Command LTTNG_LIST_CHANNELS processed by the client thread. |
| 3711 | */ |
| 3712 | enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain, |
| 3713 | struct ltt_session *session, |
| 3714 | struct lttng_payload *payload) |
| 3715 | { |
| 3716 | int ret = 0; |
| 3717 | unsigned int i = 0; |
| 3718 | struct lttcomm_list_command_header cmd_header = {}; |
| 3719 | size_t cmd_header_offset; |
| 3720 | enum lttng_error_code ret_code; |
| 3721 | |
| 3722 | assert(session); |
| 3723 | assert(payload); |
| 3724 | |
| 3725 | DBG("Listing channels for session %s", session->name); |
| 3726 | |
| 3727 | cmd_header_offset = payload->buffer.size; |
| 3728 | |
| 3729 | /* Reserve space for command reply header. */ |
| 3730 | ret = lttng_dynamic_buffer_set_size(&payload->buffer, |
| 3731 | cmd_header_offset + sizeof(cmd_header)); |
| 3732 | if (ret) { |
| 3733 | ret_code = LTTNG_ERR_NOMEM; |
| 3734 | goto end; |
| 3735 | } |
| 3736 | |
| 3737 | switch (domain) { |
| 3738 | case LTTNG_DOMAIN_KERNEL: |
| 3739 | { |
| 3740 | /* Kernel channels */ |
| 3741 | struct ltt_kernel_channel *kchan; |
| 3742 | if (session->kernel_session != nullptr) { |
| 3743 | cds_list_for_each_entry ( |
| 3744 | kchan, &session->kernel_session->channel_list.head, list) { |
| 3745 | uint64_t discarded_events, lost_packets; |
| 3746 | struct lttng_channel_extended *extended; |
| 3747 | |
| 3748 | extended = (struct lttng_channel_extended *) |
| 3749 | kchan->channel->attr.extended.ptr; |
| 3750 | |
| 3751 | ret = get_kernel_runtime_stats( |
| 3752 | session, kchan, &discarded_events, &lost_packets); |
| 3753 | if (ret < 0) { |
| 3754 | ret_code = LTTNG_ERR_UNK; |
| 3755 | goto end; |
| 3756 | } |
| 3757 | |
| 3758 | /* |
| 3759 | * Update the discarded_events and lost_packets |
| 3760 | * count for the channel |
| 3761 | */ |
| 3762 | extended->discarded_events = discarded_events; |
| 3763 | extended->lost_packets = lost_packets; |
| 3764 | |
| 3765 | ret = lttng_channel_serialize(kchan->channel, &payload->buffer); |
| 3766 | if (ret) { |
| 3767 | ERR("Failed to serialize lttng_channel: channel name = '%s'", |
| 3768 | kchan->channel->name); |
| 3769 | ret_code = LTTNG_ERR_UNK; |
| 3770 | goto end; |
| 3771 | } |
| 3772 | |
| 3773 | i++; |
| 3774 | } |
| 3775 | } |
| 3776 | break; |
| 3777 | } |
| 3778 | case LTTNG_DOMAIN_UST: |
| 3779 | { |
| 3780 | struct lttng_ht_iter iter; |
| 3781 | struct ltt_ust_channel *uchan; |
| 3782 | |
| 3783 | rcu_read_lock(); |
| 3784 | cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht, |
| 3785 | &iter.iter, |
| 3786 | uchan, |
| 3787 | node.node) { |
| 3788 | uint64_t discarded_events = 0, lost_packets = 0; |
| 3789 | struct lttng_channel *channel = nullptr; |
| 3790 | struct lttng_channel_extended *extended; |
| 3791 | |
| 3792 | channel = trace_ust_channel_to_lttng_channel(uchan); |
| 3793 | if (!channel) { |
| 3794 | ret_code = LTTNG_ERR_NOMEM; |
| 3795 | goto end; |
| 3796 | } |
| 3797 | |
| 3798 | extended = (struct lttng_channel_extended *) channel->attr.extended.ptr; |
| 3799 | |
| 3800 | ret = get_ust_runtime_stats( |
| 3801 | session, uchan, &discarded_events, &lost_packets); |
| 3802 | if (ret < 0) { |
| 3803 | lttng_channel_destroy(channel); |
| 3804 | ret_code = LTTNG_ERR_UNK; |
| 3805 | goto end; |
| 3806 | } |
| 3807 | |
| 3808 | extended->discarded_events = discarded_events; |
| 3809 | extended->lost_packets = lost_packets; |
| 3810 | |
| 3811 | ret = lttng_channel_serialize(channel, &payload->buffer); |
| 3812 | if (ret) { |
| 3813 | ERR("Failed to serialize lttng_channel: channel name = '%s'", |
| 3814 | channel->name); |
| 3815 | lttng_channel_destroy(channel); |
| 3816 | ret_code = LTTNG_ERR_UNK; |
| 3817 | goto end; |
| 3818 | } |
| 3819 | |
| 3820 | lttng_channel_destroy(channel); |
| 3821 | i++; |
| 3822 | } |
| 3823 | rcu_read_unlock(); |
| 3824 | break; |
| 3825 | } |
| 3826 | default: |
| 3827 | break; |
| 3828 | } |
| 3829 | |
| 3830 | if (i > UINT32_MAX) { |
| 3831 | ERR("Channel count would overflow the channel listing command's reply"); |
| 3832 | ret_code = LTTNG_ERR_OVERFLOW; |
| 3833 | goto end; |
| 3834 | } |
| 3835 | |
| 3836 | /* Update command reply header. */ |
| 3837 | cmd_header.count = (uint32_t) i; |
| 3838 | memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header)); |
| 3839 | ret_code = LTTNG_OK; |
| 3840 | |
| 3841 | end: |
| 3842 | return ret_code; |
| 3843 | } |
| 3844 | |
| 3845 | /* |
| 3846 | * Command LTTNG_LIST_EVENTS processed by the client thread. |
| 3847 | */ |
| 3848 | enum lttng_error_code cmd_list_events(enum lttng_domain_type domain, |
| 3849 | struct ltt_session *session, |
| 3850 | char *channel_name, |
| 3851 | struct lttng_payload *reply_payload) |
| 3852 | { |
| 3853 | int buffer_resize_ret; |
| 3854 | enum lttng_error_code ret_code = LTTNG_OK; |
| 3855 | struct lttcomm_list_command_header reply_command_header = {}; |
| 3856 | size_t reply_command_header_offset; |
| 3857 | unsigned int nb_events = 0; |
| 3858 | |
| 3859 | assert(reply_payload); |
| 3860 | |
| 3861 | /* Reserve space for command reply header. */ |
| 3862 | reply_command_header_offset = reply_payload->buffer.size; |
| 3863 | buffer_resize_ret = lttng_dynamic_buffer_set_size( |
| 3864 | &reply_payload->buffer, |
| 3865 | reply_command_header_offset + sizeof(struct lttcomm_list_command_header)); |
| 3866 | if (buffer_resize_ret) { |
| 3867 | ret_code = LTTNG_ERR_NOMEM; |
| 3868 | goto end; |
| 3869 | } |
| 3870 | |
| 3871 | switch (domain) { |
| 3872 | case LTTNG_DOMAIN_KERNEL: |
| 3873 | if (session->kernel_session != nullptr) { |
| 3874 | ret_code = list_lttng_kernel_events( |
| 3875 | channel_name, session->kernel_session, reply_payload, &nb_events); |
| 3876 | } |
| 3877 | |
| 3878 | break; |
| 3879 | case LTTNG_DOMAIN_UST: |
| 3880 | { |
| 3881 | if (session->ust_session != nullptr) { |
| 3882 | ret_code = |
| 3883 | list_lttng_ust_global_events(channel_name, |
| 3884 | &session->ust_session->domain_global, |
| 3885 | reply_payload, |
| 3886 | &nb_events); |
| 3887 | } |
| 3888 | |
| 3889 | break; |
| 3890 | } |
| 3891 | case LTTNG_DOMAIN_LOG4J: |
| 3892 | case LTTNG_DOMAIN_JUL: |
| 3893 | case LTTNG_DOMAIN_PYTHON: |
| 3894 | if (session->ust_session) { |
| 3895 | struct lttng_ht_iter iter; |
| 3896 | struct agent *agt; |
| 3897 | |
| 3898 | rcu_read_lock(); |
| 3899 | cds_lfht_for_each_entry ( |
| 3900 | session->ust_session->agents->ht, &iter.iter, agt, node.node) { |
| 3901 | if (agt->domain == domain) { |
| 3902 | ret_code = list_lttng_agent_events( |
| 3903 | agt, reply_payload, &nb_events); |
| 3904 | break; |
| 3905 | } |
| 3906 | } |
| 3907 | |
| 3908 | rcu_read_unlock(); |
| 3909 | } |
| 3910 | break; |
| 3911 | default: |
| 3912 | ret_code = LTTNG_ERR_UND; |
| 3913 | break; |
| 3914 | } |
| 3915 | |
| 3916 | if (nb_events > UINT32_MAX) { |
| 3917 | ret_code = LTTNG_ERR_OVERFLOW; |
| 3918 | goto end; |
| 3919 | } |
| 3920 | |
| 3921 | /* Update command reply header. */ |
| 3922 | reply_command_header.count = (uint32_t) nb_events; |
| 3923 | memcpy(reply_payload->buffer.data + reply_command_header_offset, |
| 3924 | &reply_command_header, |
| 3925 | sizeof(reply_command_header)); |
| 3926 | |
| 3927 | end: |
| 3928 | return ret_code; |
| 3929 | } |
| 3930 | |
| 3931 | /* |
| 3932 | * Using the session list, filled a lttng_session array to send back to the |
| 3933 | * client for session listing. |
| 3934 | * |
| 3935 | * The session list lock MUST be acquired before calling this function. Use |
| 3936 | * session_lock_list() and session_unlock_list(). |
| 3937 | */ |
| 3938 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
| 3939 | size_t session_count, |
| 3940 | uid_t uid, |
| 3941 | gid_t gid) |
| 3942 | { |
| 3943 | int ret; |
| 3944 | unsigned int i = 0; |
| 3945 | struct ltt_session *session; |
| 3946 | struct ltt_session_list *list = session_get_list(); |
| 3947 | struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]); |
| 3948 | |
| 3949 | DBG("Getting all available session for UID %d GID %d", uid, gid); |
| 3950 | /* |
| 3951 | * Iterate over session list and append data after the control struct in |
| 3952 | * the buffer. |
| 3953 | */ |
| 3954 | cds_list_for_each_entry (session, &list->head, list) { |
| 3955 | if (!session_get(session)) { |
| 3956 | continue; |
| 3957 | } |
| 3958 | /* |
| 3959 | * Only list the sessions the user can control. |
| 3960 | */ |
| 3961 | if (!session_access_ok(session, uid) || session->destroyed) { |
| 3962 | session_put(session); |
| 3963 | continue; |
| 3964 | } |
| 3965 | |
| 3966 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 3967 | struct ltt_ust_session *usess = session->ust_session; |
| 3968 | |
| 3969 | if (session->consumer->type == CONSUMER_DST_NET || |
| 3970 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || |
| 3971 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { |
| 3972 | ret = build_network_session_path( |
| 3973 | sessions[i].path, sizeof(sessions[i].path), session); |
| 3974 | } else { |
| 3975 | ret = snprintf(sessions[i].path, |
| 3976 | sizeof(sessions[i].path), |
| 3977 | "%s", |
| 3978 | session->consumer->dst.session_root_path); |
| 3979 | } |
| 3980 | if (ret < 0) { |
| 3981 | PERROR("snprintf session path"); |
| 3982 | session_put(session); |
| 3983 | continue; |
| 3984 | } |
| 3985 | |
| 3986 | strncpy(sessions[i].name, session->name, NAME_MAX); |
| 3987 | sessions[i].name[NAME_MAX - 1] = '\0'; |
| 3988 | sessions[i].enabled = session->active; |
| 3989 | sessions[i].snapshot_mode = session->snapshot_mode; |
| 3990 | sessions[i].live_timer_interval = session->live_timer; |
| 3991 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
| 3992 | extended[i].creation_time.is_set = 1; |
| 3993 | i++; |
| 3994 | session_put(session); |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | /* |
| 3999 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
| 4000 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
| 4001 | */ |
| 4002 | int cmd_data_pending(struct ltt_session *session) |
| 4003 | { |
| 4004 | int ret; |
| 4005 | struct ltt_kernel_session *ksess = session->kernel_session; |
| 4006 | struct ltt_ust_session *usess = session->ust_session; |
| 4007 | |
| 4008 | LTTNG_ASSERT(session); |
| 4009 | |
| 4010 | DBG("Data pending for session %s", session->name); |
| 4011 | |
| 4012 | /* Session MUST be stopped to ask for data availability. */ |
| 4013 | if (session->active) { |
| 4014 | ret = LTTNG_ERR_SESSION_STARTED; |
| 4015 | goto error; |
| 4016 | } else { |
| 4017 | /* |
| 4018 | * If stopped, just make sure we've started before else the above call |
| 4019 | * will always send that there is data pending. |
| 4020 | * |
| 4021 | * The consumer assumes that when the data pending command is received, |
| 4022 | * the trace has been started before or else no output data is written |
| 4023 | * by the streams which is a condition for data pending. So, this is |
| 4024 | * *VERY* important that we don't ask the consumer before a start |
| 4025 | * trace. |
| 4026 | */ |
| 4027 | if (!session->has_been_started) { |
| 4028 | ret = 0; |
| 4029 | goto error; |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /* A rotation is still pending, we have to wait. */ |
| 4034 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 4035 | DBG("Rotate still pending for session %s", session->name); |
| 4036 | ret = 1; |
| 4037 | goto error; |
| 4038 | } |
| 4039 | |
| 4040 | if (ksess && ksess->consumer) { |
| 4041 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
| 4042 | if (ret == 1) { |
| 4043 | /* Data is still being extracted for the kernel. */ |
| 4044 | goto error; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | if (usess && usess->consumer) { |
| 4049 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
| 4050 | if (ret == 1) { |
| 4051 | /* Data is still being extracted for the kernel. */ |
| 4052 | goto error; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | /* Data is ready to be read by a viewer */ |
| 4057 | ret = 0; |
| 4058 | |
| 4059 | error: |
| 4060 | return ret; |
| 4061 | } |
| 4062 | |
| 4063 | /* |
| 4064 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. |
| 4065 | * |
| 4066 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4067 | */ |
| 4068 | int cmd_snapshot_add_output(struct ltt_session *session, |
| 4069 | const struct lttng_snapshot_output *output, |
| 4070 | uint32_t *id) |
| 4071 | { |
| 4072 | int ret; |
| 4073 | struct snapshot_output *new_output; |
| 4074 | |
| 4075 | LTTNG_ASSERT(session); |
| 4076 | LTTNG_ASSERT(output); |
| 4077 | |
| 4078 | DBG("Cmd snapshot add output for session %s", session->name); |
| 4079 | |
| 4080 | /* |
| 4081 | * Can't create an output if the session is not set in no-output mode. |
| 4082 | */ |
| 4083 | if (session->output_traces) { |
| 4084 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4085 | goto error; |
| 4086 | } |
| 4087 | |
| 4088 | if (session->has_non_mmap_channel) { |
| 4089 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; |
| 4090 | goto error; |
| 4091 | } |
| 4092 | |
| 4093 | /* Only one output is allowed until we have the "tee" feature. */ |
| 4094 | if (session->snapshot.nb_output == 1) { |
| 4095 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; |
| 4096 | goto error; |
| 4097 | } |
| 4098 | |
| 4099 | new_output = snapshot_output_alloc(); |
| 4100 | if (!new_output) { |
| 4101 | ret = LTTNG_ERR_NOMEM; |
| 4102 | goto error; |
| 4103 | } |
| 4104 | |
| 4105 | ret = snapshot_output_init(session, |
| 4106 | output->max_size, |
| 4107 | output->name, |
| 4108 | output->ctrl_url, |
| 4109 | output->data_url, |
| 4110 | session->consumer, |
| 4111 | new_output, |
| 4112 | &session->snapshot); |
| 4113 | if (ret < 0) { |
| 4114 | if (ret == -ENOMEM) { |
| 4115 | ret = LTTNG_ERR_NOMEM; |
| 4116 | } else { |
| 4117 | ret = LTTNG_ERR_INVALID; |
| 4118 | } |
| 4119 | goto free_error; |
| 4120 | } |
| 4121 | |
| 4122 | rcu_read_lock(); |
| 4123 | snapshot_add_output(&session->snapshot, new_output); |
| 4124 | if (id) { |
| 4125 | *id = new_output->id; |
| 4126 | } |
| 4127 | rcu_read_unlock(); |
| 4128 | |
| 4129 | return LTTNG_OK; |
| 4130 | |
| 4131 | free_error: |
| 4132 | snapshot_output_destroy(new_output); |
| 4133 | error: |
| 4134 | return ret; |
| 4135 | } |
| 4136 | |
| 4137 | /* |
| 4138 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. |
| 4139 | * |
| 4140 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4141 | */ |
| 4142 | int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output) |
| 4143 | { |
| 4144 | int ret; |
| 4145 | struct snapshot_output *sout = nullptr; |
| 4146 | |
| 4147 | LTTNG_ASSERT(session); |
| 4148 | LTTNG_ASSERT(output); |
| 4149 | |
| 4150 | rcu_read_lock(); |
| 4151 | |
| 4152 | /* |
| 4153 | * Permission denied to create an output if the session is not |
| 4154 | * set in no output mode. |
| 4155 | */ |
| 4156 | if (session->output_traces) { |
| 4157 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4158 | goto error; |
| 4159 | } |
| 4160 | |
| 4161 | if (output->id) { |
| 4162 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", |
| 4163 | output->id, |
| 4164 | session->name); |
| 4165 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); |
| 4166 | } else if (*output->name != '\0') { |
| 4167 | DBG("Cmd snapshot del output name %s for session %s", output->name, session->name); |
| 4168 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); |
| 4169 | } |
| 4170 | if (!sout) { |
| 4171 | ret = LTTNG_ERR_INVALID; |
| 4172 | goto error; |
| 4173 | } |
| 4174 | |
| 4175 | snapshot_delete_output(&session->snapshot, sout); |
| 4176 | snapshot_output_destroy(sout); |
| 4177 | ret = LTTNG_OK; |
| 4178 | |
| 4179 | error: |
| 4180 | rcu_read_unlock(); |
| 4181 | return ret; |
| 4182 | } |
| 4183 | |
| 4184 | /* |
| 4185 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. |
| 4186 | * |
| 4187 | * If no output is available, outputs is untouched and 0 is returned. |
| 4188 | * |
| 4189 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. |
| 4190 | */ |
| 4191 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, |
| 4192 | struct lttng_snapshot_output **outputs) |
| 4193 | { |
| 4194 | int ret, idx = 0; |
| 4195 | struct lttng_snapshot_output *list = nullptr; |
| 4196 | struct lttng_ht_iter iter; |
| 4197 | struct snapshot_output *output; |
| 4198 | |
| 4199 | LTTNG_ASSERT(session); |
| 4200 | LTTNG_ASSERT(outputs); |
| 4201 | |
| 4202 | DBG("Cmd snapshot list outputs for session %s", session->name); |
| 4203 | |
| 4204 | /* |
| 4205 | * Permission denied to create an output if the session is not |
| 4206 | * set in no output mode. |
| 4207 | */ |
| 4208 | if (session->output_traces) { |
| 4209 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 4210 | goto end; |
| 4211 | } |
| 4212 | |
| 4213 | if (session->snapshot.nb_output == 0) { |
| 4214 | ret = 0; |
| 4215 | goto end; |
| 4216 | } |
| 4217 | |
| 4218 | list = calloc<lttng_snapshot_output>(session->snapshot.nb_output); |
| 4219 | if (!list) { |
| 4220 | ret = -LTTNG_ERR_NOMEM; |
| 4221 | goto end; |
| 4222 | } |
| 4223 | |
| 4224 | /* Copy list from session to the new list object. */ |
| 4225 | rcu_read_lock(); |
| 4226 | cds_lfht_for_each_entry (session->snapshot.output_ht->ht, &iter.iter, output, node.node) { |
| 4227 | LTTNG_ASSERT(output->consumer); |
| 4228 | list[idx].id = output->id; |
| 4229 | list[idx].max_size = output->max_size; |
| 4230 | if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) { |
| 4231 | ret = -LTTNG_ERR_INVALID; |
| 4232 | goto error; |
| 4233 | } |
| 4234 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
| 4235 | if (lttng_strncpy(list[idx].ctrl_url, |
| 4236 | output->consumer->dst.session_root_path, |
| 4237 | sizeof(list[idx].ctrl_url))) { |
| 4238 | ret = -LTTNG_ERR_INVALID; |
| 4239 | goto error; |
| 4240 | } |
| 4241 | } else { |
| 4242 | /* Control URI. */ |
| 4243 | ret = uri_to_str_url(&output->consumer->dst.net.control, |
| 4244 | list[idx].ctrl_url, |
| 4245 | sizeof(list[idx].ctrl_url)); |
| 4246 | if (ret < 0) { |
| 4247 | ret = -LTTNG_ERR_NOMEM; |
| 4248 | goto error; |
| 4249 | } |
| 4250 | |
| 4251 | /* Data URI. */ |
| 4252 | ret = uri_to_str_url(&output->consumer->dst.net.data, |
| 4253 | list[idx].data_url, |
| 4254 | sizeof(list[idx].data_url)); |
| 4255 | if (ret < 0) { |
| 4256 | ret = -LTTNG_ERR_NOMEM; |
| 4257 | goto error; |
| 4258 | } |
| 4259 | } |
| 4260 | idx++; |
| 4261 | } |
| 4262 | |
| 4263 | *outputs = list; |
| 4264 | list = nullptr; |
| 4265 | ret = session->snapshot.nb_output; |
| 4266 | error: |
| 4267 | rcu_read_unlock(); |
| 4268 | free(list); |
| 4269 | end: |
| 4270 | return ret; |
| 4271 | } |
| 4272 | |
| 4273 | /* |
| 4274 | * Check if we can regenerate the metadata for this session. |
| 4275 | * Only kernel, UST per-uid and non-live sessions are supported. |
| 4276 | * |
| 4277 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. |
| 4278 | */ |
| 4279 | static int check_regenerate_metadata_support(struct ltt_session *session) |
| 4280 | { |
| 4281 | int ret; |
| 4282 | |
| 4283 | LTTNG_ASSERT(session); |
| 4284 | |
| 4285 | if (session->live_timer != 0) { |
| 4286 | ret = LTTNG_ERR_LIVE_SESSION; |
| 4287 | goto end; |
| 4288 | } |
| 4289 | if (!session->active) { |
| 4290 | ret = LTTNG_ERR_SESSION_NOT_STARTED; |
| 4291 | goto end; |
| 4292 | } |
| 4293 | if (session->ust_session) { |
| 4294 | switch (session->ust_session->buffer_type) { |
| 4295 | case LTTNG_BUFFER_PER_UID: |
| 4296 | break; |
| 4297 | case LTTNG_BUFFER_PER_PID: |
| 4298 | ret = LTTNG_ERR_PER_PID_SESSION; |
| 4299 | goto end; |
| 4300 | default: |
| 4301 | abort(); |
| 4302 | ret = LTTNG_ERR_UNK; |
| 4303 | goto end; |
| 4304 | } |
| 4305 | } |
| 4306 | if (session->consumer->type == CONSUMER_DST_NET && |
| 4307 | session->consumer->relay_minor_version < 8) { |
| 4308 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 4309 | goto end; |
| 4310 | } |
| 4311 | ret = 0; |
| 4312 | |
| 4313 | end: |
| 4314 | return ret; |
| 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
| 4319 | * |
| 4320 | * Ask the consumer to truncate the existing metadata file(s) and |
| 4321 | * then regenerate the metadata. Live and per-pid sessions are not |
| 4322 | * supported and return an error. |
| 4323 | * |
| 4324 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4325 | */ |
| 4326 | int cmd_regenerate_metadata(struct ltt_session *session) |
| 4327 | { |
| 4328 | int ret; |
| 4329 | |
| 4330 | LTTNG_ASSERT(session); |
| 4331 | |
| 4332 | ret = check_regenerate_metadata_support(session); |
| 4333 | if (ret) { |
| 4334 | goto end; |
| 4335 | } |
| 4336 | |
| 4337 | if (session->kernel_session) { |
| 4338 | ret = kernctl_session_regenerate_metadata(session->kernel_session->fd); |
| 4339 | if (ret < 0) { |
| 4340 | ERR("Failed to regenerate the kernel metadata"); |
| 4341 | goto end; |
| 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | if (session->ust_session) { |
| 4346 | ret = trace_ust_regenerate_metadata(session->ust_session); |
| 4347 | if (ret < 0) { |
| 4348 | ERR("Failed to regenerate the UST metadata"); |
| 4349 | goto end; |
| 4350 | } |
| 4351 | } |
| 4352 | DBG("Cmd metadata regenerate for session %s", session->name); |
| 4353 | ret = LTTNG_OK; |
| 4354 | |
| 4355 | end: |
| 4356 | return ret; |
| 4357 | } |
| 4358 | |
| 4359 | /* |
| 4360 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. |
| 4361 | * |
| 4362 | * Ask the tracer to regenerate a new statedump. |
| 4363 | * |
| 4364 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 4365 | */ |
| 4366 | int cmd_regenerate_statedump(struct ltt_session *session) |
| 4367 | { |
| 4368 | int ret; |
| 4369 | |
| 4370 | LTTNG_ASSERT(session); |
| 4371 | |
| 4372 | if (!session->active) { |
| 4373 | ret = LTTNG_ERR_SESSION_NOT_STARTED; |
| 4374 | goto end; |
| 4375 | } |
| 4376 | |
| 4377 | if (session->kernel_session) { |
| 4378 | ret = kernctl_session_regenerate_statedump(session->kernel_session->fd); |
| 4379 | /* |
| 4380 | * Currently, the statedump in kernel can only fail if out |
| 4381 | * of memory. |
| 4382 | */ |
| 4383 | if (ret < 0) { |
| 4384 | if (ret == -ENOMEM) { |
| 4385 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; |
| 4386 | } else { |
| 4387 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; |
| 4388 | } |
| 4389 | ERR("Failed to regenerate the kernel statedump"); |
| 4390 | goto end; |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | if (session->ust_session) { |
| 4395 | ret = ust_app_regenerate_statedump_all(session->ust_session); |
| 4396 | /* |
| 4397 | * Currently, the statedump in UST always returns 0. |
| 4398 | */ |
| 4399 | if (ret < 0) { |
| 4400 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; |
| 4401 | ERR("Failed to regenerate the UST statedump"); |
| 4402 | goto end; |
| 4403 | } |
| 4404 | } |
| 4405 | DBG("Cmd regenerate statedump for session %s", session->name); |
| 4406 | ret = LTTNG_OK; |
| 4407 | |
| 4408 | end: |
| 4409 | return ret; |
| 4410 | } |
| 4411 | |
| 4412 | static enum lttng_error_code |
| 4413 | synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread, |
| 4414 | struct lttng_trigger *trigger, |
| 4415 | const struct lttng_credentials *cmd_creds) |
| 4416 | { |
| 4417 | enum lttng_error_code ret_code; |
| 4418 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
| 4419 | const char *trigger_name; |
| 4420 | uid_t trigger_owner; |
| 4421 | enum lttng_trigger_status trigger_status; |
| 4422 | const enum lttng_domain_type trigger_domain = |
| 4423 | lttng_trigger_get_underlying_domain_type_restriction(trigger); |
| 4424 | |
| 4425 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4426 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4427 | |
| 4428 | LTTNG_ASSERT(condition); |
| 4429 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 4430 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 4431 | |
| 4432 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4433 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4434 | |
| 4435 | session_lock_list(); |
| 4436 | switch (trigger_domain) { |
| 4437 | case LTTNG_DOMAIN_KERNEL: |
| 4438 | { |
| 4439 | ret_code = kernel_register_event_notifier(trigger, cmd_creds); |
| 4440 | if (ret_code != LTTNG_OK) { |
| 4441 | enum lttng_error_code notif_thread_unregister_ret; |
| 4442 | |
| 4443 | notif_thread_unregister_ret = |
| 4444 | notification_thread_command_unregister_trigger(notification_thread, |
| 4445 | trigger); |
| 4446 | |
| 4447 | if (notif_thread_unregister_ret != LTTNG_OK) { |
| 4448 | /* Return the original error code. */ |
| 4449 | ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4450 | trigger_name, |
| 4451 | (int) trigger_owner, |
| 4452 | ret_code); |
| 4453 | } |
| 4454 | } |
| 4455 | break; |
| 4456 | } |
| 4457 | case LTTNG_DOMAIN_UST: |
| 4458 | ust_app_global_update_all_event_notifier_rules(); |
| 4459 | break; |
| 4460 | case LTTNG_DOMAIN_JUL: |
| 4461 | case LTTNG_DOMAIN_LOG4J: |
| 4462 | case LTTNG_DOMAIN_PYTHON: |
| 4463 | { |
| 4464 | /* Agent domains. */ |
| 4465 | struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain); |
| 4466 | |
| 4467 | if (!agt) { |
| 4468 | agt = agent_create(trigger_domain); |
| 4469 | if (!agt) { |
| 4470 | ret_code = LTTNG_ERR_NOMEM; |
| 4471 | goto end_unlock_session_list; |
| 4472 | } |
| 4473 | |
| 4474 | agent_add(agt, the_trigger_agents_ht_by_domain); |
| 4475 | } |
| 4476 | |
| 4477 | ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt); |
| 4478 | if (ret_code != LTTNG_OK) { |
| 4479 | goto end_unlock_session_list; |
| 4480 | } |
| 4481 | |
| 4482 | break; |
| 4483 | } |
| 4484 | case LTTNG_DOMAIN_NONE: |
| 4485 | default: |
| 4486 | abort(); |
| 4487 | } |
| 4488 | |
| 4489 | ret_code = LTTNG_OK; |
| 4490 | end_unlock_session_list: |
| 4491 | session_unlock_list(); |
| 4492 | return ret_code; |
| 4493 | } |
| 4494 | |
| 4495 | enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds, |
| 4496 | struct lttng_trigger *trigger, |
| 4497 | bool is_trigger_anonymous, |
| 4498 | struct notification_thread_handle *notification_thread, |
| 4499 | struct lttng_trigger **return_trigger) |
| 4500 | { |
| 4501 | enum lttng_error_code ret_code; |
| 4502 | const char *trigger_name; |
| 4503 | uid_t trigger_owner; |
| 4504 | enum lttng_trigger_status trigger_status; |
| 4505 | |
| 4506 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4507 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4508 | |
| 4509 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4510 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4511 | |
| 4512 | DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4513 | trigger_name, |
| 4514 | (int) trigger_owner, |
| 4515 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4516 | |
| 4517 | /* |
| 4518 | * Validate the trigger credentials against the command credentials. |
| 4519 | * Only the root user can register a trigger with non-matching |
| 4520 | * credentials. |
| 4521 | */ |
| 4522 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) { |
| 4523 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4524 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4525 | trigger_name, |
| 4526 | (int) trigger_owner, |
| 4527 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4528 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4529 | goto end; |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | /* |
| 4534 | * The bytecode generation also serves as a validation step for the |
| 4535 | * bytecode expressions. |
| 4536 | */ |
| 4537 | ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds); |
| 4538 | if (ret_code != LTTNG_OK) { |
| 4539 | ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4540 | trigger_name, |
| 4541 | (int) trigger_owner, |
| 4542 | ret_code); |
| 4543 | goto end; |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * A reference to the trigger is acquired by the notification thread. |
| 4548 | * It is safe to return the same trigger to the caller since it the |
| 4549 | * other user holds a reference. |
| 4550 | * |
| 4551 | * The trigger is modified during the execution of the |
| 4552 | * "register trigger" command. However, by the time the command returns, |
| 4553 | * it is safe to use without any locking as its properties are |
| 4554 | * immutable. |
| 4555 | */ |
| 4556 | ret_code = notification_thread_command_register_trigger( |
| 4557 | notification_thread, trigger, is_trigger_anonymous); |
| 4558 | if (ret_code != LTTNG_OK) { |
| 4559 | DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4560 | trigger_name, |
| 4561 | (int) trigger_owner, |
| 4562 | ret_code); |
| 4563 | goto end; |
| 4564 | } |
| 4565 | |
| 4566 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4567 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4568 | |
| 4569 | /* |
| 4570 | * Synchronize tracers if the trigger adds an event notifier. |
| 4571 | */ |
| 4572 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
| 4573 | ret_code = synchronize_tracer_notifier_register( |
| 4574 | notification_thread, trigger, cmd_creds); |
| 4575 | if (ret_code != LTTNG_OK) { |
| 4576 | ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code)); |
| 4577 | goto end; |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | /* |
| 4582 | * Return an updated trigger to the client. |
| 4583 | * |
| 4584 | * Since a modified version of the same trigger is returned, acquire a |
| 4585 | * reference to the trigger so the caller doesn't have to care if those |
| 4586 | * are distinct instances or not. |
| 4587 | */ |
| 4588 | if (ret_code == LTTNG_OK) { |
| 4589 | lttng_trigger_get(trigger); |
| 4590 | *return_trigger = trigger; |
| 4591 | /* Ownership of trigger was transferred to caller. */ |
| 4592 | trigger = nullptr; |
| 4593 | } |
| 4594 | end: |
| 4595 | return ret_code; |
| 4596 | } |
| 4597 | |
| 4598 | static enum lttng_error_code |
| 4599 | synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger) |
| 4600 | { |
| 4601 | enum lttng_error_code ret_code; |
| 4602 | const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger); |
| 4603 | const enum lttng_domain_type trigger_domain = |
| 4604 | lttng_trigger_get_underlying_domain_type_restriction(trigger); |
| 4605 | |
| 4606 | LTTNG_ASSERT(condition); |
| 4607 | LTTNG_ASSERT(lttng_condition_get_type(condition) == |
| 4608 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
| 4609 | |
| 4610 | session_lock_list(); |
| 4611 | switch (trigger_domain) { |
| 4612 | case LTTNG_DOMAIN_KERNEL: |
| 4613 | ret_code = kernel_unregister_event_notifier(trigger); |
| 4614 | if (ret_code != LTTNG_OK) { |
| 4615 | goto end_unlock_session_list; |
| 4616 | } |
| 4617 | |
| 4618 | break; |
| 4619 | case LTTNG_DOMAIN_UST: |
| 4620 | ust_app_global_update_all_event_notifier_rules(); |
| 4621 | break; |
| 4622 | case LTTNG_DOMAIN_JUL: |
| 4623 | case LTTNG_DOMAIN_LOG4J: |
| 4624 | case LTTNG_DOMAIN_PYTHON: |
| 4625 | { |
| 4626 | /* Agent domains. */ |
| 4627 | struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain); |
| 4628 | |
| 4629 | /* |
| 4630 | * This trigger was never registered in the first place. Calling |
| 4631 | * this function under those circumstances is an internal error. |
| 4632 | */ |
| 4633 | LTTNG_ASSERT(agt); |
| 4634 | ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt); |
| 4635 | if (ret_code != LTTNG_OK) { |
| 4636 | goto end_unlock_session_list; |
| 4637 | } |
| 4638 | |
| 4639 | break; |
| 4640 | } |
| 4641 | case LTTNG_DOMAIN_NONE: |
| 4642 | default: |
| 4643 | abort(); |
| 4644 | } |
| 4645 | |
| 4646 | ret_code = LTTNG_OK; |
| 4647 | |
| 4648 | end_unlock_session_list: |
| 4649 | session_unlock_list(); |
| 4650 | return ret_code; |
| 4651 | } |
| 4652 | |
| 4653 | enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds, |
| 4654 | const struct lttng_trigger *trigger, |
| 4655 | struct notification_thread_handle *notification_thread) |
| 4656 | { |
| 4657 | enum lttng_error_code ret_code; |
| 4658 | const char *trigger_name; |
| 4659 | uid_t trigger_owner; |
| 4660 | enum lttng_trigger_status trigger_status; |
| 4661 | struct lttng_trigger *sessiond_trigger = nullptr; |
| 4662 | |
| 4663 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
| 4664 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4665 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
| 4666 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4667 | |
| 4668 | DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4669 | trigger_name, |
| 4670 | (int) trigger_owner, |
| 4671 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4672 | |
| 4673 | /* |
| 4674 | * Validate the trigger credentials against the command credentials. |
| 4675 | * Only the root user can unregister a trigger with non-matching |
| 4676 | * credentials. |
| 4677 | */ |
| 4678 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) { |
| 4679 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4680 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4681 | trigger_name, |
| 4682 | (int) trigger_owner, |
| 4683 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4684 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4685 | goto end; |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | /* Fetch the sessiond side trigger object. */ |
| 4690 | ret_code = notification_thread_command_get_trigger( |
| 4691 | notification_thread, trigger, &sessiond_trigger); |
| 4692 | if (ret_code != LTTNG_OK) { |
| 4693 | DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4694 | trigger_name, |
| 4695 | (int) trigger_owner, |
| 4696 | ret_code); |
| 4697 | goto end; |
| 4698 | } |
| 4699 | |
| 4700 | LTTNG_ASSERT(sessiond_trigger); |
| 4701 | |
| 4702 | /* |
| 4703 | * From this point on, no matter what, consider the trigger |
| 4704 | * unregistered. |
| 4705 | * |
| 4706 | * We set the unregistered state of the sessiond side trigger object in |
| 4707 | * the client thread since we want to minimize the possibility of the |
| 4708 | * notification thread being stalled due to a long execution of an |
| 4709 | * action that required the trigger lock. |
| 4710 | */ |
| 4711 | lttng_trigger_set_as_unregistered(sessiond_trigger); |
| 4712 | |
| 4713 | ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger); |
| 4714 | if (ret_code != LTTNG_OK) { |
| 4715 | DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
| 4716 | trigger_name, |
| 4717 | (int) trigger_owner, |
| 4718 | ret_code); |
| 4719 | goto end; |
| 4720 | } |
| 4721 | |
| 4722 | /* |
| 4723 | * Synchronize tracers if the trigger removes an event notifier. |
| 4724 | * Do this even if the trigger unregistration failed to at least stop |
| 4725 | * the tracers from producing notifications associated with this |
| 4726 | * event notifier. |
| 4727 | */ |
| 4728 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
| 4729 | ret_code = synchronize_tracer_notifier_unregister(trigger); |
| 4730 | if (ret_code != LTTNG_OK) { |
| 4731 | ERR("Error unregistering trigger to tracer."); |
| 4732 | goto end; |
| 4733 | } |
| 4734 | } |
| 4735 | |
| 4736 | end: |
| 4737 | lttng_trigger_put(sessiond_trigger); |
| 4738 | return ret_code; |
| 4739 | } |
| 4740 | |
| 4741 | enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx, |
| 4742 | struct notification_thread_handle *notification_thread, |
| 4743 | struct lttng_triggers **return_triggers) |
| 4744 | { |
| 4745 | int ret; |
| 4746 | enum lttng_error_code ret_code; |
| 4747 | struct lttng_triggers *triggers = nullptr; |
| 4748 | |
| 4749 | /* Get the set of triggers from the notification thread. */ |
| 4750 | ret_code = notification_thread_command_list_triggers( |
| 4751 | notification_thread, cmd_ctx->creds.uid, &triggers); |
| 4752 | if (ret_code != LTTNG_OK) { |
| 4753 | goto end; |
| 4754 | } |
| 4755 | |
| 4756 | ret = lttng_triggers_remove_hidden_triggers(triggers); |
| 4757 | if (ret) { |
| 4758 | ret_code = LTTNG_ERR_UNK; |
| 4759 | goto end; |
| 4760 | } |
| 4761 | |
| 4762 | *return_triggers = triggers; |
| 4763 | triggers = nullptr; |
| 4764 | ret_code = LTTNG_OK; |
| 4765 | end: |
| 4766 | lttng_triggers_destroy(triggers); |
| 4767 | return ret_code; |
| 4768 | } |
| 4769 | |
| 4770 | enum lttng_error_code |
| 4771 | cmd_execute_error_query(const struct lttng_credentials *cmd_creds, |
| 4772 | const struct lttng_error_query *query, |
| 4773 | struct lttng_error_query_results **_results, |
| 4774 | struct notification_thread_handle *notification_thread) |
| 4775 | { |
| 4776 | enum lttng_error_code ret_code; |
| 4777 | const struct lttng_trigger *query_target_trigger; |
| 4778 | const struct lttng_action *query_target_action = nullptr; |
| 4779 | struct lttng_trigger *matching_trigger = nullptr; |
| 4780 | const char *trigger_name; |
| 4781 | uid_t trigger_owner; |
| 4782 | enum lttng_trigger_status trigger_status; |
| 4783 | struct lttng_error_query_results *results = nullptr; |
| 4784 | |
| 4785 | switch (lttng_error_query_get_target_type(query)) { |
| 4786 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: |
| 4787 | query_target_trigger = lttng_error_query_trigger_borrow_target(query); |
| 4788 | break; |
| 4789 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
| 4790 | query_target_trigger = lttng_error_query_condition_borrow_target(query); |
| 4791 | break; |
| 4792 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
| 4793 | query_target_trigger = lttng_error_query_action_borrow_trigger_target(query); |
| 4794 | break; |
| 4795 | default: |
| 4796 | abort(); |
| 4797 | } |
| 4798 | |
| 4799 | LTTNG_ASSERT(query_target_trigger); |
| 4800 | |
| 4801 | ret_code = notification_thread_command_get_trigger( |
| 4802 | notification_thread, query_target_trigger, &matching_trigger); |
| 4803 | if (ret_code != LTTNG_OK) { |
| 4804 | goto end; |
| 4805 | } |
| 4806 | |
| 4807 | /* No longer needed. */ |
| 4808 | query_target_trigger = nullptr; |
| 4809 | |
| 4810 | if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) { |
| 4811 | /* Get the sessiond-side version of the target action. */ |
| 4812 | query_target_action = |
| 4813 | lttng_error_query_action_borrow_action_target(query, matching_trigger); |
| 4814 | } |
| 4815 | |
| 4816 | trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name); |
| 4817 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
| 4818 | trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner); |
| 4819 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
| 4820 | |
| 4821 | results = lttng_error_query_results_create(); |
| 4822 | if (!results) { |
| 4823 | ret_code = LTTNG_ERR_NOMEM; |
| 4824 | goto end; |
| 4825 | } |
| 4826 | |
| 4827 | DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4828 | trigger_name, |
| 4829 | (int) trigger_owner, |
| 4830 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4831 | |
| 4832 | /* |
| 4833 | * Validate the trigger credentials against the command credentials. |
| 4834 | * Only the root user can target a trigger with non-matching |
| 4835 | * credentials. |
| 4836 | */ |
| 4837 | if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger), |
| 4838 | cmd_creds)) { |
| 4839 | if (lttng_credentials_get_uid(cmd_creds) != 0) { |
| 4840 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
| 4841 | trigger_name, |
| 4842 | (int) trigger_owner, |
| 4843 | (int) lttng_credentials_get_uid(cmd_creds)); |
| 4844 | ret_code = LTTNG_ERR_INVALID_TRIGGER; |
| 4845 | goto end; |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | switch (lttng_error_query_get_target_type(query)) { |
| 4850 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: |
| 4851 | trigger_status = lttng_trigger_add_error_results(matching_trigger, results); |
| 4852 | |
| 4853 | switch (trigger_status) { |
| 4854 | case LTTNG_TRIGGER_STATUS_OK: |
| 4855 | break; |
| 4856 | default: |
| 4857 | ret_code = LTTNG_ERR_UNK; |
| 4858 | goto end; |
| 4859 | } |
| 4860 | |
| 4861 | break; |
| 4862 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
| 4863 | { |
| 4864 | trigger_status = |
| 4865 | lttng_trigger_condition_add_error_results(matching_trigger, results); |
| 4866 | |
| 4867 | switch (trigger_status) { |
| 4868 | case LTTNG_TRIGGER_STATUS_OK: |
| 4869 | break; |
| 4870 | default: |
| 4871 | ret_code = LTTNG_ERR_UNK; |
| 4872 | goto end; |
| 4873 | } |
| 4874 | |
| 4875 | break; |
| 4876 | } |
| 4877 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
| 4878 | { |
| 4879 | const enum lttng_action_status action_status = |
| 4880 | lttng_action_add_error_query_results(query_target_action, results); |
| 4881 | |
| 4882 | switch (action_status) { |
| 4883 | case LTTNG_ACTION_STATUS_OK: |
| 4884 | break; |
| 4885 | default: |
| 4886 | ret_code = LTTNG_ERR_UNK; |
| 4887 | goto end; |
| 4888 | } |
| 4889 | |
| 4890 | break; |
| 4891 | } |
| 4892 | default: |
| 4893 | abort(); |
| 4894 | break; |
| 4895 | } |
| 4896 | |
| 4897 | *_results = results; |
| 4898 | results = nullptr; |
| 4899 | ret_code = LTTNG_OK; |
| 4900 | end: |
| 4901 | lttng_trigger_put(matching_trigger); |
| 4902 | lttng_error_query_results_destroy(results); |
| 4903 | return ret_code; |
| 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * Send relayd sockets from snapshot output to consumer. Ignore request if the |
| 4908 | * snapshot output is *not* set with a remote destination. |
| 4909 | * |
| 4910 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
| 4911 | */ |
| 4912 | static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output, |
| 4913 | const struct ltt_session *session) |
| 4914 | { |
| 4915 | enum lttng_error_code status = LTTNG_OK; |
| 4916 | struct lttng_ht_iter iter; |
| 4917 | struct consumer_socket *socket; |
| 4918 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
| 4919 | const char *base_path; |
| 4920 | |
| 4921 | LTTNG_ASSERT(output); |
| 4922 | LTTNG_ASSERT(session); |
| 4923 | |
| 4924 | DBG2("Set relayd object from snapshot output"); |
| 4925 | |
| 4926 | if (session->current_trace_chunk) { |
| 4927 | enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id( |
| 4928 | session->current_trace_chunk, ¤t_chunk_id.value); |
| 4929 | |
| 4930 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 4931 | current_chunk_id.is_set = true; |
| 4932 | } else { |
| 4933 | ERR("Failed to get current trace chunk id"); |
| 4934 | status = LTTNG_ERR_UNK; |
| 4935 | goto error; |
| 4936 | } |
| 4937 | } |
| 4938 | |
| 4939 | /* Ignore if snapshot consumer output is not network. */ |
| 4940 | if (output->type != CONSUMER_DST_NET) { |
| 4941 | goto error; |
| 4942 | } |
| 4943 | |
| 4944 | /* |
| 4945 | * The snapshot record URI base path overrides the session |
| 4946 | * base path. |
| 4947 | */ |
| 4948 | if (output->dst.net.control.subdir[0] != '\0') { |
| 4949 | base_path = output->dst.net.control.subdir; |
| 4950 | } else { |
| 4951 | base_path = session->base_path; |
| 4952 | } |
| 4953 | |
| 4954 | /* |
| 4955 | * For each consumer socket, create and send the relayd object of the |
| 4956 | * snapshot output. |
| 4957 | */ |
| 4958 | rcu_read_lock(); |
| 4959 | cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) { |
| 4960 | pthread_mutex_lock(socket->lock); |
| 4961 | status = send_consumer_relayd_sockets( |
| 4962 | session->id, |
| 4963 | output, |
| 4964 | socket, |
| 4965 | session->name, |
| 4966 | session->hostname, |
| 4967 | base_path, |
| 4968 | session->live_timer, |
| 4969 | current_chunk_id.is_set ? ¤t_chunk_id.value : nullptr, |
| 4970 | session->creation_time, |
| 4971 | session->name_contains_creation_time); |
| 4972 | pthread_mutex_unlock(socket->lock); |
| 4973 | if (status != LTTNG_OK) { |
| 4974 | rcu_read_unlock(); |
| 4975 | goto error; |
| 4976 | } |
| 4977 | } |
| 4978 | rcu_read_unlock(); |
| 4979 | |
| 4980 | error: |
| 4981 | return status; |
| 4982 | } |
| 4983 | |
| 4984 | /* |
| 4985 | * Record a kernel snapshot. |
| 4986 | * |
| 4987 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
| 4988 | */ |
| 4989 | static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess, |
| 4990 | const struct consumer_output *output, |
| 4991 | const struct ltt_session *session, |
| 4992 | uint64_t nb_packets_per_stream) |
| 4993 | { |
| 4994 | enum lttng_error_code status; |
| 4995 | |
| 4996 | LTTNG_ASSERT(ksess); |
| 4997 | LTTNG_ASSERT(output); |
| 4998 | LTTNG_ASSERT(session); |
| 4999 | |
| 5000 | status = kernel_snapshot_record(ksess, output, nb_packets_per_stream); |
| 5001 | return status; |
| 5002 | } |
| 5003 | |
| 5004 | /* |
| 5005 | * Record a UST snapshot. |
| 5006 | * |
| 5007 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
| 5008 | */ |
| 5009 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
| 5010 | const struct consumer_output *output, |
| 5011 | const struct ltt_session *session, |
| 5012 | uint64_t nb_packets_per_stream) |
| 5013 | { |
| 5014 | enum lttng_error_code status; |
| 5015 | |
| 5016 | LTTNG_ASSERT(usess); |
| 5017 | LTTNG_ASSERT(output); |
| 5018 | LTTNG_ASSERT(session); |
| 5019 | |
| 5020 | status = ust_app_snapshot_record(usess, output, nb_packets_per_stream); |
| 5021 | return status; |
| 5022 | } |
| 5023 | |
| 5024 | static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session, |
| 5025 | uint64_t cur_nr_packets) |
| 5026 | { |
| 5027 | uint64_t tot_size = 0; |
| 5028 | |
| 5029 | if (session->kernel_session) { |
| 5030 | struct ltt_kernel_channel *chan; |
| 5031 | const struct ltt_kernel_session *ksess = session->kernel_session; |
| 5032 | |
| 5033 | cds_list_for_each_entry (chan, &ksess->channel_list.head, list) { |
| 5034 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
| 5035 | /* |
| 5036 | * Don't take channel into account if we |
| 5037 | * already grab all its packets. |
| 5038 | */ |
| 5039 | continue; |
| 5040 | } |
| 5041 | tot_size += chan->channel->attr.subbuf_size * chan->stream_count; |
| 5042 | } |
| 5043 | } |
| 5044 | |
| 5045 | if (session->ust_session) { |
| 5046 | const struct ltt_ust_session *usess = session->ust_session; |
| 5047 | |
| 5048 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets); |
| 5049 | } |
| 5050 | |
| 5051 | return tot_size; |
| 5052 | } |
| 5053 | |
| 5054 | /* |
| 5055 | * Calculate the number of packets we can grab from each stream that |
| 5056 | * fits within the overall snapshot max size. |
| 5057 | * |
| 5058 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is |
| 5059 | * the number of packets per stream. |
| 5060 | * |
| 5061 | * TODO: this approach is not perfect: we consider the worse case |
| 5062 | * (packet filling the sub-buffers) as an upper bound, but we could do |
| 5063 | * better if we do this calculation while we actually grab the packet |
| 5064 | * content: we would know how much padding we don't actually store into |
| 5065 | * the file. |
| 5066 | * |
| 5067 | * This algorithm is currently bounded by the number of packets per |
| 5068 | * stream. |
| 5069 | * |
| 5070 | * Since we call this algorithm before actually grabbing the data, it's |
| 5071 | * an approximation: for instance, applications could appear/disappear |
| 5072 | * in between this call and actually grabbing data. |
| 5073 | */ |
| 5074 | static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
| 5075 | uint64_t max_size) |
| 5076 | { |
| 5077 | int64_t size_left; |
| 5078 | uint64_t cur_nb_packets = 0; |
| 5079 | |
| 5080 | if (!max_size) { |
| 5081 | return 0; /* Infinite */ |
| 5082 | } |
| 5083 | |
| 5084 | size_left = max_size; |
| 5085 | for (;;) { |
| 5086 | uint64_t one_more_packet_tot_size; |
| 5087 | |
| 5088 | one_more_packet_tot_size = |
| 5089 | get_session_size_one_more_packet_per_stream(session, cur_nb_packets); |
| 5090 | if (!one_more_packet_tot_size) { |
| 5091 | /* We are already grabbing all packets. */ |
| 5092 | break; |
| 5093 | } |
| 5094 | size_left -= one_more_packet_tot_size; |
| 5095 | if (size_left < 0) { |
| 5096 | break; |
| 5097 | } |
| 5098 | cur_nb_packets++; |
| 5099 | } |
| 5100 | if (!cur_nb_packets && size_left != max_size) { |
| 5101 | /* Not enough room to grab one packet of each stream, error. */ |
| 5102 | return -1; |
| 5103 | } |
| 5104 | return cur_nb_packets; |
| 5105 | } |
| 5106 | |
| 5107 | static enum lttng_error_code snapshot_record(struct ltt_session *session, |
| 5108 | const struct snapshot_output *snapshot_output) |
| 5109 | { |
| 5110 | int64_t nb_packets_per_stream; |
| 5111 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
| 5112 | int ret; |
| 5113 | enum lttng_error_code ret_code = LTTNG_OK; |
| 5114 | struct lttng_trace_chunk *snapshot_trace_chunk; |
| 5115 | struct consumer_output *original_ust_consumer_output = nullptr; |
| 5116 | struct consumer_output *original_kernel_consumer_output = nullptr; |
| 5117 | struct consumer_output *snapshot_ust_consumer_output = nullptr; |
| 5118 | struct consumer_output *snapshot_kernel_consumer_output = nullptr; |
| 5119 | |
| 5120 | ret = snprintf(snapshot_chunk_name, |
| 5121 | sizeof(snapshot_chunk_name), |
| 5122 | "%s-%s-%" PRIu64, |
| 5123 | snapshot_output->name, |
| 5124 | snapshot_output->datetime, |
| 5125 | snapshot_output->nb_snapshot); |
| 5126 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
| 5127 | ERR("Failed to format snapshot name"); |
| 5128 | ret_code = LTTNG_ERR_INVALID; |
| 5129 | goto error; |
| 5130 | } |
| 5131 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", |
| 5132 | snapshot_output->name, |
| 5133 | session->name, |
| 5134 | snapshot_chunk_name); |
| 5135 | if (!session->kernel_session && !session->ust_session) { |
| 5136 | ERR("Failed to record snapshot as no channels exist"); |
| 5137 | ret_code = LTTNG_ERR_NO_CHANNEL; |
| 5138 | goto error; |
| 5139 | } |
| 5140 | |
| 5141 | if (session->kernel_session) { |
| 5142 | original_kernel_consumer_output = session->kernel_session->consumer; |
| 5143 | snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer); |
| 5144 | strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name); |
| 5145 | |
| 5146 | /* Copy the original domain subdir. */ |
| 5147 | strcpy(snapshot_kernel_consumer_output->domain_subdir, |
| 5148 | original_kernel_consumer_output->domain_subdir); |
| 5149 | |
| 5150 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, |
| 5151 | original_kernel_consumer_output); |
| 5152 | if (ret < 0) { |
| 5153 | ERR("Failed to copy consumer sockets from snapshot output configuration"); |
| 5154 | ret_code = LTTNG_ERR_NOMEM; |
| 5155 | goto error; |
| 5156 | } |
| 5157 | ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session); |
| 5158 | if (ret_code != LTTNG_OK) { |
| 5159 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); |
| 5160 | goto error; |
| 5161 | } |
| 5162 | session->kernel_session->consumer = snapshot_kernel_consumer_output; |
| 5163 | } |
| 5164 | if (session->ust_session) { |
| 5165 | original_ust_consumer_output = session->ust_session->consumer; |
| 5166 | snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer); |
| 5167 | strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name); |
| 5168 | |
| 5169 | /* Copy the original domain subdir. */ |
| 5170 | strcpy(snapshot_ust_consumer_output->domain_subdir, |
| 5171 | original_ust_consumer_output->domain_subdir); |
| 5172 | |
| 5173 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, |
| 5174 | original_ust_consumer_output); |
| 5175 | if (ret < 0) { |
| 5176 | ERR("Failed to copy consumer sockets from snapshot output configuration"); |
| 5177 | ret_code = LTTNG_ERR_NOMEM; |
| 5178 | goto error; |
| 5179 | } |
| 5180 | ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session); |
| 5181 | if (ret_code != LTTNG_OK) { |
| 5182 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); |
| 5183 | goto error; |
| 5184 | } |
| 5185 | session->ust_session->consumer = snapshot_ust_consumer_output; |
| 5186 | } |
| 5187 | |
| 5188 | snapshot_trace_chunk = session_create_new_trace_chunk( |
| 5189 | session, |
| 5190 | snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output, |
| 5191 | consumer_output_get_base_path(snapshot_output->consumer), |
| 5192 | snapshot_chunk_name); |
| 5193 | if (!snapshot_trace_chunk) { |
| 5194 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
| 5195 | session->name); |
| 5196 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; |
| 5197 | goto error; |
| 5198 | } |
| 5199 | LTTNG_ASSERT(!session->current_trace_chunk); |
| 5200 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr); |
| 5201 | lttng_trace_chunk_put(snapshot_trace_chunk); |
| 5202 | snapshot_trace_chunk = nullptr; |
| 5203 | if (ret) { |
| 5204 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
| 5205 | session->name); |
| 5206 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5207 | goto error; |
| 5208 | } |
| 5209 | |
| 5210 | nb_packets_per_stream = |
| 5211 | get_session_nb_packets_per_stream(session, snapshot_output->max_size); |
| 5212 | if (nb_packets_per_stream < 0) { |
| 5213 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
| 5214 | goto error_close_trace_chunk; |
| 5215 | } |
| 5216 | |
| 5217 | if (session->kernel_session) { |
| 5218 | ret_code = record_kernel_snapshot(session->kernel_session, |
| 5219 | snapshot_kernel_consumer_output, |
| 5220 | session, |
| 5221 | nb_packets_per_stream); |
| 5222 | if (ret_code != LTTNG_OK) { |
| 5223 | goto error_close_trace_chunk; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | if (session->ust_session) { |
| 5228 | ret_code = record_ust_snapshot(session->ust_session, |
| 5229 | snapshot_ust_consumer_output, |
| 5230 | session, |
| 5231 | nb_packets_per_stream); |
| 5232 | if (ret_code != LTTNG_OK) { |
| 5233 | goto error_close_trace_chunk; |
| 5234 | } |
| 5235 | } |
| 5236 | |
| 5237 | error_close_trace_chunk: |
| 5238 | if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) { |
| 5239 | ERR("Failed to release the current trace chunk of session \"%s\"", session->name); |
| 5240 | ret_code = LTTNG_ERR_UNK; |
| 5241 | } |
| 5242 | |
| 5243 | if (session_close_trace_chunk(session, |
| 5244 | snapshot_trace_chunk, |
| 5245 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, |
| 5246 | nullptr)) { |
| 5247 | /* |
| 5248 | * Don't goto end; make sure the chunk is closed for the session |
| 5249 | * to allow future snapshots. |
| 5250 | */ |
| 5251 | ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name); |
| 5252 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5253 | } |
| 5254 | |
| 5255 | lttng_trace_chunk_put(snapshot_trace_chunk); |
| 5256 | snapshot_trace_chunk = nullptr; |
| 5257 | error: |
| 5258 | if (original_ust_consumer_output) { |
| 5259 | session->ust_session->consumer = original_ust_consumer_output; |
| 5260 | } |
| 5261 | if (original_kernel_consumer_output) { |
| 5262 | session->kernel_session->consumer = original_kernel_consumer_output; |
| 5263 | } |
| 5264 | consumer_output_put(snapshot_ust_consumer_output); |
| 5265 | consumer_output_put(snapshot_kernel_consumer_output); |
| 5266 | return ret_code; |
| 5267 | } |
| 5268 | |
| 5269 | /* |
| 5270 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. |
| 5271 | * |
| 5272 | * The wait parameter is ignored so this call always wait for the snapshot to |
| 5273 | * complete before returning. |
| 5274 | * |
| 5275 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
| 5276 | */ |
| 5277 | int cmd_snapshot_record(struct ltt_session *session, |
| 5278 | const struct lttng_snapshot_output *output, |
| 5279 | int wait __attribute__((unused))) |
| 5280 | { |
| 5281 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5282 | int ret; |
| 5283 | unsigned int snapshot_success = 0; |
| 5284 | char datetime[16]; |
| 5285 | struct snapshot_output *tmp_output = nullptr; |
| 5286 | |
| 5287 | LTTNG_ASSERT(session); |
| 5288 | LTTNG_ASSERT(output); |
| 5289 | |
| 5290 | DBG("Cmd snapshot record for session %s", session->name); |
| 5291 | |
| 5292 | /* Get the datetime for the snapshot output directory. */ |
| 5293 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime)); |
| 5294 | if (!ret) { |
| 5295 | cmd_ret = LTTNG_ERR_INVALID; |
| 5296 | goto error; |
| 5297 | } |
| 5298 | |
| 5299 | /* |
| 5300 | * Permission denied to create an output if the session is not |
| 5301 | * set in no output mode. |
| 5302 | */ |
| 5303 | if (session->output_traces) { |
| 5304 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
| 5305 | goto error; |
| 5306 | } |
| 5307 | |
| 5308 | /* The session needs to be started at least once. */ |
| 5309 | if (!session->has_been_started) { |
| 5310 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
| 5311 | goto error; |
| 5312 | } |
| 5313 | |
| 5314 | /* Use temporary output for the session. */ |
| 5315 | if (*output->ctrl_url != '\0') { |
| 5316 | tmp_output = snapshot_output_alloc(); |
| 5317 | if (!tmp_output) { |
| 5318 | cmd_ret = LTTNG_ERR_NOMEM; |
| 5319 | goto error; |
| 5320 | } |
| 5321 | |
| 5322 | ret = snapshot_output_init(session, |
| 5323 | output->max_size, |
| 5324 | output->name, |
| 5325 | output->ctrl_url, |
| 5326 | output->data_url, |
| 5327 | session->consumer, |
| 5328 | tmp_output, |
| 5329 | nullptr); |
| 5330 | if (ret < 0) { |
| 5331 | if (ret == -ENOMEM) { |
| 5332 | cmd_ret = LTTNG_ERR_NOMEM; |
| 5333 | } else { |
| 5334 | cmd_ret = LTTNG_ERR_INVALID; |
| 5335 | } |
| 5336 | goto error; |
| 5337 | } |
| 5338 | /* Use the global session count for the temporary snapshot. */ |
| 5339 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
| 5340 | |
| 5341 | /* Use the global datetime */ |
| 5342 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
| 5343 | cmd_ret = snapshot_record(session, tmp_output); |
| 5344 | if (cmd_ret != LTTNG_OK) { |
| 5345 | goto error; |
| 5346 | } |
| 5347 | snapshot_success = 1; |
| 5348 | } else { |
| 5349 | struct snapshot_output *sout; |
| 5350 | struct lttng_ht_iter iter; |
| 5351 | |
| 5352 | rcu_read_lock(); |
| 5353 | cds_lfht_for_each_entry ( |
| 5354 | session->snapshot.output_ht->ht, &iter.iter, sout, node.node) { |
| 5355 | struct snapshot_output output_copy; |
| 5356 | |
| 5357 | /* |
| 5358 | * Make a local copy of the output and override output |
| 5359 | * parameters with those provided as part of the |
| 5360 | * command. |
| 5361 | */ |
| 5362 | memcpy(&output_copy, sout, sizeof(output_copy)); |
| 5363 | |
| 5364 | if (output->max_size != (uint64_t) -1ULL) { |
| 5365 | output_copy.max_size = output->max_size; |
| 5366 | } |
| 5367 | |
| 5368 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
| 5369 | memcpy(output_copy.datetime, datetime, sizeof(datetime)); |
| 5370 | |
| 5371 | /* Use temporary name. */ |
| 5372 | if (*output->name != '\0') { |
| 5373 | if (lttng_strncpy(output_copy.name, |
| 5374 | output->name, |
| 5375 | sizeof(output_copy.name))) { |
| 5376 | cmd_ret = LTTNG_ERR_INVALID; |
| 5377 | rcu_read_unlock(); |
| 5378 | goto error; |
| 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | cmd_ret = snapshot_record(session, &output_copy); |
| 5383 | if (cmd_ret != LTTNG_OK) { |
| 5384 | rcu_read_unlock(); |
| 5385 | goto error; |
| 5386 | } |
| 5387 | snapshot_success = 1; |
| 5388 | } |
| 5389 | rcu_read_unlock(); |
| 5390 | } |
| 5391 | |
| 5392 | if (snapshot_success) { |
| 5393 | session->snapshot.nb_snapshot++; |
| 5394 | } else { |
| 5395 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
| 5396 | } |
| 5397 | |
| 5398 | error: |
| 5399 | if (tmp_output) { |
| 5400 | snapshot_output_destroy(tmp_output); |
| 5401 | } |
| 5402 | return cmd_ret; |
| 5403 | } |
| 5404 | |
| 5405 | /* |
| 5406 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. |
| 5407 | */ |
| 5408 | int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path) |
| 5409 | { |
| 5410 | /* Safety net */ |
| 5411 | LTTNG_ASSERT(session); |
| 5412 | |
| 5413 | /* |
| 5414 | * Can only set shm path before session is started. |
| 5415 | */ |
| 5416 | if (session->has_been_started) { |
| 5417 | return LTTNG_ERR_SESSION_STARTED; |
| 5418 | } |
| 5419 | |
| 5420 | strncpy(session->shm_path, shm_path, sizeof(session->shm_path)); |
| 5421 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; |
| 5422 | |
| 5423 | return LTTNG_OK; |
| 5424 | } |
| 5425 | |
| 5426 | /* |
| 5427 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. |
| 5428 | * |
| 5429 | * Ask the consumer to rotate the session output directory. |
| 5430 | * The session lock must be held. |
| 5431 | * |
| 5432 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
| 5433 | */ |
| 5434 | int cmd_rotate_session(struct ltt_session *session, |
| 5435 | struct lttng_rotate_session_return *rotate_return, |
| 5436 | bool quiet_rotation, |
| 5437 | enum lttng_trace_chunk_command_type command) |
| 5438 | { |
| 5439 | int ret; |
| 5440 | uint64_t ongoing_rotation_chunk_id; |
| 5441 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5442 | struct lttng_trace_chunk *chunk_being_archived = nullptr; |
| 5443 | struct lttng_trace_chunk *new_trace_chunk = nullptr; |
| 5444 | enum lttng_trace_chunk_status chunk_status; |
| 5445 | bool failed_to_rotate = false; |
| 5446 | enum lttng_error_code rotation_fail_code = LTTNG_OK; |
| 5447 | |
| 5448 | LTTNG_ASSERT(session); |
| 5449 | |
| 5450 | if (!session->has_been_started) { |
| 5451 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
| 5452 | goto end; |
| 5453 | } |
| 5454 | |
| 5455 | /* |
| 5456 | * Explicit rotation is not supported for live sessions. |
| 5457 | * However, live sessions can perform a quiet rotation on |
| 5458 | * destroy. |
| 5459 | * Rotation is not supported for snapshot traces (no output). |
| 5460 | */ |
| 5461 | if ((!quiet_rotation && session->live_timer) || !session->output_traces) { |
| 5462 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
| 5463 | goto end; |
| 5464 | } |
| 5465 | |
| 5466 | /* Unsupported feature in lttng-relayd before 2.11. */ |
| 5467 | if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET && |
| 5468 | (session->consumer->relay_major_version == 2 && |
| 5469 | session->consumer->relay_minor_version < 11)) { |
| 5470 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
| 5471 | goto end; |
| 5472 | } |
| 5473 | |
| 5474 | /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */ |
| 5475 | if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) { |
| 5476 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL; |
| 5477 | goto end; |
| 5478 | } |
| 5479 | |
| 5480 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 5481 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
| 5482 | session->name); |
| 5483 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
| 5484 | goto end; |
| 5485 | } |
| 5486 | |
| 5487 | /* |
| 5488 | * After a stop, we only allow one rotation to occur, the other ones are |
| 5489 | * useless until a new start. |
| 5490 | */ |
| 5491 | if (session->rotated_after_last_stop) { |
| 5492 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", |
| 5493 | session->name); |
| 5494 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
| 5495 | goto end; |
| 5496 | } |
| 5497 | |
| 5498 | /* |
| 5499 | * After a stop followed by a clear, disallow following rotations a they would |
| 5500 | * generate empty chunks. |
| 5501 | */ |
| 5502 | if (session->cleared_after_last_stop) { |
| 5503 | DBG("Session \"%s\" was already cleared after stop, refusing rotation", |
| 5504 | session->name); |
| 5505 | cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR; |
| 5506 | goto end; |
| 5507 | } |
| 5508 | |
| 5509 | if (session->active) { |
| 5510 | new_trace_chunk = |
| 5511 | session_create_new_trace_chunk(session, nullptr, nullptr, nullptr); |
| 5512 | if (!new_trace_chunk) { |
| 5513 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; |
| 5514 | goto error; |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | /* |
| 5519 | * The current trace chunk becomes the chunk being archived. |
| 5520 | * |
| 5521 | * After this point, "chunk_being_archived" must absolutely |
| 5522 | * be closed on the consumer(s), otherwise it will never be |
| 5523 | * cleaned-up, which will result in a leak. |
| 5524 | */ |
| 5525 | ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived); |
| 5526 | if (ret) { |
| 5527 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5528 | goto error; |
| 5529 | } |
| 5530 | |
| 5531 | if (session->kernel_session) { |
| 5532 | cmd_ret = kernel_rotate_session(session); |
| 5533 | if (cmd_ret != LTTNG_OK) { |
| 5534 | failed_to_rotate = true; |
| 5535 | rotation_fail_code = cmd_ret; |
| 5536 | } |
| 5537 | } |
| 5538 | if (session->ust_session) { |
| 5539 | cmd_ret = ust_app_rotate_session(session); |
| 5540 | if (cmd_ret != LTTNG_OK) { |
| 5541 | failed_to_rotate = true; |
| 5542 | rotation_fail_code = cmd_ret; |
| 5543 | } |
| 5544 | } |
| 5545 | |
| 5546 | if (!session->active) { |
| 5547 | session->rotated_after_last_stop = true; |
| 5548 | } |
| 5549 | |
| 5550 | if (!chunk_being_archived) { |
| 5551 | DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check", |
| 5552 | session->name); |
| 5553 | if (failed_to_rotate) { |
| 5554 | cmd_ret = rotation_fail_code; |
| 5555 | goto error; |
| 5556 | } |
| 5557 | cmd_ret = LTTNG_OK; |
| 5558 | goto end; |
| 5559 | } |
| 5560 | |
| 5561 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
| 5562 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id); |
| 5563 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 5564 | |
| 5565 | ret = session_close_trace_chunk( |
| 5566 | session, chunk_being_archived, command, session->last_chunk_path); |
| 5567 | if (ret) { |
| 5568 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
| 5569 | goto error; |
| 5570 | } |
| 5571 | |
| 5572 | if (failed_to_rotate) { |
| 5573 | cmd_ret = rotation_fail_code; |
| 5574 | goto error; |
| 5575 | } |
| 5576 | |
| 5577 | session->quiet_rotation = quiet_rotation; |
| 5578 | ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER); |
| 5579 | if (ret) { |
| 5580 | cmd_ret = LTTNG_ERR_UNK; |
| 5581 | goto error; |
| 5582 | } |
| 5583 | |
| 5584 | if (rotate_return) { |
| 5585 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
| 5586 | } |
| 5587 | |
| 5588 | session->chunk_being_archived = chunk_being_archived; |
| 5589 | chunk_being_archived = nullptr; |
| 5590 | if (!quiet_rotation) { |
| 5591 | ret = notification_thread_command_session_rotation_ongoing( |
| 5592 | the_notification_thread_handle, session->id, ongoing_rotation_chunk_id); |
| 5593 | if (ret != LTTNG_OK) { |
| 5594 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", |
| 5595 | session->name); |
| 5596 | cmd_ret = (lttng_error_code) ret; |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
| 5601 | session->name, |
| 5602 | ongoing_rotation_chunk_id); |
| 5603 | end: |
| 5604 | lttng_trace_chunk_put(new_trace_chunk); |
| 5605 | lttng_trace_chunk_put(chunk_being_archived); |
| 5606 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
| 5607 | return ret; |
| 5608 | error: |
| 5609 | if (session_reset_rotation_state(session, LTTNG_ROTATION_STATE_ERROR)) { |
| 5610 | ERR("Failed to reset rotation state of session \"%s\"", session->name); |
| 5611 | } |
| 5612 | goto end; |
| 5613 | } |
| 5614 | |
| 5615 | /* |
| 5616 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
| 5617 | * |
| 5618 | * Check if the session has finished its rotation. |
| 5619 | * |
| 5620 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
| 5621 | */ |
| 5622 | int cmd_rotate_get_info(struct ltt_session *session, |
| 5623 | struct lttng_rotation_get_info_return *info_return, |
| 5624 | uint64_t rotation_id) |
| 5625 | { |
| 5626 | enum lttng_error_code cmd_ret = LTTNG_OK; |
| 5627 | enum lttng_rotation_state rotation_state; |
| 5628 | |
| 5629 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, |
| 5630 | session->name, |
| 5631 | session->most_recent_chunk_id.value); |
| 5632 | |
| 5633 | if (session->chunk_being_archived) { |
| 5634 | enum lttng_trace_chunk_status chunk_status; |
| 5635 | uint64_t chunk_id; |
| 5636 | |
| 5637 | chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id); |
| 5638 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 5639 | |
| 5640 | rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING : |
| 5641 | LTTNG_ROTATION_STATE_EXPIRED; |
| 5642 | } else { |
| 5643 | if (session->last_archived_chunk_id.is_set && |
| 5644 | rotation_id != session->last_archived_chunk_id.value) { |
| 5645 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; |
| 5646 | } else { |
| 5647 | rotation_state = session->rotation_state; |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | switch (rotation_state) { |
| 5652 | case LTTNG_ROTATION_STATE_NO_ROTATION: |
| 5653 | DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"", |
| 5654 | session->name); |
| 5655 | goto end; |
| 5656 | case LTTNG_ROTATION_STATE_EXPIRED: |
| 5657 | DBG("Reporting that the rotation state of rotation id %" PRIu64 |
| 5658 | " of session \"%s\" has expired", |
| 5659 | rotation_id, |
| 5660 | session->name); |
| 5661 | break; |
| 5662 | case LTTNG_ROTATION_STATE_ONGOING: |
| 5663 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
| 5664 | rotation_id, |
| 5665 | session->name); |
| 5666 | break; |
| 5667 | case LTTNG_ROTATION_STATE_COMPLETED: |
| 5668 | { |
| 5669 | int fmt_ret; |
| 5670 | char *chunk_path; |
| 5671 | char *current_tracing_path_reply; |
| 5672 | size_t current_tracing_path_reply_len; |
| 5673 | |
| 5674 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
| 5675 | rotation_id, |
| 5676 | session->name); |
| 5677 | |
| 5678 | switch (session_get_consumer_destination_type(session)) { |
| 5679 | case CONSUMER_DST_LOCAL: |
| 5680 | current_tracing_path_reply = info_return->location.local.absolute_path; |
| 5681 | current_tracing_path_reply_len = |
| 5682 | sizeof(info_return->location.local.absolute_path); |
| 5683 | info_return->location_type = |
| 5684 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
| 5685 | fmt_ret = asprintf(&chunk_path, |
| 5686 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", |
| 5687 | session_get_base_path(session), |
| 5688 | session->last_archived_chunk_name); |
| 5689 | if (fmt_ret == -1) { |
| 5690 | PERROR("Failed to format the path of the last archived trace chunk"); |
| 5691 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5692 | cmd_ret = LTTNG_ERR_UNK; |
| 5693 | goto end; |
| 5694 | } |
| 5695 | break; |
| 5696 | case CONSUMER_DST_NET: |
| 5697 | { |
| 5698 | uint16_t ctrl_port, data_port; |
| 5699 | |
| 5700 | current_tracing_path_reply = info_return->location.relay.relative_path; |
| 5701 | current_tracing_path_reply_len = |
| 5702 | sizeof(info_return->location.relay.relative_path); |
| 5703 | /* Currently the only supported relay protocol. */ |
| 5704 | info_return->location.relay.protocol = |
| 5705 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
| 5706 | |
| 5707 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
| 5708 | session_get_net_consumer_hostname(session), |
| 5709 | sizeof(info_return->location.relay.host)); |
| 5710 | if (fmt_ret) { |
| 5711 | ERR("Failed to copy host name to rotate_get_info reply"); |
| 5712 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5713 | cmd_ret = LTTNG_ERR_SET_URL; |
| 5714 | goto end; |
| 5715 | } |
| 5716 | |
| 5717 | session_get_net_consumer_ports(session, &ctrl_port, &data_port); |
| 5718 | info_return->location.relay.ports.control = ctrl_port; |
| 5719 | info_return->location.relay.ports.data = data_port; |
| 5720 | info_return->location_type = |
| 5721 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
| 5722 | chunk_path = strdup(session->last_chunk_path); |
| 5723 | if (!chunk_path) { |
| 5724 | ERR("Failed to allocate the path of the last archived trace chunk"); |
| 5725 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5726 | cmd_ret = LTTNG_ERR_UNK; |
| 5727 | goto end; |
| 5728 | } |
| 5729 | break; |
| 5730 | } |
| 5731 | default: |
| 5732 | abort(); |
| 5733 | } |
| 5734 | |
| 5735 | fmt_ret = lttng_strncpy( |
| 5736 | current_tracing_path_reply, chunk_path, current_tracing_path_reply_len); |
| 5737 | free(chunk_path); |
| 5738 | if (fmt_ret) { |
| 5739 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); |
| 5740 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
| 5741 | cmd_ret = LTTNG_ERR_UNK; |
| 5742 | goto end; |
| 5743 | } |
| 5744 | |
| 5745 | break; |
| 5746 | } |
| 5747 | case LTTNG_ROTATION_STATE_ERROR: |
| 5748 | DBG("Reporting that an error occurred during rotation %" PRIu64 |
| 5749 | " of session \"%s\"", |
| 5750 | rotation_id, |
| 5751 | session->name); |
| 5752 | break; |
| 5753 | default: |
| 5754 | abort(); |
| 5755 | } |
| 5756 | |
| 5757 | cmd_ret = LTTNG_OK; |
| 5758 | end: |
| 5759 | info_return->status = (int32_t) rotation_state; |
| 5760 | return cmd_ret; |
| 5761 | } |
| 5762 | |
| 5763 | /* |
| 5764 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. |
| 5765 | * |
| 5766 | * Configure the automatic rotation parameters. |
| 5767 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
| 5768 | * 'activate' to false means deactivate the rotation schedule and validate that |
| 5769 | * 'new_value' has the same value as the currently active value. |
| 5770 | * |
| 5771 | * Return LTTNG_OK on success or else a positive LTTNG_ERR code. |
| 5772 | */ |
| 5773 | int cmd_rotation_set_schedule(struct ltt_session *session, |
| 5774 | bool activate, |
| 5775 | enum lttng_rotation_schedule_type schedule_type, |
| 5776 | uint64_t new_value, |
| 5777 | struct notification_thread_handle *notification_thread_handle) |
| 5778 | { |
| 5779 | int ret; |
| 5780 | uint64_t *parameter_value; |
| 5781 | |
| 5782 | LTTNG_ASSERT(session); |
| 5783 | |
| 5784 | DBG("Cmd rotate set schedule session %s", session->name); |
| 5785 | |
| 5786 | if (session->live_timer || !session->output_traces) { |
| 5787 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
| 5788 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
| 5789 | goto end; |
| 5790 | } |
| 5791 | |
| 5792 | switch (schedule_type) { |
| 5793 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 5794 | parameter_value = &session->rotate_size; |
| 5795 | break; |
| 5796 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 5797 | parameter_value = &session->rotate_timer_period; |
| 5798 | if (new_value >= UINT_MAX) { |
| 5799 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 |
| 5800 | " > %u (UINT_MAX)", |
| 5801 | new_value, |
| 5802 | UINT_MAX); |
| 5803 | ret = LTTNG_ERR_INVALID; |
| 5804 | goto end; |
| 5805 | } |
| 5806 | break; |
| 5807 | default: |
| 5808 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); |
| 5809 | ret = LTTNG_ERR_INVALID; |
| 5810 | goto end; |
| 5811 | } |
| 5812 | |
| 5813 | /* Improper use of the API. */ |
| 5814 | if (new_value == -1ULL) { |
| 5815 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); |
| 5816 | ret = LTTNG_ERR_INVALID; |
| 5817 | goto end; |
| 5818 | } |
| 5819 | |
| 5820 | /* |
| 5821 | * As indicated in struct ltt_session's comments, a value of == 0 means |
| 5822 | * this schedule rotation type is not in use. |
| 5823 | * |
| 5824 | * Reject the command if we were asked to activate a schedule that was |
| 5825 | * already active. |
| 5826 | */ |
| 5827 | if (activate && *parameter_value != 0) { |
| 5828 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); |
| 5829 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; |
| 5830 | goto end; |
| 5831 | } |
| 5832 | |
| 5833 | /* |
| 5834 | * Reject the command if we were asked to deactivate a schedule that was |
| 5835 | * not active. |
| 5836 | */ |
| 5837 | if (!activate && *parameter_value == 0) { |
| 5838 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); |
| 5839 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; |
| 5840 | goto end; |
| 5841 | } |
| 5842 | |
| 5843 | /* |
| 5844 | * Reject the command if we were asked to deactivate a schedule that |
| 5845 | * doesn't exist. |
| 5846 | */ |
| 5847 | if (!activate && *parameter_value != new_value) { |
| 5848 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); |
| 5849 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; |
| 5850 | goto end; |
| 5851 | } |
| 5852 | |
| 5853 | *parameter_value = activate ? new_value : 0; |
| 5854 | |
| 5855 | switch (schedule_type) { |
| 5856 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: |
| 5857 | if (activate && session->active) { |
| 5858 | /* |
| 5859 | * Only start the timer if the session is active, |
| 5860 | * otherwise it will be started when the session starts. |
| 5861 | */ |
| 5862 | ret = timer_session_rotation_schedule_timer_start(session, new_value); |
| 5863 | if (ret) { |
| 5864 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
| 5865 | ret = LTTNG_ERR_UNK; |
| 5866 | goto end; |
| 5867 | } |
| 5868 | } else { |
| 5869 | ret = timer_session_rotation_schedule_timer_stop(session); |
| 5870 | if (ret) { |
| 5871 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); |
| 5872 | ret = LTTNG_ERR_UNK; |
| 5873 | goto end; |
| 5874 | } |
| 5875 | } |
| 5876 | break; |
| 5877 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: |
| 5878 | if (activate) { |
| 5879 | ret = subscribe_session_consumed_size_rotation( |
| 5880 | session, new_value, notification_thread_handle); |
| 5881 | if (ret) { |
| 5882 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
| 5883 | ret = LTTNG_ERR_UNK; |
| 5884 | goto end; |
| 5885 | } |
| 5886 | } else { |
| 5887 | ret = unsubscribe_session_consumed_size_rotation( |
| 5888 | session, notification_thread_handle); |
| 5889 | if (ret) { |
| 5890 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
| 5891 | ret = LTTNG_ERR_UNK; |
| 5892 | goto end; |
| 5893 | } |
| 5894 | } |
| 5895 | break; |
| 5896 | default: |
| 5897 | /* Would have been caught before. */ |
| 5898 | abort(); |
| 5899 | } |
| 5900 | |
| 5901 | ret = LTTNG_OK; |
| 5902 | |
| 5903 | goto end; |
| 5904 | |
| 5905 | end: |
| 5906 | return ret; |
| 5907 | } |
| 5908 | |
| 5909 | /* Wait for a given path to be removed before continuing. */ |
| 5910 | static enum lttng_error_code wait_on_path(void *path_data) |
| 5911 | { |
| 5912 | const char *shm_path = (const char *) path_data; |
| 5913 | |
| 5914 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", |
| 5915 | shm_path); |
| 5916 | while (true) { |
| 5917 | int ret; |
| 5918 | struct stat st; |
| 5919 | |
| 5920 | ret = stat(shm_path, &st); |
| 5921 | if (ret) { |
| 5922 | if (errno != ENOENT) { |
| 5923 | PERROR("stat() returned an error while checking for the existence of the shm path"); |
| 5924 | } else { |
| 5925 | DBG("shm path no longer exists, completing the destruction of session"); |
| 5926 | } |
| 5927 | break; |
| 5928 | } else { |
| 5929 | if (!S_ISDIR(st.st_mode)) { |
| 5930 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", |
| 5931 | shm_path); |
| 5932 | break; |
| 5933 | } |
| 5934 | } |
| 5935 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); |
| 5936 | } |
| 5937 | return LTTNG_OK; |
| 5938 | } |
| 5939 | |
| 5940 | /* |
| 5941 | * Returns a pointer to a handler to run on completion of a command. |
| 5942 | * Returns NULL if no handler has to be run for the last command executed. |
| 5943 | */ |
| 5944 | const struct cmd_completion_handler *cmd_pop_completion_handler() |
| 5945 | { |
| 5946 | struct cmd_completion_handler *handler = current_completion_handler; |
| 5947 | |
| 5948 | current_completion_handler = nullptr; |
| 5949 | return handler; |
| 5950 | } |
| 5951 | |
| 5952 | /* |
| 5953 | * Init command subsystem. |
| 5954 | */ |
| 5955 | void cmd_init() |
| 5956 | { |
| 5957 | /* |
| 5958 | * Set network sequence index to 1 for streams to match a relayd |
| 5959 | * socket on the consumer side. |
| 5960 | */ |
| 5961 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
| 5962 | relayd_net_seq_idx = 1; |
| 5963 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); |
| 5964 | |
| 5965 | DBG("Command subsystem initialized"); |
| 5966 | } |