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