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