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