Fix error handling for config_document/element
[deliverable/lttng-tools.git] / src / common / config / session-config.c
CommitLineData
1501a7f3
JG
1/*
2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
1501a7f3 19#include <assert.h>
1501a7f3 20#include <ctype.h>
1501a7f3
JG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
36f2332b 24#include <inttypes.h>
dcf266c0
JG
25#include <dirent.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/stat.h>
6c66fa0f 29#include <stdbool.h>
1501a7f3
JG
30
31#include <common/defaults.h>
32#include <common/error.h>
33#include <common/macros.h>
34#include <common/utils.h>
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
3239 document->document = xmlParseFile(path);
3240 if (!document->document) {
3241 ERR("Configuration document parsing failed.");
3242 goto error;
3243 }
3244
3245 ret = xmlSchemaValidateDoc(validation_ctx.schema_validation_ctx,
3246 document->document);
3247 if (ret) {
3248 ERR("Session configuration file validation failed");
3249 goto error;
3250 }
3251
3252end:
4640a526 3253 fini_session_config_validation_ctx(&validation_ctx);
fa8b52a2
JR
3254 return document;
3255error:
3256 xmlFreeDoc(document->document);
3257 free(document);
3258 return NULL;
3259}
3260
3261LTTNG_HIDDEN
3262void config_document_free(struct config_document *document)
3263{
3264 if (document) {
3265 xmlFreeDoc(document->document);
3266 document->document = NULL;
3267 }
4640a526
JR
3268
3269 free(document);
fa8b52a2
JR
3270}
3271
3272LTTNG_HIDDEN
3273int config_document_replace_element_value(struct config_document *document,
3274 const char *xpath, const char *value)
3275{
4640a526 3276 int ret = 0;
fa8b52a2
JR
3277 int xpath_result_size;
3278
3279 xmlXPathContextPtr xpath_context = NULL;
3280 xmlXPathObjectPtr xpath_object = NULL;
3281 xmlNodeSetPtr xpath_result_set = NULL;
3282 xmlChar *internal_xpath = NULL;
3283 xmlChar *internal_value = NULL;
3284
3285 assert(document);
3286 assert(xpath);
3287 assert(value);
3288
3289 internal_xpath = encode_string(xpath);
3290 if (!internal_xpath) {
3291 ret = -LTTNG_ERR_NOMEM;
3292 ERR("Unable to encode xpath string");
3293 goto end;
3294 }
3295
3296 internal_value = encode_string(value);
3297 if (!internal_value) {
3298 ret = -LTTNG_ERR_NOMEM;
3299 ERR("Unable to encode xpath replace value string");
3300 goto end;
3301 }
3302
3303 /* Initialize xpath context */
3304 xpath_context = xmlXPathNewContext(document->document);
3305 if (!xpath_context) {
3306 ret = -LTTNG_ERR_NOMEM;
3307 ERR("Unable to create xpath context");
3308 goto end;
3309 }
3310
3311 /* Evaluate de xpath expression */
3312 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3313 if (!xpath_object) {
3314 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3315 goto end;
3316 }
3317
3318 /* TODO: from here could be extracted and previopus could be a step in
3319 * subsequent operation, modify/substitute/delete node.
3320 */
3321 xpath_result_set = xpath_object->nodesetval;
3322 if (!xpath_result_set) {
3323 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3324 goto end;
3325 }
3326
3327 xpath_result_size = xpath_result_set->nodeNr;
3328
3329 /*
3330 * Reverse traversal since last element could be nested under parent
3331 * element present in the result set.
3332 */
3333 for (int i = xpath_result_size - 1; i >=0; i--) {
3334 assert(xpath_result_set->nodeTab[i]);
3335 xmlNodeSetContent(xpath_result_set->nodeTab[i], internal_value);
3336
3337 /*
3338 * Libxml2 quirk regarding the freing and namesplace node see
3339 * libxml2 example and documentation for more details.
3340 */
3341 if (xpath_result_set->nodeTab[i]->type != XML_NAMESPACE_DECL) {
3342 xpath_result_set->nodeTab[i] = NULL;
3343 }
3344 }
3345
3346end:
3347 xmlXPathFreeContext(xpath_context);
3348 xmlXPathFreeObject(xpath_object);
3349 xmlFree(internal_xpath);
3350 xmlFree(internal_value);
3351 return ret;
3352}
3353
3354LTTNG_HIDDEN
3355int config_load_configuration_sessions(struct config_document *document,
3356 const char *session_name, int override)
3357{
3358 int ret;
3359 struct session_config_validation_ctx validation_ctx = { 0 };
3360
3361 ret = init_session_config_validation_ctx(&validation_ctx);
3362 if (ret) {
3363 goto end;
3364 }
3365 ret = load_session_from_document(document, session_name,
3366 &validation_ctx, override);
3367end:
4640a526 3368 fini_session_config_validation_ctx(&validation_ctx);
fa8b52a2
JR
3369 return ret;
3370}
3371
3372LTTNG_HIDDEN
3373int config_document_replace_element(struct config_document *document,
3374 const char *xpath, const struct config_element *element)
3375{
3376 int ret = 0;
3377 int xpath_result_size;
3378
3379 xmlXPathContextPtr xpath_context = NULL;
3380 xmlXPathObjectPtr xpath_object = NULL;
3381 xmlNodeSetPtr xpath_result_set = NULL;
3382 xmlChar *internal_xpath = NULL;
3383 xmlNodePtr old_node = NULL;
3384 xmlNodePtr copy = NULL;
3385
3386 assert(document);
3387 assert(xpath);
3388 assert(element);
3389 assert(element->element);
3390
3391 internal_xpath = encode_string(xpath);
3392 if (!internal_xpath) {
3393 ret = -LTTNG_ERR_NOMEM;
3394 ERR("Unable to encode xpath string");
3395 goto end;
3396 }
3397
3398 /* Initialize xpath context */
3399 xpath_context = xmlXPathNewContext(document->document);
3400 if (!xpath_context) {
3401 ret = -LTTNG_ERR_NOMEM;
3402 ERR("Unable to create xpath context");
3403 goto end;
3404 }
3405
3406 /* Evaluate the xpath expression */
3407 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3408 if (!xpath_object) {
3409 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3410 goto end;
3411 }
3412
3413 xpath_result_set = xpath_object->nodesetval;
3414 if (!xpath_result_set) {
3415 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3416 goto end;
3417 }
3418
3419 xpath_result_size = xpath_result_set->nodeNr;
3420
3421 if (xpath_result_size > 1) {
3422 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3423 goto end;
3424 }
3425
3426 if (xpath_result_size == 0) {
4ba28db3 3427 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
fa8b52a2
JR
3428 goto end;
3429 }
3430
3431 assert(xpath_result_set->nodeTab[0]);
3432
3433 /* Do a copy of the element to ease caller memory management */
3434 copy = xmlCopyNode(element->element, 1);
3435 if (!copy) {
3436 ret = -LTTNG_ERR_NOMEM;
fa8b52a2
JR
3437 goto end;
3438 }
3439
fa8b52a2 3440 old_node = xmlReplaceNode(xpath_result_set->nodeTab[0], copy);
4640a526
JR
3441 if (xpath_result_set->nodeTab[0]->type != XML_NAMESPACE_DECL)
3442 xpath_result_set->nodeTab[0] = NULL;
fa8b52a2
JR
3443 if (!old_node) {
3444 ret = -LTTNG_ERR_CONFIG_REPLACEMENT;
3445 xmlFreeNode(copy);
3446 goto end;
3447 }
4640a526
JR
3448 xmlUnlinkNode(old_node);
3449 xmlFreeNode(old_node);
fa8b52a2
JR
3450end:
3451 xmlXPathFreeContext(xpath_context);
3452 xmlXPathFreeObject(xpath_object);
3453 xmlFree(internal_xpath);
3454 return ret;
3455}
3456
3457LTTNG_HIDDEN
3458char *config_document_get_element_value(struct config_document *document,
3459 const char *xpath)
3460{
3461 char *value = NULL;
3462 int xpath_result_size;
3463 int value_result_size;
3464
3465 xmlXPathContextPtr xpath_context = NULL;
3466 xmlXPathObjectPtr xpath_object = NULL;
3467 xmlNodeSetPtr xpath_result_set = NULL;
3468 xmlChar *internal_xpath = NULL;
3469 xmlChar *internal_value = NULL;
3470
3471 assert(document);
3472 assert(xpath);
3473
3474 internal_xpath = encode_string(xpath);
3475 if (!internal_xpath) {
3476 value = NULL;
3477 ERR("Unable to encode xpath string");
3478 goto end;
3479 }
3480
3481 /* Initialize xpath context */
3482 xpath_context = xmlXPathNewContext(document->document);
3483 if (!xpath_context) {
3484 value = NULL;
3485 ERR("Unable to create xpath context");
3486 goto end;
3487 }
3488
3489 /* Evaluate the xpath expression */
3490 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3491 if (!xpath_object) {
3492 value = NULL;
3493 ERR("Unable to evaluate xpath query (invalid query format)");
3494 goto end;
3495 }
3496
3497 xpath_result_set = xpath_object->nodesetval;
3498 if (!xpath_result_set) {
3499 value = NULL;
3500 goto end;
3501 }
3502
3503 xpath_result_size = xpath_result_set->nodeNr;
3504
3505 if (xpath_result_size > 1) {
3506 ERR("To many result while fetching config element value");
3507 value = NULL;
3508 goto end;
3509 }
3510
3511 if (xpath_result_size == 0) {
3512 value = NULL;
3513 goto end;
3514 }
3515
3516 /*
3517 * Reverse traversal since last element could be nested under parent
3518 * element present in the result set.
3519 */
3520 assert(xpath_result_set->nodeTab[0]);
3521 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
3522 if (!internal_value) {
3523 value = NULL;
3524 goto end;
3525 }
3526
3527 value_result_size = xmlStrlen(internal_value);
3528 value = calloc(value_result_size + 1, sizeof(char));
3529 strncpy(value, (char *) internal_value, value_result_size);
3530
3531end:
3532 xmlXPathFreeContext(xpath_context);
3533 xmlXPathFreeObject(xpath_object);
3534 xmlFree(internal_xpath);
3535 xmlFree(internal_value);
3536 return value;
3537}
3538
3539LTTNG_HIDDEN
3540int config_document_element_exist(struct config_document *document,
3541 const char *xpath)
3542{
3543 int exist = 0;
3544 int xpath_result_size;
3545
3546 xmlXPathContextPtr xpath_context = NULL;
3547 xmlXPathObjectPtr xpath_object = NULL;
3548 xmlNodeSetPtr xpath_result_set = NULL;
3549 xmlChar *internal_xpath = NULL;
3550
3551 assert(document);
3552 assert(document->document);
3553 assert(xpath);
3554
3555 internal_xpath = encode_string(xpath);
3556 if (!internal_xpath) {
3557 ERR("Unable to encode xpath string");
3558 goto end;
3559 }
3560
3561 /* Initialize xpath context */
3562 xpath_context = xmlXPathNewContext(document->document);
3563 if (!xpath_context) {
3564 ERR("Unable to create xpath context");
3565 goto end;
3566 }
3567
3568 /* Evaluate the xpath expression */
3569 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3570 if (!xpath_object) {
3571 ERR("Unable to evaluate xpath query (invalid query format)");
3572 goto end;
3573 }
3574
3575 xpath_result_set = xpath_object->nodesetval;
3576 if (!xpath_result_set) {
3577 goto end;
3578 }
3579
3580 xpath_result_size = xpath_result_set->nodeNr;
3581
3582 if (xpath_result_size > 0) {
3583 exist = 1;
3584 }
3585end:
3586 xmlXPathFreeContext(xpath_context);
3587 xmlXPathFreeObject(xpath_object);
3588 xmlFree(internal_xpath);
3589 return exist;
3590
3591}
3592
3593LTTNG_HIDDEN
3594struct config_element *config_element_create(const char *name,
3595 const char* value)
3596{
3597 struct config_element *element;
3598
3599 assert(name);
3600
3601 xmlChar *internal_name = NULL;
3602 xmlChar *internal_value = NULL;
3603
3604 internal_name = encode_string(name);
3605 if (!internal_name) {
3606 ERR("Unable to encode config element name string");
3607 element = NULL;
3608 goto end;
3609 }
3610
3611
3612 if (value) {
3613 internal_value = encode_string(value);
3614 if (!internal_value) {
3615 ERR("Unable to encode config_element value string");
3616 element = NULL;
3617 goto end;
3618 }
3619 }
3620
3621 element = zmalloc(sizeof(struct config_element));
3622 if (!element) {
3623 goto end;
3624 }
3625
3626 element->element = xmlNewNode(NULL, internal_name);
3627 if (!element->element) {
3628 free(element);
3629 element = NULL;
00df7c2f 3630 goto end;
fa8b52a2
JR
3631 }
3632
3633 if (internal_value) {
3634 xmlNodeAddContent(element->element, internal_value);
3635 }
3636end:
3637 xmlFree(internal_name);
3638 xmlFree(internal_value);
3639 return element;
3640}
3641
3642LTTNG_HIDDEN
3643int config_element_add_child(struct config_element *parent,
3644 const struct config_element *child)
3645{
3646 assert(parent);
3647 assert(child);
3648 assert(parent->element);
3649 assert(child->element);
3650
3651 int ret = 0;
3652 xmlNodePtr node = NULL;
3653 xmlNodePtr copy = NULL;
3654
3655 /* Do a copy to ease the memory management for caller */
3656 copy = xmlCopyNode(child->element, 1);
3657 if (!copy) {
3658 ret = -LTTNG_ERR_NOMEM;
3659 goto error;
3660 }
3661 node = xmlAddChild(parent->element, copy);
3662 if (!node) {
3663 xmlFreeNode(copy);
3664 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3665 goto error;
3666 }
3667error:
3668 return ret;
3669}
3670
3671
3672LTTNG_HIDDEN
3673int config_document_insert_element(struct config_document *document,
3674 const char *xpath, const struct config_element *element)
3675{
3676 int ret = 0;
3677 int xpath_result_size;
3678
3679 xmlXPathContextPtr xpath_context = NULL;
3680 xmlXPathObjectPtr xpath_object = NULL;
3681 xmlNodeSetPtr xpath_result_set = NULL;
3682 xmlChar *internal_xpath = NULL;
3683 xmlNodePtr child_node = NULL;
3684 xmlNodePtr local_copy = NULL;
3685
3686 assert(document);
3687 assert(xpath);
3688 assert(element);
3689 assert(element->element);
3690
3691 internal_xpath = encode_string(xpath);
3692 if (!internal_xpath) {
3693 ret = -LTTNG_ERR_NOMEM;
3694 goto end;
3695 }
3696
3697 /* Initialize xpath context */
3698 xpath_context = xmlXPathNewContext(document->document);
3699 if (!xpath_context) {
3700 ret = -LTTNG_ERR_NOMEM;
3701 goto end;
3702 }
3703
3704 /* Evaluate the xpath expression */
3705 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3706 if (!xpath_object) {
3707 ret = -LTTNG_ERR_CONFIG_INVALID_QUERY;
3708 goto end;
3709 }
3710
3711 xpath_result_set = xpath_object->nodesetval;
3712 if (!xpath_result_set) {
3713 ret = -LTTNG_ERR_CONFIG_EMPTY_SET;
3714 goto end;
3715 }
3716
3717 xpath_result_size = xpath_result_set->nodeNr;
3718
3719 if (xpath_result_size > 1) {
3720 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3721 goto end;
3722 }
3723
3724 if (xpath_result_size == 0) {
3725 ret = -LTTNG_ERR_CONFIG_INVALID_COUNT;
3726 goto end;
3727 }
3728
3729 assert(xpath_result_set->nodeTab[0]);
3730 /* Do a copy to simply memory management */
3731 local_copy = xmlCopyNode(element->element, 1);
3732 if (!local_copy) {
3733 ret = -LTTNG_ERR_NOMEM;
fa8b52a2
JR
3734 goto end;
3735 }
3736
3737 child_node = xmlAddChild(xpath_result_set->nodeTab[0], local_copy);
3738 if (!child_node) {
3739 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
3740 xmlFreeNode(local_copy);
3741 goto end;
3742 }
3743end:
3744 xmlXPathFreeContext(xpath_context);
3745 xmlXPathFreeObject(xpath_object);
3746 xmlFree(internal_xpath);
3747 return ret;
3748}
3749
3750LTTNG_HIDDEN
3751void config_element_free(struct config_element *element) {
4640a526
JR
3752 if (element && element->element) {
3753 xmlFreeNode(element->element);
fa8b52a2
JR
3754 }
3755
3756 free(element);
3757}
f7962e8f
JR
3758
3759LTTNG_HIDDEN
3760void config_element_free_array(struct config_element **array, int size) {
3761
3762 if (array) {
3763 for (int i = 0; i < size; i++) {
3764 if (!array[i]) {
3765 continue;
3766 }
3767 if (array[i]->element) {
3768 xmlFreeNode(array[i]->element);
3769 }
3770 free(array[i]);
3771 }
3772 }
3773 free(array);
3774}
3775
3776LTTNG_HIDDEN
3777void config_document_get_element_array(const struct config_document *document, const char *xpath, struct config_element ***element_array, int *array_size)
3778{
3779 int xpath_result_size;
3780
3781 xmlXPathContextPtr xpath_context = NULL;
3782 xmlXPathObjectPtr xpath_object = NULL;
3783 xmlNodeSetPtr xpath_result_set = NULL;
3784 xmlChar *internal_xpath = NULL;
3785 xmlChar *internal_value = NULL;
3786
3787 assert(document);
3788 assert(xpath);
3789
3790 *element_array=NULL;
3791 *array_size=0;
3792
3793
3794 internal_xpath = encode_string(xpath);
3795 if (!internal_xpath) {
3796 ERR("Unable to encode xpath string");
3797 goto end;
3798 }
3799
3800 /* Initialize xpath context */
3801 xpath_context = xmlXPathNewContext(document->document);
3802 if (!xpath_context) {
3803 ERR("Unable to create xpath context");
3804 goto end;
3805 }
3806
3807 /* Evaluate the xpath expression */
3808 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3809 if (!xpath_object) {
3810 ERR("Unable to evaluate xpath query (invalid query format)");
3811 goto end;
3812 }
3813
3814 xpath_result_set = xpath_object->nodesetval;
3815 if (!xpath_result_set) {
3816 goto end;
3817 }
3818
3819 xpath_result_size = xpath_result_set->nodeNr;
3820
3821 if (xpath_result_size == 0) {
3822 goto end;
3823 }
3824
3825 /* alloc the array */
3826 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3827 *array_size = xpath_result_size;
3828 if (!element_array) {
3829 ERR("Calloc config element array");
3830 goto end;
3831 }
3832
3833 for (int i = 0; i < xpath_result_size; i++) {
3834 xmlNodePtr node_copy = NULL;
3835 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3836 if (!node_copy) {
3837 ERR("Xml copy of event node");
3838 goto error;
3839 }
3840 /* Hand off ownership */
3841 (*element_array)[i] = malloc(sizeof(struct config_element));
3842 if (!((*element_array)[i])){
3843 ERR("Malloc config_element array");
3844 xmlFreeNode(node_copy);
3845 goto error;
3846 };
3847 (*element_array)[i]->element = node_copy;
3848 }
3849end:
3850 xmlXPathFreeContext(xpath_context);
3851 xmlXPathFreeObject(xpath_object);
3852 xmlFree(internal_xpath);
3853 xmlFree(internal_value);
3854 return;
3855error:
3856
3857 if (element_array) {
3858 config_element_free_array(*element_array, *array_size);
3859 }
3860
3861 *array_size = 0;
3862
3863 xmlXPathFreeContext(xpath_context);
3864 xmlXPathFreeObject(xpath_object);
3865 xmlFree(internal_xpath);
3866 xmlFree(internal_value);
3867 return;
3868}
3869
3870LTTNG_HIDDEN
3871void config_element_get_element_array(const struct config_element *element, const char *xpath, struct config_element ***element_array, int *array_size)
3872{
3873 int xpath_result_size;
3874
3875 xmlXPathContextPtr xpath_context = NULL;
3876 xmlXPathObjectPtr xpath_object = NULL;
3877 xmlNodeSetPtr xpath_result_set = NULL;
3878 xmlChar *internal_xpath = NULL;
3879 xmlChar *internal_value = NULL;
3880 xmlNodePtr local_copy = NULL;
3881 xmlNodePtr place_holder = NULL;
3882 xmlDocPtr document = NULL;
3883
3884 assert(element);
3885 assert(element->element);
3886 assert(xpath);
3887
3888 *element_array=NULL;
3889 *array_size=0;
3890
3891 /* Create a new doc and set root node as passed element */
3892 document = xmlNewDoc(BAD_CAST "1.0");
3893 if (!document) {
3894 ERR("Unable to create xml document");
3895 goto end;
3896 }
3897
3898 local_copy = xmlCopyNode(element->element, 1);
3899 if (!local_copy) {
3900 ERR("Unable to copy an xml node");
3901 goto end;
3902 }
3903
3904 /* Hand off the local_copy to the xmlDoc */
3905 place_holder = xmlDocSetRootElement(document, local_copy);
3906 if (place_holder) {
3907 ERR("The document root was already set");
3908 xmlFreeNode(local_copy);
3909 goto end;
3910 }
3911
3912 internal_xpath = encode_string(xpath);
3913 if (!internal_xpath) {
3914 ERR("Unable to encode xpath string");
3915 goto end;
3916 }
3917
3918 /* Initialize xpath context */
3919 xpath_context = xmlXPathNewContext(document);
3920 if (!xpath_context) {
3921 ERR("Unable to create xpath context");
3922 goto end;
3923 }
3924
3925 /* Evaluate the xpath expression */
3926 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
3927 if (!xpath_object) {
3928 ERR("Unable to evaluate xpath query (invalid query format)");
3929 goto end;
3930 }
3931
3932 xpath_result_set = xpath_object->nodesetval;
3933 if (!xpath_result_set) {
3934 goto end;
3935 }
3936
3937 xpath_result_size = xpath_result_set->nodeNr;
3938
3939 if (xpath_result_size == 0) {
3940 goto end;
3941 }
3942
3943 /* alloc the array */
3944 *element_array = calloc(xpath_result_size, sizeof(struct config_element *));
3945 *array_size = xpath_result_size;
3946 if (!element_array) {
3947 ERR("Calloc config element array");
3948 goto end;
3949 }
3950
3951 for (int i = 0; i < xpath_result_size; i++) {
3952 xmlNodePtr node_copy = NULL;
3953 node_copy = xmlCopyNode(xpath_result_set->nodeTab[i], 1);
3954 if (!node_copy) {
3955 ERR("Xml copy of event node");
3956 goto error;
3957 }
3958 /* Hand off ownership */
3959 (*element_array)[i] = malloc(sizeof(struct config_element));
3960 if (!((*element_array)[i])){
3961 ERR("Malloc config_element array");
3962 xmlFreeNode(node_copy);
3963 goto error;
3964 };
3965 (*element_array)[i]->element = node_copy;
3966 }
3967end:
3968 xmlXPathFreeContext(xpath_context);
3969 xmlXPathFreeObject(xpath_object);
3970 xmlFree(internal_xpath);
3971 xmlFree(internal_value);
3972 xmlFreeDoc(document);
3973 return;
3974error:
3975
3976 if (element_array) {
3977 config_element_free_array(*element_array, *array_size);
3978 }
3979
3980 *array_size = 0;
3981
3982 xmlXPathFreeContext(xpath_context);
3983 xmlXPathFreeObject(xpath_object);
3984 xmlFree(internal_xpath);
3985 xmlFree(internal_value);
3986 return;
3987}
3988/* Create an internal copy of the config_element.
3989 * The users is responsible for the freeing of the passed child.
3990 */
3991LTTNG_HIDDEN
3992int config_element_add_or_replace_child(struct config_element *parent, struct config_element *child)
3993{
3994 assert(parent);
3995 assert(child);
3996 assert(parent->element);
3997 assert(child->element);
3998
3999 int ret = 0;
4000 xmlNodePtr local_copy;
4001 xmlNodePtr x_ret = NULL;
4002 xmlNodePtr iterator = NULL;
4003
4004 xmlNodePtr x_parent = parent->element;
4005 xmlNodePtr x_child = child->element;
4006
4007 /* Find and delete the node with the same name if present */
4008 iterator = x_parent->children;
4009 while (iterator != NULL) {
4010 if ((!xmlStrcmp(iterator->name, x_child->name))){
4011 xmlNodePtr del = iterator;
4012 iterator = iterator->next;
4013 xmlUnlinkNode(del);
4014 xmlFreeNode(del);
4015 } else {
4016 iterator = iterator->next;
4017 }
4018 }
4019
4020 /* Add the child */
4021 local_copy = xmlCopyNode(x_child, 1);
4022 if (!local_copy) {
f7962e8f 4023 ERR("Duplication of node to be insert failed");
4ba28db3 4024 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
f7962e8f
JR
4025 goto end;
4026 }
4027 x_ret = xmlAddChild(x_parent, local_copy);
4028 if (!x_ret) {
4029 xmlFreeNode(local_copy);
4ba28db3 4030 ret = -LTTNG_ERR_CONFIG_ADD_CHILD;
f7962e8f
JR
4031 }
4032end:
4033 return ret;
4034}
4035
4036LTTNG_HIDDEN
4037char *config_element_get_element_value(const struct config_element *element, const char *xpath)
4038{
4039 char *value = NULL;
4040 int xpath_result_size;
4041 int value_result_size;
4042
4043 xmlXPathContextPtr xpath_context = NULL;
4044 xmlXPathObjectPtr xpath_object = NULL;
4045 xmlNodeSetPtr xpath_result_set = NULL;
4046 xmlChar *internal_xpath = NULL;
4047 xmlChar *internal_value = NULL;
4048 xmlDocPtr document = NULL;
4049 xmlNodePtr local_copy = NULL;
4050 xmlNodePtr place_holder = NULL;
4051
4052 assert(element);
4053 assert(element->element);
4054 assert(xpath);
4055
4056 /* Create a new doc and set root node as passed element */
4057 document = xmlNewDoc(BAD_CAST "1.0");
4058 if (!document) {
4059 value = NULL;
4060 ERR("Unable to create xml document");
4061 goto end;
4062 }
4063
4064 local_copy = xmlCopyNode(element->element, 1);
4065 if (!local_copy) {
4066 value = NULL;
4067 ERR("Unable to copy an xml node");
4068 goto end;
4069 }
4070
4071 /* Hand off the local_copy to the xmlDoc */
4072 place_holder = xmlDocSetRootElement(document, local_copy);
4073 if (place_holder) {
4074 value = NULL;
4075 ERR("The document root was already set");
4076 xmlFreeNode(local_copy);
4077 goto end;
4078 }
4079
4080 internal_xpath = encode_string(xpath);
4081 if (!internal_xpath) {
4082 /*TODO set valid error */
4083 value = NULL;
4084 ERR("Unable to encode xpath string");
4085 goto end;
4086 }
4087
4088 /* Initialize xpath context */
4089 xpath_context = xmlXPathNewContext(document);
4090 if (!xpath_context) {
4091 /*TODO set valid error */
4092 value = NULL;
4093 ERR("Unable to create xpath context");
4094 goto end;
4095 }
4096
4097 /* Evaluate the xpath expression */
4098 xpath_object = xmlXPathEvalExpression(internal_xpath, xpath_context);
4099 if (!xpath_object) {
4100 /*TODO set valid error */
4101 value = NULL;
4102 ERR("Unable to evaluate xpath query (invalid query format)");
4103 goto end;
4104 }
4105
4106 xpath_result_set = xpath_object->nodesetval;
4107 if (!xpath_result_set) {
4108 /*TODO set valid error */
4109 value = NULL;
4110 goto end;
4111 }
4112
4113 xpath_result_size = xpath_result_set->nodeNr;
4114
4115 if (xpath_result_size > 1) {
4116 /*TODO set valid error */
4117 ERR("To many result while fetching config element value");
4118 value = NULL;
4119 goto end;
4120 }
4121
4122 if (xpath_result_size == 0) {
4123 value = NULL;
4124 goto end;
4125 }
4126
4127 assert(xpath_result_set->nodeTab[0]);
4128 internal_value = xmlNodeGetContent(xpath_result_set->nodeTab[0]);
4129 if (!internal_value) {
4130 value = NULL;
4131 goto end;
4132 }
4133
4134 value_result_size = xmlStrlen(internal_value);
4135 value = calloc(value_result_size + 1, sizeof(char));
4136 strncpy(value, (char *) internal_value, value_result_size);
4137
4138end:
4139 xmlXPathFreeContext(xpath_context);
4140 xmlXPathFreeObject(xpath_object);
4141 xmlFree(internal_xpath);
4142 xmlFree(internal_value);
4143 xmlFreeDoc(document);
4144 return value;
4145
4146}
4147
4148LTTNG_HIDDEN
4149int config_process_event_element(const struct config_element *element, const char *session_name, int domain_type, const char *channel_name) {
4150
4151 int ret = 0;
4152 xmlNodePtr node;
4153 struct lttng_domain domain;
4154 struct lttng_handle *handle;
4155
4156 assert(element);
4157 assert(element->element);
4158
4159 memset(&domain, 0, sizeof(struct lttng_domain));
4160
4161 /* Create handle */
4162 domain.type = domain_type;
4163 switch (domain.type) {
4164 case LTTNG_DOMAIN_KERNEL:
4165 domain.buf_type = LTTNG_BUFFER_GLOBAL;
4166 break;
4167 case LTTNG_DOMAIN_UST:
4168 case LTTNG_DOMAIN_JUL:
4169 case LTTNG_DOMAIN_LOG4J:
4170 case LTTNG_DOMAIN_PYTHON:
4171 domain.buf_type = LTTNG_BUFFER_PER_UID;
4172 break;
4173 case LTTNG_DOMAIN_NONE:
4174 default:
4175 assert(0);
4176 }
4177
4178 handle = lttng_create_handle(session_name, &domain);
4179 if (!handle) {
4ba28db3 4180 ret = -LTTNG_ERR_HANDLE_CREATION;
f7962e8f
JR
4181 goto error;
4182 }
4183
4184
4185 node = element->element;
4186 /* Check if element is really and event */
4187 if (xmlStrcmp(BAD_CAST config_element_event, node->name)) {
4ba28db3 4188 ret = -LTTNG_ERR_CONFIG_INVALID_ELEMENT;
f7962e8f
JR
4189 goto error;
4190 }
4191
4192 ret = process_event_node(node, handle, channel_name, ENABLE);
4193 if (ret) {
4194 goto error;
4195 }
4196error:
4197 free(handle);
4198 return ret;
4199}
This page took 0.252103 seconds and 5 git commands to generate.