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