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