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