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