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