trackers: update config xsd schema
[deliverable/lttng-tools.git] / src / common / config / session-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
6c1c0768 18#define _LGPL_SOURCE
1501a7f3 19#include <assert.h>
1501a7f3 20#include <ctype.h>
1501a7f3
JG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
36f2332b 24#include <inttypes.h>
dcf266c0
JG
25#include <dirent.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/stat.h>
6c66fa0f 29#include <stdbool.h>
1501a7f3
JG
30
31#include <common/defaults.h>
32#include <common/error.h>
33#include <common/macros.h>
34#include <common/utils.h>
cce35f91 35#include <common/dynamic-buffer.h>
e8fa9fb0 36#include <common/compat/getenv.h>
fb198a11
JG
37#include <lttng/lttng-error.h>
38#include <libxml/parser.h>
39#include <libxml/valid.h>
40#include <libxml/xmlschemas.h>
dcf266c0
JG
41#include <libxml/tree.h>
42#include <lttng/lttng.h>
43#include <lttng/snapshot.h>
259c2674 44#include <lttng/rotation.h>
1501a7f3 45
f40ef1d5 46#include "session-config.h"
36f2332b 47#include "config-internal.h"
1501a7f3
JG
48
49struct handler_filter_args {
50 const char* section;
51 config_entry_handler_cb handler;
52 void *user_data;
53};
54
dcf266c0
JG
55struct session_config_validation_ctx {
56 xmlSchemaParserCtxtPtr parser_ctx;
57 xmlSchemaPtr schema;
58 xmlSchemaValidCtxtPtr schema_validation_ctx;
59};
60
96518a56 61const char * const config_element_all = "all";
1501a7f3
JG
62const char * const config_str_yes = "yes";
63const char * const config_str_true = "true";
64const char * const config_str_on = "on";
65const char * const config_str_no = "no";
66const char * const config_str_false = "false";
67const char * const config_str_off = "off";
36f2332b 68const char * const config_xml_encoding = "UTF-8";
fb198a11 69const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */
36f2332b
JG
70const char * const config_xml_indent_string = "\t";
71const char * const config_xml_true = "true";
72const char * const config_xml_false = "false";
1501a7f3 73
fb198a11
JG
74const char * const config_element_channel = "channel";
75const char * const config_element_channels = "channels";
76const char * const config_element_domain = "domain";
77const char * const config_element_domains = "domains";
78const char * const config_element_event = "event";
79const char * const config_element_events = "events";
80const char * const config_element_context = "context";
81const char * const config_element_contexts = "contexts";
82const char * const config_element_attributes = "attributes";
83const char * const config_element_exclusion = "exclusion";
84const char * const config_element_exclusions = "exclusions";
85const char * const config_element_function_attributes = "function_attributes";
86const char * const config_element_probe_attributes = "probe_attributes";
87const char * const config_element_symbol_name = "symbol_name";
88const char * const config_element_address = "address";
89const char * const config_element_offset = "offset";
fb198a11
JG
90const char * const config_element_enabled = "enabled";
91const char * const config_element_overwrite_mode = "overwrite_mode";
92const char * const config_element_subbuf_size = "subbuffer_size";
93const char * const config_element_num_subbuf = "subbuffer_count";
94const char * const config_element_switch_timer_interval = "switch_timer_interval";
95const char * const config_element_read_timer_interval = "read_timer_interval";
6ab3e98b
JG
96LTTNG_HIDDEN const char * const config_element_monitor_timer_interval = "monitor_timer_interval";
97LTTNG_HIDDEN const char * const config_element_blocking_timeout = "blocking_timeout";
fb198a11
JG
98const char * const config_element_output = "output";
99const char * const config_element_output_type = "output_type";
100const char * const config_element_tracefile_size = "tracefile_size";
101const char * const config_element_tracefile_count = "tracefile_count";
102const char * const config_element_live_timer_interval = "live_timer_interval";
ec2ed7da
JG
103LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events";
104LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets";
fb198a11
JG
105const char * const config_element_type = "type";
106const char * const config_element_buffer_type = "buffer_type";
107const char * const config_element_session = "session";
108const char * const config_element_sessions = "sessions";
ec2ed7da
JG
109LTTNG_HIDDEN const char * const config_element_context_perf = "perf";
110LTTNG_HIDDEN const char * const config_element_context_app = "app";
111LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name";
112LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name";
fb198a11
JG
113const char * const config_element_config = "config";
114const char * const config_element_started = "started";
115const char * const config_element_snapshot_mode = "snapshot_mode";
116const char * const config_element_loglevel = "loglevel";
117const char * const config_element_loglevel_type = "loglevel_type";
118const char * const config_element_filter = "filter";
ec2ed7da 119LTTNG_HIDDEN const char * const config_element_filter_expression = "filter_expression";
fb198a11
JG
120const char * const config_element_snapshot_outputs = "snapshot_outputs";
121const char * const config_element_consumer_output = "consumer_output";
122const char * const config_element_destination = "destination";
123const char * const config_element_path = "path";
124const char * const config_element_net_output = "net_output";
125const char * const config_element_control_uri = "control_uri";
126const char * const config_element_data_uri = "data_uri";
127const char * const config_element_max_size = "max_size";
ccf10263 128const char * const config_element_pid = "pid";
96518a56 129const char * const config_element_id = "id";
ccf10263 130const char * const config_element_pids = "pids";
96518a56 131const char * const config_element_name = "name";
9e7c9f56 132const char * const config_element_shared_memory_path = "shared_memory_path";
ebbf5ab7 133const char * const config_element_pid_tracker = "pid_tracker";
96518a56
MD
134const char * const config_element_vpid_tracker = "vpid_tracker";
135const char * const config_element_uid_tracker = "uid_tracker";
136const char * const config_element_vuid_tracker = "vuid_tracker";
137const char * const config_element_gid_tracker = "gid_tracker";
138const char * const config_element_vgid_tracker = "vgid_tracker";
ebbf5ab7 139const char * const config_element_trackers = "trackers";
847a5916 140const char * const config_element_targets = "targets";
96518a56 141const char * const config_element_target_type = "target_type";
847a5916 142const char * const config_element_target_pid = "pid_target";
96518a56
MD
143const char * const config_element_target_vpid = "vpid_target";
144const char * const config_element_target_uid = "uid_target";
145const char * const config_element_target_vuid = "vuid_target";
146const char * const config_element_target_gid = "gid_target";
147const char * const config_element_target_vgid = "vgid_target";
148const char * const config_element_tracker_type = "tracker_type";
fb198a11 149
259c2674 150LTTNG_HIDDEN const char * const config_element_rotation_timer_interval = "rotation_schedule_timer_period";
329f3443 151LTTNG_HIDDEN const char * const config_element_rotation_size = "rotation_schedule_size";
259c2674
JD
152LTTNG_HIDDEN const char * const config_element_rotation_schedule = "rotation_schedule";
153
fb198a11
JG
154const char * const config_domain_type_kernel = "KERNEL";
155const char * const config_domain_type_ust = "UST";
156const char * const config_domain_type_jul = "JUL";
5cdb6027 157const char * const config_domain_type_log4j = "LOG4J";
0e115563 158const char * const config_domain_type_python = "PYTHON";
fb198a11
JG
159
160const char * const config_buffer_type_per_pid = "PER_PID";
161const char * const config_buffer_type_per_uid = "PER_UID";
162const char * const config_buffer_type_global = "GLOBAL";
163
164const char * const config_overwrite_mode_discard = "DISCARD";
165const char * const config_overwrite_mode_overwrite = "OVERWRITE";
166
167const char * const config_output_type_splice = "SPLICE";
168const char * const config_output_type_mmap = "MMAP";
169
170const char * const config_loglevel_type_all = "ALL";
171const char * const config_loglevel_type_range = "RANGE";
172const char * const config_loglevel_type_single = "SINGLE";
173
174const char * const config_event_type_all = "ALL";
175const char * const config_event_type_tracepoint = "TRACEPOINT";
176const char * const config_event_type_probe = "PROBE";
177const char * const config_event_type_function = "FUNCTION";
178const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
179const char * const config_event_type_noop = "NOOP";
180const char * const config_event_type_syscall = "SYSCALL";
181const char * const config_event_type_kprobe = "KPROBE";
182const char * const config_event_type_kretprobe = "KRETPROBE";
183
184const char * const config_event_context_pid = "PID";
185const char * const config_event_context_procname = "PROCNAME";
186const char * const config_event_context_prio = "PRIO";
187const char * const config_event_context_nice = "NICE";
188const char * const config_event_context_vpid = "VPID";
189const char * const config_event_context_tid = "TID";
190const char * const config_event_context_vtid = "VTID";
191const char * const config_event_context_ppid = "PPID";
192const char * const config_event_context_vppid = "VPPID";
193const char * const config_event_context_pthread_id = "PTHREAD_ID";
194const char * const config_event_context_hostname = "HOSTNAME";
195const char * const config_event_context_ip = "IP";
e885a367 196const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER";
ec2ed7da
JG
197LTTNG_HIDDEN const char * const config_event_context_app = "APP";
198LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE";
199LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
200LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
201LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
fb198a11 202
fbd987c9
JG
203/* Deprecated symbols */
204const char * const config_element_perf;
205
d7b645e2
JR
206enum process_event_node_phase {
207 CREATION = 0,
208 ENABLE = 1,
209};
210
dcf266c0
JG
211struct consumer_output {
212 int enabled;
213 char *path;
214 char *control_uri;
215 char *data_uri;
216};
217
1501a7f3
JG
218static int config_entry_handler_filter(struct handler_filter_args *args,
219 const char *section, const char *name, const char *value)
220{
221 int ret = 0;
222 struct config_entry entry = { section, name, value };
223
224 assert(args);
225
226 if (!section || !name || !value) {
227 ret = -EIO;
228 goto end;
229 }
230
231 if (args->section) {
232 if (strcmp(args->section, section)) {
233 goto end;
234 }
235 }
236
237 ret = args->handler(&entry, args->user_data);
238end:
239 return ret;
240}
241
242LTTNG_HIDDEN
243int config_get_section_entries(const char *override_path, const char *section,
244 config_entry_handler_cb handler, void *user_data)
245{
246 int ret = 0;
7ab02a27 247 char *path;
1501a7f3
JG
248 FILE *config_file = NULL;
249 struct handler_filter_args filter = { section, handler, user_data };
250
7ab02a27
DG
251 /* First, try system-wide conf. file. */
252 path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH;
253
254 config_file = fopen(path, "r");
255 if (config_file) {
256 DBG("Loading daemon conf file at %s", path);
257 /*
258 * Return value is not very important here since error or not, we
259 * continue and try the next possible conf. file.
260 */
261 (void) ini_parse_file(config_file,
262 (ini_entry_handler) config_entry_handler_filter,
263 (void *) &filter);
264 fclose(config_file);
265 }
266
267 /* Second is the user local configuration. */
268 path = utils_get_home_dir();
269 if (path) {
270 char fullpath[PATH_MAX];
271
272 ret = snprintf(fullpath, sizeof(fullpath),
273 DEFAULT_DAEMON_HOME_CONFIGPATH, path);
274 if (ret < 0) {
275 PERROR("snprintf user conf. path");
276 goto error;
277 }
278
279 config_file = fopen(fullpath, "r");
280 if (config_file) {
281 DBG("Loading daemon user conf file at %s", path);
282 /*
283 * Return value is not very important here since error or not, we
284 * continue and try the next possible conf. file.
285 */
286 (void) ini_parse_file(config_file,
287 (ini_entry_handler) config_entry_handler_filter,
288 (void *) &filter);
289 fclose(config_file);
290 }
291 }
292
293 /* Final path is the one that the user might have provided. */
1501a7f3
JG
294 if (override_path) {
295 config_file = fopen(override_path, "r");
296 if (config_file) {
7ab02a27
DG
297 DBG("Loading daemon command line conf file at %s", override_path);
298 (void) ini_parse_file(config_file,
299 (ini_entry_handler) config_entry_handler_filter,
300 (void *) &filter);
301 fclose(config_file);
1501a7f3
JG
302 } else {
303 ERR("Failed to open daemon configuration file at %s",
304 override_path);
305 ret = -ENOENT;
7ab02a27 306 goto error;
1501a7f3
JG
307 }
308 }
309
7ab02a27
DG
310 /* Everything went well. */
311 ret = 0;
1501a7f3 312
7ab02a27 313error:
1501a7f3
JG
314 return ret;
315}
316
317LTTNG_HIDDEN
318int config_parse_value(const char *value)
319{
320 int i, ret = 0;
321 char *endptr, *lower_str;
322 size_t len;
323 unsigned long v;
324
325 len = strlen(value);
326 if (!len) {
327 ret = -1;
328 goto end;
329 }
330
331 v = strtoul(value, &endptr, 10);
332 if (endptr != value) {
333 ret = v;
334 goto end;
335 }
336
337 lower_str = zmalloc(len + 1);
338 if (!lower_str) {
339 PERROR("zmalloc");
340 ret = -errno;
341 goto end;
342 }
343
344 for (i = 0; i < len; i++) {
345 lower_str[i] = tolower(value[i]);
346 }
347
348 if (!strcmp(lower_str, config_str_yes) ||
349 !strcmp(lower_str, config_str_true) ||
350 !strcmp(lower_str, config_str_on)) {
351 ret = 1;
352 } else if (!strcmp(lower_str, config_str_no) ||
353 !strcmp(lower_str, config_str_false) ||
354 !strcmp(lower_str, config_str_off)) {
355 ret = 0;
356 } else {
357 ret = -1;
358 }
359
360 free(lower_str);
361end:
362 return ret;
363}
36f2332b
JG
364
365/*
366 * Returns a xmlChar string which must be released using xmlFree().
367 */
368static xmlChar *encode_string(const char *in_str)
369{
370 xmlChar *out_str = NULL;
371 xmlCharEncodingHandlerPtr handler;
372 int out_len, ret, in_len;
373
374 assert(in_str);
375
376 handler = xmlFindCharEncodingHandler(config_xml_encoding);
377 if (!handler) {
378 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
379 goto end;
380 }
381
382 in_len = strlen(in_str);
383 /*
f76d886f
JG
384 * Add 1 byte for the NULL terminted character. The factor 4 here is
385 * used because UTF-8 characters can take up to 4 bytes.
36f2332b 386 */
f76d886f 387 out_len = (in_len * 4) + 1;
36f2332b
JG
388 out_str = xmlMalloc(out_len);
389 if (!out_str) {
390 goto end;
391 }
392
393 ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len);
394 if (ret < 0) {
395 xmlFree(out_str);
396 out_str = NULL;
397 goto end;
398 }
399
400 /* out_len is now the size of out_str */
401 out_str[out_len] = '\0';
402end:
403 return out_str;
404}
405
406LTTNG_HIDDEN
705bb62f 407struct config_writer *config_writer_create(int fd_output, int indent)
36f2332b
JG
408{
409 int ret;
410 struct config_writer *writer;
411 xmlOutputBufferPtr buffer;
412
413 writer = zmalloc(sizeof(struct config_writer));
414 if (!writer) {
415 PERROR("zmalloc config_writer_create");
416 goto end;
417 }
418
419 buffer = xmlOutputBufferCreateFd(fd_output, NULL);
420 if (!buffer) {
421 goto error_destroy;
422 }
423
424 writer->writer = xmlNewTextWriter(buffer);
425 ret = xmlTextWriterStartDocument(writer->writer, NULL,
426 config_xml_encoding, NULL);
427 if (ret < 0) {
428 goto error_destroy;
429 }
430
431 ret = xmlTextWriterSetIndentString(writer->writer,
432 BAD_CAST config_xml_indent_string);
705bb62f 433 if (ret) {
36f2332b
JG
434 goto error_destroy;
435 }
436
705bb62f
JRJ
437 ret = xmlTextWriterSetIndent(writer->writer, indent);
438 if (ret) {
36f2332b
JG
439 goto error_destroy;
440 }
441
442end:
443 return writer;
444error_destroy:
445 config_writer_destroy(writer);
446 return NULL;
447}
448
449LTTNG_HIDDEN
450int config_writer_destroy(struct config_writer *writer)
451{
452 int ret = 0;
453
454 if (!writer) {
455 ret = -EINVAL;
456 goto end;
457 }
458
459 if (xmlTextWriterEndDocument(writer->writer) < 0) {
460 WARN("Could not close XML document");
461 ret = -EIO;
462 }
463
464 if (writer->writer) {
465 xmlFreeTextWriter(writer->writer);
466 }
467
468 free(writer);
469end:
470 return ret;
471}
472
473LTTNG_HIDDEN
474int config_writer_open_element(struct config_writer *writer,
475 const char *element_name)
476{
477 int ret;
478 xmlChar *encoded_element_name;
479
480 if (!writer || !writer->writer || !element_name || !element_name[0]) {
481 ret = -1;
482 goto end;
483 }
484
485 encoded_element_name = encode_string(element_name);
486 if (!encoded_element_name) {
487 ret = -1;
488 goto end;
489 }
490
491 ret = xmlTextWriterStartElement(writer->writer, encoded_element_name);
492 xmlFree(encoded_element_name);
493end:
28676a1d 494 return ret >= 0 ? 0 : ret;
36f2332b
JG
495}
496
e10b6a1c
JG
497LTTNG_HIDDEN
498int config_writer_write_attribute(struct config_writer *writer,
499 const char *name, const char *value)
500{
501 int ret;
502 xmlChar *encoded_name = NULL;
503 xmlChar *encoded_value = NULL;
504
505 if (!writer || !writer->writer || !name || !name[0]) {
506 ret = -1;
507 goto end;
508 }
509
510 encoded_name = encode_string(name);
511 if (!encoded_name) {
512 ret = -1;
513 goto end;
514 }
515
516 encoded_value = encode_string(value);
517 if (!encoded_value) {
518 ret = -1;
519 goto end;
520 }
521
522 ret = xmlTextWriterWriteAttribute(writer->writer, encoded_name,
523 encoded_value);
524end:
525 xmlFree(encoded_name);
526 xmlFree(encoded_value);
527 return ret >= 0 ? 0 : ret;
528}
529
36f2332b
JG
530LTTNG_HIDDEN
531int config_writer_close_element(struct config_writer *writer)
532{
533 int ret;
534
535 if (!writer || !writer->writer) {
536 ret = -1;
537 goto end;
538 }
539
540 ret = xmlTextWriterEndElement(writer->writer);
541end:
28676a1d 542 return ret >= 0 ? 0 : ret;
36f2332b
JG
543}
544
545LTTNG_HIDDEN
546int config_writer_write_element_unsigned_int(struct config_writer *writer,
547 const char *element_name, uint64_t value)
548{
549 int ret;
550 xmlChar *encoded_element_name;
551
552 if (!writer || !writer->writer || !element_name || !element_name[0]) {
553 ret = -1;
554 goto end;
555 }
556
557 encoded_element_name = encode_string(element_name);
558 if (!encoded_element_name) {
559 ret = -1;
560 goto end;
561 }
562
563 ret = xmlTextWriterWriteFormatElement(writer->writer,
564 encoded_element_name, "%" PRIu64, value);
565 xmlFree(encoded_element_name);
566end:
28676a1d 567 return ret >= 0 ? 0 : ret;
36f2332b
JG
568}
569
570LTTNG_HIDDEN
571int config_writer_write_element_signed_int(struct config_writer *writer,
572 const char *element_name, int64_t value)
573{
574 int ret;
575 xmlChar *encoded_element_name;
576
577 if (!writer || !writer->writer || !element_name || !element_name[0]) {
578 ret = -1;
579 goto end;
580 }
581
582 encoded_element_name = encode_string(element_name);
583 if (!encoded_element_name) {
584 ret = -1;
585 goto end;
586 }
587
588 ret = xmlTextWriterWriteFormatElement(writer->writer,
589 encoded_element_name, "%" PRIi64, value);
590 xmlFree(encoded_element_name);
591end:
28676a1d 592 return ret >= 0 ? 0 : ret;
36f2332b
JG
593}
594
595LTTNG_HIDDEN
596int config_writer_write_element_bool(struct config_writer *writer,
597 const char *element_name, int value)
598{
599 return config_writer_write_element_string(writer, element_name,
600 value ? config_xml_true : config_xml_false);
601}
602
603LTTNG_HIDDEN
604int config_writer_write_element_string(struct config_writer *writer,
605 const char *element_name, const char *value)
606{
607 int ret;
608 xmlChar *encoded_element_name = NULL;
609 xmlChar *encoded_value = NULL;
610
611 if (!writer || !writer->writer || !element_name || !element_name[0] ||
612 !value) {
613 ret = -1;
614 goto end;
615 }
616
617 encoded_element_name = encode_string(element_name);
618 if (!encoded_element_name) {
619 ret = -1;
620 goto end;
621 }
622
623 encoded_value = encode_string(value);
624 if (!encoded_value) {
625 ret = -1;
626 goto end;
627 }
628
629 ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name,
dcf266c0 630 encoded_value);
36f2332b
JG
631end:
632 xmlFree(encoded_element_name);
633 xmlFree(encoded_value);
28676a1d 634 return ret >= 0 ? 0 : ret;
36f2332b 635}
dcf266c0
JG
636
637static
638void xml_error_handler(void *ctx, const char *format, ...)
639{
640 char *errMsg;
641 va_list args;
642 int ret;
643
644 va_start(args, format);
645 ret = vasprintf(&errMsg, format, args);
6c043b48 646 va_end(args);
dcf266c0
JG
647 if (ret == -1) {
648 ERR("String allocation failed in xml error handler");
649 return;
650 }
dcf266c0
JG
651
652 fprintf(stderr, "XML Error: %s", errMsg);
653 free(errMsg);
654}
655
656static
657void fini_session_config_validation_ctx(
658 struct session_config_validation_ctx *ctx)
659{
660 if (ctx->parser_ctx) {
661 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
662 }
663
664 if (ctx->schema) {
665 xmlSchemaFree(ctx->schema);
666 }
667
668 if (ctx->schema_validation_ctx) {
669 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
670 }
671
672 memset(ctx, 0, sizeof(struct session_config_validation_ctx));
673}
674
54e399cb
JG
675static
676char *get_session_config_xsd_path()
677{
678 char *xsd_path;
e8fa9fb0 679 const char *base_path = lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
54e399cb
JG
680 size_t base_path_len;
681 size_t max_path_len;
682
683 if (!base_path) {
684 base_path = DEFAULT_SESSION_CONFIG_XSD_PATH;
685 }
686
687 base_path_len = strlen(base_path);
688 max_path_len = base_path_len +
689 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1;
690 xsd_path = zmalloc(max_path_len);
691 if (!xsd_path) {
692 goto end;
693 }
694
00a7f6f8 695 strcpy(xsd_path, base_path);
54e399cb
JG
696 if (xsd_path[base_path_len - 1] != '/') {
697 xsd_path[base_path_len++] = '/';
698 }
699
00a7f6f8 700 strcpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME);
54e399cb
JG
701end:
702 return xsd_path;
703}
704
dcf266c0
JG
705static
706int init_session_config_validation_ctx(
707 struct session_config_validation_ctx *ctx)
708{
709 int ret;
54e399cb
JG
710 char *xsd_path = get_session_config_xsd_path();
711
712 if (!xsd_path) {
713 ret = -LTTNG_ERR_NOMEM;
714 goto end;
715 }
dcf266c0 716
54e399cb 717 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
dcf266c0
JG
718 if (!ctx->parser_ctx) {
719 ERR("XSD parser context creation failed");
720 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
721 goto end;
722 }
723 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
724 xml_error_handler, NULL);
725
726 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
727 if (!ctx->schema) {
728 ERR("XSD parsing failed");
729 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
730 goto end;
731 }
732
733 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
734 if (!ctx->schema_validation_ctx) {
735 ERR("XSD validation context creation failed");
736 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
737 goto end;
738 }
739
740 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
741 xml_error_handler, NULL);
742 ret = 0;
743
744end:
745 if (ret) {
746 fini_session_config_validation_ctx(ctx);
747 }
748
54e399cb 749 free(xsd_path);
dcf266c0
JG
750 return ret;
751}
752
753static
754int parse_uint(xmlChar *str, uint64_t *val)
755{
756 int ret;
757 char *endptr;
758
759 if (!str || !val) {
760 ret = -1;
761 goto end;
762 }
763
764 *val = strtoull((const char *) str, &endptr, 10);
765 if (!endptr || *endptr) {
766 ret = -1;
767 } else {
768 ret = 0;
769 }
770
771end:
772 return ret;
773}
774
775static
776int parse_int(xmlChar *str, int64_t *val)
777{
778 int ret;
779 char *endptr;
780
781 if (!str || !val) {
782 ret = -1;
783 goto end;
784 }
785
786 *val = strtoll((const char *) str, &endptr, 10);
787 if (!endptr || *endptr) {
788 ret = -1;
789 } else {
790 ret = 0;
791 }
792
793end:
794 return ret;
795}
796
797static
798int parse_bool(xmlChar *str, int *val)
799{
800 int ret = 0;
801
802 if (!str || !val) {
803 ret = -1;
804 goto end;
805 }
806
807 if (!strcmp((const char *) str, config_xml_true)) {
808 *val = 1;
809 } else if (!strcmp((const char *) str, config_xml_false)) {
810 *val = 0;
811 } else {
812 WARN("Invalid boolean value encoutered (%s).",
813 (const char *) str);
814 ret = -1;
815 }
816end:
817 return ret;
818}
819
820static
821int get_domain_type(xmlChar *domain)
822{
823 int ret;
824
825 if (!domain) {
826 goto error;
827 }
828
829 if (!strcmp((char *) domain, config_domain_type_kernel)) {
830 ret = LTTNG_DOMAIN_KERNEL;
831 } else if (!strcmp((char *) domain, config_domain_type_ust)) {
832 ret = LTTNG_DOMAIN_UST;
833 } else if (!strcmp((char *) domain, config_domain_type_jul)) {
834 ret = LTTNG_DOMAIN_JUL;
5cdb6027
DG
835 } else if (!strcmp((char *) domain, config_domain_type_log4j)) {
836 ret = LTTNG_DOMAIN_LOG4J;
0e115563
DG
837 } else if (!strcmp((char *) domain, config_domain_type_python)) {
838 ret = LTTNG_DOMAIN_PYTHON;
dcf266c0
JG
839 } else {
840 goto error;
841 }
842
843 return ret;
844error:
845 return -1;
846}
847
848static
849int get_buffer_type(xmlChar *buffer_type)
850{
851 int ret;
852
853 if (!buffer_type) {
854 goto error;
855 }
856
857 if (!strcmp((char *) buffer_type, config_buffer_type_global)) {
858 ret = LTTNG_BUFFER_GLOBAL;
859 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) {
860 ret = LTTNG_BUFFER_PER_UID;
861 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) {
862 ret = LTTNG_BUFFER_PER_PID;
863 } else {
864 goto error;
865 }
866
867 return ret;
868error:
869 return -1;
870}
871
872static
873int get_overwrite_mode(xmlChar *overwrite_mode)
874{
875 int ret;
876
877 if (!overwrite_mode) {
878 goto error;
879 }
880
881 if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) {
882 ret = 1;
883 } else if (!strcmp((char *) overwrite_mode,
884 config_overwrite_mode_discard)) {
885 ret = 0;
886 } else {
887 goto error;
888 }
889
890 return ret;
891error:
892 return -1;
893}
894
895static
896int get_output_type(xmlChar *output_type)
897{
898 int ret;
899
900 if (!output_type) {
901 goto error;
902 }
903
904 if (!strcmp((char *) output_type, config_output_type_mmap)) {
905 ret = LTTNG_EVENT_MMAP;
906 } else if (!strcmp((char *) output_type, config_output_type_splice)) {
907 ret = LTTNG_EVENT_SPLICE;
908 } else {
909 goto error;
910 }
911
912 return ret;
913error:
914 return -1;
915}
916
917static
918int get_event_type(xmlChar *event_type)
919{
920 int ret;
921
922 if (!event_type) {
923 goto error;
924 }
925
926 if (!strcmp((char *) event_type, config_event_type_all)) {
927 ret = LTTNG_EVENT_ALL;
928 } else if (!strcmp((char *) event_type, config_event_type_tracepoint)) {
929 ret = LTTNG_EVENT_TRACEPOINT;
930 } else if (!strcmp((char *) event_type, config_event_type_probe)) {
931 ret = LTTNG_EVENT_PROBE;
932 } else if (!strcmp((char *) event_type, config_event_type_function)) {
933 ret = LTTNG_EVENT_FUNCTION;
934 } else if (!strcmp((char *) event_type,
935 config_event_type_function_entry)) {
936 ret = LTTNG_EVENT_FUNCTION_ENTRY;
937 } else if (!strcmp((char *) event_type, config_event_type_noop)) {
938 ret = LTTNG_EVENT_NOOP;
939 } else if (!strcmp((char *) event_type, config_event_type_syscall)) {
940 ret = LTTNG_EVENT_SYSCALL;
941 } else {
942 goto error;
943 }
944
945 return ret;
946error:
947 return -1;
948}
949
950static
951int get_loglevel_type(xmlChar *loglevel_type)
952{
953 int ret;
954
955 if (!loglevel_type) {
956 goto error;
957 }
958
959 if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) {
960 ret = LTTNG_EVENT_LOGLEVEL_ALL;
961 } else if (!strcmp((char *) loglevel_type,
962 config_loglevel_type_range)) {
963 ret = LTTNG_EVENT_LOGLEVEL_RANGE;
964 } else if (!strcmp((char *) loglevel_type,
965 config_loglevel_type_single)) {
966 ret = LTTNG_EVENT_LOGLEVEL_SINGLE;
967 } else {
968 goto error;
969 }
970
971 return ret;
972error:
973 return -1;
974}
975
976/*
977 * Return the context type or -1 on error.
978 */
979static
980int get_context_type(xmlChar *context_type)
981{
982 int ret;
983
984 if (!context_type) {
985 goto error;
986 }
987
988 if (!strcmp((char *) context_type, config_event_context_pid)) {
989 ret = LTTNG_EVENT_CONTEXT_PID;
990 } else if (!strcmp((char *) context_type,
991 config_event_context_procname)) {
992 ret = LTTNG_EVENT_CONTEXT_PROCNAME;
993 } else if (!strcmp((char *) context_type,
994 config_event_context_prio)) {
995 ret = LTTNG_EVENT_CONTEXT_PRIO;
996 } else if (!strcmp((char *) context_type,
997 config_event_context_nice)) {
998 ret = LTTNG_EVENT_CONTEXT_NICE;
999 } else if (!strcmp((char *) context_type,
1000 config_event_context_vpid)) {
1001 ret = LTTNG_EVENT_CONTEXT_VPID;
1002 } else if (!strcmp((char *) context_type,
1003 config_event_context_tid)) {
1004 ret = LTTNG_EVENT_CONTEXT_TID;
1005 } else if (!strcmp((char *) context_type,
1006 config_event_context_vtid)) {
1007 ret = LTTNG_EVENT_CONTEXT_VTID;
1008 } else if (!strcmp((char *) context_type,
1009 config_event_context_ppid)) {
1010 ret = LTTNG_EVENT_CONTEXT_PPID;
1011 } else if (!strcmp((char *) context_type,
1012 config_event_context_vppid)) {
1013 ret = LTTNG_EVENT_CONTEXT_VPPID;
1014 } else if (!strcmp((char *) context_type,
1015 config_event_context_pthread_id)) {
1016 ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID;
1017 } else if (!strcmp((char *) context_type,
1018 config_event_context_hostname)) {
1019 ret = LTTNG_EVENT_CONTEXT_HOSTNAME;
1020 } else if (!strcmp((char *) context_type,
1021 config_event_context_ip)) {
1022 ret = LTTNG_EVENT_CONTEXT_IP;
1ae5e83e
JD
1023 } else if (!strcmp((char *) context_type,
1024 config_event_context_interruptible)) {
1025 ret = LTTNG_EVENT_CONTEXT_INTERRUPTIBLE;
1026 } else if (!strcmp((char *) context_type,
1027 config_event_context_preemptible)) {
1028 ret = LTTNG_EVENT_CONTEXT_PREEMPTIBLE;
1029 } else if (!strcmp((char *) context_type,
1030 config_event_context_need_reschedule)) {
1031 ret = LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE;
1032 } else if (!strcmp((char *) context_type,
1033 config_event_context_migratable)) {
1034 ret = LTTNG_EVENT_CONTEXT_MIGRATABLE;
dcf266c0
JG
1035 } else {
1036 goto error;
1037 }
1038
1039 return ret;
1040error:
1041 return -1;
1042}
1043
1044static
1045int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
1046{
1047 int ret;
1048 xmlNodePtr node;
1049
1050 for (node = xmlFirstElementChild(domain_node); node;
1051 node = xmlNextElementSibling(node)) {
1052 if (!strcmp((const char *) node->name, config_element_type)) {
1053 /* domain type */
1054 xmlChar *node_content = xmlNodeGetContent(node);
1055 if (!node_content) {
1056 ret = -LTTNG_ERR_NOMEM;
1057 goto end;
1058 }
1059
1060 ret = get_domain_type(node_content);
1061 free(node_content);
1062 if (ret < 0) {
1063 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1064 goto end;
1065 }
1066
1067 domain->type = ret;
1068 } else if (!strcmp((const char *) node->name,
1069 config_element_buffer_type)) {
1070 /* buffer type */
1071 xmlChar *node_content = xmlNodeGetContent(node);
1072 if (!node_content) {
1073 ret = -LTTNG_ERR_NOMEM;
1074 goto end;
1075 }
1076
1077 ret = get_buffer_type(node_content);
1078 free(node_content);
1079 if (ret < 0) {
1080 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1081 goto end;
1082 }
1083
1084 domain->buf_type = ret;
1085 }
1086 }
1087 ret = 0;
1088end:
1089 return ret;
1090}
1091
1092static
1093int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
1094 char **data_uri)
1095{
1096 xmlNodePtr node;
1097
1098 for (node = xmlFirstElementChild(net_output_node); node;
1099 node = xmlNextElementSibling(node)) {
1100 if (!strcmp((const char *) node->name, config_element_control_uri)) {
1101 /* control_uri */
1102 *control_uri = (char *) xmlNodeGetContent(node);
1103 if (!*control_uri) {
1104 break;
1105 }
1106 } else {
1107 /* data_uri */
1108 *data_uri = (char *) xmlNodeGetContent(node);
1109 if (!*data_uri) {
1110 break;
1111 }
1112 }
1113 }
1114
1115 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
1116}
1117
1118static
1119int process_consumer_output(xmlNodePtr consumer_output_node,
1120 struct consumer_output *output)
1121{
1122 int ret;
1123 xmlNodePtr node;
1124
1125 assert(output);
1126
1127 for (node = xmlFirstElementChild(consumer_output_node); node;
1128 node = xmlNextElementSibling(node)) {
1129 if (!strcmp((const char *) node->name, config_element_enabled)) {
1130 xmlChar *enabled_str = xmlNodeGetContent(node);
1131
1132 /* enabled */
1133 if (!enabled_str) {
1134 ret = -LTTNG_ERR_NOMEM;
1135 goto end;
1136 }
1137
1138 ret = parse_bool(enabled_str, &output->enabled);
1139 free(enabled_str);
1140 if (ret) {
1141 goto end;
1142 }
1143 } else {
1144 xmlNodePtr output_type_node;
1145
1146 /* destination */
1147 output_type_node = xmlFirstElementChild(node);
1148 if (!output_type_node) {
1149 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1150 goto end;
1151 }
1152
1153 if (!strcmp((const char *) output_type_node->name,
1154 config_element_path)) {
1155 /* path */
1156 output->path = (char *) xmlNodeGetContent(output_type_node);
1157 if (!output->path) {
1158 ret = -LTTNG_ERR_NOMEM;
1159 goto end;
1160 }
1161 } else {
1162 /* net_output */
1163 ret = get_net_output_uris(output_type_node,
1164 &output->control_uri, &output->data_uri);
1165 if (ret) {
1166 goto end;
1167 }
1168 }
1169 }
1170 }
1171 ret = 0;
1172
1173end:
1174 if (ret) {
1175 free(output->path);
1176 free(output->control_uri);
1177 free(output->data_uri);
1178 memset(output, 0, sizeof(struct consumer_output));
1179 }
1180 return ret;
1181}
1182
1183static
95681498
JG
1184int create_session_net_output(const char *name, const char *control_uri,
1185 const char *data_uri)
dcf266c0
JG
1186{
1187 int ret;
1188 struct lttng_handle *handle;
1189 const char *uri = NULL;
1190
1191 assert(name);
dcf266c0 1192
95681498 1193 handle = lttng_create_handle(name, NULL);
dcf266c0
JG
1194 if (!handle) {
1195 ret = -LTTNG_ERR_NOMEM;
1196 goto end;
1197 }
1198
1199 if (!control_uri || !data_uri) {
1200 uri = control_uri ? control_uri : data_uri;
1201 control_uri = uri;
1202 data_uri = uri;
1203 }
1204
1205 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1206 lttng_destroy_handle(handle);
1207end:
1208 return ret;
1209}
1210
1211static
1b08cbce
JR
1212int create_snapshot_session(const char *session_name, xmlNodePtr output_node,
1213 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1214{
1215 int ret;
1216 xmlNodePtr node = NULL;
1217 xmlNodePtr snapshot_output_list_node;
1218 xmlNodePtr snapshot_output_node;
1219
1220 assert(session_name);
1221
1222 ret = lttng_create_session_snapshot(session_name, NULL);
1223 if (ret) {
1224 goto end;
1225 }
1226
1227 if (!output_node) {
1228 goto end;
1229 }
1230
1231 snapshot_output_list_node = xmlFirstElementChild(output_node);
1232
1233 /* Parse and create snapshot outputs */
1234
1235 for (snapshot_output_node =
1236 xmlFirstElementChild(snapshot_output_list_node);
1237 snapshot_output_node; snapshot_output_node =
1238 xmlNextElementSibling(snapshot_output_node)) {
1239 char *name = NULL;
1240 uint64_t max_size = UINT64_MAX;
1241 struct consumer_output output = { 0 };
1242 struct lttng_snapshot_output *snapshot_output = NULL;
1b08cbce
JR
1243 const char *control_uri = NULL;
1244 const char *data_uri = NULL;
1245 const char *path = NULL;
dcf266c0
JG
1246
1247 for (node = xmlFirstElementChild(snapshot_output_node); node;
1248 node = xmlNextElementSibling(node)) {
1249 if (!strcmp((const char *) node->name,
1250 config_element_name)) {
1251 /* name */
1252 name = (char *) xmlNodeGetContent(node);
1253 if (!name) {
1254 ret = -LTTNG_ERR_NOMEM;
1255 goto error_snapshot_output;
1256 }
1257 } else if (!strcmp((const char *) node->name,
1258 config_element_max_size)) {
1259 xmlChar *content = xmlNodeGetContent(node);
1260
1261 /* max_size */
1262 if (!content) {
1263 ret = -LTTNG_ERR_NOMEM;
1264 goto error_snapshot_output;
1265 }
1266 ret = parse_uint(content, &max_size);
1267 free(content);
1268 if (ret) {
1269 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1270 goto error_snapshot_output;
1271 }
1272 } else {
1273 /* consumer_output */
1274 ret = process_consumer_output(node, &output);
1275 if (ret) {
1276 goto error_snapshot_output;
1277 }
1278 }
1279 }
1280
21a3f144
JR
1281 control_uri = output.control_uri;
1282 data_uri = output.data_uri;
1283 path = output.path;
1284
1b08cbce
JR
1285 if (overrides) {
1286 if (overrides->path_url) {
1b08cbce 1287 path = overrides->path_url;
21a3f144
JR
1288 /* Control/data_uri are null */
1289 control_uri = NULL;
1290 data_uri = NULL;
1b08cbce
JR
1291 } else {
1292 if (overrides->ctrl_url) {
1b08cbce 1293 control_uri = overrides->ctrl_url;
21a3f144
JR
1294 /* path is null */
1295 path = NULL;
1b08cbce
JR
1296 }
1297 if (overrides->data_url) {
1b08cbce 1298 data_uri = overrides->data_url;
21a3f144
JR
1299 /* path is null */
1300 path = NULL;
1b08cbce
JR
1301 }
1302 }
1b08cbce
JR
1303 }
1304
dcf266c0
JG
1305 snapshot_output = lttng_snapshot_output_create();
1306 if (!snapshot_output) {
1307 ret = -LTTNG_ERR_NOMEM;
1308 goto error_snapshot_output;
1309 }
1310
1311 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1312 if (ret) {
1313 goto error_snapshot_output;
1314 }
1315
1316 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1317 if (ret) {
1318 goto error_snapshot_output;
1319 }
1320
1b08cbce
JR
1321 if (path) {
1322 ret = lttng_snapshot_output_set_ctrl_url(path,
dcf266c0
JG
1323 snapshot_output);
1324 if (ret) {
1325 goto error_snapshot_output;
1326 }
1327 } else {
1b08cbce
JR
1328 if (control_uri) {
1329 ret = lttng_snapshot_output_set_ctrl_url(control_uri,
dcf266c0
JG
1330 snapshot_output);
1331 if (ret) {
1332 goto error_snapshot_output;
1333 }
1334 }
1335
1b08cbce
JR
1336 if (data_uri) {
1337 ret = lttng_snapshot_output_set_data_url(data_uri,
dcf266c0
JG
1338 snapshot_output);
1339 if (ret) {
1340 goto error_snapshot_output;
1341 }
1342 }
1343 }
1344
1345 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1346error_snapshot_output:
1347 free(name);
1348 free(output.path);
1349 free(output.control_uri);
1350 free(output.data_uri);
1351 lttng_snapshot_output_destroy(snapshot_output);
1352 if (ret) {
1353 goto end;
1354 }
1355 }
1356end:
1357 return ret;
1358}
1359
1360static
1361int create_session(const char *name,
dcf266c0 1362 xmlNodePtr output_node,
1b08cbce
JR
1363 uint64_t live_timer_interval,
1364 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1365{
1366 int ret;
1367 struct consumer_output output = { 0 };
1368 xmlNodePtr consumer_output_node;
1b08cbce
JR
1369 const char *control_uri = NULL;
1370 const char *data_uri = NULL;
1371 const char *path = NULL;
dcf266c0
JG
1372
1373 assert(name);
dcf266c0
JG
1374
1375 if (output_node) {
1376 consumer_output_node = xmlFirstElementChild(output_node);
1377 if (!consumer_output_node) {
1378 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1379 goto end;
1380 }
1381
1382 if (strcmp((const char *) consumer_output_node->name,
1383 config_element_consumer_output)) {
1384 WARN("Invalid output type, expected %s node",
1385 config_element_consumer_output);
1386 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1387 goto end;
1388 }
1389
1390 ret = process_consumer_output(consumer_output_node, &output);
1391 if (ret) {
1392 goto end;
1393 }
1394 }
1395
21a3f144
JR
1396 control_uri = output.control_uri;
1397 data_uri = output.data_uri;
1398 path = output.path;
1399
1b08cbce
JR
1400 /* Check for override and apply them */
1401 if (overrides) {
1402 if (overrides->path_url) {
1b08cbce 1403 path = overrides->path_url;
21a3f144
JR
1404 /* control/data_uri are null */;
1405 control_uri = NULL;
1406 data_uri = NULL;
1b08cbce
JR
1407 } else {
1408 if (overrides->ctrl_url) {
1b08cbce 1409 control_uri = overrides->ctrl_url;
21a3f144
JR
1410 /* path is null */
1411 path = NULL;
1b08cbce
JR
1412 }
1413 if (overrides->data_url) {
1b08cbce 1414 data_uri = overrides->data_url;
21a3f144
JR
1415 /* path is null */
1416 path = NULL;
1b08cbce
JR
1417 }
1418 }
1b08cbce
JR
1419 }
1420
21a3f144 1421
1b08cbce 1422 if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) {
dcf266c0
JG
1423 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1424 goto end;
1425 }
1426
1b08cbce 1427 if (control_uri || data_uri) {
dcf266c0
JG
1428 /* network destination */
1429 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
b664f89a
DG
1430 /*
1431 * URLs are provided for sure since the test above make sure that
1432 * with a live timer the data and control URIs are provided. So,
1433 * NULL is passed here and will be set right after.
1434 */
1435 ret = lttng_create_session_live(name, NULL, live_timer_interval);
dcf266c0
JG
1436 } else {
1437 ret = lttng_create_session(name, NULL);
1438 }
1439 if (ret) {
1440 goto end;
1441 }
1442
1b08cbce 1443 ret = create_session_net_output(name, control_uri, data_uri);
95681498
JG
1444 if (ret) {
1445 goto end;
dcf266c0 1446 }
95681498 1447
dcf266c0
JG
1448 } else {
1449 /* either local output or no output */
1b08cbce 1450 ret = lttng_create_session(name, path);
dcf266c0
JG
1451 if (ret) {
1452 goto end;
1453 }
1454 }
1455end:
1456 free(output.path);
1457 free(output.control_uri);
1458 free(output.data_uri);
1459 return ret;
1460}
1461static
1462int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1463 struct lttng_event_probe_attr *attr)
1464{
1465 int ret;
1466
1467 assert(probe_attribute_node);
1468 assert(attr);
1469
1470 if (!strcmp((const char *) probe_attribute_node->name,
1471 config_element_address)) {
1472 xmlChar *content;
1473 uint64_t addr = 0;
1474
1475 /* addr */
1476 content = xmlNodeGetContent(probe_attribute_node);
1477 if (!content) {
1478 ret = -LTTNG_ERR_NOMEM;
1479 goto end;
1480 }
1481
1482 ret = parse_uint(content, &addr);
1483 free(content);
1484 if (ret) {
1485 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1486 goto end;
1487 }
1488
1489 attr->addr = addr;
1490 } else if (!strcmp((const char *) probe_attribute_node->name,
1491 config_element_offset)) {
1492 xmlChar *content;
1493 uint64_t offset = 0;
1494
1495 /* offset */
1496 content = xmlNodeGetContent(probe_attribute_node);
1497 if (!content) {
1498 ret = -LTTNG_ERR_NOMEM;
1499 goto end;
1500 }
1501
1502 ret = parse_uint(content, &offset);
1503 free(content);
1504 if (ret) {
1505 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1506 goto end;
1507 }
1508
1509 attr->offset = offset;
1510 } else if (!strcmp((const char *) probe_attribute_node->name,
1511 config_element_symbol_name)) {
1512 xmlChar *content;
dcf266c0
JG
1513
1514 /* symbol_name */
1515 content = xmlNodeGetContent(probe_attribute_node);
1516 if (!content) {
1517 ret = -LTTNG_ERR_NOMEM;
1518 goto end;
1519 }
1520
d2e67842
JG
1521 ret = lttng_strncpy(attr->symbol_name,
1522 (const char *) content,
1523 LTTNG_SYMBOL_NAME_LEN);
1524 if (ret == -1) {
1525 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1526 (const char *) content,
1527 strlen((const char *) content),
1528 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
1529 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1530 free(content);
1531 goto end;
1532 }
dcf266c0
JG
1533 free(content);
1534 }
1535 ret = 0;
1536end:
1537 return ret;
1538}
1539
1540static
1541int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
d7b645e2 1542 const char *channel_name, const enum process_event_node_phase phase)
dcf266c0 1543{
d7b645e2 1544 int ret = 0, i;
dcf266c0
JG
1545 xmlNodePtr node;
1546 struct lttng_event event;
1547 char **exclusions = NULL;
1548 unsigned long exclusion_count = 0;
1549 char *filter_expression = NULL;
1550
1551 assert(event_node);
1552 assert(handle);
1553 assert(channel_name);
1554
1555 memset(&event, 0, sizeof(event));
1556
f40eba3d
JG
1557 /* Initialize default log level which varies by domain */
1558 switch (handle->domain.type)
1559 {
1560 case LTTNG_DOMAIN_JUL:
1561 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1562 break;
1563 case LTTNG_DOMAIN_LOG4J:
1564 event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1565 break;
1566 case LTTNG_DOMAIN_PYTHON:
1567 event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1568 break;
1569 case LTTNG_DOMAIN_UST:
1570 case LTTNG_DOMAIN_KERNEL:
1571 event.loglevel = LTTNG_LOGLEVEL_DEBUG;
1572 break;
1573 default:
1574 assert(0);
1575 }
1576
dcf266c0
JG
1577 for (node = xmlFirstElementChild(event_node); node;
1578 node = xmlNextElementSibling(node)) {
1579 if (!strcmp((const char *) node->name, config_element_name)) {
1580 xmlChar *content;
dcf266c0
JG
1581
1582 /* name */
1583 content = xmlNodeGetContent(node);
1584 if (!content) {
1585 ret = -LTTNG_ERR_NOMEM;
1586 goto end;
1587 }
1588
d2e67842
JG
1589 ret = lttng_strncpy(event.name,
1590 (const char *) content,
1591 LTTNG_SYMBOL_NAME_LEN);
1592 if (ret == -1) {
1593 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1594 (const char *) content,
1595 strlen((const char *) content),
1596 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
1597 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1598 free(content);
1599 goto end;
1600 }
dcf266c0
JG
1601 free(content);
1602 } else if (!strcmp((const char *) node->name,
1603 config_element_enabled)) {
1604 xmlChar *content = xmlNodeGetContent(node);
1605
1606 /* enabled */
1607 if (!content) {
1608 ret = -LTTNG_ERR_NOMEM;
1609 goto end;
1610 }
1611
1612 ret = parse_bool(content, &event.enabled);
1613 free(content);
1614 if (ret) {
1615 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1616 goto end;
1617 }
1618 } else if (!strcmp((const char *) node->name,
1619 config_element_type)) {
1620 xmlChar *content = xmlNodeGetContent(node);
1621
1622 /* type */
1623 if (!content) {
1624 ret = -LTTNG_ERR_NOMEM;
1625 goto end;
1626 }
1627
1628 ret = get_event_type(content);
1629 free(content);
1630 if (ret < 0) {
1631 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1632 goto end;
1633 }
1634
1635 event.type = ret;
1636 } else if (!strcmp((const char *) node->name,
1637 config_element_loglevel_type)) {
1638 xmlChar *content = xmlNodeGetContent(node);
1639
1640 /* loglevel_type */
1641 if (!content) {
1642 ret = -LTTNG_ERR_NOMEM;
1643 goto end;
1644 }
1645
1646 ret = get_loglevel_type(content);
1647 free(content);
1648 if (ret < 0) {
1649 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1650 goto end;
1651 }
1652
1653 event.loglevel_type = ret;
1654 } else if (!strcmp((const char *) node->name,
1655 config_element_loglevel)) {
1656 xmlChar *content;
1657 int64_t loglevel = 0;
1658
1659 /* loglevel */
1660 content = xmlNodeGetContent(node);
1661 if (!content) {
1662 ret = -LTTNG_ERR_NOMEM;
1663 goto end;
1664 }
1665
1666 ret = parse_int(content, &loglevel);
1667 free(content);
1668 if (ret) {
1669 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1670 goto end;
1671 }
1672
1673 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1674 WARN("loglevel out of range.");
1675 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1676 goto end;
1677 }
1678
1679 event.loglevel = loglevel;
1680 } else if (!strcmp((const char *) node->name,
1681 config_element_filter)) {
1682 xmlChar *content =
1683 xmlNodeGetContent(node);
1684
1685 /* filter */
1686 if (!content) {
1687 ret = -LTTNG_ERR_NOMEM;
1688 goto end;
1689 }
1690
02d8ac3d 1691 free(filter_expression);
dcf266c0
JG
1692 filter_expression = strdup((char *) content);
1693 free(content);
1694 if (!filter_expression) {
1695 ret = -LTTNG_ERR_NOMEM;
1696 goto end;
1697 }
1698 } else if (!strcmp((const char *) node->name,
1699 config_element_exclusions)) {
1700 xmlNodePtr exclusion_node;
1701 int exclusion_index = 0;
1702
1703 /* exclusions */
1704 if (exclusions) {
1705 /*
1706 * Exclusions has already been initialized,
1707 * invalid file.
1708 */
1709 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1710 goto end;
1711 }
1712
1713 exclusion_count = xmlChildElementCount(node);
1714 if (!exclusion_count) {
1715 continue;
1716 }
1717
1718 exclusions = zmalloc(exclusion_count * sizeof(char *));
1719 if (!exclusions) {
1720 exclusion_count = 0;
1721 ret = -LTTNG_ERR_NOMEM;
1722 goto end;
1723 }
1724
1725 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1726 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1727 xmlChar *content =
1728 xmlNodeGetContent(exclusion_node);
1729
1730 if (!content) {
1731 ret = -LTTNG_ERR_NOMEM;
1732 goto end;
1733 }
1734
1735 exclusions[exclusion_index] = strdup((const char *) content);
1736 free(content);
1737 if (!exclusions[exclusion_index]) {
1738 ret = -LTTNG_ERR_NOMEM;
1739 goto end;
1740 }
1741 exclusion_index++;
1742 }
1743
1744 event.exclusion = 1;
1745 } else if (!strcmp((const char *) node->name,
1746 config_element_attributes)) {
1747 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1748
1749 /* attributes */
1750 if (!attribute_node) {
1751 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1752 goto end;
1753 }
1754
1755 if (!strcmp((const char *) node->name,
1756 config_element_probe_attributes)) {
1757 xmlNodePtr probe_attribute_node;
1758
1759 /* probe_attributes */
1760 for (probe_attribute_node =
1761 xmlFirstElementChild(attribute_node); probe_attribute_node;
1762 probe_attribute_node = xmlNextElementSibling(
1763 probe_attribute_node)) {
1764
1765 ret = process_probe_attribute_node(probe_attribute_node,
1766 &event.attr.probe);
1767 if (ret) {
1768 goto end;
1769 }
1770 }
1771 } else {
1772 size_t sym_len;
1773 xmlChar *content;
1774 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1775
1776 /* function_attributes */
1777 content = xmlNodeGetContent(symbol_node);
1778 if (!content) {
1779 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1780 goto end;
1781 }
1782
1783 sym_len = strlen((char *) content);
1784 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1785 WARN("Function name too long.");
1786 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1787 free(content);
1788 goto end;
1789 }
1790
d2e67842
JG
1791 ret = lttng_strncpy(
1792 event.attr.ftrace.symbol_name,
1793 (char *) content, sym_len);
1794 if (ret == -1) {
1795 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1796 free(content);
1797 goto end;
1798 }
dcf266c0
JG
1799 free(content);
1800 }
1801 }
1802 }
1803
d7b645e2
JR
1804 if ((event.enabled && phase == ENABLE) || phase == CREATION) {
1805 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1806 filter_expression, exclusion_count, exclusions);
1807 if (ret < 0) {
1808 WARN("Enabling event (name:%s) on load failed.", event.name);
1809 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1810 goto end;
1811 }
065321e9 1812 }
d2e67842 1813 ret = 0;
dcf266c0
JG
1814end:
1815 for (i = 0; i < exclusion_count; i++) {
1816 free(exclusions[i]);
1817 }
1818
1819 free(exclusions);
1820 free(filter_expression);
1821 return ret;
1822}
1823
1824static
1825int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1826 const char *channel_name)
1827{
1828 int ret = 0;
d7b645e2 1829 struct lttng_event event;
dcf266c0
JG
1830 xmlNodePtr node;
1831
1832 assert(events_node);
1833 assert(handle);
1834 assert(channel_name);
1835
1836 for (node = xmlFirstElementChild(events_node); node;
1837 node = xmlNextElementSibling(node)) {
d7b645e2 1838 ret = process_event_node(node, handle, channel_name, CREATION);
dcf266c0
JG
1839 if (ret) {
1840 goto end;
1841 }
1842 }
d7b645e2
JR
1843
1844 /*
1845 * Disable all events to enable only the necessary events.
1846 * Limitations regarding lttng_disable_events and tuple descriptor
1847 * force this approach.
1848 */
1849 memset(&event, 0, sizeof(event));
1850 event.loglevel = -1;
1851 event.type = LTTNG_EVENT_ALL;
1852 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
1853 if (ret) {
1854 goto end;
1855 }
1856
1857 for (node = xmlFirstElementChild(events_node); node;
1858 node = xmlNextElementSibling(node)) {
1859 ret = process_event_node(node, handle, channel_name, ENABLE);
1860 if (ret) {
1861 goto end;
1862 }
1863 }
1864
dcf266c0
JG
1865end:
1866 return ret;
1867}
1868
1869static
1870int process_channel_attr_node(xmlNodePtr attr_node,
1871 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1872 xmlNodePtr *events_node)
1873{
1874 int ret;
1875
1876 assert(attr_node);
1877 assert(channel);
1878 assert(contexts_node);
1879 assert(events_node);
1880
1881 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1882 xmlChar *content;
dcf266c0
JG
1883
1884 /* name */
1885 content = xmlNodeGetContent(attr_node);
1886 if (!content) {
1887 ret = -LTTNG_ERR_NOMEM;
1888 goto end;
1889 }
1890
d2e67842
JG
1891 ret = lttng_strncpy(channel->name,
1892 (const char *) content,
1893 LTTNG_SYMBOL_NAME_LEN);
1894 if (ret == -1) {
1895 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1896 (const char *) content,
1897 strlen((const char *) content),
1898 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
1899 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1900 free(content);
1901 goto end;
1902 }
dcf266c0
JG
1903 free(content);
1904 } else if (!strcmp((const char *) attr_node->name,
1905 config_element_enabled)) {
1906 xmlChar *content;
1907 int enabled;
1908
1909 /* enabled */
1910 content = xmlNodeGetContent(attr_node);
1911 if (!content) {
1912 ret = -LTTNG_ERR_NOMEM;
1913 goto end;
1914 }
1915
1916 ret = parse_bool(content, &enabled);
1917 free(content);
1918 if (ret) {
1919 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1920 goto end;
1921 }
1922
1923 channel->enabled = enabled;
1924 } else if (!strcmp((const char *) attr_node->name,
1925 config_element_overwrite_mode)) {
1926 xmlChar *content;
1927
1928 /* overwrite_mode */
1929 content = xmlNodeGetContent(attr_node);
1930 if (!content) {
1931 ret = -LTTNG_ERR_NOMEM;
1932 goto end;
1933 }
1934
1935 ret = get_overwrite_mode(content);
1936 free(content);
1937 if (ret < 0) {
1938 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1939 goto end;
1940 }
1941
1942 channel->attr.overwrite = ret;
1943 } else if (!strcmp((const char *) attr_node->name,
1944 config_element_subbuf_size)) {
1945 xmlChar *content;
1946
1947 /* subbuffer_size */
1948 content = xmlNodeGetContent(attr_node);
1949 if (!content) {
1950 ret = -LTTNG_ERR_NOMEM;
1951 goto end;
1952 }
1953
1954 ret = parse_uint(content, &channel->attr.subbuf_size);
1955 free(content);
1956 if (ret) {
1957 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1958 goto end;
1959 }
1960 } else if (!strcmp((const char *) attr_node->name,
1961 config_element_num_subbuf)) {
1962 xmlChar *content;
1963
1964 /* subbuffer_count */
1965 content = xmlNodeGetContent(attr_node);
1966 if (!content) {
1967 ret = -LTTNG_ERR_NOMEM;
1968 goto end;
1969 }
1970
1971 ret = parse_uint(content, &channel->attr.num_subbuf);
1972 free(content);
1973 if (ret) {
1974 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1975 goto end;
1976 }
1977 } else if (!strcmp((const char *) attr_node->name,
1978 config_element_switch_timer_interval)) {
1979 xmlChar *content;
1980 uint64_t switch_timer_interval = 0;
1981
1982 /* switch_timer_interval */
1983 content = xmlNodeGetContent(attr_node);
1984 if (!content) {
1985 ret = -LTTNG_ERR_NOMEM;
1986 goto end;
1987 }
1988
1989 ret = parse_uint(content, &switch_timer_interval);
1990 free(content);
1991 if (ret) {
1992 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1993 goto end;
1994 }
1995
1996 if (switch_timer_interval > UINT_MAX) {
1997 WARN("switch_timer_interval out of range.");
1998 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1999 goto end;
2000 }
2001
2002 channel->attr.switch_timer_interval =
2003 switch_timer_interval;
2004 } else if (!strcmp((const char *) attr_node->name,
2005 config_element_read_timer_interval)) {
2006 xmlChar *content;
2007 uint64_t read_timer_interval = 0;
2008
2009 /* read_timer_interval */
2010 content = xmlNodeGetContent(attr_node);
2011 if (!content) {
2012 ret = -LTTNG_ERR_NOMEM;
2013 goto end;
2014 }
2015
2016 ret = parse_uint(content, &read_timer_interval);
2017 free(content);
2018 if (ret) {
2019 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2020 goto end;
2021 }
2022
2023 if (read_timer_interval > UINT_MAX) {
2024 WARN("read_timer_interval out of range.");
2025 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2026 goto end;
2027 }
2028
2029 channel->attr.read_timer_interval =
2030 read_timer_interval;
2031 } else if (!strcmp((const char *) attr_node->name,
2032 config_element_output_type)) {
2033 xmlChar *content;
2034
2035 /* output_type */
2036 content = xmlNodeGetContent(attr_node);
2037 if (!content) {
2038 ret = -LTTNG_ERR_NOMEM;
2039 goto end;
2040 }
2041
2042 ret = get_output_type(content);
2043 free(content);
2044 if (ret < 0) {
2045 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2046 goto end;
2047 }
2048
2049 channel->attr.output = ret;
2050 } else if (!strcmp((const char *) attr_node->name,
2051 config_element_tracefile_size)) {
2052 xmlChar *content;
2053
2054 /* tracefile_size */
2055 content = xmlNodeGetContent(attr_node);
2056 if (!content) {
2057 ret = -LTTNG_ERR_NOMEM;
2058 goto end;
2059 }
2060
2061 ret = parse_uint(content, &channel->attr.tracefile_size);
2062 free(content);
2063 if (ret) {
2064 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2065 goto end;
2066 }
2067 } else if (!strcmp((const char *) attr_node->name,
2068 config_element_tracefile_count)) {
2069 xmlChar *content;
2070
2071 /* tracefile_count */
2072 content = xmlNodeGetContent(attr_node);
2073 if (!content) {
2074 ret = -LTTNG_ERR_NOMEM;
2075 goto end;
2076 }
2077
2078 ret = parse_uint(content, &channel->attr.tracefile_count);
2079 free(content);
2080 if (ret) {
2081 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2082 goto end;
2083 }
2084 } else if (!strcmp((const char *) attr_node->name,
2085 config_element_live_timer_interval)) {
2086 xmlChar *content;
2087 uint64_t live_timer_interval = 0;
2088
2089 /* live_timer_interval */
2090 content = xmlNodeGetContent(attr_node);
2091 if (!content) {
2092 ret = -LTTNG_ERR_NOMEM;
2093 goto end;
2094 }
2095
2096 ret = parse_uint(content, &live_timer_interval);
2097 free(content);
2098 if (ret) {
2099 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2100 goto end;
2101 }
2102
2103 if (live_timer_interval > UINT_MAX) {
2104 WARN("live_timer_interval out of range.");
2105 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2106 goto end;
2107 }
2108
2109 channel->attr.live_timer_interval =
2110 live_timer_interval;
4fc2b126
JR
2111 } else if (!strcmp((const char *) attr_node->name,
2112 config_element_monitor_timer_interval)) {
2113 xmlChar *content;
2114 uint64_t monitor_timer_interval = 0;
2115
2116 /* monitor_timer_interval */
2117 content = xmlNodeGetContent(attr_node);
2118 if (!content) {
2119 ret = -LTTNG_ERR_NOMEM;
2120 goto end;
2121 }
2122
2123 ret = parse_uint(content, &monitor_timer_interval);
2124 free(content);
2125 if (ret) {
2126 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2127 goto end;
2128 }
2129
2130 ret = lttng_channel_set_monitor_timer_interval(channel,
2131 monitor_timer_interval);
2132 if (ret) {
2133 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2134 goto end;
2135 }
275472aa
JR
2136 } else if (!strcmp((const char *) attr_node->name,
2137 config_element_blocking_timeout)) {
2138 xmlChar *content;
2139 int64_t blocking_timeout = 0;
2140
2141 /* blocking_timeout */
2142 content = xmlNodeGetContent(attr_node);
2143 if (!content) {
2144 ret = -LTTNG_ERR_NOMEM;
2145 goto end;
2146 }
2147
2148 ret = parse_int(content, &blocking_timeout);
2149 free(content);
2150 if (ret) {
2151 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2152 goto end;
2153 }
2154
2155 ret = lttng_channel_set_blocking_timeout(channel,
2156 blocking_timeout);
2157 if (ret) {
2158 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2159 goto end;
2160 }
dcf266c0
JG
2161 } else if (!strcmp((const char *) attr_node->name,
2162 config_element_events)) {
2163 /* events */
2164 *events_node = attr_node;
2165 } else {
2166 /* contexts */
2167 *contexts_node = attr_node;
2168 }
2169 ret = 0;
2170end:
2171 return ret;
2172}
2173
2174static
2175int process_context_node(xmlNodePtr context_node,
2176 struct lttng_handle *handle, const char *channel_name)
2177{
2178 int ret;
2179 struct lttng_event_context context;
2180 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2181
2182 assert(handle);
2183 assert(channel_name);
2184
2185 if (!context_child_node) {
2186 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2187 goto end;
2188 }
2189
2190 memset(&context, 0, sizeof(context));
2191
2192 if (!strcmp((const char *) context_child_node->name,
2193 config_element_type)) {
2194 /* type */
2195 xmlChar *content = xmlNodeGetContent(context_child_node);
045fc617 2196
dcf266c0
JG
2197 if (!content) {
2198 ret = -LTTNG_ERR_NOMEM;
2199 goto end;
2200 }
2201
2202 ret = get_context_type(content);
2203 free(content);
2204 if (ret < 0) {
2205 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2206 goto end;
2207 }
2208
2209 context.ctx = ret;
045fc617
JG
2210 } else if (!strcmp((const char *) context_child_node->name,
2211 config_element_context_perf)) {
2212 /* perf */
dcf266c0
JG
2213 xmlNodePtr perf_attr_node;
2214
14ce5bd8
JG
2215 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2216 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2217 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
2218 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2219 perf_attr_node; perf_attr_node =
2220 xmlNextElementSibling(perf_attr_node)) {
2221 if (!strcmp((const char *) perf_attr_node->name,
2222 config_element_type)) {
2223 xmlChar *content;
2224 uint64_t type = 0;
2225
2226 /* type */
2227 content = xmlNodeGetContent(perf_attr_node);
2228 if (!content) {
2229 ret = -LTTNG_ERR_NOMEM;
2230 goto end;
2231 }
2232
2233 ret = parse_uint(content, &type);
2234 free(content);
2235 if (ret) {
2236 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2237 goto end;
2238 }
2239
2240 if (type > UINT32_MAX) {
2241 WARN("perf context type out of range.");
2242 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2243 goto end;
2244 }
2245
2246 context.u.perf_counter.type = type;
2247 } else if (!strcmp((const char *) perf_attr_node->name,
2248 config_element_config)) {
2249 xmlChar *content;
2250 uint64_t config = 0;
2251
2252 /* config */
2253 content = xmlNodeGetContent(perf_attr_node);
2254 if (!content) {
2255 ret = -LTTNG_ERR_NOMEM;
2256 goto end;
2257 }
2258
2259 ret = parse_uint(content, &config);
2260 free(content);
2261 if (ret) {
2262 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2263 goto end;
2264 }
2265
2266 context.u.perf_counter.config = config;
2267 } else if (!strcmp((const char *) perf_attr_node->name,
2268 config_element_name)) {
2269 xmlChar *content;
dcf266c0
JG
2270
2271 /* name */
2272 content = xmlNodeGetContent(perf_attr_node);
2273 if (!content) {
2274 ret = -LTTNG_ERR_NOMEM;
2275 goto end;
2276 }
2277
d2e67842
JG
2278 ret = lttng_strncpy(context.u.perf_counter.name,
2279 (const char *) content,
2280 LTTNG_SYMBOL_NAME_LEN);
2281 if (ret == -1) {
2282 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2283 (const char *) content,
2284 strlen((const char *) content),
2285 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
2286 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2287 free(content);
2288 goto end;
2289 }
dcf266c0
JG
2290 free(content);
2291 }
2292 }
045fc617
JG
2293 } else if (!strcmp((const char *) context_child_node->name,
2294 config_element_context_app)) {
2295 /* application context */
2296 xmlNodePtr app_ctx_node;
2297
2298 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2299 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2300 app_ctx_node; app_ctx_node =
2301 xmlNextElementSibling(app_ctx_node)) {
2302 xmlChar *content;
2303 char **target = strcmp(
2304 (const char *) app_ctx_node->name,
2305 config_element_context_app_provider_name) == 0 ?
2306 &context.u.app_ctx.provider_name :
2307 &context.u.app_ctx.ctx_name;
2308
2309 content = xmlNodeGetContent(app_ctx_node);
2310 if (!content) {
2311 ret = -LTTNG_ERR_NOMEM;
2312 goto end;
2313 }
2314
2315 *target = (char *) content;
2316 }
2317 } else {
2318 /* Unrecognized context type */
2319 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2320 goto end;
dcf266c0
JG
2321 }
2322
2323 ret = lttng_add_context(handle, &context, NULL, channel_name);
045fc617
JG
2324 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2325 free(context.u.app_ctx.provider_name);
2326 free(context.u.app_ctx.ctx_name);
2327 }
dcf266c0
JG
2328end:
2329 return ret;
2330}
2331
2332static
2333int process_contexts_node(xmlNodePtr contexts_node,
2334 struct lttng_handle *handle, const char *channel_name)
2335{
2336 int ret = 0;
2337 xmlNodePtr context_node;
2338
2339 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2340 context_node = xmlNextElementSibling(context_node)) {
2341 ret = process_context_node(context_node, handle, channel_name);
2342 if (ret) {
2343 goto end;
2344 }
2345 }
2346end:
2347 return ret;
2348}
2349
847a5916 2350static
96518a56
MD
2351int get_tracker_elements(enum lttng_tracker_type tracker_type,
2352 const char **element_id_tracker,
2353 const char **element_target_id,
2354 const char **element_id,
2355 const char **element_id_alias,
2356 const char **element_name)
2357{
2358 int ret = 0;
2359
2360 switch (tracker_type) {
2361 case LTTNG_TRACKER_PID:
2362 *element_id_tracker = config_element_pid_tracker;
2363 *element_target_id = config_element_target_pid;
2364 *element_id = config_element_id;
2365 *element_id_alias = config_element_pid;
2366 *element_name = NULL;
2367 break;
2368 case LTTNG_TRACKER_VPID:
2369 *element_id_tracker = config_element_vpid_tracker;
2370 *element_target_id = config_element_target_vpid;
2371 *element_id = config_element_id;
2372 *element_id_alias = NULL;
2373 *element_name = NULL;
2374 break;
2375 case LTTNG_TRACKER_UID:
2376 *element_id_tracker = config_element_uid_tracker;
2377 *element_target_id = config_element_target_uid;
2378 *element_id = config_element_id;
2379 *element_id_alias = NULL;
2380 *element_name = config_element_name;
2381 break;
2382 case LTTNG_TRACKER_VUID:
2383 *element_id_tracker = config_element_vuid_tracker;
2384 *element_target_id = config_element_target_vuid;
2385 *element_id = config_element_id;
2386 *element_id_alias = NULL;
2387 *element_name = config_element_name;
2388 break;
2389 case LTTNG_TRACKER_GID:
2390 *element_id_tracker = config_element_gid_tracker;
2391 *element_target_id = config_element_target_gid;
2392 *element_id = config_element_id;
2393 *element_id_alias = NULL;
2394 *element_name = config_element_name;
2395 break;
2396 case LTTNG_TRACKER_VGID:
2397 *element_id_tracker = config_element_vgid_tracker;
2398 *element_target_id = config_element_target_vgid;
2399 *element_id = config_element_id;
2400 *element_id_alias = NULL;
2401 *element_name = config_element_name;
2402 break;
2403 default:
2404 ret = LTTNG_ERR_INVALID;
2405 }
2406 return ret;
2407}
2408
2409static
2410int process_id_tracker_node(xmlNodePtr id_tracker_node,
2411 struct lttng_handle *handle, enum lttng_tracker_type tracker_type)
847a5916 2412{
dd49e13f 2413 int ret = 0, child;
847a5916
JR
2414 xmlNodePtr targets_node = NULL;
2415 xmlNodePtr node;
96518a56
MD
2416 const char *element_id_tracker;
2417 const char *element_target_id;
2418 const char *element_id;
2419 const char *element_id_alias;
2420 const char *element_name;
847a5916
JR
2421
2422 assert(handle);
96518a56
MD
2423 assert(id_tracker_node);
2424
2425 ret = get_tracker_elements(tracker_type, &element_id_tracker,
2426 &element_target_id, &element_id, &element_id_alias, &element_name);
2427 if (ret) {
2428 return ret;
2429 }
2430
847a5916 2431 /* get the targets node */
96518a56 2432 for (node = xmlFirstElementChild(id_tracker_node); node;
847a5916
JR
2433 node = xmlNextElementSibling(node)) {
2434 if (!strcmp((const char *) node->name,
2435 config_element_targets)) {
2436 targets_node = node;
2437 break;
2438 }
2439 }
2440
2441 if (!targets_node) {
2442 ret = LTTNG_ERR_INVALID;
2443 goto end;
2444 }
2445
96518a56 2446 /* Go through all id target node */
847a5916
JR
2447 child = xmlChildElementCount(targets_node);
2448 if (child == 0) {
96518a56
MD
2449 struct lttng_tracker_id tracker_id;
2450
2451 tracker_id.type = LTTNG_ID_ALL;
847a5916 2452 /* The session is explicitly set to target nothing. */
96518a56 2453 ret = lttng_untrack_id(handle, tracker_type, &tracker_id);
847a5916
JR
2454 if (ret) {
2455 goto end;
2456 }
2457 }
2458 for (node = xmlFirstElementChild(targets_node); node;
2459 node = xmlNextElementSibling(node)) {
96518a56 2460 xmlNodePtr id_target_node = node;
847a5916 2461
96518a56
MD
2462 /* get id node and track it */
2463 for (node = xmlFirstElementChild(id_target_node); node;
847a5916 2464 node = xmlNextElementSibling(node)) {
96518a56
MD
2465 if (!strcmp((const char *) node->name, element_id) ||
2466 (element_id_alias && !strcmp((const char *) node->name,
2467 element_id_alias))) {
2468 int64_t id;
847a5916 2469 xmlChar *content = NULL;
96518a56 2470 struct lttng_tracker_id tracker_id;
847a5916
JR
2471
2472 content = xmlNodeGetContent(node);
2473 if (!content) {
2474 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2475 goto end;
2476 }
2477
96518a56 2478 ret = parse_int(content, &id);
847a5916
JR
2479 free(content);
2480 if (ret) {
2481 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2482 goto end;
2483 }
2484
96518a56
MD
2485 tracker_id.type = LTTNG_ID_VALUE;
2486 tracker_id.value = (int) id;
2487 ret = lttng_track_id(handle, tracker_type, &tracker_id);
2488 if (ret) {
2489 goto end;
2490 }
2491 }
2492 if (element_name && !strcmp((const char *) node->name,
2493 element_name)) {
2494 xmlChar *content = NULL;
2495 struct lttng_tracker_id tracker_id;
2496
2497 content = xmlNodeGetContent(node);
2498 if (!content) {
2499 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2500 goto end;
2501 }
2502 tracker_id.type = LTTNG_ID_STRING;
2503 tracker_id.string = (char *) content;
2504 ret = lttng_track_id(handle, tracker_type, &tracker_id);
2505 free(content);
847a5916
JR
2506 if (ret) {
2507 goto end;
2508 }
2509 }
2510 }
96518a56 2511 node = id_target_node;
847a5916
JR
2512 }
2513
2514end:
2515 return ret;
2516}
2517
dcf266c0
JG
2518static
2519int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2520{
2521 int ret;
2522 struct lttng_domain domain = { 0 };
2523 struct lttng_handle *handle = NULL;
4fc2b126 2524 struct lttng_channel *channel = NULL;
dcf266c0 2525 xmlNodePtr channels_node = NULL;
847a5916
JR
2526 xmlNodePtr trackers_node = NULL;
2527 xmlNodePtr pid_tracker_node = NULL;
96518a56
MD
2528 xmlNodePtr vpid_tracker_node = NULL;
2529 xmlNodePtr uid_tracker_node = NULL;
2530 xmlNodePtr vuid_tracker_node = NULL;
2531 xmlNodePtr gid_tracker_node = NULL;
2532 xmlNodePtr vgid_tracker_node = NULL;
dcf266c0
JG
2533 xmlNodePtr node;
2534
2535 assert(session_name);
2536
2537 ret = init_domain(domain_node, &domain);
2538 if (ret) {
2539 goto end;
2540 }
2541
2542 handle = lttng_create_handle(session_name, &domain);
2543 if (!handle) {
2544 ret = -LTTNG_ERR_NOMEM;
2545 goto end;
2546 }
2547
2548 /* get the channels node */
2549 for (node = xmlFirstElementChild(domain_node); node;
2550 node = xmlNextElementSibling(node)) {
2551 if (!strcmp((const char *) node->name,
2552 config_element_channels)) {
2553 channels_node = node;
2554 break;
2555 }
2556 }
2557
2558 if (!channels_node) {
2559 goto end;
2560 }
2561
2562 /* create all channels */
2563 for (node = xmlFirstElementChild(channels_node); node;
2564 node = xmlNextElementSibling(node)) {
36d1687c 2565 const enum lttng_domain_type original_domain = domain.type;
dcf266c0
JG
2566 xmlNodePtr contexts_node = NULL;
2567 xmlNodePtr events_node = NULL;
2568 xmlNodePtr channel_attr_node;
2569
36d1687c
JG
2570 /*
2571 * Channels of the "agent" types cannot be created directly.
2572 * They are meant to be created implicitly through the
2573 * activation of events in their domain. However, a user
2574 * can override the default channel configuration attributes
2575 * by creating the underlying UST channel _before_ enabling
2576 * an agent domain event.
2577 *
2578 * Hence, the channel's type is substituted before the creation
2579 * and restored by the time the events are created.
2580 */
2581 switch (domain.type) {
2582 case LTTNG_DOMAIN_JUL:
2583 case LTTNG_DOMAIN_LOG4J:
2584 case LTTNG_DOMAIN_PYTHON:
2585 domain.type = LTTNG_DOMAIN_UST;
2586 default:
2587 break;
2588 }
2589
4fc2b126
JR
2590 channel = lttng_channel_create(&domain);
2591 if (!channel) {
2592 ret = -1;
2593 goto end;
2594 }
dcf266c0
JG
2595
2596 for (channel_attr_node = xmlFirstElementChild(node);
2597 channel_attr_node; channel_attr_node =
2598 xmlNextElementSibling(channel_attr_node)) {
2599 ret = process_channel_attr_node(channel_attr_node,
4fc2b126 2600 channel, &contexts_node, &events_node);
dcf266c0
JG
2601 if (ret) {
2602 goto end;
2603 }
2604 }
2605
4fc2b126 2606 ret = lttng_enable_channel(handle, channel);
dcf266c0
JG
2607 if (ret < 0) {
2608 goto end;
2609 }
2610
36d1687c
JG
2611 /* Restore the original channel domain. */
2612 domain.type = original_domain;
2613
4fc2b126 2614 ret = process_events_node(events_node, handle, channel->name);
dcf266c0
JG
2615 if (ret) {
2616 goto end;
2617 }
2618
2619 ret = process_contexts_node(contexts_node, handle,
4fc2b126 2620 channel->name);
dcf266c0
JG
2621 if (ret) {
2622 goto end;
2623 }
4fc2b126
JR
2624
2625 lttng_channel_destroy(channel);
dcf266c0 2626 }
4fc2b126 2627 channel = NULL;
847a5916
JR
2628
2629 /* get the trackers node */
2630 for (node = xmlFirstElementChild(domain_node); node;
2631 node = xmlNextElementSibling(node)) {
2632 if (!strcmp((const char *) node->name,
2633 config_element_trackers)) {
2634 trackers_node = node;
2635 break;
2636 }
2637 }
2638
2639 if (!trackers_node) {
2640 goto end;
2641 }
2642
2643 for (node = xmlFirstElementChild(trackers_node); node;
2644 node = xmlNextElementSibling(node)) {
96518a56 2645 if (!strcmp((const char *)node->name, config_element_pid_tracker)) {
847a5916 2646 pid_tracker_node = node;
96518a56
MD
2647 ret = process_id_tracker_node(pid_tracker_node, handle,
2648 LTTNG_TRACKER_PID);
2649 if (ret) {
2650 goto end;
2651 }
2652 }
2653 if (!strcmp((const char *)node->name, config_element_vpid_tracker)) {
2654 vpid_tracker_node = node;
2655 ret = process_id_tracker_node(vpid_tracker_node, handle,
2656 LTTNG_TRACKER_VPID);
2657 if (ret) {
2658 goto end;
2659 }
2660 }
2661 if (!strcmp((const char *)node->name, config_element_uid_tracker)) {
2662 uid_tracker_node = node;
2663 ret = process_id_tracker_node(uid_tracker_node, handle,
2664 LTTNG_TRACKER_UID);
2665 if (ret) {
2666 goto end;
2667 }
2668 }
2669 if (!strcmp((const char *)node->name, config_element_vuid_tracker)) {
2670 vuid_tracker_node = node;
2671 ret = process_id_tracker_node(vuid_tracker_node, handle,
2672 LTTNG_TRACKER_VUID);
2673 if (ret) {
2674 goto end;
2675 }
2676 }
2677 if (!strcmp((const char *)node->name, config_element_gid_tracker)) {
2678 gid_tracker_node = node;
2679 ret = process_id_tracker_node(gid_tracker_node, handle,
2680 LTTNG_TRACKER_GID);
2681 if (ret) {
2682 goto end;
2683 }
2684 }
2685 if (!strcmp((const char *)node->name, config_element_vgid_tracker)) {
2686 vgid_tracker_node = node;
2687 ret = process_id_tracker_node(vgid_tracker_node, handle,
2688 LTTNG_TRACKER_VGID);
847a5916
JR
2689 if (ret) {
2690 goto end;
2691 }
2692 }
847a5916
JR
2693 }
2694
dcf266c0 2695end:
4fc2b126 2696 lttng_channel_destroy(channel);
dcf266c0
JG
2697 lttng_destroy_handle(handle);
2698 return ret;
2699}
2700
2701static
2702int process_session_node(xmlNodePtr session_node, const char *session_name,
1b08cbce
JR
2703 int overwrite,
2704 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
2705{
2706 int ret, started = -1, snapshot_mode = -1;
259c2674 2707 uint64_t live_timer_interval = UINT64_MAX,
90936dcf
JD
2708 rotation_timer_interval = 0,
2709 rotation_size = 0;
d324faf7 2710 xmlChar *name = NULL;
9e7c9f56 2711 xmlChar *shm_path = NULL;
dcf266c0
JG
2712 xmlNodePtr domains_node = NULL;
2713 xmlNodePtr output_node = NULL;
2714 xmlNodePtr node;
90936dcf 2715 xmlNodePtr attributes_child;
dcf266c0
JG
2716 struct lttng_domain *kernel_domain = NULL;
2717 struct lttng_domain *ust_domain = NULL;
2718 struct lttng_domain *jul_domain = NULL;
5cdb6027 2719 struct lttng_domain *log4j_domain = NULL;
0e115563 2720 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
2721
2722 for (node = xmlFirstElementChild(session_node); node;
2723 node = xmlNextElementSibling(node)) {
2724 if (!name && !strcmp((const char *) node->name,
2725 config_element_name)) {
2726 /* name */
2727 xmlChar *node_content = xmlNodeGetContent(node);
2728 if (!node_content) {
2729 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2730 goto error;
dcf266c0
JG
2731 }
2732
d324faf7 2733 name = node_content;
dcf266c0
JG
2734 } else if (!domains_node && !strcmp((const char *) node->name,
2735 config_element_domains)) {
2736 /* domains */
2737 domains_node = node;
2738 } else if (started == -1 && !strcmp((const char *) node->name,
2739 config_element_started)) {
2740 /* started */
2741 xmlChar *node_content = xmlNodeGetContent(node);
2742 if (!node_content) {
2743 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2744 goto error;
dcf266c0
JG
2745 }
2746
2747 ret = parse_bool(node_content, &started);
2748 free(node_content);
2749 if (ret) {
2750 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2751 goto error;
dcf266c0
JG
2752 }
2753 } else if (!output_node && !strcmp((const char *) node->name,
2754 config_element_output)) {
2755 /* output */
2756 output_node = node;
9e7c9f56
JR
2757 } else if (!shm_path && !strcmp((const char *) node->name,
2758 config_element_shared_memory_path)) {
2759 /* shared memory path */
2760 xmlChar *node_content = xmlNodeGetContent(node);
2761 if (!node_content) {
2762 ret = -LTTNG_ERR_NOMEM;
2763 goto error;
2764 }
2765
2766 shm_path = node_content;
dcf266c0 2767 } else {
259c2674
JD
2768 /*
2769 * attributes, snapshot_mode, live_timer_interval, rotation_size,
90936dcf
JD
2770 * rotation_timer_interval.
2771 */
2772 for (attributes_child = xmlFirstElementChild(node); attributes_child;
2773 attributes_child = xmlNextElementSibling(attributes_child)) {
2774 if (!strcmp((const char *) attributes_child->name,
2775 config_element_snapshot_mode)) {
2776 /* snapshot_mode */
2777 xmlChar *snapshot_mode_content =
2778 xmlNodeGetContent(attributes_child);
2779 if (!snapshot_mode_content) {
2780 ret = -LTTNG_ERR_NOMEM;
2781 goto error;
2782 }
dcf266c0 2783
90936dcf
JD
2784 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2785 free(snapshot_mode_content);
2786 if (ret) {
2787 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2788 goto error;
2789 }
2790 } else if (!strcmp((const char *) attributes_child->name,
2791 config_element_live_timer_interval)) {
2792 /* live_timer_interval */
2793 xmlChar *timer_interval_content =
2794 xmlNodeGetContent(attributes_child);
2795 if (!timer_interval_content) {
2796 ret = -LTTNG_ERR_NOMEM;
2797 goto error;
2798 }
dcf266c0 2799
90936dcf
JD
2800 ret = parse_uint(timer_interval_content, &live_timer_interval);
2801 free(timer_interval_content);
2802 if (ret) {
2803 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2804 goto error;
2805 }
dcf266c0 2806 }
90936dcf
JD
2807 if (!strcmp((const char *) attributes_child->name,
2808 config_element_rotation_timer_interval)) {
2809 /* rotation_timer_interval */
2810 xmlChar *timer_interval_content =
2811 xmlNodeGetContent(attributes_child);
2812 if (!timer_interval_content) {
2813 ret = -LTTNG_ERR_NOMEM;
2814 goto error;
2815 }
2816
2817 ret = parse_uint(timer_interval_content, &rotation_timer_interval);
2818 free(timer_interval_content);
2819 if (ret) {
2820 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2821 goto error;
2822 }
259c2674 2823 }
90936dcf
JD
2824 if (!strcmp((const char *) attributes_child->name,
2825 config_element_rotation_size)) {
2826 /* rotation_size */
2827 xmlChar *rotation_size_content =
2828 xmlNodeGetContent(attributes_child);
2829 if (!rotation_size_content) {
2830 ret = -LTTNG_ERR_NOMEM;
2831 goto error;
2832 }
259c2674 2833
90936dcf
JD
2834 ret = parse_uint(rotation_size_content, &rotation_size);
2835 free(rotation_size_content);
2836 if (ret) {
2837 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2838 goto error;
2839 }
259c2674
JD
2840 }
2841 }
dcf266c0
JG
2842 }
2843 }
2844
2845 if (!name) {
2846 /* Mandatory attribute, as defined in the session XSD */
2847 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2848 goto error;
dcf266c0
JG
2849 }
2850
d324faf7 2851 if (session_name && strcmp((char *) name, session_name)) {
dcf266c0 2852 /* This is not the session we are looking for */
c2da8cde
DG
2853 ret = -LTTNG_ERR_NO_SESSION;
2854 goto error;
dcf266c0
JG
2855 }
2856
2857 /* Init domains to create the session handles */
2858 for (node = xmlFirstElementChild(domains_node); node;
2859 node = xmlNextElementSibling(node)) {
2860 struct lttng_domain *domain;
2861
2862 domain = zmalloc(sizeof(*domain));
2863 if (!domain) {
2864 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2865 goto error;
dcf266c0
JG
2866 }
2867
2868 ret = init_domain(node, domain);
2869 if (ret) {
2870 goto domain_init_error;
2871 }
2872
2873 switch (domain->type) {
2874 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
2875 if (kernel_domain) {
2876 /* Same domain seen twice, invalid! */
2877 goto domain_init_error;
2878 }
dcf266c0
JG
2879 kernel_domain = domain;
2880 break;
2881 case LTTNG_DOMAIN_UST:
c33e6729
DG
2882 if (ust_domain) {
2883 /* Same domain seen twice, invalid! */
2884 goto domain_init_error;
2885 }
dcf266c0
JG
2886 ust_domain = domain;
2887 break;
2888 case LTTNG_DOMAIN_JUL:
c33e6729
DG
2889 if (jul_domain) {
2890 /* Same domain seen twice, invalid! */
2891 goto domain_init_error;
2892 }
dcf266c0
JG
2893 jul_domain = domain;
2894 break;
5cdb6027
DG
2895 case LTTNG_DOMAIN_LOG4J:
2896 if (log4j_domain) {
2897 /* Same domain seen twice, invalid! */
2898 goto domain_init_error;
2899 }
2900 log4j_domain = domain;
2901 break;
0e115563
DG
2902 case LTTNG_DOMAIN_PYTHON:
2903 if (python_domain) {
2904 /* Same domain seen twice, invalid! */
2905 goto domain_init_error;
2906 }
2907 python_domain = domain;
2908 break;
dcf266c0
JG
2909 default:
2910 WARN("Invalid domain type");
2911 goto domain_init_error;
2912 }
2913 continue;
2914domain_init_error:
2915 free(domain);
2916 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2917 goto error;
dcf266c0
JG
2918 }
2919
2aaf5fc7
JR
2920 /* Apply overrides */
2921 if (overrides) {
2922 if (overrides->session_name) {
2923 xmlChar *name_override = xmlStrdup(BAD_CAST(overrides->session_name));
2924 if (!name_override) {
2925 ret = -LTTNG_ERR_NOMEM;
2926 goto error;
2927 }
2928
2929 /* Overrides the session name to the provided name */
2930 xmlFree(name);
2931 name = name_override;
2932 }
2933 }
2934
40b4155f 2935 if (overwrite) {
dcf266c0 2936 /* Destroy session if it exists */
d324faf7 2937 ret = lttng_destroy_session((const char *) name);
dcf266c0
JG
2938 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2939 ERR("Failed to destroy existing session.");
c2da8cde 2940 goto error;
dcf266c0
JG
2941 }
2942 }
2943
2944 /* Create session type depending on output type */
2945 if (snapshot_mode && snapshot_mode != -1) {
1b08cbce
JR
2946 ret = create_snapshot_session((const char *) name, output_node,
2947 overrides);
dcf266c0
JG
2948 } else if (live_timer_interval &&
2949 live_timer_interval != UINT64_MAX) {
194dfca0 2950 ret = create_session((const char *) name,
1b08cbce 2951 output_node, live_timer_interval, overrides);
dcf266c0
JG
2952 } else {
2953 /* regular session */
194dfca0 2954 ret = create_session((const char *) name,
1b08cbce 2955 output_node, UINT64_MAX, overrides);
dcf266c0 2956 }
dcf266c0 2957 if (ret) {
c2da8cde 2958 goto error;
dcf266c0
JG
2959 }
2960
9e7c9f56
JR
2961 if (shm_path) {
2962 ret = lttng_set_session_shm_path((const char *) name,
d324faf7 2963 (const char *) shm_path);
9e7c9f56
JR
2964 if (ret) {
2965 goto error;
2966 }
2967 }
2968
dcf266c0
JG
2969 for (node = xmlFirstElementChild(domains_node); node;
2970 node = xmlNextElementSibling(node)) {
d324faf7 2971 ret = process_domain_node(node, (const char *) name);
dcf266c0
JG
2972 if (ret) {
2973 goto end;
2974 }
2975 }
2976
90936dcf 2977 if (rotation_timer_interval || rotation_size) {
259c2674
JD
2978 struct lttng_rotation_schedule_attr *rotation_attr = lttng_rotation_schedule_attr_create();
2979
2980 if (!rotation_attr) {
2981 goto error;
2982 }
2983 ret = lttng_rotation_schedule_attr_set_session_name(rotation_attr, (const char *) name);
2984 if (ret) {
2985 lttng_rotation_schedule_attr_destroy(rotation_attr);
2986 goto error;
2987 }
2988 lttng_rotation_schedule_attr_set_timer_period(rotation_attr,
2989 rotation_timer_interval);
90936dcf 2990 lttng_rotation_schedule_attr_set_size(rotation_attr, rotation_size);
259c2674
JD
2991 ret = lttng_rotation_set_schedule(rotation_attr);
2992 lttng_rotation_schedule_attr_destroy(rotation_attr);
2993 if (ret) {
2994 goto error;
2995 }
2996 }
2997
dcf266c0 2998 if (started) {
d324faf7 2999 ret = lttng_start_tracing((const char *) name);
dcf266c0
JG
3000 if (ret) {
3001 goto end;
3002 }
3003 }
c2da8cde 3004
dcf266c0 3005end:
b2579dc1 3006 if (ret < 0) {
d324faf7
JG
3007 ERR("Failed to load session %s: %s", (const char *) name,
3008 lttng_strerror(ret));
3009 lttng_destroy_session((const char *) name);
b2579dc1
JG
3010 }
3011
c2da8cde 3012error:
dcf266c0
JG
3013 free(kernel_domain);
3014 free(ust_domain);
3015 free(jul_domain);
5cdb6027 3016 free(log4j_domain);
135a3893 3017 free(python_domain);
d324faf7 3018 xmlFree(name);
9e7c9f56 3019 xmlFree(shm_path);
dcf266c0
JG
3020 return ret;
3021}
3022
cf53c06d
DG
3023/*
3024 * Return 1 if the given path is readable by the current UID or 0 if not.
3025 * Return -1 if the path is EPERM.
3026 */
3027static int validate_file_read_creds(const char *path)
3028{
3029 int ret;
3030
3031 assert(path);
3032
3033 /* Can we read the file. */
3034 ret = access(path, R_OK);
3035 if (!ret) {
3036 goto valid;
3037 }
3038 if (errno == EACCES) {
3039 return -1;
3040 } else {
3041 /* Invalid. */
3042 return 0;
3043 }
3044valid:
3045 return 1;
3046}
3047
dcf266c0
JG
3048static
3049int load_session_from_file(const char *path, const char *session_name,
1b08cbce
JR
3050 struct session_config_validation_ctx *validation_ctx, int overwrite,
3051 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3052{
3053 int ret, session_found = !session_name;
3054 xmlDocPtr doc = NULL;
3055 xmlNodePtr sessions_node;
3056 xmlNodePtr session_node;
dcf266c0
JG
3057
3058 assert(path);
3059 assert(validation_ctx);
3060
cf53c06d
DG
3061 ret = validate_file_read_creds(path);
3062 if (ret != 1) {
3063 if (ret == -1) {
3064 ret = -LTTNG_ERR_EPERM;
3065 } else {
3066 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
3067 }
dcf266c0
JG
3068 goto end;
3069 }
3070
3071 doc = xmlParseFile(path);
3072 if (!doc) {
3073 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3074 goto end;
3075 }
3076
3077 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
3078 if (ret) {
3079 ERR("Session configuration file validation failed");
3080 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3081 goto end;
3082 }
3083
3084 sessions_node = xmlDocGetRootElement(doc);
3085 if (!sessions_node) {
96518a56 3086 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
dcf266c0
JG
3087 goto end;
3088 }
3089
3090 for (session_node = xmlFirstElementChild(sessions_node);
3091 session_node; session_node =
3092 xmlNextElementSibling(session_node)) {
3093 ret = process_session_node(session_node,
1b08cbce 3094 session_name, overwrite, overrides);
dcf266c0
JG
3095 if (session_name && ret == 0) {
3096 /* Target session found and loaded */
3097 session_found = 1;
3098 break;
3099 }
3100 }
3101end:
3102 xmlFreeDoc(doc);
3103 if (!ret) {
a96bc65d 3104 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0 3105 }
96518a56
MD
3106 if (ret == -LTTNG_ERR_NO_SESSION) {
3107 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
3108 }
dcf266c0
JG
3109 return ret;
3110}
3111
3112static
3113int load_session_from_path(const char *path, const char *session_name,
1b08cbce
JR
3114 struct session_config_validation_ctx *validation_ctx, int overwrite,
3115 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3116{
3117 int ret, session_found = !session_name;
dcf266c0 3118 DIR *directory = NULL;
cce35f91
JG
3119 struct lttng_dynamic_buffer file_path;
3120 size_t path_len;
dcf266c0
JG
3121
3122 assert(path);
3123 assert(validation_ctx);
cce35f91
JG
3124 path_len = strlen(path);
3125 lttng_dynamic_buffer_init(&file_path);
3126 if (path_len >= LTTNG_PATH_MAX) {
3127 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3128 path, path_len, LTTNG_PATH_MAX);
3129 ret = -LTTNG_ERR_INVALID;
3130 goto end;
3131 }
dcf266c0 3132
4af16958
DG
3133 directory = opendir(path);
3134 if (!directory) {
11143783
DG
3135 switch (errno) {
3136 case ENOTDIR:
0f0a81b5
DG
3137 /* Try the file loading. */
3138 break;
11143783
DG
3139 case ENOENT:
3140 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
3141 goto end;
3142 default:
0f0a81b5
DG
3143 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3144 goto end;
4af16958 3145 }
dcf266c0 3146 }
4af16958 3147 if (directory) {
cce35f91 3148 size_t file_path_root_len;
dcf266c0 3149
cce35f91
JG
3150 ret = lttng_dynamic_buffer_set_capacity(&file_path,
3151 LTTNG_PATH_MAX);
3152 if (ret) {
3153 ret = -LTTNG_ERR_NOMEM;
dcf266c0
JG
3154 goto end;
3155 }
3156
cce35f91
JG
3157 ret = lttng_dynamic_buffer_append(&file_path, path, path_len);
3158 if (ret) {
dcf266c0 3159 ret = -LTTNG_ERR_NOMEM;
dcf266c0
JG
3160 goto end;
3161 }
3162
cce35f91
JG
3163 if (file_path.data[file_path.size - 1] != '/') {
3164 ret = lttng_dynamic_buffer_append(&file_path, "/", 1);
3165 if (ret) {
3166 ret = -LTTNG_ERR_NOMEM;
3167 goto end;
3168 }
dcf266c0 3169 }
cce35f91 3170 file_path_root_len = file_path.size;
dcf266c0
JG
3171
3172 /* Search for *.lttng files */
9a2df626
MJ
3173 for (;;) {
3174 size_t file_name_len;
3175 struct dirent *result;
3176
3177 /*
3178 * When the end of the directory stream is reached, NULL
3179 * is returned and errno is kept unchanged. When an
3180 * error occurs, NULL is returned and errno is set
3181 * accordingly. To distinguish between the two, set
3182 * errno to zero before calling readdir().
3183 *
3184 * On success, readdir() returns a pointer to a dirent
3185 * structure. This structure may be statically
3186 * allocated, do not attempt to free(3) it.
3187 */
3188 errno = 0;
3189 result = readdir(directory);
3190
ff86d8d0 3191 /* Reached end of dir stream or error out. */
9a2df626
MJ
3192 if (!result) {
3193 if (errno) {
3194 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path);
3195 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3196 goto end;
3197 }
3198 break;
3199 }
3200
3201 file_name_len = strlen(result->d_name);
dcf266c0
JG
3202
3203 if (file_name_len <=
3204 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
3205 continue;
3206 }
3207
cce35f91
JG
3208 if (file_path.size + file_name_len >= LTTNG_PATH_MAX) {
3209 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3210 result->d_name,
3211 /* +1 to account for NULL terminator. */
3212 file_path.size + file_name_len + 1,
3213 LTTNG_PATH_MAX);
dcf266c0
JG
3214 continue;
3215 }
3216
cce35f91 3217 /* Does the file end with .lttng? */
dcf266c0 3218 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
cce35f91
JG
3219 result->d_name + file_name_len - sizeof(
3220 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
dcf266c0
JG
3221 continue;
3222 }
3223
cce35f91
JG
3224 ret = lttng_dynamic_buffer_append(&file_path, result->d_name,
3225 file_name_len + 1);
3226 if (ret) {
3227 ret = -LTTNG_ERR_NOMEM;
3228 goto end;
3229 }
dcf266c0 3230
cce35f91 3231 ret = load_session_from_file(file_path.data, session_name,
1b08cbce 3232 validation_ctx, overwrite, overrides);
96518a56 3233 if (session_name && (!ret || ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
dcf266c0
JG
3234 session_found = 1;
3235 break;
3236 }
96518a56
MD
3237 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3238 goto end;
3239 }
cce35f91
JG
3240 /*
3241 * Reset the buffer's size to the location of the
3242 * path's trailing '/'.
3243 */
3244 ret = lttng_dynamic_buffer_set_size(&file_path,
3245 file_path_root_len);
3246 if (ret) {
3247 ret = -LTTNG_ERR_UNK;
3248 goto end;
3249 }
dcf266c0 3250 }
dcf266c0
JG
3251 } else {
3252 ret = load_session_from_file(path, session_name,
1b08cbce 3253 validation_ctx, overwrite, overrides);
dcf266c0
JG
3254 if (ret) {
3255 goto end;
dcf266c0 3256 }
96518a56 3257 session_found = 1;
dcf266c0
JG
3258 }
3259
96518a56 3260 ret = 0;
dcf266c0
JG
3261end:
3262 if (directory) {
3263 if (closedir(directory)) {
3264 PERROR("closedir");
3265 }
3266 }
96518a56
MD
3267 if (!ret && !session_found) {
3268 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0 3269 }
cce35f91 3270 lttng_dynamic_buffer_reset(&file_path);
dcf266c0
JG
3271 return ret;
3272}
3273
ab38c13f
DG
3274/*
3275 * Validate that the given path's credentials and the current process have the
cf53c06d 3276 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
3277 */
3278static int validate_path_creds(const char *path)
3279{
3280 int ret, uid = getuid();
3281 struct stat buf;
3282
3283 assert(path);
3284
cf53c06d 3285 if (uid == 0) {
ab38c13f
DG
3286 goto valid;
3287 }
3288
3289 ret = stat(path, &buf);
3290 if (ret < 0) {
3291 if (errno != ENOENT) {
3292 PERROR("stat");
3293 }
ab38c13f
DG
3294 goto valid;
3295 }
3296
3297 if (buf.st_uid != uid) {
3298 goto invalid;
3299 }
3300
3301valid:
ab38c13f 3302 return 1;
cf53c06d
DG
3303invalid:
3304 return 0;
ab38c13f
DG
3305}
3306
dcf266c0
JG
3307LTTNG_HIDDEN
3308int config_load_session(const char *path, const char *session_name,
1b08cbce
JR
3309 int overwrite, unsigned int autoload,
3310 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3311{
3312 int ret;
6c66fa0f 3313 bool session_loaded = false;
cf53c06d 3314 const char *path_ptr = NULL;
dcf266c0
JG
3315 struct session_config_validation_ctx validation_ctx = { 0 };
3316
3317 ret = init_session_config_validation_ctx(&validation_ctx);
3318 if (ret) {
3319 goto end;
3320 }
3321
3322 if (!path) {
ab38c13f
DG
3323 char *home_path;
3324 const char *sys_path;
3325
dcf266c0 3326 /* Try home path */
ab38c13f 3327 home_path = utils_get_home_dir();
dcf266c0 3328 if (home_path) {
ab38c13f 3329 char path[PATH_MAX];
dcf266c0 3330
d4fcf703
DG
3331 /*
3332 * Try user session configuration path. Ignore error here so we can
3333 * continue loading the system wide sessions.
3334 */
ab38c13f
DG
3335 if (autoload) {
3336 ret = snprintf(path, sizeof(path),
3337 DEFAULT_SESSION_HOME_CONFIGPATH "/"
3338 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
3339 if (ret < 0) {
3340 PERROR("snprintf session autoload home config path");
96518a56 3341 ret = -LTTNG_ERR_INVALID;
cf53c06d
DG
3342 goto end;
3343 }
3344
3345 /*
3346 * Credentials are only validated for the autoload in order to
3347 * avoid any user session daemon to try to load kernel sessions
3348 * automatically and failing all the times.
3349 */
3350 ret = validate_path_creds(path);
3351 if (ret) {
3352 path_ptr = path;
3353 }
ab38c13f
DG
3354 } else {
3355 ret = snprintf(path, sizeof(path),
3356 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
3357 if (ret < 0) {
3358 PERROR("snprintf session home config path");
96518a56 3359 ret = -LTTNG_ERR_INVALID;
cf53c06d
DG
3360 goto end;
3361 }
3362 path_ptr = path;
ab38c13f 3363 }
cf53c06d
DG
3364 if (path_ptr) {
3365 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 3366 &validation_ctx, overwrite, overrides);
d4fcf703
DG
3367 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3368 goto end;
3369 }
3370 /*
3371 * Continue even if the session was found since we have to try
3372 * the system wide sessions.
3373 */
6c66fa0f 3374 session_loaded = true;
ab38c13f 3375 }
d4fcf703 3376 }
ab38c13f 3377
cf53c06d
DG
3378 /* Reset path pointer for the system wide dir. */
3379 path_ptr = NULL;
3380
d4fcf703
DG
3381 /* Try system wide configuration directory. */
3382 if (autoload) {
3383 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
3384 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
3385 ret = validate_path_creds(sys_path);
3386 if (ret) {
3387 path_ptr = sys_path;
3388 }
d4fcf703 3389 } else {
cf53c06d
DG
3390 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
3391 path_ptr = sys_path;
d4fcf703
DG
3392 }
3393
cf53c06d
DG
3394 if (path_ptr) {
3395 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 3396 &validation_ctx, overwrite, overrides);
6c66fa0f
JG
3397 if (!ret) {
3398 session_loaded = true;
3399 }
96518a56
MD
3400 } else {
3401 ret = 0;
dcf266c0
JG
3402 }
3403 } else {
3404 ret = access(path, F_OK);
3405 if (ret < 0) {
3406 PERROR("access");
3407 switch (errno) {
3408 case ENOENT:
3409 ret = -LTTNG_ERR_INVALID;
3410 WARN("Session configuration path does not exist.");
3411 break;
3412 case EACCES:
3413 ret = -LTTNG_ERR_EPERM;
3414 break;
3415 default:
3416 ret = -LTTNG_ERR_UNK;
3417 break;
3418 }
3419 goto end;
3420 }
3421
3422 ret = load_session_from_path(path, session_name,
1b08cbce 3423 &validation_ctx, overwrite, overrides);
dcf266c0
JG
3424 }
3425end:
3426 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
3427 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
3428 /*
3429 * Don't report an error if no sessions are found when called
3430 * without a session_name or a search path.
3431 */
3432 ret = 0;
3433 }
6c66fa0f
JG
3434
3435 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
3436 /* A matching session was found in one of the search paths. */
3437 ret = 0;
3438 }
dcf266c0
JG
3439 return ret;
3440}
56766c75
JG
3441
3442static
3443void __attribute__((destructor)) session_config_exit(void)
3444{
3445 xmlCleanupParser();
3446}
This page took 0.208357 seconds and 5 git commands to generate.