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