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