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