739627ab1757af30884a762f93da61252f54e725
[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 <libxml/xpath.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.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 const char * const config_element_output = "output";
96 const char * const config_element_output_type = "output_type";
97 const char * const config_element_tracefile_size = "tracefile_size";
98 const char * const config_element_tracefile_count = "tracefile_count";
99 const char * const config_element_live_timer_interval = "live_timer_interval";
100 LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events";
101 LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets";
102 const char * const config_element_type = "type";
103 const char * const config_element_buffer_type = "buffer_type";
104 const char * const config_element_session = "session";
105 const char * const config_element_sessions = "sessions";
106 LTTNG_HIDDEN const char * const config_element_context_perf = "perf";
107 LTTNG_HIDDEN const char * const config_element_context_app = "app";
108 LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name";
109 LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name";
110 const char * const config_element_config = "config";
111 const char * const config_element_started = "started";
112 const char * const config_element_snapshot_mode = "snapshot_mode";
113 const char * const config_element_loglevel = "loglevel";
114 const char * const config_element_loglevel_type = "loglevel_type";
115 const char * const config_element_filter = "filter";
116 LTTNG_HIDDEN const char * const config_element_filter_expression = "filter";
117 const char * const config_element_snapshot_outputs = "snapshot_outputs";
118 const char * const config_element_consumer_output = "consumer_output";
119 const char * const config_element_destination = "destination";
120 const char * const config_element_path = "path";
121 const char * const config_element_net_output = "net_output";
122 const char * const config_element_control_uri = "control_uri";
123 const char * const config_element_data_uri = "data_uri";
124 const char * const config_element_max_size = "max_size";
125 const char * const config_element_pid = "pid";
126 const char * const config_element_pids = "pids";
127 const char * const config_element_shared_memory_path = "shared_memory_path";
128 const char * const config_element_pid_tracker = "pid_tracker";
129 const char * const config_element_trackers = "trackers";
130 const char * const config_element_targets = "targets";
131 const char * const config_element_target_pid = "pid_target";
132 const char * const config_element_omit_name = "omit_name";
133 const char * const config_element_omit_output = "omit_output";
134
135 const char * const config_domain_type_kernel = "KERNEL";
136 const char * const config_domain_type_ust = "UST";
137 const char * const config_domain_type_jul = "JUL";
138 const char * const config_domain_type_log4j = "LOG4J";
139 const char * const config_domain_type_python = "PYTHON";
140
141 const char * const config_buffer_type_per_pid = "PER_PID";
142 const char * const config_buffer_type_per_uid = "PER_UID";
143 const char * const config_buffer_type_global = "GLOBAL";
144
145 const char * const config_overwrite_mode_discard = "DISCARD";
146 const char * const config_overwrite_mode_overwrite = "OVERWRITE";
147
148 const char * const config_output_type_splice = "SPLICE";
149 const char * const config_output_type_mmap = "MMAP";
150
151 const char * const config_loglevel_type_all = "ALL";
152 const char * const config_loglevel_type_range = "RANGE";
153 const char * const config_loglevel_type_single = "SINGLE";
154
155 const char * const config_event_type_all = "ALL";
156 const char * const config_event_type_tracepoint = "TRACEPOINT";
157 const char * const config_event_type_probe = "PROBE";
158 const char * const config_event_type_function = "FUNCTION";
159 const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
160 const char * const config_event_type_noop = "NOOP";
161 const char * const config_event_type_syscall = "SYSCALL";
162 const char * const config_event_type_kprobe = "KPROBE";
163 const char * const config_event_type_kretprobe = "KRETPROBE";
164
165 const char * const config_event_context_pid = "PID";
166 const char * const config_event_context_procname = "PROCNAME";
167 const char * const config_event_context_prio = "PRIO";
168 const char * const config_event_context_nice = "NICE";
169 const char * const config_event_context_vpid = "VPID";
170 const char * const config_event_context_tid = "TID";
171 const char * const config_event_context_vtid = "VTID";
172 const char * const config_event_context_ppid = "PPID";
173 const char * const config_event_context_vppid = "VPPID";
174 const char * const config_event_context_pthread_id = "PTHREAD_ID";
175 const char * const config_event_context_hostname = "HOSTNAME";
176 const char * const config_event_context_ip = "IP";
177 const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER";
178 LTTNG_HIDDEN const char * const config_event_context_app = "APP";
179 LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE";
180 LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
181 LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
182 LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
183
184 /* Deprecated symbols */
185 const char * const config_element_perf;
186
187 enum process_event_node_phase {
188 CREATION = 0,
189 ENABLE = 1,
190 };
191
192 struct consumer_output {
193 int enabled;
194 char *path;
195 char *control_uri;
196 char *data_uri;
197 };
198
199 static int config_entry_handler_filter(struct handler_filter_args *args,
200 const char *section, const char *name, const char *value)
201 {
202 int ret = 0;
203 struct config_entry entry = { section, name, value };
204
205 assert(args);
206
207 if (!section || !name || !value) {
208 ret = -EIO;
209 goto end;
210 }
211
212 if (args->section) {
213 if (strcmp(args->section, section)) {
214 goto end;
215 }
216 }
217
218 ret = args->handler(&entry, args->user_data);
219 end:
220 return ret;
221 }
222
223 LTTNG_HIDDEN
224 const char *config_get_domain_str(enum lttng_domain_type domain)
225 {
226 const char *str_dom;
227
228 switch (domain) {
229 case LTTNG_DOMAIN_KERNEL:
230 str_dom = config_domain_type_kernel;
231 break;
232 case LTTNG_DOMAIN_UST:
233 str_dom = config_domain_type_ust;
234 break;
235 case LTTNG_DOMAIN_JUL:
236 str_dom = config_domain_type_jul;
237 break;
238 case LTTNG_DOMAIN_LOG4J:
239 str_dom = config_domain_type_log4j;
240 break;
241 case LTTNG_DOMAIN_PYTHON:
242 str_dom = config_domain_type_python;
243 break;
244 default:
245 assert(0);
246 }
247
248 return str_dom;
249 }
250
251 LTTNG_HIDDEN
252 const char *config_get_loglevel_type_string(
253 enum lttng_loglevel_type loglevel_type)
254 {
255 const char *loglevel_type_string;
256
257 switch (loglevel_type) {
258 case LTTNG_EVENT_LOGLEVEL_ALL:
259 loglevel_type_string = config_loglevel_type_all;
260 break;
261 case LTTNG_EVENT_LOGLEVEL_RANGE:
262 loglevel_type_string = config_loglevel_type_range;
263 break;
264 case LTTNG_EVENT_LOGLEVEL_SINGLE:
265 loglevel_type_string = config_loglevel_type_single;
266 break;
267 default:
268 loglevel_type_string = NULL;
269 }
270
271 return loglevel_type_string;
272 }
273
274 LTTNG_HIDDEN
275 int config_get_section_entries(const char *override_path, const char *section,
276 config_entry_handler_cb handler, void *user_data)
277 {
278 int ret = 0;
279 char *path;
280 FILE *config_file = NULL;
281 struct handler_filter_args filter = { section, handler, user_data };
282
283 /* First, try system-wide conf. file. */
284 path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH;
285
286 config_file = fopen(path, "r");
287 if (config_file) {
288 DBG("Loading daemon conf file at %s", path);
289 /*
290 * Return value is not very important here since error or not, we
291 * continue and try the next possible conf. file.
292 */
293 (void) ini_parse_file(config_file,
294 (ini_entry_handler) config_entry_handler_filter,
295 (void *) &filter);
296 fclose(config_file);
297 }
298
299 /* Second is the user local configuration. */
300 path = utils_get_home_dir();
301 if (path) {
302 char fullpath[PATH_MAX];
303
304 ret = snprintf(fullpath, sizeof(fullpath),
305 DEFAULT_DAEMON_HOME_CONFIGPATH, path);
306 if (ret < 0) {
307 PERROR("snprintf user conf. path");
308 goto error;
309 }
310
311 config_file = fopen(fullpath, "r");
312 if (config_file) {
313 DBG("Loading daemon user conf file at %s", path);
314 /*
315 * Return value is not very important here since error or not, we
316 * continue and try the next possible conf. file.
317 */
318 (void) ini_parse_file(config_file,
319 (ini_entry_handler) config_entry_handler_filter,
320 (void *) &filter);
321 fclose(config_file);
322 }
323 }
324
325 /* Final path is the one that the user might have provided. */
326 if (override_path) {
327 config_file = fopen(override_path, "r");
328 if (config_file) {
329 DBG("Loading daemon command line conf file at %s", override_path);
330 (void) ini_parse_file(config_file,
331 (ini_entry_handler) config_entry_handler_filter,
332 (void *) &filter);
333 fclose(config_file);
334 } else {
335 ERR("Failed to open daemon configuration file at %s",
336 override_path);
337 ret = -ENOENT;
338 goto error;
339 }
340 }
341
342 /* Everything went well. */
343 ret = 0;
344
345 error:
346 return ret;
347 }
348
349 LTTNG_HIDDEN
350 int config_parse_value(const char *value)
351 {
352 int i, ret = 0;
353 char *endptr, *lower_str;
354 size_t len;
355 unsigned long v;
356
357 len = strlen(value);
358 if (!len) {
359 ret = -1;
360 goto end;
361 }
362
363 v = strtoul(value, &endptr, 10);
364 if (endptr != value) {
365 ret = v;
366 goto end;
367 }
368
369 lower_str = zmalloc(len + 1);
370 if (!lower_str) {
371 PERROR("zmalloc");
372 ret = -errno;
373 goto end;
374 }
375
376 for (i = 0; i < len; i++) {
377 lower_str[i] = tolower(value[i]);
378 }
379
380 if (!strcmp(lower_str, config_str_yes) ||
381 !strcmp(lower_str, config_str_true) ||
382 !strcmp(lower_str, config_str_on)) {
383 ret = 1;
384 } else if (!strcmp(lower_str, config_str_no) ||
385 !strcmp(lower_str, config_str_false) ||
386 !strcmp(lower_str, config_str_off)) {
387 ret = 0;
388 } else {
389 ret = -1;
390 }
391
392 free(lower_str);
393 end:
394 return ret;
395 }
396
397 /*
398 * Returns a xmlChar string which must be released using xmlFree().
399 */
400 static xmlChar *encode_string(const char *in_str)
401 {
402 xmlChar *out_str = NULL;
403 xmlCharEncodingHandlerPtr handler;
404 int out_len, ret, in_len;
405
406 assert(in_str);
407
408 handler = xmlFindCharEncodingHandler(config_xml_encoding);
409 if (!handler) {
410 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
411 goto end;
412 }
413
414 in_len = strlen(in_str);
415 /*
416 * Add 1 byte for the NULL terminted character. The factor 4 here is
417 * used because UTF-8 characters can take up to 4 bytes.
418 */
419 out_len = (in_len * 4) + 1;
420 out_str = xmlMalloc(out_len);
421 if (!out_str) {
422 goto end;
423 }
424
425 ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len);
426 if (ret < 0) {
427 xmlFree(out_str);
428 out_str = NULL;
429 goto end;
430 }
431
432 /* out_len is now the size of out_str */
433 out_str[out_len] = '\0';
434 end:
435 return out_str;
436 }
437
438 LTTNG_HIDDEN
439 struct config_writer *config_writer_create(int fd_output, int indent)
440 {
441 int ret;
442 struct config_writer *writer;
443 xmlOutputBufferPtr buffer;
444
445 writer = zmalloc(sizeof(struct config_writer));
446 if (!writer) {
447 PERROR("zmalloc config_writer_create");
448 goto end;
449 }
450
451 buffer = xmlOutputBufferCreateFd(fd_output, NULL);
452 if (!buffer) {
453 goto error_destroy;
454 }
455
456 writer->writer = xmlNewTextWriter(buffer);
457 ret = xmlTextWriterStartDocument(writer->writer, NULL,
458 config_xml_encoding, NULL);
459 if (ret < 0) {
460 goto error_destroy;
461 }
462
463 ret = xmlTextWriterSetIndentString(writer->writer,
464 BAD_CAST config_xml_indent_string);
465 if (ret) {
466 goto error_destroy;
467 }
468
469 ret = xmlTextWriterSetIndent(writer->writer, indent);
470 if (ret) {
471 goto error_destroy;
472 }
473
474 end:
475 return writer;
476 error_destroy:
477 config_writer_destroy(writer);
478 return NULL;
479 }
480
481 LTTNG_HIDDEN
482 int config_writer_destroy(struct config_writer *writer)
483 {
484 int ret = 0;
485
486 if (!writer) {
487 ret = -EINVAL;
488 goto end;
489 }
490
491 if (xmlTextWriterEndDocument(writer->writer) < 0) {
492 WARN("Could not close XML document");
493 ret = -EIO;
494 }
495
496 if (writer->writer) {
497 xmlFreeTextWriter(writer->writer);
498 }
499
500 free(writer);
501 end:
502 return ret;
503 }
504
505 LTTNG_HIDDEN
506 int config_writer_open_element(struct config_writer *writer,
507 const char *element_name)
508 {
509 int ret;
510 xmlChar *encoded_element_name;
511
512 if (!writer || !writer->writer || !element_name || !element_name[0]) {
513 ret = -1;
514 goto end;
515 }
516
517 encoded_element_name = encode_string(element_name);
518 if (!encoded_element_name) {
519 ret = -1;
520 goto end;
521 }
522
523 ret = xmlTextWriterStartElement(writer->writer, encoded_element_name);
524 xmlFree(encoded_element_name);
525 end:
526 return ret >= 0 ? 0 : ret;
527 }
528
529 LTTNG_HIDDEN
530 int config_writer_write_attribute(struct config_writer *writer,
531 const char *name, const char *value)
532 {
533 int ret;
534 xmlChar *encoded_name = NULL;
535 xmlChar *encoded_value = NULL;
536
537 if (!writer || !writer->writer || !name || !name[0]) {
538 ret = -1;
539 goto end;
540 }
541
542 encoded_name = encode_string(name);
543 if (!encoded_name) {
544 ret = -1;
545 goto end;
546 }
547
548 encoded_value = encode_string(value);
549 if (!encoded_value) {
550 ret = -1;
551 goto end;
552 }
553
554 ret = xmlTextWriterWriteAttribute(writer->writer, encoded_name,
555 encoded_value);
556 end:
557 xmlFree(encoded_name);
558 xmlFree(encoded_value);
559 return ret >= 0 ? 0 : ret;
560 }
561
562 LTTNG_HIDDEN
563 int config_writer_close_element(struct config_writer *writer)
564 {
565 int ret;
566
567 if (!writer || !writer->writer) {
568 ret = -1;
569 goto end;
570 }
571
572 ret = xmlTextWriterEndElement(writer->writer);
573 end:
574 return ret >= 0 ? 0 : ret;
575 }
576
577 LTTNG_HIDDEN
578 int config_writer_write_element_unsigned_int(struct config_writer *writer,
579 const char *element_name, uint64_t value)
580 {
581 int ret;
582 xmlChar *encoded_element_name;
583
584 if (!writer || !writer->writer || !element_name || !element_name[0]) {
585 ret = -1;
586 goto end;
587 }
588
589 encoded_element_name = encode_string(element_name);
590 if (!encoded_element_name) {
591 ret = -1;
592 goto end;
593 }
594
595 ret = xmlTextWriterWriteFormatElement(writer->writer,
596 encoded_element_name, "%" PRIu64, value);
597 xmlFree(encoded_element_name);
598 end:
599 return ret >= 0 ? 0 : ret;
600 }
601
602 LTTNG_HIDDEN
603 int config_writer_write_element_signed_int(struct config_writer *writer,
604 const char *element_name, int64_t value)
605 {
606 int ret;
607 xmlChar *encoded_element_name;
608
609 if (!writer || !writer->writer || !element_name || !element_name[0]) {
610 ret = -1;
611 goto end;
612 }
613
614 encoded_element_name = encode_string(element_name);
615 if (!encoded_element_name) {
616 ret = -1;
617 goto end;
618 }
619
620 ret = xmlTextWriterWriteFormatElement(writer->writer,
621 encoded_element_name, "%" PRIi64, value);
622 xmlFree(encoded_element_name);
623 end:
624 return ret >= 0 ? 0 : ret;
625 }
626
627 LTTNG_HIDDEN
628 int config_writer_write_element_bool(struct config_writer *writer,
629 const char *element_name, int value)
630 {
631 return config_writer_write_element_string(writer, element_name,
632 value ? config_xml_true : config_xml_false);
633 }
634
635 LTTNG_HIDDEN
636 int config_writer_write_element_string(struct config_writer *writer,
637 const char *element_name, const char *value)
638 {
639 int ret;
640 xmlChar *encoded_element_name = NULL;
641 xmlChar *encoded_value = NULL;
642
643 if (!writer || !writer->writer || !element_name || !element_name[0] ||
644 !value) {
645 ret = -1;
646 goto end;
647 }
648
649 encoded_element_name = encode_string(element_name);
650 if (!encoded_element_name) {
651 ret = -1;
652 goto end;
653 }
654
655 encoded_value = encode_string(value);
656 if (!encoded_value) {
657 ret = -1;
658 goto end;
659 }
660
661 ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name,
662 encoded_value);
663 end:
664 xmlFree(encoded_element_name);
665 xmlFree(encoded_value);
666 return ret >= 0 ? 0 : ret;
667 }
668
669 LTTNG_HIDDEN
670 int config_writer_write_config_element(struct config_writer *writer,
671 const struct config_element *element)
672 {
673 int ret = 0;
674
675 xmlNodePtr local_copy = NULL;
676 xmlNodePtr place_holder = NULL;
677 xmlDocPtr document = NULL;
678 xmlBufferPtr buffer = NULL;
679
680 assert(element);
681 assert(element->element);
682 assert(writer);
683
684 /* Create a new doc and set root node as passed element */
685 document = xmlNewDoc(BAD_CAST "1.0");
686 document->parseFlags |= XML_PARSE_NOBLANKS;
687 if (!document) {
688 ERR("Unable to create xml document");
689 ret = -1;
690 goto end;
691 }
692
693 local_copy = xmlCopyNode(element->element, 1);
694 if (!local_copy) {
695 ERR("Unable to copy an xml node");
696 ret = -1;
697 goto end;
698 }
699
700 /* Hand off the local_copy to the xmlDoc */
701 place_holder = xmlDocSetRootElement(document, local_copy);
702 if (place_holder) {
703 ERR("The document root was already set");
704 xmlFreeNode(local_copy);
705 ret = -1;
706 goto end;
707 }
708
709 buffer = xmlBufferCreate();
710 if (!buffer) {
711 ret = -1;
712 goto end;
713 }
714
715 ret = xmlNodeDump(buffer, document, local_copy, 0, 0);
716 xmlTextWriterWriteRaw(writer->writer, xmlBufferContent(buffer));
717 end:
718 xmlBufferFree(buffer);
719 xmlFreeDoc(document);
720 return ret;
721 }
722
723 static
724 void xml_error_handler(void *ctx, const char *format, ...)
725 {
726 char *errMsg;
727 va_list args;
728 int ret;
729
730 va_start(args, format);
731 ret = vasprintf(&errMsg, format, args);
732 va_end(args);
733 if (ret == -1) {
734 ERR("String allocation failed in xml error handler");
735 return;
736 }
737
738 fprintf(stderr, "XML Error: %s", errMsg);
739 free(errMsg);
740 }
741
742 static
743 void fini_session_config_validation_ctx(
744 struct session_config_validation_ctx *ctx)
745 {
746 if (ctx->parser_ctx) {
747 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
748 }
749
750 if (ctx->schema) {
751 xmlSchemaFree(ctx->schema);
752 }
753
754 if (ctx->schema_validation_ctx) {
755 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
756 }
757
758 memset(ctx, 0, sizeof(struct session_config_validation_ctx));
759 }
760
761 static
762 char *get_session_config_xsd_path()
763 {
764 char *xsd_path;
765 const char *base_path = lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
766 size_t base_path_len;
767 size_t max_path_len;
768
769 if (!base_path) {
770 base_path = DEFAULT_SESSION_CONFIG_XSD_PATH;
771 }
772
773 base_path_len = strlen(base_path);
774 max_path_len = base_path_len +
775 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1;
776 xsd_path = zmalloc(max_path_len);
777 if (!xsd_path) {
778 goto end;
779 }
780
781 strncpy(xsd_path, base_path, max_path_len);
782 if (xsd_path[base_path_len - 1] != '/') {
783 xsd_path[base_path_len++] = '/';
784 }
785
786 strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME,
787 max_path_len - base_path_len);
788 end:
789 return xsd_path;
790 }
791
792 static
793 int init_session_config_validation_ctx(
794 struct session_config_validation_ctx *ctx)
795 {
796 int ret;
797 char *xsd_path = get_session_config_xsd_path();
798
799 if (!xsd_path) {
800 ret = -LTTNG_ERR_NOMEM;
801 goto end;
802 }
803
804 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
805 if (!ctx->parser_ctx) {
806 ERR("XSD parser context creation failed");
807 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
808 goto end;
809 }
810 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
811 xml_error_handler, NULL);
812
813 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
814 if (!ctx->schema) {
815 ERR("XSD parsing failed");
816 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
817 goto end;
818 }
819
820 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
821 if (!ctx->schema_validation_ctx) {
822 ERR("XSD validation context creation failed");
823 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
824 goto end;
825 }
826
827 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
828 xml_error_handler, NULL);
829 ret = 0;
830
831 end:
832 if (ret) {
833 fini_session_config_validation_ctx(ctx);
834 }
835
836 free(xsd_path);
837 return ret;
838 }
839
840 static
841 int parse_uint(xmlChar *str, uint64_t *val)
842 {
843 int ret;
844 char *endptr;
845
846 if (!str || !val) {
847 ret = -1;
848 goto end;
849 }
850
851 *val = strtoull((const char *) str, &endptr, 10);
852 if (!endptr || *endptr) {
853 ret = -1;
854 } else {
855 ret = 0;
856 }
857
858 end:
859 return ret;
860 }
861
862 static
863 int parse_int(xmlChar *str, int64_t *val)
864 {
865 int ret;
866 char *endptr;
867
868 if (!str || !val) {
869 ret = -1;
870 goto end;
871 }
872
873 *val = strtoll((const char *) str, &endptr, 10);
874 if (!endptr || *endptr) {
875 ret = -1;
876 } else {
877 ret = 0;
878 }
879
880 end:
881 return ret;
882 }
883
884 static
885 int parse_bool(xmlChar *str, int *val)
886 {
887 int ret = 0;
888
889 if (!str || !val) {
890 ret = -1;
891 goto end;
892 }
893
894 if (!strcmp((const char *) str, config_xml_true)) {
895 *val = 1;
896 } else if (!strcmp((const char *) str, config_xml_false)) {
897 *val = 0;
898 } else {
899 WARN("Invalid boolean value encoutered (%s).",
900 (const char *) str);
901 ret = -1;
902 }
903 end:
904 return ret;
905 }
906
907 static
908 int get_domain_type(xmlChar *domain)
909 {
910 int ret;
911
912 if (!domain) {
913 goto error;
914 }
915
916 if (!strcmp((char *) domain, config_domain_type_kernel)) {
917 ret = LTTNG_DOMAIN_KERNEL;
918 } else if (!strcmp((char *) domain, config_domain_type_ust)) {
919 ret = LTTNG_DOMAIN_UST;
920 } else if (!strcmp((char *) domain, config_domain_type_jul)) {
921 ret = LTTNG_DOMAIN_JUL;
922 } else if (!strcmp((char *) domain, config_domain_type_log4j)) {
923 ret = LTTNG_DOMAIN_LOG4J;
924 } else if (!strcmp((char *) domain, config_domain_type_python)) {
925 ret = LTTNG_DOMAIN_PYTHON;
926 } else {
927 goto error;
928 }
929
930 return ret;
931 error:
932 return -1;
933 }
934
935 static
936 int get_buffer_type(xmlChar *buffer_type)
937 {
938 int ret;
939
940 if (!buffer_type) {
941 goto error;
942 }
943
944 if (!strcmp((char *) buffer_type, config_buffer_type_global)) {
945 ret = LTTNG_BUFFER_GLOBAL;
946 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) {
947 ret = LTTNG_BUFFER_PER_UID;
948 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) {
949 ret = LTTNG_BUFFER_PER_PID;
950 } else {
951 goto error;
952 }
953
954 return ret;
955 error:
956 return -1;
957 }
958
959 static
960 int get_overwrite_mode(xmlChar *overwrite_mode)
961 {
962 int ret;
963
964 if (!overwrite_mode) {
965 goto error;
966 }
967
968 if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) {
969 ret = 1;
970 } else if (!strcmp((char *) overwrite_mode,
971 config_overwrite_mode_discard)) {
972 ret = 0;
973 } else {
974 goto error;
975 }
976
977 return ret;
978 error:
979 return -1;
980 }
981
982 static
983 int get_output_type(xmlChar *output_type)
984 {
985 int ret;
986
987 if (!output_type) {
988 goto error;
989 }
990
991 if (!strcmp((char *) output_type, config_output_type_mmap)) {
992 ret = LTTNG_EVENT_MMAP;
993 } else if (!strcmp((char *) output_type, config_output_type_splice)) {
994 ret = LTTNG_EVENT_SPLICE;
995 } else {
996 goto error;
997 }
998
999 return ret;
1000 error:
1001 return -1;
1002 }
1003
1004 LTTNG_HIDDEN
1005 int config_get_event_type(const char *event_type)
1006 {
1007 int ret;
1008
1009 if (!event_type) {
1010 goto error;
1011 }
1012
1013 if (!strcmp(event_type, config_event_type_all)) {
1014 ret = LTTNG_EVENT_ALL;
1015 } else if (!strcmp(event_type, config_event_type_tracepoint)) {
1016 ret = LTTNG_EVENT_TRACEPOINT;
1017 } else if (!strcmp(event_type, config_event_type_probe)) {
1018 ret = LTTNG_EVENT_PROBE;
1019 } else if (!strcmp(event_type, config_event_type_function)) {
1020 ret = LTTNG_EVENT_FUNCTION;
1021 } else if (!strcmp(event_type,
1022 config_event_type_function_entry)) {
1023 ret = LTTNG_EVENT_FUNCTION_ENTRY;
1024 } else if (!strcmp(event_type, config_event_type_noop)) {
1025 ret = LTTNG_EVENT_NOOP;
1026 } else if (!strcmp(event_type, config_event_type_syscall)) {
1027 ret = LTTNG_EVENT_SYSCALL;
1028 } else {
1029 goto error;
1030 }
1031
1032 return ret;
1033 error:
1034 return -1;
1035 }
1036
1037 static
1038 int get_loglevel_type(xmlChar *loglevel_type)
1039 {
1040 int ret;
1041
1042 if (!loglevel_type) {
1043 goto error;
1044 }
1045
1046 if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) {
1047 ret = LTTNG_EVENT_LOGLEVEL_ALL;
1048 } else if (!strcmp((char *) loglevel_type,
1049 config_loglevel_type_range)) {
1050 ret = LTTNG_EVENT_LOGLEVEL_RANGE;
1051 } else if (!strcmp((char *) loglevel_type,
1052 config_loglevel_type_single)) {
1053 ret = LTTNG_EVENT_LOGLEVEL_SINGLE;
1054 } else {
1055 goto error;
1056 }
1057
1058 return ret;
1059 error:
1060 return -1;
1061 }
1062
1063 /*
1064 * Return the context type or -1 on error.
1065 */
1066 static
1067 int get_context_type(xmlChar *context_type)
1068 {
1069 int ret;
1070
1071 if (!context_type) {
1072 goto error;
1073 }
1074
1075 if (!strcmp((char *) context_type, config_event_context_pid)) {
1076 ret = LTTNG_EVENT_CONTEXT_PID;
1077 } else if (!strcmp((char *) context_type,
1078 config_event_context_procname)) {
1079 ret = LTTNG_EVENT_CONTEXT_PROCNAME;
1080 } else if (!strcmp((char *) context_type,
1081 config_event_context_prio)) {
1082 ret = LTTNG_EVENT_CONTEXT_PRIO;
1083 } else if (!strcmp((char *) context_type,
1084 config_event_context_nice)) {
1085 ret = LTTNG_EVENT_CONTEXT_NICE;
1086 } else if (!strcmp((char *) context_type,
1087 config_event_context_vpid)) {
1088 ret = LTTNG_EVENT_CONTEXT_VPID;
1089 } else if (!strcmp((char *) context_type,
1090 config_event_context_tid)) {
1091 ret = LTTNG_EVENT_CONTEXT_TID;
1092 } else if (!strcmp((char *) context_type,
1093 config_event_context_vtid)) {
1094 ret = LTTNG_EVENT_CONTEXT_VTID;
1095 } else if (!strcmp((char *) context_type,
1096 config_event_context_ppid)) {
1097 ret = LTTNG_EVENT_CONTEXT_PPID;
1098 } else if (!strcmp((char *) context_type,
1099 config_event_context_vppid)) {
1100 ret = LTTNG_EVENT_CONTEXT_VPPID;
1101 } else if (!strcmp((char *) context_type,
1102 config_event_context_pthread_id)) {
1103 ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID;
1104 } else if (!strcmp((char *) context_type,
1105 config_event_context_hostname)) {
1106 ret = LTTNG_EVENT_CONTEXT_HOSTNAME;
1107 } else if (!strcmp((char *) context_type,
1108 config_event_context_ip)) {
1109 ret = LTTNG_EVENT_CONTEXT_IP;
1110 } else if (!strcmp((char *) context_type,
1111 config_event_context_interruptible)) {
1112 ret = LTTNG_EVENT_CONTEXT_INTERRUPTIBLE;
1113 } else if (!strcmp((char *) context_type,
1114 config_event_context_preemptible)) {
1115 ret = LTTNG_EVENT_CONTEXT_PREEMPTIBLE;
1116 } else if (!strcmp((char *) context_type,
1117 config_event_context_need_reschedule)) {
1118 ret = LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE;
1119 } else if (!strcmp((char *) context_type,
1120 config_event_context_migratable)) {
1121 ret = LTTNG_EVENT_CONTEXT_MIGRATABLE;
1122 } else {
1123 goto error;
1124 }
1125
1126 return ret;
1127 error:
1128 return -1;
1129 }
1130
1131 static
1132 int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
1133 {
1134 int ret;
1135 xmlNodePtr node;
1136
1137 for (node = xmlFirstElementChild(domain_node); node;
1138 node = xmlNextElementSibling(node)) {
1139 if (!strcmp((const char *) node->name, config_element_type)) {
1140 /* domain type */
1141 xmlChar *node_content = xmlNodeGetContent(node);
1142 if (!node_content) {
1143 ret = -LTTNG_ERR_NOMEM;
1144 goto end;
1145 }
1146
1147 ret = get_domain_type(node_content);
1148 free(node_content);
1149 if (ret < 0) {
1150 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1151 goto end;
1152 }
1153
1154 domain->type = ret;
1155 } else if (!strcmp((const char *) node->name,
1156 config_element_buffer_type)) {
1157 /* buffer type */
1158 xmlChar *node_content = xmlNodeGetContent(node);
1159 if (!node_content) {
1160 ret = -LTTNG_ERR_NOMEM;
1161 goto end;
1162 }
1163
1164 ret = get_buffer_type(node_content);
1165 free(node_content);
1166 if (ret < 0) {
1167 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1168 goto end;
1169 }
1170
1171 domain->buf_type = ret;
1172 }
1173 }
1174 ret = 0;
1175 end:
1176 return ret;
1177 }
1178
1179 static
1180 int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
1181 char **data_uri)
1182 {
1183 xmlNodePtr node;
1184
1185 for (node = xmlFirstElementChild(net_output_node); node;
1186 node = xmlNextElementSibling(node)) {
1187 if (!strcmp((const char *) node->name, config_element_control_uri)) {
1188 /* control_uri */
1189 *control_uri = (char *) xmlNodeGetContent(node);
1190 if (!*control_uri) {
1191 break;
1192 }
1193 } else {
1194 /* data_uri */
1195 *data_uri = (char *) xmlNodeGetContent(node);
1196 if (!*data_uri) {
1197 break;
1198 }
1199 }
1200 }
1201
1202 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
1203 }
1204
1205 static
1206 int process_consumer_output(xmlNodePtr consumer_output_node,
1207 struct consumer_output *output)
1208 {
1209 int ret;
1210 xmlNodePtr node;
1211
1212 assert(output);
1213
1214 for (node = xmlFirstElementChild(consumer_output_node); node;
1215 node = xmlNextElementSibling(node)) {
1216 if (!strcmp((const char *) node->name, config_element_enabled)) {
1217 xmlChar *enabled_str = xmlNodeGetContent(node);
1218
1219 /* enabled */
1220 if (!enabled_str) {
1221 ret = -LTTNG_ERR_NOMEM;
1222 goto end;
1223 }
1224
1225 ret = parse_bool(enabled_str, &output->enabled);
1226 free(enabled_str);
1227 if (ret) {
1228 goto end;
1229 }
1230 } else {
1231 xmlNodePtr output_type_node;
1232
1233 /* destination */
1234 output_type_node = xmlFirstElementChild(node);
1235 if (!output_type_node) {
1236 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1237 goto end;
1238 }
1239
1240 if (!strcmp((const char *) output_type_node->name,
1241 config_element_path)) {
1242 /* path */
1243 output->path = (char *) xmlNodeGetContent(output_type_node);
1244 if (!output->path) {
1245 ret = -LTTNG_ERR_NOMEM;
1246 goto end;
1247 }
1248 } else {
1249 /* net_output */
1250 ret = get_net_output_uris(output_type_node,
1251 &output->control_uri, &output->data_uri);
1252 if (ret) {
1253 goto end;
1254 }
1255 }
1256 }
1257 }
1258 ret = 0;
1259
1260 end:
1261 if (ret) {
1262 free(output->path);
1263 free(output->control_uri);
1264 free(output->data_uri);
1265 memset(output, 0, sizeof(struct consumer_output));
1266 }
1267 return ret;
1268 }
1269
1270 static
1271 int create_session_net_output(const char *name, const char *control_uri,
1272 const char *data_uri)
1273 {
1274 int ret;
1275 struct lttng_handle *handle;
1276 const char *uri = NULL;
1277
1278 assert(name);
1279
1280 handle = lttng_create_handle(name, NULL);
1281 if (!handle) {
1282 ret = -LTTNG_ERR_NOMEM;
1283 goto end;
1284 }
1285
1286 if (!control_uri || !data_uri) {
1287 uri = control_uri ? control_uri : data_uri;
1288 control_uri = uri;
1289 data_uri = uri;
1290 }
1291
1292 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1293 lttng_destroy_handle(handle);
1294 end:
1295 return ret;
1296 }
1297
1298 static
1299 int create_snapshot_session(const char *session_name, xmlNodePtr output_node)
1300 {
1301 int ret;
1302 xmlNodePtr node = NULL;
1303 xmlNodePtr snapshot_output_list_node;
1304 xmlNodePtr snapshot_output_node;
1305
1306 assert(session_name);
1307
1308 ret = lttng_create_session_snapshot(session_name, NULL);
1309 if (ret) {
1310 goto end;
1311 }
1312
1313 if (!output_node) {
1314 goto end;
1315 }
1316
1317 snapshot_output_list_node = xmlFirstElementChild(output_node);
1318
1319 /* Parse and create snapshot outputs */
1320
1321 for (snapshot_output_node =
1322 xmlFirstElementChild(snapshot_output_list_node);
1323 snapshot_output_node; snapshot_output_node =
1324 xmlNextElementSibling(snapshot_output_node)) {
1325 char *name = NULL;
1326 uint64_t max_size = DEFAULT_SNAPSHOT_MAX_SIZE;
1327 struct consumer_output output = { 0 };
1328 struct lttng_snapshot_output *snapshot_output = NULL;
1329
1330 for (node = xmlFirstElementChild(snapshot_output_node); node;
1331 node = xmlNextElementSibling(node)) {
1332 if (!strcmp((const char *) node->name,
1333 config_element_name)) {
1334 /* name */
1335 name = (char *) xmlNodeGetContent(node);
1336 if (!name) {
1337 ret = -LTTNG_ERR_NOMEM;
1338 goto error_snapshot_output;
1339 }
1340 } else if (!strcmp((const char *) node->name,
1341 config_element_max_size)) {
1342 xmlChar *content = xmlNodeGetContent(node);
1343
1344 /* max_size */
1345 if (!content) {
1346 ret = -LTTNG_ERR_NOMEM;
1347 goto error_snapshot_output;
1348 }
1349 ret = parse_uint(content, &max_size);
1350 free(content);
1351 if (ret) {
1352 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1353 goto error_snapshot_output;
1354 }
1355 } else {
1356 /* consumer_output */
1357 ret = process_consumer_output(node, &output);
1358 if (ret) {
1359 goto error_snapshot_output;
1360 }
1361 }
1362 }
1363
1364 snapshot_output = lttng_snapshot_output_create();
1365 if (!snapshot_output) {
1366 ret = -LTTNG_ERR_NOMEM;
1367 goto error_snapshot_output;
1368 }
1369
1370 if (!name) {
1371 /* Generate a default name */
1372 int pret;
1373 pret = asprintf(&name, DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
1374 lttng_snapshot_output_get_id(snapshot_output));
1375 if (pret < 0) {
1376 name = NULL;
1377 PERROR("snprintf output name");
1378 goto error_snapshot_output;
1379 }
1380 }
1381
1382 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1383 if (ret) {
1384 goto error_snapshot_output;
1385 }
1386
1387 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1388 if (ret) {
1389 goto error_snapshot_output;
1390 }
1391
1392 if (output.path) {
1393 ret = lttng_snapshot_output_set_ctrl_url(output.path,
1394 snapshot_output);
1395 if (ret) {
1396 goto error_snapshot_output;
1397 }
1398 } else {
1399 if (output.control_uri) {
1400 ret = lttng_snapshot_output_set_ctrl_url(output.control_uri,
1401 snapshot_output);
1402 if (ret) {
1403 goto error_snapshot_output;
1404 }
1405 }
1406
1407 if (output.data_uri) {
1408 ret = lttng_snapshot_output_set_data_url(output.data_uri,
1409 snapshot_output);
1410 if (ret) {
1411 goto error_snapshot_output;
1412 }
1413 }
1414 }
1415
1416 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1417 error_snapshot_output:
1418 free(name);
1419 free(output.path);
1420 free(output.control_uri);
1421 free(output.data_uri);
1422 lttng_snapshot_output_destroy(snapshot_output);
1423 if (ret) {
1424 goto end;
1425 }
1426 }
1427 end:
1428 return ret;
1429 }
1430
1431 static
1432 int create_session(const char *name,
1433 struct lttng_domain *kernel_domain,
1434 struct lttng_domain *ust_domain,
1435 struct lttng_domain *jul_domain,
1436 struct lttng_domain *log4j_domain,
1437 xmlNodePtr output_node,
1438 uint64_t live_timer_interval)
1439 {
1440 int ret;
1441 struct consumer_output output = { 0 };
1442 xmlNodePtr consumer_output_node;
1443
1444 assert(name);
1445
1446 if (output_node) {
1447 consumer_output_node = xmlFirstElementChild(output_node);
1448 if (!consumer_output_node) {
1449 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1450 goto end;
1451 }
1452
1453 if (strcmp((const char *) consumer_output_node->name,
1454 config_element_consumer_output)) {
1455 WARN("Invalid output type, expected %s node",
1456 config_element_consumer_output);
1457 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1458 goto end;
1459 }
1460
1461 ret = process_consumer_output(consumer_output_node, &output);
1462 if (ret) {
1463 goto end;
1464 }
1465 }
1466
1467 if (live_timer_interval != UINT64_MAX &&
1468 !output.control_uri && !output.data_uri) {
1469 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1470 goto end;
1471 }
1472
1473 if (output.control_uri || output.data_uri) {
1474 /* network destination */
1475 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
1476 /*
1477 * URLs are provided for sure since the test above make sure that
1478 * with a live timer the data and control URIs are provided. So,
1479 * NULL is passed here and will be set right after.
1480 */
1481 ret = lttng_create_session_live(name, NULL, live_timer_interval);
1482 } else {
1483 ret = lttng_create_session(name, NULL);
1484 }
1485 if (ret) {
1486 goto end;
1487 }
1488
1489 ret = create_session_net_output(name, output.control_uri,
1490 output.data_uri);
1491 if (ret) {
1492 goto end;
1493 }
1494
1495 } else {
1496 /* either local output or no output */
1497 ret = lttng_create_session(name, output.path);
1498 if (ret) {
1499 goto end;
1500 }
1501 }
1502 end:
1503 free(output.path);
1504 free(output.control_uri);
1505 free(output.data_uri);
1506 return ret;
1507 }
1508 static
1509 int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1510 struct lttng_event_probe_attr *attr)
1511 {
1512 int ret;
1513
1514 assert(probe_attribute_node);
1515 assert(attr);
1516
1517 if (!strcmp((const char *) probe_attribute_node->name,
1518 config_element_address)) {
1519 xmlChar *content;
1520 uint64_t addr = 0;
1521
1522 /* addr */
1523 content = xmlNodeGetContent(probe_attribute_node);
1524 if (!content) {
1525 ret = -LTTNG_ERR_NOMEM;
1526 goto end;
1527 }
1528
1529 ret = parse_uint(content, &addr);
1530 free(content);
1531 if (ret) {
1532 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1533 goto end;
1534 }
1535
1536 attr->addr = addr;
1537 } else if (!strcmp((const char *) probe_attribute_node->name,
1538 config_element_offset)) {
1539 xmlChar *content;
1540 uint64_t offset = 0;
1541
1542 /* offset */
1543 content = xmlNodeGetContent(probe_attribute_node);
1544 if (!content) {
1545 ret = -LTTNG_ERR_NOMEM;
1546 goto end;
1547 }
1548
1549 ret = parse_uint(content, &offset);
1550 free(content);
1551 if (ret) {
1552 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1553 goto end;
1554 }
1555
1556 attr->offset = offset;
1557 } else if (!strcmp((const char *) probe_attribute_node->name,
1558 config_element_symbol_name)) {
1559 xmlChar *content;
1560 size_t name_len;
1561
1562 /* symbol_name */
1563 content = xmlNodeGetContent(probe_attribute_node);
1564 if (!content) {
1565 ret = -LTTNG_ERR_NOMEM;
1566 goto end;
1567 }
1568
1569 name_len = strlen((char *) content);
1570 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1571 WARN("symbol_name too long.");
1572 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1573 free(content);
1574 goto end;
1575 }
1576
1577 strncpy(attr->symbol_name, (const char *) content, name_len);
1578 free(content);
1579 }
1580 ret = 0;
1581 end:
1582 return ret;
1583 }
1584
1585 static
1586 int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
1587 const char *channel_name, const enum process_event_node_phase phase)
1588 {
1589 int ret = 0, i;
1590 xmlNodePtr node;
1591 struct lttng_event event;
1592 char **exclusions = NULL;
1593 unsigned long exclusion_count = 0;
1594 char *filter_expression = NULL;
1595
1596 assert(event_node);
1597 assert(handle);
1598
1599 memset(&event, 0, sizeof(event));
1600
1601 /* Initialize default log level which varies by domain */
1602 switch (handle->domain.type)
1603 {
1604 case LTTNG_DOMAIN_JUL:
1605 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1606 break;
1607 case LTTNG_DOMAIN_LOG4J:
1608 event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1609 break;
1610 case LTTNG_DOMAIN_PYTHON:
1611 event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1612 break;
1613 case LTTNG_DOMAIN_UST:
1614 case LTTNG_DOMAIN_KERNEL:
1615 event.loglevel = LTTNG_LOGLEVEL_DEBUG;
1616 break;
1617 default:
1618 assert(0);
1619 }
1620
1621 for (node = xmlFirstElementChild(event_node); node;
1622 node = xmlNextElementSibling(node)) {
1623 if (!strcmp((const char *) node->name, config_element_name)) {
1624 xmlChar *content;
1625 size_t name_len;
1626
1627 /* name */
1628 content = xmlNodeGetContent(node);
1629 if (!content) {
1630 ret = -LTTNG_ERR_NOMEM;
1631 goto end;
1632 }
1633
1634 name_len = strlen((char *) content);
1635 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1636 WARN("Channel name too long.");
1637 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1638 free(content);
1639 goto end;
1640 }
1641
1642 strncpy(event.name, (const char *) content, name_len);
1643 free(content);
1644 } else if (!strcmp((const char *) node->name,
1645 config_element_enabled)) {
1646 xmlChar *content = xmlNodeGetContent(node);
1647
1648 /* enabled */
1649 if (!content) {
1650 ret = -LTTNG_ERR_NOMEM;
1651 goto end;
1652 }
1653
1654 ret = parse_bool(content, &event.enabled);
1655 free(content);
1656 if (ret) {
1657 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1658 goto end;
1659 }
1660 } else if (!strcmp((const char *) node->name,
1661 config_element_type)) {
1662 xmlChar *content = xmlNodeGetContent(node);
1663
1664 /* type */
1665 if (!content) {
1666 ret = -LTTNG_ERR_NOMEM;
1667 goto end;
1668 }
1669
1670 ret = config_get_event_type((char *) content);
1671 free(content);
1672 if (ret < 0) {
1673 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1674 goto end;
1675 }
1676
1677 event.type = ret;
1678 } else if (!strcmp((const char *) node->name,
1679 config_element_loglevel_type)) {
1680 xmlChar *content = xmlNodeGetContent(node);
1681
1682 /* loglevel_type */
1683 if (!content) {
1684 ret = -LTTNG_ERR_NOMEM;
1685 goto end;
1686 }
1687
1688 ret = get_loglevel_type(content);
1689 free(content);
1690 if (ret < 0) {
1691 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1692 goto end;
1693 }
1694
1695 event.loglevel_type = ret;
1696 } else if (!strcmp((const char *) node->name,
1697 config_element_loglevel)) {
1698 xmlChar *content;
1699 int64_t loglevel = 0;
1700
1701 /* loglevel */
1702 content = xmlNodeGetContent(node);
1703 if (!content) {
1704 ret = -LTTNG_ERR_NOMEM;
1705 goto end;
1706 }
1707
1708 ret = parse_int(content, &loglevel);
1709 free(content);
1710 if (ret) {
1711 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1712 goto end;
1713 }
1714
1715 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1716 WARN("loglevel out of range.");
1717 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1718 goto end;
1719 }
1720
1721 event.loglevel = loglevel;
1722 } else if (!strcmp((const char *) node->name,
1723 config_element_filter)) {
1724 xmlChar *content =
1725 xmlNodeGetContent(node);
1726
1727 /* filter */
1728 if (!content) {
1729 ret = -LTTNG_ERR_NOMEM;
1730 goto end;
1731 }
1732
1733 free(filter_expression);
1734 filter_expression = strdup((char *) content);
1735 free(content);
1736 if (!filter_expression) {
1737 ret = -LTTNG_ERR_NOMEM;
1738 goto end;
1739 }
1740 } else if (!strcmp((const char *) node->name,
1741 config_element_exclusions)) {
1742 xmlNodePtr exclusion_node;
1743 int exclusion_index = 0;
1744
1745 /* exclusions */
1746 if (exclusions) {
1747 /*
1748 * Exclusions has already been initialized,
1749 * invalid file.
1750 */
1751 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1752 goto end;
1753 }
1754
1755 exclusion_count = xmlChildElementCount(node);
1756 if (!exclusion_count) {
1757 continue;
1758 }
1759
1760 exclusions = zmalloc(exclusion_count * sizeof(char *));
1761 if (!exclusions) {
1762 exclusion_count = 0;
1763 ret = -LTTNG_ERR_NOMEM;
1764 goto end;
1765 }
1766
1767 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1768 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1769 xmlChar *content =
1770 xmlNodeGetContent(exclusion_node);
1771
1772 if (!content) {
1773 ret = -LTTNG_ERR_NOMEM;
1774 goto end;
1775 }
1776
1777 exclusions[exclusion_index] = strdup((const char *) content);
1778 free(content);
1779 if (!exclusions[exclusion_index]) {
1780 ret = -LTTNG_ERR_NOMEM;
1781 goto end;
1782 }
1783 exclusion_index++;
1784 }
1785
1786 event.exclusion = 1;
1787 } else if (!strcmp((const char *) node->name,
1788 config_element_attributes)) {
1789 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1790
1791 /* attributes */
1792 if (!attribute_node) {
1793 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1794 goto end;
1795 }
1796
1797 if (!strcmp((const char *) node->name,
1798 config_element_probe_attributes)) {
1799 xmlNodePtr probe_attribute_node;
1800
1801 /* probe_attributes */
1802 for (probe_attribute_node =
1803 xmlFirstElementChild(attribute_node); probe_attribute_node;
1804 probe_attribute_node = xmlNextElementSibling(
1805 probe_attribute_node)) {
1806
1807 ret = process_probe_attribute_node(probe_attribute_node,
1808 &event.attr.probe);
1809 if (ret) {
1810 goto end;
1811 }
1812 }
1813 } else {
1814 size_t sym_len;
1815 xmlChar *content;
1816 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1817
1818 /* function_attributes */
1819 content = xmlNodeGetContent(symbol_node);
1820 if (!content) {
1821 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1822 goto end;
1823 }
1824
1825 sym_len = strlen((char *) content);
1826 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1827 WARN("Function name too long.");
1828 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1829 free(content);
1830 goto end;
1831 }
1832
1833 strncpy(event.attr.ftrace.symbol_name, (char *) content,
1834 sym_len);
1835 free(content);
1836 }
1837 }
1838 }
1839
1840 if ((event.enabled && phase == ENABLE) || phase == CREATION) {
1841 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1842 filter_expression, exclusion_count, exclusions);
1843 if (ret < 0) {
1844 goto end;
1845 }
1846 }
1847 end:
1848 for (i = 0; i < exclusion_count; i++) {
1849 free(exclusions[i]);
1850 }
1851
1852 free(exclusions);
1853 free(filter_expression);
1854 return ret;
1855 }
1856
1857 static
1858 int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1859 const char *channel_name)
1860 {
1861 int ret = 0;
1862 struct lttng_event event;
1863 xmlNodePtr node;
1864
1865 assert(events_node);
1866 assert(handle);
1867 assert(channel_name);
1868
1869 for (node = xmlFirstElementChild(events_node); node;
1870 node = xmlNextElementSibling(node)) {
1871 ret = process_event_node(node, handle, channel_name, CREATION);
1872 if (ret) {
1873 goto end;
1874 }
1875 }
1876
1877 /*
1878 * Disable all events to enable only the necessary events.
1879 * Limitations regarding lttng_disable_events and tuple descriptor
1880 * force this approach.
1881 */
1882 memset(&event, 0, sizeof(event));
1883 event.loglevel = -1;
1884 event.type = LTTNG_EVENT_ALL;
1885 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
1886 if (ret) {
1887 goto end;
1888 }
1889
1890 for (node = xmlFirstElementChild(events_node); node;
1891 node = xmlNextElementSibling(node)) {
1892 ret = process_event_node(node, handle, channel_name, ENABLE);
1893 if (ret) {
1894 goto end;
1895 }
1896 }
1897
1898 end:
1899 return ret;
1900 }
1901
1902 static
1903 int process_channel_attr_node(xmlNodePtr attr_node, bool snapshot_mode,
1904 enum lttng_domain_type domain, struct lttng_channel *channel,
1905 xmlNodePtr *contexts_node, xmlNodePtr *events_node)
1906 {
1907 int ret;
1908
1909 assert(attr_node);
1910 assert(channel);
1911 assert(contexts_node);
1912 assert(events_node);
1913
1914 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1915 xmlChar *content;
1916 size_t name_len;
1917
1918 /* name */
1919 content = xmlNodeGetContent(attr_node);
1920 if (!content) {
1921 ret = -LTTNG_ERR_NOMEM;
1922 goto end;
1923 }
1924
1925 name_len = strlen((char *) content);
1926 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1927 WARN("Channel name too long.");
1928 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1929 free(content);
1930 goto end;
1931 }
1932
1933 strncpy(channel->name, (const char *) content, name_len);
1934 free(content);
1935 } else if (!strcmp((const char *) attr_node->name,
1936 config_element_enabled)) {
1937 xmlChar *content;
1938 int enabled;
1939
1940 /* enabled */
1941 content = xmlNodeGetContent(attr_node);
1942 if (!content) {
1943 ret = -LTTNG_ERR_NOMEM;
1944 goto end;
1945 }
1946
1947 ret = parse_bool(content, &enabled);
1948 free(content);
1949 if (ret) {
1950 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1951 goto end;
1952 }
1953
1954 channel->enabled = enabled;
1955 } else if (!strcmp((const char *) attr_node->name,
1956 config_element_overwrite_mode)) {
1957 xmlChar *content;
1958
1959 /* overwrite_mode */
1960 content = xmlNodeGetContent(attr_node);
1961 if (!content) {
1962 ret = -LTTNG_ERR_NOMEM;
1963 goto end;
1964 }
1965
1966 ret = get_overwrite_mode(content);
1967 free(content);
1968 if (ret < 0) {
1969 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1970 goto end;
1971 }
1972
1973 channel->attr.overwrite = ret;
1974 } else if (!strcmp((const char *) attr_node->name,
1975 config_element_subbuf_size)) {
1976 xmlChar *content;
1977
1978 /* subbuffer_size */
1979 content = xmlNodeGetContent(attr_node);
1980 if (!content) {
1981 ret = -LTTNG_ERR_NOMEM;
1982 goto end;
1983 }
1984
1985 ret = parse_uint(content, &channel->attr.subbuf_size);
1986 free(content);
1987 if (ret) {
1988 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1989 goto end;
1990 }
1991 } else if (!strcmp((const char *) attr_node->name,
1992 config_element_num_subbuf)) {
1993 xmlChar *content;
1994
1995 /* subbuffer_count */
1996 content = xmlNodeGetContent(attr_node);
1997 if (!content) {
1998 ret = -LTTNG_ERR_NOMEM;
1999 goto end;
2000 }
2001
2002 ret = parse_uint(content, &channel->attr.num_subbuf);
2003 free(content);
2004 if (ret) {
2005 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2006 goto end;
2007 }
2008 } else if (!strcmp((const char *) attr_node->name,
2009 config_element_switch_timer_interval)) {
2010 xmlChar *content;
2011 uint64_t switch_timer_interval = 0;
2012
2013 /* switch_timer_interval */
2014 content = xmlNodeGetContent(attr_node);
2015 if (!content) {
2016 ret = -LTTNG_ERR_NOMEM;
2017 goto end;
2018 }
2019
2020 ret = parse_uint(content, &switch_timer_interval);
2021 free(content);
2022 if (ret) {
2023 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2024 goto end;
2025 }
2026
2027 if (switch_timer_interval > UINT_MAX) {
2028 WARN("switch_timer_interval out of range.");
2029 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2030 goto end;
2031 }
2032
2033 channel->attr.switch_timer_interval =
2034 switch_timer_interval;
2035 } else if (!strcmp((const char *) attr_node->name,
2036 config_element_read_timer_interval)) {
2037 xmlChar *content;
2038 uint64_t read_timer_interval = 0;
2039
2040 /* read_timer_interval */
2041 content = xmlNodeGetContent(attr_node);
2042 if (!content) {
2043 ret = -LTTNG_ERR_NOMEM;
2044 goto end;
2045 }
2046
2047 ret = parse_uint(content, &read_timer_interval);
2048 free(content);
2049 if (ret) {
2050 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2051 goto end;
2052 }
2053
2054 if (read_timer_interval > UINT_MAX) {
2055 WARN("read_timer_interval out of range.");
2056 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2057 goto end;
2058 }
2059
2060 channel->attr.read_timer_interval =
2061 read_timer_interval;
2062 } else if (!strcmp((const char *) attr_node->name,
2063 config_element_output_type)) {
2064 xmlChar *content;
2065
2066 /* output_type */
2067 content = xmlNodeGetContent(attr_node);
2068 if (!content) {
2069 ret = -LTTNG_ERR_NOMEM;
2070 goto end;
2071 }
2072
2073 ret = get_output_type(content);
2074 free(content);
2075 if (ret < 0) {
2076 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2077 goto end;
2078 }
2079
2080 channel->attr.output = ret;
2081 } else if (!strcmp((const char *) attr_node->name,
2082 config_element_tracefile_size)) {
2083 xmlChar *content;
2084
2085 /* tracefile_size */
2086 content = xmlNodeGetContent(attr_node);
2087 if (!content) {
2088 ret = -LTTNG_ERR_NOMEM;
2089 goto end;
2090 }
2091
2092 ret = parse_uint(content, &channel->attr.tracefile_size);
2093 free(content);
2094 if (ret) {
2095 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2096 goto end;
2097 }
2098 } else if (!strcmp((const char *) attr_node->name,
2099 config_element_tracefile_count)) {
2100 xmlChar *content;
2101
2102 /* tracefile_count */
2103 content = xmlNodeGetContent(attr_node);
2104 if (!content) {
2105 ret = -LTTNG_ERR_NOMEM;
2106 goto end;
2107 }
2108
2109 ret = parse_uint(content, &channel->attr.tracefile_count);
2110 free(content);
2111 if (ret) {
2112 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2113 goto end;
2114 }
2115 } else if (!strcmp((const char *) attr_node->name,
2116 config_element_live_timer_interval)) {
2117 xmlChar *content;
2118 uint64_t live_timer_interval = 0;
2119
2120 /* live_timer_interval */
2121 content = xmlNodeGetContent(attr_node);
2122 if (!content) {
2123 ret = -LTTNG_ERR_NOMEM;
2124 goto end;
2125 }
2126
2127 ret = parse_uint(content, &live_timer_interval);
2128 free(content);
2129 if (ret) {
2130 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2131 goto end;
2132 }
2133
2134 if (live_timer_interval > UINT_MAX) {
2135 WARN("live_timer_interval out of range.");
2136 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2137 goto end;
2138 }
2139
2140 channel->attr.live_timer_interval =
2141 live_timer_interval;
2142 } else if (!strcmp((const char *) attr_node->name,
2143 config_element_events)) {
2144 /* events */
2145 *events_node = attr_node;
2146 } else {
2147 /* contexts */
2148 *contexts_node = attr_node;
2149 }
2150
2151 ret = 0;
2152 end:
2153 return ret;
2154 }
2155
2156 static
2157 int process_context_node(xmlNodePtr context_node,
2158 struct lttng_handle *handle, const char *channel_name)
2159 {
2160 int ret;
2161 struct lttng_event_context context;
2162 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2163
2164 assert(handle);
2165 assert(channel_name);
2166
2167 if (!context_child_node) {
2168 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2169 goto end;
2170 }
2171
2172 memset(&context, 0, sizeof(context));
2173
2174 if (!strcmp((const char *) context_child_node->name,
2175 config_element_type)) {
2176 /* type */
2177 xmlChar *content = xmlNodeGetContent(context_child_node);
2178
2179 if (!content) {
2180 ret = -LTTNG_ERR_NOMEM;
2181 goto end;
2182 }
2183
2184 ret = get_context_type(content);
2185 free(content);
2186 if (ret < 0) {
2187 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2188 goto end;
2189 }
2190
2191 context.ctx = ret;
2192 } else if (!strcmp((const char *) context_child_node->name,
2193 config_element_context_perf)) {
2194 /* perf */
2195 xmlNodePtr perf_attr_node;
2196
2197 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2198 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2199 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
2200 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2201 perf_attr_node; perf_attr_node =
2202 xmlNextElementSibling(perf_attr_node)) {
2203 if (!strcmp((const char *) perf_attr_node->name,
2204 config_element_type)) {
2205 xmlChar *content;
2206 uint64_t type = 0;
2207
2208 /* type */
2209 content = xmlNodeGetContent(perf_attr_node);
2210 if (!content) {
2211 ret = -LTTNG_ERR_NOMEM;
2212 goto end;
2213 }
2214
2215 ret = parse_uint(content, &type);
2216 free(content);
2217 if (ret) {
2218 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2219 goto end;
2220 }
2221
2222 if (type > UINT32_MAX) {
2223 WARN("perf context type out of range.");
2224 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2225 goto end;
2226 }
2227
2228 context.u.perf_counter.type = type;
2229 } else if (!strcmp((const char *) perf_attr_node->name,
2230 config_element_config)) {
2231 xmlChar *content;
2232 uint64_t config = 0;
2233
2234 /* config */
2235 content = xmlNodeGetContent(perf_attr_node);
2236 if (!content) {
2237 ret = -LTTNG_ERR_NOMEM;
2238 goto end;
2239 }
2240
2241 ret = parse_uint(content, &config);
2242 free(content);
2243 if (ret) {
2244 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2245 goto end;
2246 }
2247
2248 context.u.perf_counter.config = config;
2249 } else if (!strcmp((const char *) perf_attr_node->name,
2250 config_element_name)) {
2251 xmlChar *content;
2252 size_t name_len;
2253
2254 /* name */
2255 content = xmlNodeGetContent(perf_attr_node);
2256 if (!content) {
2257 ret = -LTTNG_ERR_NOMEM;
2258 goto end;
2259 }
2260
2261 name_len = strlen((char *) content);
2262 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2263 WARN("perf context name too long.");
2264 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2265 free(content);
2266 goto end;
2267 }
2268
2269 strncpy(context.u.perf_counter.name, (const char *) content,
2270 name_len);
2271 free(content);
2272 }
2273 }
2274 } else if (!strcmp((const char *) context_child_node->name,
2275 config_element_context_app)) {
2276 /* application context */
2277 xmlNodePtr app_ctx_node;
2278
2279 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2280 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2281 app_ctx_node; app_ctx_node =
2282 xmlNextElementSibling(app_ctx_node)) {
2283 xmlChar *content;
2284 char **target = strcmp(
2285 (const char *) app_ctx_node->name,
2286 config_element_context_app_provider_name) == 0 ?
2287 &context.u.app_ctx.provider_name :
2288 &context.u.app_ctx.ctx_name;
2289
2290 content = xmlNodeGetContent(app_ctx_node);
2291 if (!content) {
2292 ret = -LTTNG_ERR_NOMEM;
2293 goto end;
2294 }
2295
2296 *target = (char *) content;
2297 }
2298 } else {
2299 /* Unrecognized context type */
2300 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2301 goto end;
2302 }
2303
2304 ret = lttng_add_context(handle, &context, NULL, channel_name);
2305 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2306 free(context.u.app_ctx.provider_name);
2307 free(context.u.app_ctx.ctx_name);
2308 }
2309 end:
2310 return ret;
2311 }
2312
2313 static
2314 int process_contexts_node(xmlNodePtr contexts_node,
2315 struct lttng_handle *handle, const char *channel_name)
2316 {
2317 int ret = 0;
2318 xmlNodePtr context_node;
2319
2320 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2321 context_node = xmlNextElementSibling(context_node)) {
2322 ret = process_context_node(context_node, handle, channel_name);
2323 if (ret) {
2324 goto end;
2325 }
2326 }
2327 end:
2328 return ret;
2329 }
2330
2331 static
2332 int process_pid_tracker_node(xmlNodePtr pid_tracker_node,
2333 struct lttng_handle *handle)
2334 {
2335 int ret = 0, child;
2336 xmlNodePtr targets_node = NULL;
2337 xmlNodePtr node;
2338
2339 assert(handle);
2340 assert(pid_tracker_node);
2341 /* get the targets node */
2342 for (node = xmlFirstElementChild(pid_tracker_node); node;
2343 node = xmlNextElementSibling(node)) {
2344 if (!strcmp((const char *) node->name,
2345 config_element_targets)) {
2346 targets_node = node;
2347 break;
2348 }
2349 }
2350
2351 if (!targets_node) {
2352 ret = LTTNG_ERR_INVALID;
2353 goto end;
2354 }
2355
2356 /* Go through all pid_target node */
2357 child = xmlChildElementCount(targets_node);
2358 if (child == 0) {
2359 /* The session is explicitly set to target nothing. */
2360 ret = lttng_untrack_pid(handle, -1);
2361 if (ret) {
2362 goto end;
2363 }
2364 }
2365 for (node = xmlFirstElementChild(targets_node); node;
2366 node = xmlNextElementSibling(node)) {
2367 xmlNodePtr pid_target_node = node;
2368
2369 /* get pid node and track it */
2370 for (node = xmlFirstElementChild(pid_target_node); node;
2371 node = xmlNextElementSibling(node)) {
2372 if (!strcmp((const char *) node->name,
2373 config_element_pid)) {
2374 int64_t pid;
2375 xmlChar *content = NULL;
2376
2377 content = xmlNodeGetContent(node);
2378 if (!content) {
2379 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2380 goto end;
2381 }
2382
2383 ret = parse_int(content, &pid);
2384 free(content);
2385 if (ret) {
2386 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2387 goto end;
2388 }
2389
2390 ret = lttng_track_pid(handle, (int) pid);
2391 if (ret) {
2392 goto end;
2393 }
2394 }
2395 }
2396 node = pid_target_node;
2397 }
2398
2399 end:
2400 return ret;
2401 }
2402
2403
2404 static
2405 int process_domain_node(xmlNodePtr domain_node, const char *session_name,
2406 bool snapshot_mode)
2407 {
2408 int ret;
2409 struct lttng_domain domain = { 0 };
2410 struct lttng_handle *handle = NULL;
2411 xmlNodePtr channels_node = NULL;
2412 xmlNodePtr trackers_node = NULL;
2413 xmlNodePtr pid_tracker_node = NULL;
2414 xmlNodePtr node;
2415
2416 assert(session_name);
2417
2418 ret = init_domain(domain_node, &domain);
2419 if (ret) {
2420 goto end;
2421 }
2422
2423 handle = lttng_create_handle(session_name, &domain);
2424 if (!handle) {
2425 ret = -LTTNG_ERR_NOMEM;
2426 goto end;
2427 }
2428
2429 /* get the channels node */
2430 for (node = xmlFirstElementChild(domain_node); node;
2431 node = xmlNextElementSibling(node)) {
2432 if (!strcmp((const char *) node->name,
2433 config_element_channels)) {
2434 channels_node = node;
2435 break;
2436 }
2437 }
2438
2439 if (!channels_node) {
2440 goto end;
2441 }
2442
2443 /* create all channels */
2444 for (node = xmlFirstElementChild(channels_node); node;
2445 node = xmlNextElementSibling(node)) {
2446 struct lttng_channel channel;
2447 xmlNodePtr contexts_node = NULL;
2448 xmlNodePtr events_node = NULL;
2449 xmlNodePtr channel_attr_node;
2450
2451 memset(&channel, 0, sizeof(channel));
2452 lttng_channel_set_default_attr(&domain, &channel.attr);
2453
2454 for (channel_attr_node = xmlFirstElementChild(node);
2455 channel_attr_node; channel_attr_node =
2456 xmlNextElementSibling(channel_attr_node)) {
2457 ret = process_channel_attr_node(channel_attr_node,
2458 snapshot_mode, domain.type, &channel,
2459 &contexts_node, &events_node);
2460 if (ret) {
2461 goto end;
2462 }
2463 }
2464
2465 if (!channel.name) {
2466 ERR("Encountered a channel with no name attribute.");
2467 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2468 goto end;
2469 }
2470
2471 ret = lttng_enable_channel(handle, &channel);
2472 if (ret < 0) {
2473 goto end;
2474 }
2475
2476 ret = process_events_node(events_node, handle, channel.name);
2477 if (ret) {
2478 goto end;
2479 }
2480
2481 ret = process_contexts_node(contexts_node, handle,
2482 channel.name);
2483 if (ret) {
2484 goto end;
2485 }
2486 }
2487
2488 /* get the trackers node */
2489 for (node = xmlFirstElementChild(domain_node); node;
2490 node = xmlNextElementSibling(node)) {
2491 if (!strcmp((const char *) node->name,
2492 config_element_trackers)) {
2493 trackers_node = node;
2494 break;
2495 }
2496 }
2497
2498 if (!trackers_node) {
2499 goto end;
2500 }
2501
2502 for (node = xmlFirstElementChild(trackers_node); node;
2503 node = xmlNextElementSibling(node)) {
2504 if (!strcmp((const char *)node->name,config_element_pid_tracker)) {
2505 pid_tracker_node = node;
2506 ret = process_pid_tracker_node(pid_tracker_node, handle);
2507 if (ret) {
2508 goto end;
2509 }
2510 }
2511 }
2512
2513 if (!pid_tracker_node) {
2514 lttng_track_pid(handle, -1);
2515 }
2516
2517 end:
2518 lttng_destroy_handle(handle);
2519 return ret;
2520 }
2521
2522 static
2523 int process_session_node(xmlNodePtr session_node, const char *session_name,
2524 int override)
2525 {
2526 int ret, started = -1, snapshot_mode = -1;
2527 uint64_t live_timer_interval = UINT64_MAX;
2528 xmlChar *name = NULL;
2529 xmlChar *shm_path = NULL;
2530 xmlNodePtr domains_node = NULL;
2531 xmlNodePtr output_node = NULL;
2532 xmlNodePtr node;
2533 struct lttng_domain *kernel_domain = NULL;
2534 struct lttng_domain *ust_domain = NULL;
2535 struct lttng_domain *jul_domain = NULL;
2536 struct lttng_domain *log4j_domain = NULL;
2537 struct lttng_domain *python_domain = NULL;
2538
2539 for (node = xmlFirstElementChild(session_node); node;
2540 node = xmlNextElementSibling(node)) {
2541 if (!name && !strcmp((const char *) node->name,
2542 config_element_name)) {
2543 /* name */
2544 xmlChar *node_content = xmlNodeGetContent(node);
2545 if (!node_content) {
2546 ret = -LTTNG_ERR_NOMEM;
2547 goto error;
2548 }
2549
2550 name = node_content;
2551 } else if (!domains_node && !strcmp((const char *) node->name,
2552 config_element_domains)) {
2553 /* domains */
2554 domains_node = node;
2555 } else if (started == -1 && !strcmp((const char *) node->name,
2556 config_element_started)) {
2557 /* started */
2558 xmlChar *node_content = xmlNodeGetContent(node);
2559 if (!node_content) {
2560 ret = -LTTNG_ERR_NOMEM;
2561 goto error;
2562 }
2563
2564 ret = parse_bool(node_content, &started);
2565 free(node_content);
2566 if (ret) {
2567 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2568 goto error;
2569 }
2570 } else if (!output_node && !strcmp((const char *) node->name,
2571 config_element_output)) {
2572 /* output */
2573 output_node = node;
2574 } else if (!shm_path && !strcmp((const char *) node->name,
2575 config_element_shared_memory_path)) {
2576 /* shared memory path */
2577 xmlChar *node_content = xmlNodeGetContent(node);
2578 if (!node_content) {
2579 ret = -LTTNG_ERR_NOMEM;
2580 goto error;
2581 }
2582
2583 shm_path = node_content;
2584 } else {
2585 /* attributes, snapshot_mode or live_timer_interval */
2586 xmlNodePtr attributes_child =
2587 xmlFirstElementChild(node);
2588
2589 if (!strcmp((const char *) attributes_child->name,
2590 config_element_snapshot_mode)) {
2591 /* snapshot_mode */
2592 xmlChar *snapshot_mode_content =
2593 xmlNodeGetContent(attributes_child);
2594 if (!snapshot_mode_content) {
2595 ret = -LTTNG_ERR_NOMEM;
2596 goto error;
2597 }
2598
2599 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2600 free(snapshot_mode_content);
2601 if (ret) {
2602 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2603 goto error;
2604 }
2605 } else {
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 }
2622 }
2623
2624 if (!name) {
2625 /* Mandatory attribute, as defined in the session XSD */
2626 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2627 goto error;
2628 }
2629
2630 if (session_name && strcmp((char *) name, session_name)) {
2631 /* This is not the session we are looking for */
2632 ret = -LTTNG_ERR_NO_SESSION;
2633 goto error;
2634 }
2635
2636 /* Init domains to create the session handles */
2637 for (node = xmlFirstElementChild(domains_node); node;
2638 node = xmlNextElementSibling(node)) {
2639 struct lttng_domain *domain;
2640
2641 domain = zmalloc(sizeof(*domain));
2642 if (!domain) {
2643 ret = -LTTNG_ERR_NOMEM;
2644 goto error;
2645 }
2646
2647 ret = init_domain(node, domain);
2648 if (ret) {
2649 goto domain_init_error;
2650 }
2651
2652 switch (domain->type) {
2653 case LTTNG_DOMAIN_KERNEL:
2654 if (kernel_domain) {
2655 /* Same domain seen twice, invalid! */
2656 goto domain_init_error;
2657 }
2658 kernel_domain = domain;
2659 break;
2660 case LTTNG_DOMAIN_UST:
2661 if (ust_domain) {
2662 /* Same domain seen twice, invalid! */
2663 goto domain_init_error;
2664 }
2665 ust_domain = domain;
2666 break;
2667 case LTTNG_DOMAIN_JUL:
2668 if (jul_domain) {
2669 /* Same domain seen twice, invalid! */
2670 goto domain_init_error;
2671 }
2672 jul_domain = domain;
2673 break;
2674 case LTTNG_DOMAIN_LOG4J:
2675 if (log4j_domain) {
2676 /* Same domain seen twice, invalid! */
2677 goto domain_init_error;
2678 }
2679 log4j_domain = domain;
2680 break;
2681 case LTTNG_DOMAIN_PYTHON:
2682 if (python_domain) {
2683 /* Same domain seen twice, invalid! */
2684 goto domain_init_error;
2685 }
2686 python_domain = domain;
2687 break;
2688 default:
2689 WARN("Invalid domain type");
2690 goto domain_init_error;
2691 }
2692 continue;
2693 domain_init_error:
2694 free(domain);
2695 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2696 goto error;
2697 }
2698
2699 if (override) {
2700 /* Destroy session if it exists */
2701 ret = lttng_destroy_session((const char *) name);
2702 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2703 ERR("Failed to destroy existing session.");
2704 goto error;
2705 }
2706 }
2707
2708 /* Create session type depending on output type */
2709 if (snapshot_mode && snapshot_mode != -1) {
2710 ret = create_snapshot_session((const char *) name, output_node);
2711 } else if (live_timer_interval &&
2712 live_timer_interval != UINT64_MAX) {
2713 ret = create_session((const char *) name, kernel_domain,
2714 ust_domain, jul_domain, log4j_domain,
2715 output_node, live_timer_interval);
2716 } else {
2717 /* regular session */
2718 ret = create_session((const char *) name, kernel_domain,
2719 ust_domain, jul_domain, log4j_domain,
2720 output_node, UINT64_MAX);
2721 }
2722 if (ret) {
2723 goto error;
2724 }
2725
2726 if (shm_path) {
2727 ret = lttng_set_session_shm_path((const char *) name,
2728 (const char *) shm_path);
2729 if (ret) {
2730 goto error;
2731 }
2732 }
2733
2734 for (node = xmlFirstElementChild(domains_node); node;
2735 node = xmlNextElementSibling(node)) {
2736 ret = process_domain_node(node, (const char *) name,
2737 snapshot_mode == 1);
2738 if (ret) {
2739 goto end;
2740 }
2741 }
2742
2743 if (started) {
2744 ret = lttng_start_tracing((const char *) name);
2745 if (ret) {
2746 goto end;
2747 }
2748 }
2749
2750 end:
2751 if (ret < 0) {
2752 ERR("Failed to load session %s: %s", (const char *) name,
2753 lttng_strerror(ret));
2754 lttng_destroy_session((const char *) name);
2755 }
2756
2757 error:
2758 free(kernel_domain);
2759 free(ust_domain);
2760 free(jul_domain);
2761 free(log4j_domain);
2762 free(python_domain);
2763 xmlFree(name);
2764 xmlFree(shm_path);
2765 return ret;
2766 }
2767
2768 /*
2769 * Return 1 if the given path is readable by the current UID or 0 if not.
2770 * Return -1 if the path is EPERM.
2771 */
2772 static int validate_file_read_creds(const char *path)
2773 {
2774 int ret;
2775
2776 assert(path);
2777
2778 /* Can we read the file. */
2779 ret = access(path, R_OK);
2780 if (!ret) {
2781 goto valid;
2782 }
2783 if (errno == EACCES) {
2784 return -1;
2785 } else {
2786 /* Invalid. */
2787 return 0;
2788 }
2789 valid:
2790 return 1;
2791 }
2792
2793 static
2794 int load_session_from_file(const char *path, const char *session_name,
2795 struct session_config_validation_ctx *validation_ctx, int override)
2796 {
2797 int ret, session_found = !session_name;
2798 xmlDocPtr doc = NULL;
2799 xmlNodePtr sessions_node;
2800 xmlNodePtr session_node;
2801
2802 assert(path);
2803 assert(validation_ctx);
2804
2805 ret = validate_file_read_creds(path);
2806 if (ret != 1) {
2807 if (ret == -1) {
2808 ret = -LTTNG_ERR_EPERM;
2809 } else {
2810 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2811 }
2812 goto end;
2813 }
2814
2815 doc = xmlParseFile(path);
2816 if (!doc) {
2817 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2818 goto end;
2819 }
2820
2821 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2822 if (ret) {
2823 ERR("Session configuration file validation failed");
2824 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2825 goto end;
2826 }
2827
2828 sessions_node = xmlDocGetRootElement(doc);
2829 if (!sessions_node) {
2830 goto end;
2831 }
2832
2833 for (session_node = xmlFirstElementChild(sessions_node);
2834 session_node; session_node =
2835 xmlNextElementSibling(session_node)) {
2836 ret = process_session_node(session_node,
2837 session_name, override);
2838 if (session_name && ret == 0) {
2839 /* Target session found and loaded */
2840 session_found = 1;
2841 break;
2842 }
2843 }
2844 end:
2845 xmlFreeDoc(doc);
2846 if (!ret) {
2847 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
2848 }
2849 return ret;
2850 }
2851
2852 static
2853 int load_session_from_document(struct config_document *document, const char *session_name,
2854 struct session_config_validation_ctx *validation_ctx, int override)
2855 {
2856 int ret, session_found = !session_name;
2857 xmlNodePtr sessions_node;
2858 xmlNodePtr session_node;
2859
2860 assert(validation_ctx);
2861
2862 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, document->document);
2863 if (ret) {
2864 ERR("Session configuration file validation failed");
2865 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2866 goto end;
2867 }
2868
2869 sessions_node = xmlDocGetRootElement(document->document);
2870 if (!sessions_node) {
2871 goto end;
2872 }
2873
2874 for (session_node = xmlFirstElementChild(sessions_node);
2875 session_node; session_node =
2876 xmlNextElementSibling(session_node)) {
2877 ret = process_session_node(session_node,
2878 session_name, override);
2879 if (session_name && ret == 0) {
2880 /* Target session found and loaded */
2881 session_found = 1;
2882 break;
2883 }
2884 }
2885 end:
2886 if (!ret) {
2887 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
2888 }
2889 return ret;
2890 }
2891
2892 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2893 static
2894 struct dirent *alloc_dirent(const char *path)
2895 {
2896 size_t len;
2897 long name_max;
2898 struct dirent *entry;
2899
2900 name_max = pathconf(path, _PC_NAME_MAX);
2901 if (name_max == -1) {
2902 name_max = PATH_MAX;
2903 }
2904 len = offsetof(struct dirent, d_name) + name_max + 1;
2905 entry = zmalloc(len);
2906 return entry;
2907 }
2908
2909 static
2910 int load_session_from_path(const char *path, const char *session_name,
2911 struct session_config_validation_ctx *validation_ctx, int override)
2912 {
2913 int ret, session_found = !session_name;
2914 DIR *directory = NULL;
2915
2916 assert(path);
2917 assert(validation_ctx);
2918
2919 directory = opendir(path);
2920 if (!directory) {
2921 switch (errno) {
2922 case ENOTDIR:
2923 /* Try the file loading. */
2924 break;
2925 case ENOENT:
2926 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2927 goto end;
2928 default:
2929 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2930 goto end;
2931 }
2932 }
2933 if (directory) {
2934 struct dirent *entry;
2935 struct dirent *result;
2936 char *file_path = NULL;
2937 size_t path_len = strlen(path);
2938
2939 if (path_len >= PATH_MAX) {
2940 ret = -LTTNG_ERR_INVALID;
2941 goto end;
2942 }
2943
2944 entry = alloc_dirent(path);
2945 if (!entry) {
2946 ret = -LTTNG_ERR_NOMEM;
2947 goto end;
2948 }
2949
2950 file_path = zmalloc(PATH_MAX);
2951 if (!file_path) {
2952 ret = -LTTNG_ERR_NOMEM;
2953 free(entry);
2954 goto end;
2955 }
2956
2957 strncpy(file_path, path, path_len);
2958 if (file_path[path_len - 1] != '/') {
2959 file_path[path_len++] = '/';
2960 }
2961
2962 ret = 0;
2963 /* Search for *.lttng files */
2964 while (!readdir_r(directory, entry, &result) && result) {
2965 size_t file_name_len = strlen(result->d_name);
2966
2967 if (file_name_len <=
2968 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2969 continue;
2970 }
2971
2972 if (path_len + file_name_len >= PATH_MAX) {
2973 continue;
2974 }
2975
2976 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2977 result->d_name + file_name_len - sizeof(
2978 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2979 continue;
2980 }
2981
2982 strncpy(file_path + path_len, result->d_name, file_name_len);
2983 file_path[path_len + file_name_len] = '\0';
2984
2985 ret = load_session_from_file(file_path, session_name,
2986 validation_ctx, override);
2987 if (session_name && !ret) {
2988 session_found = 1;
2989 break;
2990 }
2991 }
2992
2993 free(entry);
2994 free(file_path);
2995 } else {
2996 ret = load_session_from_file(path, session_name,
2997 validation_ctx, override);
2998 if (ret) {
2999 goto end;
3000 } else {
3001 session_found = 1;
3002 }
3003 }
3004
3005 end:
3006 if (directory) {
3007 if (closedir(directory)) {
3008 PERROR("closedir");
3009 }
3010 }
3011
3012 if (session_found) {
3013 ret = 0;
3014 }
3015
3016 return ret;
3017 }
3018
3019 /*
3020 * Validate that the given path's credentials and the current process have the
3021 * same UID. If so, return 1 else return 0 if it does NOT match.
3022 */
3023 static int validate_path_creds(const char *path)
3024 {
3025 int ret, uid = getuid();
3026 struct stat buf;
3027
3028 assert(path);
3029
3030 if (uid == 0) {
3031 goto valid;
3032 }
3033
3034 ret = stat(path, &buf);
3035 if (ret < 0) {
3036 if (errno != ENOENT) {
3037 PERROR("stat");
3038 }
3039 ret = -LTTNG_ERR_INVALID;
3040 goto valid;
3041 }
3042
3043 if (buf.st_uid != uid) {
3044 goto invalid;
3045 }
3046
3047 valid:
3048 return 1;
3049 invalid:
3050 return 0;
3051 }
3052
3053 LTTNG_HIDDEN
3054 int config_load_session(const char *path, const char *session_name,
3055 int override, unsigned int autoload)
3056 {
3057 int ret;
3058 bool session_loaded = false;
3059 const char *path_ptr = NULL;
3060 struct session_config_validation_ctx validation_ctx = { 0 };
3061
3062 ret = init_session_config_validation_ctx(&validation_ctx);
3063 if (ret) {
3064 goto end;
3065 }
3066
3067 if (!path) {
3068 char *home_path;
3069 const char *sys_path;
3070
3071 /* Try home path */
3072 home_path = utils_get_home_dir();
3073 if (home_path) {
3074 char path[PATH_MAX];
3075
3076 /*
3077 * Try user session configuration path. Ignore error here so we can
3078 * continue loading the system wide sessions.
3079 */
3080 if (autoload) {
3081 ret = snprintf(path, sizeof(path),
3082 DEFAULT_SESSION_HOME_CONFIGPATH "/"
3083 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
3084 if (ret < 0) {
3085 PERROR("snprintf session autoload home config path");
3086 goto end;
3087 }
3088
3089 /*
3090 * Credentials are only validated for the autoload in order to
3091 * avoid any user session daemon to try to load kernel sessions
3092 * automatically and failing all the times.
3093 */
3094 ret = validate_path_creds(path);
3095 if (ret) {
3096 path_ptr = path;
3097 }
3098 } else {
3099 ret = snprintf(path, sizeof(path),
3100 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
3101 if (ret < 0) {
3102 PERROR("snprintf session home config path");
3103 goto end;
3104 }
3105 path_ptr = path;
3106 }
3107 if (path_ptr) {
3108 ret = load_session_from_path(path_ptr, session_name,
3109 &validation_ctx, override);
3110 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3111 goto end;
3112 }
3113 /*
3114 * Continue even if the session was found since we have to try
3115 * the system wide sessions.
3116 */
3117 session_loaded = true;
3118 }
3119 }
3120
3121 /* Reset path pointer for the system wide dir. */
3122 path_ptr = NULL;
3123
3124 /* Try system wide configuration directory. */
3125 if (autoload) {
3126 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
3127 DEFAULT_SESSION_CONFIG_AUTOLOAD;
3128 ret = validate_path_creds(sys_path);
3129 if (ret) {
3130 path_ptr = sys_path;
3131 }
3132 } else {
3133 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
3134 path_ptr = sys_path;
3135 }
3136
3137 if (path_ptr) {
3138 ret = load_session_from_path(path_ptr, session_name,
3139 &validation_ctx, override);
3140 if (!ret) {
3141 session_loaded = true;
3142 }
3143 }
3144 } else {
3145 ret = access(path, F_OK);
3146 if (ret < 0) {
3147 PERROR("access");
3148 switch (errno) {
3149 case ENOENT:
3150 ret = -LTTNG_ERR_INVALID;
3151 WARN("Session configuration path does not exist.");
3152 break;
3153 case EACCES:
3154 ret = -LTTNG_ERR_EPERM;
3155 break;
3156 default:
3157 ret = -LTTNG_ERR_UNK;
3158 break;
3159 }
3160 goto end;
3161 }
3162
3163 ret = load_session_from_path(path, session_name,
3164 &validation_ctx, override);
3165 }
3166 end:
3167 fini_session_config_validation_ctx(&validation_ctx);
3168 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
3169 /*
3170 * Don't report an error if no sessions are found when called
3171 * without a session_name or a search path.
3172 */
3173 ret = 0;
3174 }
3175
3176 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
3177 /* A matching session was found in one of the search paths. */
3178 ret = 0;
3179 }
3180 return ret;
3181 }
3182
3183 static
3184 void __attribute__((destructor)) session_config_exit(void)
3185 {
3186 xmlCleanupParser();
3187 }
3188
3189 LTTNG_HIDDEN
3190 struct config_document *config_document_get(const char *path, int xsd_validation)
3191 {
3192 int ret;
3193 struct config_document *document = NULL;
3194 struct session_config_validation_ctx validation_ctx = { 0 };
3195
3196 assert(path);
3197
3198 ret = access(path, F_OK);
3199 if (ret < 0) {
3200 PERROR("access");
3201 switch (errno) {
3202 case ENOENT:
3203 ERR("Configuration document path does not exist.");
3204 break;
3205 case EACCES:
3206 ERR("Configuration document permission denied.");
3207 break;
3208 default:
3209 ERR("Configuration document unknown error.");
3210 break;
3211 }
3212 goto end;
3213 }
3214
3215 ret = init_session_config_validation_ctx(&validation_ctx);
3216 if (ret) {
3217 goto end;
3218 }
3219
3220 ret = validate_file_read_creds(path);
3221 if (ret != 1) {
3222 if (ret == -1) {
3223 ERR("Configuration document permission denied.");
3224 } else {
3225 ERR("Configuration document does not exist.");
3226 }
3227 goto end;
3228 }
3229
3230 document = zmalloc(sizeof(struct config_document));
3231 if (!document) {
3232 PERROR("zmalloc");
3233 goto end;
3234 }
3235
3236 /*
3237 * Do not care of blanks and also make sure that direct dump have no
3238 * indentations in them
3239 */
3240 xmlKeepBlanksDefault(0);
3241 document->document = xmlParseFile(path);
3242 if (!document->document) {
3243 ERR("Configuration document parsing failed.");
3244 goto error;
3245 }
3246
3247 /* TODO: REMOVE */
3248 ret = 0;
3249 if (xsd_validation) {
3250 ret = xmlSchemaValidateDoc(validation_ctx.schema_validation_ctx,
3251 document->document);
3252 }
3253 if (ret) {
3254 ERR("Session configuration file validation failed");
3255 goto error;
3256 }
3257
3258 end:
3259 fini_session_config_validation_ctx(&validation_ctx);
3260 return document;
3261 error:
3262 xmlFreeDoc(document->document);
3263 free(document);
3264 return NULL;
3265 }
3266
3267 LTTNG_HIDDEN
3268 void config_document_free(struct config_document *document)
3269 {
3270 if (document) {
3271 xmlFreeDoc(document->document);
3272 document->document = NULL;
3273 }
3274
3275 free(document);
3276 }
3277
3278 LTTNG_HIDDEN
3279 int config_document_replace_element_value(struct config_document *document,
3280 const char *xpath, const char *value)
3281 {
3282 int ret = 0;
3283 int xpath_result_size;
3284
3285 xmlXPathContextPtr xpath_context = NULL;
3286 xmlXPathObjectPtr xpath_object = NULL;
3287 xmlNodeSetPtr xpath_result_set = NULL;
3288 xmlChar *internal_xpath = NULL;
3289 xmlChar *internal_value = NULL;
3290
3291 assert(document);
3292 assert(xpath);
3293 assert(value);
3294
3295 internal_xpath = encode_string(xpath);
3296 if (!internal_xpath) {
3297 ret = -LTTNG_ERR_NOMEM;
3298 ERR("Unable to encode xpath string");
3299 goto end;
3300 }
3301
3302 internal_value = encode_string(value);
3303 if (!internal_value) {
3304 ret = -LTTNG_ERR_NOMEM;
3305 ERR("Unable to encode xpath replace value string");
3306 goto end;
3307 }
3308
3309 /* Initialize xpath context */
3310 xpath_context = xmlXPathNewContext(document->document);
3311 if (!xpath_context) {
3312 ret = -LTTNG_ERR_NOMEM;
3313 ERR("Unable to create xpath context");
3314 goto end;
3315 }
3316
3317 /* Evaluate de xpath expression */
3318 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3319 if (!xpath_object) {
3320 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3321 goto end;
3322 }
3323
3324 /* TODO: from here could be extracted and previopus could be a step in
3325 * subsequent operation, modify/substitute/delete node.
3326 */
3327 xpath_result_set = xpath_object->nodesetval;
3328 if (!xpath_result_set) {
3329 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3330 goto end;
3331 }
3332
3333 xpath_result_size = xpath_result_set->nodeNr;
3334
3335 /*
3336 * Reverse traversal since last element could be nested under parent
3337 * element present in the result set.
3338 */
3339 for (int i = xpath_result_size - 1; i >=0; i--) {
3340 assert(xpath_result_set->nodeTab[i]);
3341 xmlNodeSetContent(xpath_result_set->nodeTab[i], internal_value);
3342
3343 /*
3344 * Libxml2 quirk regarding the freing and namesplace node see
3345 * libxml2 example and documentation for more details.
3346 */
3347 if (xpath_result_set->nodeTab[i]->type != XML_NAMESPACE_DECL) {
3348 xpath_result_set->nodeTab[i] = NULL;
3349 }
3350 }
3351
3352 end:
3353 xmlXPathFreeContext(xpath_context);
3354 xmlXPathFreeObject(xpath_object);
3355 xmlFree(internal_xpath);
3356 xmlFree(internal_value);
3357 return ret;
3358 }
3359
3360 LTTNG_HIDDEN
3361 int config_load_configuration_sessions(struct config_document *document,
3362 const char *session_name, int override)
3363 {
3364 int ret;
3365 struct session_config_validation_ctx validation_ctx = { 0 };
3366
3367 ret = init_session_config_validation_ctx(&validation_ctx);
3368 if (ret) {
3369 goto end;
3370 }
3371 ret = load_session_from_document(document, session_name,
3372 &validation_ctx, override);
3373 end:
3374 fini_session_config_validation_ctx(&validation_ctx);
3375 return ret;
3376 }
3377
3378 LTTNG_HIDDEN
3379 int config_document_replace_element(struct config_document *document,
3380 const char *xpath, const struct config_element *element)
3381 {
3382 int ret = 0;
3383 int xpath_result_size;
3384
3385 xmlXPathContextPtr xpath_context = NULL;
3386 xmlXPathObjectPtr xpath_object = NULL;
3387 xmlNodeSetPtr xpath_result_set = NULL;
3388 xmlChar *internal_xpath = NULL;
3389 xmlNodePtr old_node = NULL;
3390 xmlNodePtr copy = NULL;
3391
3392 assert(document);
3393 assert(xpath);
3394 assert(element);
3395 assert(element->element);
3396
3397 internal_xpath = encode_string(xpath);
3398 if (!internal_xpath) {
3399 ret = -LTTNG_ERR_NOMEM;
3400 ERR("Unable to encode xpath string");
3401 goto end;
3402 }
3403
3404 /* Initialize xpath context */
3405 xpath_context = xmlXPathNewContext(document->document);
3406 if (!xpath_context) {
3407 ret = -LTTNG_ERR_NOMEM;
3408 ERR("Unable to create xpath context");
3409 goto end;
3410 }
3411
3412 /* Evaluate the xpath expression */
3413 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3414 if (!xpath_object) {
3415 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3416 goto end;
3417 }
3418
3419 xpath_result_set = xpath_object->nodesetval;
3420 if (!xpath_result_set) {
3421 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3422 goto end;
3423 }
3424
3425 xpath_result_size = xpath_result_set->nodeNr;
3426
3427 if (xpath_result_size > 1) {
3428 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3429 goto end;
3430 }
3431
3432 if (xpath_result_size == 0) {
3433 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3434 goto end;
3435 }
3436
3437 assert(xpath_result_set->nodeTab[0]);
3438
3439 /* Do a copy of the element to ease caller memory management */
3440 copy = xmlCopyNode(element->element, 1);
3441 if (!copy) {
3442 ret = -LTTNG_ERR_NOMEM;
3443 goto end;
3444 }
3445
3446 old_node = xmlReplaceNode(xpath_result_set->nodeTab[0], copy);
3447 if (xpath_result_set->nodeTab[0]->type != XML_NAMESPACE_DECL)
3448 xpath_result_set->nodeTab[0] = NULL;
3449 if (!old_node) {
3450 ret = -LTTNG_ERR_CONFIG_REPLACEMENT;
3451 xmlFreeNode(copy);
3452 goto end;
3453 }
3454 xmlUnlinkNode(old_node);
3455 xmlFreeNode(old_node);
3456 end:
3457 xmlXPathFreeContext(xpath_context);
3458 xmlXPathFreeObject(xpath_object);
3459 xmlFree(internal_xpath);
3460 return ret;
3461 }
3462
3463 LTTNG_HIDDEN
3464 char *config_document_get_element_value(struct config_document *document,
3465 const char *xpath)
3466 {
3467 char *value = NULL;
3468 int xpath_result_size;
3469 int value_result_size;
3470
3471 xmlXPathContextPtr xpath_context = NULL;
3472 xmlXPathObjectPtr xpath_object = NULL;
3473 xmlNodeSetPtr xpath_result_set = NULL;
3474 xmlChar *internal_xpath = NULL;
3475 xmlChar *internal_value = NULL;
3476
3477 assert(document);
3478 assert(xpath);
3479
3480 internal_xpath = encode_string(xpath);
3481 if (!internal_xpath) {
3482 value = NULL;
3483 ERR("Unable to encode xpath string");
3484 goto end;
3485 }
3486
3487 /* Initialize xpath context */
3488 xpath_context = xmlXPathNewContext(document->document);
3489 if (!xpath_context) {
3490 value = NULL;
3491 ERR("Unable to create xpath context");
3492 goto end;
3493 }
3494
3495 /* Evaluate the xpath expression */
3496 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3497 if (!xpath_object) {
3498 value = NULL;
3499 ERR("Unable to evaluate xpath query (invalid query format)");
3500 goto end;
3501 }
3502
3503 xpath_result_set = xpath_object->nodesetval;
3504 if (!xpath_result_set) {
3505 value = NULL;
3506 goto end;
3507 }
3508
3509 xpath_result_size = xpath_result_set->nodeNr;
3510
3511 if (xpath_result_size > 1) {
3512 ERR("To many result while fetching config element value");
3513 value = NULL;
3514 goto end;
3515 }
3516
3517 if (xpath_result_size == 0) {
3518 value = NULL;
3519 goto end;
3520 }
3521
3522 /*
3523 * Reverse traversal since last element could be nested under parent
3524 * element present in the result set.
3525 */
3526 assert(xpath_result_set->nodeTab[0]);
3527 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
3528 if (!internal_value) {
3529 value = NULL;
3530 goto end;
3531 }
3532
3533 value_result_size = xmlStrlen(internal_value);
3534 value = calloc(value_result_size + 1, sizeof(char));
3535 strncpy(value, (char *) internal_value, value_result_size);
3536
3537 end:
3538 xmlXPathFreeContext(xpath_context);
3539 xmlXPathFreeObject(xpath_object);
3540 xmlFree(internal_xpath);
3541 xmlFree(internal_value);
3542 return value;
3543 }
3544
3545 LTTNG_HIDDEN
3546 int config_document_element_exist(struct config_document *document,
3547 const char *xpath)
3548 {
3549 int exist = 0;
3550 int xpath_result_size;
3551
3552 xmlXPathContextPtr xpath_context = NULL;
3553 xmlXPathObjectPtr xpath_object = NULL;
3554 xmlNodeSetPtr xpath_result_set = NULL;
3555 xmlChar *internal_xpath = NULL;
3556
3557 assert(document);
3558 assert(document->document);
3559 assert(xpath);
3560
3561 internal_xpath = encode_string(xpath);
3562 if (!internal_xpath) {
3563 ERR("Unable to encode xpath string");
3564 goto end;
3565 }
3566
3567 /* Initialize xpath context */
3568 xpath_context = xmlXPathNewContext(document->document);
3569 if (!xpath_context) {
3570 ERR("Unable to create xpath context");
3571 goto end;
3572 }
3573
3574 /* Evaluate the xpath expression */
3575 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3576 if (!xpath_object) {
3577 ERR("Unable to evaluate xpath query (invalid query format)");
3578 goto end;
3579 }
3580
3581 xpath_result_set = xpath_object->nodesetval;
3582 if (!xpath_result_set) {
3583 goto end;
3584 }
3585
3586 xpath_result_size = xpath_result_set->nodeNr;
3587
3588 if (xpath_result_size > 0) {
3589 exist = 1;
3590 }
3591 end:
3592 xmlXPathFreeContext(xpath_context);
3593 xmlXPathFreeObject(xpath_object);
3594 xmlFree(internal_xpath);
3595 return exist;
3596
3597 }
3598
3599 LTTNG_HIDDEN
3600 struct config_element *config_element_create(const char *name,
3601 const char* value)
3602 {
3603 struct config_element *element;
3604
3605 assert(name);
3606
3607 xmlChar *internal_name = NULL;
3608 xmlChar *internal_value = NULL;
3609
3610 internal_name = encode_string(name);
3611 if (!internal_name) {
3612 ERR("Unable to encode config element name string");
3613 element = NULL;
3614 goto end;
3615 }
3616
3617
3618 if (value) {
3619 internal_value = encode_string(value);
3620 if (!internal_value) {
3621 ERR("Unable to encode config_element value string");
3622 element = NULL;
3623 goto end;
3624 }
3625 }
3626
3627 element = zmalloc(sizeof(struct config_element));
3628 if (!element) {
3629 goto end;
3630 }
3631
3632 element->element = xmlNewNode(NULL, internal_name);
3633 if (!element->element) {
3634 free(element);
3635 element = NULL;
3636 goto end;
3637 }
3638
3639 if (internal_value) {
3640 xmlNodeAddContent(element->element, internal_value);
3641 }
3642 end:
3643 xmlFree(internal_name);
3644 xmlFree(internal_value);
3645 return element;
3646 }
3647
3648 LTTNG_HIDDEN
3649 int config_element_add_child(struct config_element *parent,
3650 const struct config_element *child)
3651 {
3652 assert(parent);
3653 assert(child);
3654 assert(parent->element);
3655 assert(child->element);
3656
3657 int ret = 0;
3658 xmlNodePtr node = NULL;
3659 xmlNodePtr copy = NULL;
3660
3661 /* Do a copy to ease the memory management for caller */
3662 copy = xmlCopyNode(child->element, 1);
3663 if (!copy) {
3664 ret = -LTTNG_ERR_NOMEM;
3665 goto error;
3666 }
3667 node = xmlAddChild(parent->element, copy);
3668 if (!node) {
3669 xmlFreeNode(copy);
3670 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3671 goto error;
3672 }
3673 error:
3674 return ret;
3675 }
3676
3677
3678 LTTNG_HIDDEN
3679 int config_document_insert_element(struct config_document *document,
3680 const char *xpath, const struct config_element *element)
3681 {
3682 int ret = 0;
3683 int xpath_result_size;
3684
3685 xmlXPathContextPtr xpath_context = NULL;
3686 xmlXPathObjectPtr xpath_object = NULL;
3687 xmlNodeSetPtr xpath_result_set = NULL;
3688 xmlChar *internal_xpath = NULL;
3689 xmlNodePtr child_node = NULL;
3690 xmlNodePtr local_copy = NULL;
3691
3692 assert(document);
3693 assert(xpath);
3694 assert(element);
3695 assert(element->element);
3696
3697 internal_xpath = encode_string(xpath);
3698 if (!internal_xpath) {
3699 ret = -LTTNG_ERR_NOMEM;
3700 goto end;
3701 }
3702
3703 /* Initialize xpath context */
3704 xpath_context = xmlXPathNewContext(document->document);
3705 if (!xpath_context) {
3706 ret = -LTTNG_ERR_NOMEM;
3707 goto end;
3708 }
3709
3710 /* Evaluate the xpath expression */
3711 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3712 if (!xpath_object) {
3713 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3714 goto end;
3715 }
3716
3717 xpath_result_set = xpath_object->nodesetval;
3718 if (!xpath_result_set) {
3719 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3720 goto end;
3721 }
3722
3723 xpath_result_size = xpath_result_set->nodeNr;
3724
3725 if (xpath_result_size > 1) {
3726 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3727 goto end;
3728 }
3729
3730 if (xpath_result_size == 0) {
3731 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3732 goto end;
3733 }
3734
3735 assert(xpath_result_set->nodeTab[0]);
3736 /* Do a copy to simply memory management */
3737 local_copy = xmlCopyNode(element->element, 1);
3738 if (!local_copy) {
3739 ret = -LTTNG_ERR_NOMEM;
3740 goto end;
3741 }
3742
3743 child_node = xmlAddChild(xpath_result_set->nodeTab[0], local_copy);
3744 if (!child_node) {
3745 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3746 xmlFreeNode(local_copy);
3747 goto end;
3748 }
3749 end:
3750 xmlXPathFreeContext(xpath_context);
3751 xmlXPathFreeObject(xpath_object);
3752 xmlFree(internal_xpath);
3753 return ret;
3754 }
3755
3756 LTTNG_HIDDEN
3757 void config_element_free(struct config_element *element) {
3758 if (element && element->element) {
3759 xmlFreeNode(element->element);
3760 }
3761
3762 free(element);
3763 }
3764
3765 LTTNG_HIDDEN
3766 void config_element_free_array(struct config_element **array, int size) {
3767
3768 if (array) {
3769 for (int i = 0; i < size; i++) {
3770 if (!array[i]) {
3771 continue;
3772 }
3773 if (array[i]->element) {
3774 xmlFreeNode(array[i]->element);
3775 }
3776 free(array[i]);
3777 }
3778 }
3779 free(array);
3780 }
3781
3782 LTTNG_HIDDEN
3783 void config_document_get_element_array(const struct config_document *document, const char *xpath, struct config_element ***element_array, int *array_size)
3784 {
3785 int xpath_result_size;
3786
3787 xmlXPathContextPtr xpath_context = NULL;
3788 xmlXPathObjectPtr xpath_object = NULL;
3789 xmlNodeSetPtr xpath_result_set = NULL;
3790 xmlChar *internal_xpath = NULL;
3791 xmlChar *internal_value = NULL;
3792
3793 assert(document);
3794 assert(xpath);
3795
3796 *element_array=NULL;
3797 *array_size=0;
3798
3799
3800 internal_xpath = encode_string(xpath);
3801 if (!internal_xpath) {
3802 ERR("Unable to encode xpath string");
3803 goto end;
3804 }
3805
3806 /* Initialize xpath context */
3807 xpath_context = xmlXPathNewContext(document->document);
3808 if (!xpath_context) {
3809 ERR("Unable to create xpath context");
3810 goto end;
3811 }
3812
3813 /* Evaluate the xpath expression */
3814 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3815 if (!xpath_object) {
3816 ERR("Unable to evaluate xpath query (invalid query format)");
3817 goto end;
3818 }
3819
3820 xpath_result_set = xpath_object->nodesetval;
3821 if (!xpath_result_set) {
3822 goto end;
3823 }
3824
3825 xpath_result_size = xpath_result_set->nodeNr;
3826
3827 if (xpath_result_size == 0) {
3828 goto end;
3829 }
3830
3831 /* alloc the array */
3832 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3833 *array_size = xpath_result_size;
3834 if (!element_array) {
3835 ERR("Calloc config element array");
3836 goto end;
3837 }
3838
3839 for (int i = 0; i < xpath_result_size; i++) {
3840 xmlNodePtr node_copy = NULL;
3841 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3842 if (!node_copy) {
3843 ERR("Xml copy of event node");
3844 goto error;
3845 }
3846 /* Hand off ownership */
3847 (*element_array)[i] = malloc(sizeof(struct config_element));
3848 if (!((*element_array)[i])){
3849 ERR("Malloc config_element array");
3850 xmlFreeNode(node_copy);
3851 goto error;
3852 };
3853 (*element_array)[i]->element = node_copy;
3854 }
3855 end:
3856 xmlXPathFreeContext(xpath_context);
3857 xmlXPathFreeObject(xpath_object);
3858 xmlFree(internal_xpath);
3859 xmlFree(internal_value);
3860 return;
3861 error:
3862
3863 if (element_array) {
3864 config_element_free_array(*element_array, *array_size);
3865 }
3866
3867 *array_size = 0;
3868
3869 xmlXPathFreeContext(xpath_context);
3870 xmlXPathFreeObject(xpath_object);
3871 xmlFree(internal_xpath);
3872 xmlFree(internal_value);
3873 return;
3874 }
3875
3876 LTTNG_HIDDEN
3877 void config_element_get_element_array(const struct config_element *element, const char *xpath, struct config_element ***element_array, int *array_size)
3878 {
3879 int xpath_result_size;
3880
3881 xmlXPathContextPtr xpath_context = NULL;
3882 xmlXPathObjectPtr xpath_object = NULL;
3883 xmlNodeSetPtr xpath_result_set = NULL;
3884 xmlChar *internal_xpath = NULL;
3885 xmlChar *internal_value = NULL;
3886 xmlNodePtr local_copy = NULL;
3887 xmlNodePtr place_holder = NULL;
3888 xmlDocPtr document = NULL;
3889
3890 assert(element);
3891 assert(element->element);
3892 assert(xpath);
3893
3894 *element_array=NULL;
3895 *array_size=0;
3896
3897 /* Create a new doc and set root node as passed element */
3898 document = xmlNewDoc(BAD_CAST "1.0");
3899 if (!document) {
3900 ERR("Unable to create xml document");
3901 goto end;
3902 }
3903
3904 local_copy = xmlCopyNode(element->element, 1);
3905 if (!local_copy) {
3906 ERR("Unable to copy an xml node");
3907 goto end;
3908 }
3909
3910 /* Hand off the local_copy to the xmlDoc */
3911 place_holder = xmlDocSetRootElement(document, local_copy);
3912 if (place_holder) {
3913 ERR("The document root was already set");
3914 xmlFreeNode(local_copy);
3915 goto end;
3916 }
3917
3918 internal_xpath = encode_string(xpath);
3919 if (!internal_xpath) {
3920 ERR("Unable to encode xpath string");
3921 goto end;
3922 }
3923
3924 /* Initialize xpath context */
3925 xpath_context = xmlXPathNewContext(document);
3926 if (!xpath_context) {
3927 ERR("Unable to create xpath context");
3928 goto end;
3929 }
3930
3931 /* Evaluate the xpath expression */
3932 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3933 if (!xpath_object) {
3934 ERR("Unable to evaluate xpath query (invalid query format)");
3935 goto end;
3936 }
3937
3938 xpath_result_set = xpath_object->nodesetval;
3939 if (!xpath_result_set) {
3940 goto end;
3941 }
3942
3943 xpath_result_size = xpath_result_set->nodeNr;
3944
3945 if (xpath_result_size == 0) {
3946 goto end;
3947 }
3948
3949 /* alloc the array */
3950 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3951 *array_size = xpath_result_size;
3952 if (!element_array) {
3953 ERR("Calloc config element array");
3954 goto end;
3955 }
3956
3957 for (int i = 0; i < xpath_result_size; i++) {
3958 xmlNodePtr node_copy = NULL;
3959 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3960 if (!node_copy) {
3961 ERR("Xml copy of event node");
3962 goto error;
3963 }
3964 /* Hand off ownership */
3965 (*element_array)[i] = malloc(sizeof(struct config_element));
3966 if (!((*element_array)[i])){
3967 ERR("Malloc config_element array");
3968 xmlFreeNode(node_copy);
3969 goto error;
3970 };
3971 (*element_array)[i]->element = node_copy;
3972 }
3973 end:
3974 xmlXPathFreeContext(xpath_context);
3975 xmlXPathFreeObject(xpath_object);
3976 xmlFree(internal_xpath);
3977 xmlFree(internal_value);
3978 xmlFreeDoc(document);
3979 return;
3980 error:
3981
3982 if (element_array) {
3983 config_element_free_array(*element_array, *array_size);
3984 }
3985
3986 *array_size = 0;
3987
3988 xmlXPathFreeContext(xpath_context);
3989 xmlXPathFreeObject(xpath_object);
3990 xmlFree(internal_xpath);
3991 xmlFree(internal_value);
3992 return;
3993 }
3994 /* Create an internal copy of the config_element.
3995 * The users is responsible for the freeing of the passed child.
3996 */
3997 LTTNG_HIDDEN
3998 int config_element_add_or_replace_child(struct config_element *parent, struct config_element *child)
3999 {
4000 assert(parent);
4001 assert(child);
4002 assert(parent->element);
4003 assert(child->element);
4004
4005 int ret = 0;
4006 xmlNodePtr local_copy;
4007 xmlNodePtr x_ret = NULL;
4008 xmlNodePtr iterator = NULL;
4009
4010 xmlNodePtr x_parent = parent->element;
4011 xmlNodePtr x_child = child->element;
4012
4013 /* Find and delete the node with the same name if present */
4014 iterator = x_parent->children;
4015 while (iterator != NULL) {
4016 if ((!xmlStrcmp(iterator->name, x_child->name))){
4017 xmlNodePtr del = iterator;
4018 iterator = iterator->next;
4019 xmlUnlinkNode(del);
4020 xmlFreeNode(del);
4021 } else {
4022 iterator = iterator->next;
4023 }
4024 }
4025
4026 /* Add the child */
4027 local_copy = xmlCopyNode(x_child, 1);
4028 if (!local_copy) {
4029 ERR("Duplication of node to be insert failed");
4030 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
4031 goto end;
4032 }
4033 x_ret = xmlAddChild(x_parent, local_copy);
4034 if (!x_ret) {
4035 xmlFreeNode(local_copy);
4036 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
4037 }
4038 end:
4039 return ret;
4040 }
4041
4042 LTTNG_HIDDEN
4043 char *config_element_get_element_value(const struct config_element *element, const char *xpath)
4044 {
4045 char *value = NULL;
4046 int xpath_result_size;
4047 int value_result_size;
4048
4049 xmlXPathContextPtr xpath_context = NULL;
4050 xmlXPathObjectPtr xpath_object = NULL;
4051 xmlNodeSetPtr xpath_result_set = NULL;
4052 xmlChar *internal_xpath = NULL;
4053 xmlChar *internal_value = NULL;
4054 xmlDocPtr document = NULL;
4055 xmlNodePtr local_copy = NULL;
4056 xmlNodePtr place_holder = NULL;
4057
4058 assert(element);
4059 assert(element->element);
4060 assert(xpath);
4061
4062 /* Create a new doc and set root node as passed element */
4063 document = xmlNewDoc(BAD_CAST "1.0");
4064 if (!document) {
4065 value = NULL;
4066 ERR("Unable to create xml document");
4067 goto end;
4068 }
4069
4070 local_copy = xmlCopyNode(element->element, 1);
4071 if (!local_copy) {
4072 value = NULL;
4073 ERR("Unable to copy an xml node");
4074 goto end;
4075 }
4076
4077 /* Hand off the local_copy to the xmlDoc */
4078 place_holder = xmlDocSetRootElement(document, local_copy);
4079 if (place_holder) {
4080 value = NULL;
4081 ERR("The document root was already set");
4082 xmlFreeNode(local_copy);
4083 goto end;
4084 }
4085
4086 internal_xpath = encode_string(xpath);
4087 if (!internal_xpath) {
4088 /*TODO set valid error */
4089 value = NULL;
4090 ERR("Unable to encode xpath string");
4091 goto end;
4092 }
4093
4094 /* Initialize xpath context */
4095 xpath_context = xmlXPathNewContext(document);
4096 if (!xpath_context) {
4097 /*TODO set valid error */
4098 value = NULL;
4099 ERR("Unable to create xpath context");
4100 goto end;
4101 }
4102
4103 /* Evaluate the xpath expression */
4104 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
4105 if (!xpath_object) {
4106 /*TODO set valid error */
4107 value = NULL;
4108 ERR("Unable to evaluate xpath query (invalid query format)");
4109 goto end;
4110 }
4111
4112 xpath_result_set = xpath_object->nodesetval;
4113 if (!xpath_result_set) {
4114 /*TODO set valid error */
4115 value = NULL;
4116 goto end;
4117 }
4118
4119 xpath_result_size = xpath_result_set->nodeNr;
4120
4121 if (xpath_result_size > 1) {
4122 /*TODO set valid error */
4123 ERR("To many result while fetching config element value");
4124 value = NULL;
4125 goto end;
4126 }
4127
4128 if (xpath_result_size == 0) {
4129 value = NULL;
4130 goto end;
4131 }
4132
4133 assert(xpath_result_set->nodeTab[0]);
4134 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
4135 if (!internal_value) {
4136 value = NULL;
4137 goto end;
4138 }
4139
4140 value_result_size = xmlStrlen(internal_value);
4141 value = calloc(value_result_size + 1, sizeof(char));
4142 strncpy(value, (char *) internal_value, value_result_size);
4143
4144 end:
4145 xmlXPathFreeContext(xpath_context);
4146 xmlXPathFreeObject(xpath_object);
4147 xmlFree(internal_xpath);
4148 xmlFree(internal_value);
4149 xmlFreeDoc(document);
4150 return value;
4151
4152 }
4153
4154 LTTNG_HIDDEN
4155 int config_process_event_element(const struct config_element *element, const char *session_name, int domain_type, const char *channel_name) {
4156
4157 int ret = 0;
4158 xmlNodePtr node;
4159 struct lttng_domain domain;
4160 struct lttng_handle *handle;
4161
4162 assert(element);
4163 assert(element->element);
4164
4165 memset(&domain, 0, sizeof(struct lttng_domain));
4166
4167 /* Create handle */
4168 domain.type = domain_type;
4169 switch (domain.type) {
4170 case LTTNG_DOMAIN_KERNEL:
4171 domain.buf_type = LTTNG_BUFFER_GLOBAL;
4172 break;
4173 case LTTNG_DOMAIN_UST:
4174 case LTTNG_DOMAIN_JUL:
4175 case LTTNG_DOMAIN_LOG4J:
4176 case LTTNG_DOMAIN_PYTHON:
4177 domain.buf_type = LTTNG_BUFFER_PER_UID;
4178 break;
4179 case LTTNG_DOMAIN_NONE:
4180 default:
4181 assert(0);
4182 }
4183
4184 handle = lttng_create_handle(session_name, &domain);
4185 if (!handle) {
4186 ret = -LTTNG_ERR_HANDLE_CREATION;
4187 goto error;
4188 }
4189
4190
4191 node = element->element;
4192 /* Check if element is really and event */
4193 if (xmlStrcmp(BAD_CAST config_element_event, node->name)) {
4194 ret = -LTTNG_ERR_CONFIG_INVALID_ELEMENT;
4195 goto error;
4196 }
4197
4198 ret = process_event_node(node, handle, channel_name, ENABLE);
4199 if (ret) {
4200 goto error;
4201 }
4202 error:
4203 free(handle);
4204 return ret;
4205 }
This page took 0.138545 seconds and 5 git commands to generate.