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