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