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