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