Fix: assign values to path, ctrl and data uris during configuration load
[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
1b08cbce
JR
1191int create_snapshot_session(const char *session_name, xmlNodePtr output_node,
1192 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1193{
1194 int ret;
1195 xmlNodePtr node = NULL;
1196 xmlNodePtr snapshot_output_list_node;
1197 xmlNodePtr snapshot_output_node;
1198
1199 assert(session_name);
1200
1201 ret = lttng_create_session_snapshot(session_name, NULL);
1202 if (ret) {
1203 goto end;
1204 }
1205
1206 if (!output_node) {
1207 goto end;
1208 }
1209
1210 snapshot_output_list_node = xmlFirstElementChild(output_node);
1211
1212 /* Parse and create snapshot outputs */
1213
1214 for (snapshot_output_node =
1215 xmlFirstElementChild(snapshot_output_list_node);
1216 snapshot_output_node; snapshot_output_node =
1217 xmlNextElementSibling(snapshot_output_node)) {
1218 char *name = NULL;
1219 uint64_t max_size = UINT64_MAX;
1220 struct consumer_output output = { 0 };
1221 struct lttng_snapshot_output *snapshot_output = NULL;
1b08cbce
JR
1222 const char *control_uri = NULL;
1223 const char *data_uri = NULL;
1224 const char *path = NULL;
dcf266c0
JG
1225
1226 for (node = xmlFirstElementChild(snapshot_output_node); node;
1227 node = xmlNextElementSibling(node)) {
1228 if (!strcmp((const char *) node->name,
1229 config_element_name)) {
1230 /* name */
1231 name = (char *) xmlNodeGetContent(node);
1232 if (!name) {
1233 ret = -LTTNG_ERR_NOMEM;
1234 goto error_snapshot_output;
1235 }
1236 } else if (!strcmp((const char *) node->name,
1237 config_element_max_size)) {
1238 xmlChar *content = xmlNodeGetContent(node);
1239
1240 /* max_size */
1241 if (!content) {
1242 ret = -LTTNG_ERR_NOMEM;
1243 goto error_snapshot_output;
1244 }
1245 ret = parse_uint(content, &max_size);
1246 free(content);
1247 if (ret) {
1248 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1249 goto error_snapshot_output;
1250 }
1251 } else {
1252 /* consumer_output */
1253 ret = process_consumer_output(node, &output);
1254 if (ret) {
1255 goto error_snapshot_output;
1256 }
1257 }
1258 }
1259
21a3f144
JR
1260 control_uri = output.control_uri;
1261 data_uri = output.data_uri;
1262 path = output.path;
1263
1b08cbce
JR
1264 if (overrides) {
1265 if (overrides->path_url) {
1b08cbce 1266 path = overrides->path_url;
21a3f144
JR
1267 /* Control/data_uri are null */
1268 control_uri = NULL;
1269 data_uri = NULL;
1b08cbce
JR
1270 } else {
1271 if (overrides->ctrl_url) {
1b08cbce 1272 control_uri = overrides->ctrl_url;
21a3f144
JR
1273 /* path is null */
1274 path = NULL;
1b08cbce
JR
1275 }
1276 if (overrides->data_url) {
1b08cbce 1277 data_uri = overrides->data_url;
21a3f144
JR
1278 /* path is null */
1279 path = NULL;
1b08cbce
JR
1280 }
1281 }
1b08cbce
JR
1282 }
1283
dcf266c0
JG
1284 snapshot_output = lttng_snapshot_output_create();
1285 if (!snapshot_output) {
1286 ret = -LTTNG_ERR_NOMEM;
1287 goto error_snapshot_output;
1288 }
1289
1290 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1291 if (ret) {
1292 goto error_snapshot_output;
1293 }
1294
1295 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1296 if (ret) {
1297 goto error_snapshot_output;
1298 }
1299
1b08cbce
JR
1300 if (path) {
1301 ret = lttng_snapshot_output_set_ctrl_url(path,
dcf266c0
JG
1302 snapshot_output);
1303 if (ret) {
1304 goto error_snapshot_output;
1305 }
1306 } else {
1b08cbce
JR
1307 if (control_uri) {
1308 ret = lttng_snapshot_output_set_ctrl_url(control_uri,
dcf266c0
JG
1309 snapshot_output);
1310 if (ret) {
1311 goto error_snapshot_output;
1312 }
1313 }
1314
1b08cbce
JR
1315 if (data_uri) {
1316 ret = lttng_snapshot_output_set_data_url(data_uri,
dcf266c0
JG
1317 snapshot_output);
1318 if (ret) {
1319 goto error_snapshot_output;
1320 }
1321 }
1322 }
1323
1324 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1325error_snapshot_output:
1326 free(name);
1327 free(output.path);
1328 free(output.control_uri);
1329 free(output.data_uri);
1330 lttng_snapshot_output_destroy(snapshot_output);
1331 if (ret) {
1332 goto end;
1333 }
1334 }
1335end:
1336 return ret;
1337}
1338
1339static
1340int create_session(const char *name,
1341 struct lttng_domain *kernel_domain,
1342 struct lttng_domain *ust_domain,
1343 struct lttng_domain *jul_domain,
5cdb6027 1344 struct lttng_domain *log4j_domain,
dcf266c0 1345 xmlNodePtr output_node,
1b08cbce
JR
1346 uint64_t live_timer_interval,
1347 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1348{
1349 int ret;
1350 struct consumer_output output = { 0 };
1351 xmlNodePtr consumer_output_node;
1b08cbce
JR
1352 const char *control_uri = NULL;
1353 const char *data_uri = NULL;
1354 const char *path = NULL;
dcf266c0
JG
1355
1356 assert(name);
dcf266c0
JG
1357
1358 if (output_node) {
1359 consumer_output_node = xmlFirstElementChild(output_node);
1360 if (!consumer_output_node) {
1361 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1362 goto end;
1363 }
1364
1365 if (strcmp((const char *) consumer_output_node->name,
1366 config_element_consumer_output)) {
1367 WARN("Invalid output type, expected %s node",
1368 config_element_consumer_output);
1369 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1370 goto end;
1371 }
1372
1373 ret = process_consumer_output(consumer_output_node, &output);
1374 if (ret) {
1375 goto end;
1376 }
1377 }
1378
21a3f144
JR
1379 control_uri = output.control_uri;
1380 data_uri = output.data_uri;
1381 path = output.path;
1382
1b08cbce
JR
1383 /* Check for override and apply them */
1384 if (overrides) {
1385 if (overrides->path_url) {
1b08cbce 1386 path = overrides->path_url;
21a3f144
JR
1387 /* control/data_uri are null */;
1388 control_uri = NULL;
1389 data_uri = NULL;
1b08cbce
JR
1390 } else {
1391 if (overrides->ctrl_url) {
1b08cbce 1392 control_uri = overrides->ctrl_url;
21a3f144
JR
1393 /* path is null */
1394 path = NULL;
1b08cbce
JR
1395 }
1396 if (overrides->data_url) {
1b08cbce 1397 data_uri = overrides->data_url;
21a3f144
JR
1398 /* path is null */
1399 path = NULL;
1b08cbce
JR
1400 }
1401 }
1b08cbce
JR
1402 }
1403
21a3f144 1404
1b08cbce 1405 if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) {
dcf266c0
JG
1406 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1407 goto end;
1408 }
1409
1b08cbce 1410 if (control_uri || data_uri) {
dcf266c0
JG
1411 /* network destination */
1412 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
b664f89a
DG
1413 /*
1414 * URLs are provided for sure since the test above make sure that
1415 * with a live timer the data and control URIs are provided. So,
1416 * NULL is passed here and will be set right after.
1417 */
1418 ret = lttng_create_session_live(name, NULL, live_timer_interval);
dcf266c0
JG
1419 } else {
1420 ret = lttng_create_session(name, NULL);
1421 }
1422 if (ret) {
1423 goto end;
1424 }
1425
1b08cbce 1426 ret = create_session_net_output(name, control_uri, data_uri);
95681498
JG
1427 if (ret) {
1428 goto end;
dcf266c0 1429 }
95681498 1430
dcf266c0
JG
1431 } else {
1432 /* either local output or no output */
1b08cbce 1433 ret = lttng_create_session(name, path);
dcf266c0
JG
1434 if (ret) {
1435 goto end;
1436 }
1437 }
1438end:
1439 free(output.path);
1440 free(output.control_uri);
1441 free(output.data_uri);
1442 return ret;
1443}
1444static
1445int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1446 struct lttng_event_probe_attr *attr)
1447{
1448 int ret;
1449
1450 assert(probe_attribute_node);
1451 assert(attr);
1452
1453 if (!strcmp((const char *) probe_attribute_node->name,
1454 config_element_address)) {
1455 xmlChar *content;
1456 uint64_t addr = 0;
1457
1458 /* addr */
1459 content = xmlNodeGetContent(probe_attribute_node);
1460 if (!content) {
1461 ret = -LTTNG_ERR_NOMEM;
1462 goto end;
1463 }
1464
1465 ret = parse_uint(content, &addr);
1466 free(content);
1467 if (ret) {
1468 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1469 goto end;
1470 }
1471
1472 attr->addr = addr;
1473 } else if (!strcmp((const char *) probe_attribute_node->name,
1474 config_element_offset)) {
1475 xmlChar *content;
1476 uint64_t offset = 0;
1477
1478 /* offset */
1479 content = xmlNodeGetContent(probe_attribute_node);
1480 if (!content) {
1481 ret = -LTTNG_ERR_NOMEM;
1482 goto end;
1483 }
1484
1485 ret = parse_uint(content, &offset);
1486 free(content);
1487 if (ret) {
1488 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1489 goto end;
1490 }
1491
1492 attr->offset = offset;
1493 } else if (!strcmp((const char *) probe_attribute_node->name,
1494 config_element_symbol_name)) {
1495 xmlChar *content;
1496 size_t name_len;
1497
1498 /* symbol_name */
1499 content = xmlNodeGetContent(probe_attribute_node);
1500 if (!content) {
1501 ret = -LTTNG_ERR_NOMEM;
1502 goto end;
1503 }
1504
1505 name_len = strlen((char *) content);
1506 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1507 WARN("symbol_name too long.");
1508 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1509 free(content);
1510 goto end;
1511 }
1512
1513 strncpy(attr->symbol_name, (const char *) content, name_len);
1514 free(content);
1515 }
1516 ret = 0;
1517end:
1518 return ret;
1519}
1520
1521static
1522int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
d7b645e2 1523 const char *channel_name, const enum process_event_node_phase phase)
dcf266c0 1524{
d7b645e2 1525 int ret = 0, i;
dcf266c0
JG
1526 xmlNodePtr node;
1527 struct lttng_event event;
1528 char **exclusions = NULL;
1529 unsigned long exclusion_count = 0;
1530 char *filter_expression = NULL;
1531
1532 assert(event_node);
1533 assert(handle);
1534 assert(channel_name);
1535
1536 memset(&event, 0, sizeof(event));
1537
f40eba3d
JG
1538 /* Initialize default log level which varies by domain */
1539 switch (handle->domain.type)
1540 {
1541 case LTTNG_DOMAIN_JUL:
1542 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1543 break;
1544 case LTTNG_DOMAIN_LOG4J:
1545 event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1546 break;
1547 case LTTNG_DOMAIN_PYTHON:
1548 event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1549 break;
1550 case LTTNG_DOMAIN_UST:
1551 case LTTNG_DOMAIN_KERNEL:
1552 event.loglevel = LTTNG_LOGLEVEL_DEBUG;
1553 break;
1554 default:
1555 assert(0);
1556 }
1557
dcf266c0
JG
1558 for (node = xmlFirstElementChild(event_node); node;
1559 node = xmlNextElementSibling(node)) {
1560 if (!strcmp((const char *) node->name, config_element_name)) {
1561 xmlChar *content;
1562 size_t name_len;
1563
1564 /* name */
1565 content = xmlNodeGetContent(node);
1566 if (!content) {
1567 ret = -LTTNG_ERR_NOMEM;
1568 goto end;
1569 }
1570
1571 name_len = strlen((char *) content);
1572 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1573 WARN("Channel name too long.");
1574 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1575 free(content);
1576 goto end;
1577 }
1578
1579 strncpy(event.name, (const char *) content, name_len);
1580 free(content);
1581 } else if (!strcmp((const char *) node->name,
1582 config_element_enabled)) {
1583 xmlChar *content = xmlNodeGetContent(node);
1584
1585 /* enabled */
1586 if (!content) {
1587 ret = -LTTNG_ERR_NOMEM;
1588 goto end;
1589 }
1590
1591 ret = parse_bool(content, &event.enabled);
1592 free(content);
1593 if (ret) {
1594 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1595 goto end;
1596 }
1597 } else if (!strcmp((const char *) node->name,
1598 config_element_type)) {
1599 xmlChar *content = xmlNodeGetContent(node);
1600
1601 /* type */
1602 if (!content) {
1603 ret = -LTTNG_ERR_NOMEM;
1604 goto end;
1605 }
1606
1607 ret = get_event_type(content);
1608 free(content);
1609 if (ret < 0) {
1610 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1611 goto end;
1612 }
1613
1614 event.type = ret;
1615 } else if (!strcmp((const char *) node->name,
1616 config_element_loglevel_type)) {
1617 xmlChar *content = xmlNodeGetContent(node);
1618
1619 /* loglevel_type */
1620 if (!content) {
1621 ret = -LTTNG_ERR_NOMEM;
1622 goto end;
1623 }
1624
1625 ret = get_loglevel_type(content);
1626 free(content);
1627 if (ret < 0) {
1628 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1629 goto end;
1630 }
1631
1632 event.loglevel_type = ret;
1633 } else if (!strcmp((const char *) node->name,
1634 config_element_loglevel)) {
1635 xmlChar *content;
1636 int64_t loglevel = 0;
1637
1638 /* loglevel */
1639 content = xmlNodeGetContent(node);
1640 if (!content) {
1641 ret = -LTTNG_ERR_NOMEM;
1642 goto end;
1643 }
1644
1645 ret = parse_int(content, &loglevel);
1646 free(content);
1647 if (ret) {
1648 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1649 goto end;
1650 }
1651
1652 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1653 WARN("loglevel out of range.");
1654 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1655 goto end;
1656 }
1657
1658 event.loglevel = loglevel;
1659 } else if (!strcmp((const char *) node->name,
1660 config_element_filter)) {
1661 xmlChar *content =
1662 xmlNodeGetContent(node);
1663
1664 /* filter */
1665 if (!content) {
1666 ret = -LTTNG_ERR_NOMEM;
1667 goto end;
1668 }
1669
02d8ac3d 1670 free(filter_expression);
dcf266c0
JG
1671 filter_expression = strdup((char *) content);
1672 free(content);
1673 if (!filter_expression) {
1674 ret = -LTTNG_ERR_NOMEM;
1675 goto end;
1676 }
1677 } else if (!strcmp((const char *) node->name,
1678 config_element_exclusions)) {
1679 xmlNodePtr exclusion_node;
1680 int exclusion_index = 0;
1681
1682 /* exclusions */
1683 if (exclusions) {
1684 /*
1685 * Exclusions has already been initialized,
1686 * invalid file.
1687 */
1688 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1689 goto end;
1690 }
1691
1692 exclusion_count = xmlChildElementCount(node);
1693 if (!exclusion_count) {
1694 continue;
1695 }
1696
1697 exclusions = zmalloc(exclusion_count * sizeof(char *));
1698 if (!exclusions) {
1699 exclusion_count = 0;
1700 ret = -LTTNG_ERR_NOMEM;
1701 goto end;
1702 }
1703
1704 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1705 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1706 xmlChar *content =
1707 xmlNodeGetContent(exclusion_node);
1708
1709 if (!content) {
1710 ret = -LTTNG_ERR_NOMEM;
1711 goto end;
1712 }
1713
1714 exclusions[exclusion_index] = strdup((const char *) content);
1715 free(content);
1716 if (!exclusions[exclusion_index]) {
1717 ret = -LTTNG_ERR_NOMEM;
1718 goto end;
1719 }
1720 exclusion_index++;
1721 }
1722
1723 event.exclusion = 1;
1724 } else if (!strcmp((const char *) node->name,
1725 config_element_attributes)) {
1726 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1727
1728 /* attributes */
1729 if (!attribute_node) {
1730 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1731 goto end;
1732 }
1733
1734 if (!strcmp((const char *) node->name,
1735 config_element_probe_attributes)) {
1736 xmlNodePtr probe_attribute_node;
1737
1738 /* probe_attributes */
1739 for (probe_attribute_node =
1740 xmlFirstElementChild(attribute_node); probe_attribute_node;
1741 probe_attribute_node = xmlNextElementSibling(
1742 probe_attribute_node)) {
1743
1744 ret = process_probe_attribute_node(probe_attribute_node,
1745 &event.attr.probe);
1746 if (ret) {
1747 goto end;
1748 }
1749 }
1750 } else {
1751 size_t sym_len;
1752 xmlChar *content;
1753 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1754
1755 /* function_attributes */
1756 content = xmlNodeGetContent(symbol_node);
1757 if (!content) {
1758 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1759 goto end;
1760 }
1761
1762 sym_len = strlen((char *) content);
1763 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1764 WARN("Function name too long.");
1765 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1766 free(content);
1767 goto end;
1768 }
1769
1770 strncpy(event.attr.ftrace.symbol_name, (char *) content,
1771 sym_len);
1772 free(content);
1773 }
1774 }
1775 }
1776
d7b645e2
JR
1777 if ((event.enabled && phase == ENABLE) || phase == CREATION) {
1778 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1779 filter_expression, exclusion_count, exclusions);
1780 if (ret < 0) {
1781 WARN("Enabling event (name:%s) on load failed.", event.name);
1782 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1783 goto end;
1784 }
065321e9 1785 }
dcf266c0
JG
1786end:
1787 for (i = 0; i < exclusion_count; i++) {
1788 free(exclusions[i]);
1789 }
1790
1791 free(exclusions);
1792 free(filter_expression);
1793 return ret;
1794}
1795
1796static
1797int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1798 const char *channel_name)
1799{
1800 int ret = 0;
d7b645e2 1801 struct lttng_event event;
dcf266c0
JG
1802 xmlNodePtr node;
1803
1804 assert(events_node);
1805 assert(handle);
1806 assert(channel_name);
1807
1808 for (node = xmlFirstElementChild(events_node); node;
1809 node = xmlNextElementSibling(node)) {
d7b645e2 1810 ret = process_event_node(node, handle, channel_name, CREATION);
dcf266c0
JG
1811 if (ret) {
1812 goto end;
1813 }
1814 }
d7b645e2
JR
1815
1816 /*
1817 * Disable all events to enable only the necessary events.
1818 * Limitations regarding lttng_disable_events and tuple descriptor
1819 * force this approach.
1820 */
1821 memset(&event, 0, sizeof(event));
1822 event.loglevel = -1;
1823 event.type = LTTNG_EVENT_ALL;
1824 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
1825 if (ret) {
1826 goto end;
1827 }
1828
1829 for (node = xmlFirstElementChild(events_node); node;
1830 node = xmlNextElementSibling(node)) {
1831 ret = process_event_node(node, handle, channel_name, ENABLE);
1832 if (ret) {
1833 goto end;
1834 }
1835 }
1836
dcf266c0
JG
1837end:
1838 return ret;
1839}
1840
1841static
1842int process_channel_attr_node(xmlNodePtr attr_node,
1843 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1844 xmlNodePtr *events_node)
1845{
1846 int ret;
1847
1848 assert(attr_node);
1849 assert(channel);
1850 assert(contexts_node);
1851 assert(events_node);
1852
1853 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1854 xmlChar *content;
1855 size_t name_len;
1856
1857 /* name */
1858 content = xmlNodeGetContent(attr_node);
1859 if (!content) {
1860 ret = -LTTNG_ERR_NOMEM;
1861 goto end;
1862 }
1863
1864 name_len = strlen((char *) content);
1865 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1866 WARN("Channel name too long.");
1867 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1868 free(content);
1869 goto end;
1870 }
1871
1872 strncpy(channel->name, (const char *) content, name_len);
1873 free(content);
1874 } else if (!strcmp((const char *) attr_node->name,
1875 config_element_enabled)) {
1876 xmlChar *content;
1877 int enabled;
1878
1879 /* enabled */
1880 content = xmlNodeGetContent(attr_node);
1881 if (!content) {
1882 ret = -LTTNG_ERR_NOMEM;
1883 goto end;
1884 }
1885
1886 ret = parse_bool(content, &enabled);
1887 free(content);
1888 if (ret) {
1889 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1890 goto end;
1891 }
1892
1893 channel->enabled = enabled;
1894 } else if (!strcmp((const char *) attr_node->name,
1895 config_element_overwrite_mode)) {
1896 xmlChar *content;
1897
1898 /* overwrite_mode */
1899 content = xmlNodeGetContent(attr_node);
1900 if (!content) {
1901 ret = -LTTNG_ERR_NOMEM;
1902 goto end;
1903 }
1904
1905 ret = get_overwrite_mode(content);
1906 free(content);
1907 if (ret < 0) {
1908 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1909 goto end;
1910 }
1911
1912 channel->attr.overwrite = ret;
1913 } else if (!strcmp((const char *) attr_node->name,
1914 config_element_subbuf_size)) {
1915 xmlChar *content;
1916
1917 /* subbuffer_size */
1918 content = xmlNodeGetContent(attr_node);
1919 if (!content) {
1920 ret = -LTTNG_ERR_NOMEM;
1921 goto end;
1922 }
1923
1924 ret = parse_uint(content, &channel->attr.subbuf_size);
1925 free(content);
1926 if (ret) {
1927 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1928 goto end;
1929 }
1930 } else if (!strcmp((const char *) attr_node->name,
1931 config_element_num_subbuf)) {
1932 xmlChar *content;
1933
1934 /* subbuffer_count */
1935 content = xmlNodeGetContent(attr_node);
1936 if (!content) {
1937 ret = -LTTNG_ERR_NOMEM;
1938 goto end;
1939 }
1940
1941 ret = parse_uint(content, &channel->attr.num_subbuf);
1942 free(content);
1943 if (ret) {
1944 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1945 goto end;
1946 }
1947 } else if (!strcmp((const char *) attr_node->name,
1948 config_element_switch_timer_interval)) {
1949 xmlChar *content;
1950 uint64_t switch_timer_interval = 0;
1951
1952 /* switch_timer_interval */
1953 content = xmlNodeGetContent(attr_node);
1954 if (!content) {
1955 ret = -LTTNG_ERR_NOMEM;
1956 goto end;
1957 }
1958
1959 ret = parse_uint(content, &switch_timer_interval);
1960 free(content);
1961 if (ret) {
1962 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1963 goto end;
1964 }
1965
1966 if (switch_timer_interval > UINT_MAX) {
1967 WARN("switch_timer_interval out of range.");
1968 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1969 goto end;
1970 }
1971
1972 channel->attr.switch_timer_interval =
1973 switch_timer_interval;
1974 } else if (!strcmp((const char *) attr_node->name,
1975 config_element_read_timer_interval)) {
1976 xmlChar *content;
1977 uint64_t read_timer_interval = 0;
1978
1979 /* read_timer_interval */
1980 content = xmlNodeGetContent(attr_node);
1981 if (!content) {
1982 ret = -LTTNG_ERR_NOMEM;
1983 goto end;
1984 }
1985
1986 ret = parse_uint(content, &read_timer_interval);
1987 free(content);
1988 if (ret) {
1989 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1990 goto end;
1991 }
1992
1993 if (read_timer_interval > UINT_MAX) {
1994 WARN("read_timer_interval out of range.");
1995 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1996 goto end;
1997 }
1998
1999 channel->attr.read_timer_interval =
2000 read_timer_interval;
2001 } else if (!strcmp((const char *) attr_node->name,
2002 config_element_output_type)) {
2003 xmlChar *content;
2004
2005 /* output_type */
2006 content = xmlNodeGetContent(attr_node);
2007 if (!content) {
2008 ret = -LTTNG_ERR_NOMEM;
2009 goto end;
2010 }
2011
2012 ret = get_output_type(content);
2013 free(content);
2014 if (ret < 0) {
2015 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2016 goto end;
2017 }
2018
2019 channel->attr.output = ret;
2020 } else if (!strcmp((const char *) attr_node->name,
2021 config_element_tracefile_size)) {
2022 xmlChar *content;
2023
2024 /* tracefile_size */
2025 content = xmlNodeGetContent(attr_node);
2026 if (!content) {
2027 ret = -LTTNG_ERR_NOMEM;
2028 goto end;
2029 }
2030
2031 ret = parse_uint(content, &channel->attr.tracefile_size);
2032 free(content);
2033 if (ret) {
2034 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2035 goto end;
2036 }
2037 } else if (!strcmp((const char *) attr_node->name,
2038 config_element_tracefile_count)) {
2039 xmlChar *content;
2040
2041 /* tracefile_count */
2042 content = xmlNodeGetContent(attr_node);
2043 if (!content) {
2044 ret = -LTTNG_ERR_NOMEM;
2045 goto end;
2046 }
2047
2048 ret = parse_uint(content, &channel->attr.tracefile_count);
2049 free(content);
2050 if (ret) {
2051 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2052 goto end;
2053 }
2054 } else if (!strcmp((const char *) attr_node->name,
2055 config_element_live_timer_interval)) {
2056 xmlChar *content;
2057 uint64_t live_timer_interval = 0;
2058
2059 /* live_timer_interval */
2060 content = xmlNodeGetContent(attr_node);
2061 if (!content) {
2062 ret = -LTTNG_ERR_NOMEM;
2063 goto end;
2064 }
2065
2066 ret = parse_uint(content, &live_timer_interval);
2067 free(content);
2068 if (ret) {
2069 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2070 goto end;
2071 }
2072
2073 if (live_timer_interval > UINT_MAX) {
2074 WARN("live_timer_interval out of range.");
2075 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2076 goto end;
2077 }
2078
2079 channel->attr.live_timer_interval =
2080 live_timer_interval;
2081 } else if (!strcmp((const char *) attr_node->name,
2082 config_element_events)) {
2083 /* events */
2084 *events_node = attr_node;
2085 } else {
2086 /* contexts */
2087 *contexts_node = attr_node;
2088 }
2089 ret = 0;
2090end:
2091 return ret;
2092}
2093
2094static
2095int process_context_node(xmlNodePtr context_node,
2096 struct lttng_handle *handle, const char *channel_name)
2097{
2098 int ret;
2099 struct lttng_event_context context;
2100 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2101
2102 assert(handle);
2103 assert(channel_name);
2104
2105 if (!context_child_node) {
2106 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2107 goto end;
2108 }
2109
2110 memset(&context, 0, sizeof(context));
2111
2112 if (!strcmp((const char *) context_child_node->name,
2113 config_element_type)) {
2114 /* type */
2115 xmlChar *content = xmlNodeGetContent(context_child_node);
045fc617 2116
dcf266c0
JG
2117 if (!content) {
2118 ret = -LTTNG_ERR_NOMEM;
2119 goto end;
2120 }
2121
2122 ret = get_context_type(content);
2123 free(content);
2124 if (ret < 0) {
2125 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2126 goto end;
2127 }
2128
2129 context.ctx = ret;
045fc617
JG
2130 } else if (!strcmp((const char *) context_child_node->name,
2131 config_element_context_perf)) {
2132 /* perf */
dcf266c0
JG
2133 xmlNodePtr perf_attr_node;
2134
14ce5bd8
JG
2135 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2136 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2137 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
2138 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2139 perf_attr_node; perf_attr_node =
2140 xmlNextElementSibling(perf_attr_node)) {
2141 if (!strcmp((const char *) perf_attr_node->name,
2142 config_element_type)) {
2143 xmlChar *content;
2144 uint64_t type = 0;
2145
2146 /* type */
2147 content = xmlNodeGetContent(perf_attr_node);
2148 if (!content) {
2149 ret = -LTTNG_ERR_NOMEM;
2150 goto end;
2151 }
2152
2153 ret = parse_uint(content, &type);
2154 free(content);
2155 if (ret) {
2156 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2157 goto end;
2158 }
2159
2160 if (type > UINT32_MAX) {
2161 WARN("perf context type out of range.");
2162 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2163 goto end;
2164 }
2165
2166 context.u.perf_counter.type = type;
2167 } else if (!strcmp((const char *) perf_attr_node->name,
2168 config_element_config)) {
2169 xmlChar *content;
2170 uint64_t config = 0;
2171
2172 /* config */
2173 content = xmlNodeGetContent(perf_attr_node);
2174 if (!content) {
2175 ret = -LTTNG_ERR_NOMEM;
2176 goto end;
2177 }
2178
2179 ret = parse_uint(content, &config);
2180 free(content);
2181 if (ret) {
2182 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2183 goto end;
2184 }
2185
2186 context.u.perf_counter.config = config;
2187 } else if (!strcmp((const char *) perf_attr_node->name,
2188 config_element_name)) {
2189 xmlChar *content;
2190 size_t name_len;
2191
2192 /* name */
2193 content = xmlNodeGetContent(perf_attr_node);
2194 if (!content) {
2195 ret = -LTTNG_ERR_NOMEM;
2196 goto end;
2197 }
2198
2199 name_len = strlen((char *) content);
2200 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2201 WARN("perf context name too long.");
2202 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2203 free(content);
2204 goto end;
2205 }
2206
2207 strncpy(context.u.perf_counter.name, (const char *) content,
2208 name_len);
2209 free(content);
2210 }
2211 }
045fc617
JG
2212 } else if (!strcmp((const char *) context_child_node->name,
2213 config_element_context_app)) {
2214 /* application context */
2215 xmlNodePtr app_ctx_node;
2216
2217 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2218 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2219 app_ctx_node; app_ctx_node =
2220 xmlNextElementSibling(app_ctx_node)) {
2221 xmlChar *content;
2222 char **target = strcmp(
2223 (const char *) app_ctx_node->name,
2224 config_element_context_app_provider_name) == 0 ?
2225 &context.u.app_ctx.provider_name :
2226 &context.u.app_ctx.ctx_name;
2227
2228 content = xmlNodeGetContent(app_ctx_node);
2229 if (!content) {
2230 ret = -LTTNG_ERR_NOMEM;
2231 goto end;
2232 }
2233
2234 *target = (char *) content;
2235 }
2236 } else {
2237 /* Unrecognized context type */
2238 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2239 goto end;
dcf266c0
JG
2240 }
2241
2242 ret = lttng_add_context(handle, &context, NULL, channel_name);
045fc617
JG
2243 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2244 free(context.u.app_ctx.provider_name);
2245 free(context.u.app_ctx.ctx_name);
2246 }
dcf266c0
JG
2247end:
2248 return ret;
2249}
2250
2251static
2252int process_contexts_node(xmlNodePtr contexts_node,
2253 struct lttng_handle *handle, const char *channel_name)
2254{
2255 int ret = 0;
2256 xmlNodePtr context_node;
2257
2258 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2259 context_node = xmlNextElementSibling(context_node)) {
2260 ret = process_context_node(context_node, handle, channel_name);
2261 if (ret) {
2262 goto end;
2263 }
2264 }
2265end:
2266 return ret;
2267}
2268
847a5916
JR
2269static
2270int process_pid_tracker_node(xmlNodePtr pid_tracker_node,
2271 struct lttng_handle *handle)
2272{
dd49e13f 2273 int ret = 0, child;
847a5916
JR
2274 xmlNodePtr targets_node = NULL;
2275 xmlNodePtr node;
2276
2277 assert(handle);
2278 assert(pid_tracker_node);
2279 /* get the targets node */
2280 for (node = xmlFirstElementChild(pid_tracker_node); node;
2281 node = xmlNextElementSibling(node)) {
2282 if (!strcmp((const char *) node->name,
2283 config_element_targets)) {
2284 targets_node = node;
2285 break;
2286 }
2287 }
2288
2289 if (!targets_node) {
2290 ret = LTTNG_ERR_INVALID;
2291 goto end;
2292 }
2293
2294 /* Go through all pid_target node */
2295 child = xmlChildElementCount(targets_node);
2296 if (child == 0) {
2297 /* The session is explicitly set to target nothing. */
2298 ret = lttng_untrack_pid(handle, -1);
2299 if (ret) {
2300 goto end;
2301 }
2302 }
2303 for (node = xmlFirstElementChild(targets_node); node;
2304 node = xmlNextElementSibling(node)) {
2305 xmlNodePtr pid_target_node = node;
2306
2307 /* get pid node and track it */
2308 for (node = xmlFirstElementChild(pid_target_node); node;
2309 node = xmlNextElementSibling(node)) {
2310 if (!strcmp((const char *) node->name,
2311 config_element_pid)) {
2312 int64_t pid;
2313 xmlChar *content = NULL;
2314
2315 content = xmlNodeGetContent(node);
2316 if (!content) {
2317 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2318 goto end;
2319 }
2320
2321 ret = parse_int(content, &pid);
2322 free(content);
2323 if (ret) {
2324 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2325 goto end;
2326 }
2327
2328 ret = lttng_track_pid(handle, (int) pid);
2329 if (ret) {
2330 goto end;
2331 }
2332 }
2333 }
2334 node = pid_target_node;
2335 }
2336
2337end:
2338 return ret;
2339}
2340
2341
dcf266c0
JG
2342static
2343int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2344{
2345 int ret;
2346 struct lttng_domain domain = { 0 };
2347 struct lttng_handle *handle = NULL;
2348 xmlNodePtr channels_node = NULL;
847a5916
JR
2349 xmlNodePtr trackers_node = NULL;
2350 xmlNodePtr pid_tracker_node = NULL;
dcf266c0
JG
2351 xmlNodePtr node;
2352
2353 assert(session_name);
2354
2355 ret = init_domain(domain_node, &domain);
2356 if (ret) {
2357 goto end;
2358 }
2359
2360 handle = lttng_create_handle(session_name, &domain);
2361 if (!handle) {
2362 ret = -LTTNG_ERR_NOMEM;
2363 goto end;
2364 }
2365
2366 /* get the channels node */
2367 for (node = xmlFirstElementChild(domain_node); node;
2368 node = xmlNextElementSibling(node)) {
2369 if (!strcmp((const char *) node->name,
2370 config_element_channels)) {
2371 channels_node = node;
2372 break;
2373 }
2374 }
2375
2376 if (!channels_node) {
2377 goto end;
2378 }
2379
2380 /* create all channels */
2381 for (node = xmlFirstElementChild(channels_node); node;
2382 node = xmlNextElementSibling(node)) {
2383 struct lttng_channel channel;
2384 xmlNodePtr contexts_node = NULL;
2385 xmlNodePtr events_node = NULL;
2386 xmlNodePtr channel_attr_node;
2387
2388 memset(&channel, 0, sizeof(channel));
2389 lttng_channel_set_default_attr(&domain, &channel.attr);
2390
2391 for (channel_attr_node = xmlFirstElementChild(node);
2392 channel_attr_node; channel_attr_node =
2393 xmlNextElementSibling(channel_attr_node)) {
2394 ret = process_channel_attr_node(channel_attr_node,
2395 &channel, &contexts_node, &events_node);
2396 if (ret) {
2397 goto end;
2398 }
2399 }
2400
2401 ret = lttng_enable_channel(handle, &channel);
2402 if (ret < 0) {
2403 goto end;
2404 }
2405
2406 ret = process_events_node(events_node, handle, channel.name);
2407 if (ret) {
2408 goto end;
2409 }
2410
2411 ret = process_contexts_node(contexts_node, handle,
2412 channel.name);
2413 if (ret) {
2414 goto end;
2415 }
2416 }
847a5916
JR
2417
2418 /* get the trackers node */
2419 for (node = xmlFirstElementChild(domain_node); node;
2420 node = xmlNextElementSibling(node)) {
2421 if (!strcmp((const char *) node->name,
2422 config_element_trackers)) {
2423 trackers_node = node;
2424 break;
2425 }
2426 }
2427
2428 if (!trackers_node) {
2429 goto end;
2430 }
2431
2432 for (node = xmlFirstElementChild(trackers_node); node;
2433 node = xmlNextElementSibling(node)) {
2434 if (!strcmp((const char *)node->name,config_element_pid_tracker)) {
2435 pid_tracker_node = node;
2436 ret = process_pid_tracker_node(pid_tracker_node, handle);
2437 if (ret) {
2438 goto end;
2439 }
2440 }
2441 }
2442
2443 if (!pid_tracker_node) {
2444 lttng_track_pid(handle, -1);
2445 }
2446
dcf266c0
JG
2447end:
2448 lttng_destroy_handle(handle);
2449 return ret;
2450}
2451
2452static
2453int process_session_node(xmlNodePtr session_node, const char *session_name,
1b08cbce
JR
2454 int overwrite,
2455 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
2456{
2457 int ret, started = -1, snapshot_mode = -1;
2458 uint64_t live_timer_interval = UINT64_MAX;
d324faf7 2459 xmlChar *name = NULL;
9e7c9f56 2460 xmlChar *shm_path = NULL;
dcf266c0
JG
2461 xmlNodePtr domains_node = NULL;
2462 xmlNodePtr output_node = NULL;
2463 xmlNodePtr node;
2464 struct lttng_domain *kernel_domain = NULL;
2465 struct lttng_domain *ust_domain = NULL;
2466 struct lttng_domain *jul_domain = NULL;
5cdb6027 2467 struct lttng_domain *log4j_domain = NULL;
0e115563 2468 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
2469
2470 for (node = xmlFirstElementChild(session_node); node;
2471 node = xmlNextElementSibling(node)) {
2472 if (!name && !strcmp((const char *) node->name,
2473 config_element_name)) {
2474 /* name */
2475 xmlChar *node_content = xmlNodeGetContent(node);
2476 if (!node_content) {
2477 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2478 goto error;
dcf266c0
JG
2479 }
2480
d324faf7 2481 name = node_content;
dcf266c0
JG
2482 } else if (!domains_node && !strcmp((const char *) node->name,
2483 config_element_domains)) {
2484 /* domains */
2485 domains_node = node;
2486 } else if (started == -1 && !strcmp((const char *) node->name,
2487 config_element_started)) {
2488 /* started */
2489 xmlChar *node_content = xmlNodeGetContent(node);
2490 if (!node_content) {
2491 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2492 goto error;
dcf266c0
JG
2493 }
2494
2495 ret = parse_bool(node_content, &started);
2496 free(node_content);
2497 if (ret) {
2498 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2499 goto error;
dcf266c0
JG
2500 }
2501 } else if (!output_node && !strcmp((const char *) node->name,
2502 config_element_output)) {
2503 /* output */
2504 output_node = node;
9e7c9f56
JR
2505 } else if (!shm_path && !strcmp((const char *) node->name,
2506 config_element_shared_memory_path)) {
2507 /* shared memory path */
2508 xmlChar *node_content = xmlNodeGetContent(node);
2509 if (!node_content) {
2510 ret = -LTTNG_ERR_NOMEM;
2511 goto error;
2512 }
2513
2514 shm_path = node_content;
dcf266c0
JG
2515 } else {
2516 /* attributes, snapshot_mode or live_timer_interval */
2517 xmlNodePtr attributes_child =
2518 xmlFirstElementChild(node);
2519
2520 if (!strcmp((const char *) attributes_child->name,
2521 config_element_snapshot_mode)) {
2522 /* snapshot_mode */
2523 xmlChar *snapshot_mode_content =
2524 xmlNodeGetContent(attributes_child);
2525 if (!snapshot_mode_content) {
2526 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2527 goto error;
dcf266c0
JG
2528 }
2529
2530 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2531 free(snapshot_mode_content);
2532 if (ret) {
2533 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2534 goto error;
dcf266c0
JG
2535 }
2536 } else {
2537 /* live_timer_interval */
2538 xmlChar *timer_interval_content =
2539 xmlNodeGetContent(attributes_child);
2540 if (!timer_interval_content) {
2541 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2542 goto error;
dcf266c0
JG
2543 }
2544
2545 ret = parse_uint(timer_interval_content, &live_timer_interval);
2546 free(timer_interval_content);
2547 if (ret) {
2548 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2549 goto error;
dcf266c0
JG
2550 }
2551 }
2552 }
2553 }
2554
2555 if (!name) {
2556 /* Mandatory attribute, as defined in the session XSD */
2557 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2558 goto error;
dcf266c0
JG
2559 }
2560
d324faf7 2561 if (session_name && strcmp((char *) name, session_name)) {
dcf266c0 2562 /* This is not the session we are looking for */
c2da8cde
DG
2563 ret = -LTTNG_ERR_NO_SESSION;
2564 goto error;
dcf266c0
JG
2565 }
2566
2567 /* Init domains to create the session handles */
2568 for (node = xmlFirstElementChild(domains_node); node;
2569 node = xmlNextElementSibling(node)) {
2570 struct lttng_domain *domain;
2571
2572 domain = zmalloc(sizeof(*domain));
2573 if (!domain) {
2574 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2575 goto error;
dcf266c0
JG
2576 }
2577
2578 ret = init_domain(node, domain);
2579 if (ret) {
2580 goto domain_init_error;
2581 }
2582
2583 switch (domain->type) {
2584 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
2585 if (kernel_domain) {
2586 /* Same domain seen twice, invalid! */
2587 goto domain_init_error;
2588 }
dcf266c0
JG
2589 kernel_domain = domain;
2590 break;
2591 case LTTNG_DOMAIN_UST:
c33e6729
DG
2592 if (ust_domain) {
2593 /* Same domain seen twice, invalid! */
2594 goto domain_init_error;
2595 }
dcf266c0
JG
2596 ust_domain = domain;
2597 break;
2598 case LTTNG_DOMAIN_JUL:
c33e6729
DG
2599 if (jul_domain) {
2600 /* Same domain seen twice, invalid! */
2601 goto domain_init_error;
2602 }
dcf266c0
JG
2603 jul_domain = domain;
2604 break;
5cdb6027
DG
2605 case LTTNG_DOMAIN_LOG4J:
2606 if (log4j_domain) {
2607 /* Same domain seen twice, invalid! */
2608 goto domain_init_error;
2609 }
2610 log4j_domain = domain;
2611 break;
0e115563
DG
2612 case LTTNG_DOMAIN_PYTHON:
2613 if (python_domain) {
2614 /* Same domain seen twice, invalid! */
2615 goto domain_init_error;
2616 }
2617 python_domain = domain;
2618 break;
dcf266c0
JG
2619 default:
2620 WARN("Invalid domain type");
2621 goto domain_init_error;
2622 }
2623 continue;
2624domain_init_error:
2625 free(domain);
2626 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2627 goto error;
dcf266c0
JG
2628 }
2629
2aaf5fc7
JR
2630 /* Apply overrides */
2631 if (overrides) {
2632 if (overrides->session_name) {
2633 xmlChar *name_override = xmlStrdup(BAD_CAST(overrides->session_name));
2634 if (!name_override) {
2635 ret = -LTTNG_ERR_NOMEM;
2636 goto error;
2637 }
2638
2639 /* Overrides the session name to the provided name */
2640 xmlFree(name);
2641 name = name_override;
2642 }
2643 }
2644
40b4155f 2645 if (overwrite) {
dcf266c0 2646 /* Destroy session if it exists */
d324faf7 2647 ret = lttng_destroy_session((const char *) name);
dcf266c0
JG
2648 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2649 ERR("Failed to destroy existing session.");
c2da8cde 2650 goto error;
dcf266c0
JG
2651 }
2652 }
2653
2654 /* Create session type depending on output type */
2655 if (snapshot_mode && snapshot_mode != -1) {
1b08cbce
JR
2656 ret = create_snapshot_session((const char *) name, output_node,
2657 overrides);
dcf266c0
JG
2658 } else if (live_timer_interval &&
2659 live_timer_interval != UINT64_MAX) {
d324faf7
JG
2660 ret = create_session((const char *) name, kernel_domain,
2661 ust_domain, jul_domain, log4j_domain,
1b08cbce 2662 output_node, live_timer_interval, overrides);
dcf266c0
JG
2663 } else {
2664 /* regular session */
d324faf7
JG
2665 ret = create_session((const char *) name, kernel_domain,
2666 ust_domain, jul_domain, log4j_domain,
1b08cbce 2667 output_node, UINT64_MAX, overrides);
dcf266c0 2668 }
dcf266c0 2669 if (ret) {
c2da8cde 2670 goto error;
dcf266c0
JG
2671 }
2672
9e7c9f56
JR
2673 if (shm_path) {
2674 ret = lttng_set_session_shm_path((const char *) name,
d324faf7 2675 (const char *) shm_path);
9e7c9f56
JR
2676 if (ret) {
2677 goto error;
2678 }
2679 }
2680
dcf266c0
JG
2681 for (node = xmlFirstElementChild(domains_node); node;
2682 node = xmlNextElementSibling(node)) {
d324faf7 2683 ret = process_domain_node(node, (const char *) name);
dcf266c0
JG
2684 if (ret) {
2685 goto end;
2686 }
2687 }
2688
2689 if (started) {
d324faf7 2690 ret = lttng_start_tracing((const char *) name);
dcf266c0
JG
2691 if (ret) {
2692 goto end;
2693 }
2694 }
c2da8cde 2695
dcf266c0 2696end:
b2579dc1 2697 if (ret < 0) {
d324faf7
JG
2698 ERR("Failed to load session %s: %s", (const char *) name,
2699 lttng_strerror(ret));
2700 lttng_destroy_session((const char *) name);
b2579dc1
JG
2701 }
2702
c2da8cde 2703error:
dcf266c0
JG
2704 free(kernel_domain);
2705 free(ust_domain);
2706 free(jul_domain);
5cdb6027 2707 free(log4j_domain);
135a3893 2708 free(python_domain);
d324faf7 2709 xmlFree(name);
9e7c9f56 2710 xmlFree(shm_path);
dcf266c0
JG
2711 return ret;
2712}
2713
cf53c06d
DG
2714/*
2715 * Return 1 if the given path is readable by the current UID or 0 if not.
2716 * Return -1 if the path is EPERM.
2717 */
2718static int validate_file_read_creds(const char *path)
2719{
2720 int ret;
2721
2722 assert(path);
2723
2724 /* Can we read the file. */
2725 ret = access(path, R_OK);
2726 if (!ret) {
2727 goto valid;
2728 }
2729 if (errno == EACCES) {
2730 return -1;
2731 } else {
2732 /* Invalid. */
2733 return 0;
2734 }
2735valid:
2736 return 1;
2737}
2738
dcf266c0
JG
2739static
2740int load_session_from_file(const char *path, const char *session_name,
1b08cbce
JR
2741 struct session_config_validation_ctx *validation_ctx, int overwrite,
2742 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
2743{
2744 int ret, session_found = !session_name;
2745 xmlDocPtr doc = NULL;
2746 xmlNodePtr sessions_node;
2747 xmlNodePtr session_node;
dcf266c0
JG
2748
2749 assert(path);
2750 assert(validation_ctx);
2751
cf53c06d
DG
2752 ret = validate_file_read_creds(path);
2753 if (ret != 1) {
2754 if (ret == -1) {
2755 ret = -LTTNG_ERR_EPERM;
2756 } else {
2757 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2758 }
dcf266c0
JG
2759 goto end;
2760 }
2761
2762 doc = xmlParseFile(path);
2763 if (!doc) {
2764 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2765 goto end;
2766 }
2767
2768 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2769 if (ret) {
2770 ERR("Session configuration file validation failed");
2771 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2772 goto end;
2773 }
2774
2775 sessions_node = xmlDocGetRootElement(doc);
2776 if (!sessions_node) {
2777 goto end;
2778 }
2779
2780 for (session_node = xmlFirstElementChild(sessions_node);
2781 session_node; session_node =
2782 xmlNextElementSibling(session_node)) {
2783 ret = process_session_node(session_node,
1b08cbce 2784 session_name, overwrite, overrides);
dcf266c0
JG
2785 if (session_name && ret == 0) {
2786 /* Target session found and loaded */
2787 session_found = 1;
2788 break;
2789 }
2790 }
2791end:
2792 xmlFreeDoc(doc);
2793 if (!ret) {
a96bc65d 2794 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
2795 }
2796 return ret;
2797}
2798
60f70355
JG
2799/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2800static
2801struct dirent *alloc_dirent(const char *path)
2802{
2803 size_t len;
2804 long name_max;
2805 struct dirent *entry;
2806
2807 name_max = pathconf(path, _PC_NAME_MAX);
2808 if (name_max == -1) {
2809 name_max = PATH_MAX;
2810 }
2811 len = offsetof(struct dirent, d_name) + name_max + 1;
2812 entry = zmalloc(len);
2813 return entry;
2814}
2815
dcf266c0
JG
2816static
2817int load_session_from_path(const char *path, const char *session_name,
1b08cbce
JR
2818 struct session_config_validation_ctx *validation_ctx, int overwrite,
2819 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
2820{
2821 int ret, session_found = !session_name;
dcf266c0
JG
2822 DIR *directory = NULL;
2823
2824 assert(path);
2825 assert(validation_ctx);
2826
4af16958
DG
2827 directory = opendir(path);
2828 if (!directory) {
11143783
DG
2829 switch (errno) {
2830 case ENOTDIR:
0f0a81b5
DG
2831 /* Try the file loading. */
2832 break;
11143783
DG
2833 case ENOENT:
2834 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2835 goto end;
2836 default:
0f0a81b5
DG
2837 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2838 goto end;
4af16958 2839 }
dcf266c0 2840 }
4af16958 2841 if (directory) {
dcf266c0
JG
2842 struct dirent *entry;
2843 struct dirent *result;
2844 char *file_path = NULL;
2845 size_t path_len = strlen(path);
2846
2847 if (path_len >= PATH_MAX) {
2848 ret = -LTTNG_ERR_INVALID;
2849 goto end;
2850 }
2851
60f70355 2852 entry = alloc_dirent(path);
dcf266c0
JG
2853 if (!entry) {
2854 ret = -LTTNG_ERR_NOMEM;
2855 goto end;
2856 }
2857
dcf266c0
JG
2858 file_path = zmalloc(PATH_MAX);
2859 if (!file_path) {
2860 ret = -LTTNG_ERR_NOMEM;
2861 free(entry);
2862 goto end;
2863 }
2864
2865 strncpy(file_path, path, path_len);
2866 if (file_path[path_len - 1] != '/') {
2867 file_path[path_len++] = '/';
2868 }
2869
4af16958 2870 ret = 0;
dcf266c0
JG
2871 /* Search for *.lttng files */
2872 while (!readdir_r(directory, entry, &result) && result) {
2873 size_t file_name_len = strlen(result->d_name);
2874
2875 if (file_name_len <=
2876 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2877 continue;
2878 }
2879
431f702e 2880 if (path_len + file_name_len >= PATH_MAX) {
dcf266c0
JG
2881 continue;
2882 }
2883
2884 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2885 result->d_name + file_name_len - sizeof(
2886 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2887 continue;
2888 }
2889
2890 strncpy(file_path + path_len, result->d_name, file_name_len);
2891 file_path[path_len + file_name_len] = '\0';
2892
2893 ret = load_session_from_file(file_path, session_name,
1b08cbce 2894 validation_ctx, overwrite, overrides);
dcf266c0
JG
2895 if (session_name && !ret) {
2896 session_found = 1;
2897 break;
2898 }
2899 }
2900
2901 free(entry);
2902 free(file_path);
2903 } else {
2904 ret = load_session_from_file(path, session_name,
1b08cbce 2905 validation_ctx, overwrite, overrides);
dcf266c0
JG
2906 if (ret) {
2907 goto end;
2908 } else {
2909 session_found = 1;
2910 }
2911 }
2912
2913end:
2914 if (directory) {
2915 if (closedir(directory)) {
2916 PERROR("closedir");
2917 }
2918 }
2919
464133ea 2920 if (session_found && !ret) {
6c66fa0f 2921 ret = 0;
dcf266c0
JG
2922 }
2923
2924 return ret;
2925}
2926
ab38c13f
DG
2927/*
2928 * Validate that the given path's credentials and the current process have the
cf53c06d 2929 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
2930 */
2931static int validate_path_creds(const char *path)
2932{
2933 int ret, uid = getuid();
2934 struct stat buf;
2935
2936 assert(path);
2937
cf53c06d 2938 if (uid == 0) {
ab38c13f
DG
2939 goto valid;
2940 }
2941
2942 ret = stat(path, &buf);
2943 if (ret < 0) {
2944 if (errno != ENOENT) {
2945 PERROR("stat");
2946 }
2947 ret = -LTTNG_ERR_INVALID;
2948 goto valid;
2949 }
2950
2951 if (buf.st_uid != uid) {
2952 goto invalid;
2953 }
2954
2955valid:
ab38c13f 2956 return 1;
cf53c06d
DG
2957invalid:
2958 return 0;
ab38c13f
DG
2959}
2960
dcf266c0
JG
2961LTTNG_HIDDEN
2962int config_load_session(const char *path, const char *session_name,
1b08cbce
JR
2963 int overwrite, unsigned int autoload,
2964 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
2965{
2966 int ret;
6c66fa0f 2967 bool session_loaded = false;
cf53c06d 2968 const char *path_ptr = NULL;
dcf266c0
JG
2969 struct session_config_validation_ctx validation_ctx = { 0 };
2970
2971 ret = init_session_config_validation_ctx(&validation_ctx);
2972 if (ret) {
2973 goto end;
2974 }
2975
2976 if (!path) {
ab38c13f
DG
2977 char *home_path;
2978 const char *sys_path;
2979
dcf266c0 2980 /* Try home path */
ab38c13f 2981 home_path = utils_get_home_dir();
dcf266c0 2982 if (home_path) {
ab38c13f 2983 char path[PATH_MAX];
dcf266c0 2984
d4fcf703
DG
2985 /*
2986 * Try user session configuration path. Ignore error here so we can
2987 * continue loading the system wide sessions.
2988 */
ab38c13f
DG
2989 if (autoload) {
2990 ret = snprintf(path, sizeof(path),
2991 DEFAULT_SESSION_HOME_CONFIGPATH "/"
2992 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
2993 if (ret < 0) {
2994 PERROR("snprintf session autoload home config path");
2995 goto end;
2996 }
2997
2998 /*
2999 * Credentials are only validated for the autoload in order to
3000 * avoid any user session daemon to try to load kernel sessions
3001 * automatically and failing all the times.
3002 */
3003 ret = validate_path_creds(path);
3004 if (ret) {
3005 path_ptr = path;
3006 }
ab38c13f
DG
3007 } else {
3008 ret = snprintf(path, sizeof(path),
3009 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
3010 if (ret < 0) {
3011 PERROR("snprintf session home config path");
3012 goto end;
3013 }
3014 path_ptr = path;
ab38c13f 3015 }
cf53c06d
DG
3016 if (path_ptr) {
3017 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 3018 &validation_ctx, overwrite, overrides);
d4fcf703
DG
3019 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3020 goto end;
3021 }
3022 /*
3023 * Continue even if the session was found since we have to try
3024 * the system wide sessions.
3025 */
6c66fa0f 3026 session_loaded = true;
ab38c13f 3027 }
d4fcf703 3028 }
ab38c13f 3029
cf53c06d
DG
3030 /* Reset path pointer for the system wide dir. */
3031 path_ptr = NULL;
3032
d4fcf703
DG
3033 /* Try system wide configuration directory. */
3034 if (autoload) {
3035 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
3036 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
3037 ret = validate_path_creds(sys_path);
3038 if (ret) {
3039 path_ptr = sys_path;
3040 }
d4fcf703 3041 } else {
cf53c06d
DG
3042 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
3043 path_ptr = sys_path;
d4fcf703
DG
3044 }
3045
cf53c06d
DG
3046 if (path_ptr) {
3047 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 3048 &validation_ctx, overwrite, overrides);
6c66fa0f
JG
3049 if (!ret) {
3050 session_loaded = true;
3051 }
dcf266c0
JG
3052 }
3053 } else {
3054 ret = access(path, F_OK);
3055 if (ret < 0) {
3056 PERROR("access");
3057 switch (errno) {
3058 case ENOENT:
3059 ret = -LTTNG_ERR_INVALID;
3060 WARN("Session configuration path does not exist.");
3061 break;
3062 case EACCES:
3063 ret = -LTTNG_ERR_EPERM;
3064 break;
3065 default:
3066 ret = -LTTNG_ERR_UNK;
3067 break;
3068 }
3069 goto end;
3070 }
3071
3072 ret = load_session_from_path(path, session_name,
1b08cbce 3073 &validation_ctx, overwrite, overrides);
dcf266c0
JG
3074 }
3075end:
3076 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
3077 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
3078 /*
3079 * Don't report an error if no sessions are found when called
3080 * without a session_name or a search path.
3081 */
3082 ret = 0;
3083 }
6c66fa0f
JG
3084
3085 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
3086 /* A matching session was found in one of the search paths. */
3087 ret = 0;
3088 }
dcf266c0
JG
3089 return ret;
3090}
56766c75
JG
3091
3092static
3093void __attribute__((destructor)) session_config_exit(void)
3094{
3095 xmlCleanupParser();
3096}
This page took 0.17731 seconds and 5 git commands to generate.