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