| 1 | /* |
| 2 | * Copyright (C) 2013 - 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 <ctype.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <dirent.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #include <common/defaults.h> |
| 31 | #include <common/error.h> |
| 32 | #include <common/macros.h> |
| 33 | #include <common/utils.h> |
| 34 | #include <lttng/lttng-error.h> |
| 35 | #include <libxml/parser.h> |
| 36 | #include <libxml/valid.h> |
| 37 | #include <libxml/xmlschemas.h> |
| 38 | #include <libxml/tree.h> |
| 39 | #include <lttng/lttng.h> |
| 40 | #include <lttng/snapshot.h> |
| 41 | |
| 42 | #include "config.h" |
| 43 | #include "config-internal.h" |
| 44 | |
| 45 | struct handler_filter_args { |
| 46 | const char* section; |
| 47 | config_entry_handler_cb handler; |
| 48 | void *user_data; |
| 49 | }; |
| 50 | |
| 51 | struct session_config_validation_ctx { |
| 52 | xmlSchemaParserCtxtPtr parser_ctx; |
| 53 | xmlSchemaPtr schema; |
| 54 | xmlSchemaValidCtxtPtr schema_validation_ctx; |
| 55 | }; |
| 56 | |
| 57 | const char * const config_str_yes = "yes"; |
| 58 | const char * const config_str_true = "true"; |
| 59 | const char * const config_str_on = "on"; |
| 60 | const char * const config_str_no = "no"; |
| 61 | const char * const config_str_false = "false"; |
| 62 | const char * const config_str_off = "off"; |
| 63 | const char * const config_xml_encoding = "UTF-8"; |
| 64 | const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */ |
| 65 | const char * const config_xml_indent_string = "\t"; |
| 66 | const char * const config_xml_true = "true"; |
| 67 | const char * const config_xml_false = "false"; |
| 68 | |
| 69 | const char * const config_element_channel = "channel"; |
| 70 | const char * const config_element_channels = "channels"; |
| 71 | const char * const config_element_domain = "domain"; |
| 72 | const char * const config_element_domains = "domains"; |
| 73 | const char * const config_element_event = "event"; |
| 74 | const char * const config_element_events = "events"; |
| 75 | const char * const config_element_context = "context"; |
| 76 | const char * const config_element_contexts = "contexts"; |
| 77 | const char * const config_element_attributes = "attributes"; |
| 78 | const char * const config_element_exclusion = "exclusion"; |
| 79 | const char * const config_element_exclusions = "exclusions"; |
| 80 | const char * const config_element_function_attributes = "function_attributes"; |
| 81 | const char * const config_element_probe_attributes = "probe_attributes"; |
| 82 | const char * const config_element_symbol_name = "symbol_name"; |
| 83 | const char * const config_element_address = "address"; |
| 84 | const char * const config_element_offset = "offset"; |
| 85 | const char * const config_element_name = "name"; |
| 86 | const char * const config_element_enabled = "enabled"; |
| 87 | const char * const config_element_overwrite_mode = "overwrite_mode"; |
| 88 | const char * const config_element_subbuf_size = "subbuffer_size"; |
| 89 | const char * const config_element_num_subbuf = "subbuffer_count"; |
| 90 | const char * const config_element_switch_timer_interval = "switch_timer_interval"; |
| 91 | const char * const config_element_read_timer_interval = "read_timer_interval"; |
| 92 | const char * const config_element_output = "output"; |
| 93 | const char * const config_element_output_type = "output_type"; |
| 94 | const char * const config_element_tracefile_size = "tracefile_size"; |
| 95 | const char * const config_element_tracefile_count = "tracefile_count"; |
| 96 | const char * const config_element_live_timer_interval = "live_timer_interval"; |
| 97 | const char * const config_element_type = "type"; |
| 98 | const char * const config_element_buffer_type = "buffer_type"; |
| 99 | const char * const config_element_session = "session"; |
| 100 | const char * const config_element_sessions = "sessions"; |
| 101 | const char * const config_element_perf = "perf"; |
| 102 | const char * const config_element_config = "config"; |
| 103 | const char * const config_element_started = "started"; |
| 104 | const char * const config_element_snapshot_mode = "snapshot_mode"; |
| 105 | const char * const config_element_loglevel = "loglevel"; |
| 106 | const char * const config_element_loglevel_type = "loglevel_type"; |
| 107 | const char * const config_element_filter = "filter"; |
| 108 | const char * const config_element_snapshot_outputs = "snapshot_outputs"; |
| 109 | const char * const config_element_consumer_output = "consumer_output"; |
| 110 | const char * const config_element_destination = "destination"; |
| 111 | const char * const config_element_path = "path"; |
| 112 | const char * const config_element_net_output = "net_output"; |
| 113 | const char * const config_element_control_uri = "control_uri"; |
| 114 | const char * const config_element_data_uri = "data_uri"; |
| 115 | const char * const config_element_max_size = "max_size"; |
| 116 | |
| 117 | const char * const config_domain_type_kernel = "KERNEL"; |
| 118 | const char * const config_domain_type_ust = "UST"; |
| 119 | const char * const config_domain_type_jul = "JUL"; |
| 120 | const char * const config_domain_type_log4j = "LOG4J"; |
| 121 | |
| 122 | const char * const config_buffer_type_per_pid = "PER_PID"; |
| 123 | const char * const config_buffer_type_per_uid = "PER_UID"; |
| 124 | const char * const config_buffer_type_global = "GLOBAL"; |
| 125 | |
| 126 | const char * const config_overwrite_mode_discard = "DISCARD"; |
| 127 | const char * const config_overwrite_mode_overwrite = "OVERWRITE"; |
| 128 | |
| 129 | const char * const config_output_type_splice = "SPLICE"; |
| 130 | const char * const config_output_type_mmap = "MMAP"; |
| 131 | |
| 132 | const char * const config_loglevel_type_all = "ALL"; |
| 133 | const char * const config_loglevel_type_range = "RANGE"; |
| 134 | const char * const config_loglevel_type_single = "SINGLE"; |
| 135 | |
| 136 | const char * const config_event_type_all = "ALL"; |
| 137 | const char * const config_event_type_tracepoint = "TRACEPOINT"; |
| 138 | const char * const config_event_type_probe = "PROBE"; |
| 139 | const char * const config_event_type_function = "FUNCTION"; |
| 140 | const char * const config_event_type_function_entry = "FUNCTION_ENTRY"; |
| 141 | const char * const config_event_type_noop = "NOOP"; |
| 142 | const char * const config_event_type_syscall = "SYSCALL"; |
| 143 | const char * const config_event_type_kprobe = "KPROBE"; |
| 144 | const char * const config_event_type_kretprobe = "KRETPROBE"; |
| 145 | |
| 146 | const char * const config_event_context_pid = "PID"; |
| 147 | const char * const config_event_context_procname = "PROCNAME"; |
| 148 | const char * const config_event_context_prio = "PRIO"; |
| 149 | const char * const config_event_context_nice = "NICE"; |
| 150 | const char * const config_event_context_vpid = "VPID"; |
| 151 | const char * const config_event_context_tid = "TID"; |
| 152 | const char * const config_event_context_vtid = "VTID"; |
| 153 | const char * const config_event_context_ppid = "PPID"; |
| 154 | const char * const config_event_context_vppid = "VPPID"; |
| 155 | const char * const config_event_context_pthread_id = "PTHREAD_ID"; |
| 156 | const char * const config_event_context_hostname = "HOSTNAME"; |
| 157 | const char * const config_event_context_ip = "IP"; |
| 158 | const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER"; |
| 159 | |
| 160 | struct consumer_output { |
| 161 | int enabled; |
| 162 | char *path; |
| 163 | char *control_uri; |
| 164 | char *data_uri; |
| 165 | }; |
| 166 | |
| 167 | static int config_entry_handler_filter(struct handler_filter_args *args, |
| 168 | const char *section, const char *name, const char *value) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | struct config_entry entry = { section, name, value }; |
| 172 | |
| 173 | assert(args); |
| 174 | |
| 175 | if (!section || !name || !value) { |
| 176 | ret = -EIO; |
| 177 | goto end; |
| 178 | } |
| 179 | |
| 180 | if (args->section) { |
| 181 | if (strcmp(args->section, section)) { |
| 182 | goto end; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | ret = args->handler(&entry, args->user_data); |
| 187 | end: |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | LTTNG_HIDDEN |
| 192 | int config_get_section_entries(const char *override_path, const char *section, |
| 193 | config_entry_handler_cb handler, void *user_data) |
| 194 | { |
| 195 | int ret = 0; |
| 196 | char *path; |
| 197 | FILE *config_file = NULL; |
| 198 | struct handler_filter_args filter = { section, handler, user_data }; |
| 199 | |
| 200 | /* First, try system-wide conf. file. */ |
| 201 | path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH; |
| 202 | |
| 203 | config_file = fopen(path, "r"); |
| 204 | if (config_file) { |
| 205 | DBG("Loading daemon conf file at %s", path); |
| 206 | /* |
| 207 | * Return value is not very important here since error or not, we |
| 208 | * continue and try the next possible conf. file. |
| 209 | */ |
| 210 | (void) ini_parse_file(config_file, |
| 211 | (ini_entry_handler) config_entry_handler_filter, |
| 212 | (void *) &filter); |
| 213 | fclose(config_file); |
| 214 | } |
| 215 | |
| 216 | /* Second is the user local configuration. */ |
| 217 | path = utils_get_home_dir(); |
| 218 | if (path) { |
| 219 | char fullpath[PATH_MAX]; |
| 220 | |
| 221 | ret = snprintf(fullpath, sizeof(fullpath), |
| 222 | DEFAULT_DAEMON_HOME_CONFIGPATH, path); |
| 223 | if (ret < 0) { |
| 224 | PERROR("snprintf user conf. path"); |
| 225 | goto error; |
| 226 | } |
| 227 | |
| 228 | config_file = fopen(fullpath, "r"); |
| 229 | if (config_file) { |
| 230 | DBG("Loading daemon user conf file at %s", path); |
| 231 | /* |
| 232 | * Return value is not very important here since error or not, we |
| 233 | * continue and try the next possible conf. file. |
| 234 | */ |
| 235 | (void) ini_parse_file(config_file, |
| 236 | (ini_entry_handler) config_entry_handler_filter, |
| 237 | (void *) &filter); |
| 238 | fclose(config_file); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* Final path is the one that the user might have provided. */ |
| 243 | if (override_path) { |
| 244 | config_file = fopen(override_path, "r"); |
| 245 | if (config_file) { |
| 246 | DBG("Loading daemon command line conf file at %s", override_path); |
| 247 | (void) ini_parse_file(config_file, |
| 248 | (ini_entry_handler) config_entry_handler_filter, |
| 249 | (void *) &filter); |
| 250 | fclose(config_file); |
| 251 | } else { |
| 252 | ERR("Failed to open daemon configuration file at %s", |
| 253 | override_path); |
| 254 | ret = -ENOENT; |
| 255 | goto error; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Everything went well. */ |
| 260 | ret = 0; |
| 261 | |
| 262 | error: |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | LTTNG_HIDDEN |
| 267 | int config_parse_value(const char *value) |
| 268 | { |
| 269 | int i, ret = 0; |
| 270 | char *endptr, *lower_str; |
| 271 | size_t len; |
| 272 | unsigned long v; |
| 273 | |
| 274 | len = strlen(value); |
| 275 | if (!len) { |
| 276 | ret = -1; |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | v = strtoul(value, &endptr, 10); |
| 281 | if (endptr != value) { |
| 282 | ret = v; |
| 283 | goto end; |
| 284 | } |
| 285 | |
| 286 | lower_str = zmalloc(len + 1); |
| 287 | if (!lower_str) { |
| 288 | PERROR("zmalloc"); |
| 289 | ret = -errno; |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | for (i = 0; i < len; i++) { |
| 294 | lower_str[i] = tolower(value[i]); |
| 295 | } |
| 296 | |
| 297 | if (!strcmp(lower_str, config_str_yes) || |
| 298 | !strcmp(lower_str, config_str_true) || |
| 299 | !strcmp(lower_str, config_str_on)) { |
| 300 | ret = 1; |
| 301 | } else if (!strcmp(lower_str, config_str_no) || |
| 302 | !strcmp(lower_str, config_str_false) || |
| 303 | !strcmp(lower_str, config_str_off)) { |
| 304 | ret = 0; |
| 305 | } else { |
| 306 | ret = -1; |
| 307 | } |
| 308 | |
| 309 | free(lower_str); |
| 310 | end: |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Returns a xmlChar string which must be released using xmlFree(). |
| 316 | */ |
| 317 | static xmlChar *encode_string(const char *in_str) |
| 318 | { |
| 319 | xmlChar *out_str = NULL; |
| 320 | xmlCharEncodingHandlerPtr handler; |
| 321 | int out_len, ret, in_len; |
| 322 | |
| 323 | assert(in_str); |
| 324 | |
| 325 | handler = xmlFindCharEncodingHandler(config_xml_encoding); |
| 326 | if (!handler) { |
| 327 | ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!"); |
| 328 | goto end; |
| 329 | } |
| 330 | |
| 331 | in_len = strlen(in_str); |
| 332 | /* |
| 333 | * Add 1 byte for the NULL terminted character. The factor 2 here is |
| 334 | * because UTF-8 can be on two bytes so this fits the worst case for each |
| 335 | * bytes. |
| 336 | */ |
| 337 | out_len = (in_len * 2) + 1; |
| 338 | out_str = xmlMalloc(out_len); |
| 339 | if (!out_str) { |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len); |
| 344 | if (ret < 0) { |
| 345 | xmlFree(out_str); |
| 346 | out_str = NULL; |
| 347 | goto end; |
| 348 | } |
| 349 | |
| 350 | /* out_len is now the size of out_str */ |
| 351 | out_str[out_len] = '\0'; |
| 352 | end: |
| 353 | return out_str; |
| 354 | } |
| 355 | |
| 356 | LTTNG_HIDDEN |
| 357 | struct config_writer *config_writer_create(int fd_output, int indent) |
| 358 | { |
| 359 | int ret; |
| 360 | struct config_writer *writer; |
| 361 | xmlOutputBufferPtr buffer; |
| 362 | |
| 363 | writer = zmalloc(sizeof(struct config_writer)); |
| 364 | if (!writer) { |
| 365 | PERROR("zmalloc config_writer_create"); |
| 366 | goto end; |
| 367 | } |
| 368 | |
| 369 | buffer = xmlOutputBufferCreateFd(fd_output, NULL); |
| 370 | if (!buffer) { |
| 371 | goto error_destroy; |
| 372 | } |
| 373 | |
| 374 | writer->writer = xmlNewTextWriter(buffer); |
| 375 | ret = xmlTextWriterStartDocument(writer->writer, NULL, |
| 376 | config_xml_encoding, NULL); |
| 377 | if (ret < 0) { |
| 378 | goto error_destroy; |
| 379 | } |
| 380 | |
| 381 | ret = xmlTextWriterSetIndentString(writer->writer, |
| 382 | BAD_CAST config_xml_indent_string); |
| 383 | if (ret) { |
| 384 | goto error_destroy; |
| 385 | } |
| 386 | |
| 387 | ret = xmlTextWriterSetIndent(writer->writer, indent); |
| 388 | if (ret) { |
| 389 | goto error_destroy; |
| 390 | } |
| 391 | |
| 392 | end: |
| 393 | return writer; |
| 394 | error_destroy: |
| 395 | config_writer_destroy(writer); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | LTTNG_HIDDEN |
| 400 | int config_writer_destroy(struct config_writer *writer) |
| 401 | { |
| 402 | int ret = 0; |
| 403 | |
| 404 | if (!writer) { |
| 405 | ret = -EINVAL; |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | if (xmlTextWriterEndDocument(writer->writer) < 0) { |
| 410 | WARN("Could not close XML document"); |
| 411 | ret = -EIO; |
| 412 | } |
| 413 | |
| 414 | if (writer->writer) { |
| 415 | xmlFreeTextWriter(writer->writer); |
| 416 | } |
| 417 | |
| 418 | free(writer); |
| 419 | end: |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | LTTNG_HIDDEN |
| 424 | int config_writer_open_element(struct config_writer *writer, |
| 425 | const char *element_name) |
| 426 | { |
| 427 | int ret; |
| 428 | xmlChar *encoded_element_name; |
| 429 | |
| 430 | if (!writer || !writer->writer || !element_name || !element_name[0]) { |
| 431 | ret = -1; |
| 432 | goto end; |
| 433 | } |
| 434 | |
| 435 | encoded_element_name = encode_string(element_name); |
| 436 | if (!encoded_element_name) { |
| 437 | ret = -1; |
| 438 | goto end; |
| 439 | } |
| 440 | |
| 441 | ret = xmlTextWriterStartElement(writer->writer, encoded_element_name); |
| 442 | xmlFree(encoded_element_name); |
| 443 | end: |
| 444 | return ret > 0 ? 0 : ret; |
| 445 | } |
| 446 | |
| 447 | LTTNG_HIDDEN |
| 448 | int config_writer_close_element(struct config_writer *writer) |
| 449 | { |
| 450 | int ret; |
| 451 | |
| 452 | if (!writer || !writer->writer) { |
| 453 | ret = -1; |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | ret = xmlTextWriterEndElement(writer->writer); |
| 458 | end: |
| 459 | return ret > 0 ? 0 : ret; |
| 460 | } |
| 461 | |
| 462 | LTTNG_HIDDEN |
| 463 | int config_writer_write_element_unsigned_int(struct config_writer *writer, |
| 464 | const char *element_name, uint64_t value) |
| 465 | { |
| 466 | int ret; |
| 467 | xmlChar *encoded_element_name; |
| 468 | |
| 469 | if (!writer || !writer->writer || !element_name || !element_name[0]) { |
| 470 | ret = -1; |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | encoded_element_name = encode_string(element_name); |
| 475 | if (!encoded_element_name) { |
| 476 | ret = -1; |
| 477 | goto end; |
| 478 | } |
| 479 | |
| 480 | ret = xmlTextWriterWriteFormatElement(writer->writer, |
| 481 | encoded_element_name, "%" PRIu64, value); |
| 482 | xmlFree(encoded_element_name); |
| 483 | end: |
| 484 | return ret > 0 ? 0 : ret; |
| 485 | } |
| 486 | |
| 487 | LTTNG_HIDDEN |
| 488 | int config_writer_write_element_signed_int(struct config_writer *writer, |
| 489 | const char *element_name, int64_t value) |
| 490 | { |
| 491 | int ret; |
| 492 | xmlChar *encoded_element_name; |
| 493 | |
| 494 | if (!writer || !writer->writer || !element_name || !element_name[0]) { |
| 495 | ret = -1; |
| 496 | goto end; |
| 497 | } |
| 498 | |
| 499 | encoded_element_name = encode_string(element_name); |
| 500 | if (!encoded_element_name) { |
| 501 | ret = -1; |
| 502 | goto end; |
| 503 | } |
| 504 | |
| 505 | ret = xmlTextWriterWriteFormatElement(writer->writer, |
| 506 | encoded_element_name, "%" PRIi64, value); |
| 507 | xmlFree(encoded_element_name); |
| 508 | end: |
| 509 | return ret > 0 ? 0 : ret; |
| 510 | } |
| 511 | |
| 512 | LTTNG_HIDDEN |
| 513 | int config_writer_write_element_bool(struct config_writer *writer, |
| 514 | const char *element_name, int value) |
| 515 | { |
| 516 | return config_writer_write_element_string(writer, element_name, |
| 517 | value ? config_xml_true : config_xml_false); |
| 518 | } |
| 519 | |
| 520 | LTTNG_HIDDEN |
| 521 | int config_writer_write_element_string(struct config_writer *writer, |
| 522 | const char *element_name, const char *value) |
| 523 | { |
| 524 | int ret; |
| 525 | xmlChar *encoded_element_name = NULL; |
| 526 | xmlChar *encoded_value = NULL; |
| 527 | |
| 528 | if (!writer || !writer->writer || !element_name || !element_name[0] || |
| 529 | !value) { |
| 530 | ret = -1; |
| 531 | goto end; |
| 532 | } |
| 533 | |
| 534 | encoded_element_name = encode_string(element_name); |
| 535 | if (!encoded_element_name) { |
| 536 | ret = -1; |
| 537 | goto end; |
| 538 | } |
| 539 | |
| 540 | encoded_value = encode_string(value); |
| 541 | if (!encoded_value) { |
| 542 | ret = -1; |
| 543 | goto end; |
| 544 | } |
| 545 | |
| 546 | ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name, |
| 547 | encoded_value); |
| 548 | end: |
| 549 | xmlFree(encoded_element_name); |
| 550 | xmlFree(encoded_value); |
| 551 | return ret > 0 ? 0 : ret; |
| 552 | } |
| 553 | |
| 554 | static |
| 555 | void xml_error_handler(void *ctx, const char *format, ...) |
| 556 | { |
| 557 | char *errMsg; |
| 558 | va_list args; |
| 559 | int ret; |
| 560 | |
| 561 | va_start(args, format); |
| 562 | ret = vasprintf(&errMsg, format, args); |
| 563 | va_end(args); |
| 564 | if (ret == -1) { |
| 565 | ERR("String allocation failed in xml error handler"); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | fprintf(stderr, "XML Error: %s", errMsg); |
| 570 | free(errMsg); |
| 571 | } |
| 572 | |
| 573 | static |
| 574 | void fini_session_config_validation_ctx( |
| 575 | struct session_config_validation_ctx *ctx) |
| 576 | { |
| 577 | if (ctx->parser_ctx) { |
| 578 | xmlSchemaFreeParserCtxt(ctx->parser_ctx); |
| 579 | } |
| 580 | |
| 581 | if (ctx->schema) { |
| 582 | xmlSchemaFree(ctx->schema); |
| 583 | } |
| 584 | |
| 585 | if (ctx->schema_validation_ctx) { |
| 586 | xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); |
| 587 | } |
| 588 | |
| 589 | memset(ctx, 0, sizeof(struct session_config_validation_ctx)); |
| 590 | } |
| 591 | |
| 592 | static |
| 593 | char *get_session_config_xsd_path() |
| 594 | { |
| 595 | char *xsd_path; |
| 596 | const char *base_path = getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV); |
| 597 | size_t base_path_len; |
| 598 | size_t max_path_len; |
| 599 | |
| 600 | if (!base_path) { |
| 601 | base_path = DEFAULT_SESSION_CONFIG_XSD_PATH; |
| 602 | } |
| 603 | |
| 604 | base_path_len = strlen(base_path); |
| 605 | max_path_len = base_path_len + |
| 606 | sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1; |
| 607 | xsd_path = zmalloc(max_path_len); |
| 608 | if (!xsd_path) { |
| 609 | goto end; |
| 610 | } |
| 611 | |
| 612 | strncpy(xsd_path, base_path, max_path_len); |
| 613 | if (xsd_path[base_path_len - 1] != '/') { |
| 614 | xsd_path[base_path_len++] = '/'; |
| 615 | } |
| 616 | |
| 617 | strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME, |
| 618 | max_path_len - base_path_len); |
| 619 | end: |
| 620 | return xsd_path; |
| 621 | } |
| 622 | |
| 623 | static |
| 624 | int init_session_config_validation_ctx( |
| 625 | struct session_config_validation_ctx *ctx) |
| 626 | { |
| 627 | int ret; |
| 628 | char *xsd_path = get_session_config_xsd_path(); |
| 629 | |
| 630 | if (!xsd_path) { |
| 631 | ret = -LTTNG_ERR_NOMEM; |
| 632 | goto end; |
| 633 | } |
| 634 | |
| 635 | ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); |
| 636 | if (!ctx->parser_ctx) { |
| 637 | ERR("XSD parser context creation failed"); |
| 638 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 639 | goto end; |
| 640 | } |
| 641 | xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, |
| 642 | xml_error_handler, NULL); |
| 643 | |
| 644 | ctx->schema = xmlSchemaParse(ctx->parser_ctx); |
| 645 | if (!ctx->schema) { |
| 646 | ERR("XSD parsing failed"); |
| 647 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 648 | goto end; |
| 649 | } |
| 650 | |
| 651 | ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema); |
| 652 | if (!ctx->schema_validation_ctx) { |
| 653 | ERR("XSD validation context creation failed"); |
| 654 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, |
| 659 | xml_error_handler, NULL); |
| 660 | ret = 0; |
| 661 | |
| 662 | end: |
| 663 | if (ret) { |
| 664 | fini_session_config_validation_ctx(ctx); |
| 665 | } |
| 666 | |
| 667 | free(xsd_path); |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | static |
| 672 | int parse_uint(xmlChar *str, uint64_t *val) |
| 673 | { |
| 674 | int ret; |
| 675 | char *endptr; |
| 676 | |
| 677 | if (!str || !val) { |
| 678 | ret = -1; |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | *val = strtoull((const char *) str, &endptr, 10); |
| 683 | if (!endptr || *endptr) { |
| 684 | ret = -1; |
| 685 | } else { |
| 686 | ret = 0; |
| 687 | } |
| 688 | |
| 689 | end: |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static |
| 694 | int parse_int(xmlChar *str, int64_t *val) |
| 695 | { |
| 696 | int ret; |
| 697 | char *endptr; |
| 698 | |
| 699 | if (!str || !val) { |
| 700 | ret = -1; |
| 701 | goto end; |
| 702 | } |
| 703 | |
| 704 | *val = strtoll((const char *) str, &endptr, 10); |
| 705 | if (!endptr || *endptr) { |
| 706 | ret = -1; |
| 707 | } else { |
| 708 | ret = 0; |
| 709 | } |
| 710 | |
| 711 | end: |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | static |
| 716 | int parse_bool(xmlChar *str, int *val) |
| 717 | { |
| 718 | int ret = 0; |
| 719 | |
| 720 | if (!str || !val) { |
| 721 | ret = -1; |
| 722 | goto end; |
| 723 | } |
| 724 | |
| 725 | if (!strcmp((const char *) str, config_xml_true)) { |
| 726 | *val = 1; |
| 727 | } else if (!strcmp((const char *) str, config_xml_false)) { |
| 728 | *val = 0; |
| 729 | } else { |
| 730 | WARN("Invalid boolean value encoutered (%s).", |
| 731 | (const char *) str); |
| 732 | ret = -1; |
| 733 | } |
| 734 | end: |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | static |
| 739 | int get_domain_type(xmlChar *domain) |
| 740 | { |
| 741 | int ret; |
| 742 | |
| 743 | if (!domain) { |
| 744 | goto error; |
| 745 | } |
| 746 | |
| 747 | if (!strcmp((char *) domain, config_domain_type_kernel)) { |
| 748 | ret = LTTNG_DOMAIN_KERNEL; |
| 749 | } else if (!strcmp((char *) domain, config_domain_type_ust)) { |
| 750 | ret = LTTNG_DOMAIN_UST; |
| 751 | } else if (!strcmp((char *) domain, config_domain_type_jul)) { |
| 752 | ret = LTTNG_DOMAIN_JUL; |
| 753 | } else if (!strcmp((char *) domain, config_domain_type_log4j)) { |
| 754 | ret = LTTNG_DOMAIN_LOG4J; |
| 755 | } else { |
| 756 | goto error; |
| 757 | } |
| 758 | |
| 759 | return ret; |
| 760 | error: |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | static |
| 765 | int get_buffer_type(xmlChar *buffer_type) |
| 766 | { |
| 767 | int ret; |
| 768 | |
| 769 | if (!buffer_type) { |
| 770 | goto error; |
| 771 | } |
| 772 | |
| 773 | if (!strcmp((char *) buffer_type, config_buffer_type_global)) { |
| 774 | ret = LTTNG_BUFFER_GLOBAL; |
| 775 | } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) { |
| 776 | ret = LTTNG_BUFFER_PER_UID; |
| 777 | } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) { |
| 778 | ret = LTTNG_BUFFER_PER_PID; |
| 779 | } else { |
| 780 | goto error; |
| 781 | } |
| 782 | |
| 783 | return ret; |
| 784 | error: |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | static |
| 789 | int get_overwrite_mode(xmlChar *overwrite_mode) |
| 790 | { |
| 791 | int ret; |
| 792 | |
| 793 | if (!overwrite_mode) { |
| 794 | goto error; |
| 795 | } |
| 796 | |
| 797 | if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) { |
| 798 | ret = 1; |
| 799 | } else if (!strcmp((char *) overwrite_mode, |
| 800 | config_overwrite_mode_discard)) { |
| 801 | ret = 0; |
| 802 | } else { |
| 803 | goto error; |
| 804 | } |
| 805 | |
| 806 | return ret; |
| 807 | error: |
| 808 | return -1; |
| 809 | } |
| 810 | |
| 811 | static |
| 812 | int get_output_type(xmlChar *output_type) |
| 813 | { |
| 814 | int ret; |
| 815 | |
| 816 | if (!output_type) { |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | if (!strcmp((char *) output_type, config_output_type_mmap)) { |
| 821 | ret = LTTNG_EVENT_MMAP; |
| 822 | } else if (!strcmp((char *) output_type, config_output_type_splice)) { |
| 823 | ret = LTTNG_EVENT_SPLICE; |
| 824 | } else { |
| 825 | goto error; |
| 826 | } |
| 827 | |
| 828 | return ret; |
| 829 | error: |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | static |
| 834 | int get_event_type(xmlChar *event_type) |
| 835 | { |
| 836 | int ret; |
| 837 | |
| 838 | if (!event_type) { |
| 839 | goto error; |
| 840 | } |
| 841 | |
| 842 | if (!strcmp((char *) event_type, config_event_type_all)) { |
| 843 | ret = LTTNG_EVENT_ALL; |
| 844 | } else if (!strcmp((char *) event_type, config_event_type_tracepoint)) { |
| 845 | ret = LTTNG_EVENT_TRACEPOINT; |
| 846 | } else if (!strcmp((char *) event_type, config_event_type_probe)) { |
| 847 | ret = LTTNG_EVENT_PROBE; |
| 848 | } else if (!strcmp((char *) event_type, config_event_type_function)) { |
| 849 | ret = LTTNG_EVENT_FUNCTION; |
| 850 | } else if (!strcmp((char *) event_type, |
| 851 | config_event_type_function_entry)) { |
| 852 | ret = LTTNG_EVENT_FUNCTION_ENTRY; |
| 853 | } else if (!strcmp((char *) event_type, config_event_type_noop)) { |
| 854 | ret = LTTNG_EVENT_NOOP; |
| 855 | } else if (!strcmp((char *) event_type, config_event_type_syscall)) { |
| 856 | ret = LTTNG_EVENT_SYSCALL; |
| 857 | } else { |
| 858 | goto error; |
| 859 | } |
| 860 | |
| 861 | return ret; |
| 862 | error: |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | static |
| 867 | int get_loglevel_type(xmlChar *loglevel_type) |
| 868 | { |
| 869 | int ret; |
| 870 | |
| 871 | if (!loglevel_type) { |
| 872 | goto error; |
| 873 | } |
| 874 | |
| 875 | if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) { |
| 876 | ret = LTTNG_EVENT_LOGLEVEL_ALL; |
| 877 | } else if (!strcmp((char *) loglevel_type, |
| 878 | config_loglevel_type_range)) { |
| 879 | ret = LTTNG_EVENT_LOGLEVEL_RANGE; |
| 880 | } else if (!strcmp((char *) loglevel_type, |
| 881 | config_loglevel_type_single)) { |
| 882 | ret = LTTNG_EVENT_LOGLEVEL_SINGLE; |
| 883 | } else { |
| 884 | goto error; |
| 885 | } |
| 886 | |
| 887 | return ret; |
| 888 | error: |
| 889 | return -1; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Return the context type or -1 on error. |
| 894 | */ |
| 895 | static |
| 896 | int get_context_type(xmlChar *context_type) |
| 897 | { |
| 898 | int ret; |
| 899 | |
| 900 | if (!context_type) { |
| 901 | goto error; |
| 902 | } |
| 903 | |
| 904 | if (!strcmp((char *) context_type, config_event_context_pid)) { |
| 905 | ret = LTTNG_EVENT_CONTEXT_PID; |
| 906 | } else if (!strcmp((char *) context_type, |
| 907 | config_event_context_procname)) { |
| 908 | ret = LTTNG_EVENT_CONTEXT_PROCNAME; |
| 909 | } else if (!strcmp((char *) context_type, |
| 910 | config_event_context_prio)) { |
| 911 | ret = LTTNG_EVENT_CONTEXT_PRIO; |
| 912 | } else if (!strcmp((char *) context_type, |
| 913 | config_event_context_nice)) { |
| 914 | ret = LTTNG_EVENT_CONTEXT_NICE; |
| 915 | } else if (!strcmp((char *) context_type, |
| 916 | config_event_context_vpid)) { |
| 917 | ret = LTTNG_EVENT_CONTEXT_VPID; |
| 918 | } else if (!strcmp((char *) context_type, |
| 919 | config_event_context_tid)) { |
| 920 | ret = LTTNG_EVENT_CONTEXT_TID; |
| 921 | } else if (!strcmp((char *) context_type, |
| 922 | config_event_context_vtid)) { |
| 923 | ret = LTTNG_EVENT_CONTEXT_VTID; |
| 924 | } else if (!strcmp((char *) context_type, |
| 925 | config_event_context_ppid)) { |
| 926 | ret = LTTNG_EVENT_CONTEXT_PPID; |
| 927 | } else if (!strcmp((char *) context_type, |
| 928 | config_event_context_vppid)) { |
| 929 | ret = LTTNG_EVENT_CONTEXT_VPPID; |
| 930 | } else if (!strcmp((char *) context_type, |
| 931 | config_event_context_pthread_id)) { |
| 932 | ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID; |
| 933 | } else if (!strcmp((char *) context_type, |
| 934 | config_event_context_hostname)) { |
| 935 | ret = LTTNG_EVENT_CONTEXT_HOSTNAME; |
| 936 | } else if (!strcmp((char *) context_type, |
| 937 | config_event_context_ip)) { |
| 938 | ret = LTTNG_EVENT_CONTEXT_IP; |
| 939 | } else { |
| 940 | goto error; |
| 941 | } |
| 942 | |
| 943 | return ret; |
| 944 | error: |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | static |
| 949 | int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain) |
| 950 | { |
| 951 | int ret; |
| 952 | xmlNodePtr node; |
| 953 | |
| 954 | for (node = xmlFirstElementChild(domain_node); node; |
| 955 | node = xmlNextElementSibling(node)) { |
| 956 | if (!strcmp((const char *) node->name, config_element_type)) { |
| 957 | /* domain type */ |
| 958 | xmlChar *node_content = xmlNodeGetContent(node); |
| 959 | if (!node_content) { |
| 960 | ret = -LTTNG_ERR_NOMEM; |
| 961 | goto end; |
| 962 | } |
| 963 | |
| 964 | ret = get_domain_type(node_content); |
| 965 | free(node_content); |
| 966 | if (ret < 0) { |
| 967 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 968 | goto end; |
| 969 | } |
| 970 | |
| 971 | domain->type = ret; |
| 972 | } else if (!strcmp((const char *) node->name, |
| 973 | config_element_buffer_type)) { |
| 974 | /* buffer type */ |
| 975 | xmlChar *node_content = xmlNodeGetContent(node); |
| 976 | if (!node_content) { |
| 977 | ret = -LTTNG_ERR_NOMEM; |
| 978 | goto end; |
| 979 | } |
| 980 | |
| 981 | ret = get_buffer_type(node_content); |
| 982 | free(node_content); |
| 983 | if (ret < 0) { |
| 984 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 985 | goto end; |
| 986 | } |
| 987 | |
| 988 | domain->buf_type = ret; |
| 989 | } |
| 990 | } |
| 991 | ret = 0; |
| 992 | end: |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | static |
| 997 | int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri, |
| 998 | char **data_uri) |
| 999 | { |
| 1000 | xmlNodePtr node; |
| 1001 | |
| 1002 | for (node = xmlFirstElementChild(net_output_node); node; |
| 1003 | node = xmlNextElementSibling(node)) { |
| 1004 | if (!strcmp((const char *) node->name, config_element_control_uri)) { |
| 1005 | /* control_uri */ |
| 1006 | *control_uri = (char *) xmlNodeGetContent(node); |
| 1007 | if (!*control_uri) { |
| 1008 | break; |
| 1009 | } |
| 1010 | } else { |
| 1011 | /* data_uri */ |
| 1012 | *data_uri = (char *) xmlNodeGetContent(node); |
| 1013 | if (!*data_uri) { |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1020 | } |
| 1021 | |
| 1022 | static |
| 1023 | int process_consumer_output(xmlNodePtr consumer_output_node, |
| 1024 | struct consumer_output *output) |
| 1025 | { |
| 1026 | int ret; |
| 1027 | xmlNodePtr node; |
| 1028 | |
| 1029 | assert(output); |
| 1030 | |
| 1031 | for (node = xmlFirstElementChild(consumer_output_node); node; |
| 1032 | node = xmlNextElementSibling(node)) { |
| 1033 | if (!strcmp((const char *) node->name, config_element_enabled)) { |
| 1034 | xmlChar *enabled_str = xmlNodeGetContent(node); |
| 1035 | |
| 1036 | /* enabled */ |
| 1037 | if (!enabled_str) { |
| 1038 | ret = -LTTNG_ERR_NOMEM; |
| 1039 | goto end; |
| 1040 | } |
| 1041 | |
| 1042 | ret = parse_bool(enabled_str, &output->enabled); |
| 1043 | free(enabled_str); |
| 1044 | if (ret) { |
| 1045 | goto end; |
| 1046 | } |
| 1047 | } else { |
| 1048 | xmlNodePtr output_type_node; |
| 1049 | |
| 1050 | /* destination */ |
| 1051 | output_type_node = xmlFirstElementChild(node); |
| 1052 | if (!output_type_node) { |
| 1053 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1054 | goto end; |
| 1055 | } |
| 1056 | |
| 1057 | if (!strcmp((const char *) output_type_node->name, |
| 1058 | config_element_path)) { |
| 1059 | /* path */ |
| 1060 | output->path = (char *) xmlNodeGetContent(output_type_node); |
| 1061 | if (!output->path) { |
| 1062 | ret = -LTTNG_ERR_NOMEM; |
| 1063 | goto end; |
| 1064 | } |
| 1065 | } else { |
| 1066 | /* net_output */ |
| 1067 | ret = get_net_output_uris(output_type_node, |
| 1068 | &output->control_uri, &output->data_uri); |
| 1069 | if (ret) { |
| 1070 | goto end; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | ret = 0; |
| 1076 | |
| 1077 | end: |
| 1078 | if (ret) { |
| 1079 | free(output->path); |
| 1080 | free(output->control_uri); |
| 1081 | free(output->data_uri); |
| 1082 | memset(output, 0, sizeof(struct consumer_output)); |
| 1083 | } |
| 1084 | return ret; |
| 1085 | } |
| 1086 | |
| 1087 | static |
| 1088 | int create_session_net_output(const char *name, struct lttng_domain *domain, |
| 1089 | const char *control_uri, const char *data_uri) |
| 1090 | { |
| 1091 | int ret; |
| 1092 | struct lttng_handle *handle; |
| 1093 | const char *uri = NULL; |
| 1094 | |
| 1095 | assert(name); |
| 1096 | assert(domain); |
| 1097 | |
| 1098 | handle = lttng_create_handle(name, domain); |
| 1099 | if (!handle) { |
| 1100 | ret = -LTTNG_ERR_NOMEM; |
| 1101 | goto end; |
| 1102 | } |
| 1103 | |
| 1104 | if (!control_uri || !data_uri) { |
| 1105 | uri = control_uri ? control_uri : data_uri; |
| 1106 | control_uri = uri; |
| 1107 | data_uri = uri; |
| 1108 | } |
| 1109 | |
| 1110 | ret = lttng_set_consumer_url(handle, control_uri, data_uri); |
| 1111 | lttng_destroy_handle(handle); |
| 1112 | end: |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | static |
| 1117 | int create_snapshot_session(const char *session_name, xmlNodePtr output_node) |
| 1118 | { |
| 1119 | int ret; |
| 1120 | xmlNodePtr node = NULL; |
| 1121 | xmlNodePtr snapshot_output_list_node; |
| 1122 | xmlNodePtr snapshot_output_node; |
| 1123 | |
| 1124 | assert(session_name); |
| 1125 | |
| 1126 | ret = lttng_create_session_snapshot(session_name, NULL); |
| 1127 | if (ret) { |
| 1128 | goto end; |
| 1129 | } |
| 1130 | |
| 1131 | if (!output_node) { |
| 1132 | goto end; |
| 1133 | } |
| 1134 | |
| 1135 | snapshot_output_list_node = xmlFirstElementChild(output_node); |
| 1136 | |
| 1137 | /* Parse and create snapshot outputs */ |
| 1138 | |
| 1139 | for (snapshot_output_node = |
| 1140 | xmlFirstElementChild(snapshot_output_list_node); |
| 1141 | snapshot_output_node; snapshot_output_node = |
| 1142 | xmlNextElementSibling(snapshot_output_node)) { |
| 1143 | char *name = NULL; |
| 1144 | uint64_t max_size = UINT64_MAX; |
| 1145 | struct consumer_output output = { 0 }; |
| 1146 | struct lttng_snapshot_output *snapshot_output = NULL; |
| 1147 | |
| 1148 | for (node = xmlFirstElementChild(snapshot_output_node); node; |
| 1149 | node = xmlNextElementSibling(node)) { |
| 1150 | if (!strcmp((const char *) node->name, |
| 1151 | config_element_name)) { |
| 1152 | /* name */ |
| 1153 | name = (char *) xmlNodeGetContent(node); |
| 1154 | if (!name) { |
| 1155 | ret = -LTTNG_ERR_NOMEM; |
| 1156 | goto error_snapshot_output; |
| 1157 | } |
| 1158 | } else if (!strcmp((const char *) node->name, |
| 1159 | config_element_max_size)) { |
| 1160 | xmlChar *content = xmlNodeGetContent(node); |
| 1161 | |
| 1162 | /* max_size */ |
| 1163 | if (!content) { |
| 1164 | ret = -LTTNG_ERR_NOMEM; |
| 1165 | goto error_snapshot_output; |
| 1166 | } |
| 1167 | ret = parse_uint(content, &max_size); |
| 1168 | free(content); |
| 1169 | if (ret) { |
| 1170 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1171 | goto error_snapshot_output; |
| 1172 | } |
| 1173 | } else { |
| 1174 | /* consumer_output */ |
| 1175 | ret = process_consumer_output(node, &output); |
| 1176 | if (ret) { |
| 1177 | goto error_snapshot_output; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | snapshot_output = lttng_snapshot_output_create(); |
| 1183 | if (!snapshot_output) { |
| 1184 | ret = -LTTNG_ERR_NOMEM; |
| 1185 | goto error_snapshot_output; |
| 1186 | } |
| 1187 | |
| 1188 | ret = lttng_snapshot_output_set_name(name, snapshot_output); |
| 1189 | if (ret) { |
| 1190 | goto error_snapshot_output; |
| 1191 | } |
| 1192 | |
| 1193 | ret = lttng_snapshot_output_set_size(max_size, snapshot_output); |
| 1194 | if (ret) { |
| 1195 | goto error_snapshot_output; |
| 1196 | } |
| 1197 | |
| 1198 | if (output.path) { |
| 1199 | ret = lttng_snapshot_output_set_ctrl_url(output.path, |
| 1200 | snapshot_output); |
| 1201 | if (ret) { |
| 1202 | goto error_snapshot_output; |
| 1203 | } |
| 1204 | } else { |
| 1205 | if (output.control_uri) { |
| 1206 | ret = lttng_snapshot_output_set_ctrl_url(output.control_uri, |
| 1207 | snapshot_output); |
| 1208 | if (ret) { |
| 1209 | goto error_snapshot_output; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (output.data_uri) { |
| 1214 | ret = lttng_snapshot_output_set_data_url(output.data_uri, |
| 1215 | snapshot_output); |
| 1216 | if (ret) { |
| 1217 | goto error_snapshot_output; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | ret = lttng_snapshot_add_output(session_name, snapshot_output); |
| 1223 | error_snapshot_output: |
| 1224 | free(name); |
| 1225 | free(output.path); |
| 1226 | free(output.control_uri); |
| 1227 | free(output.data_uri); |
| 1228 | lttng_snapshot_output_destroy(snapshot_output); |
| 1229 | if (ret) { |
| 1230 | goto end; |
| 1231 | } |
| 1232 | } |
| 1233 | end: |
| 1234 | return ret; |
| 1235 | } |
| 1236 | |
| 1237 | static |
| 1238 | int create_session(const char *name, |
| 1239 | struct lttng_domain *kernel_domain, |
| 1240 | struct lttng_domain *ust_domain, |
| 1241 | struct lttng_domain *jul_domain, |
| 1242 | struct lttng_domain *log4j_domain, |
| 1243 | xmlNodePtr output_node, |
| 1244 | uint64_t live_timer_interval) |
| 1245 | { |
| 1246 | int ret; |
| 1247 | struct consumer_output output = { 0 }; |
| 1248 | xmlNodePtr consumer_output_node; |
| 1249 | |
| 1250 | assert(name); |
| 1251 | |
| 1252 | if (output_node) { |
| 1253 | consumer_output_node = xmlFirstElementChild(output_node); |
| 1254 | if (!consumer_output_node) { |
| 1255 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1256 | goto end; |
| 1257 | } |
| 1258 | |
| 1259 | if (strcmp((const char *) consumer_output_node->name, |
| 1260 | config_element_consumer_output)) { |
| 1261 | WARN("Invalid output type, expected %s node", |
| 1262 | config_element_consumer_output); |
| 1263 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1264 | goto end; |
| 1265 | } |
| 1266 | |
| 1267 | ret = process_consumer_output(consumer_output_node, &output); |
| 1268 | if (ret) { |
| 1269 | goto end; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (live_timer_interval != UINT64_MAX && |
| 1274 | !output.control_uri && !output.data_uri) { |
| 1275 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1276 | goto end; |
| 1277 | } |
| 1278 | |
| 1279 | if (output.control_uri || output.data_uri) { |
| 1280 | int i; |
| 1281 | struct lttng_domain *domain; |
| 1282 | struct lttng_domain *domains[] = |
| 1283 | { kernel_domain, ust_domain, jul_domain, log4j_domain}; |
| 1284 | |
| 1285 | /* network destination */ |
| 1286 | if (live_timer_interval && live_timer_interval != UINT64_MAX) { |
| 1287 | /* |
| 1288 | * URLs are provided for sure since the test above make sure that |
| 1289 | * with a live timer the data and control URIs are provided. So, |
| 1290 | * NULL is passed here and will be set right after. |
| 1291 | */ |
| 1292 | ret = lttng_create_session_live(name, NULL, live_timer_interval); |
| 1293 | } else { |
| 1294 | ret = lttng_create_session(name, NULL); |
| 1295 | } |
| 1296 | if (ret) { |
| 1297 | goto end; |
| 1298 | } |
| 1299 | |
| 1300 | for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) { |
| 1301 | domain = domains[i]; |
| 1302 | if (!domain) { |
| 1303 | continue; |
| 1304 | } |
| 1305 | |
| 1306 | ret = create_session_net_output(name, domain, output.control_uri, |
| 1307 | output.data_uri); |
| 1308 | if (ret) { |
| 1309 | goto end; |
| 1310 | } |
| 1311 | } |
| 1312 | } else { |
| 1313 | /* either local output or no output */ |
| 1314 | ret = lttng_create_session(name, output.path); |
| 1315 | if (ret) { |
| 1316 | goto end; |
| 1317 | } |
| 1318 | } |
| 1319 | end: |
| 1320 | free(output.path); |
| 1321 | free(output.control_uri); |
| 1322 | free(output.data_uri); |
| 1323 | return ret; |
| 1324 | } |
| 1325 | static |
| 1326 | int process_probe_attribute_node(xmlNodePtr probe_attribute_node, |
| 1327 | struct lttng_event_probe_attr *attr) |
| 1328 | { |
| 1329 | int ret; |
| 1330 | |
| 1331 | assert(probe_attribute_node); |
| 1332 | assert(attr); |
| 1333 | |
| 1334 | if (!strcmp((const char *) probe_attribute_node->name, |
| 1335 | config_element_address)) { |
| 1336 | xmlChar *content; |
| 1337 | uint64_t addr = 0; |
| 1338 | |
| 1339 | /* addr */ |
| 1340 | content = xmlNodeGetContent(probe_attribute_node); |
| 1341 | if (!content) { |
| 1342 | ret = -LTTNG_ERR_NOMEM; |
| 1343 | goto end; |
| 1344 | } |
| 1345 | |
| 1346 | ret = parse_uint(content, &addr); |
| 1347 | free(content); |
| 1348 | if (ret) { |
| 1349 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1350 | goto end; |
| 1351 | } |
| 1352 | |
| 1353 | attr->addr = addr; |
| 1354 | } else if (!strcmp((const char *) probe_attribute_node->name, |
| 1355 | config_element_offset)) { |
| 1356 | xmlChar *content; |
| 1357 | uint64_t offset = 0; |
| 1358 | |
| 1359 | /* offset */ |
| 1360 | content = xmlNodeGetContent(probe_attribute_node); |
| 1361 | if (!content) { |
| 1362 | ret = -LTTNG_ERR_NOMEM; |
| 1363 | goto end; |
| 1364 | } |
| 1365 | |
| 1366 | ret = parse_uint(content, &offset); |
| 1367 | free(content); |
| 1368 | if (ret) { |
| 1369 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1370 | goto end; |
| 1371 | } |
| 1372 | |
| 1373 | attr->offset = offset; |
| 1374 | } else if (!strcmp((const char *) probe_attribute_node->name, |
| 1375 | config_element_symbol_name)) { |
| 1376 | xmlChar *content; |
| 1377 | size_t name_len; |
| 1378 | |
| 1379 | /* symbol_name */ |
| 1380 | content = xmlNodeGetContent(probe_attribute_node); |
| 1381 | if (!content) { |
| 1382 | ret = -LTTNG_ERR_NOMEM; |
| 1383 | goto end; |
| 1384 | } |
| 1385 | |
| 1386 | name_len = strlen((char *) content); |
| 1387 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { |
| 1388 | WARN("symbol_name too long."); |
| 1389 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1390 | free(content); |
| 1391 | goto end; |
| 1392 | } |
| 1393 | |
| 1394 | strncpy(attr->symbol_name, (const char *) content, name_len); |
| 1395 | free(content); |
| 1396 | } |
| 1397 | ret = 0; |
| 1398 | end: |
| 1399 | return ret; |
| 1400 | } |
| 1401 | |
| 1402 | static |
| 1403 | int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, |
| 1404 | const char *channel_name) |
| 1405 | { |
| 1406 | int ret, i; |
| 1407 | xmlNodePtr node; |
| 1408 | struct lttng_event event; |
| 1409 | char **exclusions = NULL; |
| 1410 | unsigned long exclusion_count = 0; |
| 1411 | char *filter_expression = NULL; |
| 1412 | |
| 1413 | assert(event_node); |
| 1414 | assert(handle); |
| 1415 | assert(channel_name); |
| 1416 | |
| 1417 | memset(&event, 0, sizeof(event)); |
| 1418 | |
| 1419 | for (node = xmlFirstElementChild(event_node); node; |
| 1420 | node = xmlNextElementSibling(node)) { |
| 1421 | if (!strcmp((const char *) node->name, config_element_name)) { |
| 1422 | xmlChar *content; |
| 1423 | size_t name_len; |
| 1424 | |
| 1425 | /* name */ |
| 1426 | content = xmlNodeGetContent(node); |
| 1427 | if (!content) { |
| 1428 | ret = -LTTNG_ERR_NOMEM; |
| 1429 | goto end; |
| 1430 | } |
| 1431 | |
| 1432 | name_len = strlen((char *) content); |
| 1433 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { |
| 1434 | WARN("Channel name too long."); |
| 1435 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1436 | free(content); |
| 1437 | goto end; |
| 1438 | } |
| 1439 | |
| 1440 | strncpy(event.name, (const char *) content, name_len); |
| 1441 | free(content); |
| 1442 | } else if (!strcmp((const char *) node->name, |
| 1443 | config_element_enabled)) { |
| 1444 | xmlChar *content = xmlNodeGetContent(node); |
| 1445 | |
| 1446 | /* enabled */ |
| 1447 | if (!content) { |
| 1448 | ret = -LTTNG_ERR_NOMEM; |
| 1449 | goto end; |
| 1450 | } |
| 1451 | |
| 1452 | ret = parse_bool(content, &event.enabled); |
| 1453 | free(content); |
| 1454 | if (ret) { |
| 1455 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1456 | goto end; |
| 1457 | } |
| 1458 | } else if (!strcmp((const char *) node->name, |
| 1459 | config_element_type)) { |
| 1460 | xmlChar *content = xmlNodeGetContent(node); |
| 1461 | |
| 1462 | /* type */ |
| 1463 | if (!content) { |
| 1464 | ret = -LTTNG_ERR_NOMEM; |
| 1465 | goto end; |
| 1466 | } |
| 1467 | |
| 1468 | ret = get_event_type(content); |
| 1469 | free(content); |
| 1470 | if (ret < 0) { |
| 1471 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1472 | goto end; |
| 1473 | } |
| 1474 | |
| 1475 | event.type = ret; |
| 1476 | } else if (!strcmp((const char *) node->name, |
| 1477 | config_element_loglevel_type)) { |
| 1478 | xmlChar *content = xmlNodeGetContent(node); |
| 1479 | |
| 1480 | /* loglevel_type */ |
| 1481 | if (!content) { |
| 1482 | ret = -LTTNG_ERR_NOMEM; |
| 1483 | goto end; |
| 1484 | } |
| 1485 | |
| 1486 | ret = get_loglevel_type(content); |
| 1487 | free(content); |
| 1488 | if (ret < 0) { |
| 1489 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1490 | goto end; |
| 1491 | } |
| 1492 | |
| 1493 | event.loglevel_type = ret; |
| 1494 | } else if (!strcmp((const char *) node->name, |
| 1495 | config_element_loglevel)) { |
| 1496 | xmlChar *content; |
| 1497 | int64_t loglevel = 0; |
| 1498 | |
| 1499 | /* loglevel */ |
| 1500 | content = xmlNodeGetContent(node); |
| 1501 | if (!content) { |
| 1502 | ret = -LTTNG_ERR_NOMEM; |
| 1503 | goto end; |
| 1504 | } |
| 1505 | |
| 1506 | ret = parse_int(content, &loglevel); |
| 1507 | free(content); |
| 1508 | if (ret) { |
| 1509 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1510 | goto end; |
| 1511 | } |
| 1512 | |
| 1513 | if (loglevel > INT_MAX || loglevel < INT_MIN) { |
| 1514 | WARN("loglevel out of range."); |
| 1515 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1516 | goto end; |
| 1517 | } |
| 1518 | |
| 1519 | event.loglevel = loglevel; |
| 1520 | } else if (!strcmp((const char *) node->name, |
| 1521 | config_element_filter)) { |
| 1522 | xmlChar *content = |
| 1523 | xmlNodeGetContent(node); |
| 1524 | |
| 1525 | /* filter */ |
| 1526 | if (!content) { |
| 1527 | ret = -LTTNG_ERR_NOMEM; |
| 1528 | goto end; |
| 1529 | } |
| 1530 | |
| 1531 | filter_expression = strdup((char *) content); |
| 1532 | free(content); |
| 1533 | if (!filter_expression) { |
| 1534 | ret = -LTTNG_ERR_NOMEM; |
| 1535 | goto end; |
| 1536 | } |
| 1537 | } else if (!strcmp((const char *) node->name, |
| 1538 | config_element_exclusions)) { |
| 1539 | xmlNodePtr exclusion_node; |
| 1540 | int exclusion_index = 0; |
| 1541 | |
| 1542 | /* exclusions */ |
| 1543 | if (exclusions) { |
| 1544 | /* |
| 1545 | * Exclusions has already been initialized, |
| 1546 | * invalid file. |
| 1547 | */ |
| 1548 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | |
| 1552 | exclusion_count = xmlChildElementCount(node); |
| 1553 | if (!exclusion_count) { |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
| 1557 | exclusions = zmalloc(exclusion_count * sizeof(char *)); |
| 1558 | if (!exclusions) { |
| 1559 | exclusion_count = 0; |
| 1560 | ret = -LTTNG_ERR_NOMEM; |
| 1561 | goto end; |
| 1562 | } |
| 1563 | |
| 1564 | for (exclusion_node = xmlFirstElementChild(node); exclusion_node; |
| 1565 | exclusion_node = xmlNextElementSibling(exclusion_node)) { |
| 1566 | xmlChar *content = |
| 1567 | xmlNodeGetContent(exclusion_node); |
| 1568 | |
| 1569 | if (!content) { |
| 1570 | ret = -LTTNG_ERR_NOMEM; |
| 1571 | goto end; |
| 1572 | } |
| 1573 | |
| 1574 | exclusions[exclusion_index] = strdup((const char *) content); |
| 1575 | free(content); |
| 1576 | if (!exclusions[exclusion_index]) { |
| 1577 | ret = -LTTNG_ERR_NOMEM; |
| 1578 | goto end; |
| 1579 | } |
| 1580 | exclusion_index++; |
| 1581 | } |
| 1582 | |
| 1583 | event.exclusion = 1; |
| 1584 | } else if (!strcmp((const char *) node->name, |
| 1585 | config_element_attributes)) { |
| 1586 | xmlNodePtr attribute_node = xmlFirstElementChild(node); |
| 1587 | |
| 1588 | /* attributes */ |
| 1589 | if (!attribute_node) { |
| 1590 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1591 | goto end; |
| 1592 | } |
| 1593 | |
| 1594 | if (!strcmp((const char *) node->name, |
| 1595 | config_element_probe_attributes)) { |
| 1596 | xmlNodePtr probe_attribute_node; |
| 1597 | |
| 1598 | /* probe_attributes */ |
| 1599 | for (probe_attribute_node = |
| 1600 | xmlFirstElementChild(attribute_node); probe_attribute_node; |
| 1601 | probe_attribute_node = xmlNextElementSibling( |
| 1602 | probe_attribute_node)) { |
| 1603 | |
| 1604 | ret = process_probe_attribute_node(probe_attribute_node, |
| 1605 | &event.attr.probe); |
| 1606 | if (ret) { |
| 1607 | goto end; |
| 1608 | } |
| 1609 | } |
| 1610 | } else { |
| 1611 | size_t sym_len; |
| 1612 | xmlChar *content; |
| 1613 | xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node); |
| 1614 | |
| 1615 | /* function_attributes */ |
| 1616 | content = xmlNodeGetContent(symbol_node); |
| 1617 | if (!content) { |
| 1618 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1619 | goto end; |
| 1620 | } |
| 1621 | |
| 1622 | sym_len = strlen((char *) content); |
| 1623 | if (sym_len >= LTTNG_SYMBOL_NAME_LEN) { |
| 1624 | WARN("Function name too long."); |
| 1625 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1626 | free(content); |
| 1627 | goto end; |
| 1628 | } |
| 1629 | |
| 1630 | strncpy(event.attr.ftrace.symbol_name, (char *) content, |
| 1631 | sym_len); |
| 1632 | free(content); |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | ret = lttng_enable_event_with_exclusions(handle, &event, channel_name, |
| 1638 | filter_expression, exclusion_count, exclusions); |
| 1639 | end: |
| 1640 | for (i = 0; i < exclusion_count; i++) { |
| 1641 | free(exclusions[i]); |
| 1642 | } |
| 1643 | |
| 1644 | free(exclusions); |
| 1645 | free(filter_expression); |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static |
| 1650 | int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle, |
| 1651 | const char *channel_name) |
| 1652 | { |
| 1653 | int ret = 0; |
| 1654 | xmlNodePtr node; |
| 1655 | |
| 1656 | assert(events_node); |
| 1657 | assert(handle); |
| 1658 | assert(channel_name); |
| 1659 | |
| 1660 | for (node = xmlFirstElementChild(events_node); node; |
| 1661 | node = xmlNextElementSibling(node)) { |
| 1662 | ret = process_event_node(node, handle, channel_name); |
| 1663 | if (ret) { |
| 1664 | goto end; |
| 1665 | } |
| 1666 | } |
| 1667 | end: |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | static |
| 1672 | int process_channel_attr_node(xmlNodePtr attr_node, |
| 1673 | struct lttng_channel *channel, xmlNodePtr *contexts_node, |
| 1674 | xmlNodePtr *events_node) |
| 1675 | { |
| 1676 | int ret; |
| 1677 | |
| 1678 | assert(attr_node); |
| 1679 | assert(channel); |
| 1680 | assert(contexts_node); |
| 1681 | assert(events_node); |
| 1682 | |
| 1683 | if (!strcmp((const char *) attr_node->name, config_element_name)) { |
| 1684 | xmlChar *content; |
| 1685 | size_t name_len; |
| 1686 | |
| 1687 | /* name */ |
| 1688 | content = xmlNodeGetContent(attr_node); |
| 1689 | if (!content) { |
| 1690 | ret = -LTTNG_ERR_NOMEM; |
| 1691 | goto end; |
| 1692 | } |
| 1693 | |
| 1694 | name_len = strlen((char *) content); |
| 1695 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { |
| 1696 | WARN("Channel name too long."); |
| 1697 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1698 | free(content); |
| 1699 | goto end; |
| 1700 | } |
| 1701 | |
| 1702 | strncpy(channel->name, (const char *) content, name_len); |
| 1703 | free(content); |
| 1704 | } else if (!strcmp((const char *) attr_node->name, |
| 1705 | config_element_enabled)) { |
| 1706 | xmlChar *content; |
| 1707 | int enabled; |
| 1708 | |
| 1709 | /* enabled */ |
| 1710 | content = xmlNodeGetContent(attr_node); |
| 1711 | if (!content) { |
| 1712 | ret = -LTTNG_ERR_NOMEM; |
| 1713 | goto end; |
| 1714 | } |
| 1715 | |
| 1716 | ret = parse_bool(content, &enabled); |
| 1717 | free(content); |
| 1718 | if (ret) { |
| 1719 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1720 | goto end; |
| 1721 | } |
| 1722 | |
| 1723 | channel->enabled = enabled; |
| 1724 | } else if (!strcmp((const char *) attr_node->name, |
| 1725 | config_element_overwrite_mode)) { |
| 1726 | xmlChar *content; |
| 1727 | |
| 1728 | /* overwrite_mode */ |
| 1729 | content = xmlNodeGetContent(attr_node); |
| 1730 | if (!content) { |
| 1731 | ret = -LTTNG_ERR_NOMEM; |
| 1732 | goto end; |
| 1733 | } |
| 1734 | |
| 1735 | ret = get_overwrite_mode(content); |
| 1736 | free(content); |
| 1737 | if (ret < 0) { |
| 1738 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1739 | goto end; |
| 1740 | } |
| 1741 | |
| 1742 | channel->attr.overwrite = ret; |
| 1743 | } else if (!strcmp((const char *) attr_node->name, |
| 1744 | config_element_subbuf_size)) { |
| 1745 | xmlChar *content; |
| 1746 | |
| 1747 | /* subbuffer_size */ |
| 1748 | content = xmlNodeGetContent(attr_node); |
| 1749 | if (!content) { |
| 1750 | ret = -LTTNG_ERR_NOMEM; |
| 1751 | goto end; |
| 1752 | } |
| 1753 | |
| 1754 | ret = parse_uint(content, &channel->attr.subbuf_size); |
| 1755 | free(content); |
| 1756 | if (ret) { |
| 1757 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1758 | goto end; |
| 1759 | } |
| 1760 | } else if (!strcmp((const char *) attr_node->name, |
| 1761 | config_element_num_subbuf)) { |
| 1762 | xmlChar *content; |
| 1763 | |
| 1764 | /* subbuffer_count */ |
| 1765 | content = xmlNodeGetContent(attr_node); |
| 1766 | if (!content) { |
| 1767 | ret = -LTTNG_ERR_NOMEM; |
| 1768 | goto end; |
| 1769 | } |
| 1770 | |
| 1771 | ret = parse_uint(content, &channel->attr.num_subbuf); |
| 1772 | free(content); |
| 1773 | if (ret) { |
| 1774 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1775 | goto end; |
| 1776 | } |
| 1777 | } else if (!strcmp((const char *) attr_node->name, |
| 1778 | config_element_switch_timer_interval)) { |
| 1779 | xmlChar *content; |
| 1780 | uint64_t switch_timer_interval = 0; |
| 1781 | |
| 1782 | /* switch_timer_interval */ |
| 1783 | content = xmlNodeGetContent(attr_node); |
| 1784 | if (!content) { |
| 1785 | ret = -LTTNG_ERR_NOMEM; |
| 1786 | goto end; |
| 1787 | } |
| 1788 | |
| 1789 | ret = parse_uint(content, &switch_timer_interval); |
| 1790 | free(content); |
| 1791 | if (ret) { |
| 1792 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1793 | goto end; |
| 1794 | } |
| 1795 | |
| 1796 | if (switch_timer_interval > UINT_MAX) { |
| 1797 | WARN("switch_timer_interval out of range."); |
| 1798 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1799 | goto end; |
| 1800 | } |
| 1801 | |
| 1802 | channel->attr.switch_timer_interval = |
| 1803 | switch_timer_interval; |
| 1804 | } else if (!strcmp((const char *) attr_node->name, |
| 1805 | config_element_read_timer_interval)) { |
| 1806 | xmlChar *content; |
| 1807 | uint64_t read_timer_interval = 0; |
| 1808 | |
| 1809 | /* read_timer_interval */ |
| 1810 | content = xmlNodeGetContent(attr_node); |
| 1811 | if (!content) { |
| 1812 | ret = -LTTNG_ERR_NOMEM; |
| 1813 | goto end; |
| 1814 | } |
| 1815 | |
| 1816 | ret = parse_uint(content, &read_timer_interval); |
| 1817 | free(content); |
| 1818 | if (ret) { |
| 1819 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1820 | goto end; |
| 1821 | } |
| 1822 | |
| 1823 | if (read_timer_interval > UINT_MAX) { |
| 1824 | WARN("read_timer_interval out of range."); |
| 1825 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1826 | goto end; |
| 1827 | } |
| 1828 | |
| 1829 | channel->attr.read_timer_interval = |
| 1830 | read_timer_interval; |
| 1831 | } else if (!strcmp((const char *) attr_node->name, |
| 1832 | config_element_output_type)) { |
| 1833 | xmlChar *content; |
| 1834 | |
| 1835 | /* output_type */ |
| 1836 | content = xmlNodeGetContent(attr_node); |
| 1837 | if (!content) { |
| 1838 | ret = -LTTNG_ERR_NOMEM; |
| 1839 | goto end; |
| 1840 | } |
| 1841 | |
| 1842 | ret = get_output_type(content); |
| 1843 | free(content); |
| 1844 | if (ret < 0) { |
| 1845 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1846 | goto end; |
| 1847 | } |
| 1848 | |
| 1849 | channel->attr.output = ret; |
| 1850 | } else if (!strcmp((const char *) attr_node->name, |
| 1851 | config_element_tracefile_size)) { |
| 1852 | xmlChar *content; |
| 1853 | |
| 1854 | /* tracefile_size */ |
| 1855 | content = xmlNodeGetContent(attr_node); |
| 1856 | if (!content) { |
| 1857 | ret = -LTTNG_ERR_NOMEM; |
| 1858 | goto end; |
| 1859 | } |
| 1860 | |
| 1861 | ret = parse_uint(content, &channel->attr.tracefile_size); |
| 1862 | free(content); |
| 1863 | if (ret) { |
| 1864 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1865 | goto end; |
| 1866 | } |
| 1867 | } else if (!strcmp((const char *) attr_node->name, |
| 1868 | config_element_tracefile_count)) { |
| 1869 | xmlChar *content; |
| 1870 | |
| 1871 | /* tracefile_count */ |
| 1872 | content = xmlNodeGetContent(attr_node); |
| 1873 | if (!content) { |
| 1874 | ret = -LTTNG_ERR_NOMEM; |
| 1875 | goto end; |
| 1876 | } |
| 1877 | |
| 1878 | ret = parse_uint(content, &channel->attr.tracefile_count); |
| 1879 | free(content); |
| 1880 | if (ret) { |
| 1881 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1882 | goto end; |
| 1883 | } |
| 1884 | } else if (!strcmp((const char *) attr_node->name, |
| 1885 | config_element_live_timer_interval)) { |
| 1886 | xmlChar *content; |
| 1887 | uint64_t live_timer_interval = 0; |
| 1888 | |
| 1889 | /* live_timer_interval */ |
| 1890 | content = xmlNodeGetContent(attr_node); |
| 1891 | if (!content) { |
| 1892 | ret = -LTTNG_ERR_NOMEM; |
| 1893 | goto end; |
| 1894 | } |
| 1895 | |
| 1896 | ret = parse_uint(content, &live_timer_interval); |
| 1897 | free(content); |
| 1898 | if (ret) { |
| 1899 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1900 | goto end; |
| 1901 | } |
| 1902 | |
| 1903 | if (live_timer_interval > UINT_MAX) { |
| 1904 | WARN("live_timer_interval out of range."); |
| 1905 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1906 | goto end; |
| 1907 | } |
| 1908 | |
| 1909 | channel->attr.live_timer_interval = |
| 1910 | live_timer_interval; |
| 1911 | } else if (!strcmp((const char *) attr_node->name, |
| 1912 | config_element_events)) { |
| 1913 | /* events */ |
| 1914 | *events_node = attr_node; |
| 1915 | } else { |
| 1916 | /* contexts */ |
| 1917 | *contexts_node = attr_node; |
| 1918 | } |
| 1919 | ret = 0; |
| 1920 | end: |
| 1921 | return ret; |
| 1922 | } |
| 1923 | |
| 1924 | static |
| 1925 | int process_context_node(xmlNodePtr context_node, |
| 1926 | struct lttng_handle *handle, const char *channel_name) |
| 1927 | { |
| 1928 | int ret; |
| 1929 | struct lttng_event_context context; |
| 1930 | xmlNodePtr context_child_node = xmlFirstElementChild(context_node); |
| 1931 | |
| 1932 | assert(handle); |
| 1933 | assert(channel_name); |
| 1934 | |
| 1935 | if (!context_child_node) { |
| 1936 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1937 | goto end; |
| 1938 | } |
| 1939 | |
| 1940 | memset(&context, 0, sizeof(context)); |
| 1941 | |
| 1942 | if (!strcmp((const char *) context_child_node->name, |
| 1943 | config_element_type)) { |
| 1944 | /* type */ |
| 1945 | xmlChar *content = xmlNodeGetContent(context_child_node); |
| 1946 | if (!content) { |
| 1947 | ret = -LTTNG_ERR_NOMEM; |
| 1948 | goto end; |
| 1949 | } |
| 1950 | |
| 1951 | ret = get_context_type(content); |
| 1952 | free(content); |
| 1953 | if (ret < 0) { |
| 1954 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1955 | goto end; |
| 1956 | } |
| 1957 | |
| 1958 | context.ctx = ret; |
| 1959 | } else { |
| 1960 | xmlNodePtr perf_attr_node; |
| 1961 | |
| 1962 | /* perf */ |
| 1963 | context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ? |
| 1964 | LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER : |
| 1965 | LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER; |
| 1966 | for (perf_attr_node = xmlFirstElementChild(context_child_node); |
| 1967 | perf_attr_node; perf_attr_node = |
| 1968 | xmlNextElementSibling(perf_attr_node)) { |
| 1969 | if (!strcmp((const char *) perf_attr_node->name, |
| 1970 | config_element_type)) { |
| 1971 | xmlChar *content; |
| 1972 | uint64_t type = 0; |
| 1973 | |
| 1974 | /* type */ |
| 1975 | content = xmlNodeGetContent(perf_attr_node); |
| 1976 | if (!content) { |
| 1977 | ret = -LTTNG_ERR_NOMEM; |
| 1978 | goto end; |
| 1979 | } |
| 1980 | |
| 1981 | ret = parse_uint(content, &type); |
| 1982 | free(content); |
| 1983 | if (ret) { |
| 1984 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1985 | goto end; |
| 1986 | } |
| 1987 | |
| 1988 | if (type > UINT32_MAX) { |
| 1989 | WARN("perf context type out of range."); |
| 1990 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 1991 | goto end; |
| 1992 | } |
| 1993 | |
| 1994 | context.u.perf_counter.type = type; |
| 1995 | } else if (!strcmp((const char *) perf_attr_node->name, |
| 1996 | config_element_config)) { |
| 1997 | xmlChar *content; |
| 1998 | uint64_t config = 0; |
| 1999 | |
| 2000 | /* config */ |
| 2001 | content = xmlNodeGetContent(perf_attr_node); |
| 2002 | if (!content) { |
| 2003 | ret = -LTTNG_ERR_NOMEM; |
| 2004 | goto end; |
| 2005 | } |
| 2006 | |
| 2007 | ret = parse_uint(content, &config); |
| 2008 | free(content); |
| 2009 | if (ret) { |
| 2010 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2011 | goto end; |
| 2012 | } |
| 2013 | |
| 2014 | context.u.perf_counter.config = config; |
| 2015 | } else if (!strcmp((const char *) perf_attr_node->name, |
| 2016 | config_element_name)) { |
| 2017 | xmlChar *content; |
| 2018 | size_t name_len; |
| 2019 | |
| 2020 | /* name */ |
| 2021 | content = xmlNodeGetContent(perf_attr_node); |
| 2022 | if (!content) { |
| 2023 | ret = -LTTNG_ERR_NOMEM; |
| 2024 | goto end; |
| 2025 | } |
| 2026 | |
| 2027 | name_len = strlen((char *) content); |
| 2028 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { |
| 2029 | WARN("perf context name too long."); |
| 2030 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2031 | free(content); |
| 2032 | goto end; |
| 2033 | } |
| 2034 | |
| 2035 | strncpy(context.u.perf_counter.name, (const char *) content, |
| 2036 | name_len); |
| 2037 | free(content); |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | ret = lttng_add_context(handle, &context, NULL, channel_name); |
| 2043 | end: |
| 2044 | return ret; |
| 2045 | } |
| 2046 | |
| 2047 | static |
| 2048 | int process_contexts_node(xmlNodePtr contexts_node, |
| 2049 | struct lttng_handle *handle, const char *channel_name) |
| 2050 | { |
| 2051 | int ret = 0; |
| 2052 | xmlNodePtr context_node; |
| 2053 | |
| 2054 | for (context_node = xmlFirstElementChild(contexts_node); context_node; |
| 2055 | context_node = xmlNextElementSibling(context_node)) { |
| 2056 | ret = process_context_node(context_node, handle, channel_name); |
| 2057 | if (ret) { |
| 2058 | goto end; |
| 2059 | } |
| 2060 | } |
| 2061 | end: |
| 2062 | return ret; |
| 2063 | } |
| 2064 | |
| 2065 | static |
| 2066 | int process_domain_node(xmlNodePtr domain_node, const char *session_name) |
| 2067 | { |
| 2068 | int ret; |
| 2069 | struct lttng_domain domain = { 0 }; |
| 2070 | struct lttng_handle *handle = NULL; |
| 2071 | xmlNodePtr channels_node = NULL; |
| 2072 | xmlNodePtr node; |
| 2073 | |
| 2074 | assert(session_name); |
| 2075 | |
| 2076 | ret = init_domain(domain_node, &domain); |
| 2077 | if (ret) { |
| 2078 | goto end; |
| 2079 | } |
| 2080 | |
| 2081 | handle = lttng_create_handle(session_name, &domain); |
| 2082 | if (!handle) { |
| 2083 | ret = -LTTNG_ERR_NOMEM; |
| 2084 | goto end; |
| 2085 | } |
| 2086 | |
| 2087 | /* get the channels node */ |
| 2088 | for (node = xmlFirstElementChild(domain_node); node; |
| 2089 | node = xmlNextElementSibling(node)) { |
| 2090 | if (!strcmp((const char *) node->name, |
| 2091 | config_element_channels)) { |
| 2092 | channels_node = node; |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | if (!channels_node) { |
| 2098 | goto end; |
| 2099 | } |
| 2100 | |
| 2101 | /* create all channels */ |
| 2102 | for (node = xmlFirstElementChild(channels_node); node; |
| 2103 | node = xmlNextElementSibling(node)) { |
| 2104 | struct lttng_channel channel; |
| 2105 | xmlNodePtr contexts_node = NULL; |
| 2106 | xmlNodePtr events_node = NULL; |
| 2107 | xmlNodePtr channel_attr_node; |
| 2108 | |
| 2109 | memset(&channel, 0, sizeof(channel)); |
| 2110 | lttng_channel_set_default_attr(&domain, &channel.attr); |
| 2111 | |
| 2112 | for (channel_attr_node = xmlFirstElementChild(node); |
| 2113 | channel_attr_node; channel_attr_node = |
| 2114 | xmlNextElementSibling(channel_attr_node)) { |
| 2115 | ret = process_channel_attr_node(channel_attr_node, |
| 2116 | &channel, &contexts_node, &events_node); |
| 2117 | if (ret) { |
| 2118 | goto end; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | ret = lttng_enable_channel(handle, &channel); |
| 2123 | if (ret < 0) { |
| 2124 | goto end; |
| 2125 | } |
| 2126 | |
| 2127 | ret = process_events_node(events_node, handle, channel.name); |
| 2128 | if (ret) { |
| 2129 | goto end; |
| 2130 | } |
| 2131 | |
| 2132 | ret = process_contexts_node(contexts_node, handle, |
| 2133 | channel.name); |
| 2134 | if (ret) { |
| 2135 | goto end; |
| 2136 | } |
| 2137 | } |
| 2138 | end: |
| 2139 | lttng_destroy_handle(handle); |
| 2140 | return ret; |
| 2141 | } |
| 2142 | |
| 2143 | static |
| 2144 | int process_session_node(xmlNodePtr session_node, const char *session_name, |
| 2145 | int override) |
| 2146 | { |
| 2147 | int ret, started = -1, snapshot_mode = -1; |
| 2148 | uint64_t live_timer_interval = UINT64_MAX; |
| 2149 | char *name = NULL; |
| 2150 | xmlNodePtr domains_node = NULL; |
| 2151 | xmlNodePtr output_node = NULL; |
| 2152 | xmlNodePtr node; |
| 2153 | struct lttng_domain *kernel_domain = NULL; |
| 2154 | struct lttng_domain *ust_domain = NULL; |
| 2155 | struct lttng_domain *jul_domain = NULL; |
| 2156 | struct lttng_domain *log4j_domain = NULL; |
| 2157 | |
| 2158 | for (node = xmlFirstElementChild(session_node); node; |
| 2159 | node = xmlNextElementSibling(node)) { |
| 2160 | if (!name && !strcmp((const char *) node->name, |
| 2161 | config_element_name)) { |
| 2162 | /* name */ |
| 2163 | xmlChar *node_content = xmlNodeGetContent(node); |
| 2164 | if (!node_content) { |
| 2165 | ret = -LTTNG_ERR_NOMEM; |
| 2166 | goto error; |
| 2167 | } |
| 2168 | |
| 2169 | name = (char *) node_content; |
| 2170 | } else if (!domains_node && !strcmp((const char *) node->name, |
| 2171 | config_element_domains)) { |
| 2172 | /* domains */ |
| 2173 | domains_node = node; |
| 2174 | } else if (started == -1 && !strcmp((const char *) node->name, |
| 2175 | config_element_started)) { |
| 2176 | /* started */ |
| 2177 | xmlChar *node_content = xmlNodeGetContent(node); |
| 2178 | if (!node_content) { |
| 2179 | ret = -LTTNG_ERR_NOMEM; |
| 2180 | goto error; |
| 2181 | } |
| 2182 | |
| 2183 | ret = parse_bool(node_content, &started); |
| 2184 | free(node_content); |
| 2185 | if (ret) { |
| 2186 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2187 | goto error; |
| 2188 | } |
| 2189 | } else if (!output_node && !strcmp((const char *) node->name, |
| 2190 | config_element_output)) { |
| 2191 | /* output */ |
| 2192 | output_node = node; |
| 2193 | } else { |
| 2194 | /* attributes, snapshot_mode or live_timer_interval */ |
| 2195 | xmlNodePtr attributes_child = |
| 2196 | xmlFirstElementChild(node); |
| 2197 | |
| 2198 | if (!strcmp((const char *) attributes_child->name, |
| 2199 | config_element_snapshot_mode)) { |
| 2200 | /* snapshot_mode */ |
| 2201 | xmlChar *snapshot_mode_content = |
| 2202 | xmlNodeGetContent(attributes_child); |
| 2203 | if (!snapshot_mode_content) { |
| 2204 | ret = -LTTNG_ERR_NOMEM; |
| 2205 | goto error; |
| 2206 | } |
| 2207 | |
| 2208 | ret = parse_bool(snapshot_mode_content, &snapshot_mode); |
| 2209 | free(snapshot_mode_content); |
| 2210 | if (ret) { |
| 2211 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2212 | goto error; |
| 2213 | } |
| 2214 | } else { |
| 2215 | /* live_timer_interval */ |
| 2216 | xmlChar *timer_interval_content = |
| 2217 | xmlNodeGetContent(attributes_child); |
| 2218 | if (!timer_interval_content) { |
| 2219 | ret = -LTTNG_ERR_NOMEM; |
| 2220 | goto error; |
| 2221 | } |
| 2222 | |
| 2223 | ret = parse_uint(timer_interval_content, &live_timer_interval); |
| 2224 | free(timer_interval_content); |
| 2225 | if (ret) { |
| 2226 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2227 | goto error; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | if (!name) { |
| 2234 | /* Mandatory attribute, as defined in the session XSD */ |
| 2235 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2236 | goto error; |
| 2237 | } |
| 2238 | |
| 2239 | if (session_name && strcmp(name, session_name)) { |
| 2240 | /* This is not the session we are looking for */ |
| 2241 | ret = -LTTNG_ERR_NO_SESSION; |
| 2242 | goto error; |
| 2243 | } |
| 2244 | |
| 2245 | /* Init domains to create the session handles */ |
| 2246 | for (node = xmlFirstElementChild(domains_node); node; |
| 2247 | node = xmlNextElementSibling(node)) { |
| 2248 | struct lttng_domain *domain; |
| 2249 | |
| 2250 | domain = zmalloc(sizeof(*domain)); |
| 2251 | if (!domain) { |
| 2252 | ret = -LTTNG_ERR_NOMEM; |
| 2253 | goto error; |
| 2254 | } |
| 2255 | |
| 2256 | ret = init_domain(node, domain); |
| 2257 | if (ret) { |
| 2258 | goto domain_init_error; |
| 2259 | } |
| 2260 | |
| 2261 | switch (domain->type) { |
| 2262 | case LTTNG_DOMAIN_KERNEL: |
| 2263 | if (kernel_domain) { |
| 2264 | /* Same domain seen twice, invalid! */ |
| 2265 | goto domain_init_error; |
| 2266 | } |
| 2267 | kernel_domain = domain; |
| 2268 | break; |
| 2269 | case LTTNG_DOMAIN_UST: |
| 2270 | if (ust_domain) { |
| 2271 | /* Same domain seen twice, invalid! */ |
| 2272 | goto domain_init_error; |
| 2273 | } |
| 2274 | ust_domain = domain; |
| 2275 | break; |
| 2276 | case LTTNG_DOMAIN_JUL: |
| 2277 | if (jul_domain) { |
| 2278 | /* Same domain seen twice, invalid! */ |
| 2279 | goto domain_init_error; |
| 2280 | } |
| 2281 | jul_domain = domain; |
| 2282 | break; |
| 2283 | case LTTNG_DOMAIN_LOG4J: |
| 2284 | if (log4j_domain) { |
| 2285 | /* Same domain seen twice, invalid! */ |
| 2286 | goto domain_init_error; |
| 2287 | } |
| 2288 | log4j_domain = domain; |
| 2289 | break; |
| 2290 | default: |
| 2291 | WARN("Invalid domain type"); |
| 2292 | goto domain_init_error; |
| 2293 | } |
| 2294 | continue; |
| 2295 | domain_init_error: |
| 2296 | free(domain); |
| 2297 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2298 | goto error; |
| 2299 | } |
| 2300 | |
| 2301 | if (override) { |
| 2302 | /* Destroy session if it exists */ |
| 2303 | ret = lttng_destroy_session(name); |
| 2304 | if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) { |
| 2305 | ERR("Failed to destroy existing session."); |
| 2306 | goto error; |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | /* Create session type depending on output type */ |
| 2311 | if (snapshot_mode && snapshot_mode != -1) { |
| 2312 | ret = create_snapshot_session(name, output_node); |
| 2313 | } else if (live_timer_interval && |
| 2314 | live_timer_interval != UINT64_MAX) { |
| 2315 | ret = create_session(name, kernel_domain, ust_domain, jul_domain, |
| 2316 | log4j_domain, output_node, live_timer_interval); |
| 2317 | } else { |
| 2318 | /* regular session */ |
| 2319 | ret = create_session(name, kernel_domain, ust_domain, jul_domain, |
| 2320 | log4j_domain, output_node, UINT64_MAX); |
| 2321 | } |
| 2322 | if (ret) { |
| 2323 | goto error; |
| 2324 | } |
| 2325 | |
| 2326 | for (node = xmlFirstElementChild(domains_node); node; |
| 2327 | node = xmlNextElementSibling(node)) { |
| 2328 | ret = process_domain_node(node, name); |
| 2329 | if (ret) { |
| 2330 | goto end; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | if (started) { |
| 2335 | ret = lttng_start_tracing(name); |
| 2336 | if (ret) { |
| 2337 | goto end; |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | end: |
| 2342 | if (ret < 0) { |
| 2343 | ERR("Failed to load session %s: %s", name, lttng_strerror(ret)); |
| 2344 | lttng_destroy_session(name); |
| 2345 | } |
| 2346 | |
| 2347 | error: |
| 2348 | free(kernel_domain); |
| 2349 | free(ust_domain); |
| 2350 | free(jul_domain); |
| 2351 | free(log4j_domain); |
| 2352 | free(name); |
| 2353 | return ret; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * Return 1 if the given path is readable by the current UID or 0 if not. |
| 2358 | * Return -1 if the path is EPERM. |
| 2359 | */ |
| 2360 | static int validate_file_read_creds(const char *path) |
| 2361 | { |
| 2362 | int ret; |
| 2363 | |
| 2364 | assert(path); |
| 2365 | |
| 2366 | /* Can we read the file. */ |
| 2367 | ret = access(path, R_OK); |
| 2368 | if (!ret) { |
| 2369 | goto valid; |
| 2370 | } |
| 2371 | if (errno == EACCES) { |
| 2372 | return -1; |
| 2373 | } else { |
| 2374 | /* Invalid. */ |
| 2375 | return 0; |
| 2376 | } |
| 2377 | valid: |
| 2378 | return 1; |
| 2379 | } |
| 2380 | |
| 2381 | static |
| 2382 | int load_session_from_file(const char *path, const char *session_name, |
| 2383 | struct session_config_validation_ctx *validation_ctx, int override) |
| 2384 | { |
| 2385 | int ret, session_found = !session_name; |
| 2386 | xmlDocPtr doc = NULL; |
| 2387 | xmlNodePtr sessions_node; |
| 2388 | xmlNodePtr session_node; |
| 2389 | |
| 2390 | assert(path); |
| 2391 | assert(validation_ctx); |
| 2392 | |
| 2393 | ret = validate_file_read_creds(path); |
| 2394 | if (ret != 1) { |
| 2395 | if (ret == -1) { |
| 2396 | ret = -LTTNG_ERR_EPERM; |
| 2397 | } else { |
| 2398 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; |
| 2399 | } |
| 2400 | goto end; |
| 2401 | } |
| 2402 | |
| 2403 | doc = xmlParseFile(path); |
| 2404 | if (!doc) { |
| 2405 | ret = -LTTNG_ERR_LOAD_IO_FAIL; |
| 2406 | goto end; |
| 2407 | } |
| 2408 | |
| 2409 | ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc); |
| 2410 | if (ret) { |
| 2411 | ERR("Session configuration file validation failed"); |
| 2412 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
| 2413 | goto end; |
| 2414 | } |
| 2415 | |
| 2416 | sessions_node = xmlDocGetRootElement(doc); |
| 2417 | if (!sessions_node) { |
| 2418 | goto end; |
| 2419 | } |
| 2420 | |
| 2421 | for (session_node = xmlFirstElementChild(sessions_node); |
| 2422 | session_node; session_node = |
| 2423 | xmlNextElementSibling(session_node)) { |
| 2424 | ret = process_session_node(session_node, |
| 2425 | session_name, override); |
| 2426 | if (session_name && ret == 0) { |
| 2427 | /* Target session found and loaded */ |
| 2428 | session_found = 1; |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | end: |
| 2433 | xmlFreeDoc(doc); |
| 2434 | if (!ret) { |
| 2435 | ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT; |
| 2436 | } |
| 2437 | return ret; |
| 2438 | } |
| 2439 | |
| 2440 | static |
| 2441 | int load_session_from_path(const char *path, const char *session_name, |
| 2442 | struct session_config_validation_ctx *validation_ctx, int override) |
| 2443 | { |
| 2444 | int ret, session_found = !session_name; |
| 2445 | DIR *directory = NULL; |
| 2446 | |
| 2447 | assert(path); |
| 2448 | assert(validation_ctx); |
| 2449 | |
| 2450 | directory = opendir(path); |
| 2451 | if (!directory) { |
| 2452 | switch (errno) { |
| 2453 | case ENOTDIR: |
| 2454 | /* Try the file loading. */ |
| 2455 | break; |
| 2456 | case ENOENT: |
| 2457 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; |
| 2458 | goto end; |
| 2459 | default: |
| 2460 | ret = -LTTNG_ERR_LOAD_IO_FAIL; |
| 2461 | goto end; |
| 2462 | } |
| 2463 | } |
| 2464 | if (directory) { |
| 2465 | struct dirent *entry; |
| 2466 | struct dirent *result; |
| 2467 | char *file_path = NULL; |
| 2468 | size_t path_len = strlen(path); |
| 2469 | |
| 2470 | if (path_len >= PATH_MAX) { |
| 2471 | ret = -LTTNG_ERR_INVALID; |
| 2472 | goto end; |
| 2473 | } |
| 2474 | |
| 2475 | entry = zmalloc(sizeof(*entry)); |
| 2476 | if (!entry) { |
| 2477 | ret = -LTTNG_ERR_NOMEM; |
| 2478 | goto end; |
| 2479 | } |
| 2480 | |
| 2481 | file_path = zmalloc(PATH_MAX); |
| 2482 | if (!file_path) { |
| 2483 | ret = -LTTNG_ERR_NOMEM; |
| 2484 | free(entry); |
| 2485 | goto end; |
| 2486 | } |
| 2487 | |
| 2488 | strncpy(file_path, path, path_len); |
| 2489 | if (file_path[path_len - 1] != '/') { |
| 2490 | file_path[path_len++] = '/'; |
| 2491 | } |
| 2492 | |
| 2493 | ret = 0; |
| 2494 | /* Search for *.lttng files */ |
| 2495 | while (!readdir_r(directory, entry, &result) && result) { |
| 2496 | size_t file_name_len = strlen(result->d_name); |
| 2497 | |
| 2498 | if (file_name_len <= |
| 2499 | sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) { |
| 2500 | continue; |
| 2501 | } |
| 2502 | |
| 2503 | if (path_len + file_name_len >= PATH_MAX) { |
| 2504 | continue; |
| 2505 | } |
| 2506 | |
| 2507 | if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION, |
| 2508 | result->d_name + file_name_len - sizeof( |
| 2509 | DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { |
| 2510 | continue; |
| 2511 | } |
| 2512 | |
| 2513 | strncpy(file_path + path_len, result->d_name, file_name_len); |
| 2514 | file_path[path_len + file_name_len] = '\0'; |
| 2515 | |
| 2516 | ret = load_session_from_file(file_path, session_name, |
| 2517 | validation_ctx, override); |
| 2518 | if (session_name && !ret) { |
| 2519 | session_found = 1; |
| 2520 | break; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | free(entry); |
| 2525 | free(file_path); |
| 2526 | } else { |
| 2527 | ret = load_session_from_file(path, session_name, |
| 2528 | validation_ctx, override); |
| 2529 | if (ret) { |
| 2530 | goto end; |
| 2531 | } else { |
| 2532 | session_found = 1; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | end: |
| 2537 | if (directory) { |
| 2538 | if (closedir(directory)) { |
| 2539 | PERROR("closedir"); |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | if (!session_found) { |
| 2544 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; |
| 2545 | } |
| 2546 | |
| 2547 | return ret; |
| 2548 | } |
| 2549 | |
| 2550 | /* |
| 2551 | * Validate that the given path's credentials and the current process have the |
| 2552 | * same UID. If so, return 1 else return 0 if it does NOT match. |
| 2553 | */ |
| 2554 | static int validate_path_creds(const char *path) |
| 2555 | { |
| 2556 | int ret, uid = getuid(); |
| 2557 | struct stat buf; |
| 2558 | |
| 2559 | assert(path); |
| 2560 | |
| 2561 | if (uid == 0) { |
| 2562 | goto valid; |
| 2563 | } |
| 2564 | |
| 2565 | ret = stat(path, &buf); |
| 2566 | if (ret < 0) { |
| 2567 | if (errno != ENOENT) { |
| 2568 | PERROR("stat"); |
| 2569 | } |
| 2570 | ret = -LTTNG_ERR_INVALID; |
| 2571 | goto valid; |
| 2572 | } |
| 2573 | |
| 2574 | if (buf.st_uid != uid) { |
| 2575 | goto invalid; |
| 2576 | } |
| 2577 | |
| 2578 | valid: |
| 2579 | return 1; |
| 2580 | invalid: |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
| 2584 | LTTNG_HIDDEN |
| 2585 | int config_load_session(const char *path, const char *session_name, |
| 2586 | int override, unsigned int autoload) |
| 2587 | { |
| 2588 | int ret; |
| 2589 | const char *path_ptr = NULL; |
| 2590 | struct session_config_validation_ctx validation_ctx = { 0 }; |
| 2591 | |
| 2592 | ret = init_session_config_validation_ctx(&validation_ctx); |
| 2593 | if (ret) { |
| 2594 | goto end; |
| 2595 | } |
| 2596 | |
| 2597 | if (!path) { |
| 2598 | char *home_path; |
| 2599 | const char *sys_path; |
| 2600 | |
| 2601 | /* Try home path */ |
| 2602 | home_path = utils_get_home_dir(); |
| 2603 | if (home_path) { |
| 2604 | char path[PATH_MAX]; |
| 2605 | |
| 2606 | /* |
| 2607 | * Try user session configuration path. Ignore error here so we can |
| 2608 | * continue loading the system wide sessions. |
| 2609 | */ |
| 2610 | if (autoload) { |
| 2611 | ret = snprintf(path, sizeof(path), |
| 2612 | DEFAULT_SESSION_HOME_CONFIGPATH "/" |
| 2613 | DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path); |
| 2614 | if (ret < 0) { |
| 2615 | PERROR("snprintf session autoload home config path"); |
| 2616 | goto end; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
| 2620 | * Credentials are only validated for the autoload in order to |
| 2621 | * avoid any user session daemon to try to load kernel sessions |
| 2622 | * automatically and failing all the times. |
| 2623 | */ |
| 2624 | ret = validate_path_creds(path); |
| 2625 | if (ret) { |
| 2626 | path_ptr = path; |
| 2627 | } |
| 2628 | } else { |
| 2629 | ret = snprintf(path, sizeof(path), |
| 2630 | DEFAULT_SESSION_HOME_CONFIGPATH, home_path); |
| 2631 | if (ret < 0) { |
| 2632 | PERROR("snprintf session home config path"); |
| 2633 | goto end; |
| 2634 | } |
| 2635 | path_ptr = path; |
| 2636 | } |
| 2637 | if (path_ptr) { |
| 2638 | ret = load_session_from_path(path_ptr, session_name, |
| 2639 | &validation_ctx, override); |
| 2640 | if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) { |
| 2641 | goto end; |
| 2642 | } |
| 2643 | /* |
| 2644 | * Continue even if the session was found since we have to try |
| 2645 | * the system wide sessions. |
| 2646 | */ |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | /* Reset path pointer for the system wide dir. */ |
| 2651 | path_ptr = NULL; |
| 2652 | |
| 2653 | /* Try system wide configuration directory. */ |
| 2654 | if (autoload) { |
| 2655 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/" |
| 2656 | DEFAULT_SESSION_CONFIG_AUTOLOAD; |
| 2657 | ret = validate_path_creds(sys_path); |
| 2658 | if (ret) { |
| 2659 | path_ptr = sys_path; |
| 2660 | } |
| 2661 | } else { |
| 2662 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH; |
| 2663 | path_ptr = sys_path; |
| 2664 | } |
| 2665 | |
| 2666 | if (path_ptr) { |
| 2667 | ret = load_session_from_path(path_ptr, session_name, |
| 2668 | &validation_ctx, override); |
| 2669 | } |
| 2670 | } else { |
| 2671 | ret = access(path, F_OK); |
| 2672 | if (ret < 0) { |
| 2673 | PERROR("access"); |
| 2674 | switch (errno) { |
| 2675 | case ENOENT: |
| 2676 | ret = -LTTNG_ERR_INVALID; |
| 2677 | WARN("Session configuration path does not exist."); |
| 2678 | break; |
| 2679 | case EACCES: |
| 2680 | ret = -LTTNG_ERR_EPERM; |
| 2681 | break; |
| 2682 | default: |
| 2683 | ret = -LTTNG_ERR_UNK; |
| 2684 | break; |
| 2685 | } |
| 2686 | goto end; |
| 2687 | } |
| 2688 | |
| 2689 | ret = load_session_from_path(path, session_name, |
| 2690 | &validation_ctx, override); |
| 2691 | } |
| 2692 | end: |
| 2693 | fini_session_config_validation_ctx(&validation_ctx); |
| 2694 | if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) { |
| 2695 | /* |
| 2696 | * Don't report an error if no sessions are found when called |
| 2697 | * without a session_name or a search path. |
| 2698 | */ |
| 2699 | ret = 0; |
| 2700 | } |
| 2701 | return ret; |
| 2702 | } |