Disable blank element parsing on get_document
[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>
e8fa9fb0 35#include <common/compat/getenv.h>
fb198a11
JG
36#include <lttng/lttng-error.h>
37#include <libxml/parser.h>
38#include <libxml/valid.h>
39#include <libxml/xmlschemas.h>
dcf266c0 40#include <libxml/tree.h>
fa8b52a2 41#include <libxml/xpath.h>
dcf266c0
JG
42#include <lttng/lttng.h>
43#include <lttng/snapshot.h>
1501a7f3 44
f40ef1d5 45#include "session-config.h"
36f2332b 46#include "config-internal.h"
1501a7f3
JG
47
48struct handler_filter_args {
49 const char* section;
50 config_entry_handler_cb handler;
51 void *user_data;
52};
53
dcf266c0
JG
54struct session_config_validation_ctx {
55 xmlSchemaParserCtxtPtr parser_ctx;
56 xmlSchemaPtr schema;
57 xmlSchemaValidCtxtPtr schema_validation_ctx;
58};
59
1501a7f3
JG
60const char * const config_str_yes = "yes";
61const char * const config_str_true = "true";
62const char * const config_str_on = "on";
63const char * const config_str_no = "no";
64const char * const config_str_false = "false";
65const char * const config_str_off = "off";
36f2332b 66const char * const config_xml_encoding = "UTF-8";
fb198a11 67const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */
36f2332b
JG
68const char * const config_xml_indent_string = "\t";
69const char * const config_xml_true = "true";
70const char * const config_xml_false = "false";
1501a7f3 71
fb198a11
JG
72const char * const config_element_channel = "channel";
73const char * const config_element_channels = "channels";
74const char * const config_element_domain = "domain";
75const char * const config_element_domains = "domains";
76const char * const config_element_event = "event";
77const char * const config_element_events = "events";
78const char * const config_element_context = "context";
79const char * const config_element_contexts = "contexts";
80const char * const config_element_attributes = "attributes";
81const char * const config_element_exclusion = "exclusion";
82const char * const config_element_exclusions = "exclusions";
83const char * const config_element_function_attributes = "function_attributes";
84const char * const config_element_probe_attributes = "probe_attributes";
85const char * const config_element_symbol_name = "symbol_name";
86const char * const config_element_address = "address";
87const char * const config_element_offset = "offset";
88const char * const config_element_name = "name";
89const char * const config_element_enabled = "enabled";
90const char * const config_element_overwrite_mode = "overwrite_mode";
91const char * const config_element_subbuf_size = "subbuffer_size";
92const char * const config_element_num_subbuf = "subbuffer_count";
93const char * const config_element_switch_timer_interval = "switch_timer_interval";
94const char * const config_element_read_timer_interval = "read_timer_interval";
95const char * const config_element_output = "output";
96const char * const config_element_output_type = "output_type";
97const char * const config_element_tracefile_size = "tracefile_size";
98const char * const config_element_tracefile_count = "tracefile_count";
99const char * const config_element_live_timer_interval = "live_timer_interval";
ec2ed7da
JG
100LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events";
101LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets";
fb198a11
JG
102const char * const config_element_type = "type";
103const char * const config_element_buffer_type = "buffer_type";
104const char * const config_element_session = "session";
105const char * const config_element_sessions = "sessions";
ec2ed7da
JG
106LTTNG_HIDDEN const char * const config_element_context_perf = "perf";
107LTTNG_HIDDEN const char * const config_element_context_app = "app";
108LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name";
109LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name";
fb198a11
JG
110const char * const config_element_config = "config";
111const char * const config_element_started = "started";
112const char * const config_element_snapshot_mode = "snapshot_mode";
113const char * const config_element_loglevel = "loglevel";
114const char * const config_element_loglevel_type = "loglevel_type";
115const char * const config_element_filter = "filter";
ec2ed7da 116LTTNG_HIDDEN const char * const config_element_filter_expression = "filter_expression";
fb198a11
JG
117const char * const config_element_snapshot_outputs = "snapshot_outputs";
118const char * const config_element_consumer_output = "consumer_output";
119const char * const config_element_destination = "destination";
120const char * const config_element_path = "path";
121const char * const config_element_net_output = "net_output";
122const char * const config_element_control_uri = "control_uri";
123const char * const config_element_data_uri = "data_uri";
124const char * const config_element_max_size = "max_size";
ccf10263
MD
125const char * const config_element_pid = "pid";
126const char * const config_element_pids = "pids";
9e7c9f56 127const char * const config_element_shared_memory_path = "shared_memory_path";
ebbf5ab7
JR
128const char * const config_element_pid_tracker = "pid_tracker";
129const char * const config_element_trackers = "trackers";
847a5916
JR
130const char * const config_element_targets = "targets";
131const char * const config_element_target_pid = "pid_target";
7f66d5e7
JG
132const char * const config_element_omit_name = "omit_name";
133const char * const config_element_omit_output = "omit_output";
fb198a11
JG
134
135const char * const config_domain_type_kernel = "KERNEL";
136const char * const config_domain_type_ust = "UST";
137const char * const config_domain_type_jul = "JUL";
5cdb6027 138const char * const config_domain_type_log4j = "LOG4J";
0e115563 139const char * const config_domain_type_python = "PYTHON";
fb198a11
JG
140
141const char * const config_buffer_type_per_pid = "PER_PID";
142const char * const config_buffer_type_per_uid = "PER_UID";
143const char * const config_buffer_type_global = "GLOBAL";
144
145const char * const config_overwrite_mode_discard = "DISCARD";
146const char * const config_overwrite_mode_overwrite = "OVERWRITE";
147
148const char * const config_output_type_splice = "SPLICE";
149const char * const config_output_type_mmap = "MMAP";
150
151const char * const config_loglevel_type_all = "ALL";
152const char * const config_loglevel_type_range = "RANGE";
153const char * const config_loglevel_type_single = "SINGLE";
154
155const char * const config_event_type_all = "ALL";
156const char * const config_event_type_tracepoint = "TRACEPOINT";
157const char * const config_event_type_probe = "PROBE";
158const char * const config_event_type_function = "FUNCTION";
159const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
160const char * const config_event_type_noop = "NOOP";
161const char * const config_event_type_syscall = "SYSCALL";
162const char * const config_event_type_kprobe = "KPROBE";
163const char * const config_event_type_kretprobe = "KRETPROBE";
164
165const char * const config_event_context_pid = "PID";
166const char * const config_event_context_procname = "PROCNAME";
167const char * const config_event_context_prio = "PRIO";
168const char * const config_event_context_nice = "NICE";
169const char * const config_event_context_vpid = "VPID";
170const char * const config_event_context_tid = "TID";
171const char * const config_event_context_vtid = "VTID";
172const char * const config_event_context_ppid = "PPID";
173const char * const config_event_context_vppid = "VPPID";
174const char * const config_event_context_pthread_id = "PTHREAD_ID";
175const char * const config_event_context_hostname = "HOSTNAME";
176const char * const config_event_context_ip = "IP";
e885a367 177const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER";
ec2ed7da
JG
178LTTNG_HIDDEN const char * const config_event_context_app = "APP";
179LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE";
180LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
181LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
182LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
fb198a11 183
fbd987c9
JG
184/* Deprecated symbols */
185const char * const config_element_perf;
186
d7b645e2
JR
187enum process_event_node_phase {
188 CREATION = 0,
189 ENABLE = 1,
190};
191
dcf266c0
JG
192struct consumer_output {
193 int enabled;
194 char *path;
195 char *control_uri;
196 char *data_uri;
197};
198
1501a7f3
JG
199static int config_entry_handler_filter(struct handler_filter_args *args,
200 const char *section, const char *name, const char *value)
201{
202 int ret = 0;
203 struct config_entry entry = { section, name, value };
204
205 assert(args);
206
207 if (!section || !name || !value) {
208 ret = -EIO;
209 goto end;
210 }
211
212 if (args->section) {
213 if (strcmp(args->section, section)) {
214 goto end;
215 }
216 }
217
218 ret = args->handler(&entry, args->user_data);
219end:
220 return ret;
221}
222
5aee5c85
JR
223LTTNG_HIDDEN
224const char *config_get_domain_str(enum lttng_domain_type domain)
225{
226 const char *str_dom;
227
228 switch (domain) {
229 case LTTNG_DOMAIN_KERNEL:
230 str_dom = config_domain_type_kernel;
231 break;
232 case LTTNG_DOMAIN_UST:
233 str_dom = config_domain_type_ust;
234 break;
235 case LTTNG_DOMAIN_JUL:
236 str_dom = config_domain_type_jul;
237 break;
238 case LTTNG_DOMAIN_LOG4J:
239 str_dom = config_domain_type_log4j;
240 break;
241 case LTTNG_DOMAIN_PYTHON:
242 str_dom = config_domain_type_python;
243 break;
244 default:
245 assert(0);
246 }
247
248 return str_dom;
249}
250
f71bde2f
JR
251LTTNG_HIDDEN
252const char *config_get_loglevel_type_string(
253 enum lttng_loglevel_type loglevel_type)
254{
255 const char *loglevel_type_string;
256
257 switch (loglevel_type) {
258 case LTTNG_EVENT_LOGLEVEL_ALL:
259 loglevel_type_string = config_loglevel_type_all;
260 break;
261 case LTTNG_EVENT_LOGLEVEL_RANGE:
262 loglevel_type_string = config_loglevel_type_range;
263 break;
264 case LTTNG_EVENT_LOGLEVEL_SINGLE:
265 loglevel_type_string = config_loglevel_type_single;
266 break;
267 default:
268 loglevel_type_string = NULL;
269 }
270
271 return loglevel_type_string;
272}
273
1501a7f3
JG
274LTTNG_HIDDEN
275int config_get_section_entries(const char *override_path, const char *section,
276 config_entry_handler_cb handler, void *user_data)
277{
278 int ret = 0;
7ab02a27 279 char *path;
1501a7f3
JG
280 FILE *config_file = NULL;
281 struct handler_filter_args filter = { section, handler, user_data };
282
7ab02a27
DG
283 /* First, try system-wide conf. file. */
284 path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH;
285
286 config_file = fopen(path, "r");
287 if (config_file) {
288 DBG("Loading daemon conf file at %s", path);
289 /*
290 * Return value is not very important here since error or not, we
291 * continue and try the next possible conf. file.
292 */
293 (void) ini_parse_file(config_file,
294 (ini_entry_handler) config_entry_handler_filter,
295 (void *) &filter);
296 fclose(config_file);
297 }
298
299 /* Second is the user local configuration. */
300 path = utils_get_home_dir();
301 if (path) {
302 char fullpath[PATH_MAX];
303
304 ret = snprintf(fullpath, sizeof(fullpath),
305 DEFAULT_DAEMON_HOME_CONFIGPATH, path);
306 if (ret < 0) {
307 PERROR("snprintf user conf. path");
308 goto error;
309 }
310
311 config_file = fopen(fullpath, "r");
312 if (config_file) {
313 DBG("Loading daemon user conf file at %s", path);
314 /*
315 * Return value is not very important here since error or not, we
316 * continue and try the next possible conf. file.
317 */
318 (void) ini_parse_file(config_file,
319 (ini_entry_handler) config_entry_handler_filter,
320 (void *) &filter);
321 fclose(config_file);
322 }
323 }
324
325 /* Final path is the one that the user might have provided. */
1501a7f3
JG
326 if (override_path) {
327 config_file = fopen(override_path, "r");
328 if (config_file) {
7ab02a27
DG
329 DBG("Loading daemon command line conf file at %s", override_path);
330 (void) ini_parse_file(config_file,
331 (ini_entry_handler) config_entry_handler_filter,
332 (void *) &filter);
333 fclose(config_file);
1501a7f3
JG
334 } else {
335 ERR("Failed to open daemon configuration file at %s",
336 override_path);
337 ret = -ENOENT;
7ab02a27 338 goto error;
1501a7f3
JG
339 }
340 }
341
7ab02a27
DG
342 /* Everything went well. */
343 ret = 0;
1501a7f3 344
7ab02a27 345error:
1501a7f3
JG
346 return ret;
347}
348
349LTTNG_HIDDEN
350int config_parse_value(const char *value)
351{
352 int i, ret = 0;
353 char *endptr, *lower_str;
354 size_t len;
355 unsigned long v;
356
357 len = strlen(value);
358 if (!len) {
359 ret = -1;
360 goto end;
361 }
362
363 v = strtoul(value, &endptr, 10);
364 if (endptr != value) {
365 ret = v;
366 goto end;
367 }
368
369 lower_str = zmalloc(len + 1);
370 if (!lower_str) {
371 PERROR("zmalloc");
372 ret = -errno;
373 goto end;
374 }
375
376 for (i = 0; i < len; i++) {
377 lower_str[i] = tolower(value[i]);
378 }
379
380 if (!strcmp(lower_str, config_str_yes) ||
381 !strcmp(lower_str, config_str_true) ||
382 !strcmp(lower_str, config_str_on)) {
383 ret = 1;
384 } else if (!strcmp(lower_str, config_str_no) ||
385 !strcmp(lower_str, config_str_false) ||
386 !strcmp(lower_str, config_str_off)) {
387 ret = 0;
388 } else {
389 ret = -1;
390 }
391
392 free(lower_str);
393end:
394 return ret;
395}
36f2332b
JG
396
397/*
398 * Returns a xmlChar string which must be released using xmlFree().
399 */
400static xmlChar *encode_string(const char *in_str)
401{
402 xmlChar *out_str = NULL;
403 xmlCharEncodingHandlerPtr handler;
404 int out_len, ret, in_len;
405
406 assert(in_str);
407
408 handler = xmlFindCharEncodingHandler(config_xml_encoding);
409 if (!handler) {
410 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
411 goto end;
412 }
413
414 in_len = strlen(in_str);
415 /*
f76d886f
JG
416 * Add 1 byte for the NULL terminted character. The factor 4 here is
417 * used because UTF-8 characters can take up to 4 bytes.
36f2332b 418 */
f76d886f 419 out_len = (in_len * 4) + 1;
36f2332b
JG
420 out_str = xmlMalloc(out_len);
421 if (!out_str) {
422 goto end;
423 }
424
425 ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len);
426 if (ret < 0) {
427 xmlFree(out_str);
428 out_str = NULL;
429 goto end;
430 }
431
432 /* out_len is now the size of out_str */
433 out_str[out_len] = '\0';
434end:
435 return out_str;
436}
437
438LTTNG_HIDDEN
705bb62f 439struct config_writer *config_writer_create(int fd_output, int indent)
36f2332b
JG
440{
441 int ret;
442 struct config_writer *writer;
443 xmlOutputBufferPtr buffer;
444
445 writer = zmalloc(sizeof(struct config_writer));
446 if (!writer) {
447 PERROR("zmalloc config_writer_create");
448 goto end;
449 }
450
451 buffer = xmlOutputBufferCreateFd(fd_output, NULL);
452 if (!buffer) {
453 goto error_destroy;
454 }
455
456 writer->writer = xmlNewTextWriter(buffer);
457 ret = xmlTextWriterStartDocument(writer->writer, NULL,
458 config_xml_encoding, NULL);
459 if (ret < 0) {
460 goto error_destroy;
461 }
462
463 ret = xmlTextWriterSetIndentString(writer->writer,
464 BAD_CAST config_xml_indent_string);
705bb62f 465 if (ret) {
36f2332b
JG
466 goto error_destroy;
467 }
468
705bb62f
JRJ
469 ret = xmlTextWriterSetIndent(writer->writer, indent);
470 if (ret) {
36f2332b
JG
471 goto error_destroy;
472 }
473
474end:
475 return writer;
476error_destroy:
477 config_writer_destroy(writer);
478 return NULL;
479}
480
481LTTNG_HIDDEN
482int config_writer_destroy(struct config_writer *writer)
483{
484 int ret = 0;
485
486 if (!writer) {
487 ret = -EINVAL;
488 goto end;
489 }
490
491 if (xmlTextWriterEndDocument(writer->writer) < 0) {
492 WARN("Could not close XML document");
493 ret = -EIO;
494 }
495
496 if (writer->writer) {
497 xmlFreeTextWriter(writer->writer);
498 }
499
500 free(writer);
501end:
502 return ret;
503}
504
505LTTNG_HIDDEN
506int config_writer_open_element(struct config_writer *writer,
507 const char *element_name)
508{
509 int ret;
510 xmlChar *encoded_element_name;
511
512 if (!writer || !writer->writer || !element_name || !element_name[0]) {
513 ret = -1;
514 goto end;
515 }
516
517 encoded_element_name = encode_string(element_name);
518 if (!encoded_element_name) {
519 ret = -1;
520 goto end;
521 }
522
523 ret = xmlTextWriterStartElement(writer->writer, encoded_element_name);
524 xmlFree(encoded_element_name);
525end:
28676a1d 526 return ret >= 0 ? 0 : ret;
36f2332b
JG
527}
528
e10b6a1c
JG
529LTTNG_HIDDEN
530int config_writer_write_attribute(struct config_writer *writer,
531 const char *name, const char *value)
532{
533 int ret;
534 xmlChar *encoded_name = NULL;
535 xmlChar *encoded_value = NULL;
536
537 if (!writer || !writer->writer || !name || !name[0]) {
538 ret = -1;
539 goto end;
540 }
541
542 encoded_name = encode_string(name);
543 if (!encoded_name) {
544 ret = -1;
545 goto end;
546 }
547
548 encoded_value = encode_string(value);
549 if (!encoded_value) {
550 ret = -1;
551 goto end;
552 }
553
554 ret = xmlTextWriterWriteAttribute(writer->writer, encoded_name,
555 encoded_value);
556end:
557 xmlFree(encoded_name);
558 xmlFree(encoded_value);
559 return ret >= 0 ? 0 : ret;
560}
561
36f2332b
JG
562LTTNG_HIDDEN
563int config_writer_close_element(struct config_writer *writer)
564{
565 int ret;
566
567 if (!writer || !writer->writer) {
568 ret = -1;
569 goto end;
570 }
571
572 ret = xmlTextWriterEndElement(writer->writer);
573end:
28676a1d 574 return ret >= 0 ? 0 : ret;
36f2332b
JG
575}
576
577LTTNG_HIDDEN
578int config_writer_write_element_unsigned_int(struct config_writer *writer,
579 const char *element_name, uint64_t value)
580{
581 int ret;
582 xmlChar *encoded_element_name;
583
584 if (!writer || !writer->writer || !element_name || !element_name[0]) {
585 ret = -1;
586 goto end;
587 }
588
589 encoded_element_name = encode_string(element_name);
590 if (!encoded_element_name) {
591 ret = -1;
592 goto end;
593 }
594
595 ret = xmlTextWriterWriteFormatElement(writer->writer,
596 encoded_element_name, "%" PRIu64, value);
597 xmlFree(encoded_element_name);
598end:
28676a1d 599 return ret >= 0 ? 0 : ret;
36f2332b
JG
600}
601
602LTTNG_HIDDEN
603int config_writer_write_element_signed_int(struct config_writer *writer,
604 const char *element_name, int64_t value)
605{
606 int ret;
607 xmlChar *encoded_element_name;
608
609 if (!writer || !writer->writer || !element_name || !element_name[0]) {
610 ret = -1;
611 goto end;
612 }
613
614 encoded_element_name = encode_string(element_name);
615 if (!encoded_element_name) {
616 ret = -1;
617 goto end;
618 }
619
620 ret = xmlTextWriterWriteFormatElement(writer->writer,
621 encoded_element_name, "%" PRIi64, value);
622 xmlFree(encoded_element_name);
623end:
28676a1d 624 return ret >= 0 ? 0 : ret;
36f2332b
JG
625}
626
627LTTNG_HIDDEN
628int config_writer_write_element_bool(struct config_writer *writer,
629 const char *element_name, int value)
630{
631 return config_writer_write_element_string(writer, element_name,
632 value ? config_xml_true : config_xml_false);
633}
634
635LTTNG_HIDDEN
636int config_writer_write_element_string(struct config_writer *writer,
637 const char *element_name, const char *value)
638{
639 int ret;
640 xmlChar *encoded_element_name = NULL;
641 xmlChar *encoded_value = NULL;
642
643 if (!writer || !writer->writer || !element_name || !element_name[0] ||
644 !value) {
645 ret = -1;
646 goto end;
647 }
648
649 encoded_element_name = encode_string(element_name);
650 if (!encoded_element_name) {
651 ret = -1;
652 goto end;
653 }
654
655 encoded_value = encode_string(value);
656 if (!encoded_value) {
657 ret = -1;
658 goto end;
659 }
660
661 ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name,
dcf266c0 662 encoded_value);
36f2332b
JG
663end:
664 xmlFree(encoded_element_name);
665 xmlFree(encoded_value);
28676a1d 666 return ret >= 0 ? 0 : ret;
36f2332b 667}
dcf266c0 668
8e79e1cd
JR
669LTTNG_HIDDEN
670int config_writer_write_config_element(struct config_writer *writer,
671 const struct config_element *element)
672{
673 int ret = 0;
674
675 xmlNodePtr local_copy = NULL;
676 xmlNodePtr place_holder = NULL;
677 xmlDocPtr document = NULL;
678 xmlBufferPtr buffer = NULL;
679
680 assert(element);
681 assert(element->element);
682 assert(writer);
683
684 /* Create a new doc and set root node as passed element */
685 document = xmlNewDoc(BAD_CAST "1.0");
686 document->parseFlags |= XML_PARSE_NOBLANKS;
687 if (!document) {
688 ERR("Unable to create xml document");
689 ret = -1;
690 goto end;
691 }
692
693 local_copy = xmlCopyNode(element->element, 1);
694 if (!local_copy) {
695 ERR("Unable to copy an xml node");
696 ret = -1;
697 goto end;
698 }
699
700 /* Hand off the local_copy to the xmlDoc */
701 place_holder = xmlDocSetRootElement(document, local_copy);
702 if (place_holder) {
703 ERR("The document root was already set");
704 xmlFreeNode(local_copy);
705 ret = -1;
706 goto end;
707 }
708
709 buffer = xmlBufferCreate();
710 if (!buffer) {
711 ret = -1;
712 goto end;
713 }
714
715 ret = xmlNodeDump(buffer, document, local_copy, 0, 0);
716 xmlTextWriterWriteRaw(writer->writer, xmlBufferContent(buffer));
717end:
718 xmlBufferFree(buffer);
719 xmlFreeDoc(document);
720 return ret;
721}
722
dcf266c0
JG
723static
724void xml_error_handler(void *ctx, const char *format, ...)
725{
726 char *errMsg;
727 va_list args;
728 int ret;
729
730 va_start(args, format);
731 ret = vasprintf(&errMsg, format, args);
6c043b48 732 va_end(args);
dcf266c0
JG
733 if (ret == -1) {
734 ERR("String allocation failed in xml error handler");
735 return;
736 }
dcf266c0
JG
737
738 fprintf(stderr, "XML Error: %s", errMsg);
739 free(errMsg);
740}
741
742static
743void fini_session_config_validation_ctx(
744 struct session_config_validation_ctx *ctx)
745{
746 if (ctx->parser_ctx) {
747 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
748 }
749
750 if (ctx->schema) {
751 xmlSchemaFree(ctx->schema);
752 }
753
754 if (ctx->schema_validation_ctx) {
755 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
756 }
757
758 memset(ctx, 0, sizeof(struct session_config_validation_ctx));
759}
760
54e399cb
JG
761static
762char *get_session_config_xsd_path()
763{
764 char *xsd_path;
e8fa9fb0 765 const char *base_path = lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV);
54e399cb
JG
766 size_t base_path_len;
767 size_t max_path_len;
768
769 if (!base_path) {
770 base_path = DEFAULT_SESSION_CONFIG_XSD_PATH;
771 }
772
773 base_path_len = strlen(base_path);
774 max_path_len = base_path_len +
775 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1;
776 xsd_path = zmalloc(max_path_len);
777 if (!xsd_path) {
778 goto end;
779 }
780
781 strncpy(xsd_path, base_path, max_path_len);
782 if (xsd_path[base_path_len - 1] != '/') {
783 xsd_path[base_path_len++] = '/';
784 }
785
786 strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME,
787 max_path_len - base_path_len);
788end:
789 return xsd_path;
790}
791
dcf266c0
JG
792static
793int init_session_config_validation_ctx(
794 struct session_config_validation_ctx *ctx)
795{
796 int ret;
54e399cb
JG
797 char *xsd_path = get_session_config_xsd_path();
798
799 if (!xsd_path) {
800 ret = -LTTNG_ERR_NOMEM;
801 goto end;
802 }
dcf266c0 803
54e399cb 804 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
dcf266c0
JG
805 if (!ctx->parser_ctx) {
806 ERR("XSD parser context creation failed");
807 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
808 goto end;
809 }
810 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
811 xml_error_handler, NULL);
812
813 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
814 if (!ctx->schema) {
815 ERR("XSD parsing failed");
816 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
817 goto end;
818 }
819
820 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
821 if (!ctx->schema_validation_ctx) {
822 ERR("XSD validation context creation failed");
823 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
824 goto end;
825 }
826
827 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
828 xml_error_handler, NULL);
829 ret = 0;
830
831end:
832 if (ret) {
833 fini_session_config_validation_ctx(ctx);
834 }
835
54e399cb 836 free(xsd_path);
dcf266c0
JG
837 return ret;
838}
839
840static
841int parse_uint(xmlChar *str, uint64_t *val)
842{
843 int ret;
844 char *endptr;
845
846 if (!str || !val) {
847 ret = -1;
848 goto end;
849 }
850
851 *val = strtoull((const char *) str, &endptr, 10);
852 if (!endptr || *endptr) {
853 ret = -1;
854 } else {
855 ret = 0;
856 }
857
858end:
859 return ret;
860}
861
862static
863int parse_int(xmlChar *str, int64_t *val)
864{
865 int ret;
866 char *endptr;
867
868 if (!str || !val) {
869 ret = -1;
870 goto end;
871 }
872
873 *val = strtoll((const char *) str, &endptr, 10);
874 if (!endptr || *endptr) {
875 ret = -1;
876 } else {
877 ret = 0;
878 }
879
880end:
881 return ret;
882}
883
884static
885int parse_bool(xmlChar *str, int *val)
886{
887 int ret = 0;
888
889 if (!str || !val) {
890 ret = -1;
891 goto end;
892 }
893
894 if (!strcmp((const char *) str, config_xml_true)) {
895 *val = 1;
896 } else if (!strcmp((const char *) str, config_xml_false)) {
897 *val = 0;
898 } else {
899 WARN("Invalid boolean value encoutered (%s).",
900 (const char *) str);
901 ret = -1;
902 }
903end:
904 return ret;
905}
906
907static
908int get_domain_type(xmlChar *domain)
909{
910 int ret;
911
912 if (!domain) {
913 goto error;
914 }
915
916 if (!strcmp((char *) domain, config_domain_type_kernel)) {
917 ret = LTTNG_DOMAIN_KERNEL;
918 } else if (!strcmp((char *) domain, config_domain_type_ust)) {
919 ret = LTTNG_DOMAIN_UST;
920 } else if (!strcmp((char *) domain, config_domain_type_jul)) {
921 ret = LTTNG_DOMAIN_JUL;
5cdb6027
DG
922 } else if (!strcmp((char *) domain, config_domain_type_log4j)) {
923 ret = LTTNG_DOMAIN_LOG4J;
0e115563
DG
924 } else if (!strcmp((char *) domain, config_domain_type_python)) {
925 ret = LTTNG_DOMAIN_PYTHON;
dcf266c0
JG
926 } else {
927 goto error;
928 }
929
930 return ret;
931error:
932 return -1;
933}
934
935static
936int get_buffer_type(xmlChar *buffer_type)
937{
938 int ret;
939
940 if (!buffer_type) {
941 goto error;
942 }
943
944 if (!strcmp((char *) buffer_type, config_buffer_type_global)) {
945 ret = LTTNG_BUFFER_GLOBAL;
946 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) {
947 ret = LTTNG_BUFFER_PER_UID;
948 } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) {
949 ret = LTTNG_BUFFER_PER_PID;
950 } else {
951 goto error;
952 }
953
954 return ret;
955error:
956 return -1;
957}
958
959static
960int get_overwrite_mode(xmlChar *overwrite_mode)
961{
962 int ret;
963
964 if (!overwrite_mode) {
965 goto error;
966 }
967
968 if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) {
969 ret = 1;
970 } else if (!strcmp((char *) overwrite_mode,
971 config_overwrite_mode_discard)) {
972 ret = 0;
973 } else {
974 goto error;
975 }
976
977 return ret;
978error:
979 return -1;
980}
981
982static
983int get_output_type(xmlChar *output_type)
984{
985 int ret;
986
987 if (!output_type) {
988 goto error;
989 }
990
991 if (!strcmp((char *) output_type, config_output_type_mmap)) {
992 ret = LTTNG_EVENT_MMAP;
993 } else if (!strcmp((char *) output_type, config_output_type_splice)) {
994 ret = LTTNG_EVENT_SPLICE;
995 } else {
996 goto error;
997 }
998
999 return ret;
1000error:
1001 return -1;
1002}
1003
471713e3
JR
1004LTTNG_HIDDEN
1005int config_get_event_type(const char *event_type)
dcf266c0
JG
1006{
1007 int ret;
1008
1009 if (!event_type) {
1010 goto error;
1011 }
1012
471713e3 1013 if (!strcmp(event_type, config_event_type_all)) {
dcf266c0 1014 ret = LTTNG_EVENT_ALL;
471713e3 1015 } else if (!strcmp(event_type, config_event_type_tracepoint)) {
dcf266c0 1016 ret = LTTNG_EVENT_TRACEPOINT;
471713e3 1017 } else if (!strcmp(event_type, config_event_type_probe)) {
dcf266c0 1018 ret = LTTNG_EVENT_PROBE;
471713e3 1019 } else if (!strcmp(event_type, config_event_type_function)) {
dcf266c0 1020 ret = LTTNG_EVENT_FUNCTION;
471713e3 1021 } else if (!strcmp(event_type,
dcf266c0
JG
1022 config_event_type_function_entry)) {
1023 ret = LTTNG_EVENT_FUNCTION_ENTRY;
471713e3 1024 } else if (!strcmp(event_type, config_event_type_noop)) {
dcf266c0 1025 ret = LTTNG_EVENT_NOOP;
471713e3 1026 } else if (!strcmp(event_type, config_event_type_syscall)) {
dcf266c0
JG
1027 ret = LTTNG_EVENT_SYSCALL;
1028 } else {
1029 goto error;
1030 }
1031
1032 return ret;
1033error:
1034 return -1;
1035}
1036
1037static
1038int get_loglevel_type(xmlChar *loglevel_type)
1039{
1040 int ret;
1041
1042 if (!loglevel_type) {
1043 goto error;
1044 }
1045
1046 if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) {
1047 ret = LTTNG_EVENT_LOGLEVEL_ALL;
1048 } else if (!strcmp((char *) loglevel_type,
1049 config_loglevel_type_range)) {
1050 ret = LTTNG_EVENT_LOGLEVEL_RANGE;
1051 } else if (!strcmp((char *) loglevel_type,
1052 config_loglevel_type_single)) {
1053 ret = LTTNG_EVENT_LOGLEVEL_SINGLE;
1054 } else {
1055 goto error;
1056 }
1057
1058 return ret;
1059error:
1060 return -1;
1061}
1062
1063/*
1064 * Return the context type or -1 on error.
1065 */
1066static
1067int get_context_type(xmlChar *context_type)
1068{
1069 int ret;
1070
1071 if (!context_type) {
1072 goto error;
1073 }
1074
1075 if (!strcmp((char *) context_type, config_event_context_pid)) {
1076 ret = LTTNG_EVENT_CONTEXT_PID;
1077 } else if (!strcmp((char *) context_type,
1078 config_event_context_procname)) {
1079 ret = LTTNG_EVENT_CONTEXT_PROCNAME;
1080 } else if (!strcmp((char *) context_type,
1081 config_event_context_prio)) {
1082 ret = LTTNG_EVENT_CONTEXT_PRIO;
1083 } else if (!strcmp((char *) context_type,
1084 config_event_context_nice)) {
1085 ret = LTTNG_EVENT_CONTEXT_NICE;
1086 } else if (!strcmp((char *) context_type,
1087 config_event_context_vpid)) {
1088 ret = LTTNG_EVENT_CONTEXT_VPID;
1089 } else if (!strcmp((char *) context_type,
1090 config_event_context_tid)) {
1091 ret = LTTNG_EVENT_CONTEXT_TID;
1092 } else if (!strcmp((char *) context_type,
1093 config_event_context_vtid)) {
1094 ret = LTTNG_EVENT_CONTEXT_VTID;
1095 } else if (!strcmp((char *) context_type,
1096 config_event_context_ppid)) {
1097 ret = LTTNG_EVENT_CONTEXT_PPID;
1098 } else if (!strcmp((char *) context_type,
1099 config_event_context_vppid)) {
1100 ret = LTTNG_EVENT_CONTEXT_VPPID;
1101 } else if (!strcmp((char *) context_type,
1102 config_event_context_pthread_id)) {
1103 ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID;
1104 } else if (!strcmp((char *) context_type,
1105 config_event_context_hostname)) {
1106 ret = LTTNG_EVENT_CONTEXT_HOSTNAME;
1107 } else if (!strcmp((char *) context_type,
1108 config_event_context_ip)) {
1109 ret = LTTNG_EVENT_CONTEXT_IP;
1ae5e83e
JD
1110 } else if (!strcmp((char *) context_type,
1111 config_event_context_interruptible)) {
1112 ret = LTTNG_EVENT_CONTEXT_INTERRUPTIBLE;
1113 } else if (!strcmp((char *) context_type,
1114 config_event_context_preemptible)) {
1115 ret = LTTNG_EVENT_CONTEXT_PREEMPTIBLE;
1116 } else if (!strcmp((char *) context_type,
1117 config_event_context_need_reschedule)) {
1118 ret = LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE;
1119 } else if (!strcmp((char *) context_type,
1120 config_event_context_migratable)) {
1121 ret = LTTNG_EVENT_CONTEXT_MIGRATABLE;
dcf266c0
JG
1122 } else {
1123 goto error;
1124 }
1125
1126 return ret;
1127error:
1128 return -1;
1129}
1130
1131static
1132int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
1133{
1134 int ret;
1135 xmlNodePtr node;
1136
1137 for (node = xmlFirstElementChild(domain_node); node;
1138 node = xmlNextElementSibling(node)) {
1139 if (!strcmp((const char *) node->name, config_element_type)) {
1140 /* domain type */
1141 xmlChar *node_content = xmlNodeGetContent(node);
1142 if (!node_content) {
1143 ret = -LTTNG_ERR_NOMEM;
1144 goto end;
1145 }
1146
1147 ret = get_domain_type(node_content);
1148 free(node_content);
1149 if (ret < 0) {
1150 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1151 goto end;
1152 }
1153
1154 domain->type = ret;
1155 } else if (!strcmp((const char *) node->name,
1156 config_element_buffer_type)) {
1157 /* buffer type */
1158 xmlChar *node_content = xmlNodeGetContent(node);
1159 if (!node_content) {
1160 ret = -LTTNG_ERR_NOMEM;
1161 goto end;
1162 }
1163
1164 ret = get_buffer_type(node_content);
1165 free(node_content);
1166 if (ret < 0) {
1167 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1168 goto end;
1169 }
1170
1171 domain->buf_type = ret;
1172 }
1173 }
1174 ret = 0;
1175end:
1176 return ret;
1177}
1178
1179static
1180int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
1181 char **data_uri)
1182{
1183 xmlNodePtr node;
1184
1185 for (node = xmlFirstElementChild(net_output_node); node;
1186 node = xmlNextElementSibling(node)) {
1187 if (!strcmp((const char *) node->name, config_element_control_uri)) {
1188 /* control_uri */
1189 *control_uri = (char *) xmlNodeGetContent(node);
1190 if (!*control_uri) {
1191 break;
1192 }
1193 } else {
1194 /* data_uri */
1195 *data_uri = (char *) xmlNodeGetContent(node);
1196 if (!*data_uri) {
1197 break;
1198 }
1199 }
1200 }
1201
1202 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
1203}
1204
1205static
1206int process_consumer_output(xmlNodePtr consumer_output_node,
1207 struct consumer_output *output)
1208{
1209 int ret;
1210 xmlNodePtr node;
1211
1212 assert(output);
1213
1214 for (node = xmlFirstElementChild(consumer_output_node); node;
1215 node = xmlNextElementSibling(node)) {
1216 if (!strcmp((const char *) node->name, config_element_enabled)) {
1217 xmlChar *enabled_str = xmlNodeGetContent(node);
1218
1219 /* enabled */
1220 if (!enabled_str) {
1221 ret = -LTTNG_ERR_NOMEM;
1222 goto end;
1223 }
1224
1225 ret = parse_bool(enabled_str, &output->enabled);
1226 free(enabled_str);
1227 if (ret) {
1228 goto end;
1229 }
1230 } else {
1231 xmlNodePtr output_type_node;
1232
1233 /* destination */
1234 output_type_node = xmlFirstElementChild(node);
1235 if (!output_type_node) {
1236 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1237 goto end;
1238 }
1239
1240 if (!strcmp((const char *) output_type_node->name,
1241 config_element_path)) {
1242 /* path */
1243 output->path = (char *) xmlNodeGetContent(output_type_node);
1244 if (!output->path) {
1245 ret = -LTTNG_ERR_NOMEM;
1246 goto end;
1247 }
1248 } else {
1249 /* net_output */
1250 ret = get_net_output_uris(output_type_node,
1251 &output->control_uri, &output->data_uri);
1252 if (ret) {
1253 goto end;
1254 }
1255 }
1256 }
1257 }
1258 ret = 0;
1259
1260end:
1261 if (ret) {
1262 free(output->path);
1263 free(output->control_uri);
1264 free(output->data_uri);
1265 memset(output, 0, sizeof(struct consumer_output));
1266 }
1267 return ret;
1268}
1269
1270static
95681498
JG
1271int create_session_net_output(const char *name, const char *control_uri,
1272 const char *data_uri)
dcf266c0
JG
1273{
1274 int ret;
1275 struct lttng_handle *handle;
1276 const char *uri = NULL;
1277
1278 assert(name);
dcf266c0 1279
95681498 1280 handle = lttng_create_handle(name, NULL);
dcf266c0
JG
1281 if (!handle) {
1282 ret = -LTTNG_ERR_NOMEM;
1283 goto end;
1284 }
1285
1286 if (!control_uri || !data_uri) {
1287 uri = control_uri ? control_uri : data_uri;
1288 control_uri = uri;
1289 data_uri = uri;
1290 }
1291
1292 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1293 lttng_destroy_handle(handle);
1294end:
1295 return ret;
1296}
1297
1298static
1299int create_snapshot_session(const char *session_name, xmlNodePtr output_node)
1300{
1301 int ret;
1302 xmlNodePtr node = NULL;
1303 xmlNodePtr snapshot_output_list_node;
1304 xmlNodePtr snapshot_output_node;
1305
1306 assert(session_name);
1307
1308 ret = lttng_create_session_snapshot(session_name, NULL);
1309 if (ret) {
1310 goto end;
1311 }
1312
1313 if (!output_node) {
1314 goto end;
1315 }
1316
1317 snapshot_output_list_node = xmlFirstElementChild(output_node);
1318
1319 /* Parse and create snapshot outputs */
1320
1321 for (snapshot_output_node =
1322 xmlFirstElementChild(snapshot_output_list_node);
1323 snapshot_output_node; snapshot_output_node =
1324 xmlNextElementSibling(snapshot_output_node)) {
1325 char *name = NULL;
98e0892f 1326 uint64_t max_size = DEFAULT_SNAPSHOT_MAX_SIZE;
dcf266c0
JG
1327 struct consumer_output output = { 0 };
1328 struct lttng_snapshot_output *snapshot_output = NULL;
1329
1330 for (node = xmlFirstElementChild(snapshot_output_node); node;
1331 node = xmlNextElementSibling(node)) {
1332 if (!strcmp((const char *) node->name,
1333 config_element_name)) {
1334 /* name */
1335 name = (char *) xmlNodeGetContent(node);
1336 if (!name) {
1337 ret = -LTTNG_ERR_NOMEM;
1338 goto error_snapshot_output;
1339 }
1340 } else if (!strcmp((const char *) node->name,
1341 config_element_max_size)) {
1342 xmlChar *content = xmlNodeGetContent(node);
1343
1344 /* max_size */
1345 if (!content) {
1346 ret = -LTTNG_ERR_NOMEM;
1347 goto error_snapshot_output;
1348 }
1349 ret = parse_uint(content, &max_size);
1350 free(content);
1351 if (ret) {
1352 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1353 goto error_snapshot_output;
1354 }
1355 } else {
1356 /* consumer_output */
1357 ret = process_consumer_output(node, &output);
1358 if (ret) {
1359 goto error_snapshot_output;
1360 }
1361 }
1362 }
1363
1364 snapshot_output = lttng_snapshot_output_create();
1365 if (!snapshot_output) {
1366 ret = -LTTNG_ERR_NOMEM;
1367 goto error_snapshot_output;
1368 }
1369
98e0892f
JR
1370 if (!name) {
1371 /* Generate a default name */
1372 int pret;
1373 pret = asprintf(&name, DEFAULT_SNAPSHOT_NAME "-%" PRIu32,
1374 lttng_snapshot_output_get_id(snapshot_output));
1375 if (pret < 0) {
1376 name = NULL;
1377 PERROR("snprintf output name");
1378 goto error_snapshot_output;
1379 }
1380 }
1381
dcf266c0
JG
1382 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1383 if (ret) {
1384 goto error_snapshot_output;
1385 }
1386
1387 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1388 if (ret) {
1389 goto error_snapshot_output;
1390 }
1391
1392 if (output.path) {
1393 ret = lttng_snapshot_output_set_ctrl_url(output.path,
1394 snapshot_output);
1395 if (ret) {
1396 goto error_snapshot_output;
1397 }
1398 } else {
1399 if (output.control_uri) {
1400 ret = lttng_snapshot_output_set_ctrl_url(output.control_uri,
1401 snapshot_output);
1402 if (ret) {
1403 goto error_snapshot_output;
1404 }
1405 }
1406
1407 if (output.data_uri) {
1408 ret = lttng_snapshot_output_set_data_url(output.data_uri,
1409 snapshot_output);
1410 if (ret) {
1411 goto error_snapshot_output;
1412 }
1413 }
1414 }
1415
1416 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1417error_snapshot_output:
1418 free(name);
1419 free(output.path);
1420 free(output.control_uri);
1421 free(output.data_uri);
1422 lttng_snapshot_output_destroy(snapshot_output);
1423 if (ret) {
1424 goto end;
1425 }
1426 }
1427end:
1428 return ret;
1429}
1430
1431static
1432int create_session(const char *name,
1433 struct lttng_domain *kernel_domain,
1434 struct lttng_domain *ust_domain,
1435 struct lttng_domain *jul_domain,
5cdb6027 1436 struct lttng_domain *log4j_domain,
dcf266c0
JG
1437 xmlNodePtr output_node,
1438 uint64_t live_timer_interval)
1439{
1440 int ret;
1441 struct consumer_output output = { 0 };
1442 xmlNodePtr consumer_output_node;
1443
1444 assert(name);
dcf266c0
JG
1445
1446 if (output_node) {
1447 consumer_output_node = xmlFirstElementChild(output_node);
1448 if (!consumer_output_node) {
1449 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1450 goto end;
1451 }
1452
1453 if (strcmp((const char *) consumer_output_node->name,
1454 config_element_consumer_output)) {
1455 WARN("Invalid output type, expected %s node",
1456 config_element_consumer_output);
1457 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1458 goto end;
1459 }
1460
1461 ret = process_consumer_output(consumer_output_node, &output);
1462 if (ret) {
1463 goto end;
1464 }
1465 }
1466
1467 if (live_timer_interval != UINT64_MAX &&
1468 !output.control_uri && !output.data_uri) {
1469 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1470 goto end;
1471 }
1472
1473 if (output.control_uri || output.data_uri) {
dcf266c0
JG
1474 /* network destination */
1475 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
b664f89a
DG
1476 /*
1477 * URLs are provided for sure since the test above make sure that
1478 * with a live timer the data and control URIs are provided. So,
1479 * NULL is passed here and will be set right after.
1480 */
1481 ret = lttng_create_session_live(name, NULL, live_timer_interval);
dcf266c0
JG
1482 } else {
1483 ret = lttng_create_session(name, NULL);
1484 }
1485 if (ret) {
1486 goto end;
1487 }
1488
95681498
JG
1489 ret = create_session_net_output(name, output.control_uri,
1490 output.data_uri);
1491 if (ret) {
1492 goto end;
dcf266c0 1493 }
95681498 1494
dcf266c0
JG
1495 } else {
1496 /* either local output or no output */
1497 ret = lttng_create_session(name, output.path);
1498 if (ret) {
1499 goto end;
1500 }
1501 }
1502end:
1503 free(output.path);
1504 free(output.control_uri);
1505 free(output.data_uri);
1506 return ret;
1507}
1508static
1509int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1510 struct lttng_event_probe_attr *attr)
1511{
1512 int ret;
1513
1514 assert(probe_attribute_node);
1515 assert(attr);
1516
1517 if (!strcmp((const char *) probe_attribute_node->name,
1518 config_element_address)) {
1519 xmlChar *content;
1520 uint64_t addr = 0;
1521
1522 /* addr */
1523 content = xmlNodeGetContent(probe_attribute_node);
1524 if (!content) {
1525 ret = -LTTNG_ERR_NOMEM;
1526 goto end;
1527 }
1528
1529 ret = parse_uint(content, &addr);
1530 free(content);
1531 if (ret) {
1532 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1533 goto end;
1534 }
1535
1536 attr->addr = addr;
1537 } else if (!strcmp((const char *) probe_attribute_node->name,
1538 config_element_offset)) {
1539 xmlChar *content;
1540 uint64_t offset = 0;
1541
1542 /* offset */
1543 content = xmlNodeGetContent(probe_attribute_node);
1544 if (!content) {
1545 ret = -LTTNG_ERR_NOMEM;
1546 goto end;
1547 }
1548
1549 ret = parse_uint(content, &offset);
1550 free(content);
1551 if (ret) {
1552 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1553 goto end;
1554 }
1555
1556 attr->offset = offset;
1557 } else if (!strcmp((const char *) probe_attribute_node->name,
1558 config_element_symbol_name)) {
1559 xmlChar *content;
1560 size_t name_len;
1561
1562 /* symbol_name */
1563 content = xmlNodeGetContent(probe_attribute_node);
1564 if (!content) {
1565 ret = -LTTNG_ERR_NOMEM;
1566 goto end;
1567 }
1568
1569 name_len = strlen((char *) content);
1570 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1571 WARN("symbol_name too long.");
1572 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1573 free(content);
1574 goto end;
1575 }
1576
1577 strncpy(attr->symbol_name, (const char *) content, name_len);
1578 free(content);
1579 }
1580 ret = 0;
1581end:
1582 return ret;
1583}
1584
1585static
1586int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
d7b645e2 1587 const char *channel_name, const enum process_event_node_phase phase)
dcf266c0 1588{
d7b645e2 1589 int ret = 0, i;
dcf266c0
JG
1590 xmlNodePtr node;
1591 struct lttng_event event;
1592 char **exclusions = NULL;
1593 unsigned long exclusion_count = 0;
1594 char *filter_expression = NULL;
1595
1596 assert(event_node);
1597 assert(handle);
1598 assert(channel_name);
1599
1600 memset(&event, 0, sizeof(event));
1601
f40eba3d
JG
1602 /* Initialize default log level which varies by domain */
1603 switch (handle->domain.type)
1604 {
1605 case LTTNG_DOMAIN_JUL:
1606 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1607 break;
1608 case LTTNG_DOMAIN_LOG4J:
1609 event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1610 break;
1611 case LTTNG_DOMAIN_PYTHON:
1612 event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1613 break;
1614 case LTTNG_DOMAIN_UST:
1615 case LTTNG_DOMAIN_KERNEL:
1616 event.loglevel = LTTNG_LOGLEVEL_DEBUG;
1617 break;
1618 default:
1619 assert(0);
1620 }
1621
dcf266c0
JG
1622 for (node = xmlFirstElementChild(event_node); node;
1623 node = xmlNextElementSibling(node)) {
1624 if (!strcmp((const char *) node->name, config_element_name)) {
1625 xmlChar *content;
1626 size_t name_len;
1627
1628 /* name */
1629 content = xmlNodeGetContent(node);
1630 if (!content) {
1631 ret = -LTTNG_ERR_NOMEM;
1632 goto end;
1633 }
1634
1635 name_len = strlen((char *) content);
1636 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1637 WARN("Channel name too long.");
1638 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1639 free(content);
1640 goto end;
1641 }
1642
1643 strncpy(event.name, (const char *) content, name_len);
1644 free(content);
1645 } else if (!strcmp((const char *) node->name,
1646 config_element_enabled)) {
1647 xmlChar *content = xmlNodeGetContent(node);
1648
1649 /* enabled */
1650 if (!content) {
1651 ret = -LTTNG_ERR_NOMEM;
1652 goto end;
1653 }
1654
1655 ret = parse_bool(content, &event.enabled);
1656 free(content);
1657 if (ret) {
1658 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1659 goto end;
1660 }
1661 } else if (!strcmp((const char *) node->name,
1662 config_element_type)) {
1663 xmlChar *content = xmlNodeGetContent(node);
1664
1665 /* type */
1666 if (!content) {
1667 ret = -LTTNG_ERR_NOMEM;
1668 goto end;
1669 }
1670
471713e3 1671 ret = config_get_event_type((char *) content);
dcf266c0
JG
1672 free(content);
1673 if (ret < 0) {
1674 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1675 goto end;
1676 }
1677
1678 event.type = ret;
1679 } else if (!strcmp((const char *) node->name,
1680 config_element_loglevel_type)) {
1681 xmlChar *content = xmlNodeGetContent(node);
1682
1683 /* loglevel_type */
1684 if (!content) {
1685 ret = -LTTNG_ERR_NOMEM;
1686 goto end;
1687 }
1688
1689 ret = get_loglevel_type(content);
1690 free(content);
1691 if (ret < 0) {
1692 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1693 goto end;
1694 }
1695
1696 event.loglevel_type = ret;
1697 } else if (!strcmp((const char *) node->name,
1698 config_element_loglevel)) {
1699 xmlChar *content;
1700 int64_t loglevel = 0;
1701
1702 /* loglevel */
1703 content = xmlNodeGetContent(node);
1704 if (!content) {
1705 ret = -LTTNG_ERR_NOMEM;
1706 goto end;
1707 }
1708
1709 ret = parse_int(content, &loglevel);
1710 free(content);
1711 if (ret) {
1712 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1713 goto end;
1714 }
1715
1716 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1717 WARN("loglevel out of range.");
1718 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1719 goto end;
1720 }
1721
1722 event.loglevel = loglevel;
1723 } else if (!strcmp((const char *) node->name,
1724 config_element_filter)) {
1725 xmlChar *content =
1726 xmlNodeGetContent(node);
1727
1728 /* filter */
1729 if (!content) {
1730 ret = -LTTNG_ERR_NOMEM;
1731 goto end;
1732 }
1733
02d8ac3d 1734 free(filter_expression);
dcf266c0
JG
1735 filter_expression = strdup((char *) content);
1736 free(content);
1737 if (!filter_expression) {
1738 ret = -LTTNG_ERR_NOMEM;
1739 goto end;
1740 }
1741 } else if (!strcmp((const char *) node->name,
1742 config_element_exclusions)) {
1743 xmlNodePtr exclusion_node;
1744 int exclusion_index = 0;
1745
1746 /* exclusions */
1747 if (exclusions) {
1748 /*
1749 * Exclusions has already been initialized,
1750 * invalid file.
1751 */
1752 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1753 goto end;
1754 }
1755
1756 exclusion_count = xmlChildElementCount(node);
1757 if (!exclusion_count) {
1758 continue;
1759 }
1760
1761 exclusions = zmalloc(exclusion_count * sizeof(char *));
1762 if (!exclusions) {
1763 exclusion_count = 0;
1764 ret = -LTTNG_ERR_NOMEM;
1765 goto end;
1766 }
1767
1768 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1769 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1770 xmlChar *content =
1771 xmlNodeGetContent(exclusion_node);
1772
1773 if (!content) {
1774 ret = -LTTNG_ERR_NOMEM;
1775 goto end;
1776 }
1777
1778 exclusions[exclusion_index] = strdup((const char *) content);
1779 free(content);
1780 if (!exclusions[exclusion_index]) {
1781 ret = -LTTNG_ERR_NOMEM;
1782 goto end;
1783 }
1784 exclusion_index++;
1785 }
1786
1787 event.exclusion = 1;
1788 } else if (!strcmp((const char *) node->name,
1789 config_element_attributes)) {
1790 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1791
1792 /* attributes */
1793 if (!attribute_node) {
1794 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1795 goto end;
1796 }
1797
1798 if (!strcmp((const char *) node->name,
1799 config_element_probe_attributes)) {
1800 xmlNodePtr probe_attribute_node;
1801
1802 /* probe_attributes */
1803 for (probe_attribute_node =
1804 xmlFirstElementChild(attribute_node); probe_attribute_node;
1805 probe_attribute_node = xmlNextElementSibling(
1806 probe_attribute_node)) {
1807
1808 ret = process_probe_attribute_node(probe_attribute_node,
1809 &event.attr.probe);
1810 if (ret) {
1811 goto end;
1812 }
1813 }
1814 } else {
1815 size_t sym_len;
1816 xmlChar *content;
1817 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1818
1819 /* function_attributes */
1820 content = xmlNodeGetContent(symbol_node);
1821 if (!content) {
1822 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1823 goto end;
1824 }
1825
1826 sym_len = strlen((char *) content);
1827 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1828 WARN("Function name too long.");
1829 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1830 free(content);
1831 goto end;
1832 }
1833
1834 strncpy(event.attr.ftrace.symbol_name, (char *) content,
1835 sym_len);
1836 free(content);
1837 }
1838 }
1839 }
1840
d7b645e2
JR
1841 if ((event.enabled && phase == ENABLE) || phase == CREATION) {
1842 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1843 filter_expression, exclusion_count, exclusions);
1844 if (ret < 0) {
1845 WARN("Enabling event (name:%s) on load failed.", event.name);
1846 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1847 goto end;
1848 }
065321e9 1849 }
dcf266c0
JG
1850end:
1851 for (i = 0; i < exclusion_count; i++) {
1852 free(exclusions[i]);
1853 }
1854
1855 free(exclusions);
1856 free(filter_expression);
1857 return ret;
1858}
1859
1860static
1861int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1862 const char *channel_name)
1863{
1864 int ret = 0;
d7b645e2 1865 struct lttng_event event;
dcf266c0
JG
1866 xmlNodePtr node;
1867
1868 assert(events_node);
1869 assert(handle);
1870 assert(channel_name);
1871
1872 for (node = xmlFirstElementChild(events_node); node;
1873 node = xmlNextElementSibling(node)) {
d7b645e2 1874 ret = process_event_node(node, handle, channel_name, CREATION);
dcf266c0
JG
1875 if (ret) {
1876 goto end;
1877 }
1878 }
d7b645e2
JR
1879
1880 /*
1881 * Disable all events to enable only the necessary events.
1882 * Limitations regarding lttng_disable_events and tuple descriptor
1883 * force this approach.
1884 */
1885 memset(&event, 0, sizeof(event));
1886 event.loglevel = -1;
1887 event.type = LTTNG_EVENT_ALL;
1888 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
1889 if (ret) {
1890 goto end;
1891 }
1892
1893 for (node = xmlFirstElementChild(events_node); node;
1894 node = xmlNextElementSibling(node)) {
1895 ret = process_event_node(node, handle, channel_name, ENABLE);
1896 if (ret) {
1897 goto end;
1898 }
1899 }
1900
dcf266c0
JG
1901end:
1902 return ret;
1903}
1904
1905static
baa4ef37
JG
1906int process_channel_attr_node(xmlNodePtr attr_node, bool snapshot_mode,
1907 enum lttng_domain_type domain, struct lttng_channel *channel,
1908 xmlNodePtr *contexts_node, xmlNodePtr *events_node)
dcf266c0
JG
1909{
1910 int ret;
1911
1912 assert(attr_node);
1913 assert(channel);
1914 assert(contexts_node);
1915 assert(events_node);
1916
1917 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1918 xmlChar *content;
1919 size_t name_len;
1920
1921 /* name */
1922 content = xmlNodeGetContent(attr_node);
1923 if (!content) {
1924 ret = -LTTNG_ERR_NOMEM;
1925 goto end;
1926 }
1927
1928 name_len = strlen((char *) content);
1929 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1930 WARN("Channel name too long.");
1931 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1932 free(content);
1933 goto end;
1934 }
1935
1936 strncpy(channel->name, (const char *) content, name_len);
1937 free(content);
1938 } else if (!strcmp((const char *) attr_node->name,
1939 config_element_enabled)) {
1940 xmlChar *content;
1941 int enabled;
1942
1943 /* enabled */
1944 content = xmlNodeGetContent(attr_node);
1945 if (!content) {
1946 ret = -LTTNG_ERR_NOMEM;
1947 goto end;
1948 }
1949
1950 ret = parse_bool(content, &enabled);
1951 free(content);
1952 if (ret) {
1953 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1954 goto end;
1955 }
1956
1957 channel->enabled = enabled;
1958 } else if (!strcmp((const char *) attr_node->name,
1959 config_element_overwrite_mode)) {
1960 xmlChar *content;
1961
1962 /* overwrite_mode */
1963 content = xmlNodeGetContent(attr_node);
1964 if (!content) {
1965 ret = -LTTNG_ERR_NOMEM;
1966 goto end;
1967 }
1968
1969 ret = get_overwrite_mode(content);
1970 free(content);
1971 if (ret < 0) {
1972 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1973 goto end;
1974 }
1975
1976 channel->attr.overwrite = ret;
1977 } else if (!strcmp((const char *) attr_node->name,
1978 config_element_subbuf_size)) {
1979 xmlChar *content;
1980
1981 /* subbuffer_size */
1982 content = xmlNodeGetContent(attr_node);
1983 if (!content) {
1984 ret = -LTTNG_ERR_NOMEM;
1985 goto end;
1986 }
1987
1988 ret = parse_uint(content, &channel->attr.subbuf_size);
1989 free(content);
1990 if (ret) {
1991 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1992 goto end;
1993 }
1994 } else if (!strcmp((const char *) attr_node->name,
1995 config_element_num_subbuf)) {
1996 xmlChar *content;
1997
1998 /* subbuffer_count */
1999 content = xmlNodeGetContent(attr_node);
2000 if (!content) {
2001 ret = -LTTNG_ERR_NOMEM;
2002 goto end;
2003 }
2004
2005 ret = parse_uint(content, &channel->attr.num_subbuf);
2006 free(content);
2007 if (ret) {
2008 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2009 goto end;
2010 }
2011 } else if (!strcmp((const char *) attr_node->name,
2012 config_element_switch_timer_interval)) {
2013 xmlChar *content;
2014 uint64_t switch_timer_interval = 0;
2015
2016 /* switch_timer_interval */
2017 content = xmlNodeGetContent(attr_node);
2018 if (!content) {
2019 ret = -LTTNG_ERR_NOMEM;
2020 goto end;
2021 }
2022
2023 ret = parse_uint(content, &switch_timer_interval);
2024 free(content);
2025 if (ret) {
2026 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2027 goto end;
2028 }
2029
2030 if (switch_timer_interval > UINT_MAX) {
2031 WARN("switch_timer_interval out of range.");
2032 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2033 goto end;
2034 }
2035
2036 channel->attr.switch_timer_interval =
2037 switch_timer_interval;
2038 } else if (!strcmp((const char *) attr_node->name,
2039 config_element_read_timer_interval)) {
2040 xmlChar *content;
2041 uint64_t read_timer_interval = 0;
2042
2043 /* read_timer_interval */
2044 content = xmlNodeGetContent(attr_node);
2045 if (!content) {
2046 ret = -LTTNG_ERR_NOMEM;
2047 goto end;
2048 }
2049
2050 ret = parse_uint(content, &read_timer_interval);
2051 free(content);
2052 if (ret) {
2053 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2054 goto end;
2055 }
2056
2057 if (read_timer_interval > UINT_MAX) {
2058 WARN("read_timer_interval out of range.");
2059 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2060 goto end;
2061 }
2062
2063 channel->attr.read_timer_interval =
2064 read_timer_interval;
2065 } else if (!strcmp((const char *) attr_node->name,
2066 config_element_output_type)) {
2067 xmlChar *content;
2068
2069 /* output_type */
2070 content = xmlNodeGetContent(attr_node);
2071 if (!content) {
2072 ret = -LTTNG_ERR_NOMEM;
2073 goto end;
2074 }
2075
2076 ret = get_output_type(content);
2077 free(content);
2078 if (ret < 0) {
2079 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2080 goto end;
2081 }
2082
2083 channel->attr.output = ret;
2084 } else if (!strcmp((const char *) attr_node->name,
2085 config_element_tracefile_size)) {
2086 xmlChar *content;
2087
2088 /* tracefile_size */
2089 content = xmlNodeGetContent(attr_node);
2090 if (!content) {
2091 ret = -LTTNG_ERR_NOMEM;
2092 goto end;
2093 }
2094
2095 ret = parse_uint(content, &channel->attr.tracefile_size);
2096 free(content);
2097 if (ret) {
2098 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2099 goto end;
2100 }
2101 } else if (!strcmp((const char *) attr_node->name,
2102 config_element_tracefile_count)) {
2103 xmlChar *content;
2104
2105 /* tracefile_count */
2106 content = xmlNodeGetContent(attr_node);
2107 if (!content) {
2108 ret = -LTTNG_ERR_NOMEM;
2109 goto end;
2110 }
2111
2112 ret = parse_uint(content, &channel->attr.tracefile_count);
2113 free(content);
2114 if (ret) {
2115 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2116 goto end;
2117 }
2118 } else if (!strcmp((const char *) attr_node->name,
2119 config_element_live_timer_interval)) {
2120 xmlChar *content;
2121 uint64_t live_timer_interval = 0;
2122
2123 /* live_timer_interval */
2124 content = xmlNodeGetContent(attr_node);
2125 if (!content) {
2126 ret = -LTTNG_ERR_NOMEM;
2127 goto end;
2128 }
2129
2130 ret = parse_uint(content, &live_timer_interval);
2131 free(content);
2132 if (ret) {
2133 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2134 goto end;
2135 }
2136
2137 if (live_timer_interval > UINT_MAX) {
2138 WARN("live_timer_interval out of range.");
2139 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2140 goto end;
2141 }
2142
2143 channel->attr.live_timer_interval =
2144 live_timer_interval;
2145 } else if (!strcmp((const char *) attr_node->name,
2146 config_element_events)) {
2147 /* events */
2148 *events_node = attr_node;
2149 } else {
2150 /* contexts */
2151 *contexts_node = attr_node;
2152 }
cc255ccd 2153
dcf266c0
JG
2154 ret = 0;
2155end:
2156 return ret;
2157}
2158
2159static
2160int process_context_node(xmlNodePtr context_node,
2161 struct lttng_handle *handle, const char *channel_name)
2162{
2163 int ret;
2164 struct lttng_event_context context;
2165 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2166
2167 assert(handle);
2168 assert(channel_name);
2169
2170 if (!context_child_node) {
2171 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2172 goto end;
2173 }
2174
2175 memset(&context, 0, sizeof(context));
2176
2177 if (!strcmp((const char *) context_child_node->name,
2178 config_element_type)) {
2179 /* type */
2180 xmlChar *content = xmlNodeGetContent(context_child_node);
045fc617 2181
dcf266c0
JG
2182 if (!content) {
2183 ret = -LTTNG_ERR_NOMEM;
2184 goto end;
2185 }
2186
2187 ret = get_context_type(content);
2188 free(content);
2189 if (ret < 0) {
2190 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2191 goto end;
2192 }
2193
2194 context.ctx = ret;
045fc617
JG
2195 } else if (!strcmp((const char *) context_child_node->name,
2196 config_element_context_perf)) {
2197 /* perf */
dcf266c0
JG
2198 xmlNodePtr perf_attr_node;
2199
14ce5bd8
JG
2200 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2201 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2202 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
2203 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2204 perf_attr_node; perf_attr_node =
2205 xmlNextElementSibling(perf_attr_node)) {
2206 if (!strcmp((const char *) perf_attr_node->name,
2207 config_element_type)) {
2208 xmlChar *content;
2209 uint64_t type = 0;
2210
2211 /* type */
2212 content = xmlNodeGetContent(perf_attr_node);
2213 if (!content) {
2214 ret = -LTTNG_ERR_NOMEM;
2215 goto end;
2216 }
2217
2218 ret = parse_uint(content, &type);
2219 free(content);
2220 if (ret) {
2221 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2222 goto end;
2223 }
2224
2225 if (type > UINT32_MAX) {
2226 WARN("perf context type out of range.");
2227 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2228 goto end;
2229 }
2230
2231 context.u.perf_counter.type = type;
2232 } else if (!strcmp((const char *) perf_attr_node->name,
2233 config_element_config)) {
2234 xmlChar *content;
2235 uint64_t config = 0;
2236
2237 /* config */
2238 content = xmlNodeGetContent(perf_attr_node);
2239 if (!content) {
2240 ret = -LTTNG_ERR_NOMEM;
2241 goto end;
2242 }
2243
2244 ret = parse_uint(content, &config);
2245 free(content);
2246 if (ret) {
2247 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2248 goto end;
2249 }
2250
2251 context.u.perf_counter.config = config;
2252 } else if (!strcmp((const char *) perf_attr_node->name,
2253 config_element_name)) {
2254 xmlChar *content;
2255 size_t name_len;
2256
2257 /* name */
2258 content = xmlNodeGetContent(perf_attr_node);
2259 if (!content) {
2260 ret = -LTTNG_ERR_NOMEM;
2261 goto end;
2262 }
2263
2264 name_len = strlen((char *) content);
2265 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2266 WARN("perf context name too long.");
2267 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2268 free(content);
2269 goto end;
2270 }
2271
2272 strncpy(context.u.perf_counter.name, (const char *) content,
2273 name_len);
2274 free(content);
2275 }
2276 }
045fc617
JG
2277 } else if (!strcmp((const char *) context_child_node->name,
2278 config_element_context_app)) {
2279 /* application context */
2280 xmlNodePtr app_ctx_node;
2281
2282 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2283 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2284 app_ctx_node; app_ctx_node =
2285 xmlNextElementSibling(app_ctx_node)) {
2286 xmlChar *content;
2287 char **target = strcmp(
2288 (const char *) app_ctx_node->name,
2289 config_element_context_app_provider_name) == 0 ?
2290 &context.u.app_ctx.provider_name :
2291 &context.u.app_ctx.ctx_name;
2292
2293 content = xmlNodeGetContent(app_ctx_node);
2294 if (!content) {
2295 ret = -LTTNG_ERR_NOMEM;
2296 goto end;
2297 }
2298
2299 *target = (char *) content;
2300 }
2301 } else {
2302 /* Unrecognized context type */
2303 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2304 goto end;
dcf266c0
JG
2305 }
2306
2307 ret = lttng_add_context(handle, &context, NULL, channel_name);
045fc617
JG
2308 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2309 free(context.u.app_ctx.provider_name);
2310 free(context.u.app_ctx.ctx_name);
2311 }
dcf266c0
JG
2312end:
2313 return ret;
2314}
2315
2316static
2317int process_contexts_node(xmlNodePtr contexts_node,
2318 struct lttng_handle *handle, const char *channel_name)
2319{
2320 int ret = 0;
2321 xmlNodePtr context_node;
2322
2323 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2324 context_node = xmlNextElementSibling(context_node)) {
2325 ret = process_context_node(context_node, handle, channel_name);
2326 if (ret) {
2327 goto end;
2328 }
2329 }
2330end:
2331 return ret;
2332}
2333
847a5916
JR
2334static
2335int process_pid_tracker_node(xmlNodePtr pid_tracker_node,
2336 struct lttng_handle *handle)
2337{
dd49e13f 2338 int ret = 0, child;
847a5916
JR
2339 xmlNodePtr targets_node = NULL;
2340 xmlNodePtr node;
2341
2342 assert(handle);
2343 assert(pid_tracker_node);
2344 /* get the targets node */
2345 for (node = xmlFirstElementChild(pid_tracker_node); node;
2346 node = xmlNextElementSibling(node)) {
2347 if (!strcmp((const char *) node->name,
2348 config_element_targets)) {
2349 targets_node = node;
2350 break;
2351 }
2352 }
2353
2354 if (!targets_node) {
2355 ret = LTTNG_ERR_INVALID;
2356 goto end;
2357 }
2358
2359 /* Go through all pid_target node */
2360 child = xmlChildElementCount(targets_node);
2361 if (child == 0) {
2362 /* The session is explicitly set to target nothing. */
2363 ret = lttng_untrack_pid(handle, -1);
2364 if (ret) {
2365 goto end;
2366 }
2367 }
2368 for (node = xmlFirstElementChild(targets_node); node;
2369 node = xmlNextElementSibling(node)) {
2370 xmlNodePtr pid_target_node = node;
2371
2372 /* get pid node and track it */
2373 for (node = xmlFirstElementChild(pid_target_node); node;
2374 node = xmlNextElementSibling(node)) {
2375 if (!strcmp((const char *) node->name,
2376 config_element_pid)) {
2377 int64_t pid;
2378 xmlChar *content = NULL;
2379
2380 content = xmlNodeGetContent(node);
2381 if (!content) {
2382 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2383 goto end;
2384 }
2385
2386 ret = parse_int(content, &pid);
2387 free(content);
2388 if (ret) {
2389 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2390 goto end;
2391 }
2392
2393 ret = lttng_track_pid(handle, (int) pid);
2394 if (ret) {
2395 goto end;
2396 }
2397 }
2398 }
2399 node = pid_target_node;
2400 }
2401
2402end:
2403 return ret;
2404}
2405
2406
dcf266c0 2407static
baa4ef37
JG
2408int process_domain_node(xmlNodePtr domain_node, const char *session_name,
2409 bool snapshot_mode)
dcf266c0
JG
2410{
2411 int ret;
2412 struct lttng_domain domain = { 0 };
2413 struct lttng_handle *handle = NULL;
2414 xmlNodePtr channels_node = NULL;
847a5916
JR
2415 xmlNodePtr trackers_node = NULL;
2416 xmlNodePtr pid_tracker_node = NULL;
dcf266c0
JG
2417 xmlNodePtr node;
2418
2419 assert(session_name);
2420
2421 ret = init_domain(domain_node, &domain);
2422 if (ret) {
2423 goto end;
2424 }
2425
2426 handle = lttng_create_handle(session_name, &domain);
2427 if (!handle) {
2428 ret = -LTTNG_ERR_NOMEM;
2429 goto end;
2430 }
2431
2432 /* get the channels node */
2433 for (node = xmlFirstElementChild(domain_node); node;
2434 node = xmlNextElementSibling(node)) {
2435 if (!strcmp((const char *) node->name,
2436 config_element_channels)) {
2437 channels_node = node;
2438 break;
2439 }
2440 }
2441
2442 if (!channels_node) {
2443 goto end;
2444 }
2445
2446 /* create all channels */
2447 for (node = xmlFirstElementChild(channels_node); node;
2448 node = xmlNextElementSibling(node)) {
2449 struct lttng_channel channel;
2450 xmlNodePtr contexts_node = NULL;
2451 xmlNodePtr events_node = NULL;
2452 xmlNodePtr channel_attr_node;
2453
2454 memset(&channel, 0, sizeof(channel));
2455 lttng_channel_set_default_attr(&domain, &channel.attr);
2456
2457 for (channel_attr_node = xmlFirstElementChild(node);
2458 channel_attr_node; channel_attr_node =
2459 xmlNextElementSibling(channel_attr_node)) {
2460 ret = process_channel_attr_node(channel_attr_node,
baa4ef37
JG
2461 snapshot_mode, domain.type, &channel,
2462 &contexts_node, &events_node);
dcf266c0
JG
2463 if (ret) {
2464 goto end;
2465 }
2466 }
2467
49309f53
JR
2468 if (!channel.name) {
2469 ERR("Encountered a channel with no name attribute.");
2470 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2471 goto end;
2472 }
2473
dcf266c0
JG
2474 ret = lttng_enable_channel(handle, &channel);
2475 if (ret < 0) {
2476 goto end;
2477 }
2478
2479 ret = process_events_node(events_node, handle, channel.name);
2480 if (ret) {
2481 goto end;
2482 }
2483
2484 ret = process_contexts_node(contexts_node, handle,
2485 channel.name);
2486 if (ret) {
2487 goto end;
2488 }
2489 }
847a5916
JR
2490
2491 /* get the trackers node */
2492 for (node = xmlFirstElementChild(domain_node); node;
2493 node = xmlNextElementSibling(node)) {
2494 if (!strcmp((const char *) node->name,
2495 config_element_trackers)) {
2496 trackers_node = node;
2497 break;
2498 }
2499 }
2500
2501 if (!trackers_node) {
2502 goto end;
2503 }
2504
2505 for (node = xmlFirstElementChild(trackers_node); node;
2506 node = xmlNextElementSibling(node)) {
2507 if (!strcmp((const char *)node->name,config_element_pid_tracker)) {
2508 pid_tracker_node = node;
2509 ret = process_pid_tracker_node(pid_tracker_node, handle);
2510 if (ret) {
2511 goto end;
2512 }
2513 }
2514 }
2515
2516 if (!pid_tracker_node) {
2517 lttng_track_pid(handle, -1);
2518 }
2519
dcf266c0
JG
2520end:
2521 lttng_destroy_handle(handle);
2522 return ret;
2523}
2524
2525static
2526int process_session_node(xmlNodePtr session_node, const char *session_name,
2527 int override)
2528{
2529 int ret, started = -1, snapshot_mode = -1;
2530 uint64_t live_timer_interval = UINT64_MAX;
d324faf7 2531 xmlChar *name = NULL;
9e7c9f56 2532 xmlChar *shm_path = NULL;
dcf266c0
JG
2533 xmlNodePtr domains_node = NULL;
2534 xmlNodePtr output_node = NULL;
2535 xmlNodePtr node;
2536 struct lttng_domain *kernel_domain = NULL;
2537 struct lttng_domain *ust_domain = NULL;
2538 struct lttng_domain *jul_domain = NULL;
5cdb6027 2539 struct lttng_domain *log4j_domain = NULL;
0e115563 2540 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
2541
2542 for (node = xmlFirstElementChild(session_node); node;
2543 node = xmlNextElementSibling(node)) {
2544 if (!name && !strcmp((const char *) node->name,
2545 config_element_name)) {
2546 /* name */
2547 xmlChar *node_content = xmlNodeGetContent(node);
2548 if (!node_content) {
2549 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2550 goto error;
dcf266c0
JG
2551 }
2552
d324faf7 2553 name = node_content;
dcf266c0
JG
2554 } else if (!domains_node && !strcmp((const char *) node->name,
2555 config_element_domains)) {
2556 /* domains */
2557 domains_node = node;
2558 } else if (started == -1 && !strcmp((const char *) node->name,
2559 config_element_started)) {
2560 /* started */
2561 xmlChar *node_content = xmlNodeGetContent(node);
2562 if (!node_content) {
2563 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2564 goto error;
dcf266c0
JG
2565 }
2566
2567 ret = parse_bool(node_content, &started);
2568 free(node_content);
2569 if (ret) {
2570 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2571 goto error;
dcf266c0
JG
2572 }
2573 } else if (!output_node && !strcmp((const char *) node->name,
2574 config_element_output)) {
2575 /* output */
2576 output_node = node;
9e7c9f56
JR
2577 } else if (!shm_path && !strcmp((const char *) node->name,
2578 config_element_shared_memory_path)) {
2579 /* shared memory path */
2580 xmlChar *node_content = xmlNodeGetContent(node);
2581 if (!node_content) {
2582 ret = -LTTNG_ERR_NOMEM;
2583 goto error;
2584 }
2585
2586 shm_path = node_content;
dcf266c0
JG
2587 } else {
2588 /* attributes, snapshot_mode or live_timer_interval */
2589 xmlNodePtr attributes_child =
2590 xmlFirstElementChild(node);
2591
2592 if (!strcmp((const char *) attributes_child->name,
2593 config_element_snapshot_mode)) {
2594 /* snapshot_mode */
2595 xmlChar *snapshot_mode_content =
2596 xmlNodeGetContent(attributes_child);
2597 if (!snapshot_mode_content) {
2598 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2599 goto error;
dcf266c0
JG
2600 }
2601
2602 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2603 free(snapshot_mode_content);
2604 if (ret) {
2605 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2606 goto error;
dcf266c0
JG
2607 }
2608 } else {
2609 /* live_timer_interval */
2610 xmlChar *timer_interval_content =
2611 xmlNodeGetContent(attributes_child);
2612 if (!timer_interval_content) {
2613 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2614 goto error;
dcf266c0
JG
2615 }
2616
2617 ret = parse_uint(timer_interval_content, &live_timer_interval);
2618 free(timer_interval_content);
2619 if (ret) {
2620 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2621 goto error;
dcf266c0
JG
2622 }
2623 }
2624 }
2625 }
2626
2627 if (!name) {
2628 /* Mandatory attribute, as defined in the session XSD */
2629 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2630 goto error;
dcf266c0
JG
2631 }
2632
d324faf7 2633 if (session_name && strcmp((char *) name, session_name)) {
dcf266c0 2634 /* This is not the session we are looking for */
c2da8cde
DG
2635 ret = -LTTNG_ERR_NO_SESSION;
2636 goto error;
dcf266c0
JG
2637 }
2638
2639 /* Init domains to create the session handles */
2640 for (node = xmlFirstElementChild(domains_node); node;
2641 node = xmlNextElementSibling(node)) {
2642 struct lttng_domain *domain;
2643
2644 domain = zmalloc(sizeof(*domain));
2645 if (!domain) {
2646 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2647 goto error;
dcf266c0
JG
2648 }
2649
2650 ret = init_domain(node, domain);
2651 if (ret) {
2652 goto domain_init_error;
2653 }
2654
2655 switch (domain->type) {
2656 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
2657 if (kernel_domain) {
2658 /* Same domain seen twice, invalid! */
2659 goto domain_init_error;
2660 }
dcf266c0
JG
2661 kernel_domain = domain;
2662 break;
2663 case LTTNG_DOMAIN_UST:
c33e6729
DG
2664 if (ust_domain) {
2665 /* Same domain seen twice, invalid! */
2666 goto domain_init_error;
2667 }
dcf266c0
JG
2668 ust_domain = domain;
2669 break;
2670 case LTTNG_DOMAIN_JUL:
c33e6729
DG
2671 if (jul_domain) {
2672 /* Same domain seen twice, invalid! */
2673 goto domain_init_error;
2674 }
dcf266c0
JG
2675 jul_domain = domain;
2676 break;
5cdb6027
DG
2677 case LTTNG_DOMAIN_LOG4J:
2678 if (log4j_domain) {
2679 /* Same domain seen twice, invalid! */
2680 goto domain_init_error;
2681 }
2682 log4j_domain = domain;
2683 break;
0e115563
DG
2684 case LTTNG_DOMAIN_PYTHON:
2685 if (python_domain) {
2686 /* Same domain seen twice, invalid! */
2687 goto domain_init_error;
2688 }
2689 python_domain = domain;
2690 break;
dcf266c0
JG
2691 default:
2692 WARN("Invalid domain type");
2693 goto domain_init_error;
2694 }
2695 continue;
2696domain_init_error:
2697 free(domain);
2698 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2699 goto error;
dcf266c0
JG
2700 }
2701
2702 if (override) {
2703 /* Destroy session if it exists */
d324faf7 2704 ret = lttng_destroy_session((const char *) name);
dcf266c0
JG
2705 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2706 ERR("Failed to destroy existing session.");
c2da8cde 2707 goto error;
dcf266c0
JG
2708 }
2709 }
2710
2711 /* Create session type depending on output type */
2712 if (snapshot_mode && snapshot_mode != -1) {
d324faf7 2713 ret = create_snapshot_session((const char *) name, output_node);
dcf266c0
JG
2714 } else if (live_timer_interval &&
2715 live_timer_interval != UINT64_MAX) {
d324faf7
JG
2716 ret = create_session((const char *) name, kernel_domain,
2717 ust_domain, jul_domain, log4j_domain,
2718 output_node, live_timer_interval);
dcf266c0
JG
2719 } else {
2720 /* regular session */
d324faf7
JG
2721 ret = create_session((const char *) name, kernel_domain,
2722 ust_domain, jul_domain, log4j_domain,
2723 output_node, UINT64_MAX);
dcf266c0 2724 }
dcf266c0 2725 if (ret) {
c2da8cde 2726 goto error;
dcf266c0
JG
2727 }
2728
9e7c9f56
JR
2729 if (shm_path) {
2730 ret = lttng_set_session_shm_path((const char *) name,
d324faf7 2731 (const char *) shm_path);
9e7c9f56
JR
2732 if (ret) {
2733 goto error;
2734 }
2735 }
2736
dcf266c0
JG
2737 for (node = xmlFirstElementChild(domains_node); node;
2738 node = xmlNextElementSibling(node)) {
baa4ef37
JG
2739 ret = process_domain_node(node, (const char *) name,
2740 snapshot_mode == 1);
dcf266c0
JG
2741 if (ret) {
2742 goto end;
2743 }
2744 }
2745
2746 if (started) {
d324faf7 2747 ret = lttng_start_tracing((const char *) name);
dcf266c0
JG
2748 if (ret) {
2749 goto end;
2750 }
2751 }
c2da8cde 2752
dcf266c0 2753end:
b2579dc1 2754 if (ret < 0) {
d324faf7
JG
2755 ERR("Failed to load session %s: %s", (const char *) name,
2756 lttng_strerror(ret));
2757 lttng_destroy_session((const char *) name);
b2579dc1
JG
2758 }
2759
c2da8cde 2760error:
dcf266c0
JG
2761 free(kernel_domain);
2762 free(ust_domain);
2763 free(jul_domain);
5cdb6027 2764 free(log4j_domain);
135a3893 2765 free(python_domain);
d324faf7 2766 xmlFree(name);
9e7c9f56 2767 xmlFree(shm_path);
dcf266c0
JG
2768 return ret;
2769}
2770
cf53c06d
DG
2771/*
2772 * Return 1 if the given path is readable by the current UID or 0 if not.
2773 * Return -1 if the path is EPERM.
2774 */
2775static int validate_file_read_creds(const char *path)
2776{
2777 int ret;
2778
2779 assert(path);
2780
2781 /* Can we read the file. */
2782 ret = access(path, R_OK);
2783 if (!ret) {
2784 goto valid;
2785 }
2786 if (errno == EACCES) {
2787 return -1;
2788 } else {
2789 /* Invalid. */
2790 return 0;
2791 }
2792valid:
2793 return 1;
2794}
2795
dcf266c0
JG
2796static
2797int load_session_from_file(const char *path, const char *session_name,
2798 struct session_config_validation_ctx *validation_ctx, int override)
2799{
2800 int ret, session_found = !session_name;
2801 xmlDocPtr doc = NULL;
2802 xmlNodePtr sessions_node;
2803 xmlNodePtr session_node;
dcf266c0
JG
2804
2805 assert(path);
2806 assert(validation_ctx);
2807
cf53c06d
DG
2808 ret = validate_file_read_creds(path);
2809 if (ret != 1) {
2810 if (ret == -1) {
2811 ret = -LTTNG_ERR_EPERM;
2812 } else {
2813 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2814 }
dcf266c0
JG
2815 goto end;
2816 }
2817
2818 doc = xmlParseFile(path);
2819 if (!doc) {
2820 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2821 goto end;
2822 }
2823
2824 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2825 if (ret) {
2826 ERR("Session configuration file validation failed");
2827 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2828 goto end;
2829 }
2830
2831 sessions_node = xmlDocGetRootElement(doc);
2832 if (!sessions_node) {
2833 goto end;
2834 }
2835
2836 for (session_node = xmlFirstElementChild(sessions_node);
2837 session_node; session_node =
2838 xmlNextElementSibling(session_node)) {
2839 ret = process_session_node(session_node,
2840 session_name, override);
2841 if (session_name && ret == 0) {
2842 /* Target session found and loaded */
2843 session_found = 1;
2844 break;
2845 }
2846 }
2847end:
2848 xmlFreeDoc(doc);
2849 if (!ret) {
a96bc65d 2850 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
2851 }
2852 return ret;
2853}
2854
fa8b52a2
JR
2855static
2856int load_session_from_document(struct config_document *document, const char *session_name,
2857 struct session_config_validation_ctx *validation_ctx, int override)
2858{
2859 int ret, session_found = !session_name;
2860 xmlNodePtr sessions_node;
2861 xmlNodePtr session_node;
2862
2863 assert(validation_ctx);
2864
2865 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, document->document);
2866 if (ret) {
2867 ERR("Session configuration file validation failed");
2868 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2869 goto end;
2870 }
2871
2872 sessions_node = xmlDocGetRootElement(document->document);
2873 if (!sessions_node) {
2874 goto end;
2875 }
2876
2877 for (session_node = xmlFirstElementChild(sessions_node);
2878 session_node; session_node =
2879 xmlNextElementSibling(session_node)) {
2880 ret = process_session_node(session_node,
2881 session_name, override);
2882 if (session_name && ret == 0) {
2883 /* Target session found and loaded */
2884 session_found = 1;
2885 break;
2886 }
2887 }
2888end:
2889 if (!ret) {
2890 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
2891 }
2892 return ret;
2893}
2894
60f70355
JG
2895/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2896static
2897struct dirent *alloc_dirent(const char *path)
2898{
2899 size_t len;
2900 long name_max;
2901 struct dirent *entry;
2902
2903 name_max = pathconf(path, _PC_NAME_MAX);
2904 if (name_max == -1) {
2905 name_max = PATH_MAX;
2906 }
2907 len = offsetof(struct dirent, d_name) + name_max + 1;
2908 entry = zmalloc(len);
2909 return entry;
2910}
2911
dcf266c0
JG
2912static
2913int load_session_from_path(const char *path, const char *session_name,
2914 struct session_config_validation_ctx *validation_ctx, int override)
2915{
2916 int ret, session_found = !session_name;
dcf266c0
JG
2917 DIR *directory = NULL;
2918
2919 assert(path);
2920 assert(validation_ctx);
2921
4af16958
DG
2922 directory = opendir(path);
2923 if (!directory) {
11143783
DG
2924 switch (errno) {
2925 case ENOTDIR:
0f0a81b5
DG
2926 /* Try the file loading. */
2927 break;
11143783
DG
2928 case ENOENT:
2929 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2930 goto end;
2931 default:
0f0a81b5
DG
2932 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2933 goto end;
4af16958 2934 }
dcf266c0 2935 }
4af16958 2936 if (directory) {
dcf266c0
JG
2937 struct dirent *entry;
2938 struct dirent *result;
2939 char *file_path = NULL;
2940 size_t path_len = strlen(path);
2941
2942 if (path_len >= PATH_MAX) {
2943 ret = -LTTNG_ERR_INVALID;
2944 goto end;
2945 }
2946
60f70355 2947 entry = alloc_dirent(path);
dcf266c0
JG
2948 if (!entry) {
2949 ret = -LTTNG_ERR_NOMEM;
2950 goto end;
2951 }
2952
dcf266c0
JG
2953 file_path = zmalloc(PATH_MAX);
2954 if (!file_path) {
2955 ret = -LTTNG_ERR_NOMEM;
2956 free(entry);
2957 goto end;
2958 }
2959
2960 strncpy(file_path, path, path_len);
2961 if (file_path[path_len - 1] != '/') {
2962 file_path[path_len++] = '/';
2963 }
2964
4af16958 2965 ret = 0;
dcf266c0
JG
2966 /* Search for *.lttng files */
2967 while (!readdir_r(directory, entry, &result) && result) {
2968 size_t file_name_len = strlen(result->d_name);
2969
2970 if (file_name_len <=
2971 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2972 continue;
2973 }
2974
431f702e 2975 if (path_len + file_name_len >= PATH_MAX) {
dcf266c0
JG
2976 continue;
2977 }
2978
2979 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2980 result->d_name + file_name_len - sizeof(
2981 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2982 continue;
2983 }
2984
2985 strncpy(file_path + path_len, result->d_name, file_name_len);
2986 file_path[path_len + file_name_len] = '\0';
2987
2988 ret = load_session_from_file(file_path, session_name,
2989 validation_ctx, override);
2990 if (session_name && !ret) {
2991 session_found = 1;
2992 break;
2993 }
2994 }
2995
2996 free(entry);
2997 free(file_path);
2998 } else {
2999 ret = load_session_from_file(path, session_name,
3000 validation_ctx, override);
3001 if (ret) {
3002 goto end;
3003 } else {
3004 session_found = 1;
3005 }
3006 }
3007
3008end:
3009 if (directory) {
3010 if (closedir(directory)) {
3011 PERROR("closedir");
3012 }
3013 }
3014
6c66fa0f
JG
3015 if (session_found) {
3016 ret = 0;
dcf266c0
JG
3017 }
3018
3019 return ret;
3020}
3021
ab38c13f
DG
3022/*
3023 * Validate that the given path's credentials and the current process have the
cf53c06d 3024 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
3025 */
3026static int validate_path_creds(const char *path)
3027{
3028 int ret, uid = getuid();
3029 struct stat buf;
3030
3031 assert(path);
3032
cf53c06d 3033 if (uid == 0) {
ab38c13f
DG
3034 goto valid;
3035 }
3036
3037 ret = stat(path, &buf);
3038 if (ret < 0) {
3039 if (errno != ENOENT) {
3040 PERROR("stat");
3041 }
3042 ret = -LTTNG_ERR_INVALID;
3043 goto valid;
3044 }
3045
3046 if (buf.st_uid != uid) {
3047 goto invalid;
3048 }
3049
3050valid:
ab38c13f 3051 return 1;
cf53c06d
DG
3052invalid:
3053 return 0;
ab38c13f
DG
3054}
3055
dcf266c0
JG
3056LTTNG_HIDDEN
3057int config_load_session(const char *path, const char *session_name,
ab38c13f 3058 int override, unsigned int autoload)
dcf266c0
JG
3059{
3060 int ret;
6c66fa0f 3061 bool session_loaded = false;
cf53c06d 3062 const char *path_ptr = NULL;
dcf266c0
JG
3063 struct session_config_validation_ctx validation_ctx = { 0 };
3064
3065 ret = init_session_config_validation_ctx(&validation_ctx);
3066 if (ret) {
3067 goto end;
3068 }
3069
3070 if (!path) {
ab38c13f
DG
3071 char *home_path;
3072 const char *sys_path;
3073
dcf266c0 3074 /* Try home path */
ab38c13f 3075 home_path = utils_get_home_dir();
dcf266c0 3076 if (home_path) {
ab38c13f 3077 char path[PATH_MAX];
dcf266c0 3078
d4fcf703
DG
3079 /*
3080 * Try user session configuration path. Ignore error here so we can
3081 * continue loading the system wide sessions.
3082 */
ab38c13f
DG
3083 if (autoload) {
3084 ret = snprintf(path, sizeof(path),
3085 DEFAULT_SESSION_HOME_CONFIGPATH "/"
3086 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
3087 if (ret < 0) {
3088 PERROR("snprintf session autoload home config path");
3089 goto end;
3090 }
3091
3092 /*
3093 * Credentials are only validated for the autoload in order to
3094 * avoid any user session daemon to try to load kernel sessions
3095 * automatically and failing all the times.
3096 */
3097 ret = validate_path_creds(path);
3098 if (ret) {
3099 path_ptr = path;
3100 }
ab38c13f
DG
3101 } else {
3102 ret = snprintf(path, sizeof(path),
3103 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
3104 if (ret < 0) {
3105 PERROR("snprintf session home config path");
3106 goto end;
3107 }
3108 path_ptr = path;
ab38c13f 3109 }
cf53c06d
DG
3110 if (path_ptr) {
3111 ret = load_session_from_path(path_ptr, session_name,
d4fcf703
DG
3112 &validation_ctx, override);
3113 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3114 goto end;
3115 }
3116 /*
3117 * Continue even if the session was found since we have to try
3118 * the system wide sessions.
3119 */
6c66fa0f 3120 session_loaded = true;
ab38c13f 3121 }
d4fcf703 3122 }
ab38c13f 3123
cf53c06d
DG
3124 /* Reset path pointer for the system wide dir. */
3125 path_ptr = NULL;
3126
d4fcf703
DG
3127 /* Try system wide configuration directory. */
3128 if (autoload) {
3129 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
3130 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
3131 ret = validate_path_creds(sys_path);
3132 if (ret) {
3133 path_ptr = sys_path;
3134 }
d4fcf703 3135 } else {
cf53c06d
DG
3136 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
3137 path_ptr = sys_path;
d4fcf703
DG
3138 }
3139
cf53c06d
DG
3140 if (path_ptr) {
3141 ret = load_session_from_path(path_ptr, session_name,
d4fcf703 3142 &validation_ctx, override);
6c66fa0f
JG
3143 if (!ret) {
3144 session_loaded = true;
3145 }
dcf266c0
JG
3146 }
3147 } else {
3148 ret = access(path, F_OK);
3149 if (ret < 0) {
3150 PERROR("access");
3151 switch (errno) {
3152 case ENOENT:
3153 ret = -LTTNG_ERR_INVALID;
3154 WARN("Session configuration path does not exist.");
3155 break;
3156 case EACCES:
3157 ret = -LTTNG_ERR_EPERM;
3158 break;
3159 default:
3160 ret = -LTTNG_ERR_UNK;
3161 break;
3162 }
3163 goto end;
3164 }
3165
3166 ret = load_session_from_path(path, session_name,
3167 &validation_ctx, override);
3168 }
3169end:
3170 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
3171 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
3172 /*
3173 * Don't report an error if no sessions are found when called
3174 * without a session_name or a search path.
3175 */
3176 ret = 0;
3177 }
6c66fa0f
JG
3178
3179 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
3180 /* A matching session was found in one of the search paths. */
3181 ret = 0;
3182 }
dcf266c0
JG
3183 return ret;
3184}
56766c75
JG
3185
3186static
3187void __attribute__((destructor)) session_config_exit(void)
3188{
3189 xmlCleanupParser();
3190}
fa8b52a2
JR
3191
3192LTTNG_HIDDEN
3193struct config_document *config_document_get(const char *path)
3194{
3195 int ret;
3196 struct config_document *document = NULL;
3197 struct session_config_validation_ctx validation_ctx = { 0 };
3198
3199 assert(path);
3200
3201 ret = access(path, F_OK);
3202 if (ret < 0) {
3203 PERROR("access");
3204 switch (errno) {
3205 case ENOENT:
3206 ERR("Configuration document path does not exist.");
3207 break;
3208 case EACCES:
3209 ERR("Configuration document permission denied.");
3210 break;
3211 default:
3212 ERR("Configuration document unknown error.");
3213 break;
3214 }
3215 goto end;
3216 }
3217
3218 ret = init_session_config_validation_ctx(&validation_ctx);
3219 if (ret) {
3220 goto end;
3221 }
3222
3223 ret = validate_file_read_creds(path);
3224 if (ret != 1) {
3225 if (ret == -1) {
3226 ERR("Configuration document permission denied.");
3227 } else {
3228 ERR("Configuration document does not exist.");
3229 }
3230 goto end;
3231 }
3232
3233 document = zmalloc(sizeof(struct config_document));
3234 if (!document) {
3235 PERROR("zmalloc");
3236 goto end;
3237 }
3238
2635a014
JR
3239 /*
3240 * Do not care of blanks and also make sure that direct dump have no
3241 * indentations in them
3242 */
3243 xmlKeepBlanksDefault(0);
fa8b52a2
JR
3244 document->document = xmlParseFile(path);
3245 if (!document->document) {
3246 ERR("Configuration document parsing failed.");
3247 goto error;
3248 }
3249
3250 ret = xmlSchemaValidateDoc(validation_ctx.schema_validation_ctx,
3251 document->document);
3252 if (ret) {
3253 ERR("Session configuration file validation failed");
3254 goto error;
3255 }
3256
3257end:
4640a526 3258 fini_session_config_validation_ctx(&validation_ctx);
fa8b52a2
JR
3259 return document;
3260error:
3261 xmlFreeDoc(document->document);
3262 free(document);
3263 return NULL;
3264}
3265
3266LTTNG_HIDDEN
3267void config_document_free(struct config_document *document)
3268{
3269 if (document) {
3270 xmlFreeDoc(document->document);
3271 document->document = NULL;
3272 }
4640a526
JR
3273
3274 free(document);
fa8b52a2
JR
3275}
3276
3277LTTNG_HIDDEN
3278int config_document_replace_element_value(struct config_document *document,
3279 const char *xpath, const char *value)
3280{
4640a526 3281 int ret = 0;
fa8b52a2
JR
3282 int xpath_result_size;
3283
3284 xmlXPathContextPtr xpath_context = NULL;
3285 xmlXPathObjectPtr xpath_object = NULL;
3286 xmlNodeSetPtr xpath_result_set = NULL;
3287 xmlChar *internal_xpath = NULL;
3288 xmlChar *internal_value = NULL;
3289
3290 assert(document);
3291 assert(xpath);
3292 assert(value);
3293
3294 internal_xpath = encode_string(xpath);
3295 if (!internal_xpath) {
3296 ret = -LTTNG_ERR_NOMEM;
3297 ERR("Unable to encode xpath string");
3298 goto end;
3299 }
3300
3301 internal_value = encode_string(value);
3302 if (!internal_value) {
3303 ret = -LTTNG_ERR_NOMEM;
3304 ERR("Unable to encode xpath replace value string");
3305 goto end;
3306 }
3307
3308 /* Initialize xpath context */
3309 xpath_context = xmlXPathNewContext(document->document);
3310 if (!xpath_context) {
3311 ret = -LTTNG_ERR_NOMEM;
3312 ERR("Unable to create xpath context");
3313 goto end;
3314 }
3315
3316 /* Evaluate de xpath expression */
3317 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3318 if (!xpath_object) {
3319 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3320 goto end;
3321 }
3322
3323 /* TODO: from here could be extracted and previopus could be a step in
3324 * subsequent operation, modify/substitute/delete node.
3325 */
3326 xpath_result_set = xpath_object->nodesetval;
3327 if (!xpath_result_set) {
3328 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3329 goto end;
3330 }
3331
3332 xpath_result_size = xpath_result_set->nodeNr;
3333
3334 /*
3335 * Reverse traversal since last element could be nested under parent
3336 * element present in the result set.
3337 */
3338 for (int i = xpath_result_size - 1; i >=0; i--) {
3339 assert(xpath_result_set->nodeTab[i]);
3340 xmlNodeSetContent(xpath_result_set->nodeTab[i], internal_value);
3341
3342 /*
3343 * Libxml2 quirk regarding the freing and namesplace node see
3344 * libxml2 example and documentation for more details.
3345 */
3346 if (xpath_result_set->nodeTab[i]->type != XML_NAMESPACE_DECL) {
3347 xpath_result_set->nodeTab[i] = NULL;
3348 }
3349 }
3350
3351end:
3352 xmlXPathFreeContext(xpath_context);
3353 xmlXPathFreeObject(xpath_object);
3354 xmlFree(internal_xpath);
3355 xmlFree(internal_value);
3356 return ret;
3357}
3358
3359LTTNG_HIDDEN
3360int config_load_configuration_sessions(struct config_document *document,
3361 const char *session_name, int override)
3362{
3363 int ret;
3364 struct session_config_validation_ctx validation_ctx = { 0 };
3365
3366 ret = init_session_config_validation_ctx(&validation_ctx);
3367 if (ret) {
3368 goto end;
3369 }
3370 ret = load_session_from_document(document, session_name,
3371 &validation_ctx, override);
3372end:
4640a526 3373 fini_session_config_validation_ctx(&validation_ctx);
fa8b52a2
JR
3374 return ret;
3375}
3376
3377LTTNG_HIDDEN
3378int config_document_replace_element(struct config_document *document,
3379 const char *xpath, const struct config_element *element)
3380{
3381 int ret = 0;
3382 int xpath_result_size;
3383
3384 xmlXPathContextPtr xpath_context = NULL;
3385 xmlXPathObjectPtr xpath_object = NULL;
3386 xmlNodeSetPtr xpath_result_set = NULL;
3387 xmlChar *internal_xpath = NULL;
3388 xmlNodePtr old_node = NULL;
3389 xmlNodePtr copy = NULL;
3390
3391 assert(document);
3392 assert(xpath);
3393 assert(element);
3394 assert(element->element);
3395
3396 internal_xpath = encode_string(xpath);
3397 if (!internal_xpath) {
3398 ret = -LTTNG_ERR_NOMEM;
3399 ERR("Unable to encode xpath string");
3400 goto end;
3401 }
3402
3403 /* Initialize xpath context */
3404 xpath_context = xmlXPathNewContext(document->document);
3405 if (!xpath_context) {
3406 ret = -LTTNG_ERR_NOMEM;
3407 ERR("Unable to create xpath context");
3408 goto end;
3409 }
3410
3411 /* Evaluate the xpath expression */
3412 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3413 if (!xpath_object) {
3414 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3415 goto end;
3416 }
3417
3418 xpath_result_set = xpath_object->nodesetval;
3419 if (!xpath_result_set) {
3420 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3421 goto end;
3422 }
3423
3424 xpath_result_size = xpath_result_set->nodeNr;
3425
3426 if (xpath_result_size > 1) {
3427 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3428 goto end;
3429 }
3430
3431 if (xpath_result_size == 0) {
4ba28db3 3432 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
fa8b52a2
JR
3433 goto end;
3434 }
3435
3436 assert(xpath_result_set->nodeTab[0]);
3437
3438 /* Do a copy of the element to ease caller memory management */
3439 copy = xmlCopyNode(element->element, 1);
3440 if (!copy) {
3441 ret = -LTTNG_ERR_NOMEM;
fa8b52a2
JR
3442 goto end;
3443 }
3444
fa8b52a2 3445 old_node = xmlReplaceNode(xpath_result_set->nodeTab[0], copy);
4640a526
JR
3446 if (xpath_result_set->nodeTab[0]->type != XML_NAMESPACE_DECL)
3447 xpath_result_set->nodeTab[0] = NULL;
fa8b52a2
JR
3448 if (!old_node) {
3449 ret = -LTTNG_ERR_CONFIG_REPLACEMENT;
3450 xmlFreeNode(copy);
3451 goto end;
3452 }
4640a526
JR
3453 xmlUnlinkNode(old_node);
3454 xmlFreeNode(old_node);
fa8b52a2
JR
3455end:
3456 xmlXPathFreeContext(xpath_context);
3457 xmlXPathFreeObject(xpath_object);
3458 xmlFree(internal_xpath);
3459 return ret;
3460}
3461
3462LTTNG_HIDDEN
3463char *config_document_get_element_value(struct config_document *document,
3464 const char *xpath)
3465{
3466 char *value = NULL;
3467 int xpath_result_size;
3468 int value_result_size;
3469
3470 xmlXPathContextPtr xpath_context = NULL;
3471 xmlXPathObjectPtr xpath_object = NULL;
3472 xmlNodeSetPtr xpath_result_set = NULL;
3473 xmlChar *internal_xpath = NULL;
3474 xmlChar *internal_value = NULL;
3475
3476 assert(document);
3477 assert(xpath);
3478
3479 internal_xpath = encode_string(xpath);
3480 if (!internal_xpath) {
3481 value = NULL;
3482 ERR("Unable to encode xpath string");
3483 goto end;
3484 }
3485
3486 /* Initialize xpath context */
3487 xpath_context = xmlXPathNewContext(document->document);
3488 if (!xpath_context) {
3489 value = NULL;
3490 ERR("Unable to create xpath context");
3491 goto end;
3492 }
3493
3494 /* Evaluate the xpath expression */
3495 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3496 if (!xpath_object) {
3497 value = NULL;
3498 ERR("Unable to evaluate xpath query (invalid query format)");
3499 goto end;
3500 }
3501
3502 xpath_result_set = xpath_object->nodesetval;
3503 if (!xpath_result_set) {
3504 value = NULL;
3505 goto end;
3506 }
3507
3508 xpath_result_size = xpath_result_set->nodeNr;
3509
3510 if (xpath_result_size > 1) {
3511 ERR("To many result while fetching config element value");
3512 value = NULL;
3513 goto end;
3514 }
3515
3516 if (xpath_result_size == 0) {
3517 value = NULL;
3518 goto end;
3519 }
3520
3521 /*
3522 * Reverse traversal since last element could be nested under parent
3523 * element present in the result set.
3524 */
3525 assert(xpath_result_set->nodeTab[0]);
3526 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
3527 if (!internal_value) {
3528 value = NULL;
3529 goto end;
3530 }
3531
3532 value_result_size = xmlStrlen(internal_value);
3533 value = calloc(value_result_size + 1, sizeof(char));
3534 strncpy(value, (char *) internal_value, value_result_size);
3535
3536end:
3537 xmlXPathFreeContext(xpath_context);
3538 xmlXPathFreeObject(xpath_object);
3539 xmlFree(internal_xpath);
3540 xmlFree(internal_value);
3541 return value;
3542}
3543
3544LTTNG_HIDDEN
3545int config_document_element_exist(struct config_document *document,
3546 const char *xpath)
3547{
3548 int exist = 0;
3549 int xpath_result_size;
3550
3551 xmlXPathContextPtr xpath_context = NULL;
3552 xmlXPathObjectPtr xpath_object = NULL;
3553 xmlNodeSetPtr xpath_result_set = NULL;
3554 xmlChar *internal_xpath = NULL;
3555
3556 assert(document);
3557 assert(document->document);
3558 assert(xpath);
3559
3560 internal_xpath = encode_string(xpath);
3561 if (!internal_xpath) {
3562 ERR("Unable to encode xpath string");
3563 goto end;
3564 }
3565
3566 /* Initialize xpath context */
3567 xpath_context = xmlXPathNewContext(document->document);
3568 if (!xpath_context) {
3569 ERR("Unable to create xpath context");
3570 goto end;
3571 }
3572
3573 /* Evaluate the xpath expression */
3574 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3575 if (!xpath_object) {
3576 ERR("Unable to evaluate xpath query (invalid query format)");
3577 goto end;
3578 }
3579
3580 xpath_result_set = xpath_object->nodesetval;
3581 if (!xpath_result_set) {
3582 goto end;
3583 }
3584
3585 xpath_result_size = xpath_result_set->nodeNr;
3586
3587 if (xpath_result_size > 0) {
3588 exist = 1;
3589 }
3590end:
3591 xmlXPathFreeContext(xpath_context);
3592 xmlXPathFreeObject(xpath_object);
3593 xmlFree(internal_xpath);
3594 return exist;
3595
3596}
3597
3598LTTNG_HIDDEN
3599struct config_element *config_element_create(const char *name,
3600 const char* value)
3601{
3602 struct config_element *element;
3603
3604 assert(name);
3605
3606 xmlChar *internal_name = NULL;
3607 xmlChar *internal_value = NULL;
3608
3609 internal_name = encode_string(name);
3610 if (!internal_name) {
3611 ERR("Unable to encode config element name string");
3612 element = NULL;
3613 goto end;
3614 }
3615
3616
3617 if (value) {
3618 internal_value = encode_string(value);
3619 if (!internal_value) {
3620 ERR("Unable to encode config_element value string");
3621 element = NULL;
3622 goto end;
3623 }
3624 }
3625
3626 element = zmalloc(sizeof(struct config_element));
3627 if (!element) {
3628 goto end;
3629 }
3630
3631 element->element = xmlNewNode(NULL, internal_name);
3632 if (!element->element) {
3633 free(element);
3634 element = NULL;
00df7c2f 3635 goto end;
fa8b52a2
JR
3636 }
3637
3638 if (internal_value) {
3639 xmlNodeAddContent(element->element, internal_value);
3640 }
3641end:
3642 xmlFree(internal_name);
3643 xmlFree(internal_value);
3644 return element;
3645}
3646
3647LTTNG_HIDDEN
3648int config_element_add_child(struct config_element *parent,
3649 const struct config_element *child)
3650{
3651 assert(parent);
3652 assert(child);
3653 assert(parent->element);
3654 assert(child->element);
3655
3656 int ret = 0;
3657 xmlNodePtr node = NULL;
3658 xmlNodePtr copy = NULL;
3659
3660 /* Do a copy to ease the memory management for caller */
3661 copy = xmlCopyNode(child->element, 1);
3662 if (!copy) {
3663 ret = -LTTNG_ERR_NOMEM;
3664 goto error;
3665 }
3666 node = xmlAddChild(parent->element, copy);
3667 if (!node) {
3668 xmlFreeNode(copy);
3669 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3670 goto error;
3671 }
3672error:
3673 return ret;
3674}
3675
3676
3677LTTNG_HIDDEN
3678int config_document_insert_element(struct config_document *document,
3679 const char *xpath, const struct config_element *element)
3680{
3681 int ret = 0;
3682 int xpath_result_size;
3683
3684 xmlXPathContextPtr xpath_context = NULL;
3685 xmlXPathObjectPtr xpath_object = NULL;
3686 xmlNodeSetPtr xpath_result_set = NULL;
3687 xmlChar *internal_xpath = NULL;
3688 xmlNodePtr child_node = NULL;
3689 xmlNodePtr local_copy = NULL;
3690
3691 assert(document);
3692 assert(xpath);
3693 assert(element);
3694 assert(element->element);
3695
3696 internal_xpath = encode_string(xpath);
3697 if (!internal_xpath) {
3698 ret = -LTTNG_ERR_NOMEM;
3699 goto end;
3700 }
3701
3702 /* Initialize xpath context */
3703 xpath_context = xmlXPathNewContext(document->document);
3704 if (!xpath_context) {
3705 ret = -LTTNG_ERR_NOMEM;
3706 goto end;
3707 }
3708
3709 /* Evaluate the xpath expression */
3710 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3711 if (!xpath_object) {
3712 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3713 goto end;
3714 }
3715
3716 xpath_result_set = xpath_object->nodesetval;
3717 if (!xpath_result_set) {
3718 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3719 goto end;
3720 }
3721
3722 xpath_result_size = xpath_result_set->nodeNr;
3723
3724 if (xpath_result_size > 1) {
3725 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3726 goto end;
3727 }
3728
3729 if (xpath_result_size == 0) {
3730 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3731 goto end;
3732 }
3733
3734 assert(xpath_result_set->nodeTab[0]);
3735 /* Do a copy to simply memory management */
3736 local_copy = xmlCopyNode(element->element, 1);
3737 if (!local_copy) {
3738 ret = -LTTNG_ERR_NOMEM;
fa8b52a2
JR
3739 goto end;
3740 }
3741
3742 child_node = xmlAddChild(xpath_result_set->nodeTab[0], local_copy);
3743 if (!child_node) {
3744 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3745 xmlFreeNode(local_copy);
3746 goto end;
3747 }
3748end:
3749 xmlXPathFreeContext(xpath_context);
3750 xmlXPathFreeObject(xpath_object);
3751 xmlFree(internal_xpath);
3752 return ret;
3753}
3754
3755LTTNG_HIDDEN
3756void config_element_free(struct config_element *element) {
4640a526
JR
3757 if (element && element->element) {
3758 xmlFreeNode(element->element);
fa8b52a2
JR
3759 }
3760
3761 free(element);
3762}
f7962e8f
JR
3763
3764LTTNG_HIDDEN
3765void config_element_free_array(struct config_element **array, int size) {
3766
3767 if (array) {
3768 for (int i = 0; i < size; i++) {
3769 if (!array[i]) {
3770 continue;
3771 }
3772 if (array[i]->element) {
3773 xmlFreeNode(array[i]->element);
3774 }
3775 free(array[i]);
3776 }
3777 }
3778 free(array);
3779}
3780
3781LTTNG_HIDDEN
3782void config_document_get_element_array(const struct config_document *document, const char *xpath, struct config_element ***element_array, int *array_size)
3783{
3784 int xpath_result_size;
3785
3786 xmlXPathContextPtr xpath_context = NULL;
3787 xmlXPathObjectPtr xpath_object = NULL;
3788 xmlNodeSetPtr xpath_result_set = NULL;
3789 xmlChar *internal_xpath = NULL;
3790 xmlChar *internal_value = NULL;
3791
3792 assert(document);
3793 assert(xpath);
3794
3795 *element_array=NULL;
3796 *array_size=0;
3797
3798
3799 internal_xpath = encode_string(xpath);
3800 if (!internal_xpath) {
3801 ERR("Unable to encode xpath string");
3802 goto end;
3803 }
3804
3805 /* Initialize xpath context */
3806 xpath_context = xmlXPathNewContext(document->document);
3807 if (!xpath_context) {
3808 ERR("Unable to create xpath context");
3809 goto end;
3810 }
3811
3812 /* Evaluate the xpath expression */
3813 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3814 if (!xpath_object) {
3815 ERR("Unable to evaluate xpath query (invalid query format)");
3816 goto end;
3817 }
3818
3819 xpath_result_set = xpath_object->nodesetval;
3820 if (!xpath_result_set) {
3821 goto end;
3822 }
3823
3824 xpath_result_size = xpath_result_set->nodeNr;
3825
3826 if (xpath_result_size == 0) {
3827 goto end;
3828 }
3829
3830 /* alloc the array */
3831 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3832 *array_size = xpath_result_size;
3833 if (!element_array) {
3834 ERR("Calloc config element array");
3835 goto end;
3836 }
3837
3838 for (int i = 0; i < xpath_result_size; i++) {
3839 xmlNodePtr node_copy = NULL;
3840 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3841 if (!node_copy) {
3842 ERR("Xml copy of event node");
3843 goto error;
3844 }
3845 /* Hand off ownership */
3846 (*element_array)[i] = malloc(sizeof(struct config_element));
3847 if (!((*element_array)[i])){
3848 ERR("Malloc config_element array");
3849 xmlFreeNode(node_copy);
3850 goto error;
3851 };
3852 (*element_array)[i]->element = node_copy;
3853 }
3854end:
3855 xmlXPathFreeContext(xpath_context);
3856 xmlXPathFreeObject(xpath_object);
3857 xmlFree(internal_xpath);
3858 xmlFree(internal_value);
3859 return;
3860error:
3861
3862 if (element_array) {
3863 config_element_free_array(*element_array, *array_size);
3864 }
3865
3866 *array_size = 0;
3867
3868 xmlXPathFreeContext(xpath_context);
3869 xmlXPathFreeObject(xpath_object);
3870 xmlFree(internal_xpath);
3871 xmlFree(internal_value);
3872 return;
3873}
3874
3875LTTNG_HIDDEN
3876void config_element_get_element_array(const struct config_element *element, const char *xpath, struct config_element ***element_array, int *array_size)
3877{
3878 int xpath_result_size;
3879
3880 xmlXPathContextPtr xpath_context = NULL;
3881 xmlXPathObjectPtr xpath_object = NULL;
3882 xmlNodeSetPtr xpath_result_set = NULL;
3883 xmlChar *internal_xpath = NULL;
3884 xmlChar *internal_value = NULL;
3885 xmlNodePtr local_copy = NULL;
3886 xmlNodePtr place_holder = NULL;
3887 xmlDocPtr document = NULL;
3888
3889 assert(element);
3890 assert(element->element);
3891 assert(xpath);
3892
3893 *element_array=NULL;
3894 *array_size=0;
3895
3896 /* Create a new doc and set root node as passed element */
3897 document = xmlNewDoc(BAD_CAST "1.0");
3898 if (!document) {
3899 ERR("Unable to create xml document");
3900 goto end;
3901 }
3902
3903 local_copy = xmlCopyNode(element->element, 1);
3904 if (!local_copy) {
3905 ERR("Unable to copy an xml node");
3906 goto end;
3907 }
3908
3909 /* Hand off the local_copy to the xmlDoc */
3910 place_holder = xmlDocSetRootElement(document, local_copy);
3911 if (place_holder) {
3912 ERR("The document root was already set");
3913 xmlFreeNode(local_copy);
3914 goto end;
3915 }
3916
3917 internal_xpath = encode_string(xpath);
3918 if (!internal_xpath) {
3919 ERR("Unable to encode xpath string");
3920 goto end;
3921 }
3922
3923 /* Initialize xpath context */
3924 xpath_context = xmlXPathNewContext(document);
3925 if (!xpath_context) {
3926 ERR("Unable to create xpath context");
3927 goto end;
3928 }
3929
3930 /* Evaluate the xpath expression */
3931 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3932 if (!xpath_object) {
3933 ERR("Unable to evaluate xpath query (invalid query format)");
3934 goto end;
3935 }
3936
3937 xpath_result_set = xpath_object->nodesetval;
3938 if (!xpath_result_set) {
3939 goto end;
3940 }
3941
3942 xpath_result_size = xpath_result_set->nodeNr;
3943
3944 if (xpath_result_size == 0) {
3945 goto end;
3946 }
3947
3948 /* alloc the array */
3949 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3950 *array_size = xpath_result_size;
3951 if (!element_array) {
3952 ERR("Calloc config element array");
3953 goto end;
3954 }
3955
3956 for (int i = 0; i < xpath_result_size; i++) {
3957 xmlNodePtr node_copy = NULL;
3958 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3959 if (!node_copy) {
3960 ERR("Xml copy of event node");
3961 goto error;
3962 }
3963 /* Hand off ownership */
3964 (*element_array)[i] = malloc(sizeof(struct config_element));
3965 if (!((*element_array)[i])){
3966 ERR("Malloc config_element array");
3967 xmlFreeNode(node_copy);
3968 goto error;
3969 };
3970 (*element_array)[i]->element = node_copy;
3971 }
3972end:
3973 xmlXPathFreeContext(xpath_context);
3974 xmlXPathFreeObject(xpath_object);
3975 xmlFree(internal_xpath);
3976 xmlFree(internal_value);
3977 xmlFreeDoc(document);
3978 return;
3979error:
3980
3981 if (element_array) {
3982 config_element_free_array(*element_array, *array_size);
3983 }
3984
3985 *array_size = 0;
3986
3987 xmlXPathFreeContext(xpath_context);
3988 xmlXPathFreeObject(xpath_object);
3989 xmlFree(internal_xpath);
3990 xmlFree(internal_value);
3991 return;
3992}
3993/* Create an internal copy of the config_element.
3994 * The users is responsible for the freeing of the passed child.
3995 */
3996LTTNG_HIDDEN
3997int config_element_add_or_replace_child(struct config_element *parent, struct config_element *child)
3998{
3999 assert(parent);
4000 assert(child);
4001 assert(parent->element);
4002 assert(child->element);
4003
4004 int ret = 0;
4005 xmlNodePtr local_copy;
4006 xmlNodePtr x_ret = NULL;
4007 xmlNodePtr iterator = NULL;
4008
4009 xmlNodePtr x_parent = parent->element;
4010 xmlNodePtr x_child = child->element;
4011
4012 /* Find and delete the node with the same name if present */
4013 iterator = x_parent->children;
4014 while (iterator != NULL) {
4015 if ((!xmlStrcmp(iterator->name, x_child->name))){
4016 xmlNodePtr del = iterator;
4017 iterator = iterator->next;
4018 xmlUnlinkNode(del);
4019 xmlFreeNode(del);
4020 } else {
4021 iterator = iterator->next;
4022 }
4023 }
4024
4025 /* Add the child */
4026 local_copy = xmlCopyNode(x_child, 1);
4027 if (!local_copy) {
f7962e8f 4028 ERR("Duplication of node to be insert failed");
4ba28db3 4029 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
f7962e8f
JR
4030 goto end;
4031 }
4032 x_ret = xmlAddChild(x_parent, local_copy);
4033 if (!x_ret) {
4034 xmlFreeNode(local_copy);
4ba28db3 4035 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
f7962e8f
JR
4036 }
4037end:
4038 return ret;
4039}
4040
4041LTTNG_HIDDEN
4042char *config_element_get_element_value(const struct config_element *element, const char *xpath)
4043{
4044 char *value = NULL;
4045 int xpath_result_size;
4046 int value_result_size;
4047
4048 xmlXPathContextPtr xpath_context = NULL;
4049 xmlXPathObjectPtr xpath_object = NULL;
4050 xmlNodeSetPtr xpath_result_set = NULL;
4051 xmlChar *internal_xpath = NULL;
4052 xmlChar *internal_value = NULL;
4053 xmlDocPtr document = NULL;
4054 xmlNodePtr local_copy = NULL;
4055 xmlNodePtr place_holder = NULL;
4056
4057 assert(element);
4058 assert(element->element);
4059 assert(xpath);
4060
4061 /* Create a new doc and set root node as passed element */
4062 document = xmlNewDoc(BAD_CAST "1.0");
4063 if (!document) {
4064 value = NULL;
4065 ERR("Unable to create xml document");
4066 goto end;
4067 }
4068
4069 local_copy = xmlCopyNode(element->element, 1);
4070 if (!local_copy) {
4071 value = NULL;
4072 ERR("Unable to copy an xml node");
4073 goto end;
4074 }
4075
4076 /* Hand off the local_copy to the xmlDoc */
4077 place_holder = xmlDocSetRootElement(document, local_copy);
4078 if (place_holder) {
4079 value = NULL;
4080 ERR("The document root was already set");
4081 xmlFreeNode(local_copy);
4082 goto end;
4083 }
4084
4085 internal_xpath = encode_string(xpath);
4086 if (!internal_xpath) {
4087 /*TODO set valid error */
4088 value = NULL;
4089 ERR("Unable to encode xpath string");
4090 goto end;
4091 }
4092
4093 /* Initialize xpath context */
4094 xpath_context = xmlXPathNewContext(document);
4095 if (!xpath_context) {
4096 /*TODO set valid error */
4097 value = NULL;
4098 ERR("Unable to create xpath context");
4099 goto end;
4100 }
4101
4102 /* Evaluate the xpath expression */
4103 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
4104 if (!xpath_object) {
4105 /*TODO set valid error */
4106 value = NULL;
4107 ERR("Unable to evaluate xpath query (invalid query format)");
4108 goto end;
4109 }
4110
4111 xpath_result_set = xpath_object->nodesetval;
4112 if (!xpath_result_set) {
4113 /*TODO set valid error */
4114 value = NULL;
4115 goto end;
4116 }
4117
4118 xpath_result_size = xpath_result_set->nodeNr;
4119
4120 if (xpath_result_size > 1) {
4121 /*TODO set valid error */
4122 ERR("To many result while fetching config element value");
4123 value = NULL;
4124 goto end;
4125 }
4126
4127 if (xpath_result_size == 0) {
4128 value = NULL;
4129 goto end;
4130 }
4131
4132 assert(xpath_result_set->nodeTab[0]);
4133 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
4134 if (!internal_value) {
4135 value = NULL;
4136 goto end;
4137 }
4138
4139 value_result_size = xmlStrlen(internal_value);
4140 value = calloc(value_result_size + 1, sizeof(char));
4141 strncpy(value, (char *) internal_value, value_result_size);
4142
4143end:
4144 xmlXPathFreeContext(xpath_context);
4145 xmlXPathFreeObject(xpath_object);
4146 xmlFree(internal_xpath);
4147 xmlFree(internal_value);
4148 xmlFreeDoc(document);
4149 return value;
4150
4151}
4152
4153LTTNG_HIDDEN
4154int config_process_event_element(const struct config_element *element, const char *session_name, int domain_type, const char *channel_name) {
4155
4156 int ret = 0;
4157 xmlNodePtr node;
4158 struct lttng_domain domain;
4159 struct lttng_handle *handle;
4160
4161 assert(element);
4162 assert(element->element);
4163
4164 memset(&domain, 0, sizeof(struct lttng_domain));
4165
4166 /* Create handle */
4167 domain.type = domain_type;
4168 switch (domain.type) {
4169 case LTTNG_DOMAIN_KERNEL:
4170 domain.buf_type = LTTNG_BUFFER_GLOBAL;
4171 break;
4172 case LTTNG_DOMAIN_UST:
4173 case LTTNG_DOMAIN_JUL:
4174 case LTTNG_DOMAIN_LOG4J:
4175 case LTTNG_DOMAIN_PYTHON:
4176 domain.buf_type = LTTNG_BUFFER_PER_UID;
4177 break;
4178 case LTTNG_DOMAIN_NONE:
4179 default:
4180 assert(0);
4181 }
4182
4183 handle = lttng_create_handle(session_name, &domain);
4184 if (!handle) {
4ba28db3 4185 ret = -LTTNG_ERR_HANDLE_CREATION;
f7962e8f
JR
4186 goto error;
4187 }
4188
4189
4190 node = element->element;
4191 /* Check if element is really and event */
4192 if (xmlStrcmp(BAD_CAST config_element_event, node->name)) {
4ba28db3 4193 ret = -LTTNG_ERR_CONFIG_INVALID_ELEMENT;
f7962e8f
JR
4194 goto error;
4195 }
4196
4197 ret = process_event_node(node, handle, channel_name, ENABLE);
4198 if (ret) {
4199 goto error;
4200 }
4201error:
4202 free(handle);
4203 return ret;
4204}
This page took 0.223626 seconds and 5 git commands to generate.