Fix: Don't leave events enabled if they were saved in a disabled state
[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);
065321e9
JG
1643 if (ret) {
1644 goto end;
1645 }
1646
1647 if (!event.enabled) {
1648 /*
1649 * Note that we should use lttng_disable_event_ext() (2.6+) to
1650 * eliminate the risk of clashing on events of the same
1651 * name (with different event types and loglevels).
1652 *
1653 * Unfortunately, lttng_disable_event_ext() only performs a
1654 * match on the name and event type and errors out if any other
1655 * event attribute is not set to its default value.
1656 *
1657 * This will disable all events that match this name.
1658 */
1659 ret = lttng_disable_event(handle, event.name, channel_name);
1660 }
dcf266c0
JG
1661end:
1662 for (i = 0; i < exclusion_count; i++) {
1663 free(exclusions[i]);
1664 }
1665
1666 free(exclusions);
1667 free(filter_expression);
1668 return ret;
1669}
1670
1671static
1672int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1673 const char *channel_name)
1674{
1675 int ret = 0;
1676 xmlNodePtr node;
1677
1678 assert(events_node);
1679 assert(handle);
1680 assert(channel_name);
1681
1682 for (node = xmlFirstElementChild(events_node); node;
1683 node = xmlNextElementSibling(node)) {
1684 ret = process_event_node(node, handle, channel_name);
1685 if (ret) {
1686 goto end;
1687 }
1688 }
1689end:
1690 return ret;
1691}
1692
1693static
1694int process_channel_attr_node(xmlNodePtr attr_node,
1695 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1696 xmlNodePtr *events_node)
1697{
1698 int ret;
1699
1700 assert(attr_node);
1701 assert(channel);
1702 assert(contexts_node);
1703 assert(events_node);
1704
1705 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1706 xmlChar *content;
1707 size_t name_len;
1708
1709 /* name */
1710 content = xmlNodeGetContent(attr_node);
1711 if (!content) {
1712 ret = -LTTNG_ERR_NOMEM;
1713 goto end;
1714 }
1715
1716 name_len = strlen((char *) content);
1717 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1718 WARN("Channel name too long.");
1719 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1720 free(content);
1721 goto end;
1722 }
1723
1724 strncpy(channel->name, (const char *) content, name_len);
1725 free(content);
1726 } else if (!strcmp((const char *) attr_node->name,
1727 config_element_enabled)) {
1728 xmlChar *content;
1729 int enabled;
1730
1731 /* enabled */
1732 content = xmlNodeGetContent(attr_node);
1733 if (!content) {
1734 ret = -LTTNG_ERR_NOMEM;
1735 goto end;
1736 }
1737
1738 ret = parse_bool(content, &enabled);
1739 free(content);
1740 if (ret) {
1741 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1742 goto end;
1743 }
1744
1745 channel->enabled = enabled;
1746 } else if (!strcmp((const char *) attr_node->name,
1747 config_element_overwrite_mode)) {
1748 xmlChar *content;
1749
1750 /* overwrite_mode */
1751 content = xmlNodeGetContent(attr_node);
1752 if (!content) {
1753 ret = -LTTNG_ERR_NOMEM;
1754 goto end;
1755 }
1756
1757 ret = get_overwrite_mode(content);
1758 free(content);
1759 if (ret < 0) {
1760 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1761 goto end;
1762 }
1763
1764 channel->attr.overwrite = ret;
1765 } else if (!strcmp((const char *) attr_node->name,
1766 config_element_subbuf_size)) {
1767 xmlChar *content;
1768
1769 /* subbuffer_size */
1770 content = xmlNodeGetContent(attr_node);
1771 if (!content) {
1772 ret = -LTTNG_ERR_NOMEM;
1773 goto end;
1774 }
1775
1776 ret = parse_uint(content, &channel->attr.subbuf_size);
1777 free(content);
1778 if (ret) {
1779 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1780 goto end;
1781 }
1782 } else if (!strcmp((const char *) attr_node->name,
1783 config_element_num_subbuf)) {
1784 xmlChar *content;
1785
1786 /* subbuffer_count */
1787 content = xmlNodeGetContent(attr_node);
1788 if (!content) {
1789 ret = -LTTNG_ERR_NOMEM;
1790 goto end;
1791 }
1792
1793 ret = parse_uint(content, &channel->attr.num_subbuf);
1794 free(content);
1795 if (ret) {
1796 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1797 goto end;
1798 }
1799 } else if (!strcmp((const char *) attr_node->name,
1800 config_element_switch_timer_interval)) {
1801 xmlChar *content;
1802 uint64_t switch_timer_interval = 0;
1803
1804 /* switch_timer_interval */
1805 content = xmlNodeGetContent(attr_node);
1806 if (!content) {
1807 ret = -LTTNG_ERR_NOMEM;
1808 goto end;
1809 }
1810
1811 ret = parse_uint(content, &switch_timer_interval);
1812 free(content);
1813 if (ret) {
1814 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1815 goto end;
1816 }
1817
1818 if (switch_timer_interval > UINT_MAX) {
1819 WARN("switch_timer_interval out of range.");
1820 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1821 goto end;
1822 }
1823
1824 channel->attr.switch_timer_interval =
1825 switch_timer_interval;
1826 } else if (!strcmp((const char *) attr_node->name,
1827 config_element_read_timer_interval)) {
1828 xmlChar *content;
1829 uint64_t read_timer_interval = 0;
1830
1831 /* read_timer_interval */
1832 content = xmlNodeGetContent(attr_node);
1833 if (!content) {
1834 ret = -LTTNG_ERR_NOMEM;
1835 goto end;
1836 }
1837
1838 ret = parse_uint(content, &read_timer_interval);
1839 free(content);
1840 if (ret) {
1841 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1842 goto end;
1843 }
1844
1845 if (read_timer_interval > UINT_MAX) {
1846 WARN("read_timer_interval out of range.");
1847 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1848 goto end;
1849 }
1850
1851 channel->attr.read_timer_interval =
1852 read_timer_interval;
1853 } else if (!strcmp((const char *) attr_node->name,
1854 config_element_output_type)) {
1855 xmlChar *content;
1856
1857 /* output_type */
1858 content = xmlNodeGetContent(attr_node);
1859 if (!content) {
1860 ret = -LTTNG_ERR_NOMEM;
1861 goto end;
1862 }
1863
1864 ret = get_output_type(content);
1865 free(content);
1866 if (ret < 0) {
1867 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1868 goto end;
1869 }
1870
1871 channel->attr.output = ret;
1872 } else if (!strcmp((const char *) attr_node->name,
1873 config_element_tracefile_size)) {
1874 xmlChar *content;
1875
1876 /* tracefile_size */
1877 content = xmlNodeGetContent(attr_node);
1878 if (!content) {
1879 ret = -LTTNG_ERR_NOMEM;
1880 goto end;
1881 }
1882
1883 ret = parse_uint(content, &channel->attr.tracefile_size);
1884 free(content);
1885 if (ret) {
1886 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1887 goto end;
1888 }
1889 } else if (!strcmp((const char *) attr_node->name,
1890 config_element_tracefile_count)) {
1891 xmlChar *content;
1892
1893 /* tracefile_count */
1894 content = xmlNodeGetContent(attr_node);
1895 if (!content) {
1896 ret = -LTTNG_ERR_NOMEM;
1897 goto end;
1898 }
1899
1900 ret = parse_uint(content, &channel->attr.tracefile_count);
1901 free(content);
1902 if (ret) {
1903 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1904 goto end;
1905 }
1906 } else if (!strcmp((const char *) attr_node->name,
1907 config_element_live_timer_interval)) {
1908 xmlChar *content;
1909 uint64_t live_timer_interval = 0;
1910
1911 /* live_timer_interval */
1912 content = xmlNodeGetContent(attr_node);
1913 if (!content) {
1914 ret = -LTTNG_ERR_NOMEM;
1915 goto end;
1916 }
1917
1918 ret = parse_uint(content, &live_timer_interval);
1919 free(content);
1920 if (ret) {
1921 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1922 goto end;
1923 }
1924
1925 if (live_timer_interval > UINT_MAX) {
1926 WARN("live_timer_interval out of range.");
1927 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1928 goto end;
1929 }
1930
1931 channel->attr.live_timer_interval =
1932 live_timer_interval;
1933 } else if (!strcmp((const char *) attr_node->name,
1934 config_element_events)) {
1935 /* events */
1936 *events_node = attr_node;
1937 } else {
1938 /* contexts */
1939 *contexts_node = attr_node;
1940 }
1941 ret = 0;
1942end:
1943 return ret;
1944}
1945
1946static
1947int process_context_node(xmlNodePtr context_node,
1948 struct lttng_handle *handle, const char *channel_name)
1949{
1950 int ret;
1951 struct lttng_event_context context;
1952 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
1953
1954 assert(handle);
1955 assert(channel_name);
1956
1957 if (!context_child_node) {
1958 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1959 goto end;
1960 }
1961
1962 memset(&context, 0, sizeof(context));
1963
1964 if (!strcmp((const char *) context_child_node->name,
1965 config_element_type)) {
1966 /* type */
1967 xmlChar *content = xmlNodeGetContent(context_child_node);
1968 if (!content) {
1969 ret = -LTTNG_ERR_NOMEM;
1970 goto end;
1971 }
1972
1973 ret = get_context_type(content);
1974 free(content);
1975 if (ret < 0) {
1976 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1977 goto end;
1978 }
1979
1980 context.ctx = ret;
1981 } else {
1982 xmlNodePtr perf_attr_node;
1983
1984 /* perf */
14ce5bd8
JG
1985 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
1986 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
1987 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
1988 for (perf_attr_node = xmlFirstElementChild(context_child_node);
1989 perf_attr_node; perf_attr_node =
1990 xmlNextElementSibling(perf_attr_node)) {
1991 if (!strcmp((const char *) perf_attr_node->name,
1992 config_element_type)) {
1993 xmlChar *content;
1994 uint64_t type = 0;
1995
1996 /* type */
1997 content = xmlNodeGetContent(perf_attr_node);
1998 if (!content) {
1999 ret = -LTTNG_ERR_NOMEM;
2000 goto end;
2001 }
2002
2003 ret = parse_uint(content, &type);
2004 free(content);
2005 if (ret) {
2006 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2007 goto end;
2008 }
2009
2010 if (type > UINT32_MAX) {
2011 WARN("perf context type out of range.");
2012 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2013 goto end;
2014 }
2015
2016 context.u.perf_counter.type = type;
2017 } else if (!strcmp((const char *) perf_attr_node->name,
2018 config_element_config)) {
2019 xmlChar *content;
2020 uint64_t config = 0;
2021
2022 /* config */
2023 content = xmlNodeGetContent(perf_attr_node);
2024 if (!content) {
2025 ret = -LTTNG_ERR_NOMEM;
2026 goto end;
2027 }
2028
2029 ret = parse_uint(content, &config);
2030 free(content);
2031 if (ret) {
2032 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2033 goto end;
2034 }
2035
2036 context.u.perf_counter.config = config;
2037 } else if (!strcmp((const char *) perf_attr_node->name,
2038 config_element_name)) {
2039 xmlChar *content;
2040 size_t name_len;
2041
2042 /* name */
2043 content = xmlNodeGetContent(perf_attr_node);
2044 if (!content) {
2045 ret = -LTTNG_ERR_NOMEM;
2046 goto end;
2047 }
2048
2049 name_len = strlen((char *) content);
2050 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2051 WARN("perf context name too long.");
2052 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2053 free(content);
2054 goto end;
2055 }
2056
2057 strncpy(context.u.perf_counter.name, (const char *) content,
2058 name_len);
2059 free(content);
2060 }
2061 }
2062 }
2063
2064 ret = lttng_add_context(handle, &context, NULL, channel_name);
2065end:
2066 return ret;
2067}
2068
2069static
2070int process_contexts_node(xmlNodePtr contexts_node,
2071 struct lttng_handle *handle, const char *channel_name)
2072{
2073 int ret = 0;
2074 xmlNodePtr context_node;
2075
2076 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2077 context_node = xmlNextElementSibling(context_node)) {
2078 ret = process_context_node(context_node, handle, channel_name);
2079 if (ret) {
2080 goto end;
2081 }
2082 }
2083end:
2084 return ret;
2085}
2086
2087static
2088int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2089{
2090 int ret;
2091 struct lttng_domain domain = { 0 };
2092 struct lttng_handle *handle = NULL;
2093 xmlNodePtr channels_node = NULL;
2094 xmlNodePtr node;
2095
2096 assert(session_name);
2097
2098 ret = init_domain(domain_node, &domain);
2099 if (ret) {
2100 goto end;
2101 }
2102
2103 handle = lttng_create_handle(session_name, &domain);
2104 if (!handle) {
2105 ret = -LTTNG_ERR_NOMEM;
2106 goto end;
2107 }
2108
2109 /* get the channels node */
2110 for (node = xmlFirstElementChild(domain_node); node;
2111 node = xmlNextElementSibling(node)) {
2112 if (!strcmp((const char *) node->name,
2113 config_element_channels)) {
2114 channels_node = node;
2115 break;
2116 }
2117 }
2118
2119 if (!channels_node) {
2120 goto end;
2121 }
2122
2123 /* create all channels */
2124 for (node = xmlFirstElementChild(channels_node); node;
2125 node = xmlNextElementSibling(node)) {
2126 struct lttng_channel channel;
2127 xmlNodePtr contexts_node = NULL;
2128 xmlNodePtr events_node = NULL;
2129 xmlNodePtr channel_attr_node;
2130
2131 memset(&channel, 0, sizeof(channel));
2132 lttng_channel_set_default_attr(&domain, &channel.attr);
2133
2134 for (channel_attr_node = xmlFirstElementChild(node);
2135 channel_attr_node; channel_attr_node =
2136 xmlNextElementSibling(channel_attr_node)) {
2137 ret = process_channel_attr_node(channel_attr_node,
2138 &channel, &contexts_node, &events_node);
2139 if (ret) {
2140 goto end;
2141 }
2142 }
2143
2144 ret = lttng_enable_channel(handle, &channel);
2145 if (ret < 0) {
2146 goto end;
2147 }
2148
2149 ret = process_events_node(events_node, handle, channel.name);
2150 if (ret) {
2151 goto end;
2152 }
2153
2154 ret = process_contexts_node(contexts_node, handle,
2155 channel.name);
2156 if (ret) {
2157 goto end;
2158 }
2159 }
2160end:
2161 lttng_destroy_handle(handle);
2162 return ret;
2163}
2164
2165static
2166int process_session_node(xmlNodePtr session_node, const char *session_name,
2167 int override)
2168{
2169 int ret, started = -1, snapshot_mode = -1;
2170 uint64_t live_timer_interval = UINT64_MAX;
2171 char *name = NULL;
2172 xmlNodePtr domains_node = NULL;
2173 xmlNodePtr output_node = NULL;
2174 xmlNodePtr node;
2175 struct lttng_domain *kernel_domain = NULL;
2176 struct lttng_domain *ust_domain = NULL;
2177 struct lttng_domain *jul_domain = NULL;
5cdb6027 2178 struct lttng_domain *log4j_domain = NULL;
0e115563 2179 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
2180
2181 for (node = xmlFirstElementChild(session_node); node;
2182 node = xmlNextElementSibling(node)) {
2183 if (!name && !strcmp((const char *) node->name,
2184 config_element_name)) {
2185 /* name */
2186 xmlChar *node_content = xmlNodeGetContent(node);
2187 if (!node_content) {
2188 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2189 goto error;
dcf266c0
JG
2190 }
2191
2192 name = (char *) node_content;
2193 } else if (!domains_node && !strcmp((const char *) node->name,
2194 config_element_domains)) {
2195 /* domains */
2196 domains_node = node;
2197 } else if (started == -1 && !strcmp((const char *) node->name,
2198 config_element_started)) {
2199 /* started */
2200 xmlChar *node_content = xmlNodeGetContent(node);
2201 if (!node_content) {
2202 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2203 goto error;
dcf266c0
JG
2204 }
2205
2206 ret = parse_bool(node_content, &started);
2207 free(node_content);
2208 if (ret) {
2209 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2210 goto error;
dcf266c0
JG
2211 }
2212 } else if (!output_node && !strcmp((const char *) node->name,
2213 config_element_output)) {
2214 /* output */
2215 output_node = node;
2216 } else {
2217 /* attributes, snapshot_mode or live_timer_interval */
2218 xmlNodePtr attributes_child =
2219 xmlFirstElementChild(node);
2220
2221 if (!strcmp((const char *) attributes_child->name,
2222 config_element_snapshot_mode)) {
2223 /* snapshot_mode */
2224 xmlChar *snapshot_mode_content =
2225 xmlNodeGetContent(attributes_child);
2226 if (!snapshot_mode_content) {
2227 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2228 goto error;
dcf266c0
JG
2229 }
2230
2231 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2232 free(snapshot_mode_content);
2233 if (ret) {
2234 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2235 goto error;
dcf266c0
JG
2236 }
2237 } else {
2238 /* live_timer_interval */
2239 xmlChar *timer_interval_content =
2240 xmlNodeGetContent(attributes_child);
2241 if (!timer_interval_content) {
2242 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2243 goto error;
dcf266c0
JG
2244 }
2245
2246 ret = parse_uint(timer_interval_content, &live_timer_interval);
2247 free(timer_interval_content);
2248 if (ret) {
2249 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2250 goto error;
dcf266c0
JG
2251 }
2252 }
2253 }
2254 }
2255
2256 if (!name) {
2257 /* Mandatory attribute, as defined in the session XSD */
2258 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2259 goto error;
dcf266c0
JG
2260 }
2261
2262 if (session_name && strcmp(name, session_name)) {
2263 /* This is not the session we are looking for */
c2da8cde
DG
2264 ret = -LTTNG_ERR_NO_SESSION;
2265 goto error;
dcf266c0
JG
2266 }
2267
2268 /* Init domains to create the session handles */
2269 for (node = xmlFirstElementChild(domains_node); node;
2270 node = xmlNextElementSibling(node)) {
2271 struct lttng_domain *domain;
2272
2273 domain = zmalloc(sizeof(*domain));
2274 if (!domain) {
2275 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2276 goto error;
dcf266c0
JG
2277 }
2278
2279 ret = init_domain(node, domain);
2280 if (ret) {
2281 goto domain_init_error;
2282 }
2283
2284 switch (domain->type) {
2285 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
2286 if (kernel_domain) {
2287 /* Same domain seen twice, invalid! */
2288 goto domain_init_error;
2289 }
dcf266c0
JG
2290 kernel_domain = domain;
2291 break;
2292 case LTTNG_DOMAIN_UST:
c33e6729
DG
2293 if (ust_domain) {
2294 /* Same domain seen twice, invalid! */
2295 goto domain_init_error;
2296 }
dcf266c0
JG
2297 ust_domain = domain;
2298 break;
2299 case LTTNG_DOMAIN_JUL:
c33e6729
DG
2300 if (jul_domain) {
2301 /* Same domain seen twice, invalid! */
2302 goto domain_init_error;
2303 }
dcf266c0
JG
2304 jul_domain = domain;
2305 break;
5cdb6027
DG
2306 case LTTNG_DOMAIN_LOG4J:
2307 if (log4j_domain) {
2308 /* Same domain seen twice, invalid! */
2309 goto domain_init_error;
2310 }
2311 log4j_domain = domain;
2312 break;
0e115563
DG
2313 case LTTNG_DOMAIN_PYTHON:
2314 if (python_domain) {
2315 /* Same domain seen twice, invalid! */
2316 goto domain_init_error;
2317 }
2318 python_domain = domain;
2319 break;
dcf266c0
JG
2320 default:
2321 WARN("Invalid domain type");
2322 goto domain_init_error;
2323 }
2324 continue;
2325domain_init_error:
2326 free(domain);
2327 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2328 goto error;
dcf266c0
JG
2329 }
2330
2331 if (override) {
2332 /* Destroy session if it exists */
2333 ret = lttng_destroy_session(name);
2334 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2335 ERR("Failed to destroy existing session.");
c2da8cde 2336 goto error;
dcf266c0
JG
2337 }
2338 }
2339
2340 /* Create session type depending on output type */
2341 if (snapshot_mode && snapshot_mode != -1) {
2342 ret = create_snapshot_session(name, output_node);
2343 } else if (live_timer_interval &&
2344 live_timer_interval != UINT64_MAX) {
2345 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
5cdb6027 2346 log4j_domain, output_node, live_timer_interval);
dcf266c0
JG
2347 } else {
2348 /* regular session */
2349 ret = create_session(name, kernel_domain, ust_domain, jul_domain,
5cdb6027 2350 log4j_domain, output_node, UINT64_MAX);
dcf266c0 2351 }
dcf266c0 2352 if (ret) {
c2da8cde 2353 goto error;
dcf266c0
JG
2354 }
2355
2356 for (node = xmlFirstElementChild(domains_node); node;
2357 node = xmlNextElementSibling(node)) {
2358 ret = process_domain_node(node, name);
2359 if (ret) {
2360 goto end;
2361 }
2362 }
2363
2364 if (started) {
2365 ret = lttng_start_tracing(name);
2366 if (ret) {
2367 goto end;
2368 }
2369 }
c2da8cde 2370
dcf266c0 2371end:
b2579dc1
JG
2372 if (ret < 0) {
2373 ERR("Failed to load session %s: %s", name, lttng_strerror(ret));
2374 lttng_destroy_session(name);
2375 }
2376
c2da8cde 2377error:
dcf266c0
JG
2378 free(kernel_domain);
2379 free(ust_domain);
2380 free(jul_domain);
5cdb6027 2381 free(log4j_domain);
135a3893 2382 free(python_domain);
dcf266c0
JG
2383 free(name);
2384 return ret;
2385}
2386
cf53c06d
DG
2387/*
2388 * Return 1 if the given path is readable by the current UID or 0 if not.
2389 * Return -1 if the path is EPERM.
2390 */
2391static int validate_file_read_creds(const char *path)
2392{
2393 int ret;
2394
2395 assert(path);
2396
2397 /* Can we read the file. */
2398 ret = access(path, R_OK);
2399 if (!ret) {
2400 goto valid;
2401 }
2402 if (errno == EACCES) {
2403 return -1;
2404 } else {
2405 /* Invalid. */
2406 return 0;
2407 }
2408valid:
2409 return 1;
2410}
2411
dcf266c0
JG
2412static
2413int load_session_from_file(const char *path, const char *session_name,
2414 struct session_config_validation_ctx *validation_ctx, int override)
2415{
2416 int ret, session_found = !session_name;
2417 xmlDocPtr doc = NULL;
2418 xmlNodePtr sessions_node;
2419 xmlNodePtr session_node;
dcf266c0
JG
2420
2421 assert(path);
2422 assert(validation_ctx);
2423
cf53c06d
DG
2424 ret = validate_file_read_creds(path);
2425 if (ret != 1) {
2426 if (ret == -1) {
2427 ret = -LTTNG_ERR_EPERM;
2428 } else {
2429 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2430 }
dcf266c0
JG
2431 goto end;
2432 }
2433
2434 doc = xmlParseFile(path);
2435 if (!doc) {
2436 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2437 goto end;
2438 }
2439
2440 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2441 if (ret) {
2442 ERR("Session configuration file validation failed");
2443 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2444 goto end;
2445 }
2446
2447 sessions_node = xmlDocGetRootElement(doc);
2448 if (!sessions_node) {
2449 goto end;
2450 }
2451
2452 for (session_node = xmlFirstElementChild(sessions_node);
2453 session_node; session_node =
2454 xmlNextElementSibling(session_node)) {
2455 ret = process_session_node(session_node,
2456 session_name, override);
2457 if (session_name && ret == 0) {
2458 /* Target session found and loaded */
2459 session_found = 1;
2460 break;
2461 }
2462 }
2463end:
2464 xmlFreeDoc(doc);
2465 if (!ret) {
a96bc65d 2466 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
2467 }
2468 return ret;
2469}
2470
2471static
2472int load_session_from_path(const char *path, const char *session_name,
2473 struct session_config_validation_ctx *validation_ctx, int override)
2474{
2475 int ret, session_found = !session_name;
dcf266c0
JG
2476 DIR *directory = NULL;
2477
2478 assert(path);
2479 assert(validation_ctx);
2480
4af16958
DG
2481 directory = opendir(path);
2482 if (!directory) {
11143783
DG
2483 switch (errno) {
2484 case ENOTDIR:
0f0a81b5
DG
2485 /* Try the file loading. */
2486 break;
11143783
DG
2487 case ENOENT:
2488 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2489 goto end;
2490 default:
0f0a81b5
DG
2491 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2492 goto end;
4af16958 2493 }
dcf266c0 2494 }
4af16958 2495 if (directory) {
dcf266c0
JG
2496 struct dirent *entry;
2497 struct dirent *result;
2498 char *file_path = NULL;
2499 size_t path_len = strlen(path);
2500
2501 if (path_len >= PATH_MAX) {
2502 ret = -LTTNG_ERR_INVALID;
2503 goto end;
2504 }
2505
2506 entry = zmalloc(sizeof(*entry));
2507 if (!entry) {
2508 ret = -LTTNG_ERR_NOMEM;
2509 goto end;
2510 }
2511
dcf266c0
JG
2512 file_path = zmalloc(PATH_MAX);
2513 if (!file_path) {
2514 ret = -LTTNG_ERR_NOMEM;
2515 free(entry);
2516 goto end;
2517 }
2518
2519 strncpy(file_path, path, path_len);
2520 if (file_path[path_len - 1] != '/') {
2521 file_path[path_len++] = '/';
2522 }
2523
4af16958 2524 ret = 0;
dcf266c0
JG
2525 /* Search for *.lttng files */
2526 while (!readdir_r(directory, entry, &result) && result) {
2527 size_t file_name_len = strlen(result->d_name);
2528
2529 if (file_name_len <=
2530 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2531 continue;
2532 }
2533
431f702e 2534 if (path_len + file_name_len >= PATH_MAX) {
dcf266c0
JG
2535 continue;
2536 }
2537
2538 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2539 result->d_name + file_name_len - sizeof(
2540 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2541 continue;
2542 }
2543
2544 strncpy(file_path + path_len, result->d_name, file_name_len);
2545 file_path[path_len + file_name_len] = '\0';
2546
2547 ret = load_session_from_file(file_path, session_name,
2548 validation_ctx, override);
2549 if (session_name && !ret) {
2550 session_found = 1;
2551 break;
2552 }
2553 }
2554
2555 free(entry);
2556 free(file_path);
2557 } else {
2558 ret = load_session_from_file(path, session_name,
2559 validation_ctx, override);
2560 if (ret) {
2561 goto end;
2562 } else {
2563 session_found = 1;
2564 }
2565 }
2566
2567end:
2568 if (directory) {
2569 if (closedir(directory)) {
2570 PERROR("closedir");
2571 }
2572 }
2573
2574 if (!session_found) {
a96bc65d 2575 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
2576 }
2577
2578 return ret;
2579}
2580
ab38c13f
DG
2581/*
2582 * Validate that the given path's credentials and the current process have the
cf53c06d 2583 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
2584 */
2585static int validate_path_creds(const char *path)
2586{
2587 int ret, uid = getuid();
2588 struct stat buf;
2589
2590 assert(path);
2591
cf53c06d 2592 if (uid == 0) {
ab38c13f
DG
2593 goto valid;
2594 }
2595
2596 ret = stat(path, &buf);
2597 if (ret < 0) {
2598 if (errno != ENOENT) {
2599 PERROR("stat");
2600 }
2601 ret = -LTTNG_ERR_INVALID;
2602 goto valid;
2603 }
2604
2605 if (buf.st_uid != uid) {
2606 goto invalid;
2607 }
2608
2609valid:
ab38c13f 2610 return 1;
cf53c06d
DG
2611invalid:
2612 return 0;
ab38c13f
DG
2613}
2614
dcf266c0
JG
2615LTTNG_HIDDEN
2616int config_load_session(const char *path, const char *session_name,
ab38c13f 2617 int override, unsigned int autoload)
dcf266c0
JG
2618{
2619 int ret;
cf53c06d 2620 const char *path_ptr = NULL;
dcf266c0
JG
2621 struct session_config_validation_ctx validation_ctx = { 0 };
2622
2623 ret = init_session_config_validation_ctx(&validation_ctx);
2624 if (ret) {
2625 goto end;
2626 }
2627
2628 if (!path) {
ab38c13f
DG
2629 char *home_path;
2630 const char *sys_path;
2631
dcf266c0 2632 /* Try home path */
ab38c13f 2633 home_path = utils_get_home_dir();
dcf266c0 2634 if (home_path) {
ab38c13f 2635 char path[PATH_MAX];
dcf266c0 2636
d4fcf703
DG
2637 /*
2638 * Try user session configuration path. Ignore error here so we can
2639 * continue loading the system wide sessions.
2640 */
ab38c13f
DG
2641 if (autoload) {
2642 ret = snprintf(path, sizeof(path),
2643 DEFAULT_SESSION_HOME_CONFIGPATH "/"
2644 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
2645 if (ret < 0) {
2646 PERROR("snprintf session autoload home config path");
2647 goto end;
2648 }
2649
2650 /*
2651 * Credentials are only validated for the autoload in order to
2652 * avoid any user session daemon to try to load kernel sessions
2653 * automatically and failing all the times.
2654 */
2655 ret = validate_path_creds(path);
2656 if (ret) {
2657 path_ptr = path;
2658 }
ab38c13f
DG
2659 } else {
2660 ret = snprintf(path, sizeof(path),
2661 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
2662 if (ret < 0) {
2663 PERROR("snprintf session home config path");
2664 goto end;
2665 }
2666 path_ptr = path;
ab38c13f 2667 }
cf53c06d
DG
2668 if (path_ptr) {
2669 ret = load_session_from_path(path_ptr, session_name,
d4fcf703
DG
2670 &validation_ctx, override);
2671 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
2672 goto end;
2673 }
2674 /*
2675 * Continue even if the session was found since we have to try
2676 * the system wide sessions.
2677 */
ab38c13f 2678 }
d4fcf703 2679 }
ab38c13f 2680
cf53c06d
DG
2681 /* Reset path pointer for the system wide dir. */
2682 path_ptr = NULL;
2683
d4fcf703
DG
2684 /* Try system wide configuration directory. */
2685 if (autoload) {
2686 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
2687 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
2688 ret = validate_path_creds(sys_path);
2689 if (ret) {
2690 path_ptr = sys_path;
2691 }
d4fcf703 2692 } else {
cf53c06d
DG
2693 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
2694 path_ptr = sys_path;
d4fcf703
DG
2695 }
2696
cf53c06d
DG
2697 if (path_ptr) {
2698 ret = load_session_from_path(path_ptr, session_name,
d4fcf703 2699 &validation_ctx, override);
dcf266c0
JG
2700 }
2701 } else {
2702 ret = access(path, F_OK);
2703 if (ret < 0) {
2704 PERROR("access");
2705 switch (errno) {
2706 case ENOENT:
2707 ret = -LTTNG_ERR_INVALID;
2708 WARN("Session configuration path does not exist.");
2709 break;
2710 case EACCES:
2711 ret = -LTTNG_ERR_EPERM;
2712 break;
2713 default:
2714 ret = -LTTNG_ERR_UNK;
2715 break;
2716 }
2717 goto end;
2718 }
2719
2720 ret = load_session_from_path(path, session_name,
2721 &validation_ctx, override);
2722 }
2723end:
2724 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
2725 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
2726 /*
2727 * Don't report an error if no sessions are found when called
2728 * without a session_name or a search path.
2729 */
2730 ret = 0;
2731 }
dcf266c0
JG
2732 return ret;
2733}
This page took 0.135633 seconds and 4 git commands to generate.