| 1 | /* |
| 2 | * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <string.h> |
| 22 | #include <urcu/uatomic.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <common/defaults.h> |
| 26 | #include <common/error.h> |
| 27 | #include <common/config/config.h> |
| 28 | #include <common/utils.h> |
| 29 | #include <common/runas.h> |
| 30 | #include <lttng/save-internal.h> |
| 31 | |
| 32 | #include "save.h" |
| 33 | #include "session.h" |
| 34 | #include "trace-ust.h" |
| 35 | |
| 36 | static |
| 37 | int save_kernel_channel_attributes(struct config_writer *writer, |
| 38 | struct lttng_channel_attr *attr) |
| 39 | { |
| 40 | int ret; |
| 41 | |
| 42 | ret = config_writer_write_element_string(writer, |
| 43 | config_element_overwrite_mode, |
| 44 | attr->overwrite ? config_overwrite_mode_overwrite : |
| 45 | config_overwrite_mode_discard); |
| 46 | if (ret) { |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | ret = config_writer_write_element_unsigned_int(writer, |
| 51 | config_element_subbuf_size, attr->subbuf_size); |
| 52 | if (ret) { |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | ret = config_writer_write_element_unsigned_int(writer, |
| 57 | config_element_num_subbuf, |
| 58 | attr->num_subbuf); |
| 59 | if (ret) { |
| 60 | goto end; |
| 61 | } |
| 62 | |
| 63 | ret = config_writer_write_element_unsigned_int(writer, |
| 64 | config_element_switch_timer_interval, |
| 65 | attr->switch_timer_interval); |
| 66 | if (ret) { |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | ret = config_writer_write_element_unsigned_int(writer, |
| 71 | config_element_read_timer_interval, |
| 72 | attr->read_timer_interval); |
| 73 | if (ret) { |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | ret = config_writer_write_element_string(writer, |
| 78 | config_element_output_type, |
| 79 | attr->output == LTTNG_EVENT_SPLICE ? |
| 80 | config_output_type_splice : config_output_type_mmap); |
| 81 | if (ret) { |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | ret = config_writer_write_element_unsigned_int(writer, |
| 86 | config_element_tracefile_size, attr->tracefile_size); |
| 87 | if (ret) { |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | ret = config_writer_write_element_unsigned_int(writer, |
| 92 | config_element_tracefile_count, |
| 93 | attr->tracefile_count); |
| 94 | if (ret) { |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | ret = config_writer_write_element_unsigned_int(writer, |
| 99 | config_element_live_timer_interval, |
| 100 | attr->live_timer_interval); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | end: |
| 105 | return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0; |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | int save_ust_channel_attributes(struct config_writer *writer, |
| 110 | struct lttng_ust_channel_attr *attr) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | ret = config_writer_write_element_string(writer, |
| 115 | config_element_overwrite_mode, |
| 116 | attr->overwrite ? config_overwrite_mode_overwrite : |
| 117 | config_overwrite_mode_discard); |
| 118 | if (ret) { |
| 119 | goto end; |
| 120 | } |
| 121 | |
| 122 | ret = config_writer_write_element_unsigned_int(writer, |
| 123 | config_element_subbuf_size, attr->subbuf_size); |
| 124 | if (ret) { |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | ret = config_writer_write_element_unsigned_int(writer, |
| 129 | config_element_num_subbuf, |
| 130 | attr->num_subbuf); |
| 131 | if (ret) { |
| 132 | goto end; |
| 133 | } |
| 134 | |
| 135 | ret = config_writer_write_element_unsigned_int(writer, |
| 136 | config_element_switch_timer_interval, |
| 137 | attr->switch_timer_interval); |
| 138 | if (ret) { |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | ret = config_writer_write_element_unsigned_int(writer, |
| 143 | config_element_read_timer_interval, |
| 144 | attr->read_timer_interval); |
| 145 | if (ret) { |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | ret = config_writer_write_element_string(writer, |
| 150 | config_element_output_type, |
| 151 | attr->output == LTTNG_UST_MMAP ? |
| 152 | config_output_type_mmap : config_output_type_splice); |
| 153 | if (ret) { |
| 154 | goto end; |
| 155 | } |
| 156 | end: |
| 157 | return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0; |
| 158 | } |
| 159 | |
| 160 | static |
| 161 | const char *get_kernel_instrumentation_string( |
| 162 | enum lttng_kernel_instrumentation instrumentation) |
| 163 | { |
| 164 | const char *instrumentation_string; |
| 165 | |
| 166 | switch (instrumentation) { |
| 167 | case LTTNG_KERNEL_ALL: |
| 168 | instrumentation_string = config_event_type_all; |
| 169 | break; |
| 170 | case LTTNG_KERNEL_TRACEPOINT: |
| 171 | instrumentation_string = config_event_type_tracepoint; |
| 172 | break; |
| 173 | case LTTNG_KERNEL_KPROBE: |
| 174 | instrumentation_string = config_event_type_kprobe; |
| 175 | break; |
| 176 | case LTTNG_KERNEL_FUNCTION: |
| 177 | instrumentation_string = config_event_type_function; |
| 178 | break; |
| 179 | case LTTNG_KERNEL_KRETPROBE: |
| 180 | instrumentation_string = config_event_type_kretprobe; |
| 181 | break; |
| 182 | case LTTNG_KERNEL_NOOP: |
| 183 | instrumentation_string = config_event_type_noop; |
| 184 | break; |
| 185 | case LTTNG_KERNEL_SYSCALL: |
| 186 | instrumentation_string = config_event_type_syscall; |
| 187 | break; |
| 188 | default: |
| 189 | instrumentation_string = NULL; |
| 190 | } |
| 191 | |
| 192 | return instrumentation_string; |
| 193 | } |
| 194 | |
| 195 | static |
| 196 | const char *get_kernel_context_type_string( |
| 197 | enum lttng_kernel_context_type context_type) |
| 198 | { |
| 199 | const char *context_type_string; |
| 200 | |
| 201 | switch (context_type) { |
| 202 | case LTTNG_KERNEL_CONTEXT_PID: |
| 203 | context_type_string = config_event_context_pid; |
| 204 | break; |
| 205 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
| 206 | context_type_string = config_event_context_procname; |
| 207 | break; |
| 208 | case LTTNG_KERNEL_CONTEXT_PRIO: |
| 209 | context_type_string = config_event_context_prio; |
| 210 | break; |
| 211 | case LTTNG_KERNEL_CONTEXT_NICE: |
| 212 | context_type_string = config_event_context_nice; |
| 213 | break; |
| 214 | case LTTNG_KERNEL_CONTEXT_VPID: |
| 215 | context_type_string = config_event_context_vpid; |
| 216 | break; |
| 217 | case LTTNG_KERNEL_CONTEXT_TID: |
| 218 | context_type_string = config_event_context_tid; |
| 219 | break; |
| 220 | case LTTNG_KERNEL_CONTEXT_VTID: |
| 221 | context_type_string = config_event_context_vtid; |
| 222 | break; |
| 223 | case LTTNG_KERNEL_CONTEXT_PPID: |
| 224 | context_type_string = config_event_context_ppid; |
| 225 | break; |
| 226 | case LTTNG_KERNEL_CONTEXT_VPPID: |
| 227 | context_type_string = config_event_context_vppid; |
| 228 | break; |
| 229 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
| 230 | context_type_string = config_event_context_hostname; |
| 231 | break; |
| 232 | default: |
| 233 | context_type_string = NULL; |
| 234 | } |
| 235 | |
| 236 | return context_type_string; |
| 237 | } |
| 238 | |
| 239 | static |
| 240 | const char *get_ust_context_type_string( |
| 241 | enum lttng_ust_context_type context_type) |
| 242 | { |
| 243 | const char *context_type_string; |
| 244 | |
| 245 | switch (context_type) { |
| 246 | case LTTNG_UST_CONTEXT_PROCNAME: |
| 247 | context_type_string = config_event_context_procname; |
| 248 | break; |
| 249 | case LTTNG_UST_CONTEXT_VPID: |
| 250 | context_type_string = config_event_context_vpid; |
| 251 | break; |
| 252 | case LTTNG_UST_CONTEXT_VTID: |
| 253 | context_type_string = config_event_context_vtid; |
| 254 | break; |
| 255 | case LTTNG_UST_CONTEXT_IP: |
| 256 | context_type_string = config_event_context_ip; |
| 257 | break; |
| 258 | case LTTNG_UST_CONTEXT_PTHREAD_ID: |
| 259 | context_type_string = config_event_context_pthread_id; |
| 260 | break; |
| 261 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: |
| 262 | context_type_string = config_event_context_perf_thread_counter; |
| 263 | break; |
| 264 | default: |
| 265 | context_type_string = NULL; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return context_type_string; |
| 270 | } |
| 271 | |
| 272 | static |
| 273 | const char *get_buffer_type_string( |
| 274 | enum lttng_buffer_type buffer_type) |
| 275 | { |
| 276 | const char *buffer_type_string; |
| 277 | |
| 278 | switch (buffer_type) { |
| 279 | case LTTNG_BUFFER_PER_PID: |
| 280 | buffer_type_string = config_buffer_type_per_pid; |
| 281 | break; |
| 282 | case LTTNG_BUFFER_PER_UID: |
| 283 | buffer_type_string = config_buffer_type_per_uid; |
| 284 | break; |
| 285 | case LTTNG_BUFFER_GLOBAL: |
| 286 | buffer_type_string = config_buffer_type_global; |
| 287 | break; |
| 288 | default: |
| 289 | buffer_type_string = NULL; |
| 290 | } |
| 291 | |
| 292 | return buffer_type_string; |
| 293 | } |
| 294 | |
| 295 | static |
| 296 | const char *get_loglevel_type_string( |
| 297 | enum lttng_ust_loglevel_type loglevel_type) |
| 298 | { |
| 299 | const char *loglevel_type_string; |
| 300 | |
| 301 | switch (loglevel_type) { |
| 302 | case LTTNG_UST_LOGLEVEL_ALL: |
| 303 | loglevel_type_string = config_loglevel_type_all; |
| 304 | break; |
| 305 | case LTTNG_UST_LOGLEVEL_RANGE: |
| 306 | loglevel_type_string = config_loglevel_type_range; |
| 307 | break; |
| 308 | case LTTNG_UST_LOGLEVEL_SINGLE: |
| 309 | loglevel_type_string = config_loglevel_type_single; |
| 310 | break; |
| 311 | default: |
| 312 | loglevel_type_string = NULL; |
| 313 | } |
| 314 | |
| 315 | return loglevel_type_string; |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | int save_kernel_event(struct config_writer *writer, |
| 320 | struct ltt_kernel_event *event) |
| 321 | { |
| 322 | int ret; |
| 323 | const char *instrumentation_type; |
| 324 | |
| 325 | ret = config_writer_open_element(writer, config_element_event); |
| 326 | if (ret) { |
| 327 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 328 | goto end; |
| 329 | } |
| 330 | |
| 331 | if (event->event->name[0]) { |
| 332 | ret = config_writer_write_element_string(writer, |
| 333 | config_element_name, event->event->name); |
| 334 | if (ret) { |
| 335 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 336 | goto end; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | ret = config_writer_write_element_bool(writer, config_element_enabled, |
| 341 | event->enabled); |
| 342 | if (ret) { |
| 343 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 344 | goto end; |
| 345 | } |
| 346 | |
| 347 | instrumentation_type = get_kernel_instrumentation_string( |
| 348 | event->event->instrumentation); |
| 349 | if (!instrumentation_type) { |
| 350 | ret = LTTNG_ERR_INVALID; |
| 351 | goto end; |
| 352 | } |
| 353 | |
| 354 | ret = config_writer_write_element_string(writer, config_element_type, |
| 355 | instrumentation_type); |
| 356 | if (ret) { |
| 357 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 358 | goto end; |
| 359 | } |
| 360 | |
| 361 | if (event->event->instrumentation == LTTNG_KERNEL_FUNCTION || |
| 362 | event->event->instrumentation == LTTNG_KERNEL_KPROBE || |
| 363 | event->event->instrumentation == LTTNG_KERNEL_KRETPROBE) { |
| 364 | |
| 365 | ret = config_writer_open_element(writer, |
| 366 | config_element_attributes); |
| 367 | if (ret) { |
| 368 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 369 | goto end; |
| 370 | } |
| 371 | |
| 372 | switch (event->event->instrumentation) { |
| 373 | case LTTNG_KERNEL_FUNCTION: |
| 374 | ret = config_writer_open_element(writer, |
| 375 | config_element_function_attributes); |
| 376 | if (ret) { |
| 377 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 378 | goto end; |
| 379 | } |
| 380 | |
| 381 | ret = config_writer_write_element_string(writer, |
| 382 | config_element_name, |
| 383 | event->event->u.ftrace.symbol_name); |
| 384 | if (ret) { |
| 385 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 386 | goto end; |
| 387 | } |
| 388 | |
| 389 | /* /function attributes */ |
| 390 | ret = config_writer_close_element(writer); |
| 391 | if (ret) { |
| 392 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 393 | goto end; |
| 394 | } |
| 395 | break; |
| 396 | case LTTNG_KERNEL_KPROBE: |
| 397 | case LTTNG_KERNEL_KRETPROBE: |
| 398 | { |
| 399 | const char *symbol_name; |
| 400 | uint64_t addr; |
| 401 | uint64_t offset; |
| 402 | |
| 403 | if (event->event->instrumentation == |
| 404 | LTTNG_KERNEL_KPROBE) { |
| 405 | /* |
| 406 | * Comments in lttng-kernel.h mention that |
| 407 | * either addr or symbol_name are set, not both. |
| 408 | */ |
| 409 | addr = event->event->u.kprobe.addr; |
| 410 | offset = event->event->u.kprobe.offset; |
| 411 | symbol_name = addr ? NULL : |
| 412 | event->event->u.kprobe.symbol_name; |
| 413 | } else { |
| 414 | symbol_name = |
| 415 | event->event->u.kretprobe.symbol_name; |
| 416 | addr = event->event->u.kretprobe.addr; |
| 417 | offset = event->event->u.kretprobe.offset; |
| 418 | } |
| 419 | |
| 420 | ret = config_writer_open_element(writer, |
| 421 | config_element_probe_attributes); |
| 422 | if (ret) { |
| 423 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 424 | goto end; |
| 425 | } |
| 426 | |
| 427 | if (symbol_name) { |
| 428 | ret = config_writer_write_element_string(writer, |
| 429 | config_element_symbol_name, |
| 430 | symbol_name); |
| 431 | if (ret) { |
| 432 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 433 | goto end; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (addr) { |
| 438 | ret = config_writer_write_element_unsigned_int( |
| 439 | writer, config_element_address, addr); |
| 440 | if (ret) { |
| 441 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 442 | goto end; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (offset) { |
| 447 | ret = config_writer_write_element_unsigned_int( |
| 448 | writer, config_element_offset, offset); |
| 449 | if (ret) { |
| 450 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 451 | goto end; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | ret = config_writer_close_element(writer); |
| 456 | if (ret) { |
| 457 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 458 | goto end; |
| 459 | } |
| 460 | break; |
| 461 | } |
| 462 | default: |
| 463 | ERR("Unsupported kernel instrumentation type."); |
| 464 | ret = LTTNG_ERR_INVALID; |
| 465 | goto end; |
| 466 | } |
| 467 | |
| 468 | /* /attributes */ |
| 469 | ret = config_writer_close_element(writer); |
| 470 | if (ret) { |
| 471 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 472 | goto end; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* /event */ |
| 477 | ret = config_writer_close_element(writer); |
| 478 | if (ret) { |
| 479 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 480 | goto end; |
| 481 | } |
| 482 | end: |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static |
| 487 | int save_kernel_events(struct config_writer *writer, |
| 488 | struct ltt_kernel_event_list *event_list) |
| 489 | { |
| 490 | int ret; |
| 491 | struct ltt_kernel_event *event; |
| 492 | |
| 493 | ret = config_writer_open_element(writer, config_element_events); |
| 494 | if (ret) { |
| 495 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 496 | goto end; |
| 497 | } |
| 498 | |
| 499 | cds_list_for_each_entry(event, &event_list->head, list) { |
| 500 | ret = save_kernel_event(writer, event); |
| 501 | if (ret) { |
| 502 | goto end; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* /events */ |
| 507 | ret = config_writer_close_element(writer); |
| 508 | if (ret) { |
| 509 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 510 | goto end; |
| 511 | } |
| 512 | end: |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | static |
| 517 | int save_ust_event(struct config_writer *writer, |
| 518 | struct ltt_ust_event *event) |
| 519 | { |
| 520 | int ret; |
| 521 | const char *loglevel_type_string; |
| 522 | |
| 523 | ret = config_writer_open_element(writer, config_element_event); |
| 524 | if (ret) { |
| 525 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 526 | goto end; |
| 527 | } |
| 528 | |
| 529 | if (event->attr.name[0]) { |
| 530 | ret = config_writer_write_element_string(writer, |
| 531 | config_element_name, event->attr.name); |
| 532 | if (ret) { |
| 533 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 534 | goto end; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | ret = config_writer_write_element_bool(writer, config_element_enabled, |
| 539 | event->enabled); |
| 540 | if (ret) { |
| 541 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 542 | goto end; |
| 543 | } |
| 544 | |
| 545 | if (event->attr.instrumentation != LTTNG_UST_TRACEPOINT) { |
| 546 | ERR("Unsupported UST instrumentation type."); |
| 547 | ret = LTTNG_ERR_INVALID; |
| 548 | goto end; |
| 549 | } |
| 550 | ret = config_writer_write_element_string(writer, config_element_type, |
| 551 | config_event_type_tracepoint); |
| 552 | if (ret) { |
| 553 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | loglevel_type_string = get_loglevel_type_string( |
| 558 | event->attr.loglevel_type); |
| 559 | if (!loglevel_type_string) { |
| 560 | ERR("Unsupported UST loglevel type."); |
| 561 | ret = LTTNG_ERR_INVALID; |
| 562 | goto end; |
| 563 | } |
| 564 | |
| 565 | ret = config_writer_write_element_string(writer, |
| 566 | config_element_loglevel_type, loglevel_type_string); |
| 567 | if (ret) { |
| 568 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 569 | goto end; |
| 570 | } |
| 571 | |
| 572 | ret = config_writer_write_element_signed_int(writer, |
| 573 | config_element_loglevel, event->attr.loglevel); |
| 574 | if (ret) { |
| 575 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 576 | goto end; |
| 577 | } |
| 578 | |
| 579 | if (event->filter_expression) { |
| 580 | ret = config_writer_write_element_string(writer, |
| 581 | config_element_filter, event->filter_expression); |
| 582 | if (ret) { |
| 583 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 584 | goto end; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (event->exclusion && event->exclusion->count) { |
| 589 | uint32_t i; |
| 590 | |
| 591 | ret = config_writer_open_element(writer, |
| 592 | config_element_exclusions); |
| 593 | if (ret) { |
| 594 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | for (i = 0; i < event->exclusion->count; i++) { |
| 599 | ret = config_writer_write_element_string(writer, |
| 600 | config_element_exclusion, |
| 601 | &event->exclusion->names[0][i]); |
| 602 | if (ret) { |
| 603 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 604 | goto end; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* /exclusions */ |
| 609 | ret = config_writer_close_element(writer); |
| 610 | if (ret) { |
| 611 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 612 | goto end; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* /event */ |
| 617 | ret = config_writer_close_element(writer); |
| 618 | if (ret) { |
| 619 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 620 | goto end; |
| 621 | } |
| 622 | end: |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static |
| 627 | int save_ust_events(struct config_writer *writer, |
| 628 | struct lttng_ht *events) |
| 629 | { |
| 630 | int ret; |
| 631 | struct ltt_ust_event *event; |
| 632 | struct lttng_ht_node_str *node; |
| 633 | struct lttng_ht_iter iter; |
| 634 | |
| 635 | ret = config_writer_open_element(writer, config_element_events); |
| 636 | if (ret) { |
| 637 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 638 | goto end; |
| 639 | } |
| 640 | |
| 641 | rcu_read_lock(); |
| 642 | cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) { |
| 643 | event = caa_container_of(node, struct ltt_ust_event, node); |
| 644 | |
| 645 | ret = save_ust_event(writer, event); |
| 646 | if (ret) { |
| 647 | rcu_read_unlock(); |
| 648 | goto end; |
| 649 | } |
| 650 | } |
| 651 | rcu_read_unlock(); |
| 652 | |
| 653 | /* /events */ |
| 654 | ret = config_writer_close_element(writer); |
| 655 | if (ret) { |
| 656 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 657 | goto end; |
| 658 | } |
| 659 | end: |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | static |
| 664 | int save_kernel_context(struct config_writer *writer, |
| 665 | struct lttng_kernel_context *ctx) |
| 666 | { |
| 667 | int ret = 0; |
| 668 | |
| 669 | if (!ctx) { |
| 670 | goto end; |
| 671 | } |
| 672 | |
| 673 | ret = config_writer_open_element(writer, config_element_context); |
| 674 | if (ret) { |
| 675 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 676 | goto end; |
| 677 | } |
| 678 | |
| 679 | if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) { |
| 680 | ret = config_writer_open_element(writer, config_element_perf); |
| 681 | if (ret) { |
| 682 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 683 | goto end; |
| 684 | } |
| 685 | |
| 686 | ret = config_writer_write_element_unsigned_int(writer, |
| 687 | config_element_type, ctx->u.perf_counter.type); |
| 688 | if (ret) { |
| 689 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 690 | goto end; |
| 691 | } |
| 692 | |
| 693 | ret = config_writer_write_element_unsigned_int(writer, |
| 694 | config_element_config, ctx->u.perf_counter.config); |
| 695 | if (ret) { |
| 696 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 697 | goto end; |
| 698 | } |
| 699 | |
| 700 | ret = config_writer_write_element_string(writer, |
| 701 | config_element_name, ctx->u.perf_counter.name); |
| 702 | if (ret) { |
| 703 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | /* /perf */ |
| 708 | ret = config_writer_close_element(writer); |
| 709 | if (ret) { |
| 710 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 711 | goto end; |
| 712 | } |
| 713 | } else { |
| 714 | const char *context_type_string = |
| 715 | get_kernel_context_type_string(ctx->ctx); |
| 716 | |
| 717 | if (!context_type_string) { |
| 718 | ERR("Unsupported kernel context type."); |
| 719 | ret = LTTNG_ERR_INVALID; |
| 720 | goto end; |
| 721 | } |
| 722 | |
| 723 | ret = config_writer_write_element_string(writer, |
| 724 | config_element_type, context_type_string); |
| 725 | if (ret) { |
| 726 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 727 | goto end; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* /context */ |
| 732 | ret = config_writer_close_element(writer); |
| 733 | if (ret) { |
| 734 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 735 | goto end; |
| 736 | } |
| 737 | |
| 738 | end: |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | static |
| 743 | int save_kernel_contexts(struct config_writer *writer, |
| 744 | struct ltt_kernel_channel *kchan) |
| 745 | { |
| 746 | int ret; |
| 747 | struct ltt_kernel_context *ctx; |
| 748 | |
| 749 | ret = config_writer_open_element(writer, config_element_contexts); |
| 750 | if (ret) { |
| 751 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 752 | goto end; |
| 753 | } |
| 754 | |
| 755 | cds_list_for_each_entry(ctx, &kchan->ctx_list, list) { |
| 756 | ret = save_kernel_context(writer, &ctx->ctx); |
| 757 | if (ret) { |
| 758 | goto end; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /* /contexts */ |
| 763 | ret = config_writer_close_element(writer); |
| 764 | if (ret) { |
| 765 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 766 | goto end; |
| 767 | } |
| 768 | end: |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | static |
| 773 | int save_ust_context(struct config_writer *writer, |
| 774 | struct cds_list_head *ctx_list) |
| 775 | { |
| 776 | int ret; |
| 777 | struct ltt_ust_context *ctx; |
| 778 | |
| 779 | assert(writer); |
| 780 | assert(ctx_list); |
| 781 | |
| 782 | ret = config_writer_open_element(writer, config_element_contexts); |
| 783 | if (ret) { |
| 784 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 785 | goto end; |
| 786 | } |
| 787 | |
| 788 | cds_list_for_each_entry(ctx, ctx_list, list) { |
| 789 | const char *context_type_string; |
| 790 | |
| 791 | context_type_string = get_ust_context_type_string(ctx->ctx.ctx); |
| 792 | if (!context_type_string) { |
| 793 | ERR("Unsupported UST context type.") |
| 794 | ret = LTTNG_ERR_INVALID; |
| 795 | goto end; |
| 796 | } |
| 797 | |
| 798 | ret = config_writer_open_element(writer, |
| 799 | config_element_context); |
| 800 | if (ret) { |
| 801 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 802 | goto end; |
| 803 | } |
| 804 | |
| 805 | ret = config_writer_write_element_string(writer, |
| 806 | config_element_type, context_type_string); |
| 807 | if (ret) { |
| 808 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 809 | goto end; |
| 810 | } |
| 811 | |
| 812 | /* /context */ |
| 813 | ret = config_writer_close_element(writer); |
| 814 | if (ret) { |
| 815 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 816 | goto end; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* /contexts */ |
| 821 | ret = config_writer_close_element(writer); |
| 822 | if (ret) { |
| 823 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 824 | goto end; |
| 825 | } |
| 826 | end: |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | static |
| 831 | int save_kernel_channel(struct config_writer *writer, |
| 832 | struct ltt_kernel_channel *kchan) |
| 833 | { |
| 834 | int ret; |
| 835 | |
| 836 | assert(writer); |
| 837 | assert(kchan); |
| 838 | |
| 839 | ret = config_writer_open_element(writer, config_element_channel); |
| 840 | if (ret) { |
| 841 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 842 | goto end; |
| 843 | } |
| 844 | |
| 845 | ret = config_writer_write_element_string(writer, config_element_name, |
| 846 | kchan->channel->name); |
| 847 | if (ret) { |
| 848 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 849 | goto end; |
| 850 | } |
| 851 | |
| 852 | ret = config_writer_write_element_bool(writer, config_element_enabled, |
| 853 | kchan->channel->enabled); |
| 854 | if (ret) { |
| 855 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 856 | goto end; |
| 857 | } |
| 858 | |
| 859 | ret = save_kernel_channel_attributes(writer, &kchan->channel->attr); |
| 860 | if (ret) { |
| 861 | goto end; |
| 862 | } |
| 863 | |
| 864 | ret = save_kernel_events(writer, &kchan->events_list); |
| 865 | if (ret) { |
| 866 | goto end; |
| 867 | } |
| 868 | |
| 869 | ret = save_kernel_contexts(writer, kchan); |
| 870 | if (ret) { |
| 871 | goto end; |
| 872 | } |
| 873 | |
| 874 | /* /channel */ |
| 875 | ret = config_writer_close_element(writer); |
| 876 | if (ret) { |
| 877 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 878 | goto end; |
| 879 | } |
| 880 | end: |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | static |
| 885 | int save_ust_channel(struct config_writer *writer, |
| 886 | struct ltt_ust_channel *ust_chan, |
| 887 | struct ltt_ust_session *session) |
| 888 | { |
| 889 | int ret; |
| 890 | |
| 891 | assert(writer); |
| 892 | assert(ust_chan); |
| 893 | assert(session); |
| 894 | |
| 895 | ret = config_writer_open_element(writer, config_element_channel); |
| 896 | if (ret) { |
| 897 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 898 | goto end; |
| 899 | } |
| 900 | |
| 901 | ret = config_writer_write_element_string(writer, config_element_name, |
| 902 | ust_chan->name); |
| 903 | if (ret) { |
| 904 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 905 | goto end; |
| 906 | } |
| 907 | |
| 908 | ret = config_writer_write_element_bool(writer, config_element_enabled, |
| 909 | ust_chan->enabled); |
| 910 | if (ret) { |
| 911 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 912 | goto end; |
| 913 | } |
| 914 | |
| 915 | ret = save_ust_channel_attributes(writer, &ust_chan->attr); |
| 916 | if (ret) { |
| 917 | goto end; |
| 918 | } |
| 919 | |
| 920 | ret = config_writer_write_element_unsigned_int(writer, |
| 921 | config_element_tracefile_size, ust_chan->tracefile_size); |
| 922 | if (ret) { |
| 923 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 924 | goto end; |
| 925 | } |
| 926 | |
| 927 | ret = config_writer_write_element_unsigned_int(writer, |
| 928 | config_element_tracefile_count, ust_chan->tracefile_count); |
| 929 | if (ret) { |
| 930 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 931 | goto end; |
| 932 | } |
| 933 | |
| 934 | ret = config_writer_write_element_unsigned_int(writer, |
| 935 | config_element_live_timer_interval, |
| 936 | session->live_timer_interval); |
| 937 | if (ret) { |
| 938 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 939 | goto end; |
| 940 | } |
| 941 | |
| 942 | ret = save_ust_events(writer, ust_chan->events); |
| 943 | if (ret) { |
| 944 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 945 | goto end; |
| 946 | } |
| 947 | |
| 948 | ret = save_ust_context(writer, &ust_chan->ctx_list); |
| 949 | if (ret) { |
| 950 | goto end; |
| 951 | } |
| 952 | |
| 953 | /* /channel */ |
| 954 | ret = config_writer_close_element(writer); |
| 955 | if (ret) { |
| 956 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 957 | goto end; |
| 958 | } |
| 959 | end: |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | static |
| 964 | int save_kernel_session(struct config_writer *writer, |
| 965 | struct ltt_session *session) |
| 966 | { |
| 967 | int ret; |
| 968 | struct ltt_kernel_channel *kchan; |
| 969 | |
| 970 | assert(writer); |
| 971 | assert(session); |
| 972 | |
| 973 | ret = config_writer_write_element_string(writer, config_element_type, |
| 974 | config_domain_type_kernel); |
| 975 | if (ret) { |
| 976 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 977 | goto end; |
| 978 | } |
| 979 | |
| 980 | ret = config_writer_write_element_string(writer, |
| 981 | config_element_buffer_type, config_buffer_type_global); |
| 982 | if (ret) { |
| 983 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 984 | goto end; |
| 985 | } |
| 986 | |
| 987 | ret = config_writer_open_element(writer, |
| 988 | config_element_channels); |
| 989 | if (ret) { |
| 990 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 991 | goto end; |
| 992 | } |
| 993 | |
| 994 | cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, |
| 995 | list) { |
| 996 | ret = save_kernel_channel(writer, kchan); |
| 997 | if (ret) { |
| 998 | goto end; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /* /channels */ |
| 1003 | ret = config_writer_close_element(writer); |
| 1004 | if (ret) { |
| 1005 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1006 | goto end; |
| 1007 | } |
| 1008 | end: |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | static |
| 1013 | int save_ust_session(struct config_writer *writer, |
| 1014 | struct ltt_session *session, int save_jul) |
| 1015 | { |
| 1016 | int ret; |
| 1017 | struct ltt_ust_channel *ust_chan; |
| 1018 | const char *buffer_type_string; |
| 1019 | struct lttng_ht_node_str *node; |
| 1020 | struct lttng_ht_iter iter; |
| 1021 | |
| 1022 | assert(writer); |
| 1023 | assert(session); |
| 1024 | |
| 1025 | ret = config_writer_write_element_string(writer, config_element_type, |
| 1026 | save_jul ? config_domain_type_jul : config_domain_type_ust); |
| 1027 | if (ret) { |
| 1028 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1029 | goto end; |
| 1030 | } |
| 1031 | |
| 1032 | buffer_type_string = get_buffer_type_string( |
| 1033 | session->ust_session->buffer_type); |
| 1034 | if (!buffer_type_string) { |
| 1035 | ERR("Unsupported buffer type."); |
| 1036 | ret = LTTNG_ERR_INVALID; |
| 1037 | goto end; |
| 1038 | } |
| 1039 | |
| 1040 | ret = config_writer_write_element_string(writer, |
| 1041 | config_element_buffer_type, buffer_type_string); |
| 1042 | if (ret) { |
| 1043 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1044 | goto end; |
| 1045 | } |
| 1046 | |
| 1047 | ret = config_writer_open_element(writer, config_element_channels); |
| 1048 | if (ret) { |
| 1049 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1050 | goto end; |
| 1051 | } |
| 1052 | |
| 1053 | rcu_read_lock(); |
| 1054 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
| 1055 | &iter.iter, node, node) { |
| 1056 | int jul_channel; |
| 1057 | |
| 1058 | ust_chan = caa_container_of(node, struct ltt_ust_channel, node); |
| 1059 | jul_channel = !strcmp(DEFAULT_JUL_CHANNEL_NAME, ust_chan->name); |
| 1060 | if (!(save_jul ^ jul_channel)) { |
| 1061 | ret = save_ust_channel(writer, ust_chan, session->ust_session); |
| 1062 | if (ret) { |
| 1063 | rcu_read_unlock(); |
| 1064 | goto end; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | rcu_read_unlock(); |
| 1069 | |
| 1070 | /* /channels */ |
| 1071 | ret = config_writer_close_element(writer); |
| 1072 | if (ret) { |
| 1073 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1074 | goto end; |
| 1075 | } |
| 1076 | end: |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | static |
| 1081 | int save_domains(struct config_writer *writer, struct ltt_session *session) |
| 1082 | { |
| 1083 | int ret = 0; |
| 1084 | |
| 1085 | assert(writer); |
| 1086 | assert(session); |
| 1087 | |
| 1088 | if (!session->kernel_session && !session->ust_session) { |
| 1089 | goto end; |
| 1090 | } |
| 1091 | |
| 1092 | ret = config_writer_open_element(writer, config_element_domains); |
| 1093 | if (ret) { |
| 1094 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1095 | goto end; |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | if (session->kernel_session) { |
| 1100 | ret = config_writer_open_element(writer, |
| 1101 | config_element_domain); |
| 1102 | if (ret) { |
| 1103 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1104 | goto end; |
| 1105 | } |
| 1106 | |
| 1107 | ret = save_kernel_session(writer, session); |
| 1108 | if (ret) { |
| 1109 | goto end; |
| 1110 | } |
| 1111 | |
| 1112 | /* /domain */ |
| 1113 | ret = config_writer_close_element(writer); |
| 1114 | if (ret) { |
| 1115 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1116 | goto end; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (session->ust_session) { |
| 1121 | ret = config_writer_open_element(writer, |
| 1122 | config_element_domain); |
| 1123 | if (ret) { |
| 1124 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1125 | goto end; |
| 1126 | } |
| 1127 | |
| 1128 | ret = save_ust_session(writer, session, 0); |
| 1129 | if (ret) { |
| 1130 | goto end; |
| 1131 | } |
| 1132 | |
| 1133 | /* /domain */ |
| 1134 | ret = config_writer_close_element(writer); |
| 1135 | if (ret) { |
| 1136 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1137 | goto end; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | if (session->ust_session && |
| 1142 | session->ust_session->domain_jul.being_used) { |
| 1143 | ret = config_writer_open_element(writer, |
| 1144 | config_element_domain); |
| 1145 | if (ret) { |
| 1146 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1147 | goto end; |
| 1148 | } |
| 1149 | |
| 1150 | ret = save_ust_session(writer, session, 1); |
| 1151 | if (ret) { |
| 1152 | goto end; |
| 1153 | } |
| 1154 | |
| 1155 | /* /domain */ |
| 1156 | ret = config_writer_close_element(writer); |
| 1157 | if (ret) { |
| 1158 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1159 | goto end; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /* /domains */ |
| 1164 | ret = config_writer_close_element(writer); |
| 1165 | if (ret) { |
| 1166 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1167 | goto end; |
| 1168 | } |
| 1169 | end: |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
| 1173 | static |
| 1174 | int save_consumer_output(struct config_writer *writer, |
| 1175 | struct consumer_output *output) |
| 1176 | { |
| 1177 | int ret; |
| 1178 | |
| 1179 | assert(writer); |
| 1180 | assert(output); |
| 1181 | |
| 1182 | ret = config_writer_open_element(writer, config_element_consumer_output); |
| 1183 | if (ret) { |
| 1184 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1185 | goto end; |
| 1186 | } |
| 1187 | |
| 1188 | ret = config_writer_write_element_bool(writer, config_element_enabled, |
| 1189 | output->enabled); |
| 1190 | if (ret) { |
| 1191 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1192 | goto end; |
| 1193 | } |
| 1194 | |
| 1195 | ret = config_writer_open_element(writer, config_element_destination); |
| 1196 | if (ret) { |
| 1197 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1198 | goto end; |
| 1199 | } |
| 1200 | |
| 1201 | switch (output->type) { |
| 1202 | case CONSUMER_DST_LOCAL: |
| 1203 | ret = config_writer_write_element_string(writer, |
| 1204 | config_element_path, output->dst.trace_path); |
| 1205 | if (ret) { |
| 1206 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1207 | goto end; |
| 1208 | } |
| 1209 | break; |
| 1210 | case CONSUMER_DST_NET: |
| 1211 | { |
| 1212 | char *uri; |
| 1213 | |
| 1214 | uri = zmalloc(PATH_MAX); |
| 1215 | if (!uri) { |
| 1216 | ret = LTTNG_ERR_NOMEM; |
| 1217 | goto end; |
| 1218 | } |
| 1219 | |
| 1220 | ret = config_writer_open_element(writer, config_element_net_output); |
| 1221 | if (ret) { |
| 1222 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1223 | goto end_net_output; |
| 1224 | } |
| 1225 | |
| 1226 | if (output->dst.net.control_isset && |
| 1227 | output->dst.net.data_isset) { |
| 1228 | ret = uri_to_str_url(&output->dst.net.control, uri, PATH_MAX); |
| 1229 | if (ret < 0) { |
| 1230 | ret = LTTNG_ERR_INVALID; |
| 1231 | goto end_net_output; |
| 1232 | } |
| 1233 | |
| 1234 | ret = config_writer_write_element_string(writer, |
| 1235 | config_element_control_uri, uri); |
| 1236 | if (ret) { |
| 1237 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1238 | goto end_net_output; |
| 1239 | } |
| 1240 | |
| 1241 | ret = uri_to_str_url(&output->dst.net.data, uri, PATH_MAX); |
| 1242 | if (ret < 0) { |
| 1243 | ret = LTTNG_ERR_INVALID; |
| 1244 | goto end_net_output; |
| 1245 | } |
| 1246 | |
| 1247 | ret = config_writer_write_element_string(writer, |
| 1248 | config_element_data_uri, uri); |
| 1249 | if (ret) { |
| 1250 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1251 | goto end_net_output; |
| 1252 | } |
| 1253 | |
| 1254 | end_net_output: |
| 1255 | free(uri); |
| 1256 | if (ret) { |
| 1257 | goto end; |
| 1258 | } |
| 1259 | } else { |
| 1260 | ret = !output->dst.net.control_isset ? |
| 1261 | LTTNG_ERR_URL_CTRL_MISS : |
| 1262 | LTTNG_ERR_URL_DATA_MISS; |
| 1263 | free(uri); |
| 1264 | goto end; |
| 1265 | } |
| 1266 | |
| 1267 | ret = config_writer_close_element(writer); |
| 1268 | if (ret) { |
| 1269 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1270 | goto end; |
| 1271 | } |
| 1272 | break; |
| 1273 | } |
| 1274 | default: |
| 1275 | ERR("Unsupported consumer output type."); |
| 1276 | ret = LTTNG_ERR_INVALID; |
| 1277 | goto end; |
| 1278 | } |
| 1279 | |
| 1280 | /* /destination */ |
| 1281 | ret = config_writer_close_element(writer); |
| 1282 | if (ret) { |
| 1283 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1284 | goto end; |
| 1285 | } |
| 1286 | |
| 1287 | /* /consumer_output */ |
| 1288 | ret = config_writer_close_element(writer); |
| 1289 | if (ret) { |
| 1290 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1291 | goto end; |
| 1292 | } |
| 1293 | end: |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
| 1297 | static |
| 1298 | int save_snapshot_outputs(struct config_writer *writer, |
| 1299 | struct snapshot *snapshot) |
| 1300 | { |
| 1301 | int ret; |
| 1302 | struct lttng_ht_iter iter; |
| 1303 | struct snapshot_output *output; |
| 1304 | |
| 1305 | assert(writer); |
| 1306 | assert(snapshot); |
| 1307 | |
| 1308 | ret = config_writer_open_element(writer, config_element_snapshot_outputs); |
| 1309 | if (ret) { |
| 1310 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1311 | goto end; |
| 1312 | } |
| 1313 | |
| 1314 | rcu_read_lock(); |
| 1315 | cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output, |
| 1316 | node.node) { |
| 1317 | ret = config_writer_open_element(writer, |
| 1318 | config_element_output); |
| 1319 | if (ret) { |
| 1320 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1321 | goto end_unlock; |
| 1322 | } |
| 1323 | |
| 1324 | ret = config_writer_write_element_string(writer, |
| 1325 | config_element_name, output->name); |
| 1326 | if (ret) { |
| 1327 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1328 | goto end_unlock; |
| 1329 | } |
| 1330 | |
| 1331 | ret = config_writer_write_element_unsigned_int(writer, |
| 1332 | config_element_max_size, output->max_size); |
| 1333 | if (ret) { |
| 1334 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1335 | goto end_unlock; |
| 1336 | } |
| 1337 | |
| 1338 | ret = save_consumer_output(writer, output->consumer); |
| 1339 | if (ret) { |
| 1340 | goto end_unlock; |
| 1341 | } |
| 1342 | |
| 1343 | /* /output */ |
| 1344 | ret = config_writer_close_element(writer); |
| 1345 | if (ret) { |
| 1346 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1347 | goto end_unlock; |
| 1348 | } |
| 1349 | } |
| 1350 | rcu_read_unlock(); |
| 1351 | |
| 1352 | /* /snapshot_outputs */ |
| 1353 | ret = config_writer_close_element(writer); |
| 1354 | if (ret) { |
| 1355 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1356 | goto end; |
| 1357 | } |
| 1358 | |
| 1359 | end: |
| 1360 | return ret; |
| 1361 | end_unlock: |
| 1362 | rcu_read_unlock(); |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | static |
| 1367 | int save_session_output(struct config_writer *writer, |
| 1368 | struct ltt_session *session) |
| 1369 | { |
| 1370 | int ret; |
| 1371 | |
| 1372 | assert(writer); |
| 1373 | assert(session); |
| 1374 | |
| 1375 | if ((session->snapshot_mode && session->snapshot.nb_output == 0) || |
| 1376 | (!session->snapshot_mode && !session->consumer)) { |
| 1377 | /* Session is in no output mode */ |
| 1378 | ret = 0; |
| 1379 | goto end; |
| 1380 | } |
| 1381 | |
| 1382 | ret = config_writer_open_element(writer, config_element_output); |
| 1383 | if (ret) { |
| 1384 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1385 | goto end; |
| 1386 | } |
| 1387 | |
| 1388 | if (session->snapshot_mode) { |
| 1389 | ret = save_snapshot_outputs(writer, &session->snapshot); |
| 1390 | if (ret) { |
| 1391 | goto end; |
| 1392 | } |
| 1393 | } else { |
| 1394 | if (session->consumer) { |
| 1395 | ret = save_consumer_output(writer, session->consumer); |
| 1396 | if (ret) { |
| 1397 | goto end; |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* /output */ |
| 1403 | ret = config_writer_close_element(writer); |
| 1404 | if (ret) { |
| 1405 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1406 | goto end; |
| 1407 | } |
| 1408 | end: |
| 1409 | return ret; |
| 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * Save the given session. |
| 1414 | * |
| 1415 | * Return 0 on success else a LTTNG_ERR* code. |
| 1416 | */ |
| 1417 | static |
| 1418 | int save_session(struct ltt_session *session, |
| 1419 | struct lttng_save_session_attr *attr, lttng_sock_cred *creds) |
| 1420 | { |
| 1421 | int ret, fd; |
| 1422 | unsigned int file_opened = 0; /* Indicate if the file has been opened */ |
| 1423 | char config_file_path[PATH_MAX]; |
| 1424 | size_t len; |
| 1425 | struct config_writer *writer = NULL; |
| 1426 | size_t session_name_len; |
| 1427 | const char *provided_path; |
| 1428 | |
| 1429 | assert(session); |
| 1430 | assert(attr); |
| 1431 | assert(creds); |
| 1432 | |
| 1433 | session_name_len = strlen(session->name); |
| 1434 | memset(config_file_path, 0, sizeof(config_file_path)); |
| 1435 | |
| 1436 | if (!session_access_ok(session, |
| 1437 | LTTNG_SOCK_GET_UID_CRED(creds), |
| 1438 | LTTNG_SOCK_GET_GID_CRED(creds))) { |
| 1439 | ret = LTTNG_ERR_EPERM; |
| 1440 | goto end; |
| 1441 | } |
| 1442 | |
| 1443 | provided_path = lttng_save_session_attr_get_output_url(attr); |
| 1444 | if (provided_path) { |
| 1445 | DBG3("Save session in provided path %s", provided_path); |
| 1446 | len = strlen(provided_path); |
| 1447 | if (len >= sizeof(config_file_path)) { |
| 1448 | ret = LTTNG_ERR_SET_URL; |
| 1449 | goto end; |
| 1450 | } |
| 1451 | strncpy(config_file_path, provided_path, len); |
| 1452 | } else { |
| 1453 | ssize_t ret_len; |
| 1454 | char *home_dir = utils_get_user_home_dir( |
| 1455 | LTTNG_SOCK_GET_UID_CRED(creds)); |
| 1456 | if (!home_dir) { |
| 1457 | ret = LTTNG_ERR_SET_URL; |
| 1458 | goto end; |
| 1459 | } |
| 1460 | |
| 1461 | ret_len = snprintf(config_file_path, sizeof(config_file_path), |
| 1462 | DEFAULT_SESSION_HOME_CONFIGPATH, home_dir); |
| 1463 | free(home_dir); |
| 1464 | if (ret_len < 0) { |
| 1465 | PERROR("snprintf save session"); |
| 1466 | ret = LTTNG_ERR_SET_URL; |
| 1467 | goto end; |
| 1468 | } |
| 1469 | len = ret_len; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Check the path fits in the config file path dst including the '/' |
| 1474 | * followed by trailing .lttng extension and the NULL terminated string. |
| 1475 | */ |
| 1476 | if ((len + session_name_len + 2 + |
| 1477 | sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) |
| 1478 | > sizeof(config_file_path)) { |
| 1479 | ret = LTTNG_ERR_SET_URL; |
| 1480 | goto end; |
| 1481 | } |
| 1482 | |
| 1483 | ret = run_as_mkdir_recursive(config_file_path, S_IRWXU | S_IRWXG, |
| 1484 | LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds)); |
| 1485 | if (ret) { |
| 1486 | ret = LTTNG_ERR_SET_URL; |
| 1487 | goto end; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * At this point, we know that everything fits in the buffer. Validation |
| 1492 | * was done just above. |
| 1493 | */ |
| 1494 | config_file_path[len++] = '/'; |
| 1495 | strncpy(config_file_path + len, session->name, session_name_len); |
| 1496 | len += session_name_len; |
| 1497 | strcpy(config_file_path + len, DEFAULT_SESSION_CONFIG_FILE_EXTENSION); |
| 1498 | len += sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION); |
| 1499 | config_file_path[len] = '\0'; |
| 1500 | |
| 1501 | if (!access(config_file_path, F_OK) && !attr->overwrite) { |
| 1502 | /* File exists, notify the user since the overwrite flag is off. */ |
| 1503 | ret = LTTNG_ERR_SAVE_FILE_EXIST; |
| 1504 | goto end; |
| 1505 | } |
| 1506 | |
| 1507 | fd = run_as_open(config_file_path, O_CREAT | O_WRONLY | O_TRUNC, |
| 1508 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, |
| 1509 | LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds)); |
| 1510 | if (fd < 0) { |
| 1511 | PERROR("Could not create configuration file"); |
| 1512 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1513 | goto end; |
| 1514 | } |
| 1515 | file_opened = 1; |
| 1516 | |
| 1517 | writer = config_writer_create(fd); |
| 1518 | if (!writer) { |
| 1519 | ret = LTTNG_ERR_NOMEM; |
| 1520 | goto end; |
| 1521 | } |
| 1522 | |
| 1523 | ret = config_writer_open_element(writer, config_element_sessions); |
| 1524 | if (ret) { |
| 1525 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1526 | goto end; |
| 1527 | } |
| 1528 | |
| 1529 | ret = config_writer_open_element(writer, config_element_session); |
| 1530 | if (ret) { |
| 1531 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1532 | goto end; |
| 1533 | } |
| 1534 | |
| 1535 | ret = config_writer_write_element_string(writer, config_element_name, |
| 1536 | session->name); |
| 1537 | if (ret) { |
| 1538 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1539 | goto end; |
| 1540 | } |
| 1541 | |
| 1542 | ret = save_domains(writer, session); |
| 1543 | if (ret) { |
| 1544 | goto end; |
| 1545 | } |
| 1546 | |
| 1547 | ret = config_writer_write_element_bool(writer, config_element_started, |
| 1548 | session->active); |
| 1549 | if (ret) { |
| 1550 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1551 | goto end; |
| 1552 | } |
| 1553 | |
| 1554 | if (session->snapshot_mode || session->live_timer) { |
| 1555 | ret = config_writer_open_element(writer, config_element_attributes); |
| 1556 | if (ret) { |
| 1557 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1558 | goto end; |
| 1559 | } |
| 1560 | |
| 1561 | if (session->snapshot_mode) { |
| 1562 | ret = config_writer_write_element_bool(writer, |
| 1563 | config_element_snapshot_mode, 1); |
| 1564 | if (ret) { |
| 1565 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1566 | goto end; |
| 1567 | } |
| 1568 | } else { |
| 1569 | ret = config_writer_write_element_unsigned_int(writer, |
| 1570 | config_element_live_timer_interval, session->live_timer); |
| 1571 | if (ret) { |
| 1572 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1573 | goto end; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /* /attributes */ |
| 1578 | ret = config_writer_close_element(writer); |
| 1579 | if (ret) { |
| 1580 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1581 | goto end; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | ret = save_session_output(writer, session); |
| 1586 | if (ret) { |
| 1587 | goto end; |
| 1588 | } |
| 1589 | |
| 1590 | /* /session */ |
| 1591 | ret = config_writer_close_element(writer); |
| 1592 | if (ret) { |
| 1593 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1594 | goto end; |
| 1595 | } |
| 1596 | |
| 1597 | /* /sessions */ |
| 1598 | ret = config_writer_close_element(writer); |
| 1599 | if (ret) { |
| 1600 | ret = LTTNG_ERR_SAVE_IO_FAIL; |
| 1601 | goto end; |
| 1602 | } |
| 1603 | end: |
| 1604 | if (writer && config_writer_destroy(writer)) { |
| 1605 | /* Preserve the original error code */ |
| 1606 | ret = ret ? ret : LTTNG_ERR_SAVE_IO_FAIL; |
| 1607 | } |
| 1608 | if (ret) { |
| 1609 | /* Delete file in case of error */ |
| 1610 | if (file_opened && unlink(config_file_path)) { |
| 1611 | PERROR("Unlinking XML session configuration."); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | int cmd_save_sessions(struct lttng_save_session_attr *attr, |
| 1619 | lttng_sock_cred *creds) |
| 1620 | { |
| 1621 | int ret; |
| 1622 | const char *session_name; |
| 1623 | struct ltt_session *session; |
| 1624 | |
| 1625 | session_lock_list(); |
| 1626 | |
| 1627 | session_name = lttng_save_session_attr_get_session_name(attr); |
| 1628 | if (session_name) { |
| 1629 | session = session_find_by_name(session_name); |
| 1630 | if (!session) { |
| 1631 | ret = LTTNG_ERR_SESS_NOT_FOUND; |
| 1632 | goto end; |
| 1633 | } |
| 1634 | |
| 1635 | session_lock(session); |
| 1636 | ret = save_session(session, attr, creds); |
| 1637 | session_unlock(session); |
| 1638 | if (ret) { |
| 1639 | goto end; |
| 1640 | } |
| 1641 | } else { |
| 1642 | struct ltt_session_list *list = session_get_list(); |
| 1643 | |
| 1644 | cds_list_for_each_entry(session, &list->head, list) { |
| 1645 | session_lock(session); |
| 1646 | ret = save_session(session, attr, creds); |
| 1647 | session_unlock(session); |
| 1648 | |
| 1649 | /* Don't abort if we don't have the required permissions. */ |
| 1650 | if (ret && ret != LTTNG_ERR_EPERM) { |
| 1651 | goto end; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | ret = LTTNG_OK; |
| 1656 | |
| 1657 | end: |
| 1658 | session_unlock_list(); |
| 1659 | return ret; |
| 1660 | } |