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