Fix: move JUL filter creation to a function
[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 char *get_session_config_xsd_path()
573 {
574 char *xsd_path;
575 const char *base_path = getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
576 size_t base_path_len;
577 size_t max_path_len;
578
579 if (!base_path) {
580 base_path = DEFAULT_SESSION_CONFIG_XSD_PATH;
581 }
582
583 base_path_len = strlen(base_path);
584 max_path_len = base_path_len +
585 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1;
586 xsd_path = zmalloc(max_path_len);
587 if (!xsd_path) {
588 goto end;
589 }
590
591 strncpy(xsd_path, base_path, max_path_len);
592 if (xsd_path[base_path_len - 1] != '/') {
593 xsd_path[base_path_len++] = '/';
594 }
595
596 strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME,
597 max_path_len - base_path_len);
598 end:
599 return xsd_path;
600 }
601
602 static
603 int init_session_config_validation_ctx(
604 struct session_config_validation_ctx *ctx)
605 {
606 int ret;
607 char *xsd_path = get_session_config_xsd_path();
608
609 if (!xsd_path) {
610 ret = -LTTNG_ERR_NOMEM;
611 goto end;
612 }
613
614 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
615 if (!ctx->parser_ctx) {
616 ERR("XSD parser context creation failed");
617 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
618 goto end;
619 }
620 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
621 xml_error_handler, NULL);
622
623 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
624 if (!ctx->schema) {
625 ERR("XSD parsing failed");
626 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
627 goto end;
628 }
629
630 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
631 if (!ctx->schema_validation_ctx) {
632 ERR("XSD validation context creation failed");
633 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
634 goto end;
635 }
636
637 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
638 xml_error_handler, NULL);
639 ret = 0;
640
641 end:
642 if (ret) {
643 fini_session_config_validation_ctx(ctx);
644 }
645
646 free(xsd_path);
647 return ret;
648 }
649
650 static
651 int parse_uint(xmlChar *str, uint64_t *val)
652 {
653 int ret;
654 char *endptr;
655
656 if (!str || !val) {
657 ret = -1;
658 goto end;
659 }
660
661 *val = strtoull((const char *) str, &endptr, 10);
662 if (!endptr || *endptr) {
663 ret = -1;
664 } else {
665 ret = 0;
666 }
667
668 end:
669 return ret;
670 }
671
672 static
673 int parse_int(xmlChar *str, int64_t *val)
674 {
675 int ret;
676 char *endptr;
677
678 if (!str || !val) {
679 ret = -1;
680 goto end;
681 }
682
683 *val = strtoll((const char *) str, &endptr, 10);
684 if (!endptr || *endptr) {
685 ret = -1;
686 } else {
687 ret = 0;
688 }
689
690 end:
691 return ret;
692 }
693
694 static
695 int parse_bool(xmlChar *str, int *val)
696 {
697 int ret = 0;
698
699 if (!str || !val) {
700 ret = -1;
701 goto end;
702 }
703
704 if (!strcmp((const char *) str, config_xml_true)) {
705 *val = 1;
706 } else if (!strcmp((const char *) str, config_xml_false)) {
707 *val = 0;
708 } else {
709 WARN("Invalid boolean value encoutered (%s).",
710 (const char *) str);
711 ret = -1;
712 }
713 end:
714 return ret;
715 }
716
717 static
718 int get_domain_type(xmlChar *domain)
719 {
720 int ret;
721
722 if (!domain) {
723 goto error;
724 }
725
726 if (!strcmp((char *) domain, config_domain_type_kernel)) {
727 ret = LTTNG_DOMAIN_KERNEL;
728 } else if (!strcmp((char *) domain, config_domain_type_ust)) {
729 ret = LTTNG_DOMAIN_UST;
730 } else if (!strcmp((char *) domain, config_domain_type_jul)) {
731 ret = LTTNG_DOMAIN_JUL;
732 } else {
733 goto error;
734 }
735
736 return ret;
737 error:
738 return -1;
739 }
740
741 static
742 int get_buffer_type(xmlChar *buffer_type)
743 {
744 int ret;
745
746 if (!buffer_type) {
747 goto error;
748 }
749
750 if (!strcmp((char *) buffer_type, config_buffer_type_global)) {
751 ret = LTTNG_BUFFER_GLOBAL;
752 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) {
753 ret = LTTNG_BUFFER_PER_UID;
754 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) {
755 ret = LTTNG_BUFFER_PER_PID;
756 } else {
757 goto error;
758 }
759
760 return ret;
761 error:
762 return -1;
763 }
764
765 static
766 int get_overwrite_mode(xmlChar *overwrite_mode)
767 {
768 int ret;
769
770 if (!overwrite_mode) {
771 goto error;
772 }
773
774 if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) {
775 ret = 1;
776 } else if (!strcmp((char *) overwrite_mode,
777 config_overwrite_mode_discard)) {
778 ret = 0;
779 } else {
780 goto error;
781 }
782
783 return ret;
784 error:
785 return -1;
786 }
787
788 static
789 int get_output_type(xmlChar *output_type)
790 {
791 int ret;
792
793 if (!output_type) {
794 goto error;
795 }
796
797 if (!strcmp((char *) output_type, config_output_type_mmap)) {
798 ret = LTTNG_EVENT_MMAP;
799 } else if (!strcmp((char *) output_type, config_output_type_splice)) {
800 ret = LTTNG_EVENT_SPLICE;
801 } else {
802 goto error;
803 }
804
805 return ret;
806 error:
807 return -1;
808 }
809
810 static
811 int get_event_type(xmlChar *event_type)
812 {
813 int ret;
814
815 if (!event_type) {
816 goto error;
817 }
818
819 if (!strcmp((char *) event_type, config_event_type_all)) {
820 ret = LTTNG_EVENT_ALL;
821 } else if (!strcmp((char *) event_type, config_event_type_tracepoint)) {
822 ret = LTTNG_EVENT_TRACEPOINT;
823 } else if (!strcmp((char *) event_type, config_event_type_probe)) {
824 ret = LTTNG_EVENT_PROBE;
825 } else if (!strcmp((char *) event_type, config_event_type_function)) {
826 ret = LTTNG_EVENT_FUNCTION;
827 } else if (!strcmp((char *) event_type,
828 config_event_type_function_entry)) {
829 ret = LTTNG_EVENT_FUNCTION_ENTRY;
830 } else if (!strcmp((char *) event_type, config_event_type_noop)) {
831 ret = LTTNG_EVENT_NOOP;
832 } else if (!strcmp((char *) event_type, config_event_type_syscall)) {
833 ret = LTTNG_EVENT_SYSCALL;
834 } else {
835 goto error;
836 }
837
838 return ret;
839 error:
840 return -1;
841 }
842
843 static
844 int get_loglevel_type(xmlChar *loglevel_type)
845 {
846 int ret;
847
848 if (!loglevel_type) {
849 goto error;
850 }
851
852 if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) {
853 ret = LTTNG_EVENT_LOGLEVEL_ALL;
854 } else if (!strcmp((char *) loglevel_type,
855 config_loglevel_type_range)) {
856 ret = LTTNG_EVENT_LOGLEVEL_RANGE;
857 } else if (!strcmp((char *) loglevel_type,
858 config_loglevel_type_single)) {
859 ret = LTTNG_EVENT_LOGLEVEL_SINGLE;
860 } else {
861 goto error;
862 }
863
864 return ret;
865 error:
866 return -1;
867 }
868
869 /*
870 * Return the context type or -1 on error.
871 */
872 static
873 int get_context_type(xmlChar *context_type)
874 {
875 int ret;
876
877 if (!context_type) {
878 goto error;
879 }
880
881 if (!strcmp((char *) context_type, config_event_context_pid)) {
882 ret = LTTNG_EVENT_CONTEXT_PID;
883 } else if (!strcmp((char *) context_type,
884 config_event_context_procname)) {
885 ret = LTTNG_EVENT_CONTEXT_PROCNAME;
886 } else if (!strcmp((char *) context_type,
887 config_event_context_prio)) {
888 ret = LTTNG_EVENT_CONTEXT_PRIO;
889 } else if (!strcmp((char *) context_type,
890 config_event_context_nice)) {
891 ret = LTTNG_EVENT_CONTEXT_NICE;
892 } else if (!strcmp((char *) context_type,
893 config_event_context_vpid)) {
894 ret = LTTNG_EVENT_CONTEXT_VPID;
895 } else if (!strcmp((char *) context_type,
896 config_event_context_tid)) {
897 ret = LTTNG_EVENT_CONTEXT_TID;
898 } else if (!strcmp((char *) context_type,
899 config_event_context_vtid)) {
900 ret = LTTNG_EVENT_CONTEXT_VTID;
901 } else if (!strcmp((char *) context_type,
902 config_event_context_ppid)) {
903 ret = LTTNG_EVENT_CONTEXT_PPID;
904 } else if (!strcmp((char *) context_type,
905 config_event_context_vppid)) {
906 ret = LTTNG_EVENT_CONTEXT_VPPID;
907 } else if (!strcmp((char *) context_type,
908 config_event_context_pthread_id)) {
909 ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID;
910 } else if (!strcmp((char *) context_type,
911 config_event_context_hostname)) {
912 ret = LTTNG_EVENT_CONTEXT_HOSTNAME;
913 } else if (!strcmp((char *) context_type,
914 config_event_context_ip)) {
915 ret = LTTNG_EVENT_CONTEXT_IP;
916 } else {
917 goto error;
918 }
919
920 return ret;
921 error:
922 return -1;
923 }
924
925 static
926 int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
927 {
928 int ret;
929 xmlNodePtr node;
930
931 for (node = xmlFirstElementChild(domain_node); node;
932 node = xmlNextElementSibling(node)) {
933 if (!strcmp((const char *) node->name, config_element_type)) {
934 /* domain type */
935 xmlChar *node_content = xmlNodeGetContent(node);
936 if (!node_content) {
937 ret = -LTTNG_ERR_NOMEM;
938 goto end;
939 }
940
941 ret = get_domain_type(node_content);
942 free(node_content);
943 if (ret < 0) {
944 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
945 goto end;
946 }
947
948 domain->type = ret;
949 } else if (!strcmp((const char *) node->name,
950 config_element_buffer_type)) {
951 /* buffer type */
952 xmlChar *node_content = xmlNodeGetContent(node);
953 if (!node_content) {
954 ret = -LTTNG_ERR_NOMEM;
955 goto end;
956 }
957
958 ret = get_buffer_type(node_content);
959 free(node_content);
960 if (ret < 0) {
961 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
962 goto end;
963 }
964
965 domain->buf_type = ret;
966 }
967 }
968 ret = 0;
969 end:
970 return ret;
971 }
972
973 static
974 int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
975 char **data_uri)
976 {
977 xmlNodePtr node;
978
979 for (node = xmlFirstElementChild(net_output_node); node;
980 node = xmlNextElementSibling(node)) {
981 if (!strcmp((const char *) node->name, config_element_control_uri)) {
982 /* control_uri */
983 *control_uri = (char *) xmlNodeGetContent(node);
984 if (!*control_uri) {
985 break;
986 }
987 } else {
988 /* data_uri */
989 *data_uri = (char *) xmlNodeGetContent(node);
990 if (!*data_uri) {
991 break;
992 }
993 }
994 }
995
996 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
997 }
998
999 static
1000 int process_consumer_output(xmlNodePtr consumer_output_node,
1001 struct consumer_output *output)
1002 {
1003 int ret;
1004 xmlNodePtr node;
1005
1006 assert(output);
1007
1008 for (node = xmlFirstElementChild(consumer_output_node); node;
1009 node = xmlNextElementSibling(node)) {
1010 if (!strcmp((const char *) node->name, config_element_enabled)) {
1011 xmlChar *enabled_str = xmlNodeGetContent(node);
1012
1013 /* enabled */
1014 if (!enabled_str) {
1015 ret = -LTTNG_ERR_NOMEM;
1016 goto end;
1017 }
1018
1019 ret = parse_bool(enabled_str, &output->enabled);
1020 free(enabled_str);
1021 if (ret) {
1022 goto end;
1023 }
1024 } else {
1025 xmlNodePtr output_type_node;
1026
1027 /* destination */
1028 output_type_node = xmlFirstElementChild(node);
1029 if (!output_type_node) {
1030 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1031 goto end;
1032 }
1033
1034 if (!strcmp((const char *) output_type_node->name,
1035 config_element_path)) {
1036 /* path */
1037 output->path = (char *) xmlNodeGetContent(output_type_node);
1038 if (!output->path) {
1039 ret = -LTTNG_ERR_NOMEM;
1040 goto end;
1041 }
1042 } else {
1043 /* net_output */
1044 ret = get_net_output_uris(output_type_node,
1045 &output->control_uri, &output->data_uri);
1046 if (ret) {
1047 goto end;
1048 }
1049 }
1050 }
1051 }
1052 ret = 0;
1053
1054 end:
1055 if (ret) {
1056 free(output->path);
1057 free(output->control_uri);
1058 free(output->data_uri);
1059 memset(output, 0, sizeof(struct consumer_output));
1060 }
1061 return ret;
1062 }
1063
1064 static
1065 int create_session_net_output(const char *name, struct lttng_domain *domain,
1066 const char *control_uri, const char *data_uri)
1067 {
1068 int ret;
1069 struct lttng_handle *handle;
1070 const char *uri = NULL;
1071
1072 assert(name);
1073 assert(domain);
1074
1075 handle = lttng_create_handle(name, domain);
1076 if (!handle) {
1077 ret = -LTTNG_ERR_NOMEM;
1078 goto end;
1079 }
1080
1081 if (!control_uri || !data_uri) {
1082 uri = control_uri ? control_uri : data_uri;
1083 control_uri = uri;
1084 data_uri = uri;
1085 }
1086
1087 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1088 lttng_destroy_handle(handle);
1089 end:
1090 return ret;
1091 }
1092
1093 static
1094 int create_snapshot_session(const char *session_name, xmlNodePtr output_node)
1095 {
1096 int ret;
1097 xmlNodePtr node = NULL;
1098 xmlNodePtr snapshot_output_list_node;
1099 xmlNodePtr snapshot_output_node;
1100
1101 assert(session_name);
1102
1103 ret = lttng_create_session_snapshot(session_name, NULL);
1104 if (ret) {
1105 goto end;
1106 }
1107
1108 if (!output_node) {
1109 goto end;
1110 }
1111
1112 snapshot_output_list_node = xmlFirstElementChild(output_node);
1113
1114 /* Parse and create snapshot outputs */
1115
1116 for (snapshot_output_node =
1117 xmlFirstElementChild(snapshot_output_list_node);
1118 snapshot_output_node; snapshot_output_node =
1119 xmlNextElementSibling(snapshot_output_node)) {
1120 char *name = NULL;
1121 uint64_t max_size = UINT64_MAX;
1122 struct consumer_output output = { 0 };
1123 struct lttng_snapshot_output *snapshot_output = NULL;
1124
1125 for (node = xmlFirstElementChild(snapshot_output_node); node;
1126 node = xmlNextElementSibling(node)) {
1127 if (!strcmp((const char *) node->name,
1128 config_element_name)) {
1129 /* name */
1130 name = (char *) xmlNodeGetContent(node);
1131 if (!name) {
1132 ret = -LTTNG_ERR_NOMEM;
1133 goto error_snapshot_output;
1134 }
1135 } else if (!strcmp((const char *) node->name,
1136 config_element_max_size)) {
1137 xmlChar *content = xmlNodeGetContent(node);
1138
1139 /* max_size */
1140 if (!content) {
1141 ret = -LTTNG_ERR_NOMEM;
1142 goto error_snapshot_output;
1143 }
1144 ret = parse_uint(content, &max_size);
1145 free(content);
1146 if (ret) {
1147 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1148 goto error_snapshot_output;
1149 }
1150 } else {
1151 /* consumer_output */
1152 ret = process_consumer_output(node, &output);
1153 if (ret) {
1154 goto error_snapshot_output;
1155 }
1156 }
1157 }
1158
1159 snapshot_output = lttng_snapshot_output_create();
1160 if (!snapshot_output) {
1161 ret = -LTTNG_ERR_NOMEM;
1162 goto error_snapshot_output;
1163 }
1164
1165 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1166 if (ret) {
1167 goto error_snapshot_output;
1168 }
1169
1170 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1171 if (ret) {
1172 goto error_snapshot_output;
1173 }
1174
1175 if (output.path) {
1176 ret = lttng_snapshot_output_set_ctrl_url(output.path,
1177 snapshot_output);
1178 if (ret) {
1179 goto error_snapshot_output;
1180 }
1181 } else {
1182 if (output.control_uri) {
1183 ret = lttng_snapshot_output_set_ctrl_url(output.control_uri,
1184 snapshot_output);
1185 if (ret) {
1186 goto error_snapshot_output;
1187 }
1188 }
1189
1190 if (output.data_uri) {
1191 ret = lttng_snapshot_output_set_data_url(output.data_uri,
1192 snapshot_output);
1193 if (ret) {
1194 goto error_snapshot_output;
1195 }
1196 }
1197 }
1198
1199 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1200 error_snapshot_output:
1201 free(name);
1202 free(output.path);
1203 free(output.control_uri);
1204 free(output.data_uri);
1205 lttng_snapshot_output_destroy(snapshot_output);
1206 if (ret) {
1207 goto end;
1208 }
1209 }
1210 end:
1211 return ret;
1212 }
1213
1214 static
1215 int create_session(const char *name,
1216 struct lttng_domain *kernel_domain,
1217 struct lttng_domain *ust_domain,
1218 struct lttng_domain *jul_domain,
1219 xmlNodePtr output_node,
1220 uint64_t live_timer_interval)
1221 {
1222 int ret;
1223 struct consumer_output output = { 0 };
1224 xmlNodePtr consumer_output_node;
1225
1226 assert(name);
1227
1228 if (output_node) {
1229 consumer_output_node = xmlFirstElementChild(output_node);
1230 if (!consumer_output_node) {
1231 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1232 goto end;
1233 }
1234
1235 if (strcmp((const char *) consumer_output_node->name,
1236 config_element_consumer_output)) {
1237 WARN("Invalid output type, expected %s node",
1238 config_element_consumer_output);
1239 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1240 goto end;
1241 }
1242
1243 ret = process_consumer_output(consumer_output_node, &output);
1244 if (ret) {
1245 goto end;
1246 }
1247 }
1248
1249 if (live_timer_interval != UINT64_MAX &&
1250 !output.control_uri && !output.data_uri) {
1251 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1252 goto end;
1253 }
1254
1255 if (output.control_uri || output.data_uri) {
1256 int i;
1257 struct lttng_domain *domain;
1258 struct lttng_domain *domains[] =
1259 { kernel_domain, ust_domain, jul_domain };
1260
1261 /* network destination */
1262 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
1263 /*
1264 * URLs are provided for sure since the test above make sure that
1265 * with a live timer the data and control URIs are provided. So,
1266 * NULL is passed here and will be set right after.
1267 */
1268 ret = lttng_create_session_live(name, NULL, live_timer_interval);
1269 } else {
1270 ret = lttng_create_session(name, NULL);
1271 }
1272 if (ret) {
1273 goto end;
1274 }
1275
1276 for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) {
1277 domain = domains[i];
1278 if (!domain) {
1279 continue;
1280 }
1281
1282 ret = create_session_net_output(name, domain, output.control_uri,
1283 output.data_uri);
1284 if (ret) {
1285 goto end;
1286 }
1287 }
1288 } else {
1289 /* either local output or no output */
1290 ret = lttng_create_session(name, output.path);
1291 if (ret) {
1292 goto end;
1293 }
1294 }
1295 end:
1296 free(output.path);
1297 free(output.control_uri);
1298 free(output.data_uri);
1299 return ret;
1300 }
1301 static
1302 int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1303 struct lttng_event_probe_attr *attr)
1304 {
1305 int ret;
1306
1307 assert(probe_attribute_node);
1308 assert(attr);
1309
1310 if (!strcmp((const char *) probe_attribute_node->name,
1311 config_element_address)) {
1312 xmlChar *content;
1313 uint64_t addr = 0;
1314
1315 /* addr */
1316 content = xmlNodeGetContent(probe_attribute_node);
1317 if (!content) {
1318 ret = -LTTNG_ERR_NOMEM;
1319 goto end;
1320 }
1321
1322 ret = parse_uint(content, &addr);
1323 free(content);
1324 if (ret) {
1325 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1326 goto end;
1327 }
1328
1329 attr->addr = addr;
1330 } else if (!strcmp((const char *) probe_attribute_node->name,
1331 config_element_offset)) {
1332 xmlChar *content;
1333 uint64_t offset = 0;
1334
1335 /* offset */
1336 content = xmlNodeGetContent(probe_attribute_node);
1337 if (!content) {
1338 ret = -LTTNG_ERR_NOMEM;
1339 goto end;
1340 }
1341
1342 ret = parse_uint(content, &offset);
1343 free(content);
1344 if (ret) {
1345 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1346 goto end;
1347 }
1348
1349 attr->offset = offset;
1350 } else if (!strcmp((const char *) probe_attribute_node->name,
1351 config_element_symbol_name)) {
1352 xmlChar *content;
1353 size_t name_len;
1354
1355 /* symbol_name */
1356 content = xmlNodeGetContent(probe_attribute_node);
1357 if (!content) {
1358 ret = -LTTNG_ERR_NOMEM;
1359 goto end;
1360 }
1361
1362 name_len = strlen((char *) content);
1363 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1364 WARN("symbol_name too long.");
1365 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1366 free(content);
1367 goto end;
1368 }
1369
1370 strncpy(attr->symbol_name, (const char *) content, name_len);
1371 free(content);
1372 }
1373 ret = 0;
1374 end:
1375 return ret;
1376 }
1377
1378 static
1379 int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
1380 const char *channel_name)
1381 {
1382 int ret, i;
1383 xmlNodePtr node;
1384 struct lttng_event event;
1385 char **exclusions = NULL;
1386 unsigned long exclusion_count = 0;
1387 char *filter_expression = NULL;
1388
1389 assert(event_node);
1390 assert(handle);
1391 assert(channel_name);
1392
1393 memset(&event, 0, sizeof(event));
1394
1395 for (node = xmlFirstElementChild(event_node); node;
1396 node = xmlNextElementSibling(node)) {
1397 if (!strcmp((const char *) node->name, config_element_name)) {
1398 xmlChar *content;
1399 size_t name_len;
1400
1401 /* name */
1402 content = xmlNodeGetContent(node);
1403 if (!content) {
1404 ret = -LTTNG_ERR_NOMEM;
1405 goto end;
1406 }
1407
1408 name_len = strlen((char *) content);
1409 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1410 WARN("Channel name too long.");
1411 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1412 free(content);
1413 goto end;
1414 }
1415
1416 strncpy(event.name, (const char *) content, name_len);
1417 free(content);
1418 } else if (!strcmp((const char *) node->name,
1419 config_element_enabled)) {
1420 xmlChar *content = xmlNodeGetContent(node);
1421
1422 /* enabled */
1423 if (!content) {
1424 ret = -LTTNG_ERR_NOMEM;
1425 goto end;
1426 }
1427
1428 ret = parse_bool(content, &event.enabled);
1429 free(content);
1430 if (ret) {
1431 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1432 goto end;
1433 }
1434 } else if (!strcmp((const char *) node->name,
1435 config_element_type)) {
1436 xmlChar *content = xmlNodeGetContent(node);
1437
1438 /* type */
1439 if (!content) {
1440 ret = -LTTNG_ERR_NOMEM;
1441 goto end;
1442 }
1443
1444 ret = get_event_type(content);
1445 free(content);
1446 if (ret < 0) {
1447 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1448 goto end;
1449 }
1450
1451 event.type = ret;
1452 } else if (!strcmp((const char *) node->name,
1453 config_element_loglevel_type)) {
1454 xmlChar *content = xmlNodeGetContent(node);
1455
1456 /* loglevel_type */
1457 if (!content) {
1458 ret = -LTTNG_ERR_NOMEM;
1459 goto end;
1460 }
1461
1462 ret = get_loglevel_type(content);
1463 free(content);
1464 if (ret < 0) {
1465 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1466 goto end;
1467 }
1468
1469 event.loglevel_type = ret;
1470 } else if (!strcmp((const char *) node->name,
1471 config_element_loglevel)) {
1472 xmlChar *content;
1473 int64_t loglevel = 0;
1474
1475 /* loglevel */
1476 content = xmlNodeGetContent(node);
1477 if (!content) {
1478 ret = -LTTNG_ERR_NOMEM;
1479 goto end;
1480 }
1481
1482 ret = parse_int(content, &loglevel);
1483 free(content);
1484 if (ret) {
1485 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1486 goto end;
1487 }
1488
1489 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1490 WARN("loglevel out of range.");
1491 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1492 goto end;
1493 }
1494
1495 event.loglevel = loglevel;
1496 } else if (!strcmp((const char *) node->name,
1497 config_element_filter)) {
1498 xmlChar *content =
1499 xmlNodeGetContent(node);
1500
1501 /* filter */
1502 if (!content) {
1503 ret = -LTTNG_ERR_NOMEM;
1504 goto end;
1505 }
1506
1507 filter_expression = strdup((char *) content);
1508 free(content);
1509 if (!filter_expression) {
1510 ret = -LTTNG_ERR_NOMEM;
1511 goto end;
1512 }
1513 } else if (!strcmp((const char *) node->name,
1514 config_element_exclusions)) {
1515 xmlNodePtr exclusion_node;
1516 int exclusion_index = 0;
1517
1518 /* exclusions */
1519 if (exclusions) {
1520 /*
1521 * Exclusions has already been initialized,
1522 * invalid file.
1523 */
1524 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1525 goto end;
1526 }
1527
1528 exclusion_count = xmlChildElementCount(node);
1529 if (!exclusion_count) {
1530 continue;
1531 }
1532
1533 exclusions = zmalloc(exclusion_count * sizeof(char *));
1534 if (!exclusions) {
1535 exclusion_count = 0;
1536 ret = -LTTNG_ERR_NOMEM;
1537 goto end;
1538 }
1539
1540 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1541 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1542 xmlChar *content =
1543 xmlNodeGetContent(exclusion_node);
1544
1545 if (!content) {
1546 ret = -LTTNG_ERR_NOMEM;
1547 goto end;
1548 }
1549
1550 exclusions[exclusion_index] = strdup((const char *) content);
1551 free(content);
1552 if (!exclusions[exclusion_index]) {
1553 ret = -LTTNG_ERR_NOMEM;
1554 goto end;
1555 }
1556 exclusion_index++;
1557 }
1558
1559 event.exclusion = 1;
1560 } else if (!strcmp((const char *) node->name,
1561 config_element_attributes)) {
1562 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1563
1564 /* attributes */
1565 if (!attribute_node) {
1566 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1567 goto end;
1568 }
1569
1570 if (!strcmp((const char *) node->name,
1571 config_element_probe_attributes)) {
1572 xmlNodePtr probe_attribute_node;
1573
1574 /* probe_attributes */
1575 for (probe_attribute_node =
1576 xmlFirstElementChild(attribute_node); probe_attribute_node;
1577 probe_attribute_node = xmlNextElementSibling(
1578 probe_attribute_node)) {
1579
1580 ret = process_probe_attribute_node(probe_attribute_node,
1581 &event.attr.probe);
1582 if (ret) {
1583 goto end;
1584 }
1585 }
1586 } else {
1587 size_t sym_len;
1588 xmlChar *content;
1589 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1590
1591 /* function_attributes */
1592 content = xmlNodeGetContent(symbol_node);
1593 if (!content) {
1594 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1595 goto end;
1596 }
1597
1598 sym_len = strlen((char *) content);
1599 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1600 WARN("Function name too long.");
1601 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1602 free(content);
1603 goto end;
1604 }
1605
1606 strncpy(event.attr.ftrace.symbol_name, (char *) content,
1607 sym_len);
1608 free(content);
1609 }
1610 }
1611 }
1612
1613 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1614 filter_expression, exclusion_count, exclusions);
1615 end:
1616 for (i = 0; i < exclusion_count; i++) {
1617 free(exclusions[i]);
1618 }
1619
1620 free(exclusions);
1621 free(filter_expression);
1622 return ret;
1623 }
1624
1625 static
1626 int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1627 const char *channel_name)
1628 {
1629 int ret = 0;
1630 xmlNodePtr node;
1631
1632 assert(events_node);
1633 assert(handle);
1634 assert(channel_name);
1635
1636 for (node = xmlFirstElementChild(events_node); node;
1637 node = xmlNextElementSibling(node)) {
1638 ret = process_event_node(node, handle, channel_name);
1639 if (ret) {
1640 goto end;
1641 }
1642 }
1643 end:
1644 return ret;
1645 }
1646
1647 static
1648 int process_channel_attr_node(xmlNodePtr attr_node,
1649 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1650 xmlNodePtr *events_node)
1651 {
1652 int ret;
1653
1654 assert(attr_node);
1655 assert(channel);
1656 assert(contexts_node);
1657 assert(events_node);
1658
1659 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1660 xmlChar *content;
1661 size_t name_len;
1662
1663 /* name */
1664 content = xmlNodeGetContent(attr_node);
1665 if (!content) {
1666 ret = -LTTNG_ERR_NOMEM;
1667 goto end;
1668 }
1669
1670 name_len = strlen((char *) content);
1671 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1672 WARN("Channel name too long.");
1673 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1674 free(content);
1675 goto end;
1676 }
1677
1678 strncpy(channel->name, (const char *) content, name_len);
1679 free(content);
1680 } else if (!strcmp((const char *) attr_node->name,
1681 config_element_enabled)) {
1682 xmlChar *content;
1683 int enabled;
1684
1685 /* enabled */
1686 content = xmlNodeGetContent(attr_node);
1687 if (!content) {
1688 ret = -LTTNG_ERR_NOMEM;
1689 goto end;
1690 }
1691
1692 ret = parse_bool(content, &enabled);
1693 free(content);
1694 if (ret) {
1695 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1696 goto end;
1697 }
1698
1699 channel->enabled = enabled;
1700 } else if (!strcmp((const char *) attr_node->name,
1701 config_element_overwrite_mode)) {
1702 xmlChar *content;
1703
1704 /* overwrite_mode */
1705 content = xmlNodeGetContent(attr_node);
1706 if (!content) {
1707 ret = -LTTNG_ERR_NOMEM;
1708 goto end;
1709 }
1710
1711 ret = get_overwrite_mode(content);
1712 free(content);
1713 if (ret < 0) {
1714 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1715 goto end;
1716 }
1717
1718 channel->attr.overwrite = ret;
1719 } else if (!strcmp((const char *) attr_node->name,
1720 config_element_subbuf_size)) {
1721 xmlChar *content;
1722
1723 /* subbuffer_size */
1724 content = xmlNodeGetContent(attr_node);
1725 if (!content) {
1726 ret = -LTTNG_ERR_NOMEM;
1727 goto end;
1728 }
1729
1730 ret = parse_uint(content, &channel->attr.subbuf_size);
1731 free(content);
1732 if (ret) {
1733 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1734 goto end;
1735 }
1736 } else if (!strcmp((const char *) attr_node->name,
1737 config_element_num_subbuf)) {
1738 xmlChar *content;
1739
1740 /* subbuffer_count */
1741 content = xmlNodeGetContent(attr_node);
1742 if (!content) {
1743 ret = -LTTNG_ERR_NOMEM;
1744 goto end;
1745 }
1746
1747 ret = parse_uint(content, &channel->attr.num_subbuf);
1748 free(content);
1749 if (ret) {
1750 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1751 goto end;
1752 }
1753 } else if (!strcmp((const char *) attr_node->name,
1754 config_element_switch_timer_interval)) {
1755 xmlChar *content;
1756 uint64_t switch_timer_interval = 0;
1757
1758 /* switch_timer_interval */
1759 content = xmlNodeGetContent(attr_node);
1760 if (!content) {
1761 ret = -LTTNG_ERR_NOMEM;
1762 goto end;
1763 }
1764
1765 ret = parse_uint(content, &switch_timer_interval);
1766 free(content);
1767 if (ret) {
1768 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1769 goto end;
1770 }
1771
1772 if (switch_timer_interval > UINT_MAX) {
1773 WARN("switch_timer_interval out of range.");
1774 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1775 goto end;
1776 }
1777
1778 channel->attr.switch_timer_interval =
1779 switch_timer_interval;
1780 } else if (!strcmp((const char *) attr_node->name,
1781 config_element_read_timer_interval)) {
1782 xmlChar *content;
1783 uint64_t read_timer_interval = 0;
1784
1785 /* read_timer_interval */
1786 content = xmlNodeGetContent(attr_node);
1787 if (!content) {
1788 ret = -LTTNG_ERR_NOMEM;
1789 goto end;
1790 }
1791
1792 ret = parse_uint(content, &read_timer_interval);
1793 free(content);
1794 if (ret) {
1795 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1796 goto end;
1797 }
1798
1799 if (read_timer_interval > UINT_MAX) {
1800 WARN("read_timer_interval out of range.");
1801 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1802 goto end;
1803 }
1804
1805 channel->attr.read_timer_interval =
1806 read_timer_interval;
1807 } else if (!strcmp((const char *) attr_node->name,
1808 config_element_output_type)) {
1809 xmlChar *content;
1810
1811 /* output_type */
1812 content = xmlNodeGetContent(attr_node);
1813 if (!content) {
1814 ret = -LTTNG_ERR_NOMEM;
1815 goto end;
1816 }
1817
1818 ret = get_output_type(content);
1819 free(content);
1820 if (ret < 0) {
1821 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1822 goto end;
1823 }
1824
1825 channel->attr.output = ret;
1826 } else if (!strcmp((const char *) attr_node->name,
1827 config_element_tracefile_size)) {
1828 xmlChar *content;
1829
1830 /* tracefile_size */
1831 content = xmlNodeGetContent(attr_node);
1832 if (!content) {
1833 ret = -LTTNG_ERR_NOMEM;
1834 goto end;
1835 }
1836
1837 ret = parse_uint(content, &channel->attr.tracefile_size);
1838 free(content);
1839 if (ret) {
1840 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1841 goto end;
1842 }
1843 } else if (!strcmp((const char *) attr_node->name,
1844 config_element_tracefile_count)) {
1845 xmlChar *content;
1846
1847 /* tracefile_count */
1848 content = xmlNodeGetContent(attr_node);
1849 if (!content) {
1850 ret = -LTTNG_ERR_NOMEM;
1851 goto end;
1852 }
1853
1854 ret = parse_uint(content, &channel->attr.tracefile_count);
1855 free(content);
1856 if (ret) {
1857 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1858 goto end;
1859 }
1860 } else if (!strcmp((const char *) attr_node->name,
1861 config_element_live_timer_interval)) {
1862 xmlChar *content;
1863 uint64_t live_timer_interval = 0;
1864
1865 /* live_timer_interval */
1866 content = xmlNodeGetContent(attr_node);
1867 if (!content) {
1868 ret = -LTTNG_ERR_NOMEM;
1869 goto end;
1870 }
1871
1872 ret = parse_uint(content, &live_timer_interval);
1873 free(content);
1874 if (ret) {
1875 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1876 goto end;
1877 }
1878
1879 if (live_timer_interval > UINT_MAX) {
1880 WARN("live_timer_interval out of range.");
1881 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1882 goto end;
1883 }
1884
1885 channel->attr.live_timer_interval =
1886 live_timer_interval;
1887 } else if (!strcmp((const char *) attr_node->name,
1888 config_element_events)) {
1889 /* events */
1890 *events_node = attr_node;
1891 } else {
1892 /* contexts */
1893 *contexts_node = attr_node;
1894 }
1895 ret = 0;
1896 end:
1897 return ret;
1898 }
1899
1900 static
1901 int process_context_node(xmlNodePtr context_node,
1902 struct lttng_handle *handle, const char *channel_name)
1903 {
1904 int ret;
1905 struct lttng_event_context context;
1906 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
1907
1908 assert(handle);
1909 assert(channel_name);
1910
1911 if (!context_child_node) {
1912 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1913 goto end;
1914 }
1915
1916 memset(&context, 0, sizeof(context));
1917
1918 if (!strcmp((const char *) context_child_node->name,
1919 config_element_type)) {
1920 /* type */
1921 xmlChar *content = xmlNodeGetContent(context_child_node);
1922 if (!content) {
1923 ret = -LTTNG_ERR_NOMEM;
1924 goto end;
1925 }
1926
1927 ret = get_context_type(content);
1928 free(content);
1929 if (ret < 0) {
1930 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1931 goto end;
1932 }
1933
1934 context.ctx = ret;
1935 } else {
1936 xmlNodePtr perf_attr_node;
1937
1938 /* perf */
1939 context.ctx = LTTNG_EVENT_CONTEXT_PERF_COUNTER;
1940 for (perf_attr_node = xmlFirstElementChild(context_child_node);
1941 perf_attr_node; perf_attr_node =
1942 xmlNextElementSibling(perf_attr_node)) {
1943 if (!strcmp((const char *) perf_attr_node->name,
1944 config_element_type)) {
1945 xmlChar *content;
1946 uint64_t type = 0;
1947
1948 /* type */
1949 content = xmlNodeGetContent(perf_attr_node);
1950 if (!content) {
1951 ret = -LTTNG_ERR_NOMEM;
1952 goto end;
1953 }
1954
1955 ret = parse_uint(content, &type);
1956 free(content);
1957 if (ret) {
1958 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1959 goto end;
1960 }
1961
1962 if (type > UINT32_MAX) {
1963 WARN("perf context type out of range.");
1964 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1965 goto end;
1966 }
1967
1968 context.u.perf_counter.type = type;
1969 } else if (!strcmp((const char *) perf_attr_node->name,
1970 config_element_config)) {
1971 xmlChar *content;
1972 uint64_t config = 0;
1973
1974 /* config */
1975 content = xmlNodeGetContent(perf_attr_node);
1976 if (!content) {
1977 ret = -LTTNG_ERR_NOMEM;
1978 goto end;
1979 }
1980
1981 ret = parse_uint(content, &config);
1982 free(content);
1983 if (ret) {
1984 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1985 goto end;
1986 }
1987
1988 context.u.perf_counter.config = config;
1989 } else if (!strcmp((const char *) perf_attr_node->name,
1990 config_element_name)) {
1991 xmlChar *content;
1992 size_t name_len;
1993
1994 /* name */
1995 content = xmlNodeGetContent(perf_attr_node);
1996 if (!content) {
1997 ret = -LTTNG_ERR_NOMEM;
1998 goto end;
1999 }
2000
2001 name_len = strlen((char *) content);
2002 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2003 WARN("perf context name too long.");
2004 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2005 free(content);
2006 goto end;
2007 }
2008
2009 strncpy(context.u.perf_counter.name, (const char *) content,
2010 name_len);
2011 free(content);
2012 }
2013 }
2014 }
2015
2016 ret = lttng_add_context(handle, &context, NULL, channel_name);
2017 end:
2018 return ret;
2019 }
2020
2021 static
2022 int process_contexts_node(xmlNodePtr contexts_node,
2023 struct lttng_handle *handle, const char *channel_name)
2024 {
2025 int ret = 0;
2026 xmlNodePtr context_node;
2027
2028 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2029 context_node = xmlNextElementSibling(context_node)) {
2030 ret = process_context_node(context_node, handle, channel_name);
2031 if (ret) {
2032 goto end;
2033 }
2034 }
2035 end:
2036 return ret;
2037 }
2038
2039 static
2040 int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2041 {
2042 int ret;
2043 struct lttng_domain domain = { 0 };
2044 struct lttng_handle *handle = NULL;
2045 xmlNodePtr channels_node = NULL;
2046 xmlNodePtr node;
2047
2048 assert(session_name);
2049
2050 ret = init_domain(domain_node, &domain);
2051 if (ret) {
2052 goto end;
2053 }
2054
2055 handle = lttng_create_handle(session_name, &domain);
2056 if (!handle) {
2057 ret = -LTTNG_ERR_NOMEM;
2058 goto end;
2059 }
2060
2061 /* get the channels node */
2062 for (node = xmlFirstElementChild(domain_node); node;
2063 node = xmlNextElementSibling(node)) {
2064 if (!strcmp((const char *) node->name,
2065 config_element_channels)) {
2066 channels_node = node;
2067 break;
2068 }
2069 }
2070
2071 if (!channels_node) {
2072 goto end;
2073 }
2074
2075 /* create all channels */
2076 for (node = xmlFirstElementChild(channels_node); node;
2077 node = xmlNextElementSibling(node)) {
2078 struct lttng_channel channel;
2079 xmlNodePtr contexts_node = NULL;
2080 xmlNodePtr events_node = NULL;
2081 xmlNodePtr channel_attr_node;
2082
2083 memset(&channel, 0, sizeof(channel));
2084 lttng_channel_set_default_attr(&domain, &channel.attr);
2085
2086 for (channel_attr_node = xmlFirstElementChild(node);
2087 channel_attr_node; channel_attr_node =
2088 xmlNextElementSibling(channel_attr_node)) {
2089 ret = process_channel_attr_node(channel_attr_node,
2090 &channel, &contexts_node, &events_node);
2091 if (ret) {
2092 goto end;
2093 }
2094 }
2095
2096 ret = lttng_enable_channel(handle, &channel);
2097 if (ret < 0) {
2098 goto end;
2099 }
2100
2101 ret = process_events_node(events_node, handle, channel.name);
2102 if (ret) {
2103 goto end;
2104 }
2105
2106 ret = process_contexts_node(contexts_node, handle,
2107 channel.name);
2108 if (ret) {
2109 goto end;
2110 }
2111 }
2112 end:
2113 lttng_destroy_handle(handle);
2114 return ret;
2115 }
2116
2117 static
2118 int process_session_node(xmlNodePtr session_node, const char *session_name,
2119 int override)
2120 {
2121 int ret, started = -1, snapshot_mode = -1;
2122 uint64_t live_timer_interval = UINT64_MAX;
2123 char *name = NULL;
2124 xmlNodePtr domains_node = NULL;
2125 xmlNodePtr output_node = NULL;
2126 xmlNodePtr node;
2127 struct lttng_domain *kernel_domain = NULL;
2128 struct lttng_domain *ust_domain = NULL;
2129 struct lttng_domain *jul_domain = NULL;
2130
2131 for (node = xmlFirstElementChild(session_node); node;
2132 node = xmlNextElementSibling(node)) {
2133 if (!name && !strcmp((const char *) node->name,
2134 config_element_name)) {
2135 /* name */
2136 xmlChar *node_content = xmlNodeGetContent(node);
2137 if (!node_content) {
2138 ret = -LTTNG_ERR_NOMEM;
2139 goto end;
2140 }
2141
2142 name = (char *) node_content;
2143 } else if (!domains_node && !strcmp((const char *) node->name,
2144 config_element_domains)) {
2145 /* domains */
2146 domains_node = node;
2147 } else if (started == -1 && !strcmp((const char *) node->name,
2148 config_element_started)) {
2149 /* started */
2150 xmlChar *node_content = xmlNodeGetContent(node);
2151 if (!node_content) {
2152 ret = -LTTNG_ERR_NOMEM;
2153 goto end;
2154 }
2155
2156 ret = parse_bool(node_content, &started);
2157 free(node_content);
2158 if (ret) {
2159 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2160 goto end;
2161 }
2162 } else if (!output_node && !strcmp((const char *) node->name,
2163 config_element_output)) {
2164 /* output */
2165 output_node = node;
2166 } else {
2167 /* attributes, snapshot_mode or live_timer_interval */
2168 xmlNodePtr attributes_child =
2169 xmlFirstElementChild(node);
2170
2171 if (!strcmp((const char *) attributes_child->name,
2172 config_element_snapshot_mode)) {
2173 /* snapshot_mode */
2174 xmlChar *snapshot_mode_content =
2175 xmlNodeGetContent(attributes_child);
2176 if (!snapshot_mode_content) {
2177 ret = -LTTNG_ERR_NOMEM;
2178 goto end;
2179 }
2180
2181 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2182 free(snapshot_mode_content);
2183 if (ret) {
2184 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2185 goto end;
2186 }
2187 } else {
2188 /* live_timer_interval */
2189 xmlChar *timer_interval_content =
2190 xmlNodeGetContent(attributes_child);
2191 if (!timer_interval_content) {
2192 ret = -LTTNG_ERR_NOMEM;
2193 goto end;
2194 }
2195
2196 ret = parse_uint(timer_interval_content, &live_timer_interval);
2197 free(timer_interval_content);
2198 if (ret) {
2199 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2200 goto end;
2201 }
2202 }
2203 }
2204 }
2205
2206 if (!name) {
2207 /* Mandatory attribute, as defined in the session XSD */
2208 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2209 goto end;
2210 }
2211
2212 if (session_name && strcmp(name, session_name)) {
2213 /* This is not the session we are looking for */
2214 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2215 goto end;
2216 }
2217
2218 /* Init domains to create the session handles */
2219 for (node = xmlFirstElementChild(domains_node); node;
2220 node = xmlNextElementSibling(node)) {
2221 struct lttng_domain *domain;
2222
2223 domain = zmalloc(sizeof(*domain));
2224 if (!domain) {
2225 ret = -LTTNG_ERR_NOMEM;
2226 goto end;
2227 }
2228
2229 ret = init_domain(node, domain);
2230 if (ret) {
2231 goto domain_init_error;
2232 }
2233
2234 switch (domain->type) {
2235 case LTTNG_DOMAIN_KERNEL:
2236 if (kernel_domain) {
2237 /* Same domain seen twice, invalid! */
2238 goto domain_init_error;
2239 }
2240 kernel_domain = domain;
2241 break;
2242 case LTTNG_DOMAIN_UST:
2243 if (ust_domain) {
2244 /* Same domain seen twice, invalid! */
2245 goto domain_init_error;
2246 }
2247 ust_domain = domain;
2248 break;
2249 case LTTNG_DOMAIN_JUL:
2250 if (jul_domain) {
2251 /* Same domain seen twice, invalid! */
2252 goto domain_init_error;
2253 }
2254 jul_domain = domain;
2255 break;
2256 default:
2257 WARN("Invalid domain type");
2258 goto domain_init_error;
2259 }
2260 continue;
2261 domain_init_error:
2262 free(domain);
2263 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2264 goto end;
2265 }
2266
2267 if (override) {
2268 /* Destroy session if it exists */
2269 ret = lttng_destroy_session(name);
2270 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2271 ERR("Failed to destroy existing session.");
2272 goto end;
2273 }
2274 }
2275
2276 /* Create session type depending on output type */
2277 if (snapshot_mode && snapshot_mode != -1) {
2278 ret = create_snapshot_session(name, output_node);
2279 } else if (live_timer_interval &&
2280 live_timer_interval != UINT64_MAX) {
2281 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
2282 output_node, live_timer_interval);
2283 } else {
2284 /* regular session */
2285 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
2286 output_node, UINT64_MAX);
2287 }
2288
2289 if (ret) {
2290 goto end;
2291 }
2292
2293 for (node = xmlFirstElementChild(domains_node); node;
2294 node = xmlNextElementSibling(node)) {
2295 ret = process_domain_node(node, name);
2296 if (ret) {
2297 goto end;
2298 }
2299 }
2300
2301 if (started) {
2302 ret = lttng_start_tracing(name);
2303 if (ret) {
2304 goto end;
2305 }
2306 }
2307 end:
2308 free(kernel_domain);
2309 free(ust_domain);
2310 free(jul_domain);
2311 free(name);
2312 return ret;
2313 }
2314
2315 static
2316 int load_session_from_file(const char *path, const char *session_name,
2317 struct session_config_validation_ctx *validation_ctx, int override)
2318 {
2319 int ret, session_found = !session_name;
2320 xmlDocPtr doc = NULL;
2321 xmlNodePtr sessions_node;
2322 xmlNodePtr session_node;
2323 struct stat sb;
2324
2325 assert(path);
2326 assert(validation_ctx);
2327
2328 ret = stat(path, &sb);
2329 if (ret) {
2330 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2331 goto end;
2332 }
2333
2334 doc = xmlParseFile(path);
2335 if (!doc) {
2336 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2337 goto end;
2338 }
2339
2340 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2341 if (ret) {
2342 ERR("Session configuration file validation failed");
2343 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2344 goto end;
2345 }
2346
2347 sessions_node = xmlDocGetRootElement(doc);
2348 if (!sessions_node) {
2349 goto end;
2350 }
2351
2352 for (session_node = xmlFirstElementChild(sessions_node);
2353 session_node; session_node =
2354 xmlNextElementSibling(session_node)) {
2355 ret = process_session_node(session_node,
2356 session_name, override);
2357 if (session_name && ret == 0) {
2358 /* Target session found and loaded */
2359 session_found = 1;
2360 break;
2361 }
2362 }
2363 end:
2364 xmlFreeDoc(doc);
2365 if (!ret) {
2366 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
2367 }
2368 return ret;
2369 }
2370
2371 static
2372 int load_session_from_path(const char *path, const char *session_name,
2373 struct session_config_validation_ctx *validation_ctx, int override)
2374 {
2375 int ret, session_found = !session_name;
2376 DIR *directory = NULL;
2377
2378 assert(path);
2379 assert(validation_ctx);
2380
2381 directory = opendir(path);
2382 if (!directory) {
2383 switch (errno) {
2384 case ENOTDIR:
2385 /* Try the file loading. */
2386 break;
2387 case ENOENT:
2388 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2389 goto end;
2390 default:
2391 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2392 goto end;
2393 }
2394 }
2395 if (directory) {
2396 struct dirent *entry;
2397 struct dirent *result;
2398 char *file_path = NULL;
2399 size_t path_len = strlen(path);
2400
2401 if (path_len >= PATH_MAX) {
2402 ret = -LTTNG_ERR_INVALID;
2403 goto end;
2404 }
2405
2406 entry = zmalloc(sizeof(*entry));
2407 if (!entry) {
2408 ret = -LTTNG_ERR_NOMEM;
2409 goto end;
2410 }
2411
2412 file_path = zmalloc(PATH_MAX);
2413 if (!file_path) {
2414 ret = -LTTNG_ERR_NOMEM;
2415 free(entry);
2416 goto end;
2417 }
2418
2419 strncpy(file_path, path, path_len);
2420 if (file_path[path_len - 1] != '/') {
2421 file_path[path_len++] = '/';
2422 }
2423
2424 ret = 0;
2425 /* Search for *.lttng files */
2426 while (!readdir_r(directory, entry, &result) && result) {
2427 size_t file_name_len = strlen(result->d_name);
2428
2429 if (file_name_len <=
2430 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2431 continue;
2432 }
2433
2434 if (path_len + file_name_len > PATH_MAX) {
2435 continue;
2436 }
2437
2438 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2439 result->d_name + file_name_len - sizeof(
2440 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2441 continue;
2442 }
2443
2444 strncpy(file_path + path_len, result->d_name, file_name_len);
2445 file_path[path_len + file_name_len] = '\0';
2446
2447 ret = load_session_from_file(file_path, session_name,
2448 validation_ctx, override);
2449 if (session_name && !ret) {
2450 session_found = 1;
2451 break;
2452 }
2453 }
2454
2455 free(entry);
2456 free(file_path);
2457 } else {
2458 ret = load_session_from_file(path, session_name,
2459 validation_ctx, override);
2460 if (ret) {
2461 goto end;
2462 } else {
2463 session_found = 1;
2464 }
2465 }
2466
2467 end:
2468 if (directory) {
2469 if (closedir(directory)) {
2470 PERROR("closedir");
2471 }
2472 }
2473
2474 if (!session_found) {
2475 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2476 }
2477
2478 return ret;
2479 }
2480
2481 LTTNG_HIDDEN
2482 int config_load_session(const char *path, const char *session_name,
2483 int override)
2484 {
2485 int ret;
2486 struct session_config_validation_ctx validation_ctx = { 0 };
2487
2488 ret = init_session_config_validation_ctx(&validation_ctx);
2489 if (ret) {
2490 goto end;
2491 }
2492
2493 if (!path) {
2494 /* Try home path */
2495 char *home_path = utils_get_home_dir();
2496 if (home_path) {
2497 char *path;
2498
2499 ret = asprintf(&path, DEFAULT_SESSION_HOME_CONFIGPATH,
2500 home_path);
2501 if (ret < 0) {
2502 goto end;
2503 }
2504
2505 ret = load_session_from_path(path, session_name,
2506 &validation_ctx, 0);
2507 if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
2508 /* Session found or an error occured */
2509 free(path);
2510 goto end;
2511 }
2512
2513 free(path);
2514 }
2515
2516 /* Try system session configuration path */
2517 ret = load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH,
2518 session_name, &validation_ctx, 0);
2519 if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
2520 /* Session found or an error occured */
2521 goto end;
2522 }
2523 } else {
2524 ret = access(path, F_OK);
2525 if (ret < 0) {
2526 PERROR("access");
2527 switch (errno) {
2528 case ENOENT:
2529 ret = -LTTNG_ERR_INVALID;
2530 WARN("Session configuration path does not exist.");
2531 break;
2532 case EACCES:
2533 ret = -LTTNG_ERR_EPERM;
2534 break;
2535 default:
2536 ret = -LTTNG_ERR_UNK;
2537 break;
2538 }
2539 goto end;
2540 }
2541
2542 ret = load_session_from_path(path, session_name,
2543 &validation_ctx, override);
2544 }
2545 end:
2546 fini_session_config_validation_ctx(&validation_ctx);
2547 return ret;
2548 }
This page took 0.09533 seconds and 5 git commands to generate.