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