Fix: Don't leave events enabled if they were saved in a disabled state
[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 if (ret) {
1636 goto end;
1637 }
1638
1639 if (!event.enabled) {
1640 /*
1641 * Note that we should use lttng_disable_event_ext() (2.6+) to
1642 * eliminate the risk of clashing on events of the same
1643 * name (with different event types and loglevels).
1644 *
1645 * Unfortunately, lttng_disable_event_ext() only performs a
1646 * match on the name and event type and errors out if any other
1647 * event attribute is not set to its default value.
1648 *
1649 * This will disable all events that match this name.
1650 */
1651 ret = lttng_disable_event(handle, event.name, channel_name);
1652 }
1653 end:
1654 for (i = 0; i < exclusion_count; i++) {
1655 free(exclusions[i]);
1656 }
1657
1658 free(exclusions);
1659 free(filter_expression);
1660 return ret;
1661 }
1662
1663 static
1664 int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1665 const char *channel_name)
1666 {
1667 int ret = 0;
1668 xmlNodePtr node;
1669
1670 assert(events_node);
1671 assert(handle);
1672 assert(channel_name);
1673
1674 for (node = xmlFirstElementChild(events_node); node;
1675 node = xmlNextElementSibling(node)) {
1676 ret = process_event_node(node, handle, channel_name);
1677 if (ret) {
1678 goto end;
1679 }
1680 }
1681 end:
1682 return ret;
1683 }
1684
1685 static
1686 int process_channel_attr_node(xmlNodePtr attr_node,
1687 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1688 xmlNodePtr *events_node)
1689 {
1690 int ret;
1691
1692 assert(attr_node);
1693 assert(channel);
1694 assert(contexts_node);
1695 assert(events_node);
1696
1697 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1698 xmlChar *content;
1699 size_t name_len;
1700
1701 /* name */
1702 content = xmlNodeGetContent(attr_node);
1703 if (!content) {
1704 ret = -LTTNG_ERR_NOMEM;
1705 goto end;
1706 }
1707
1708 name_len = strlen((char *) content);
1709 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1710 WARN("Channel name too long.");
1711 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1712 free(content);
1713 goto end;
1714 }
1715
1716 strncpy(channel->name, (const char *) content, name_len);
1717 free(content);
1718 } else if (!strcmp((const char *) attr_node->name,
1719 config_element_enabled)) {
1720 xmlChar *content;
1721 int enabled;
1722
1723 /* enabled */
1724 content = xmlNodeGetContent(attr_node);
1725 if (!content) {
1726 ret = -LTTNG_ERR_NOMEM;
1727 goto end;
1728 }
1729
1730 ret = parse_bool(content, &enabled);
1731 free(content);
1732 if (ret) {
1733 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1734 goto end;
1735 }
1736
1737 channel->enabled = enabled;
1738 } else if (!strcmp((const char *) attr_node->name,
1739 config_element_overwrite_mode)) {
1740 xmlChar *content;
1741
1742 /* overwrite_mode */
1743 content = xmlNodeGetContent(attr_node);
1744 if (!content) {
1745 ret = -LTTNG_ERR_NOMEM;
1746 goto end;
1747 }
1748
1749 ret = get_overwrite_mode(content);
1750 free(content);
1751 if (ret < 0) {
1752 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1753 goto end;
1754 }
1755
1756 channel->attr.overwrite = ret;
1757 } else if (!strcmp((const char *) attr_node->name,
1758 config_element_subbuf_size)) {
1759 xmlChar *content;
1760
1761 /* subbuffer_size */
1762 content = xmlNodeGetContent(attr_node);
1763 if (!content) {
1764 ret = -LTTNG_ERR_NOMEM;
1765 goto end;
1766 }
1767
1768 ret = parse_uint(content, &channel->attr.subbuf_size);
1769 free(content);
1770 if (ret) {
1771 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1772 goto end;
1773 }
1774 } else if (!strcmp((const char *) attr_node->name,
1775 config_element_num_subbuf)) {
1776 xmlChar *content;
1777
1778 /* subbuffer_count */
1779 content = xmlNodeGetContent(attr_node);
1780 if (!content) {
1781 ret = -LTTNG_ERR_NOMEM;
1782 goto end;
1783 }
1784
1785 ret = parse_uint(content, &channel->attr.num_subbuf);
1786 free(content);
1787 if (ret) {
1788 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1789 goto end;
1790 }
1791 } else if (!strcmp((const char *) attr_node->name,
1792 config_element_switch_timer_interval)) {
1793 xmlChar *content;
1794 uint64_t switch_timer_interval = 0;
1795
1796 /* switch_timer_interval */
1797 content = xmlNodeGetContent(attr_node);
1798 if (!content) {
1799 ret = -LTTNG_ERR_NOMEM;
1800 goto end;
1801 }
1802
1803 ret = parse_uint(content, &switch_timer_interval);
1804 free(content);
1805 if (ret) {
1806 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1807 goto end;
1808 }
1809
1810 if (switch_timer_interval > UINT_MAX) {
1811 WARN("switch_timer_interval out of range.");
1812 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1813 goto end;
1814 }
1815
1816 channel->attr.switch_timer_interval =
1817 switch_timer_interval;
1818 } else if (!strcmp((const char *) attr_node->name,
1819 config_element_read_timer_interval)) {
1820 xmlChar *content;
1821 uint64_t read_timer_interval = 0;
1822
1823 /* read_timer_interval */
1824 content = xmlNodeGetContent(attr_node);
1825 if (!content) {
1826 ret = -LTTNG_ERR_NOMEM;
1827 goto end;
1828 }
1829
1830 ret = parse_uint(content, &read_timer_interval);
1831 free(content);
1832 if (ret) {
1833 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1834 goto end;
1835 }
1836
1837 if (read_timer_interval > UINT_MAX) {
1838 WARN("read_timer_interval out of range.");
1839 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1840 goto end;
1841 }
1842
1843 channel->attr.read_timer_interval =
1844 read_timer_interval;
1845 } else if (!strcmp((const char *) attr_node->name,
1846 config_element_output_type)) {
1847 xmlChar *content;
1848
1849 /* output_type */
1850 content = xmlNodeGetContent(attr_node);
1851 if (!content) {
1852 ret = -LTTNG_ERR_NOMEM;
1853 goto end;
1854 }
1855
1856 ret = get_output_type(content);
1857 free(content);
1858 if (ret < 0) {
1859 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1860 goto end;
1861 }
1862
1863 channel->attr.output = ret;
1864 } else if (!strcmp((const char *) attr_node->name,
1865 config_element_tracefile_size)) {
1866 xmlChar *content;
1867
1868 /* tracefile_size */
1869 content = xmlNodeGetContent(attr_node);
1870 if (!content) {
1871 ret = -LTTNG_ERR_NOMEM;
1872 goto end;
1873 }
1874
1875 ret = parse_uint(content, &channel->attr.tracefile_size);
1876 free(content);
1877 if (ret) {
1878 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1879 goto end;
1880 }
1881 } else if (!strcmp((const char *) attr_node->name,
1882 config_element_tracefile_count)) {
1883 xmlChar *content;
1884
1885 /* tracefile_count */
1886 content = xmlNodeGetContent(attr_node);
1887 if (!content) {
1888 ret = -LTTNG_ERR_NOMEM;
1889 goto end;
1890 }
1891
1892 ret = parse_uint(content, &channel->attr.tracefile_count);
1893 free(content);
1894 if (ret) {
1895 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1896 goto end;
1897 }
1898 } else if (!strcmp((const char *) attr_node->name,
1899 config_element_live_timer_interval)) {
1900 xmlChar *content;
1901 uint64_t live_timer_interval = 0;
1902
1903 /* live_timer_interval */
1904 content = xmlNodeGetContent(attr_node);
1905 if (!content) {
1906 ret = -LTTNG_ERR_NOMEM;
1907 goto end;
1908 }
1909
1910 ret = parse_uint(content, &live_timer_interval);
1911 free(content);
1912 if (ret) {
1913 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1914 goto end;
1915 }
1916
1917 if (live_timer_interval > UINT_MAX) {
1918 WARN("live_timer_interval out of range.");
1919 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1920 goto end;
1921 }
1922
1923 channel->attr.live_timer_interval =
1924 live_timer_interval;
1925 } else if (!strcmp((const char *) attr_node->name,
1926 config_element_events)) {
1927 /* events */
1928 *events_node = attr_node;
1929 } else {
1930 /* contexts */
1931 *contexts_node = attr_node;
1932 }
1933 ret = 0;
1934 end:
1935 return ret;
1936 }
1937
1938 static
1939 int process_context_node(xmlNodePtr context_node,
1940 struct lttng_handle *handle, const char *channel_name)
1941 {
1942 int ret;
1943 struct lttng_event_context context;
1944 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
1945
1946 assert(handle);
1947 assert(channel_name);
1948
1949 if (!context_child_node) {
1950 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1951 goto end;
1952 }
1953
1954 memset(&context, 0, sizeof(context));
1955
1956 if (!strcmp((const char *) context_child_node->name,
1957 config_element_type)) {
1958 /* type */
1959 xmlChar *content = xmlNodeGetContent(context_child_node);
1960 if (!content) {
1961 ret = -LTTNG_ERR_NOMEM;
1962 goto end;
1963 }
1964
1965 ret = get_context_type(content);
1966 free(content);
1967 if (ret < 0) {
1968 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1969 goto end;
1970 }
1971
1972 context.ctx = ret;
1973 } else {
1974 xmlNodePtr perf_attr_node;
1975
1976 /* perf */
1977 context.ctx = LTTNG_EVENT_CONTEXT_PERF_COUNTER;
1978 for (perf_attr_node = xmlFirstElementChild(context_child_node);
1979 perf_attr_node; perf_attr_node =
1980 xmlNextElementSibling(perf_attr_node)) {
1981 if (!strcmp((const char *) perf_attr_node->name,
1982 config_element_type)) {
1983 xmlChar *content;
1984 uint64_t type = 0;
1985
1986 /* type */
1987 content = xmlNodeGetContent(perf_attr_node);
1988 if (!content) {
1989 ret = -LTTNG_ERR_NOMEM;
1990 goto end;
1991 }
1992
1993 ret = parse_uint(content, &type);
1994 free(content);
1995 if (ret) {
1996 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1997 goto end;
1998 }
1999
2000 if (type > UINT32_MAX) {
2001 WARN("perf context type out of range.");
2002 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2003 goto end;
2004 }
2005
2006 context.u.perf_counter.type = type;
2007 } else if (!strcmp((const char *) perf_attr_node->name,
2008 config_element_config)) {
2009 xmlChar *content;
2010 uint64_t config = 0;
2011
2012 /* config */
2013 content = xmlNodeGetContent(perf_attr_node);
2014 if (!content) {
2015 ret = -LTTNG_ERR_NOMEM;
2016 goto end;
2017 }
2018
2019 ret = parse_uint(content, &config);
2020 free(content);
2021 if (ret) {
2022 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2023 goto end;
2024 }
2025
2026 context.u.perf_counter.config = config;
2027 } else if (!strcmp((const char *) perf_attr_node->name,
2028 config_element_name)) {
2029 xmlChar *content;
2030 size_t name_len;
2031
2032 /* name */
2033 content = xmlNodeGetContent(perf_attr_node);
2034 if (!content) {
2035 ret = -LTTNG_ERR_NOMEM;
2036 goto end;
2037 }
2038
2039 name_len = strlen((char *) content);
2040 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2041 WARN("perf context name too long.");
2042 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2043 free(content);
2044 goto end;
2045 }
2046
2047 strncpy(context.u.perf_counter.name, (const char *) content,
2048 name_len);
2049 free(content);
2050 }
2051 }
2052 }
2053
2054 ret = lttng_add_context(handle, &context, NULL, channel_name);
2055 end:
2056 return ret;
2057 }
2058
2059 static
2060 int process_contexts_node(xmlNodePtr contexts_node,
2061 struct lttng_handle *handle, const char *channel_name)
2062 {
2063 int ret = 0;
2064 xmlNodePtr context_node;
2065
2066 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2067 context_node = xmlNextElementSibling(context_node)) {
2068 ret = process_context_node(context_node, handle, channel_name);
2069 if (ret) {
2070 goto end;
2071 }
2072 }
2073 end:
2074 return ret;
2075 }
2076
2077 static
2078 int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2079 {
2080 int ret;
2081 struct lttng_domain domain = { 0 };
2082 struct lttng_handle *handle = NULL;
2083 xmlNodePtr channels_node = NULL;
2084 xmlNodePtr node;
2085
2086 assert(session_name);
2087
2088 ret = init_domain(domain_node, &domain);
2089 if (ret) {
2090 goto end;
2091 }
2092
2093 handle = lttng_create_handle(session_name, &domain);
2094 if (!handle) {
2095 ret = -LTTNG_ERR_NOMEM;
2096 goto end;
2097 }
2098
2099 /* get the channels node */
2100 for (node = xmlFirstElementChild(domain_node); node;
2101 node = xmlNextElementSibling(node)) {
2102 if (!strcmp((const char *) node->name,
2103 config_element_channels)) {
2104 channels_node = node;
2105 break;
2106 }
2107 }
2108
2109 if (!channels_node) {
2110 goto end;
2111 }
2112
2113 /* create all channels */
2114 for (node = xmlFirstElementChild(channels_node); node;
2115 node = xmlNextElementSibling(node)) {
2116 struct lttng_channel channel;
2117 xmlNodePtr contexts_node = NULL;
2118 xmlNodePtr events_node = NULL;
2119 xmlNodePtr channel_attr_node;
2120
2121 memset(&channel, 0, sizeof(channel));
2122 lttng_channel_set_default_attr(&domain, &channel.attr);
2123
2124 for (channel_attr_node = xmlFirstElementChild(node);
2125 channel_attr_node; channel_attr_node =
2126 xmlNextElementSibling(channel_attr_node)) {
2127 ret = process_channel_attr_node(channel_attr_node,
2128 &channel, &contexts_node, &events_node);
2129 if (ret) {
2130 goto end;
2131 }
2132 }
2133
2134 ret = lttng_enable_channel(handle, &channel);
2135 if (ret < 0) {
2136 goto end;
2137 }
2138
2139 ret = process_events_node(events_node, handle, channel.name);
2140 if (ret) {
2141 goto end;
2142 }
2143
2144 ret = process_contexts_node(contexts_node, handle,
2145 channel.name);
2146 if (ret) {
2147 goto end;
2148 }
2149 }
2150 end:
2151 lttng_destroy_handle(handle);
2152 return ret;
2153 }
2154
2155 static
2156 int process_session_node(xmlNodePtr session_node, const char *session_name,
2157 int override)
2158 {
2159 int ret, started = -1, snapshot_mode = -1;
2160 uint64_t live_timer_interval = UINT64_MAX;
2161 char *name = NULL;
2162 xmlNodePtr domains_node = NULL;
2163 xmlNodePtr output_node = NULL;
2164 xmlNodePtr node;
2165 struct lttng_domain *kernel_domain = NULL;
2166 struct lttng_domain *ust_domain = NULL;
2167 struct lttng_domain *jul_domain = NULL;
2168
2169 for (node = xmlFirstElementChild(session_node); node;
2170 node = xmlNextElementSibling(node)) {
2171 if (!name && !strcmp((const char *) node->name,
2172 config_element_name)) {
2173 /* name */
2174 xmlChar *node_content = xmlNodeGetContent(node);
2175 if (!node_content) {
2176 ret = -LTTNG_ERR_NOMEM;
2177 goto error;
2178 }
2179
2180 name = (char *) node_content;
2181 } else if (!domains_node && !strcmp((const char *) node->name,
2182 config_element_domains)) {
2183 /* domains */
2184 domains_node = node;
2185 } else if (started == -1 && !strcmp((const char *) node->name,
2186 config_element_started)) {
2187 /* started */
2188 xmlChar *node_content = xmlNodeGetContent(node);
2189 if (!node_content) {
2190 ret = -LTTNG_ERR_NOMEM;
2191 goto error;
2192 }
2193
2194 ret = parse_bool(node_content, &started);
2195 free(node_content);
2196 if (ret) {
2197 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2198 goto error;
2199 }
2200 } else if (!output_node && !strcmp((const char *) node->name,
2201 config_element_output)) {
2202 /* output */
2203 output_node = node;
2204 } else {
2205 /* attributes, snapshot_mode or live_timer_interval */
2206 xmlNodePtr attributes_child =
2207 xmlFirstElementChild(node);
2208
2209 if (!strcmp((const char *) attributes_child->name,
2210 config_element_snapshot_mode)) {
2211 /* snapshot_mode */
2212 xmlChar *snapshot_mode_content =
2213 xmlNodeGetContent(attributes_child);
2214 if (!snapshot_mode_content) {
2215 ret = -LTTNG_ERR_NOMEM;
2216 goto error;
2217 }
2218
2219 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2220 free(snapshot_mode_content);
2221 if (ret) {
2222 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2223 goto error;
2224 }
2225 } else {
2226 /* live_timer_interval */
2227 xmlChar *timer_interval_content =
2228 xmlNodeGetContent(attributes_child);
2229 if (!timer_interval_content) {
2230 ret = -LTTNG_ERR_NOMEM;
2231 goto error;
2232 }
2233
2234 ret = parse_uint(timer_interval_content, &live_timer_interval);
2235 free(timer_interval_content);
2236 if (ret) {
2237 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2238 goto error;
2239 }
2240 }
2241 }
2242 }
2243
2244 if (!name) {
2245 /* Mandatory attribute, as defined in the session XSD */
2246 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2247 goto error;
2248 }
2249
2250 if (session_name && strcmp(name, session_name)) {
2251 /* This is not the session we are looking for */
2252 ret = -LTTNG_ERR_NO_SESSION;
2253 goto error;
2254 }
2255
2256 /* Init domains to create the session handles */
2257 for (node = xmlFirstElementChild(domains_node); node;
2258 node = xmlNextElementSibling(node)) {
2259 struct lttng_domain *domain;
2260
2261 domain = zmalloc(sizeof(*domain));
2262 if (!domain) {
2263 ret = -LTTNG_ERR_NOMEM;
2264 goto error;
2265 }
2266
2267 ret = init_domain(node, domain);
2268 if (ret) {
2269 goto domain_init_error;
2270 }
2271
2272 switch (domain->type) {
2273 case LTTNG_DOMAIN_KERNEL:
2274 if (kernel_domain) {
2275 /* Same domain seen twice, invalid! */
2276 goto domain_init_error;
2277 }
2278 kernel_domain = domain;
2279 break;
2280 case LTTNG_DOMAIN_UST:
2281 if (ust_domain) {
2282 /* Same domain seen twice, invalid! */
2283 goto domain_init_error;
2284 }
2285 ust_domain = domain;
2286 break;
2287 case LTTNG_DOMAIN_JUL:
2288 if (jul_domain) {
2289 /* Same domain seen twice, invalid! */
2290 goto domain_init_error;
2291 }
2292 jul_domain = domain;
2293 break;
2294 default:
2295 WARN("Invalid domain type");
2296 goto domain_init_error;
2297 }
2298 continue;
2299 domain_init_error:
2300 free(domain);
2301 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2302 goto error;
2303 }
2304
2305 if (override) {
2306 /* Destroy session if it exists */
2307 ret = lttng_destroy_session(name);
2308 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2309 ERR("Failed to destroy existing session.");
2310 goto error;
2311 }
2312 }
2313
2314 /* Create session type depending on output type */
2315 if (snapshot_mode && snapshot_mode != -1) {
2316 ret = create_snapshot_session(name, output_node);
2317 } else if (live_timer_interval &&
2318 live_timer_interval != UINT64_MAX) {
2319 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
2320 output_node, live_timer_interval);
2321 } else {
2322 /* regular session */
2323 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
2324 output_node, UINT64_MAX);
2325 }
2326 if (ret) {
2327 goto error;
2328 }
2329
2330 for (node = xmlFirstElementChild(domains_node); node;
2331 node = xmlNextElementSibling(node)) {
2332 ret = process_domain_node(node, name);
2333 if (ret) {
2334 goto end;
2335 }
2336 }
2337
2338 if (started) {
2339 ret = lttng_start_tracing(name);
2340 if (ret) {
2341 goto end;
2342 }
2343 }
2344
2345 end:
2346 if (ret < 0) {
2347 ERR("Failed to load session %s: %s", name, lttng_strerror(ret));
2348 lttng_destroy_session(name);
2349 }
2350
2351 error:
2352 free(kernel_domain);
2353 free(ust_domain);
2354 free(jul_domain);
2355 free(name);
2356 return ret;
2357 }
2358
2359 /*
2360 * Return 1 if the given path is readable by the current UID or 0 if not.
2361 * Return -1 if the path is EPERM.
2362 */
2363 static int validate_file_read_creds(const char *path)
2364 {
2365 int ret;
2366
2367 assert(path);
2368
2369 /* Can we read the file. */
2370 ret = access(path, R_OK);
2371 if (!ret) {
2372 goto valid;
2373 }
2374 if (errno == EACCES) {
2375 return -1;
2376 } else {
2377 /* Invalid. */
2378 return 0;
2379 }
2380 valid:
2381 return 1;
2382 }
2383
2384 static
2385 int load_session_from_file(const char *path, const char *session_name,
2386 struct session_config_validation_ctx *validation_ctx, int override)
2387 {
2388 int ret, session_found = !session_name;
2389 xmlDocPtr doc = NULL;
2390 xmlNodePtr sessions_node;
2391 xmlNodePtr session_node;
2392
2393 assert(path);
2394 assert(validation_ctx);
2395
2396 ret = validate_file_read_creds(path);
2397 if (ret != 1) {
2398 if (ret == -1) {
2399 ret = -LTTNG_ERR_EPERM;
2400 } else {
2401 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2402 }
2403 goto end;
2404 }
2405
2406 doc = xmlParseFile(path);
2407 if (!doc) {
2408 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2409 goto end;
2410 }
2411
2412 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2413 if (ret) {
2414 ERR("Session configuration file validation failed");
2415 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2416 goto end;
2417 }
2418
2419 sessions_node = xmlDocGetRootElement(doc);
2420 if (!sessions_node) {
2421 goto end;
2422 }
2423
2424 for (session_node = xmlFirstElementChild(sessions_node);
2425 session_node; session_node =
2426 xmlNextElementSibling(session_node)) {
2427 ret = process_session_node(session_node,
2428 session_name, override);
2429 if (session_name && ret == 0) {
2430 /* Target session found and loaded */
2431 session_found = 1;
2432 break;
2433 }
2434 }
2435 end:
2436 xmlFreeDoc(doc);
2437 if (!ret) {
2438 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
2439 }
2440 return ret;
2441 }
2442
2443 static
2444 int load_session_from_path(const char *path, const char *session_name,
2445 struct session_config_validation_ctx *validation_ctx, int override)
2446 {
2447 int ret, session_found = !session_name;
2448 DIR *directory = NULL;
2449
2450 assert(path);
2451 assert(validation_ctx);
2452
2453 directory = opendir(path);
2454 if (!directory) {
2455 switch (errno) {
2456 case ENOTDIR:
2457 /* Try the file loading. */
2458 break;
2459 case ENOENT:
2460 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2461 goto end;
2462 default:
2463 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2464 goto end;
2465 }
2466 }
2467 if (directory) {
2468 struct dirent *entry;
2469 struct dirent *result;
2470 char *file_path = NULL;
2471 size_t path_len = strlen(path);
2472
2473 if (path_len >= PATH_MAX) {
2474 ret = -LTTNG_ERR_INVALID;
2475 goto end;
2476 }
2477
2478 entry = zmalloc(sizeof(*entry));
2479 if (!entry) {
2480 ret = -LTTNG_ERR_NOMEM;
2481 goto end;
2482 }
2483
2484 file_path = zmalloc(PATH_MAX);
2485 if (!file_path) {
2486 ret = -LTTNG_ERR_NOMEM;
2487 free(entry);
2488 goto end;
2489 }
2490
2491 strncpy(file_path, path, path_len);
2492 if (file_path[path_len - 1] != '/') {
2493 file_path[path_len++] = '/';
2494 }
2495
2496 ret = 0;
2497 /* Search for *.lttng files */
2498 while (!readdir_r(directory, entry, &result) && result) {
2499 size_t file_name_len = strlen(result->d_name);
2500
2501 if (file_name_len <=
2502 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2503 continue;
2504 }
2505
2506 if (path_len + file_name_len >= PATH_MAX) {
2507 continue;
2508 }
2509
2510 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2511 result->d_name + file_name_len - sizeof(
2512 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2513 continue;
2514 }
2515
2516 strncpy(file_path + path_len, result->d_name, file_name_len);
2517 file_path[path_len + file_name_len] = '\0';
2518
2519 ret = load_session_from_file(file_path, session_name,
2520 validation_ctx, override);
2521 if (session_name && !ret) {
2522 session_found = 1;
2523 break;
2524 }
2525 }
2526
2527 free(entry);
2528 free(file_path);
2529 } else {
2530 ret = load_session_from_file(path, session_name,
2531 validation_ctx, override);
2532 if (ret) {
2533 goto end;
2534 } else {
2535 session_found = 1;
2536 }
2537 }
2538
2539 end:
2540 if (directory) {
2541 if (closedir(directory)) {
2542 PERROR("closedir");
2543 }
2544 }
2545
2546 if (!session_found) {
2547 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2548 }
2549
2550 return ret;
2551 }
2552
2553 /*
2554 * Validate that the given path's credentials and the current process have the
2555 * same UID. If so, return 1 else return 0 if it does NOT match.
2556 */
2557 static int validate_path_creds(const char *path)
2558 {
2559 int ret, uid = getuid();
2560 struct stat buf;
2561
2562 assert(path);
2563
2564 if (uid == 0) {
2565 goto valid;
2566 }
2567
2568 ret = stat(path, &buf);
2569 if (ret < 0) {
2570 if (errno != ENOENT) {
2571 PERROR("stat");
2572 }
2573 ret = -LTTNG_ERR_INVALID;
2574 goto valid;
2575 }
2576
2577 if (buf.st_uid != uid) {
2578 goto invalid;
2579 }
2580
2581 valid:
2582 return 1;
2583 invalid:
2584 return 0;
2585 }
2586
2587 LTTNG_HIDDEN
2588 int config_load_session(const char *path, const char *session_name,
2589 int override, unsigned int autoload)
2590 {
2591 int ret;
2592 const char *path_ptr = NULL;
2593 struct session_config_validation_ctx validation_ctx = { 0 };
2594
2595 ret = init_session_config_validation_ctx(&validation_ctx);
2596 if (ret) {
2597 goto end;
2598 }
2599
2600 if (!path) {
2601 char *home_path;
2602 const char *sys_path;
2603
2604 /* Try home path */
2605 home_path = utils_get_home_dir();
2606 if (home_path) {
2607 char path[PATH_MAX];
2608
2609 /*
2610 * Try user session configuration path. Ignore error here so we can
2611 * continue loading the system wide sessions.
2612 */
2613 if (autoload) {
2614 ret = snprintf(path, sizeof(path),
2615 DEFAULT_SESSION_HOME_CONFIGPATH "/"
2616 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
2617 if (ret < 0) {
2618 PERROR("snprintf session autoload home config path");
2619 goto end;
2620 }
2621
2622 /*
2623 * Credentials are only validated for the autoload in order to
2624 * avoid any user session daemon to try to load kernel sessions
2625 * automatically and failing all the times.
2626 */
2627 ret = validate_path_creds(path);
2628 if (ret) {
2629 path_ptr = path;
2630 }
2631 } else {
2632 ret = snprintf(path, sizeof(path),
2633 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
2634 if (ret < 0) {
2635 PERROR("snprintf session home config path");
2636 goto end;
2637 }
2638 path_ptr = path;
2639 }
2640 if (path_ptr) {
2641 ret = load_session_from_path(path_ptr, session_name,
2642 &validation_ctx, override);
2643 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
2644 goto end;
2645 }
2646 /*
2647 * Continue even if the session was found since we have to try
2648 * the system wide sessions.
2649 */
2650 }
2651 }
2652
2653 /* Reset path pointer for the system wide dir. */
2654 path_ptr = NULL;
2655
2656 /* Try system wide configuration directory. */
2657 if (autoload) {
2658 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
2659 DEFAULT_SESSION_CONFIG_AUTOLOAD;
2660 ret = validate_path_creds(sys_path);
2661 if (ret) {
2662 path_ptr = sys_path;
2663 }
2664 } else {
2665 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
2666 path_ptr = sys_path;
2667 }
2668
2669 if (path_ptr) {
2670 ret = load_session_from_path(path_ptr, session_name,
2671 &validation_ctx, override);
2672 }
2673 } else {
2674 ret = access(path, F_OK);
2675 if (ret < 0) {
2676 PERROR("access");
2677 switch (errno) {
2678 case ENOENT:
2679 ret = -LTTNG_ERR_INVALID;
2680 WARN("Session configuration path does not exist.");
2681 break;
2682 case EACCES:
2683 ret = -LTTNG_ERR_EPERM;
2684 break;
2685 default:
2686 ret = -LTTNG_ERR_UNK;
2687 break;
2688 }
2689 goto end;
2690 }
2691
2692 ret = load_session_from_path(path, session_name,
2693 &validation_ctx, override);
2694 }
2695 end:
2696 fini_session_config_validation_ctx(&validation_ctx);
2697 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
2698 /*
2699 * Don't report an error if no sessions are found when called
2700 * without a session_name or a search path.
2701 */
2702 ret = 0;
2703 }
2704 return ret;
2705 }
This page took 0.139305 seconds and 4 git commands to generate.