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