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