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