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