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