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