Add kernel and UST time namespace context
[lttng-tools.git] / src / common / config / session-config.c
CommitLineData
1501a7f3 1/*
ab5be9fa 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
1501a7f3 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
1501a7f3 5 *
1501a7f3
JG
6 */
7
159b042f 8#include "lttng/tracker.h"
6c1c0768 9#define _LGPL_SOURCE
1501a7f3 10#include <assert.h>
1501a7f3 11#include <ctype.h>
1501a7f3
JG
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
36f2332b 15#include <inttypes.h>
dcf266c0
JG
16#include <dirent.h>
17#include <unistd.h>
18#include <sys/types.h>
19#include <sys/stat.h>
6c66fa0f 20#include <stdbool.h>
1501a7f3
JG
21
22#include <common/defaults.h>
23#include <common/error.h>
24#include <common/macros.h>
25#include <common/utils.h>
cce35f91 26#include <common/dynamic-buffer.h>
e8fa9fb0 27#include <common/compat/getenv.h>
fb198a11
JG
28#include <lttng/lttng-error.h>
29#include <libxml/parser.h>
30#include <libxml/valid.h>
31#include <libxml/xmlschemas.h>
dcf266c0
JG
32#include <libxml/tree.h>
33#include <lttng/lttng.h>
34#include <lttng/snapshot.h>
259c2674 35#include <lttng/rotation.h>
c1e83fb4 36#include <lttng/userspace-probe.h>
1501a7f3 37
f40ef1d5 38#include "session-config.h"
36f2332b 39#include "config-internal.h"
1501a7f3 40
c1e83fb4
FD
41#define CONFIG_USERSPACE_PROBE_LOOKUP_METHOD_NAME_MAX_LEN 7
42
1501a7f3
JG
43struct handler_filter_args {
44 const char* section;
45 config_entry_handler_cb handler;
46 void *user_data;
47};
48
dcf266c0
JG
49struct session_config_validation_ctx {
50 xmlSchemaParserCtxtPtr parser_ctx;
51 xmlSchemaPtr schema;
52 xmlSchemaValidCtxtPtr schema_validation_ctx;
53};
54
72c2e2e1 55LTTNG_HIDDEN const char * const config_element_all = "all";
1501a7f3
JG
56const char * const config_str_yes = "yes";
57const char * const config_str_true = "true";
58const char * const config_str_on = "on";
59const char * const config_str_no = "no";
60const char * const config_str_false = "false";
61const char * const config_str_off = "off";
36f2332b 62const char * const config_xml_encoding = "UTF-8";
fb198a11 63const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */
36f2332b
JG
64const char * const config_xml_indent_string = "\t";
65const char * const config_xml_true = "true";
66const char * const config_xml_false = "false";
1501a7f3 67
fb198a11
JG
68const char * const config_element_channel = "channel";
69const char * const config_element_channels = "channels";
70const char * const config_element_domain = "domain";
71const char * const config_element_domains = "domains";
72const char * const config_element_event = "event";
73const char * const config_element_events = "events";
74const char * const config_element_context = "context";
75const char * const config_element_contexts = "contexts";
76const char * const config_element_attributes = "attributes";
77const char * const config_element_exclusion = "exclusion";
78const char * const config_element_exclusions = "exclusions";
79const char * const config_element_function_attributes = "function_attributes";
80const char * const config_element_probe_attributes = "probe_attributes";
81const char * const config_element_symbol_name = "symbol_name";
82const char * const config_element_address = "address";
83const char * const config_element_offset = "offset";
c1e83fb4 84
d0687332
JG
85LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup = "lookup_method";
86LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_function_default = "DEFAULT";
87LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_function_elf = "ELF";
88LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_tracepoint_sdt = "SDT";
89LTTNG_HIDDEN const char * const config_element_userspace_probe_location_binary_path = "binary_path";
90LTTNG_HIDDEN const char * const config_element_userspace_probe_function_attributes = "userspace_probe_function_attributes";
91LTTNG_HIDDEN const char * const config_element_userspace_probe_function_location_function_name = "function_name";
92LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_attributes = "userspace_probe_tracepoint_attributes";
93LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_location_provider_name = "provider_name";
94LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_location_probe_name = "probe_name";
c1e83fb4 95
fb198a11
JG
96const char * const config_element_name = "name";
97const char * const config_element_enabled = "enabled";
98const char * const config_element_overwrite_mode = "overwrite_mode";
99const char * const config_element_subbuf_size = "subbuffer_size";
100const char * const config_element_num_subbuf = "subbuffer_count";
101const char * const config_element_switch_timer_interval = "switch_timer_interval";
102const char * const config_element_read_timer_interval = "read_timer_interval";
6ab3e98b
JG
103LTTNG_HIDDEN const char * const config_element_monitor_timer_interval = "monitor_timer_interval";
104LTTNG_HIDDEN const char * const config_element_blocking_timeout = "blocking_timeout";
fb198a11
JG
105const char * const config_element_output = "output";
106const char * const config_element_output_type = "output_type";
107const char * const config_element_tracefile_size = "tracefile_size";
108const char * const config_element_tracefile_count = "tracefile_count";
109const char * const config_element_live_timer_interval = "live_timer_interval";
ec2ed7da
JG
110LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events";
111LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets";
fb198a11
JG
112const char * const config_element_type = "type";
113const char * const config_element_buffer_type = "buffer_type";
114const char * const config_element_session = "session";
115const char * const config_element_sessions = "sessions";
ec2ed7da
JG
116LTTNG_HIDDEN const char * const config_element_context_perf = "perf";
117LTTNG_HIDDEN const char * const config_element_context_app = "app";
118LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name";
119LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name";
fb198a11
JG
120const char * const config_element_config = "config";
121const char * const config_element_started = "started";
122const char * const config_element_snapshot_mode = "snapshot_mode";
123const char * const config_element_loglevel = "loglevel";
124const char * const config_element_loglevel_type = "loglevel_type";
125const char * const config_element_filter = "filter";
ec2ed7da 126LTTNG_HIDDEN const char * const config_element_filter_expression = "filter_expression";
fb198a11
JG
127const char * const config_element_snapshot_outputs = "snapshot_outputs";
128const char * const config_element_consumer_output = "consumer_output";
129const char * const config_element_destination = "destination";
130const char * const config_element_path = "path";
131const char * const config_element_net_output = "net_output";
132const char * const config_element_control_uri = "control_uri";
133const char * const config_element_data_uri = "data_uri";
134const char * const config_element_max_size = "max_size";
ccf10263
MD
135const char * const config_element_pid = "pid";
136const char * const config_element_pids = "pids";
9e7c9f56 137const char * const config_element_shared_memory_path = "shared_memory_path";
159b042f
JG
138
139LTTNG_HIDDEN const char * const config_element_process_attr_id = "id";
140LTTNG_HIDDEN const char * const config_element_process_attr_tracker_pid = "pid_process_attr_tracker";
141LTTNG_HIDDEN const char * const config_element_process_attr_tracker_vpid = "vpid_process_attr_tracker";
142LTTNG_HIDDEN const char * const config_element_process_attr_tracker_uid = "uid_process_attr_tracker";
143LTTNG_HIDDEN const char * const config_element_process_attr_tracker_vuid = "vuid_process_attr_tracker";
144LTTNG_HIDDEN const char * const config_element_process_attr_tracker_gid = "gid_process_attr_tracker";
145LTTNG_HIDDEN const char * const config_element_process_attr_tracker_vgid = "vgid_process_attr_tracker";
146LTTNG_HIDDEN const char * const config_element_process_attr_trackers = "process_attr_trackers";
147LTTNG_HIDDEN const char * const config_element_process_attr_values = "process_attr_values";
148LTTNG_HIDDEN const char * const config_element_process_attr_value_type = "process_attr_value_type";
149LTTNG_HIDDEN const char * const config_element_process_attr_pid_value = "pid";
150LTTNG_HIDDEN const char * const config_element_process_attr_vpid_value = "vpid";
151LTTNG_HIDDEN const char * const config_element_process_attr_uid_value = "uid";
152LTTNG_HIDDEN const char * const config_element_process_attr_vuid_value = "vuid";
153LTTNG_HIDDEN const char * const config_element_process_attr_gid_value = "gid";
154LTTNG_HIDDEN const char * const config_element_process_attr_vgid_value = "vgid";
155LTTNG_HIDDEN const char * const config_element_process_attr_tracker_type = "process_attr_tracker_type";
fb198a11 156
f7af9a72
JG
157/* Used for support of legacy tracker serialization (< 2.12). */
158LTTNG_HIDDEN const char * const config_element_trackers_legacy = "trackers";
159LTTNG_HIDDEN const char * const config_element_pid_tracker_legacy = "pid_tracker";
160LTTNG_HIDDEN const char * const config_element_tracker_targets_legacy = "targets";
161LTTNG_HIDDEN const char * const config_element_tracker_pid_target_legacy = "pid_target";
162LTTNG_HIDDEN const char * const config_element_tracker_pid_legacy = "pid";
163
ce6176f2
JG
164LTTNG_HIDDEN const char * const config_element_rotation_schedules = "rotation_schedules";
165LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic = "periodic";
166LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic_time_us = "time_us";
167LTTNG_HIDDEN const char * const config_element_rotation_schedule_size_threshold = "size_threshold";
168LTTNG_HIDDEN const char * const config_element_rotation_schedule_size_threshold_bytes = "bytes";
259c2674 169
fb198a11
JG
170const char * const config_domain_type_kernel = "KERNEL";
171const char * const config_domain_type_ust = "UST";
172const char * const config_domain_type_jul = "JUL";
5cdb6027 173const char * const config_domain_type_log4j = "LOG4J";
0e115563 174const char * const config_domain_type_python = "PYTHON";
fb198a11
JG
175
176const char * const config_buffer_type_per_pid = "PER_PID";
177const char * const config_buffer_type_per_uid = "PER_UID";
178const char * const config_buffer_type_global = "GLOBAL";
179
180const char * const config_overwrite_mode_discard = "DISCARD";
181const char * const config_overwrite_mode_overwrite = "OVERWRITE";
182
183const char * const config_output_type_splice = "SPLICE";
184const char * const config_output_type_mmap = "MMAP";
185
186const char * const config_loglevel_type_all = "ALL";
187const char * const config_loglevel_type_range = "RANGE";
188const char * const config_loglevel_type_single = "SINGLE";
189
190const char * const config_event_type_all = "ALL";
191const char * const config_event_type_tracepoint = "TRACEPOINT";
192const char * const config_event_type_probe = "PROBE";
a416382b 193LTTNG_HIDDEN const char * const config_event_type_userspace_probe = "USERSPACE_PROBE";
fb198a11
JG
194const char * const config_event_type_function = "FUNCTION";
195const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
196const char * const config_event_type_noop = "NOOP";
197const char * const config_event_type_syscall = "SYSCALL";
198const char * const config_event_type_kprobe = "KPROBE";
199const char * const config_event_type_kretprobe = "KRETPROBE";
200
201const char * const config_event_context_pid = "PID";
202const char * const config_event_context_procname = "PROCNAME";
203const char * const config_event_context_prio = "PRIO";
204const char * const config_event_context_nice = "NICE";
205const char * const config_event_context_vpid = "VPID";
206const char * const config_event_context_tid = "TID";
207const char * const config_event_context_vtid = "VTID";
208const char * const config_event_context_ppid = "PPID";
209const char * const config_event_context_vppid = "VPPID";
210const char * const config_event_context_pthread_id = "PTHREAD_ID";
211const char * const config_event_context_hostname = "HOSTNAME";
212const char * const config_event_context_ip = "IP";
e885a367 213const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER";
ec2ed7da
JG
214LTTNG_HIDDEN const char * const config_event_context_app = "APP";
215LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE";
216LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
217LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
218LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
16c4c991
FD
219LTTNG_HIDDEN const char * const config_event_context_callstack_user= "CALLSTACK_USER";
220LTTNG_HIDDEN const char * const config_event_context_callstack_kernel = "CALLSTACK_KERNEL";
40e14884
MJ
221LTTNG_HIDDEN const char * const config_event_context_cgroup_ns = "CGROUP_NS";
222LTTNG_HIDDEN const char * const config_event_context_ipc_ns = "IPC_NS";
223LTTNG_HIDDEN const char * const config_event_context_mnt_ns = "MNT_NS";
224LTTNG_HIDDEN const char * const config_event_context_net_ns = "NET_NS";
225LTTNG_HIDDEN const char * const config_event_context_pid_ns = "PID_NS";
d37ac3cd 226LTTNG_HIDDEN const char * const config_event_context_time_ns = "TIME_NS";
40e14884
MJ
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;
d37ac3cd
MJ
1098 } else if (!strcmp((char *) context_type,
1099 config_event_context_time_ns)) {
1100 ret = LTTNG_EVENT_CONTEXT_TIME_NS;
40e14884
MJ
1101 } else if (!strcmp((char *) context_type,
1102 config_event_context_user_ns)) {
1103 ret = LTTNG_EVENT_CONTEXT_USER_NS;
1104 } else if (!strcmp((char *) context_type,
1105 config_event_context_uts_ns)) {
1106 ret = LTTNG_EVENT_CONTEXT_UTS_NS;
499cbfa1
MJ
1107 } else if (!strcmp((char *) context_type,
1108 config_event_context_uid)) {
1109 ret = LTTNG_EVENT_CONTEXT_UID;
1110 } else if (!strcmp((char *) context_type,
1111 config_event_context_euid)) {
1112 ret = LTTNG_EVENT_CONTEXT_EUID;
1113 } else if (!strcmp((char *) context_type,
1114 config_event_context_suid)) {
1115 ret = LTTNG_EVENT_CONTEXT_SUID;
1116 } else if (!strcmp((char *) context_type,
1117 config_event_context_gid)) {
1118 ret = LTTNG_EVENT_CONTEXT_GID;
1119 } else if (!strcmp((char *) context_type,
1120 config_event_context_egid)) {
1121 ret = LTTNG_EVENT_CONTEXT_EGID;
1122 } else if (!strcmp((char *) context_type,
1123 config_event_context_sgid)) {
1124 ret = LTTNG_EVENT_CONTEXT_SGID;
1125 } else if (!strcmp((char *) context_type,
1126 config_event_context_vuid)) {
1127 ret = LTTNG_EVENT_CONTEXT_VUID;
1128 } else if (!strcmp((char *) context_type,
1129 config_event_context_veuid)) {
1130 ret = LTTNG_EVENT_CONTEXT_VEUID;
1131 } else if (!strcmp((char *) context_type,
1132 config_event_context_vsuid)) {
1133 ret = LTTNG_EVENT_CONTEXT_VSUID;
1134 } else if (!strcmp((char *) context_type,
1135 config_event_context_vgid)) {
1136 ret = LTTNG_EVENT_CONTEXT_VGID;
1137 } else if (!strcmp((char *) context_type,
1138 config_event_context_vegid)) {
1139 ret = LTTNG_EVENT_CONTEXT_VEGID;
1140 } else if (!strcmp((char *) context_type,
1141 config_event_context_vsgid)) {
1142 ret = LTTNG_EVENT_CONTEXT_VSGID;
dcf266c0
JG
1143 } else {
1144 goto error;
1145 }
1146
1147 return ret;
1148error:
1149 return -1;
1150}
1151
1152static
1153int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
1154{
1155 int ret;
1156 xmlNodePtr node;
1157
1158 for (node = xmlFirstElementChild(domain_node); node;
1159 node = xmlNextElementSibling(node)) {
1160 if (!strcmp((const char *) node->name, config_element_type)) {
1161 /* domain type */
1162 xmlChar *node_content = xmlNodeGetContent(node);
1163 if (!node_content) {
1164 ret = -LTTNG_ERR_NOMEM;
1165 goto end;
1166 }
1167
1168 ret = get_domain_type(node_content);
1169 free(node_content);
1170 if (ret < 0) {
1171 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1172 goto end;
1173 }
1174
1175 domain->type = ret;
1176 } else if (!strcmp((const char *) node->name,
1177 config_element_buffer_type)) {
1178 /* buffer type */
1179 xmlChar *node_content = xmlNodeGetContent(node);
1180 if (!node_content) {
1181 ret = -LTTNG_ERR_NOMEM;
1182 goto end;
1183 }
1184
1185 ret = get_buffer_type(node_content);
1186 free(node_content);
1187 if (ret < 0) {
1188 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1189 goto end;
1190 }
1191
1192 domain->buf_type = ret;
1193 }
1194 }
1195 ret = 0;
1196end:
1197 return ret;
1198}
1199
1200static
1201int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
1202 char **data_uri)
1203{
1204 xmlNodePtr node;
1205
1206 for (node = xmlFirstElementChild(net_output_node); node;
1207 node = xmlNextElementSibling(node)) {
1208 if (!strcmp((const char *) node->name, config_element_control_uri)) {
1209 /* control_uri */
1210 *control_uri = (char *) xmlNodeGetContent(node);
1211 if (!*control_uri) {
1212 break;
1213 }
1214 } else {
1215 /* data_uri */
1216 *data_uri = (char *) xmlNodeGetContent(node);
1217 if (!*data_uri) {
1218 break;
1219 }
1220 }
1221 }
1222
1223 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
1224}
1225
1226static
1227int process_consumer_output(xmlNodePtr consumer_output_node,
1228 struct consumer_output *output)
1229{
1230 int ret;
1231 xmlNodePtr node;
1232
1233 assert(output);
1234
1235 for (node = xmlFirstElementChild(consumer_output_node); node;
1236 node = xmlNextElementSibling(node)) {
1237 if (!strcmp((const char *) node->name, config_element_enabled)) {
1238 xmlChar *enabled_str = xmlNodeGetContent(node);
1239
1240 /* enabled */
1241 if (!enabled_str) {
1242 ret = -LTTNG_ERR_NOMEM;
1243 goto end;
1244 }
1245
1246 ret = parse_bool(enabled_str, &output->enabled);
1247 free(enabled_str);
1248 if (ret) {
1249 goto end;
1250 }
1251 } else {
1252 xmlNodePtr output_type_node;
1253
1254 /* destination */
1255 output_type_node = xmlFirstElementChild(node);
1256 if (!output_type_node) {
1257 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1258 goto end;
1259 }
1260
1261 if (!strcmp((const char *) output_type_node->name,
1262 config_element_path)) {
1263 /* path */
1264 output->path = (char *) xmlNodeGetContent(output_type_node);
1265 if (!output->path) {
1266 ret = -LTTNG_ERR_NOMEM;
1267 goto end;
1268 }
1269 } else {
1270 /* net_output */
1271 ret = get_net_output_uris(output_type_node,
1272 &output->control_uri, &output->data_uri);
1273 if (ret) {
1274 goto end;
1275 }
1276 }
1277 }
1278 }
1279 ret = 0;
1280
1281end:
1282 if (ret) {
1283 free(output->path);
1284 free(output->control_uri);
1285 free(output->data_uri);
1286 memset(output, 0, sizeof(struct consumer_output));
1287 }
1288 return ret;
1289}
1290
1291static
95681498
JG
1292int create_session_net_output(const char *name, const char *control_uri,
1293 const char *data_uri)
dcf266c0
JG
1294{
1295 int ret;
1296 struct lttng_handle *handle;
1297 const char *uri = NULL;
1298
1299 assert(name);
dcf266c0 1300
95681498 1301 handle = lttng_create_handle(name, NULL);
dcf266c0
JG
1302 if (!handle) {
1303 ret = -LTTNG_ERR_NOMEM;
1304 goto end;
1305 }
1306
1307 if (!control_uri || !data_uri) {
1308 uri = control_uri ? control_uri : data_uri;
1309 control_uri = uri;
1310 data_uri = uri;
1311 }
1312
1313 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1314 lttng_destroy_handle(handle);
1315end:
1316 return ret;
1317}
1318
1319static
1b08cbce
JR
1320int create_snapshot_session(const char *session_name, xmlNodePtr output_node,
1321 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1322{
1323 int ret;
1324 xmlNodePtr node = NULL;
1325 xmlNodePtr snapshot_output_list_node;
1326 xmlNodePtr snapshot_output_node;
1327
1328 assert(session_name);
1329
1330 ret = lttng_create_session_snapshot(session_name, NULL);
1331 if (ret) {
1332 goto end;
1333 }
1334
1335 if (!output_node) {
1336 goto end;
1337 }
1338
1339 snapshot_output_list_node = xmlFirstElementChild(output_node);
1340
1341 /* Parse and create snapshot outputs */
1342
1343 for (snapshot_output_node =
1344 xmlFirstElementChild(snapshot_output_list_node);
1345 snapshot_output_node; snapshot_output_node =
1346 xmlNextElementSibling(snapshot_output_node)) {
1347 char *name = NULL;
1348 uint64_t max_size = UINT64_MAX;
1349 struct consumer_output output = { 0 };
1350 struct lttng_snapshot_output *snapshot_output = NULL;
1b08cbce
JR
1351 const char *control_uri = NULL;
1352 const char *data_uri = NULL;
1353 const char *path = NULL;
dcf266c0
JG
1354
1355 for (node = xmlFirstElementChild(snapshot_output_node); node;
1356 node = xmlNextElementSibling(node)) {
1357 if (!strcmp((const char *) node->name,
1358 config_element_name)) {
1359 /* name */
1360 name = (char *) xmlNodeGetContent(node);
1361 if (!name) {
1362 ret = -LTTNG_ERR_NOMEM;
1363 goto error_snapshot_output;
1364 }
1365 } else if (!strcmp((const char *) node->name,
1366 config_element_max_size)) {
1367 xmlChar *content = xmlNodeGetContent(node);
1368
1369 /* max_size */
1370 if (!content) {
1371 ret = -LTTNG_ERR_NOMEM;
1372 goto error_snapshot_output;
1373 }
1374 ret = parse_uint(content, &max_size);
1375 free(content);
1376 if (ret) {
1377 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1378 goto error_snapshot_output;
1379 }
1380 } else {
1381 /* consumer_output */
1382 ret = process_consumer_output(node, &output);
1383 if (ret) {
1384 goto error_snapshot_output;
1385 }
1386 }
1387 }
1388
21a3f144
JR
1389 control_uri = output.control_uri;
1390 data_uri = output.data_uri;
1391 path = output.path;
1392
1b08cbce
JR
1393 if (overrides) {
1394 if (overrides->path_url) {
1b08cbce 1395 path = overrides->path_url;
21a3f144
JR
1396 /* Control/data_uri are null */
1397 control_uri = NULL;
1398 data_uri = NULL;
1b08cbce
JR
1399 } else {
1400 if (overrides->ctrl_url) {
1b08cbce 1401 control_uri = overrides->ctrl_url;
21a3f144
JR
1402 /* path is null */
1403 path = NULL;
1b08cbce
JR
1404 }
1405 if (overrides->data_url) {
1b08cbce 1406 data_uri = overrides->data_url;
21a3f144
JR
1407 /* path is null */
1408 path = NULL;
1b08cbce
JR
1409 }
1410 }
1b08cbce
JR
1411 }
1412
dcf266c0
JG
1413 snapshot_output = lttng_snapshot_output_create();
1414 if (!snapshot_output) {
1415 ret = -LTTNG_ERR_NOMEM;
1416 goto error_snapshot_output;
1417 }
1418
1419 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1420 if (ret) {
1421 goto error_snapshot_output;
1422 }
1423
1424 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1425 if (ret) {
1426 goto error_snapshot_output;
1427 }
1428
1b08cbce
JR
1429 if (path) {
1430 ret = lttng_snapshot_output_set_ctrl_url(path,
dcf266c0
JG
1431 snapshot_output);
1432 if (ret) {
1433 goto error_snapshot_output;
1434 }
1435 } else {
1b08cbce
JR
1436 if (control_uri) {
1437 ret = lttng_snapshot_output_set_ctrl_url(control_uri,
dcf266c0
JG
1438 snapshot_output);
1439 if (ret) {
1440 goto error_snapshot_output;
1441 }
1442 }
1443
1b08cbce
JR
1444 if (data_uri) {
1445 ret = lttng_snapshot_output_set_data_url(data_uri,
dcf266c0
JG
1446 snapshot_output);
1447 if (ret) {
1448 goto error_snapshot_output;
1449 }
1450 }
1451 }
1452
1453 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1454error_snapshot_output:
1455 free(name);
1456 free(output.path);
1457 free(output.control_uri);
1458 free(output.data_uri);
1459 lttng_snapshot_output_destroy(snapshot_output);
1460 if (ret) {
1461 goto end;
1462 }
1463 }
1464end:
1465 return ret;
1466}
1467
1468static
1469int create_session(const char *name,
dcf266c0 1470 xmlNodePtr output_node,
1b08cbce
JR
1471 uint64_t live_timer_interval,
1472 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
1473{
1474 int ret;
1475 struct consumer_output output = { 0 };
1476 xmlNodePtr consumer_output_node;
1b08cbce
JR
1477 const char *control_uri = NULL;
1478 const char *data_uri = NULL;
1479 const char *path = NULL;
dcf266c0
JG
1480
1481 assert(name);
dcf266c0
JG
1482
1483 if (output_node) {
1484 consumer_output_node = xmlFirstElementChild(output_node);
1485 if (!consumer_output_node) {
1486 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1487 goto end;
1488 }
1489
1490 if (strcmp((const char *) consumer_output_node->name,
1491 config_element_consumer_output)) {
1492 WARN("Invalid output type, expected %s node",
1493 config_element_consumer_output);
1494 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1495 goto end;
1496 }
1497
1498 ret = process_consumer_output(consumer_output_node, &output);
1499 if (ret) {
1500 goto end;
1501 }
1502 }
1503
21a3f144
JR
1504 control_uri = output.control_uri;
1505 data_uri = output.data_uri;
1506 path = output.path;
1507
1b08cbce
JR
1508 /* Check for override and apply them */
1509 if (overrides) {
1510 if (overrides->path_url) {
1b08cbce 1511 path = overrides->path_url;
21a3f144
JR
1512 /* control/data_uri are null */;
1513 control_uri = NULL;
1514 data_uri = NULL;
1b08cbce
JR
1515 } else {
1516 if (overrides->ctrl_url) {
1b08cbce 1517 control_uri = overrides->ctrl_url;
21a3f144
JR
1518 /* path is null */
1519 path = NULL;
1b08cbce
JR
1520 }
1521 if (overrides->data_url) {
1b08cbce 1522 data_uri = overrides->data_url;
21a3f144
JR
1523 /* path is null */
1524 path = NULL;
1b08cbce
JR
1525 }
1526 }
1b08cbce
JR
1527 }
1528
21a3f144 1529
1b08cbce 1530 if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) {
dcf266c0
JG
1531 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1532 goto end;
1533 }
1534
1b08cbce 1535 if (control_uri || data_uri) {
dcf266c0
JG
1536 /* network destination */
1537 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
b664f89a
DG
1538 /*
1539 * URLs are provided for sure since the test above make sure that
1540 * with a live timer the data and control URIs are provided. So,
1541 * NULL is passed here and will be set right after.
1542 */
1543 ret = lttng_create_session_live(name, NULL, live_timer_interval);
dcf266c0
JG
1544 } else {
1545 ret = lttng_create_session(name, NULL);
1546 }
1547 if (ret) {
1548 goto end;
1549 }
1550
1b08cbce 1551 ret = create_session_net_output(name, control_uri, data_uri);
95681498
JG
1552 if (ret) {
1553 goto end;
dcf266c0 1554 }
95681498 1555
dcf266c0
JG
1556 } else {
1557 /* either local output or no output */
1b08cbce 1558 ret = lttng_create_session(name, path);
dcf266c0
JG
1559 if (ret) {
1560 goto end;
1561 }
1562 }
1563end:
1564 free(output.path);
1565 free(output.control_uri);
1566 free(output.data_uri);
1567 return ret;
1568}
c1e83fb4
FD
1569
1570static
1571struct lttng_userspace_probe_location *
1572process_userspace_probe_function_attribute_node(
1573 xmlNodePtr attribute_node)
1574{
c1e83fb4 1575 xmlNodePtr function_attribute_node;
717d2dba 1576 char *function_name = NULL, *binary_path = NULL;
c1e83fb4
FD
1577 struct lttng_userspace_probe_location *location = NULL;
1578 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
1579
1580 /*
1581 * Process userspace probe location function attributes. The order of
1582 * the fields are not guaranteed so we need to iterate over all fields
1583 * and check at the end if everything we need for this location type is
1584 * there.
1585 */
1586 for (function_attribute_node =
1587 xmlFirstElementChild(attribute_node);
1588 function_attribute_node;
1589 function_attribute_node = xmlNextElementSibling(
1590 function_attribute_node)) {
1591 /* Handle function name, binary path and lookup method. */
1592 if (!strcmp((const char *) function_attribute_node->name,
1593 config_element_userspace_probe_function_location_function_name)) {
717d2dba 1594 function_name = (char *) xmlNodeGetContent(function_attribute_node);
c1e83fb4 1595 if (!function_name) {
c1e83fb4
FD
1596 goto error;
1597 }
1598 } else if (!strcmp((const char *) function_attribute_node->name,
1599 config_element_userspace_probe_location_binary_path)) {
717d2dba 1600 binary_path = (char *) xmlNodeGetContent(function_attribute_node);
c1e83fb4 1601 if (!binary_path) {
c1e83fb4
FD
1602 goto error;
1603 }
1604 } else if (!strcmp((const char *) function_attribute_node->name,
1605 config_element_userspace_probe_lookup)) {
717d2dba 1606 char *lookup_method_name;
c1e83fb4 1607
717d2dba
JG
1608 lookup_method_name = (char *) xmlNodeGetContent(
1609 function_attribute_node);
c1e83fb4 1610 if (!lookup_method_name) {
c1e83fb4
FD
1611 goto error;
1612 }
1613
1614 /*
1615 * function_default lookup method defaults to
1616 * function_elf lookup method at the moment.
1617 */
1618 if (!strcmp(lookup_method_name, config_element_userspace_probe_lookup_function_elf)
1619 || !strcmp(lookup_method_name, config_element_userspace_probe_lookup_function_default)) {
1620 lookup_method = lttng_userspace_probe_location_lookup_method_function_elf_create();
1621 if (!lookup_method) {
1622 PERROR("Error creating function default/ELF lookup method");
c1e83fb4
FD
1623 }
1624 } else {
717d2dba
JG
1625 WARN("Unknown function lookup method");
1626 }
1627
1628 free(lookup_method_name);
1629 if (!lookup_method) {
c1e83fb4
FD
1630 goto error;
1631 }
1632 } else {
1633 goto error;
1634 }
1635
1636 /* Check if all the necessary fields were found. */
1637 if (binary_path && function_name && lookup_method) {
717d2dba 1638 /* Ownership of lookup_method is transferred. */
c1e83fb4
FD
1639 location =
1640 lttng_userspace_probe_location_function_create(
1641 binary_path, function_name,
1642 lookup_method);
717d2dba
JG
1643 lookup_method = NULL;
1644 goto error;
c1e83fb4
FD
1645 }
1646 }
1647error:
717d2dba 1648 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
c1e83fb4
FD
1649 free(binary_path);
1650 free(function_name);
c1e83fb4
FD
1651 return location;
1652}
1653
1654static
1655struct lttng_userspace_probe_location *
1656process_userspace_probe_tracepoint_attribute_node(
1657 xmlNodePtr attribute_node)
1658{
c1e83fb4 1659 xmlNodePtr tracepoint_attribute_node;
c1e83fb4
FD
1660 char *probe_name = NULL, *provider_name = NULL, *binary_path = NULL;
1661 struct lttng_userspace_probe_location *location = NULL;
1662 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
1663
1664 /*
1665 * Process userspace probe location tracepoint attributes. The order of
1666 * the fields are not guaranteed so we need to iterate over all fields
1667 * and check at the end if everything we need for this location type is
1668 * there.
1669 */
1670 for (tracepoint_attribute_node =
1671 xmlFirstElementChild(attribute_node); tracepoint_attribute_node;
1672 tracepoint_attribute_node = xmlNextElementSibling(
1673 tracepoint_attribute_node)) {
1674 if (!strcmp((const char *) tracepoint_attribute_node->name,
1675 config_element_userspace_probe_tracepoint_location_probe_name)) {
717d2dba 1676 probe_name = (char *) xmlNodeGetContent(tracepoint_attribute_node);
c1e83fb4 1677 if (!probe_name) {
c1e83fb4
FD
1678 goto error;
1679 }
1680 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1681 config_element_userspace_probe_tracepoint_location_provider_name)) {
717d2dba 1682 provider_name = (char *) xmlNodeGetContent(tracepoint_attribute_node);
c1e83fb4 1683 if (!provider_name) {
c1e83fb4
FD
1684 goto error;
1685 }
1686 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1687 config_element_userspace_probe_location_binary_path)) {
717d2dba 1688 binary_path = (char *) xmlNodeGetContent(tracepoint_attribute_node);
c1e83fb4 1689 if (!binary_path) {
c1e83fb4
FD
1690 goto error;
1691 }
1692 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1693 config_element_userspace_probe_lookup)) {
717d2dba 1694 char *lookup_method_name;
c1e83fb4 1695
717d2dba
JG
1696 lookup_method_name = (char *) xmlNodeGetContent(
1697 tracepoint_attribute_node);
c1e83fb4 1698 if (!lookup_method_name) {
c1e83fb4
FD
1699 goto error;
1700 }
1701
1702 if (!strcmp(lookup_method_name,
1703 config_element_userspace_probe_lookup_tracepoint_sdt)) {
1704 lookup_method =
1705 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1706 if (!lookup_method) {
1707 PERROR("Error creating tracepoint SDT lookup method");
c1e83fb4
FD
1708 }
1709 } else {
717d2dba
JG
1710 WARN("Unknown tracepoint lookup method");
1711 }
1712
1713 free(lookup_method_name);
1714 if (!lookup_method) {
c1e83fb4
FD
1715 goto error;
1716 }
1717 } else {
717d2dba 1718 WARN("Unknown tracepoint attribute");
c1e83fb4
FD
1719 goto error;
1720 }
1721
1722 /* Check if all the necessary fields were found. */
1723 if (binary_path && provider_name && probe_name && lookup_method) {
717d2dba 1724 /* Ownership of lookup_method is transferred. */
c1e83fb4
FD
1725 location =
1726 lttng_userspace_probe_location_tracepoint_create(
1727 binary_path, provider_name,
1728 probe_name, lookup_method);
6bd61b96 1729 lookup_method = NULL;
717d2dba 1730 goto error;
c1e83fb4
FD
1731 }
1732 }
1733error:
717d2dba 1734 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
c1e83fb4 1735 free(binary_path);
c1e83fb4 1736 free(provider_name);
717d2dba 1737 free(probe_name);
c1e83fb4
FD
1738 return location;
1739}
1740
dcf266c0
JG
1741static
1742int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1743 struct lttng_event_probe_attr *attr)
1744{
1745 int ret;
1746
1747 assert(probe_attribute_node);
1748 assert(attr);
1749
1750 if (!strcmp((const char *) probe_attribute_node->name,
1751 config_element_address)) {
1752 xmlChar *content;
1753 uint64_t addr = 0;
1754
1755 /* addr */
1756 content = xmlNodeGetContent(probe_attribute_node);
1757 if (!content) {
1758 ret = -LTTNG_ERR_NOMEM;
1759 goto end;
1760 }
1761
1762 ret = parse_uint(content, &addr);
1763 free(content);
1764 if (ret) {
1765 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1766 goto end;
1767 }
1768
1769 attr->addr = addr;
1770 } else if (!strcmp((const char *) probe_attribute_node->name,
1771 config_element_offset)) {
1772 xmlChar *content;
1773 uint64_t offset = 0;
1774
1775 /* offset */
1776 content = xmlNodeGetContent(probe_attribute_node);
1777 if (!content) {
1778 ret = -LTTNG_ERR_NOMEM;
1779 goto end;
1780 }
1781
1782 ret = parse_uint(content, &offset);
1783 free(content);
1784 if (ret) {
1785 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1786 goto end;
1787 }
1788
1789 attr->offset = offset;
1790 } else if (!strcmp((const char *) probe_attribute_node->name,
1791 config_element_symbol_name)) {
1792 xmlChar *content;
dcf266c0
JG
1793
1794 /* symbol_name */
1795 content = xmlNodeGetContent(probe_attribute_node);
1796 if (!content) {
1797 ret = -LTTNG_ERR_NOMEM;
1798 goto end;
1799 }
1800
d2e67842
JG
1801 ret = lttng_strncpy(attr->symbol_name,
1802 (const char *) content,
1803 LTTNG_SYMBOL_NAME_LEN);
1804 if (ret == -1) {
1805 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1806 (const char *) content,
1807 strlen((const char *) content),
1808 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
1809 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1810 free(content);
1811 goto end;
1812 }
dcf266c0
JG
1813 free(content);
1814 }
1815 ret = 0;
1816end:
1817 return ret;
1818}
1819
1820static
1821int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
d7b645e2 1822 const char *channel_name, const enum process_event_node_phase phase)
dcf266c0 1823{
d7b645e2 1824 int ret = 0, i;
dcf266c0 1825 xmlNodePtr node;
91744e14 1826 struct lttng_event *event;
dcf266c0
JG
1827 char **exclusions = NULL;
1828 unsigned long exclusion_count = 0;
1829 char *filter_expression = NULL;
1830
1831 assert(event_node);
1832 assert(handle);
1833 assert(channel_name);
1834
91744e14
FD
1835 event = lttng_event_create();
1836 if (!event) {
1837 ret = -LTTNG_ERR_NOMEM;
1838 goto end;
1839 }
dcf266c0 1840
f40eba3d
JG
1841 /* Initialize default log level which varies by domain */
1842 switch (handle->domain.type)
1843 {
1844 case LTTNG_DOMAIN_JUL:
91744e14 1845 event->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
f40eba3d
JG
1846 break;
1847 case LTTNG_DOMAIN_LOG4J:
91744e14 1848 event->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
f40eba3d
JG
1849 break;
1850 case LTTNG_DOMAIN_PYTHON:
91744e14 1851 event->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
f40eba3d
JG
1852 break;
1853 case LTTNG_DOMAIN_UST:
1854 case LTTNG_DOMAIN_KERNEL:
91744e14 1855 event->loglevel = LTTNG_LOGLEVEL_DEBUG;
f40eba3d
JG
1856 break;
1857 default:
1858 assert(0);
1859 }
1860
dcf266c0
JG
1861 for (node = xmlFirstElementChild(event_node); node;
1862 node = xmlNextElementSibling(node)) {
1863 if (!strcmp((const char *) node->name, config_element_name)) {
1864 xmlChar *content;
dcf266c0
JG
1865
1866 /* name */
1867 content = xmlNodeGetContent(node);
1868 if (!content) {
1869 ret = -LTTNG_ERR_NOMEM;
1870 goto end;
1871 }
1872
91744e14 1873 ret = lttng_strncpy(event->name,
d2e67842
JG
1874 (const char *) content,
1875 LTTNG_SYMBOL_NAME_LEN);
1876 if (ret == -1) {
1877 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1878 (const char *) content,
1879 strlen((const char *) content),
1880 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
1881 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1882 free(content);
1883 goto end;
1884 }
dcf266c0
JG
1885 free(content);
1886 } else if (!strcmp((const char *) node->name,
1887 config_element_enabled)) {
1888 xmlChar *content = xmlNodeGetContent(node);
1889
1890 /* enabled */
1891 if (!content) {
1892 ret = -LTTNG_ERR_NOMEM;
1893 goto end;
1894 }
1895
91744e14 1896 ret = parse_bool(content, &event->enabled);
dcf266c0
JG
1897 free(content);
1898 if (ret) {
1899 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1900 goto end;
1901 }
1902 } else if (!strcmp((const char *) node->name,
1903 config_element_type)) {
1904 xmlChar *content = xmlNodeGetContent(node);
1905
1906 /* type */
1907 if (!content) {
1908 ret = -LTTNG_ERR_NOMEM;
1909 goto end;
1910 }
1911
1912 ret = get_event_type(content);
1913 free(content);
1914 if (ret < 0) {
1915 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1916 goto end;
1917 }
1918
91744e14 1919 event->type = ret;
dcf266c0
JG
1920 } else if (!strcmp((const char *) node->name,
1921 config_element_loglevel_type)) {
1922 xmlChar *content = xmlNodeGetContent(node);
1923
1924 /* loglevel_type */
1925 if (!content) {
1926 ret = -LTTNG_ERR_NOMEM;
1927 goto end;
1928 }
1929
1930 ret = get_loglevel_type(content);
1931 free(content);
1932 if (ret < 0) {
1933 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1934 goto end;
1935 }
1936
91744e14 1937 event->loglevel_type = ret;
dcf266c0
JG
1938 } else if (!strcmp((const char *) node->name,
1939 config_element_loglevel)) {
1940 xmlChar *content;
1941 int64_t loglevel = 0;
1942
1943 /* loglevel */
1944 content = xmlNodeGetContent(node);
1945 if (!content) {
1946 ret = -LTTNG_ERR_NOMEM;
1947 goto end;
1948 }
1949
1950 ret = parse_int(content, &loglevel);
1951 free(content);
1952 if (ret) {
1953 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1954 goto end;
1955 }
1956
1957 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1958 WARN("loglevel out of range.");
1959 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1960 goto end;
1961 }
1962
91744e14 1963 event->loglevel = loglevel;
dcf266c0
JG
1964 } else if (!strcmp((const char *) node->name,
1965 config_element_filter)) {
1966 xmlChar *content =
1967 xmlNodeGetContent(node);
1968
1969 /* filter */
1970 if (!content) {
1971 ret = -LTTNG_ERR_NOMEM;
1972 goto end;
1973 }
1974
02d8ac3d 1975 free(filter_expression);
dcf266c0
JG
1976 filter_expression = strdup((char *) content);
1977 free(content);
1978 if (!filter_expression) {
1979 ret = -LTTNG_ERR_NOMEM;
1980 goto end;
1981 }
1982 } else if (!strcmp((const char *) node->name,
1983 config_element_exclusions)) {
1984 xmlNodePtr exclusion_node;
1985 int exclusion_index = 0;
1986
1987 /* exclusions */
1988 if (exclusions) {
1989 /*
1990 * Exclusions has already been initialized,
1991 * invalid file.
1992 */
1993 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1994 goto end;
1995 }
1996
1997 exclusion_count = xmlChildElementCount(node);
1998 if (!exclusion_count) {
1999 continue;
2000 }
2001
2002 exclusions = zmalloc(exclusion_count * sizeof(char *));
2003 if (!exclusions) {
2004 exclusion_count = 0;
2005 ret = -LTTNG_ERR_NOMEM;
2006 goto end;
2007 }
2008
2009 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
2010 exclusion_node = xmlNextElementSibling(exclusion_node)) {
2011 xmlChar *content =
2012 xmlNodeGetContent(exclusion_node);
2013
2014 if (!content) {
2015 ret = -LTTNG_ERR_NOMEM;
2016 goto end;
2017 }
2018
2019 exclusions[exclusion_index] = strdup((const char *) content);
2020 free(content);
2021 if (!exclusions[exclusion_index]) {
2022 ret = -LTTNG_ERR_NOMEM;
2023 goto end;
2024 }
2025 exclusion_index++;
2026 }
2027
91744e14 2028 event->exclusion = 1;
dcf266c0
JG
2029 } else if (!strcmp((const char *) node->name,
2030 config_element_attributes)) {
2031 xmlNodePtr attribute_node = xmlFirstElementChild(node);
2032
2033 /* attributes */
2034 if (!attribute_node) {
2035 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2036 goto end;
2037 }
2038
c1e83fb4 2039 if (!strcmp((const char *) attribute_node->name,
dcf266c0
JG
2040 config_element_probe_attributes)) {
2041 xmlNodePtr probe_attribute_node;
2042
2043 /* probe_attributes */
2044 for (probe_attribute_node =
2045 xmlFirstElementChild(attribute_node); probe_attribute_node;
2046 probe_attribute_node = xmlNextElementSibling(
2047 probe_attribute_node)) {
2048
2049 ret = process_probe_attribute_node(probe_attribute_node,
91744e14 2050 &event->attr.probe);
dcf266c0
JG
2051 if (ret) {
2052 goto end;
2053 }
2054 }
c1e83fb4
FD
2055 } else if (!strcmp((const char *) attribute_node->name,
2056 config_element_function_attributes)) {
dcf266c0
JG
2057 size_t sym_len;
2058 xmlChar *content;
2059 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
2060
2061 /* function_attributes */
2062 content = xmlNodeGetContent(symbol_node);
2063 if (!content) {
2064 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2065 goto end;
2066 }
2067
2068 sym_len = strlen((char *) content);
2069 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
2070 WARN("Function name too long.");
2071 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2072 free(content);
2073 goto end;
2074 }
2075
d2e67842 2076 ret = lttng_strncpy(
91744e14 2077 event->attr.ftrace.symbol_name,
d2e67842
JG
2078 (char *) content, sym_len);
2079 if (ret == -1) {
2080 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2081 free(content);
2082 goto end;
2083 }
dcf266c0 2084 free(content);
c1e83fb4
FD
2085 } else if (!strcmp((const char *) attribute_node->name,
2086 config_element_userspace_probe_tracepoint_attributes)) {
2087 struct lttng_userspace_probe_location *location;
2088
2089 location = process_userspace_probe_tracepoint_attribute_node(attribute_node);
2090 if (!location) {
2091 WARN("Error processing userspace probe tracepoint attribute");
2092 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2093 goto end;
2094 }
2095 ret = lttng_event_set_userspace_probe_location(
2096 event, location);
2097 if (ret) {
2098 WARN("Error setting userspace probe location field");
2099 lttng_userspace_probe_location_destroy(
2100 location);
2101 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2102 goto end;
2103 }
2104 } else if (!strcmp((const char *) attribute_node->name,
2105 config_element_userspace_probe_function_attributes)) {
2106 struct lttng_userspace_probe_location *location;
2107
2108 location =
2109 process_userspace_probe_function_attribute_node(
2110 attribute_node);
2111 if (!location) {
2112 WARN("Error processing userspace probe function attribute");
2113 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2114 goto end;
2115 }
2116
2117 ret = lttng_event_set_userspace_probe_location(
2118 event, location);
2119 if (ret) {
2120 WARN("Error setting userspace probe location field");
2121 lttng_userspace_probe_location_destroy(
2122 location);
2123 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2124 goto end;
2125 }
2126 } else {
2127 /* Unknown event attribute. */
2128 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2129 goto end;
dcf266c0
JG
2130 }
2131 }
2132 }
2133
91744e14
FD
2134 if ((event->enabled && phase == ENABLE) || phase == CREATION) {
2135 ret = lttng_enable_event_with_exclusions(handle, event, channel_name,
d7b645e2
JR
2136 filter_expression, exclusion_count, exclusions);
2137 if (ret < 0) {
91744e14 2138 WARN("Enabling event (name:%s) on load failed.", event->name);
d7b645e2
JR
2139 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2140 goto end;
2141 }
065321e9 2142 }
d2e67842 2143 ret = 0;
dcf266c0
JG
2144end:
2145 for (i = 0; i < exclusion_count; i++) {
2146 free(exclusions[i]);
2147 }
2148
b33f872b 2149 lttng_event_destroy(event);
dcf266c0
JG
2150 free(exclusions);
2151 free(filter_expression);
2152 return ret;
2153}
2154
2155static
2156int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
2157 const char *channel_name)
2158{
2159 int ret = 0;
d7b645e2 2160 struct lttng_event event;
dcf266c0
JG
2161 xmlNodePtr node;
2162
2163 assert(events_node);
2164 assert(handle);
2165 assert(channel_name);
2166
2167 for (node = xmlFirstElementChild(events_node); node;
2168 node = xmlNextElementSibling(node)) {
d7b645e2 2169 ret = process_event_node(node, handle, channel_name, CREATION);
dcf266c0
JG
2170 if (ret) {
2171 goto end;
2172 }
2173 }
d7b645e2
JR
2174
2175 /*
2176 * Disable all events to enable only the necessary events.
2177 * Limitations regarding lttng_disable_events and tuple descriptor
2178 * force this approach.
2179 */
2180 memset(&event, 0, sizeof(event));
2181 event.loglevel = -1;
2182 event.type = LTTNG_EVENT_ALL;
2183 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
2184 if (ret) {
2185 goto end;
2186 }
2187
2188 for (node = xmlFirstElementChild(events_node); node;
2189 node = xmlNextElementSibling(node)) {
2190 ret = process_event_node(node, handle, channel_name, ENABLE);
2191 if (ret) {
2192 goto end;
2193 }
2194 }
2195
dcf266c0
JG
2196end:
2197 return ret;
2198}
2199
2200static
2201int process_channel_attr_node(xmlNodePtr attr_node,
2202 struct lttng_channel *channel, xmlNodePtr *contexts_node,
2203 xmlNodePtr *events_node)
2204{
2205 int ret;
2206
2207 assert(attr_node);
2208 assert(channel);
2209 assert(contexts_node);
2210 assert(events_node);
2211
2212 if (!strcmp((const char *) attr_node->name, config_element_name)) {
2213 xmlChar *content;
dcf266c0
JG
2214
2215 /* name */
2216 content = xmlNodeGetContent(attr_node);
2217 if (!content) {
2218 ret = -LTTNG_ERR_NOMEM;
2219 goto end;
2220 }
2221
d2e67842
JG
2222 ret = lttng_strncpy(channel->name,
2223 (const char *) content,
2224 LTTNG_SYMBOL_NAME_LEN);
2225 if (ret == -1) {
2226 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2227 (const char *) content,
2228 strlen((const char *) content),
2229 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
2230 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2231 free(content);
2232 goto end;
2233 }
dcf266c0
JG
2234 free(content);
2235 } else if (!strcmp((const char *) attr_node->name,
2236 config_element_enabled)) {
2237 xmlChar *content;
2238 int enabled;
2239
2240 /* enabled */
2241 content = xmlNodeGetContent(attr_node);
2242 if (!content) {
2243 ret = -LTTNG_ERR_NOMEM;
2244 goto end;
2245 }
2246
2247 ret = parse_bool(content, &enabled);
2248 free(content);
2249 if (ret) {
2250 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2251 goto end;
2252 }
2253
2254 channel->enabled = enabled;
2255 } else if (!strcmp((const char *) attr_node->name,
2256 config_element_overwrite_mode)) {
2257 xmlChar *content;
2258
2259 /* overwrite_mode */
2260 content = xmlNodeGetContent(attr_node);
2261 if (!content) {
2262 ret = -LTTNG_ERR_NOMEM;
2263 goto end;
2264 }
2265
2266 ret = get_overwrite_mode(content);
2267 free(content);
2268 if (ret < 0) {
2269 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2270 goto end;
2271 }
2272
2273 channel->attr.overwrite = ret;
2274 } else if (!strcmp((const char *) attr_node->name,
2275 config_element_subbuf_size)) {
2276 xmlChar *content;
2277
2278 /* subbuffer_size */
2279 content = xmlNodeGetContent(attr_node);
2280 if (!content) {
2281 ret = -LTTNG_ERR_NOMEM;
2282 goto end;
2283 }
2284
2285 ret = parse_uint(content, &channel->attr.subbuf_size);
2286 free(content);
2287 if (ret) {
2288 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2289 goto end;
2290 }
2291 } else if (!strcmp((const char *) attr_node->name,
2292 config_element_num_subbuf)) {
2293 xmlChar *content;
2294
2295 /* subbuffer_count */
2296 content = xmlNodeGetContent(attr_node);
2297 if (!content) {
2298 ret = -LTTNG_ERR_NOMEM;
2299 goto end;
2300 }
2301
2302 ret = parse_uint(content, &channel->attr.num_subbuf);
2303 free(content);
2304 if (ret) {
2305 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2306 goto end;
2307 }
2308 } else if (!strcmp((const char *) attr_node->name,
2309 config_element_switch_timer_interval)) {
2310 xmlChar *content;
2311 uint64_t switch_timer_interval = 0;
2312
2313 /* switch_timer_interval */
2314 content = xmlNodeGetContent(attr_node);
2315 if (!content) {
2316 ret = -LTTNG_ERR_NOMEM;
2317 goto end;
2318 }
2319
2320 ret = parse_uint(content, &switch_timer_interval);
2321 free(content);
2322 if (ret) {
2323 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2324 goto end;
2325 }
2326
2327 if (switch_timer_interval > UINT_MAX) {
2328 WARN("switch_timer_interval out of range.");
2329 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2330 goto end;
2331 }
2332
2333 channel->attr.switch_timer_interval =
2334 switch_timer_interval;
2335 } else if (!strcmp((const char *) attr_node->name,
2336 config_element_read_timer_interval)) {
2337 xmlChar *content;
2338 uint64_t read_timer_interval = 0;
2339
2340 /* read_timer_interval */
2341 content = xmlNodeGetContent(attr_node);
2342 if (!content) {
2343 ret = -LTTNG_ERR_NOMEM;
2344 goto end;
2345 }
2346
2347 ret = parse_uint(content, &read_timer_interval);
2348 free(content);
2349 if (ret) {
2350 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2351 goto end;
2352 }
2353
2354 if (read_timer_interval > UINT_MAX) {
2355 WARN("read_timer_interval out of range.");
2356 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2357 goto end;
2358 }
2359
2360 channel->attr.read_timer_interval =
2361 read_timer_interval;
2362 } else if (!strcmp((const char *) attr_node->name,
2363 config_element_output_type)) {
2364 xmlChar *content;
2365
2366 /* output_type */
2367 content = xmlNodeGetContent(attr_node);
2368 if (!content) {
2369 ret = -LTTNG_ERR_NOMEM;
2370 goto end;
2371 }
2372
2373 ret = get_output_type(content);
2374 free(content);
2375 if (ret < 0) {
2376 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2377 goto end;
2378 }
2379
2380 channel->attr.output = ret;
2381 } else if (!strcmp((const char *) attr_node->name,
2382 config_element_tracefile_size)) {
2383 xmlChar *content;
2384
2385 /* tracefile_size */
2386 content = xmlNodeGetContent(attr_node);
2387 if (!content) {
2388 ret = -LTTNG_ERR_NOMEM;
2389 goto end;
2390 }
2391
2392 ret = parse_uint(content, &channel->attr.tracefile_size);
2393 free(content);
2394 if (ret) {
2395 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2396 goto end;
2397 }
2398 } else if (!strcmp((const char *) attr_node->name,
2399 config_element_tracefile_count)) {
2400 xmlChar *content;
2401
2402 /* tracefile_count */
2403 content = xmlNodeGetContent(attr_node);
2404 if (!content) {
2405 ret = -LTTNG_ERR_NOMEM;
2406 goto end;
2407 }
2408
2409 ret = parse_uint(content, &channel->attr.tracefile_count);
2410 free(content);
2411 if (ret) {
2412 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2413 goto end;
2414 }
2415 } else if (!strcmp((const char *) attr_node->name,
2416 config_element_live_timer_interval)) {
2417 xmlChar *content;
2418 uint64_t live_timer_interval = 0;
2419
2420 /* live_timer_interval */
2421 content = xmlNodeGetContent(attr_node);
2422 if (!content) {
2423 ret = -LTTNG_ERR_NOMEM;
2424 goto end;
2425 }
2426
2427 ret = parse_uint(content, &live_timer_interval);
2428 free(content);
2429 if (ret) {
2430 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2431 goto end;
2432 }
2433
2434 if (live_timer_interval > UINT_MAX) {
2435 WARN("live_timer_interval out of range.");
2436 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2437 goto end;
2438 }
2439
2440 channel->attr.live_timer_interval =
2441 live_timer_interval;
4fc2b126
JR
2442 } else if (!strcmp((const char *) attr_node->name,
2443 config_element_monitor_timer_interval)) {
2444 xmlChar *content;
2445 uint64_t monitor_timer_interval = 0;
2446
2447 /* monitor_timer_interval */
2448 content = xmlNodeGetContent(attr_node);
2449 if (!content) {
2450 ret = -LTTNG_ERR_NOMEM;
2451 goto end;
2452 }
2453
2454 ret = parse_uint(content, &monitor_timer_interval);
2455 free(content);
2456 if (ret) {
2457 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2458 goto end;
2459 }
2460
2461 ret = lttng_channel_set_monitor_timer_interval(channel,
2462 monitor_timer_interval);
2463 if (ret) {
2464 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2465 goto end;
2466 }
275472aa
JR
2467 } else if (!strcmp((const char *) attr_node->name,
2468 config_element_blocking_timeout)) {
2469 xmlChar *content;
2470 int64_t blocking_timeout = 0;
2471
2472 /* blocking_timeout */
2473 content = xmlNodeGetContent(attr_node);
2474 if (!content) {
2475 ret = -LTTNG_ERR_NOMEM;
2476 goto end;
2477 }
2478
2479 ret = parse_int(content, &blocking_timeout);
2480 free(content);
2481 if (ret) {
2482 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2483 goto end;
2484 }
2485
2486 ret = lttng_channel_set_blocking_timeout(channel,
2487 blocking_timeout);
2488 if (ret) {
2489 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2490 goto end;
2491 }
dcf266c0
JG
2492 } else if (!strcmp((const char *) attr_node->name,
2493 config_element_events)) {
2494 /* events */
2495 *events_node = attr_node;
2496 } else {
2497 /* contexts */
2498 *contexts_node = attr_node;
2499 }
2500 ret = 0;
2501end:
2502 return ret;
2503}
2504
2505static
2506int process_context_node(xmlNodePtr context_node,
2507 struct lttng_handle *handle, const char *channel_name)
2508{
2509 int ret;
2510 struct lttng_event_context context;
2511 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2512
2513 assert(handle);
2514 assert(channel_name);
2515
2516 if (!context_child_node) {
2517 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2518 goto end;
2519 }
2520
2521 memset(&context, 0, sizeof(context));
2522
2523 if (!strcmp((const char *) context_child_node->name,
2524 config_element_type)) {
2525 /* type */
2526 xmlChar *content = xmlNodeGetContent(context_child_node);
045fc617 2527
dcf266c0
JG
2528 if (!content) {
2529 ret = -LTTNG_ERR_NOMEM;
2530 goto end;
2531 }
2532
2533 ret = get_context_type(content);
2534 free(content);
2535 if (ret < 0) {
2536 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2537 goto end;
2538 }
2539
2540 context.ctx = ret;
045fc617
JG
2541 } else if (!strcmp((const char *) context_child_node->name,
2542 config_element_context_perf)) {
2543 /* perf */
dcf266c0
JG
2544 xmlNodePtr perf_attr_node;
2545
14ce5bd8
JG
2546 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2547 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2548 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
2549 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2550 perf_attr_node; perf_attr_node =
2551 xmlNextElementSibling(perf_attr_node)) {
2552 if (!strcmp((const char *) perf_attr_node->name,
2553 config_element_type)) {
2554 xmlChar *content;
2555 uint64_t type = 0;
2556
2557 /* type */
2558 content = xmlNodeGetContent(perf_attr_node);
2559 if (!content) {
2560 ret = -LTTNG_ERR_NOMEM;
2561 goto end;
2562 }
2563
2564 ret = parse_uint(content, &type);
2565 free(content);
2566 if (ret) {
2567 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2568 goto end;
2569 }
2570
2571 if (type > UINT32_MAX) {
2572 WARN("perf context type out of range.");
2573 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2574 goto end;
2575 }
2576
2577 context.u.perf_counter.type = type;
2578 } else if (!strcmp((const char *) perf_attr_node->name,
2579 config_element_config)) {
2580 xmlChar *content;
2581 uint64_t config = 0;
2582
2583 /* config */
2584 content = xmlNodeGetContent(perf_attr_node);
2585 if (!content) {
2586 ret = -LTTNG_ERR_NOMEM;
2587 goto end;
2588 }
2589
2590 ret = parse_uint(content, &config);
2591 free(content);
2592 if (ret) {
2593 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2594 goto end;
2595 }
2596
2597 context.u.perf_counter.config = config;
2598 } else if (!strcmp((const char *) perf_attr_node->name,
2599 config_element_name)) {
2600 xmlChar *content;
dcf266c0
JG
2601
2602 /* name */
2603 content = xmlNodeGetContent(perf_attr_node);
2604 if (!content) {
2605 ret = -LTTNG_ERR_NOMEM;
2606 goto end;
2607 }
2608
d2e67842
JG
2609 ret = lttng_strncpy(context.u.perf_counter.name,
2610 (const char *) content,
2611 LTTNG_SYMBOL_NAME_LEN);
2612 if (ret == -1) {
2613 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2614 (const char *) content,
2615 strlen((const char *) content),
2616 LTTNG_SYMBOL_NAME_LEN);
dcf266c0
JG
2617 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2618 free(content);
2619 goto end;
2620 }
dcf266c0
JG
2621 free(content);
2622 }
2623 }
045fc617
JG
2624 } else if (!strcmp((const char *) context_child_node->name,
2625 config_element_context_app)) {
2626 /* application context */
2627 xmlNodePtr app_ctx_node;
2628
2629 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2630 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2631 app_ctx_node; app_ctx_node =
2632 xmlNextElementSibling(app_ctx_node)) {
2633 xmlChar *content;
2634 char **target = strcmp(
2635 (const char *) app_ctx_node->name,
2636 config_element_context_app_provider_name) == 0 ?
2637 &context.u.app_ctx.provider_name :
2638 &context.u.app_ctx.ctx_name;
2639
2640 content = xmlNodeGetContent(app_ctx_node);
2641 if (!content) {
2642 ret = -LTTNG_ERR_NOMEM;
2643 goto end;
2644 }
2645
2646 *target = (char *) content;
2647 }
2648 } else {
2649 /* Unrecognized context type */
2650 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2651 goto end;
dcf266c0
JG
2652 }
2653
2654 ret = lttng_add_context(handle, &context, NULL, channel_name);
045fc617
JG
2655 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2656 free(context.u.app_ctx.provider_name);
2657 free(context.u.app_ctx.ctx_name);
2658 }
dcf266c0
JG
2659end:
2660 return ret;
2661}
2662
2663static
2664int process_contexts_node(xmlNodePtr contexts_node,
2665 struct lttng_handle *handle, const char *channel_name)
2666{
2667 int ret = 0;
2668 xmlNodePtr context_node;
2669
2670 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2671 context_node = xmlNextElementSibling(context_node)) {
2672 ret = process_context_node(context_node, handle, channel_name);
2673 if (ret) {
2674 goto end;
2675 }
2676 }
2677end:
2678 return ret;
2679}
2680
159b042f 2681static int get_tracker_elements(enum lttng_process_attr process_attr,
55c9e7ca 2682 const char **element_id_tracker,
159b042f
JG
2683 const char **element_value_type,
2684 const char **element_value,
2685 const char **element_value_alias,
55c9e7ca
JR
2686 const char **element_name)
2687{
2688 int ret = 0;
2689
159b042f
JG
2690 switch (process_attr) {
2691 case LTTNG_PROCESS_ATTR_PROCESS_ID:
2692 *element_id_tracker = config_element_process_attr_tracker_pid;
2693 *element_value_type = config_element_process_attr_pid_value;
2694 *element_value = config_element_process_attr_id;
2695 *element_value_alias = config_element_process_attr_id;
55c9e7ca
JR
2696 *element_name = NULL;
2697 break;
159b042f
JG
2698 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
2699 *element_id_tracker = config_element_process_attr_tracker_vpid;
2700 *element_value_type = config_element_process_attr_vpid_value;
2701 *element_value = config_element_process_attr_id;
2702 *element_value_alias = NULL;
55c9e7ca
JR
2703 *element_name = NULL;
2704 break;
159b042f
JG
2705 case LTTNG_PROCESS_ATTR_USER_ID:
2706 *element_id_tracker = config_element_process_attr_tracker_uid;
2707 *element_value_type = config_element_process_attr_uid_value;
2708 *element_value = config_element_process_attr_id;
2709 *element_value_alias = NULL;
55c9e7ca
JR
2710 *element_name = config_element_name;
2711 break;
159b042f
JG
2712 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
2713 *element_id_tracker = config_element_process_attr_tracker_vuid;
2714 *element_value_type = config_element_process_attr_vuid_value;
2715 *element_value = config_element_process_attr_id;
2716 *element_value_alias = NULL;
55c9e7ca
JR
2717 *element_name = config_element_name;
2718 break;
159b042f
JG
2719 case LTTNG_PROCESS_ATTR_GROUP_ID:
2720 *element_id_tracker = config_element_process_attr_tracker_gid;
2721 *element_value_type = config_element_process_attr_gid_value;
2722 *element_value = config_element_process_attr_id;
2723 *element_value_alias = NULL;
55c9e7ca
JR
2724 *element_name = config_element_name;
2725 break;
159b042f
JG
2726 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
2727 *element_id_tracker = config_element_process_attr_tracker_vgid;
2728 *element_value_type = config_element_process_attr_vgid_value;
2729 *element_value = config_element_process_attr_id;
2730 *element_value_alias = NULL;
55c9e7ca
JR
2731 *element_name = config_element_name;
2732 break;
2733 default:
2734 ret = LTTNG_ERR_INVALID;
2735 }
2736 return ret;
2737}
2738
f7af9a72
JG
2739static int process_legacy_pid_tracker_node(
2740 xmlNodePtr trackers_node, struct lttng_handle *handle)
2741{
2742 int ret = 0, child_count;
2743 xmlNodePtr targets_node = NULL;
2744 xmlNodePtr node;
2745 const char *element_id_tracker;
2746 const char *element_target_id;
2747 const char *element_id;
2748 const char *element_id_alias;
2749 const char *element_name;
2750 enum lttng_error_code tracker_handle_ret_code;
2751 struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
2752 enum lttng_process_attr_tracker_handle_status status;
2753 const enum lttng_process_attr process_attr =
2754 handle->domain.type == LTTNG_DOMAIN_UST ?
2755 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID :
2756 LTTNG_PROCESS_ATTR_PROCESS_ID;
2757
2758 assert(handle);
2759
2760 tracker_handle_ret_code = lttng_session_get_tracker_handle(
2761 handle->session_name, handle->domain.type,
2762 process_attr,
2763 &tracker_handle);
2764 if (tracker_handle_ret_code != LTTNG_OK) {
2765 ret = LTTNG_ERR_INVALID;
2766 goto end;
2767 }
2768
2769 ret = get_tracker_elements(process_attr, &element_id_tracker,
2770 &element_target_id, &element_id, &element_id_alias,
2771 &element_name);
2772 if (ret) {
2773 goto end;
2774 }
2775
2776 /* Get the targets node */
2777 for (node = xmlFirstElementChild(trackers_node); node;
2778 node = xmlNextElementSibling(node)) {
2779 if (!strcmp((const char *) node->name,
2780 config_element_tracker_targets_legacy)) {
2781 targets_node = node;
2782 break;
2783 }
2784 }
2785
2786 if (!targets_node) {
2787 ret = LTTNG_ERR_INVALID;
2788 goto end;
2789 }
2790
2791 /* Go through all id target node */
2792 child_count = xmlChildElementCount(targets_node);
2793 status = lttng_process_attr_tracker_handle_set_tracking_policy(
2794 tracker_handle,
2795 child_count == 0 ? LTTNG_TRACKING_POLICY_EXCLUDE_ALL :
2796 LTTNG_TRACKING_POLICY_INCLUDE_SET);
2797 if (status != LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK) {
2798 ret = LTTNG_ERR_UNK;
2799 goto end;
2800 }
2801
2802 /* Add all tracked values. */
2803 for (node = xmlFirstElementChild(targets_node); node;
2804 node = xmlNextElementSibling(node)) {
2805 xmlNodePtr pid_target_node = node;
2806
2807 /* get pid_target node and track it */
2808 for (node = xmlFirstElementChild(pid_target_node); node;
2809 node = xmlNextElementSibling(node)) {
2810 if (!strcmp((const char *) node->name,
2811 config_element_tracker_pid_legacy)) {
2812 int64_t id;
2813 xmlChar *content = xmlNodeGetContent(node);
2814
2815 if (!content) {
2816 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2817 goto end;
2818 }
2819
2820 ret = parse_int(content, &id);
2821 free(content);
2822 if (ret) {
2823 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2824 goto end;
2825 }
2826
2827 switch (process_attr) {
2828 case LTTNG_PROCESS_ATTR_PROCESS_ID:
2829 status = lttng_process_attr_process_id_tracker_handle_add_pid(
2830 tracker_handle,
2831 (pid_t) id);
2832 break;
2833 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
2834 status = lttng_process_attr_virtual_process_id_tracker_handle_add_pid(
2835 tracker_handle,
2836 (pid_t) id);
2837 break;
2838 default:
2839 ret = LTTNG_ERR_INVALID;
2840 goto end;
2841 }
2842 }
2843 switch (status) {
2844 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK:
2845 continue;
2846 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID:
2847 ret = LTTNG_ERR_INVALID;
2848 break;
2849 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS:
2850 ret = LTTNG_ERR_PROCESS_ATTR_EXISTS;
2851 break;
2852 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING:
2853 ret = LTTNG_ERR_PROCESS_ATTR_MISSING;
2854 break;
2855 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR:
2856 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR:
2857 default:
2858 ret = LTTNG_ERR_UNK;
2859 goto end;
2860 }
2861 }
2862 node = pid_target_node;
2863 }
2864
2865end:
2866 lttng_process_attr_tracker_handle_destroy(tracker_handle);
2867 return ret;
2868 }
2869
55c9e7ca
JR
2870static int process_id_tracker_node(xmlNodePtr id_tracker_node,
2871 struct lttng_handle *handle,
159b042f 2872 enum lttng_process_attr process_attr)
847a5916 2873{
159b042f 2874 int ret = 0, child_count;
f7af9a72 2875 xmlNodePtr values_node = NULL;
847a5916 2876 xmlNodePtr node;
55c9e7ca
JR
2877 const char *element_id_tracker;
2878 const char *element_target_id;
2879 const char *element_id;
2880 const char *element_id_alias;
2881 const char *element_name;
159b042f
JG
2882 enum lttng_error_code tracker_handle_ret_code;
2883 struct lttng_process_attr_tracker_handle *tracker_handle = NULL;
2884 enum lttng_process_attr_tracker_handle_status status;
847a5916
JR
2885
2886 assert(handle);
55c9e7ca
JR
2887 assert(id_tracker_node);
2888
159b042f
JG
2889 tracker_handle_ret_code = lttng_session_get_tracker_handle(
2890 handle->session_name, handle->domain.type, process_attr,
2891 &tracker_handle);
2892 if (tracker_handle_ret_code != LTTNG_OK) {
2893 ret = LTTNG_ERR_INVALID;
2894 goto end;
2895 }
2896
2897 ret = get_tracker_elements(process_attr, &element_id_tracker,
55c9e7ca
JR
2898 &element_target_id, &element_id, &element_id_alias,
2899 &element_name);
2900 if (ret) {
159b042f 2901 goto end;
55c9e7ca
JR
2902 }
2903
f7af9a72 2904 /* get the values node */
55c9e7ca
JR
2905 for (node = xmlFirstElementChild(id_tracker_node); node;
2906 node = xmlNextElementSibling(node)) {
847a5916 2907 if (!strcmp((const char *) node->name,
159b042f 2908 config_element_process_attr_values)) {
f7af9a72 2909 values_node = node;
847a5916
JR
2910 break;
2911 }
2912 }
2913
f7af9a72 2914 if (!values_node) {
847a5916
JR
2915 ret = LTTNG_ERR_INVALID;
2916 goto end;
2917 }
2918
55c9e7ca 2919 /* Go through all id target node */
f7af9a72 2920 child_count = xmlChildElementCount(values_node);
159b042f
JG
2921 status = lttng_process_attr_tracker_handle_set_tracking_policy(
2922 tracker_handle,
2923 child_count == 0 ? LTTNG_TRACKING_POLICY_EXCLUDE_ALL :
2924 LTTNG_TRACKING_POLICY_INCLUDE_SET);
2925 if (status != LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK) {
2926 ret = LTTNG_ERR_UNK;
2927 goto end;
847a5916 2928 }
159b042f
JG
2929
2930 /* Add all tracked values. */
f7af9a72 2931 for (node = xmlFirstElementChild(values_node); node;
847a5916 2932 node = xmlNextElementSibling(node)) {
55c9e7ca 2933 xmlNodePtr id_target_node = node;
847a5916 2934
55c9e7ca
JR
2935 /* get id node and track it */
2936 for (node = xmlFirstElementChild(id_target_node); node;
2937 node = xmlNextElementSibling(node)) {
2938 if (!strcmp((const char *) node->name, element_id) ||
2939 (element_id_alias &&
2940 !strcmp((const char *) node->name,
2941 element_id_alias))) {
2942 int64_t id;
159b042f 2943 xmlChar *content = xmlNodeGetContent(node);
847a5916 2944
847a5916
JR
2945 if (!content) {
2946 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2947 goto end;
2948 }
2949
55c9e7ca 2950 ret = parse_int(content, &id);
847a5916
JR
2951 free(content);
2952 if (ret) {
2953 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2954 goto end;
2955 }
2956
159b042f
JG
2957 switch (process_attr) {
2958 case LTTNG_PROCESS_ATTR_PROCESS_ID:
2959 status = lttng_process_attr_process_id_tracker_handle_add_pid(
2960 tracker_handle,
2961 (pid_t) id);
2962 break;
2963 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
2964 status = lttng_process_attr_virtual_process_id_tracker_handle_add_pid(
2965 tracker_handle,
2966 (pid_t) id);
2967 break;
2968 case LTTNG_PROCESS_ATTR_USER_ID:
2969 status = lttng_process_attr_user_id_tracker_handle_add_uid(
2970 tracker_handle,
2971 (uid_t) id);
2972 break;
2973 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
2974 status = lttng_process_attr_virtual_user_id_tracker_handle_add_uid(
2975 tracker_handle,
2976 (uid_t) id);
2977 break;
2978 case LTTNG_PROCESS_ATTR_GROUP_ID:
2979 status = lttng_process_attr_group_id_tracker_handle_add_gid(
2980 tracker_handle,
2981 (gid_t) id);
2982 break;
2983 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
2984 status = lttng_process_attr_virtual_group_id_tracker_handle_add_gid(
2985 tracker_handle,
2986 (gid_t) id);
2987 break;
2988 default:
2989 ret = LTTNG_ERR_INVALID;
55c9e7ca
JR
2990 goto end;
2991 }
159b042f
JG
2992 } else if (element_name &&
2993 !strcmp((const char *) node->name,
2994 element_name)) {
2995 xmlChar *content = xmlNodeGetContent(node);
55c9e7ca 2996
55c9e7ca
JR
2997 if (!content) {
2998 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2999 goto end;
3000 }
2d97a006 3001
159b042f
JG
3002 switch (process_attr) {
3003 case LTTNG_PROCESS_ATTR_USER_ID:
3004 status = lttng_process_attr_user_id_tracker_handle_add_user_name(
3005 tracker_handle,
3006 (const char *) content);
3007 break;
3008 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
3009 status = lttng_process_attr_virtual_user_id_tracker_handle_add_user_name(
3010 tracker_handle,
3011 (const char *) content);
3012 break;
3013 case LTTNG_PROCESS_ATTR_GROUP_ID:
3014 status = lttng_process_attr_group_id_tracker_handle_add_group_name(
3015 tracker_handle,
3016 (const char *) content);
3017 break;
3018 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
3019 status = lttng_process_attr_virtual_group_id_tracker_handle_add_group_name(
3020 tracker_handle,
3021 (const char *) content);
3022 break;
3023 default:
3024 free(content);
3025 ret = LTTNG_ERR_INVALID;
2d97a006
JR
3026 goto end;
3027 }
55c9e7ca 3028 free(content);
159b042f
JG
3029 }
3030 switch (status) {
3031 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_OK:
3032 continue;
3033 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_INVALID:
3034 ret = LTTNG_ERR_INVALID;
3035 break;
3036 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_EXISTS:
3037 ret = LTTNG_ERR_PROCESS_ATTR_EXISTS;
3038 break;
3039 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_MISSING:
3040 ret = LTTNG_ERR_PROCESS_ATTR_MISSING;
3041 break;
3042 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_ERROR:
3043 case LTTNG_PROCESS_ATTR_TRACKER_HANDLE_STATUS_COMMUNICATION_ERROR:
3044 default:
3045 ret = LTTNG_ERR_UNK;
3046 goto end;
847a5916
JR
3047 }
3048 }
55c9e7ca 3049 node = id_target_node;
847a5916
JR
3050 }
3051
3052end:
159b042f 3053 lttng_process_attr_tracker_handle_destroy(tracker_handle);
847a5916
JR
3054 return ret;
3055}
3056
dcf266c0
JG
3057static
3058int process_domain_node(xmlNodePtr domain_node, const char *session_name)
3059{
3060 int ret;
3061 struct lttng_domain domain = { 0 };
3062 struct lttng_handle *handle = NULL;
4fc2b126 3063 struct lttng_channel *channel = NULL;
dcf266c0 3064 xmlNodePtr channels_node = NULL;
847a5916
JR
3065 xmlNodePtr trackers_node = NULL;
3066 xmlNodePtr pid_tracker_node = NULL;
55c9e7ca
JR
3067 xmlNodePtr vpid_tracker_node = NULL;
3068 xmlNodePtr uid_tracker_node = NULL;
3069 xmlNodePtr vuid_tracker_node = NULL;
3070 xmlNodePtr gid_tracker_node = NULL;
3071 xmlNodePtr vgid_tracker_node = NULL;
dcf266c0
JG
3072 xmlNodePtr node;
3073
3074 assert(session_name);
3075
3076 ret = init_domain(domain_node, &domain);
3077 if (ret) {
3078 goto end;
3079 }
3080
3081 handle = lttng_create_handle(session_name, &domain);
3082 if (!handle) {
3083 ret = -LTTNG_ERR_NOMEM;
3084 goto end;
3085 }
3086
3087 /* get the channels node */
3088 for (node = xmlFirstElementChild(domain_node); node;
3089 node = xmlNextElementSibling(node)) {
3090 if (!strcmp((const char *) node->name,
3091 config_element_channels)) {
3092 channels_node = node;
3093 break;
3094 }
3095 }
3096
3097 if (!channels_node) {
3098 goto end;
3099 }
3100
3101 /* create all channels */
3102 for (node = xmlFirstElementChild(channels_node); node;
3103 node = xmlNextElementSibling(node)) {
36d1687c 3104 const enum lttng_domain_type original_domain = domain.type;
dcf266c0
JG
3105 xmlNodePtr contexts_node = NULL;
3106 xmlNodePtr events_node = NULL;
3107 xmlNodePtr channel_attr_node;
3108
36d1687c
JG
3109 /*
3110 * Channels of the "agent" types cannot be created directly.
3111 * They are meant to be created implicitly through the
3112 * activation of events in their domain. However, a user
3113 * can override the default channel configuration attributes
3114 * by creating the underlying UST channel _before_ enabling
3115 * an agent domain event.
3116 *
3117 * Hence, the channel's type is substituted before the creation
3118 * and restored by the time the events are created.
3119 */
3120 switch (domain.type) {
3121 case LTTNG_DOMAIN_JUL:
3122 case LTTNG_DOMAIN_LOG4J:
3123 case LTTNG_DOMAIN_PYTHON:
3124 domain.type = LTTNG_DOMAIN_UST;
3125 default:
3126 break;
3127 }
3128
4fc2b126
JR
3129 channel = lttng_channel_create(&domain);
3130 if (!channel) {
3131 ret = -1;
3132 goto end;
3133 }
dcf266c0
JG
3134
3135 for (channel_attr_node = xmlFirstElementChild(node);
3136 channel_attr_node; channel_attr_node =
3137 xmlNextElementSibling(channel_attr_node)) {
3138 ret = process_channel_attr_node(channel_attr_node,
4fc2b126 3139 channel, &contexts_node, &events_node);
dcf266c0
JG
3140 if (ret) {
3141 goto end;
3142 }
3143 }
3144
4fc2b126 3145 ret = lttng_enable_channel(handle, channel);
dcf266c0
JG
3146 if (ret < 0) {
3147 goto end;
3148 }
3149
36d1687c
JG
3150 /* Restore the original channel domain. */
3151 domain.type = original_domain;
3152
4fc2b126 3153 ret = process_events_node(events_node, handle, channel->name);
dcf266c0
JG
3154 if (ret) {
3155 goto end;
3156 }
3157
3158 ret = process_contexts_node(contexts_node, handle,
4fc2b126 3159 channel->name);
dcf266c0
JG
3160 if (ret) {
3161 goto end;
3162 }
4fc2b126
JR
3163
3164 lttng_channel_destroy(channel);
dcf266c0 3165 }
4fc2b126 3166 channel = NULL;
847a5916
JR
3167
3168 /* get the trackers node */
3169 for (node = xmlFirstElementChild(domain_node); node;
3170 node = xmlNextElementSibling(node)) {
3171 if (!strcmp((const char *) node->name,
f7af9a72
JG
3172 config_element_process_attr_trackers) ||
3173 !strcmp((const char *) node->name,
3174 config_element_trackers_legacy)) {
3175 if (trackers_node) {
3176 ERR("Only one instance of `%s` or `%s` is allowed in a session configuration",
3177 config_element_process_attr_trackers,
3178 config_element_trackers_legacy);
3179 ret = -1;
3180 goto end;
3181 }
847a5916
JR
3182 trackers_node = node;
3183 break;
3184 }
3185 }
3186
3187 if (!trackers_node) {
3188 goto end;
3189 }
3190
3191 for (node = xmlFirstElementChild(trackers_node); node;
3192 node = xmlNextElementSibling(node)) {
55c9e7ca 3193 if (!strcmp((const char *) node->name,
159b042f 3194 config_element_process_attr_tracker_pid)) {
847a5916 3195 pid_tracker_node = node;
55c9e7ca 3196 ret = process_id_tracker_node(pid_tracker_node, handle,
159b042f 3197 LTTNG_PROCESS_ATTR_PROCESS_ID);
55c9e7ca
JR
3198 if (ret) {
3199 goto end;
3200 }
3201 }
3202 if (!strcmp((const char *) node->name,
159b042f 3203 config_element_process_attr_tracker_vpid)) {
55c9e7ca
JR
3204 vpid_tracker_node = node;
3205 ret = process_id_tracker_node(vpid_tracker_node, handle,
159b042f 3206 LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID);
55c9e7ca
JR
3207 if (ret) {
3208 goto end;
3209 }
3210 }
3211 if (!strcmp((const char *) node->name,
159b042f 3212 config_element_process_attr_tracker_uid)) {
55c9e7ca
JR
3213 uid_tracker_node = node;
3214 ret = process_id_tracker_node(uid_tracker_node, handle,
159b042f 3215 LTTNG_PROCESS_ATTR_USER_ID);
55c9e7ca
JR
3216 if (ret) {
3217 goto end;
3218 }
3219 }
3220 if (!strcmp((const char *) node->name,
159b042f 3221 config_element_process_attr_tracker_vuid)) {
55c9e7ca
JR
3222 vuid_tracker_node = node;
3223 ret = process_id_tracker_node(vuid_tracker_node, handle,
159b042f 3224 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID);
55c9e7ca
JR
3225 if (ret) {
3226 goto end;
3227 }
3228 }
3229 if (!strcmp((const char *) node->name,
159b042f 3230 config_element_process_attr_tracker_gid)) {
55c9e7ca
JR
3231 gid_tracker_node = node;
3232 ret = process_id_tracker_node(gid_tracker_node, handle,
159b042f 3233 LTTNG_PROCESS_ATTR_GROUP_ID);
55c9e7ca
JR
3234 if (ret) {
3235 goto end;
3236 }
3237 }
3238 if (!strcmp((const char *) node->name,
159b042f 3239 config_element_process_attr_tracker_vgid)) {
55c9e7ca
JR
3240 vgid_tracker_node = node;
3241 ret = process_id_tracker_node(vgid_tracker_node, handle,
159b042f 3242 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID);
847a5916
JR
3243 if (ret) {
3244 goto end;
3245 }
3246 }
f7af9a72
JG
3247 if (!strcmp((const char *) node->name,
3248 config_element_pid_tracker_legacy)) {
3249 ret = process_legacy_pid_tracker_node(node, handle);
3250 if (ret) {
3251 goto end;
3252 }
3253 }
847a5916
JR
3254 }
3255
dcf266c0 3256end:
4fc2b126 3257 lttng_channel_destroy(channel);
dcf266c0
JG
3258 lttng_destroy_handle(handle);
3259 return ret;
3260}
3261
66ea93b1
JG
3262static
3263int add_periodic_rotation(const char *name, uint64_t time_us)
3264{
3265 int ret;
3266 enum lttng_rotation_status status;
3267 struct lttng_rotation_schedule *periodic =
3268 lttng_rotation_schedule_periodic_create();
3269
3270 if (!periodic) {
3271 ret = -LTTNG_ERR_NOMEM;
3272 goto error;
3273 }
3274
3275 status = lttng_rotation_schedule_periodic_set_period(periodic,
3276 time_us);
3277 if (status != LTTNG_ROTATION_STATUS_OK) {
3278 ret = -LTTNG_ERR_INVALID;
3279 goto error;
3280 }
3281
3282 status = lttng_session_add_rotation_schedule(name, periodic);
3283 switch (status) {
3284 case LTTNG_ROTATION_STATUS_OK:
ce6176f2 3285 ret = 0;
66ea93b1
JG
3286 break;
3287 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
3288 case LTTNG_ROTATION_STATUS_INVALID:
3289 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3290 break;
3291 default:
3292 ret = -LTTNG_ERR_UNK;
3293 break;
3294 }
3295error:
3296 lttng_rotation_schedule_destroy(periodic);
3297 return ret;
3298}
3299
3300static
3301int add_size_rotation(const char *name, uint64_t size_bytes)
3302{
3303 int ret;
3304 enum lttng_rotation_status status;
3305 struct lttng_rotation_schedule *size =
3306 lttng_rotation_schedule_size_threshold_create();
3307
3308 if (!size) {
3309 ret = -LTTNG_ERR_NOMEM;
3310 goto error;
3311 }
3312
3313 status = lttng_rotation_schedule_size_threshold_set_threshold(size,
3314 size_bytes);
3315 if (status != LTTNG_ROTATION_STATUS_OK) {
3316 ret = -LTTNG_ERR_INVALID;
3317 goto error;
3318 }
3319
3320 status = lttng_session_add_rotation_schedule(name, size);
3321 switch (status) {
3322 case LTTNG_ROTATION_STATUS_OK:
ce6176f2 3323 ret = 0;
66ea93b1
JG
3324 break;
3325 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
3326 case LTTNG_ROTATION_STATUS_INVALID:
3327 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3328 break;
3329 default:
3330 ret = -LTTNG_ERR_UNK;
3331 break;
3332 }
3333error:
3334 lttng_rotation_schedule_destroy(size);
3335 return ret;
3336}
3337
ce6176f2
JG
3338static
3339int process_session_rotation_schedules_node(
3340 xmlNodePtr schedules_node,
3341 uint64_t *rotation_timer_interval,
3342 uint64_t *rotation_size)
3343{
3344 int ret = 0;
3345 xmlNodePtr child;
3346
3347 for (child = xmlFirstElementChild(schedules_node);
3348 child;
3349 child = xmlNextElementSibling(child)) {
3350 if (!strcmp((const char *) child->name,
3351 config_element_rotation_schedule_periodic)) {
3352 xmlChar *content;
3353 xmlNodePtr time_us_node;
3354
3355 /* periodic rotation schedule */
3356 time_us_node = xmlFirstElementChild(child);
3357 if (!time_us_node ||
3358 strcmp((const char *) time_us_node->name,
3359 config_element_rotation_schedule_periodic_time_us)) {
3360 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3361 goto end;
3362 }
3363
3364 /* time_us child */
3365 content = xmlNodeGetContent(time_us_node);
3366 if (!content) {
3367 ret = -LTTNG_ERR_NOMEM;
3368 goto end;
3369 }
3370 ret = parse_uint(content, rotation_timer_interval);
3371 free(content);
3372 if (ret) {
3373 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3374 goto end;
3375 }
3376 } else if (!strcmp((const char *) child->name,
3377 config_element_rotation_schedule_size_threshold)) {
3378 xmlChar *content;
3379 xmlNodePtr bytes_node;
3380
3381 /* size_threshold rotation schedule */
3382 bytes_node = xmlFirstElementChild(child);
3383 if (!bytes_node ||
3384 strcmp((const char *) bytes_node->name,
3385 config_element_rotation_schedule_size_threshold_bytes)) {
3386 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3387 goto end;
3388 }
3389
3390 /* bytes child */
3391 content = xmlNodeGetContent(bytes_node);
3392 if (!content) {
3393 ret = -LTTNG_ERR_NOMEM;
3394 goto end;
3395 }
3396 ret = parse_uint(content, rotation_size);
3397 free(content);
3398 if (ret) {
3399 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3400 goto end;
3401 }
3402 }
3403 }
3404
3405end:
3406 return ret;
3407}
3408
dcf266c0
JG
3409static
3410int process_session_node(xmlNodePtr session_node, const char *session_name,
1b08cbce
JR
3411 int overwrite,
3412 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3413{
3414 int ret, started = -1, snapshot_mode = -1;
259c2674 3415 uint64_t live_timer_interval = UINT64_MAX,
90936dcf
JD
3416 rotation_timer_interval = 0,
3417 rotation_size = 0;
d324faf7 3418 xmlChar *name = NULL;
9e7c9f56 3419 xmlChar *shm_path = NULL;
dcf266c0
JG
3420 xmlNodePtr domains_node = NULL;
3421 xmlNodePtr output_node = NULL;
3422 xmlNodePtr node;
90936dcf 3423 xmlNodePtr attributes_child;
dcf266c0
JG
3424 struct lttng_domain *kernel_domain = NULL;
3425 struct lttng_domain *ust_domain = NULL;
3426 struct lttng_domain *jul_domain = NULL;
5cdb6027 3427 struct lttng_domain *log4j_domain = NULL;
0e115563 3428 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
3429
3430 for (node = xmlFirstElementChild(session_node); node;
3431 node = xmlNextElementSibling(node)) {
3432 if (!name && !strcmp((const char *) node->name,
3433 config_element_name)) {
3434 /* name */
3435 xmlChar *node_content = xmlNodeGetContent(node);
3436 if (!node_content) {
3437 ret = -LTTNG_ERR_NOMEM;
c2da8cde 3438 goto error;
dcf266c0
JG
3439 }
3440
d324faf7 3441 name = node_content;
dcf266c0
JG
3442 } else if (!domains_node && !strcmp((const char *) node->name,
3443 config_element_domains)) {
3444 /* domains */
3445 domains_node = node;
3446 } else if (started == -1 && !strcmp((const char *) node->name,
3447 config_element_started)) {
3448 /* started */
3449 xmlChar *node_content = xmlNodeGetContent(node);
3450 if (!node_content) {
3451 ret = -LTTNG_ERR_NOMEM;
c2da8cde 3452 goto error;
dcf266c0
JG
3453 }
3454
3455 ret = parse_bool(node_content, &started);
3456 free(node_content);
3457 if (ret) {
3458 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 3459 goto error;
dcf266c0
JG
3460 }
3461 } else if (!output_node && !strcmp((const char *) node->name,
3462 config_element_output)) {
3463 /* output */
3464 output_node = node;
9e7c9f56
JR
3465 } else if (!shm_path && !strcmp((const char *) node->name,
3466 config_element_shared_memory_path)) {
3467 /* shared memory path */
3468 xmlChar *node_content = xmlNodeGetContent(node);
3469 if (!node_content) {
3470 ret = -LTTNG_ERR_NOMEM;
3471 goto error;
3472 }
3473
3474 shm_path = node_content;
dcf266c0 3475 } else {
259c2674
JD
3476 /*
3477 * attributes, snapshot_mode, live_timer_interval, rotation_size,
90936dcf
JD
3478 * rotation_timer_interval.
3479 */
3480 for (attributes_child = xmlFirstElementChild(node); attributes_child;
3481 attributes_child = xmlNextElementSibling(attributes_child)) {
3482 if (!strcmp((const char *) attributes_child->name,
3483 config_element_snapshot_mode)) {
3484 /* snapshot_mode */
3485 xmlChar *snapshot_mode_content =
3486 xmlNodeGetContent(attributes_child);
3487 if (!snapshot_mode_content) {
3488 ret = -LTTNG_ERR_NOMEM;
3489 goto error;
3490 }
dcf266c0 3491
90936dcf
JD
3492 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
3493 free(snapshot_mode_content);
3494 if (ret) {
3495 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3496 goto error;
3497 }
3498 } else if (!strcmp((const char *) attributes_child->name,
3499 config_element_live_timer_interval)) {
3500 /* live_timer_interval */
3501 xmlChar *timer_interval_content =
3502 xmlNodeGetContent(attributes_child);
3503 if (!timer_interval_content) {
3504 ret = -LTTNG_ERR_NOMEM;
3505 goto error;
3506 }
dcf266c0 3507
90936dcf
JD
3508 ret = parse_uint(timer_interval_content, &live_timer_interval);
3509 free(timer_interval_content);
3510 if (ret) {
3511 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3512 goto error;
3513 }
ce6176f2
JG
3514 } else if (!strcmp((const char *) attributes_child->name,
3515 config_element_rotation_schedules)) {
3516 ret = process_session_rotation_schedules_node(
3517 attributes_child,
3518 &rotation_timer_interval,
3519 &rotation_size);
90936dcf
JD
3520 if (ret) {
3521 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3522 goto error;
3523 }
259c2674 3524
259c2674
JD
3525 }
3526 }
dcf266c0
JG
3527 }
3528 }
3529
3530 if (!name) {
3531 /* Mandatory attribute, as defined in the session XSD */
3532 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 3533 goto error;
dcf266c0
JG
3534 }
3535
d324faf7 3536 if (session_name && strcmp((char *) name, session_name)) {
dcf266c0 3537 /* This is not the session we are looking for */
c2da8cde
DG
3538 ret = -LTTNG_ERR_NO_SESSION;
3539 goto error;
dcf266c0
JG
3540 }
3541
3542 /* Init domains to create the session handles */
3543 for (node = xmlFirstElementChild(domains_node); node;
3544 node = xmlNextElementSibling(node)) {
3545 struct lttng_domain *domain;
3546
3547 domain = zmalloc(sizeof(*domain));
3548 if (!domain) {
3549 ret = -LTTNG_ERR_NOMEM;
c2da8cde 3550 goto error;
dcf266c0
JG
3551 }
3552
3553 ret = init_domain(node, domain);
3554 if (ret) {
3555 goto domain_init_error;
3556 }
3557
3558 switch (domain->type) {
3559 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
3560 if (kernel_domain) {
3561 /* Same domain seen twice, invalid! */
3562 goto domain_init_error;
3563 }
dcf266c0
JG
3564 kernel_domain = domain;
3565 break;
3566 case LTTNG_DOMAIN_UST:
c33e6729
DG
3567 if (ust_domain) {
3568 /* Same domain seen twice, invalid! */
3569 goto domain_init_error;
3570 }
dcf266c0
JG
3571 ust_domain = domain;
3572 break;
3573 case LTTNG_DOMAIN_JUL:
c33e6729
DG
3574 if (jul_domain) {
3575 /* Same domain seen twice, invalid! */
3576 goto domain_init_error;
3577 }
dcf266c0
JG
3578 jul_domain = domain;
3579 break;
5cdb6027
DG
3580 case LTTNG_DOMAIN_LOG4J:
3581 if (log4j_domain) {
3582 /* Same domain seen twice, invalid! */
3583 goto domain_init_error;
3584 }
3585 log4j_domain = domain;
3586 break;
0e115563
DG
3587 case LTTNG_DOMAIN_PYTHON:
3588 if (python_domain) {
3589 /* Same domain seen twice, invalid! */
3590 goto domain_init_error;
3591 }
3592 python_domain = domain;
3593 break;
dcf266c0
JG
3594 default:
3595 WARN("Invalid domain type");
3596 goto domain_init_error;
3597 }
3598 continue;
3599domain_init_error:
3600 free(domain);
3601 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 3602 goto error;
dcf266c0
JG
3603 }
3604
2aaf5fc7
JR
3605 /* Apply overrides */
3606 if (overrides) {
3607 if (overrides->session_name) {
3608 xmlChar *name_override = xmlStrdup(BAD_CAST(overrides->session_name));
3609 if (!name_override) {
3610 ret = -LTTNG_ERR_NOMEM;
3611 goto error;
3612 }
3613
3614 /* Overrides the session name to the provided name */
3615 xmlFree(name);
3616 name = name_override;
3617 }
3618 }
3619
40b4155f 3620 if (overwrite) {
dcf266c0 3621 /* Destroy session if it exists */
d324faf7 3622 ret = lttng_destroy_session((const char *) name);
dcf266c0
JG
3623 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
3624 ERR("Failed to destroy existing session.");
c2da8cde 3625 goto error;
dcf266c0
JG
3626 }
3627 }
3628
3629 /* Create session type depending on output type */
3630 if (snapshot_mode && snapshot_mode != -1) {
1b08cbce
JR
3631 ret = create_snapshot_session((const char *) name, output_node,
3632 overrides);
dcf266c0
JG
3633 } else if (live_timer_interval &&
3634 live_timer_interval != UINT64_MAX) {
194dfca0 3635 ret = create_session((const char *) name,
1b08cbce 3636 output_node, live_timer_interval, overrides);
dcf266c0
JG
3637 } else {
3638 /* regular session */
194dfca0 3639 ret = create_session((const char *) name,
1b08cbce 3640 output_node, UINT64_MAX, overrides);
dcf266c0 3641 }
dcf266c0 3642 if (ret) {
c2da8cde 3643 goto error;
dcf266c0
JG
3644 }
3645
9e7c9f56
JR
3646 if (shm_path) {
3647 ret = lttng_set_session_shm_path((const char *) name,
d324faf7 3648 (const char *) shm_path);
9e7c9f56
JR
3649 if (ret) {
3650 goto error;
3651 }
3652 }
3653
dcf266c0
JG
3654 for (node = xmlFirstElementChild(domains_node); node;
3655 node = xmlNextElementSibling(node)) {
d324faf7 3656 ret = process_domain_node(node, (const char *) name);
dcf266c0
JG
3657 if (ret) {
3658 goto end;
3659 }
3660 }
3661
66ea93b1
JG
3662 if (rotation_timer_interval) {
3663 ret = add_periodic_rotation((const char *) name,
3664 rotation_timer_interval);
3665 if (ret < 0) {
259c2674
JD
3666 goto error;
3667 }
66ea93b1
JG
3668 }
3669 if (rotation_size) {
3670 ret = add_size_rotation((const char *) name,
dbd512ea 3671 rotation_size);
66ea93b1 3672 if (ret < 0) {
259c2674
JD
3673 goto error;
3674 }
3675 }
3676
dcf266c0 3677 if (started) {
d324faf7 3678 ret = lttng_start_tracing((const char *) name);
dcf266c0
JG
3679 if (ret) {
3680 goto end;
3681 }
3682 }
c2da8cde 3683
dcf266c0 3684end:
b2579dc1 3685 if (ret < 0) {
d324faf7
JG
3686 ERR("Failed to load session %s: %s", (const char *) name,
3687 lttng_strerror(ret));
3688 lttng_destroy_session((const char *) name);
b2579dc1
JG
3689 }
3690
c2da8cde 3691error:
dcf266c0
JG
3692 free(kernel_domain);
3693 free(ust_domain);
3694 free(jul_domain);
5cdb6027 3695 free(log4j_domain);
135a3893 3696 free(python_domain);
d324faf7 3697 xmlFree(name);
9e7c9f56 3698 xmlFree(shm_path);
dcf266c0
JG
3699 return ret;
3700}
3701
cf53c06d
DG
3702/*
3703 * Return 1 if the given path is readable by the current UID or 0 if not.
3704 * Return -1 if the path is EPERM.
3705 */
3706static int validate_file_read_creds(const char *path)
3707{
3708 int ret;
3709
3710 assert(path);
3711
3712 /* Can we read the file. */
3713 ret = access(path, R_OK);
3714 if (!ret) {
3715 goto valid;
3716 }
3717 if (errno == EACCES) {
3718 return -1;
3719 } else {
3720 /* Invalid. */
3721 return 0;
3722 }
3723valid:
3724 return 1;
3725}
3726
dcf266c0
JG
3727static
3728int load_session_from_file(const char *path, const char *session_name,
1b08cbce
JR
3729 struct session_config_validation_ctx *validation_ctx, int overwrite,
3730 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3731{
3732 int ret, session_found = !session_name;
3733 xmlDocPtr doc = NULL;
3734 xmlNodePtr sessions_node;
3735 xmlNodePtr session_node;
dcf266c0
JG
3736
3737 assert(path);
3738 assert(validation_ctx);
3739
cf53c06d
DG
3740 ret = validate_file_read_creds(path);
3741 if (ret != 1) {
3742 if (ret == -1) {
3743 ret = -LTTNG_ERR_EPERM;
3744 } else {
3745 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
3746 }
dcf266c0
JG
3747 goto end;
3748 }
3749
3750 doc = xmlParseFile(path);
3751 if (!doc) {
3752 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3753 goto end;
3754 }
3755
3756 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
3757 if (ret) {
3758 ERR("Session configuration file validation failed");
3759 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3760 goto end;
3761 }
3762
3763 sessions_node = xmlDocGetRootElement(doc);
3764 if (!sessions_node) {
55c9e7ca 3765 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
dcf266c0
JG
3766 goto end;
3767 }
3768
3769 for (session_node = xmlFirstElementChild(sessions_node);
3770 session_node; session_node =
3771 xmlNextElementSibling(session_node)) {
3772 ret = process_session_node(session_node,
1b08cbce 3773 session_name, overwrite, overrides);
317ea246
JG
3774 if (!session_name && ret) {
3775 /* Loading error occurred. */
3776 goto end;
3777 } else if (session_name) {
3778 if (ret == 0) {
3779 /* Target session found and loaded */
3780 session_found = 1;
3781 break;
3782 } else if (ret == -LTTNG_ERR_NO_SESSION) {
3783 /*
3784 * Ignore this error, we are looking for a
3785 * specific session.
3786 */
3787 ret = 0;
3788 } else {
3789 /* Loading error occurred. */
3790 goto end;
3791 }
dcf266c0
JG
3792 }
3793 }
3794end:
3795 xmlFreeDoc(doc);
3796 if (!ret) {
a96bc65d 3797 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
3798 }
3799 return ret;
3800}
3801
3802static
3803int load_session_from_path(const char *path, const char *session_name,
1b08cbce
JR
3804 struct session_config_validation_ctx *validation_ctx, int overwrite,
3805 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
3806{
3807 int ret, session_found = !session_name;
dcf266c0 3808 DIR *directory = NULL;
cce35f91
JG
3809 struct lttng_dynamic_buffer file_path;
3810 size_t path_len;
dcf266c0
JG
3811
3812 assert(path);
3813 assert(validation_ctx);
cce35f91
JG
3814 path_len = strlen(path);
3815 lttng_dynamic_buffer_init(&file_path);
3816 if (path_len >= LTTNG_PATH_MAX) {
3817 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
3818 path, path_len, LTTNG_PATH_MAX);
3819 ret = -LTTNG_ERR_INVALID;
3820 goto end;
3821 }
dcf266c0 3822
4af16958
DG
3823 directory = opendir(path);
3824 if (!directory) {
11143783
DG
3825 switch (errno) {
3826 case ENOTDIR:
0f0a81b5
DG
3827 /* Try the file loading. */
3828 break;
11143783
DG
3829 case ENOENT:
3830 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
3831 goto end;
3832 default:
0f0a81b5
DG
3833 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3834 goto end;
4af16958 3835 }
dcf266c0 3836 }
4af16958 3837 if (directory) {
cce35f91 3838 size_t file_path_root_len;
dcf266c0 3839
cce35f91
JG
3840 ret = lttng_dynamic_buffer_set_capacity(&file_path,
3841 LTTNG_PATH_MAX);
3842 if (ret) {
3843 ret = -LTTNG_ERR_NOMEM;
dcf266c0
JG
3844 goto end;
3845 }
3846
cce35f91
JG
3847 ret = lttng_dynamic_buffer_append(&file_path, path, path_len);
3848 if (ret) {
dcf266c0 3849 ret = -LTTNG_ERR_NOMEM;
dcf266c0
JG
3850 goto end;
3851 }
3852
cce35f91
JG
3853 if (file_path.data[file_path.size - 1] != '/') {
3854 ret = lttng_dynamic_buffer_append(&file_path, "/", 1);
3855 if (ret) {
3856 ret = -LTTNG_ERR_NOMEM;
3857 goto end;
3858 }
dcf266c0 3859 }
cce35f91 3860 file_path_root_len = file_path.size;
dcf266c0
JG
3861
3862 /* Search for *.lttng files */
9a2df626
MJ
3863 for (;;) {
3864 size_t file_name_len;
3865 struct dirent *result;
3866
3867 /*
3868 * When the end of the directory stream is reached, NULL
3869 * is returned and errno is kept unchanged. When an
3870 * error occurs, NULL is returned and errno is set
3871 * accordingly. To distinguish between the two, set
3872 * errno to zero before calling readdir().
3873 *
3874 * On success, readdir() returns a pointer to a dirent
3875 * structure. This structure may be statically
3876 * allocated, do not attempt to free(3) it.
3877 */
3878 errno = 0;
3879 result = readdir(directory);
3880
ff86d8d0 3881 /* Reached end of dir stream or error out. */
9a2df626
MJ
3882 if (!result) {
3883 if (errno) {
3884 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path);
3885 ret = -LTTNG_ERR_LOAD_IO_FAIL;
3886 goto end;
3887 }
3888 break;
3889 }
3890
3891 file_name_len = strlen(result->d_name);
dcf266c0
JG
3892
3893 if (file_name_len <=
3894 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
3895 continue;
3896 }
3897
cce35f91
JG
3898 if (file_path.size + file_name_len >= LTTNG_PATH_MAX) {
3899 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3900 result->d_name,
3901 /* +1 to account for NULL terminator. */
3902 file_path.size + file_name_len + 1,
3903 LTTNG_PATH_MAX);
dcf266c0
JG
3904 continue;
3905 }
3906
cce35f91 3907 /* Does the file end with .lttng? */
dcf266c0 3908 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
cce35f91
JG
3909 result->d_name + file_name_len - sizeof(
3910 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
dcf266c0
JG
3911 continue;
3912 }
3913
cce35f91
JG
3914 ret = lttng_dynamic_buffer_append(&file_path, result->d_name,
3915 file_name_len + 1);
3916 if (ret) {
3917 ret = -LTTNG_ERR_NOMEM;
3918 goto end;
3919 }
dcf266c0 3920
cce35f91 3921 ret = load_session_from_file(file_path.data, session_name,
1b08cbce 3922 validation_ctx, overwrite, overrides);
55c9e7ca
JR
3923 if (session_name &&
3924 (!ret || ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
dcf266c0
JG
3925 session_found = 1;
3926 break;
3927 }
55c9e7ca
JR
3928 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
3929 goto end;
3930 }
cce35f91
JG
3931 /*
3932 * Reset the buffer's size to the location of the
3933 * path's trailing '/'.
3934 */
3935 ret = lttng_dynamic_buffer_set_size(&file_path,
3936 file_path_root_len);
3937 if (ret) {
3938 ret = -LTTNG_ERR_UNK;
3939 goto end;
3940 }
dcf266c0 3941 }
dcf266c0
JG
3942 } else {
3943 ret = load_session_from_file(path, session_name,
1b08cbce 3944 validation_ctx, overwrite, overrides);
dcf266c0
JG
3945 if (ret) {
3946 goto end;
dcf266c0 3947 }
55c9e7ca 3948 session_found = 1;
dcf266c0
JG
3949 }
3950
55c9e7ca 3951 ret = 0;
dcf266c0
JG
3952end:
3953 if (directory) {
3954 if (closedir(directory)) {
3955 PERROR("closedir");
3956 }
3957 }
55c9e7ca
JR
3958 if (!ret && !session_found) {
3959 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0 3960 }
cce35f91 3961 lttng_dynamic_buffer_reset(&file_path);
dcf266c0
JG
3962 return ret;
3963}
3964
ab38c13f
DG
3965/*
3966 * Validate that the given path's credentials and the current process have the
cf53c06d 3967 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
3968 */
3969static int validate_path_creds(const char *path)
3970{
3971 int ret, uid = getuid();
3972 struct stat buf;
3973
3974 assert(path);
3975
cf53c06d 3976 if (uid == 0) {
ab38c13f
DG
3977 goto valid;
3978 }
3979
3980 ret = stat(path, &buf);
3981 if (ret < 0) {
3982 if (errno != ENOENT) {
3983 PERROR("stat");
3984 }
ab38c13f
DG
3985 goto valid;
3986 }
3987
3988 if (buf.st_uid != uid) {
3989 goto invalid;
3990 }
3991
3992valid:
ab38c13f 3993 return 1;
cf53c06d
DG
3994invalid:
3995 return 0;
ab38c13f
DG
3996}
3997
dcf266c0
JG
3998LTTNG_HIDDEN
3999int config_load_session(const char *path, const char *session_name,
1b08cbce
JR
4000 int overwrite, unsigned int autoload,
4001 const struct config_load_session_override_attr *overrides)
dcf266c0
JG
4002{
4003 int ret;
6c66fa0f 4004 bool session_loaded = false;
cf53c06d 4005 const char *path_ptr = NULL;
dcf266c0
JG
4006 struct session_config_validation_ctx validation_ctx = { 0 };
4007
4008 ret = init_session_config_validation_ctx(&validation_ctx);
4009 if (ret) {
4010 goto end;
4011 }
4012
4013 if (!path) {
4f00620d 4014 const char *home_path;
ab38c13f
DG
4015 const char *sys_path;
4016
dcf266c0 4017 /* Try home path */
ab38c13f 4018 home_path = utils_get_home_dir();
dcf266c0 4019 if (home_path) {
ab38c13f 4020 char path[PATH_MAX];
dcf266c0 4021
d4fcf703
DG
4022 /*
4023 * Try user session configuration path. Ignore error here so we can
4024 * continue loading the system wide sessions.
4025 */
ab38c13f
DG
4026 if (autoload) {
4027 ret = snprintf(path, sizeof(path),
4028 DEFAULT_SESSION_HOME_CONFIGPATH "/"
4029 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
4030 if (ret < 0) {
4031 PERROR("snprintf session autoload home config path");
55c9e7ca 4032 ret = -LTTNG_ERR_INVALID;
cf53c06d
DG
4033 goto end;
4034 }
4035
4036 /*
4037 * Credentials are only validated for the autoload in order to
4038 * avoid any user session daemon to try to load kernel sessions
4039 * automatically and failing all the times.
4040 */
4041 ret = validate_path_creds(path);
4042 if (ret) {
4043 path_ptr = path;
4044 }
ab38c13f
DG
4045 } else {
4046 ret = snprintf(path, sizeof(path),
4047 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
4048 if (ret < 0) {
4049 PERROR("snprintf session home config path");
55c9e7ca 4050 ret = -LTTNG_ERR_INVALID;
cf53c06d
DG
4051 goto end;
4052 }
4053 path_ptr = path;
ab38c13f 4054 }
cf53c06d
DG
4055 if (path_ptr) {
4056 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 4057 &validation_ctx, overwrite, overrides);
d4fcf703
DG
4058 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
4059 goto end;
4060 }
4061 /*
4062 * Continue even if the session was found since we have to try
4063 * the system wide sessions.
4064 */
6c66fa0f 4065 session_loaded = true;
ab38c13f 4066 }
d4fcf703 4067 }
ab38c13f 4068
cf53c06d
DG
4069 /* Reset path pointer for the system wide dir. */
4070 path_ptr = NULL;
4071
d4fcf703
DG
4072 /* Try system wide configuration directory. */
4073 if (autoload) {
4074 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
4075 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
4076 ret = validate_path_creds(sys_path);
4077 if (ret) {
4078 path_ptr = sys_path;
4079 }
d4fcf703 4080 } else {
cf53c06d
DG
4081 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
4082 path_ptr = sys_path;
d4fcf703
DG
4083 }
4084
cf53c06d
DG
4085 if (path_ptr) {
4086 ret = load_session_from_path(path_ptr, session_name,
1b08cbce 4087 &validation_ctx, overwrite, overrides);
6c66fa0f
JG
4088 if (!ret) {
4089 session_loaded = true;
4090 }
55c9e7ca
JR
4091 } else {
4092 ret = 0;
dcf266c0
JG
4093 }
4094 } else {
4095 ret = access(path, F_OK);
4096 if (ret < 0) {
4097 PERROR("access");
4098 switch (errno) {
4099 case ENOENT:
4100 ret = -LTTNG_ERR_INVALID;
4101 WARN("Session configuration path does not exist.");
4102 break;
4103 case EACCES:
4104 ret = -LTTNG_ERR_EPERM;
4105 break;
4106 default:
4107 ret = -LTTNG_ERR_UNK;
4108 break;
4109 }
4110 goto end;
4111 }
4112
4113 ret = load_session_from_path(path, session_name,
1b08cbce 4114 &validation_ctx, overwrite, overrides);
dcf266c0
JG
4115 }
4116end:
4117 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
4118 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
4119 /*
4120 * Don't report an error if no sessions are found when called
4121 * without a session_name or a search path.
4122 */
4123 ret = 0;
4124 }
6c66fa0f
JG
4125
4126 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
4127 /* A matching session was found in one of the search paths. */
4128 ret = 0;
4129 }
dcf266c0
JG
4130 return ret;
4131}
56766c75
JG
4132
4133static
4134void __attribute__((destructor)) session_config_exit(void)
4135{
4136 xmlCleanupParser();
4137}
This page took 0.23133 seconds and 4 git commands to generate.