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