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