Refactoring: use an opaque lttng_tracker_id type
[lttng-tools.git] / src / common / config / session-config.c
1 /*
2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <dirent.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdbool.h>
30
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <common/dynamic-buffer.h>
36 #include <common/compat/getenv.h>
37 #include <lttng/lttng-error.h>
38 #include <libxml/parser.h>
39 #include <libxml/valid.h>
40 #include <libxml/xmlschemas.h>
41 #include <libxml/tree.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
44 #include <lttng/rotation.h>
45 #include <lttng/userspace-probe.h>
46
47 #include "session-config.h"
48 #include "config-internal.h"
49
50 #define CONFIG_USERSPACE_PROBE_LOOKUP_METHOD_NAME_MAX_LEN 7
51
52 struct handler_filter_args {
53 const char* section;
54 config_entry_handler_cb handler;
55 void *user_data;
56 };
57
58 struct session_config_validation_ctx {
59 xmlSchemaParserCtxtPtr parser_ctx;
60 xmlSchemaPtr schema;
61 xmlSchemaValidCtxtPtr schema_validation_ctx;
62 };
63
64 const char * const config_element_all = "all";
65 const char * const config_str_yes = "yes";
66 const char * const config_str_true = "true";
67 const char * const config_str_on = "on";
68 const char * const config_str_no = "no";
69 const char * const config_str_false = "false";
70 const char * const config_str_off = "off";
71 const char * const config_xml_encoding = "UTF-8";
72 const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */
73 const char * const config_xml_indent_string = "\t";
74 const char * const config_xml_true = "true";
75 const char * const config_xml_false = "false";
76
77 const char * const config_element_channel = "channel";
78 const char * const config_element_channels = "channels";
79 const char * const config_element_domain = "domain";
80 const char * const config_element_domains = "domains";
81 const char * const config_element_event = "event";
82 const char * const config_element_events = "events";
83 const char * const config_element_context = "context";
84 const char * const config_element_contexts = "contexts";
85 const char * const config_element_attributes = "attributes";
86 const char * const config_element_exclusion = "exclusion";
87 const char * const config_element_exclusions = "exclusions";
88 const char * const config_element_function_attributes = "function_attributes";
89 const char * const config_element_probe_attributes = "probe_attributes";
90 const char * const config_element_symbol_name = "symbol_name";
91 const char * const config_element_address = "address";
92 const char * const config_element_offset = "offset";
93
94 LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup = "lookup_method";
95 LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_function_default = "DEFAULT";
96 LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_function_elf = "ELF";
97 LTTNG_HIDDEN const char * const config_element_userspace_probe_lookup_tracepoint_sdt = "SDT";
98 LTTNG_HIDDEN const char * const config_element_userspace_probe_location_binary_path = "binary_path";
99 LTTNG_HIDDEN const char * const config_element_userspace_probe_function_attributes = "userspace_probe_function_attributes";
100 LTTNG_HIDDEN const char * const config_element_userspace_probe_function_location_function_name = "function_name";
101 LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_attributes = "userspace_probe_tracepoint_attributes";
102 LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_location_provider_name = "provider_name";
103 LTTNG_HIDDEN const char * const config_element_userspace_probe_tracepoint_location_probe_name = "probe_name";
104
105 const char * const config_element_name = "name";
106 const char * const config_element_enabled = "enabled";
107 const char * const config_element_overwrite_mode = "overwrite_mode";
108 const char * const config_element_subbuf_size = "subbuffer_size";
109 const char * const config_element_num_subbuf = "subbuffer_count";
110 const char * const config_element_switch_timer_interval = "switch_timer_interval";
111 const char * const config_element_read_timer_interval = "read_timer_interval";
112 LTTNG_HIDDEN const char * const config_element_monitor_timer_interval = "monitor_timer_interval";
113 LTTNG_HIDDEN const char * const config_element_blocking_timeout = "blocking_timeout";
114 const char * const config_element_output = "output";
115 const char * const config_element_output_type = "output_type";
116 const char * const config_element_tracefile_size = "tracefile_size";
117 const char * const config_element_tracefile_count = "tracefile_count";
118 const char * const config_element_live_timer_interval = "live_timer_interval";
119 LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events";
120 LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets";
121 const char * const config_element_type = "type";
122 const char * const config_element_buffer_type = "buffer_type";
123 const char * const config_element_session = "session";
124 const char * const config_element_sessions = "sessions";
125 LTTNG_HIDDEN const char * const config_element_context_perf = "perf";
126 LTTNG_HIDDEN const char * const config_element_context_app = "app";
127 LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name";
128 LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name";
129 const char * const config_element_config = "config";
130 const char * const config_element_started = "started";
131 const char * const config_element_snapshot_mode = "snapshot_mode";
132 const char * const config_element_loglevel = "loglevel";
133 const char * const config_element_loglevel_type = "loglevel_type";
134 const char * const config_element_filter = "filter";
135 LTTNG_HIDDEN const char * const config_element_filter_expression = "filter_expression";
136 const char * const config_element_snapshot_outputs = "snapshot_outputs";
137 const char * const config_element_consumer_output = "consumer_output";
138 const char * const config_element_destination = "destination";
139 const char * const config_element_path = "path";
140 const char * const config_element_net_output = "net_output";
141 const char * const config_element_control_uri = "control_uri";
142 const char * const config_element_data_uri = "data_uri";
143 const char * const config_element_max_size = "max_size";
144 const char * const config_element_pid = "pid";
145 const char * const config_element_id = "id";
146 const char * const config_element_pids = "pids";
147 const char * const config_element_shared_memory_path = "shared_memory_path";
148 const char * const config_element_pid_tracker = "pid_tracker";
149 const char * const config_element_vpid_tracker = "vpid_tracker";
150 const char * const config_element_uid_tracker = "uid_tracker";
151 const char * const config_element_vuid_tracker = "vuid_tracker";
152 const char * const config_element_gid_tracker = "gid_tracker";
153 const char * const config_element_vgid_tracker = "vgid_tracker";
154 const char * const config_element_trackers = "trackers";
155 const char * const config_element_targets = "targets";
156 const char * const config_element_target_type = "target_type";
157 const char * const config_element_target_pid = "pid_target";
158 const char * const config_element_target_vpid = "vpid_target";
159 const char * const config_element_target_uid = "uid_target";
160 const char * const config_element_target_vuid = "vuid_target";
161 const char * const config_element_target_gid = "gid_target";
162 const char * const config_element_target_vgid = "vgid_target";
163 const char * const config_element_tracker_type = "tracker_type";
164
165 LTTNG_HIDDEN const char * const config_element_rotation_schedules = "rotation_schedules";
166 LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic = "periodic";
167 LTTNG_HIDDEN const char * const config_element_rotation_schedule_periodic_time_us = "time_us";
168 LTTNG_HIDDEN const char * const config_element_rotation_schedule_size_threshold = "size_threshold";
169 LTTNG_HIDDEN const char * const config_element_rotation_schedule_size_threshold_bytes = "bytes";
170
171 const char * const config_domain_type_kernel = "KERNEL";
172 const char * const config_domain_type_ust = "UST";
173 const char * const config_domain_type_jul = "JUL";
174 const char * const config_domain_type_log4j = "LOG4J";
175 const char * const config_domain_type_python = "PYTHON";
176
177 const char * const config_buffer_type_per_pid = "PER_PID";
178 const char * const config_buffer_type_per_uid = "PER_UID";
179 const char * const config_buffer_type_global = "GLOBAL";
180
181 const char * const config_overwrite_mode_discard = "DISCARD";
182 const char * const config_overwrite_mode_overwrite = "OVERWRITE";
183
184 const char * const config_output_type_splice = "SPLICE";
185 const char * const config_output_type_mmap = "MMAP";
186
187 const char * const config_loglevel_type_all = "ALL";
188 const char * const config_loglevel_type_range = "RANGE";
189 const char * const config_loglevel_type_single = "SINGLE";
190
191 const char * const config_event_type_all = "ALL";
192 const char * const config_event_type_tracepoint = "TRACEPOINT";
193 const char * const config_event_type_probe = "PROBE";
194 LTTNG_HIDDEN const char * const config_event_type_userspace_probe = "USERSPACE_PROBE";
195 const char * const config_event_type_function = "FUNCTION";
196 const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
197 const char * const config_event_type_noop = "NOOP";
198 const char * const config_event_type_syscall = "SYSCALL";
199 const char * const config_event_type_kprobe = "KPROBE";
200 const char * const config_event_type_kretprobe = "KRETPROBE";
201
202 const char * const config_event_context_pid = "PID";
203 const char * const config_event_context_procname = "PROCNAME";
204 const char * const config_event_context_prio = "PRIO";
205 const char * const config_event_context_nice = "NICE";
206 const char * const config_event_context_vpid = "VPID";
207 const char * const config_event_context_tid = "TID";
208 const char * const config_event_context_vtid = "VTID";
209 const char * const config_event_context_ppid = "PPID";
210 const char * const config_event_context_vppid = "VPPID";
211 const char * const config_event_context_pthread_id = "PTHREAD_ID";
212 const char * const config_event_context_hostname = "HOSTNAME";
213 const char * const config_event_context_ip = "IP";
214 const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER";
215 LTTNG_HIDDEN const char * const config_event_context_app = "APP";
216 LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE";
217 LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE";
218 LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE";
219 LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE";
220 LTTNG_HIDDEN const char * const config_event_context_callstack_user= "CALLSTACK_USER";
221 LTTNG_HIDDEN const char * const config_event_context_callstack_kernel = "CALLSTACK_KERNEL";
222 LTTNG_HIDDEN const char * const config_event_context_cgroup_ns = "CGROUP_NS";
223 LTTNG_HIDDEN const char * const config_event_context_ipc_ns = "IPC_NS";
224 LTTNG_HIDDEN const char * const config_event_context_mnt_ns = "MNT_NS";
225 LTTNG_HIDDEN const char * const config_event_context_net_ns = "NET_NS";
226 LTTNG_HIDDEN const char * const config_event_context_pid_ns = "PID_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()
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_user_ns)) {
1100 ret = LTTNG_EVENT_CONTEXT_USER_NS;
1101 } else if (!strcmp((char *) context_type,
1102 config_event_context_uts_ns)) {
1103 ret = LTTNG_EVENT_CONTEXT_UTS_NS;
1104 } else if (!strcmp((char *) context_type,
1105 config_event_context_uid)) {
1106 ret = LTTNG_EVENT_CONTEXT_UID;
1107 } else if (!strcmp((char *) context_type,
1108 config_event_context_euid)) {
1109 ret = LTTNG_EVENT_CONTEXT_EUID;
1110 } else if (!strcmp((char *) context_type,
1111 config_event_context_suid)) {
1112 ret = LTTNG_EVENT_CONTEXT_SUID;
1113 } else if (!strcmp((char *) context_type,
1114 config_event_context_gid)) {
1115 ret = LTTNG_EVENT_CONTEXT_GID;
1116 } else if (!strcmp((char *) context_type,
1117 config_event_context_egid)) {
1118 ret = LTTNG_EVENT_CONTEXT_EGID;
1119 } else if (!strcmp((char *) context_type,
1120 config_event_context_sgid)) {
1121 ret = LTTNG_EVENT_CONTEXT_SGID;
1122 } else if (!strcmp((char *) context_type,
1123 config_event_context_vuid)) {
1124 ret = LTTNG_EVENT_CONTEXT_VUID;
1125 } else if (!strcmp((char *) context_type,
1126 config_event_context_veuid)) {
1127 ret = LTTNG_EVENT_CONTEXT_VEUID;
1128 } else if (!strcmp((char *) context_type,
1129 config_event_context_vsuid)) {
1130 ret = LTTNG_EVENT_CONTEXT_VSUID;
1131 } else if (!strcmp((char *) context_type,
1132 config_event_context_vgid)) {
1133 ret = LTTNG_EVENT_CONTEXT_VGID;
1134 } else if (!strcmp((char *) context_type,
1135 config_event_context_vegid)) {
1136 ret = LTTNG_EVENT_CONTEXT_VEGID;
1137 } else if (!strcmp((char *) context_type,
1138 config_event_context_vsgid)) {
1139 ret = LTTNG_EVENT_CONTEXT_VSGID;
1140 } else {
1141 goto error;
1142 }
1143
1144 return ret;
1145 error:
1146 return -1;
1147 }
1148
1149 static
1150 int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain)
1151 {
1152 int ret;
1153 xmlNodePtr node;
1154
1155 for (node = xmlFirstElementChild(domain_node); node;
1156 node = xmlNextElementSibling(node)) {
1157 if (!strcmp((const char *) node->name, config_element_type)) {
1158 /* domain type */
1159 xmlChar *node_content = xmlNodeGetContent(node);
1160 if (!node_content) {
1161 ret = -LTTNG_ERR_NOMEM;
1162 goto end;
1163 }
1164
1165 ret = get_domain_type(node_content);
1166 free(node_content);
1167 if (ret < 0) {
1168 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1169 goto end;
1170 }
1171
1172 domain->type = ret;
1173 } else if (!strcmp((const char *) node->name,
1174 config_element_buffer_type)) {
1175 /* buffer type */
1176 xmlChar *node_content = xmlNodeGetContent(node);
1177 if (!node_content) {
1178 ret = -LTTNG_ERR_NOMEM;
1179 goto end;
1180 }
1181
1182 ret = get_buffer_type(node_content);
1183 free(node_content);
1184 if (ret < 0) {
1185 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1186 goto end;
1187 }
1188
1189 domain->buf_type = ret;
1190 }
1191 }
1192 ret = 0;
1193 end:
1194 return ret;
1195 }
1196
1197 static
1198 int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri,
1199 char **data_uri)
1200 {
1201 xmlNodePtr node;
1202
1203 for (node = xmlFirstElementChild(net_output_node); node;
1204 node = xmlNextElementSibling(node)) {
1205 if (!strcmp((const char *) node->name, config_element_control_uri)) {
1206 /* control_uri */
1207 *control_uri = (char *) xmlNodeGetContent(node);
1208 if (!*control_uri) {
1209 break;
1210 }
1211 } else {
1212 /* data_uri */
1213 *data_uri = (char *) xmlNodeGetContent(node);
1214 if (!*data_uri) {
1215 break;
1216 }
1217 }
1218 }
1219
1220 return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG;
1221 }
1222
1223 static
1224 int process_consumer_output(xmlNodePtr consumer_output_node,
1225 struct consumer_output *output)
1226 {
1227 int ret;
1228 xmlNodePtr node;
1229
1230 assert(output);
1231
1232 for (node = xmlFirstElementChild(consumer_output_node); node;
1233 node = xmlNextElementSibling(node)) {
1234 if (!strcmp((const char *) node->name, config_element_enabled)) {
1235 xmlChar *enabled_str = xmlNodeGetContent(node);
1236
1237 /* enabled */
1238 if (!enabled_str) {
1239 ret = -LTTNG_ERR_NOMEM;
1240 goto end;
1241 }
1242
1243 ret = parse_bool(enabled_str, &output->enabled);
1244 free(enabled_str);
1245 if (ret) {
1246 goto end;
1247 }
1248 } else {
1249 xmlNodePtr output_type_node;
1250
1251 /* destination */
1252 output_type_node = xmlFirstElementChild(node);
1253 if (!output_type_node) {
1254 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1255 goto end;
1256 }
1257
1258 if (!strcmp((const char *) output_type_node->name,
1259 config_element_path)) {
1260 /* path */
1261 output->path = (char *) xmlNodeGetContent(output_type_node);
1262 if (!output->path) {
1263 ret = -LTTNG_ERR_NOMEM;
1264 goto end;
1265 }
1266 } else {
1267 /* net_output */
1268 ret = get_net_output_uris(output_type_node,
1269 &output->control_uri, &output->data_uri);
1270 if (ret) {
1271 goto end;
1272 }
1273 }
1274 }
1275 }
1276 ret = 0;
1277
1278 end:
1279 if (ret) {
1280 free(output->path);
1281 free(output->control_uri);
1282 free(output->data_uri);
1283 memset(output, 0, sizeof(struct consumer_output));
1284 }
1285 return ret;
1286 }
1287
1288 static
1289 int create_session_net_output(const char *name, const char *control_uri,
1290 const char *data_uri)
1291 {
1292 int ret;
1293 struct lttng_handle *handle;
1294 const char *uri = NULL;
1295
1296 assert(name);
1297
1298 handle = lttng_create_handle(name, NULL);
1299 if (!handle) {
1300 ret = -LTTNG_ERR_NOMEM;
1301 goto end;
1302 }
1303
1304 if (!control_uri || !data_uri) {
1305 uri = control_uri ? control_uri : data_uri;
1306 control_uri = uri;
1307 data_uri = uri;
1308 }
1309
1310 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1311 lttng_destroy_handle(handle);
1312 end:
1313 return ret;
1314 }
1315
1316 static
1317 int create_snapshot_session(const char *session_name, xmlNodePtr output_node,
1318 const struct config_load_session_override_attr *overrides)
1319 {
1320 int ret;
1321 xmlNodePtr node = NULL;
1322 xmlNodePtr snapshot_output_list_node;
1323 xmlNodePtr snapshot_output_node;
1324
1325 assert(session_name);
1326
1327 ret = lttng_create_session_snapshot(session_name, NULL);
1328 if (ret) {
1329 goto end;
1330 }
1331
1332 if (!output_node) {
1333 goto end;
1334 }
1335
1336 snapshot_output_list_node = xmlFirstElementChild(output_node);
1337
1338 /* Parse and create snapshot outputs */
1339
1340 for (snapshot_output_node =
1341 xmlFirstElementChild(snapshot_output_list_node);
1342 snapshot_output_node; snapshot_output_node =
1343 xmlNextElementSibling(snapshot_output_node)) {
1344 char *name = NULL;
1345 uint64_t max_size = UINT64_MAX;
1346 struct consumer_output output = { 0 };
1347 struct lttng_snapshot_output *snapshot_output = NULL;
1348 const char *control_uri = NULL;
1349 const char *data_uri = NULL;
1350 const char *path = NULL;
1351
1352 for (node = xmlFirstElementChild(snapshot_output_node); node;
1353 node = xmlNextElementSibling(node)) {
1354 if (!strcmp((const char *) node->name,
1355 config_element_name)) {
1356 /* name */
1357 name = (char *) xmlNodeGetContent(node);
1358 if (!name) {
1359 ret = -LTTNG_ERR_NOMEM;
1360 goto error_snapshot_output;
1361 }
1362 } else if (!strcmp((const char *) node->name,
1363 config_element_max_size)) {
1364 xmlChar *content = xmlNodeGetContent(node);
1365
1366 /* max_size */
1367 if (!content) {
1368 ret = -LTTNG_ERR_NOMEM;
1369 goto error_snapshot_output;
1370 }
1371 ret = parse_uint(content, &max_size);
1372 free(content);
1373 if (ret) {
1374 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1375 goto error_snapshot_output;
1376 }
1377 } else {
1378 /* consumer_output */
1379 ret = process_consumer_output(node, &output);
1380 if (ret) {
1381 goto error_snapshot_output;
1382 }
1383 }
1384 }
1385
1386 control_uri = output.control_uri;
1387 data_uri = output.data_uri;
1388 path = output.path;
1389
1390 if (overrides) {
1391 if (overrides->path_url) {
1392 path = overrides->path_url;
1393 /* Control/data_uri are null */
1394 control_uri = NULL;
1395 data_uri = NULL;
1396 } else {
1397 if (overrides->ctrl_url) {
1398 control_uri = overrides->ctrl_url;
1399 /* path is null */
1400 path = NULL;
1401 }
1402 if (overrides->data_url) {
1403 data_uri = overrides->data_url;
1404 /* path is null */
1405 path = NULL;
1406 }
1407 }
1408 }
1409
1410 snapshot_output = lttng_snapshot_output_create();
1411 if (!snapshot_output) {
1412 ret = -LTTNG_ERR_NOMEM;
1413 goto error_snapshot_output;
1414 }
1415
1416 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1417 if (ret) {
1418 goto error_snapshot_output;
1419 }
1420
1421 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1422 if (ret) {
1423 goto error_snapshot_output;
1424 }
1425
1426 if (path) {
1427 ret = lttng_snapshot_output_set_ctrl_url(path,
1428 snapshot_output);
1429 if (ret) {
1430 goto error_snapshot_output;
1431 }
1432 } else {
1433 if (control_uri) {
1434 ret = lttng_snapshot_output_set_ctrl_url(control_uri,
1435 snapshot_output);
1436 if (ret) {
1437 goto error_snapshot_output;
1438 }
1439 }
1440
1441 if (data_uri) {
1442 ret = lttng_snapshot_output_set_data_url(data_uri,
1443 snapshot_output);
1444 if (ret) {
1445 goto error_snapshot_output;
1446 }
1447 }
1448 }
1449
1450 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1451 error_snapshot_output:
1452 free(name);
1453 free(output.path);
1454 free(output.control_uri);
1455 free(output.data_uri);
1456 lttng_snapshot_output_destroy(snapshot_output);
1457 if (ret) {
1458 goto end;
1459 }
1460 }
1461 end:
1462 return ret;
1463 }
1464
1465 static
1466 int create_session(const char *name,
1467 xmlNodePtr output_node,
1468 uint64_t live_timer_interval,
1469 const struct config_load_session_override_attr *overrides)
1470 {
1471 int ret;
1472 struct consumer_output output = { 0 };
1473 xmlNodePtr consumer_output_node;
1474 const char *control_uri = NULL;
1475 const char *data_uri = NULL;
1476 const char *path = NULL;
1477
1478 assert(name);
1479
1480 if (output_node) {
1481 consumer_output_node = xmlFirstElementChild(output_node);
1482 if (!consumer_output_node) {
1483 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1484 goto end;
1485 }
1486
1487 if (strcmp((const char *) consumer_output_node->name,
1488 config_element_consumer_output)) {
1489 WARN("Invalid output type, expected %s node",
1490 config_element_consumer_output);
1491 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1492 goto end;
1493 }
1494
1495 ret = process_consumer_output(consumer_output_node, &output);
1496 if (ret) {
1497 goto end;
1498 }
1499 }
1500
1501 control_uri = output.control_uri;
1502 data_uri = output.data_uri;
1503 path = output.path;
1504
1505 /* Check for override and apply them */
1506 if (overrides) {
1507 if (overrides->path_url) {
1508 path = overrides->path_url;
1509 /* control/data_uri are null */;
1510 control_uri = NULL;
1511 data_uri = NULL;
1512 } else {
1513 if (overrides->ctrl_url) {
1514 control_uri = overrides->ctrl_url;
1515 /* path is null */
1516 path = NULL;
1517 }
1518 if (overrides->data_url) {
1519 data_uri = overrides->data_url;
1520 /* path is null */
1521 path = NULL;
1522 }
1523 }
1524 }
1525
1526
1527 if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) {
1528 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1529 goto end;
1530 }
1531
1532 if (control_uri || data_uri) {
1533 /* network destination */
1534 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
1535 /*
1536 * URLs are provided for sure since the test above make sure that
1537 * with a live timer the data and control URIs are provided. So,
1538 * NULL is passed here and will be set right after.
1539 */
1540 ret = lttng_create_session_live(name, NULL, live_timer_interval);
1541 } else {
1542 ret = lttng_create_session(name, NULL);
1543 }
1544 if (ret) {
1545 goto end;
1546 }
1547
1548 ret = create_session_net_output(name, control_uri, data_uri);
1549 if (ret) {
1550 goto end;
1551 }
1552
1553 } else {
1554 /* either local output or no output */
1555 ret = lttng_create_session(name, path);
1556 if (ret) {
1557 goto end;
1558 }
1559 }
1560 end:
1561 free(output.path);
1562 free(output.control_uri);
1563 free(output.data_uri);
1564 return ret;
1565 }
1566
1567 static
1568 struct lttng_userspace_probe_location *
1569 process_userspace_probe_function_attribute_node(
1570 xmlNodePtr attribute_node)
1571 {
1572 xmlNodePtr function_attribute_node;
1573 char *function_name = NULL, *binary_path = NULL;
1574 struct lttng_userspace_probe_location *location = NULL;
1575 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
1576
1577 /*
1578 * Process userspace probe location function attributes. The order of
1579 * the fields are not guaranteed so we need to iterate over all fields
1580 * and check at the end if everything we need for this location type is
1581 * there.
1582 */
1583 for (function_attribute_node =
1584 xmlFirstElementChild(attribute_node);
1585 function_attribute_node;
1586 function_attribute_node = xmlNextElementSibling(
1587 function_attribute_node)) {
1588 /* Handle function name, binary path and lookup method. */
1589 if (!strcmp((const char *) function_attribute_node->name,
1590 config_element_userspace_probe_function_location_function_name)) {
1591 function_name = (char *) xmlNodeGetContent(function_attribute_node);
1592 if (!function_name) {
1593 goto error;
1594 }
1595 } else if (!strcmp((const char *) function_attribute_node->name,
1596 config_element_userspace_probe_location_binary_path)) {
1597 binary_path = (char *) xmlNodeGetContent(function_attribute_node);
1598 if (!binary_path) {
1599 goto error;
1600 }
1601 } else if (!strcmp((const char *) function_attribute_node->name,
1602 config_element_userspace_probe_lookup)) {
1603 char *lookup_method_name;
1604
1605 lookup_method_name = (char *) xmlNodeGetContent(
1606 function_attribute_node);
1607 if (!lookup_method_name) {
1608 goto error;
1609 }
1610
1611 /*
1612 * function_default lookup method defaults to
1613 * function_elf lookup method at the moment.
1614 */
1615 if (!strcmp(lookup_method_name, config_element_userspace_probe_lookup_function_elf)
1616 || !strcmp(lookup_method_name, config_element_userspace_probe_lookup_function_default)) {
1617 lookup_method = lttng_userspace_probe_location_lookup_method_function_elf_create();
1618 if (!lookup_method) {
1619 PERROR("Error creating function default/ELF lookup method");
1620 }
1621 } else {
1622 WARN("Unknown function lookup method");
1623 }
1624
1625 free(lookup_method_name);
1626 if (!lookup_method) {
1627 goto error;
1628 }
1629 } else {
1630 goto error;
1631 }
1632
1633 /* Check if all the necessary fields were found. */
1634 if (binary_path && function_name && lookup_method) {
1635 /* Ownership of lookup_method is transferred. */
1636 location =
1637 lttng_userspace_probe_location_function_create(
1638 binary_path, function_name,
1639 lookup_method);
1640 lookup_method = NULL;
1641 goto error;
1642 }
1643 }
1644 error:
1645 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
1646 free(binary_path);
1647 free(function_name);
1648 return location;
1649 }
1650
1651 static
1652 struct lttng_userspace_probe_location *
1653 process_userspace_probe_tracepoint_attribute_node(
1654 xmlNodePtr attribute_node)
1655 {
1656 xmlNodePtr tracepoint_attribute_node;
1657 char *probe_name = NULL, *provider_name = NULL, *binary_path = NULL;
1658 struct lttng_userspace_probe_location *location = NULL;
1659 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
1660
1661 /*
1662 * Process userspace probe location tracepoint attributes. The order of
1663 * the fields are not guaranteed so we need to iterate over all fields
1664 * and check at the end if everything we need for this location type is
1665 * there.
1666 */
1667 for (tracepoint_attribute_node =
1668 xmlFirstElementChild(attribute_node); tracepoint_attribute_node;
1669 tracepoint_attribute_node = xmlNextElementSibling(
1670 tracepoint_attribute_node)) {
1671 if (!strcmp((const char *) tracepoint_attribute_node->name,
1672 config_element_userspace_probe_tracepoint_location_probe_name)) {
1673 probe_name = (char *) xmlNodeGetContent(tracepoint_attribute_node);
1674 if (!probe_name) {
1675 goto error;
1676 }
1677 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1678 config_element_userspace_probe_tracepoint_location_provider_name)) {
1679 provider_name = (char *) xmlNodeGetContent(tracepoint_attribute_node);
1680 if (!provider_name) {
1681 goto error;
1682 }
1683 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1684 config_element_userspace_probe_location_binary_path)) {
1685 binary_path = (char *) xmlNodeGetContent(tracepoint_attribute_node);
1686 if (!binary_path) {
1687 goto error;
1688 }
1689 } else if (!strcmp((const char *) tracepoint_attribute_node->name,
1690 config_element_userspace_probe_lookup)) {
1691 char *lookup_method_name;
1692
1693 lookup_method_name = (char *) xmlNodeGetContent(
1694 tracepoint_attribute_node);
1695 if (!lookup_method_name) {
1696 goto error;
1697 }
1698
1699 if (!strcmp(lookup_method_name,
1700 config_element_userspace_probe_lookup_tracepoint_sdt)) {
1701 lookup_method =
1702 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1703 if (!lookup_method) {
1704 PERROR("Error creating tracepoint SDT lookup method");
1705 }
1706 } else {
1707 WARN("Unknown tracepoint lookup method");
1708 }
1709
1710 free(lookup_method_name);
1711 if (!lookup_method) {
1712 goto error;
1713 }
1714 } else {
1715 WARN("Unknown tracepoint attribute");
1716 goto error;
1717 }
1718
1719 /* Check if all the necessary fields were found. */
1720 if (binary_path && provider_name && probe_name && lookup_method) {
1721 /* Ownership of lookup_method is transferred. */
1722 location =
1723 lttng_userspace_probe_location_tracepoint_create(
1724 binary_path, provider_name,
1725 probe_name, lookup_method);
1726 lookup_method = NULL;
1727 goto error;
1728 }
1729 }
1730 error:
1731 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
1732 free(binary_path);
1733 free(provider_name);
1734 free(probe_name);
1735 return location;
1736 }
1737
1738 static
1739 int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1740 struct lttng_event_probe_attr *attr)
1741 {
1742 int ret;
1743
1744 assert(probe_attribute_node);
1745 assert(attr);
1746
1747 if (!strcmp((const char *) probe_attribute_node->name,
1748 config_element_address)) {
1749 xmlChar *content;
1750 uint64_t addr = 0;
1751
1752 /* addr */
1753 content = xmlNodeGetContent(probe_attribute_node);
1754 if (!content) {
1755 ret = -LTTNG_ERR_NOMEM;
1756 goto end;
1757 }
1758
1759 ret = parse_uint(content, &addr);
1760 free(content);
1761 if (ret) {
1762 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1763 goto end;
1764 }
1765
1766 attr->addr = addr;
1767 } else if (!strcmp((const char *) probe_attribute_node->name,
1768 config_element_offset)) {
1769 xmlChar *content;
1770 uint64_t offset = 0;
1771
1772 /* offset */
1773 content = xmlNodeGetContent(probe_attribute_node);
1774 if (!content) {
1775 ret = -LTTNG_ERR_NOMEM;
1776 goto end;
1777 }
1778
1779 ret = parse_uint(content, &offset);
1780 free(content);
1781 if (ret) {
1782 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1783 goto end;
1784 }
1785
1786 attr->offset = offset;
1787 } else if (!strcmp((const char *) probe_attribute_node->name,
1788 config_element_symbol_name)) {
1789 xmlChar *content;
1790
1791 /* symbol_name */
1792 content = xmlNodeGetContent(probe_attribute_node);
1793 if (!content) {
1794 ret = -LTTNG_ERR_NOMEM;
1795 goto end;
1796 }
1797
1798 ret = lttng_strncpy(attr->symbol_name,
1799 (const char *) content,
1800 LTTNG_SYMBOL_NAME_LEN);
1801 if (ret == -1) {
1802 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1803 (const char *) content,
1804 strlen((const char *) content),
1805 LTTNG_SYMBOL_NAME_LEN);
1806 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1807 free(content);
1808 goto end;
1809 }
1810 free(content);
1811 }
1812 ret = 0;
1813 end:
1814 return ret;
1815 }
1816
1817 static
1818 int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
1819 const char *channel_name, const enum process_event_node_phase phase)
1820 {
1821 int ret = 0, i;
1822 xmlNodePtr node;
1823 struct lttng_event *event;
1824 char **exclusions = NULL;
1825 unsigned long exclusion_count = 0;
1826 char *filter_expression = NULL;
1827
1828 assert(event_node);
1829 assert(handle);
1830 assert(channel_name);
1831
1832 event = lttng_event_create();
1833 if (!event) {
1834 ret = -LTTNG_ERR_NOMEM;
1835 goto end;
1836 }
1837
1838 /* Initialize default log level which varies by domain */
1839 switch (handle->domain.type)
1840 {
1841 case LTTNG_DOMAIN_JUL:
1842 event->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1843 break;
1844 case LTTNG_DOMAIN_LOG4J:
1845 event->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1846 break;
1847 case LTTNG_DOMAIN_PYTHON:
1848 event->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1849 break;
1850 case LTTNG_DOMAIN_UST:
1851 case LTTNG_DOMAIN_KERNEL:
1852 event->loglevel = LTTNG_LOGLEVEL_DEBUG;
1853 break;
1854 default:
1855 assert(0);
1856 }
1857
1858 for (node = xmlFirstElementChild(event_node); node;
1859 node = xmlNextElementSibling(node)) {
1860 if (!strcmp((const char *) node->name, config_element_name)) {
1861 xmlChar *content;
1862
1863 /* name */
1864 content = xmlNodeGetContent(node);
1865 if (!content) {
1866 ret = -LTTNG_ERR_NOMEM;
1867 goto end;
1868 }
1869
1870 ret = lttng_strncpy(event->name,
1871 (const char *) content,
1872 LTTNG_SYMBOL_NAME_LEN);
1873 if (ret == -1) {
1874 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1875 (const char *) content,
1876 strlen((const char *) content),
1877 LTTNG_SYMBOL_NAME_LEN);
1878 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1879 free(content);
1880 goto end;
1881 }
1882 free(content);
1883 } else if (!strcmp((const char *) node->name,
1884 config_element_enabled)) {
1885 xmlChar *content = xmlNodeGetContent(node);
1886
1887 /* enabled */
1888 if (!content) {
1889 ret = -LTTNG_ERR_NOMEM;
1890 goto end;
1891 }
1892
1893 ret = parse_bool(content, &event->enabled);
1894 free(content);
1895 if (ret) {
1896 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1897 goto end;
1898 }
1899 } else if (!strcmp((const char *) node->name,
1900 config_element_type)) {
1901 xmlChar *content = xmlNodeGetContent(node);
1902
1903 /* type */
1904 if (!content) {
1905 ret = -LTTNG_ERR_NOMEM;
1906 goto end;
1907 }
1908
1909 ret = get_event_type(content);
1910 free(content);
1911 if (ret < 0) {
1912 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1913 goto end;
1914 }
1915
1916 event->type = ret;
1917 } else if (!strcmp((const char *) node->name,
1918 config_element_loglevel_type)) {
1919 xmlChar *content = xmlNodeGetContent(node);
1920
1921 /* loglevel_type */
1922 if (!content) {
1923 ret = -LTTNG_ERR_NOMEM;
1924 goto end;
1925 }
1926
1927 ret = get_loglevel_type(content);
1928 free(content);
1929 if (ret < 0) {
1930 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1931 goto end;
1932 }
1933
1934 event->loglevel_type = ret;
1935 } else if (!strcmp((const char *) node->name,
1936 config_element_loglevel)) {
1937 xmlChar *content;
1938 int64_t loglevel = 0;
1939
1940 /* loglevel */
1941 content = xmlNodeGetContent(node);
1942 if (!content) {
1943 ret = -LTTNG_ERR_NOMEM;
1944 goto end;
1945 }
1946
1947 ret = parse_int(content, &loglevel);
1948 free(content);
1949 if (ret) {
1950 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1951 goto end;
1952 }
1953
1954 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1955 WARN("loglevel out of range.");
1956 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1957 goto end;
1958 }
1959
1960 event->loglevel = loglevel;
1961 } else if (!strcmp((const char *) node->name,
1962 config_element_filter)) {
1963 xmlChar *content =
1964 xmlNodeGetContent(node);
1965
1966 /* filter */
1967 if (!content) {
1968 ret = -LTTNG_ERR_NOMEM;
1969 goto end;
1970 }
1971
1972 free(filter_expression);
1973 filter_expression = strdup((char *) content);
1974 free(content);
1975 if (!filter_expression) {
1976 ret = -LTTNG_ERR_NOMEM;
1977 goto end;
1978 }
1979 } else if (!strcmp((const char *) node->name,
1980 config_element_exclusions)) {
1981 xmlNodePtr exclusion_node;
1982 int exclusion_index = 0;
1983
1984 /* exclusions */
1985 if (exclusions) {
1986 /*
1987 * Exclusions has already been initialized,
1988 * invalid file.
1989 */
1990 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1991 goto end;
1992 }
1993
1994 exclusion_count = xmlChildElementCount(node);
1995 if (!exclusion_count) {
1996 continue;
1997 }
1998
1999 exclusions = zmalloc(exclusion_count * sizeof(char *));
2000 if (!exclusions) {
2001 exclusion_count = 0;
2002 ret = -LTTNG_ERR_NOMEM;
2003 goto end;
2004 }
2005
2006 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
2007 exclusion_node = xmlNextElementSibling(exclusion_node)) {
2008 xmlChar *content =
2009 xmlNodeGetContent(exclusion_node);
2010
2011 if (!content) {
2012 ret = -LTTNG_ERR_NOMEM;
2013 goto end;
2014 }
2015
2016 exclusions[exclusion_index] = strdup((const char *) content);
2017 free(content);
2018 if (!exclusions[exclusion_index]) {
2019 ret = -LTTNG_ERR_NOMEM;
2020 goto end;
2021 }
2022 exclusion_index++;
2023 }
2024
2025 event->exclusion = 1;
2026 } else if (!strcmp((const char *) node->name,
2027 config_element_attributes)) {
2028 xmlNodePtr attribute_node = xmlFirstElementChild(node);
2029
2030 /* attributes */
2031 if (!attribute_node) {
2032 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2033 goto end;
2034 }
2035
2036 if (!strcmp((const char *) attribute_node->name,
2037 config_element_probe_attributes)) {
2038 xmlNodePtr probe_attribute_node;
2039
2040 /* probe_attributes */
2041 for (probe_attribute_node =
2042 xmlFirstElementChild(attribute_node); probe_attribute_node;
2043 probe_attribute_node = xmlNextElementSibling(
2044 probe_attribute_node)) {
2045
2046 ret = process_probe_attribute_node(probe_attribute_node,
2047 &event->attr.probe);
2048 if (ret) {
2049 goto end;
2050 }
2051 }
2052 } else if (!strcmp((const char *) attribute_node->name,
2053 config_element_function_attributes)) {
2054 size_t sym_len;
2055 xmlChar *content;
2056 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
2057
2058 /* function_attributes */
2059 content = xmlNodeGetContent(symbol_node);
2060 if (!content) {
2061 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2062 goto end;
2063 }
2064
2065 sym_len = strlen((char *) content);
2066 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
2067 WARN("Function name too long.");
2068 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2069 free(content);
2070 goto end;
2071 }
2072
2073 ret = lttng_strncpy(
2074 event->attr.ftrace.symbol_name,
2075 (char *) content, sym_len);
2076 if (ret == -1) {
2077 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2078 free(content);
2079 goto end;
2080 }
2081 free(content);
2082 } else if (!strcmp((const char *) attribute_node->name,
2083 config_element_userspace_probe_tracepoint_attributes)) {
2084 struct lttng_userspace_probe_location *location;
2085
2086 location = process_userspace_probe_tracepoint_attribute_node(attribute_node);
2087 if (!location) {
2088 WARN("Error processing userspace probe tracepoint attribute");
2089 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2090 goto end;
2091 }
2092 ret = lttng_event_set_userspace_probe_location(
2093 event, location);
2094 if (ret) {
2095 WARN("Error setting userspace probe location field");
2096 lttng_userspace_probe_location_destroy(
2097 location);
2098 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2099 goto end;
2100 }
2101 } else if (!strcmp((const char *) attribute_node->name,
2102 config_element_userspace_probe_function_attributes)) {
2103 struct lttng_userspace_probe_location *location;
2104
2105 location =
2106 process_userspace_probe_function_attribute_node(
2107 attribute_node);
2108 if (!location) {
2109 WARN("Error processing userspace probe function attribute");
2110 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2111 goto end;
2112 }
2113
2114 ret = lttng_event_set_userspace_probe_location(
2115 event, location);
2116 if (ret) {
2117 WARN("Error setting userspace probe location field");
2118 lttng_userspace_probe_location_destroy(
2119 location);
2120 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2121 goto end;
2122 }
2123 } else {
2124 /* Unknown event attribute. */
2125 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2126 goto end;
2127 }
2128 }
2129 }
2130
2131 if ((event->enabled && phase == ENABLE) || phase == CREATION) {
2132 ret = lttng_enable_event_with_exclusions(handle, event, channel_name,
2133 filter_expression, exclusion_count, exclusions);
2134 if (ret < 0) {
2135 WARN("Enabling event (name:%s) on load failed.", event->name);
2136 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2137 goto end;
2138 }
2139 }
2140 ret = 0;
2141 end:
2142 for (i = 0; i < exclusion_count; i++) {
2143 free(exclusions[i]);
2144 }
2145
2146 lttng_event_destroy(event);
2147 free(exclusions);
2148 free(filter_expression);
2149 return ret;
2150 }
2151
2152 static
2153 int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
2154 const char *channel_name)
2155 {
2156 int ret = 0;
2157 struct lttng_event event;
2158 xmlNodePtr node;
2159
2160 assert(events_node);
2161 assert(handle);
2162 assert(channel_name);
2163
2164 for (node = xmlFirstElementChild(events_node); node;
2165 node = xmlNextElementSibling(node)) {
2166 ret = process_event_node(node, handle, channel_name, CREATION);
2167 if (ret) {
2168 goto end;
2169 }
2170 }
2171
2172 /*
2173 * Disable all events to enable only the necessary events.
2174 * Limitations regarding lttng_disable_events and tuple descriptor
2175 * force this approach.
2176 */
2177 memset(&event, 0, sizeof(event));
2178 event.loglevel = -1;
2179 event.type = LTTNG_EVENT_ALL;
2180 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
2181 if (ret) {
2182 goto end;
2183 }
2184
2185 for (node = xmlFirstElementChild(events_node); node;
2186 node = xmlNextElementSibling(node)) {
2187 ret = process_event_node(node, handle, channel_name, ENABLE);
2188 if (ret) {
2189 goto end;
2190 }
2191 }
2192
2193 end:
2194 return ret;
2195 }
2196
2197 static
2198 int process_channel_attr_node(xmlNodePtr attr_node,
2199 struct lttng_channel *channel, xmlNodePtr *contexts_node,
2200 xmlNodePtr *events_node)
2201 {
2202 int ret;
2203
2204 assert(attr_node);
2205 assert(channel);
2206 assert(contexts_node);
2207 assert(events_node);
2208
2209 if (!strcmp((const char *) attr_node->name, config_element_name)) {
2210 xmlChar *content;
2211
2212 /* name */
2213 content = xmlNodeGetContent(attr_node);
2214 if (!content) {
2215 ret = -LTTNG_ERR_NOMEM;
2216 goto end;
2217 }
2218
2219 ret = lttng_strncpy(channel->name,
2220 (const char *) content,
2221 LTTNG_SYMBOL_NAME_LEN);
2222 if (ret == -1) {
2223 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2224 (const char *) content,
2225 strlen((const char *) content),
2226 LTTNG_SYMBOL_NAME_LEN);
2227 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2228 free(content);
2229 goto end;
2230 }
2231 free(content);
2232 } else if (!strcmp((const char *) attr_node->name,
2233 config_element_enabled)) {
2234 xmlChar *content;
2235 int enabled;
2236
2237 /* enabled */
2238 content = xmlNodeGetContent(attr_node);
2239 if (!content) {
2240 ret = -LTTNG_ERR_NOMEM;
2241 goto end;
2242 }
2243
2244 ret = parse_bool(content, &enabled);
2245 free(content);
2246 if (ret) {
2247 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2248 goto end;
2249 }
2250
2251 channel->enabled = enabled;
2252 } else if (!strcmp((const char *) attr_node->name,
2253 config_element_overwrite_mode)) {
2254 xmlChar *content;
2255
2256 /* overwrite_mode */
2257 content = xmlNodeGetContent(attr_node);
2258 if (!content) {
2259 ret = -LTTNG_ERR_NOMEM;
2260 goto end;
2261 }
2262
2263 ret = get_overwrite_mode(content);
2264 free(content);
2265 if (ret < 0) {
2266 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2267 goto end;
2268 }
2269
2270 channel->attr.overwrite = ret;
2271 } else if (!strcmp((const char *) attr_node->name,
2272 config_element_subbuf_size)) {
2273 xmlChar *content;
2274
2275 /* subbuffer_size */
2276 content = xmlNodeGetContent(attr_node);
2277 if (!content) {
2278 ret = -LTTNG_ERR_NOMEM;
2279 goto end;
2280 }
2281
2282 ret = parse_uint(content, &channel->attr.subbuf_size);
2283 free(content);
2284 if (ret) {
2285 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2286 goto end;
2287 }
2288 } else if (!strcmp((const char *) attr_node->name,
2289 config_element_num_subbuf)) {
2290 xmlChar *content;
2291
2292 /* subbuffer_count */
2293 content = xmlNodeGetContent(attr_node);
2294 if (!content) {
2295 ret = -LTTNG_ERR_NOMEM;
2296 goto end;
2297 }
2298
2299 ret = parse_uint(content, &channel->attr.num_subbuf);
2300 free(content);
2301 if (ret) {
2302 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2303 goto end;
2304 }
2305 } else if (!strcmp((const char *) attr_node->name,
2306 config_element_switch_timer_interval)) {
2307 xmlChar *content;
2308 uint64_t switch_timer_interval = 0;
2309
2310 /* switch_timer_interval */
2311 content = xmlNodeGetContent(attr_node);
2312 if (!content) {
2313 ret = -LTTNG_ERR_NOMEM;
2314 goto end;
2315 }
2316
2317 ret = parse_uint(content, &switch_timer_interval);
2318 free(content);
2319 if (ret) {
2320 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2321 goto end;
2322 }
2323
2324 if (switch_timer_interval > UINT_MAX) {
2325 WARN("switch_timer_interval out of range.");
2326 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2327 goto end;
2328 }
2329
2330 channel->attr.switch_timer_interval =
2331 switch_timer_interval;
2332 } else if (!strcmp((const char *) attr_node->name,
2333 config_element_read_timer_interval)) {
2334 xmlChar *content;
2335 uint64_t read_timer_interval = 0;
2336
2337 /* read_timer_interval */
2338 content = xmlNodeGetContent(attr_node);
2339 if (!content) {
2340 ret = -LTTNG_ERR_NOMEM;
2341 goto end;
2342 }
2343
2344 ret = parse_uint(content, &read_timer_interval);
2345 free(content);
2346 if (ret) {
2347 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2348 goto end;
2349 }
2350
2351 if (read_timer_interval > UINT_MAX) {
2352 WARN("read_timer_interval out of range.");
2353 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2354 goto end;
2355 }
2356
2357 channel->attr.read_timer_interval =
2358 read_timer_interval;
2359 } else if (!strcmp((const char *) attr_node->name,
2360 config_element_output_type)) {
2361 xmlChar *content;
2362
2363 /* output_type */
2364 content = xmlNodeGetContent(attr_node);
2365 if (!content) {
2366 ret = -LTTNG_ERR_NOMEM;
2367 goto end;
2368 }
2369
2370 ret = get_output_type(content);
2371 free(content);
2372 if (ret < 0) {
2373 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2374 goto end;
2375 }
2376
2377 channel->attr.output = ret;
2378 } else if (!strcmp((const char *) attr_node->name,
2379 config_element_tracefile_size)) {
2380 xmlChar *content;
2381
2382 /* tracefile_size */
2383 content = xmlNodeGetContent(attr_node);
2384 if (!content) {
2385 ret = -LTTNG_ERR_NOMEM;
2386 goto end;
2387 }
2388
2389 ret = parse_uint(content, &channel->attr.tracefile_size);
2390 free(content);
2391 if (ret) {
2392 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2393 goto end;
2394 }
2395 } else if (!strcmp((const char *) attr_node->name,
2396 config_element_tracefile_count)) {
2397 xmlChar *content;
2398
2399 /* tracefile_count */
2400 content = xmlNodeGetContent(attr_node);
2401 if (!content) {
2402 ret = -LTTNG_ERR_NOMEM;
2403 goto end;
2404 }
2405
2406 ret = parse_uint(content, &channel->attr.tracefile_count);
2407 free(content);
2408 if (ret) {
2409 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2410 goto end;
2411 }
2412 } else if (!strcmp((const char *) attr_node->name,
2413 config_element_live_timer_interval)) {
2414 xmlChar *content;
2415 uint64_t live_timer_interval = 0;
2416
2417 /* live_timer_interval */
2418 content = xmlNodeGetContent(attr_node);
2419 if (!content) {
2420 ret = -LTTNG_ERR_NOMEM;
2421 goto end;
2422 }
2423
2424 ret = parse_uint(content, &live_timer_interval);
2425 free(content);
2426 if (ret) {
2427 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2428 goto end;
2429 }
2430
2431 if (live_timer_interval > UINT_MAX) {
2432 WARN("live_timer_interval out of range.");
2433 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2434 goto end;
2435 }
2436
2437 channel->attr.live_timer_interval =
2438 live_timer_interval;
2439 } else if (!strcmp((const char *) attr_node->name,
2440 config_element_monitor_timer_interval)) {
2441 xmlChar *content;
2442 uint64_t monitor_timer_interval = 0;
2443
2444 /* monitor_timer_interval */
2445 content = xmlNodeGetContent(attr_node);
2446 if (!content) {
2447 ret = -LTTNG_ERR_NOMEM;
2448 goto end;
2449 }
2450
2451 ret = parse_uint(content, &monitor_timer_interval);
2452 free(content);
2453 if (ret) {
2454 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2455 goto end;
2456 }
2457
2458 ret = lttng_channel_set_monitor_timer_interval(channel,
2459 monitor_timer_interval);
2460 if (ret) {
2461 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2462 goto end;
2463 }
2464 } else if (!strcmp((const char *) attr_node->name,
2465 config_element_blocking_timeout)) {
2466 xmlChar *content;
2467 int64_t blocking_timeout = 0;
2468
2469 /* blocking_timeout */
2470 content = xmlNodeGetContent(attr_node);
2471 if (!content) {
2472 ret = -LTTNG_ERR_NOMEM;
2473 goto end;
2474 }
2475
2476 ret = parse_int(content, &blocking_timeout);
2477 free(content);
2478 if (ret) {
2479 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2480 goto end;
2481 }
2482
2483 ret = lttng_channel_set_blocking_timeout(channel,
2484 blocking_timeout);
2485 if (ret) {
2486 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2487 goto end;
2488 }
2489 } else if (!strcmp((const char *) attr_node->name,
2490 config_element_events)) {
2491 /* events */
2492 *events_node = attr_node;
2493 } else {
2494 /* contexts */
2495 *contexts_node = attr_node;
2496 }
2497 ret = 0;
2498 end:
2499 return ret;
2500 }
2501
2502 static
2503 int process_context_node(xmlNodePtr context_node,
2504 struct lttng_handle *handle, const char *channel_name)
2505 {
2506 int ret;
2507 struct lttng_event_context context;
2508 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
2509
2510 assert(handle);
2511 assert(channel_name);
2512
2513 if (!context_child_node) {
2514 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2515 goto end;
2516 }
2517
2518 memset(&context, 0, sizeof(context));
2519
2520 if (!strcmp((const char *) context_child_node->name,
2521 config_element_type)) {
2522 /* type */
2523 xmlChar *content = xmlNodeGetContent(context_child_node);
2524
2525 if (!content) {
2526 ret = -LTTNG_ERR_NOMEM;
2527 goto end;
2528 }
2529
2530 ret = get_context_type(content);
2531 free(content);
2532 if (ret < 0) {
2533 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2534 goto end;
2535 }
2536
2537 context.ctx = ret;
2538 } else if (!strcmp((const char *) context_child_node->name,
2539 config_element_context_perf)) {
2540 /* perf */
2541 xmlNodePtr perf_attr_node;
2542
2543 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2544 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2545 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
2546 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2547 perf_attr_node; perf_attr_node =
2548 xmlNextElementSibling(perf_attr_node)) {
2549 if (!strcmp((const char *) perf_attr_node->name,
2550 config_element_type)) {
2551 xmlChar *content;
2552 uint64_t type = 0;
2553
2554 /* type */
2555 content = xmlNodeGetContent(perf_attr_node);
2556 if (!content) {
2557 ret = -LTTNG_ERR_NOMEM;
2558 goto end;
2559 }
2560
2561 ret = parse_uint(content, &type);
2562 free(content);
2563 if (ret) {
2564 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2565 goto end;
2566 }
2567
2568 if (type > UINT32_MAX) {
2569 WARN("perf context type out of range.");
2570 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2571 goto end;
2572 }
2573
2574 context.u.perf_counter.type = type;
2575 } else if (!strcmp((const char *) perf_attr_node->name,
2576 config_element_config)) {
2577 xmlChar *content;
2578 uint64_t config = 0;
2579
2580 /* config */
2581 content = xmlNodeGetContent(perf_attr_node);
2582 if (!content) {
2583 ret = -LTTNG_ERR_NOMEM;
2584 goto end;
2585 }
2586
2587 ret = parse_uint(content, &config);
2588 free(content);
2589 if (ret) {
2590 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2591 goto end;
2592 }
2593
2594 context.u.perf_counter.config = config;
2595 } else if (!strcmp((const char *) perf_attr_node->name,
2596 config_element_name)) {
2597 xmlChar *content;
2598
2599 /* name */
2600 content = xmlNodeGetContent(perf_attr_node);
2601 if (!content) {
2602 ret = -LTTNG_ERR_NOMEM;
2603 goto end;
2604 }
2605
2606 ret = lttng_strncpy(context.u.perf_counter.name,
2607 (const char *) content,
2608 LTTNG_SYMBOL_NAME_LEN);
2609 if (ret == -1) {
2610 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2611 (const char *) content,
2612 strlen((const char *) content),
2613 LTTNG_SYMBOL_NAME_LEN);
2614 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2615 free(content);
2616 goto end;
2617 }
2618 free(content);
2619 }
2620 }
2621 } else if (!strcmp((const char *) context_child_node->name,
2622 config_element_context_app)) {
2623 /* application context */
2624 xmlNodePtr app_ctx_node;
2625
2626 context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT;
2627 for (app_ctx_node = xmlFirstElementChild(context_child_node);
2628 app_ctx_node; app_ctx_node =
2629 xmlNextElementSibling(app_ctx_node)) {
2630 xmlChar *content;
2631 char **target = strcmp(
2632 (const char *) app_ctx_node->name,
2633 config_element_context_app_provider_name) == 0 ?
2634 &context.u.app_ctx.provider_name :
2635 &context.u.app_ctx.ctx_name;
2636
2637 content = xmlNodeGetContent(app_ctx_node);
2638 if (!content) {
2639 ret = -LTTNG_ERR_NOMEM;
2640 goto end;
2641 }
2642
2643 *target = (char *) content;
2644 }
2645 } else {
2646 /* Unrecognized context type */
2647 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2648 goto end;
2649 }
2650
2651 ret = lttng_add_context(handle, &context, NULL, channel_name);
2652 if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
2653 free(context.u.app_ctx.provider_name);
2654 free(context.u.app_ctx.ctx_name);
2655 }
2656 end:
2657 return ret;
2658 }
2659
2660 static
2661 int process_contexts_node(xmlNodePtr contexts_node,
2662 struct lttng_handle *handle, const char *channel_name)
2663 {
2664 int ret = 0;
2665 xmlNodePtr context_node;
2666
2667 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2668 context_node = xmlNextElementSibling(context_node)) {
2669 ret = process_context_node(context_node, handle, channel_name);
2670 if (ret) {
2671 goto end;
2672 }
2673 }
2674 end:
2675 return ret;
2676 }
2677
2678 static int get_tracker_elements(enum lttng_tracker_type tracker_type,
2679 const char **element_id_tracker,
2680 const char **element_target_id,
2681 const char **element_id,
2682 const char **element_id_alias,
2683 const char **element_name)
2684 {
2685 int ret = 0;
2686
2687 switch (tracker_type) {
2688 case LTTNG_TRACKER_PID:
2689 *element_id_tracker = config_element_pid_tracker;
2690 *element_target_id = config_element_target_pid;
2691 *element_id = config_element_id;
2692 *element_id_alias = config_element_pid;
2693 *element_name = NULL;
2694 break;
2695 case LTTNG_TRACKER_VPID:
2696 *element_id_tracker = config_element_vpid_tracker;
2697 *element_target_id = config_element_target_vpid;
2698 *element_id = config_element_id;
2699 *element_id_alias = NULL;
2700 *element_name = NULL;
2701 break;
2702 case LTTNG_TRACKER_UID:
2703 *element_id_tracker = config_element_uid_tracker;
2704 *element_target_id = config_element_target_uid;
2705 *element_id = config_element_id;
2706 *element_id_alias = NULL;
2707 *element_name = config_element_name;
2708 break;
2709 case LTTNG_TRACKER_VUID:
2710 *element_id_tracker = config_element_vuid_tracker;
2711 *element_target_id = config_element_target_vuid;
2712 *element_id = config_element_id;
2713 *element_id_alias = NULL;
2714 *element_name = config_element_name;
2715 break;
2716 case LTTNG_TRACKER_GID:
2717 *element_id_tracker = config_element_gid_tracker;
2718 *element_target_id = config_element_target_gid;
2719 *element_id = config_element_id;
2720 *element_id_alias = NULL;
2721 *element_name = config_element_name;
2722 break;
2723 case LTTNG_TRACKER_VGID:
2724 *element_id_tracker = config_element_vgid_tracker;
2725 *element_target_id = config_element_target_vgid;
2726 *element_id = config_element_id;
2727 *element_id_alias = NULL;
2728 *element_name = config_element_name;
2729 break;
2730 default:
2731 ret = LTTNG_ERR_INVALID;
2732 }
2733 return ret;
2734 }
2735
2736 static int process_id_tracker_node(xmlNodePtr id_tracker_node,
2737 struct lttng_handle *handle,
2738 enum lttng_tracker_type tracker_type)
2739 {
2740 int ret = 0, child;
2741 xmlNodePtr targets_node = NULL;
2742 xmlNodePtr node;
2743 const char *element_id_tracker;
2744 const char *element_target_id;
2745 const char *element_id;
2746 const char *element_id_alias;
2747 const char *element_name;
2748 enum lttng_tracker_id_status status;
2749
2750 assert(handle);
2751 assert(id_tracker_node);
2752
2753 ret = get_tracker_elements(tracker_type, &element_id_tracker,
2754 &element_target_id, &element_id, &element_id_alias,
2755 &element_name);
2756 if (ret) {
2757 return ret;
2758 }
2759
2760 /* get the targets node */
2761 for (node = xmlFirstElementChild(id_tracker_node); node;
2762 node = xmlNextElementSibling(node)) {
2763 if (!strcmp((const char *) node->name,
2764 config_element_targets)) {
2765 targets_node = node;
2766 break;
2767 }
2768 }
2769
2770 if (!targets_node) {
2771 ret = LTTNG_ERR_INVALID;
2772 goto end;
2773 }
2774
2775 /* Go through all id target node */
2776 child = xmlChildElementCount(targets_node);
2777 if (child == 0) {
2778 struct lttng_tracker_id *tracker_id = NULL;
2779 tracker_id = lttng_tracker_id_create();
2780 if (tracker_id == NULL) {
2781 ret = LTTNG_ERR_NOMEM;
2782 goto end;
2783 }
2784 status = lttng_tracker_id_set_all(tracker_id);
2785 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
2786 ret = LTTNG_ERR_INVALID;
2787 goto end;
2788 }
2789
2790 /* The session is explicitly set to target nothing. */
2791 ret = lttng_untrack_id(handle, tracker_type, tracker_id);
2792 lttng_tracker_id_destroy(tracker_id);
2793 if (ret) {
2794 goto end;
2795 }
2796 }
2797 for (node = xmlFirstElementChild(targets_node); node;
2798 node = xmlNextElementSibling(node)) {
2799 xmlNodePtr id_target_node = node;
2800
2801 /* get id node and track it */
2802 for (node = xmlFirstElementChild(id_target_node); node;
2803 node = xmlNextElementSibling(node)) {
2804 if (!strcmp((const char *) node->name, element_id) ||
2805 (element_id_alias &&
2806 !strcmp((const char *) node->name,
2807 element_id_alias))) {
2808 int64_t id;
2809 xmlChar *content = NULL;
2810 struct lttng_tracker_id *tracker_id = NULL;
2811
2812 content = xmlNodeGetContent(node);
2813 if (!content) {
2814 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2815 goto end;
2816 }
2817
2818 ret = parse_int(content, &id);
2819 free(content);
2820 if (ret) {
2821 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2822 goto end;
2823 }
2824
2825 tracker_id = lttng_tracker_id_create();
2826 if (tracker_id == NULL) {
2827 ret = LTTNG_ERR_NOMEM;
2828 goto end;
2829 }
2830
2831 status = lttng_tracker_id_set_value(
2832 tracker_id, id);
2833 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
2834 lttng_tracker_id_destroy(tracker_id);
2835 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2836 goto end;
2837 }
2838
2839 ret = lttng_track_id(handle, tracker_type,
2840 tracker_id);
2841 lttng_tracker_id_destroy(tracker_id);
2842 if (ret) {
2843 goto end;
2844 }
2845 }
2846 if (element_name && !strcmp((const char *) node->name,
2847 element_name)) {
2848 xmlChar *content = NULL;
2849 struct lttng_tracker_id *tracker_id = NULL;
2850
2851 content = xmlNodeGetContent(node);
2852 if (!content) {
2853 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2854 goto end;
2855 }
2856
2857 tracker_id = lttng_tracker_id_create();
2858 if (tracker_id == NULL) {
2859 ret = LTTNG_ERR_NOMEM;
2860 goto end;
2861 }
2862
2863 status = lttng_tracker_id_set_string(tracker_id,
2864 (const char *) content);
2865 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
2866 lttng_tracker_id_destroy(tracker_id);
2867 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2868 goto end;
2869 }
2870
2871 ret = lttng_track_id(handle, tracker_type,
2872 tracker_id);
2873 lttng_tracker_id_destroy(tracker_id);
2874 free(content);
2875 if (ret) {
2876 goto end;
2877 }
2878 }
2879 }
2880 node = id_target_node;
2881 }
2882
2883 end:
2884 return ret;
2885 }
2886
2887 static
2888 int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2889 {
2890 int ret;
2891 struct lttng_domain domain = { 0 };
2892 struct lttng_handle *handle = NULL;
2893 struct lttng_channel *channel = NULL;
2894 xmlNodePtr channels_node = NULL;
2895 xmlNodePtr trackers_node = NULL;
2896 xmlNodePtr pid_tracker_node = NULL;
2897 xmlNodePtr vpid_tracker_node = NULL;
2898 xmlNodePtr uid_tracker_node = NULL;
2899 xmlNodePtr vuid_tracker_node = NULL;
2900 xmlNodePtr gid_tracker_node = NULL;
2901 xmlNodePtr vgid_tracker_node = NULL;
2902 xmlNodePtr node;
2903
2904 assert(session_name);
2905
2906 ret = init_domain(domain_node, &domain);
2907 if (ret) {
2908 goto end;
2909 }
2910
2911 handle = lttng_create_handle(session_name, &domain);
2912 if (!handle) {
2913 ret = -LTTNG_ERR_NOMEM;
2914 goto end;
2915 }
2916
2917 /* get the channels node */
2918 for (node = xmlFirstElementChild(domain_node); node;
2919 node = xmlNextElementSibling(node)) {
2920 if (!strcmp((const char *) node->name,
2921 config_element_channels)) {
2922 channels_node = node;
2923 break;
2924 }
2925 }
2926
2927 if (!channels_node) {
2928 goto end;
2929 }
2930
2931 /* create all channels */
2932 for (node = xmlFirstElementChild(channels_node); node;
2933 node = xmlNextElementSibling(node)) {
2934 const enum lttng_domain_type original_domain = domain.type;
2935 xmlNodePtr contexts_node = NULL;
2936 xmlNodePtr events_node = NULL;
2937 xmlNodePtr channel_attr_node;
2938
2939 /*
2940 * Channels of the "agent" types cannot be created directly.
2941 * They are meant to be created implicitly through the
2942 * activation of events in their domain. However, a user
2943 * can override the default channel configuration attributes
2944 * by creating the underlying UST channel _before_ enabling
2945 * an agent domain event.
2946 *
2947 * Hence, the channel's type is substituted before the creation
2948 * and restored by the time the events are created.
2949 */
2950 switch (domain.type) {
2951 case LTTNG_DOMAIN_JUL:
2952 case LTTNG_DOMAIN_LOG4J:
2953 case LTTNG_DOMAIN_PYTHON:
2954 domain.type = LTTNG_DOMAIN_UST;
2955 default:
2956 break;
2957 }
2958
2959 channel = lttng_channel_create(&domain);
2960 if (!channel) {
2961 ret = -1;
2962 goto end;
2963 }
2964
2965 for (channel_attr_node = xmlFirstElementChild(node);
2966 channel_attr_node; channel_attr_node =
2967 xmlNextElementSibling(channel_attr_node)) {
2968 ret = process_channel_attr_node(channel_attr_node,
2969 channel, &contexts_node, &events_node);
2970 if (ret) {
2971 goto end;
2972 }
2973 }
2974
2975 ret = lttng_enable_channel(handle, channel);
2976 if (ret < 0) {
2977 goto end;
2978 }
2979
2980 /* Restore the original channel domain. */
2981 domain.type = original_domain;
2982
2983 ret = process_events_node(events_node, handle, channel->name);
2984 if (ret) {
2985 goto end;
2986 }
2987
2988 ret = process_contexts_node(contexts_node, handle,
2989 channel->name);
2990 if (ret) {
2991 goto end;
2992 }
2993
2994 lttng_channel_destroy(channel);
2995 }
2996 channel = NULL;
2997
2998 /* get the trackers node */
2999 for (node = xmlFirstElementChild(domain_node); node;
3000 node = xmlNextElementSibling(node)) {
3001 if (!strcmp((const char *) node->name,
3002 config_element_trackers)) {
3003 trackers_node = node;
3004 break;
3005 }
3006 }
3007
3008 if (!trackers_node) {
3009 goto end;
3010 }
3011
3012 for (node = xmlFirstElementChild(trackers_node); node;
3013 node = xmlNextElementSibling(node)) {
3014 if (!strcmp((const char *) node->name,
3015 config_element_pid_tracker)) {
3016 pid_tracker_node = node;
3017 ret = process_id_tracker_node(pid_tracker_node, handle,
3018 LTTNG_TRACKER_PID);
3019 if (ret) {
3020 goto end;
3021 }
3022 }
3023 if (!strcmp((const char *) node->name,
3024 config_element_vpid_tracker)) {
3025 vpid_tracker_node = node;
3026 ret = process_id_tracker_node(vpid_tracker_node, handle,
3027 LTTNG_TRACKER_VPID);
3028 if (ret) {
3029 goto end;
3030 }
3031 }
3032 if (!strcmp((const char *) node->name,
3033 config_element_uid_tracker)) {
3034 uid_tracker_node = node;
3035 ret = process_id_tracker_node(uid_tracker_node, handle,
3036 LTTNG_TRACKER_UID);
3037 if (ret) {
3038 goto end;
3039 }
3040 }
3041 if (!strcmp((const char *) node->name,
3042 config_element_vuid_tracker)) {
3043 vuid_tracker_node = node;
3044 ret = process_id_tracker_node(vuid_tracker_node, handle,
3045 LTTNG_TRACKER_VUID);
3046 if (ret) {
3047 goto end;
3048 }
3049 }
3050 if (!strcmp((const char *) node->name,
3051 config_element_gid_tracker)) {
3052 gid_tracker_node = node;
3053 ret = process_id_tracker_node(gid_tracker_node, handle,
3054 LTTNG_TRACKER_GID);
3055 if (ret) {
3056 goto end;
3057 }
3058 }
3059 if (!strcmp((const char *) node->name,
3060 config_element_vgid_tracker)) {
3061 vgid_tracker_node = node;
3062 ret = process_id_tracker_node(vgid_tracker_node, handle,
3063 LTTNG_TRACKER_VGID);
3064 if (ret) {
3065 goto end;
3066 }
3067 }
3068 }
3069
3070 end:
3071 lttng_channel_destroy(channel);
3072 lttng_destroy_handle(handle);
3073 return ret;
3074 }
3075
3076 static
3077 int add_periodic_rotation(const char *name, uint64_t time_us)
3078 {
3079 int ret;
3080 enum lttng_rotation_status status;
3081 struct lttng_rotation_schedule *periodic =
3082 lttng_rotation_schedule_periodic_create();
3083
3084 if (!periodic) {
3085 ret = -LTTNG_ERR_NOMEM;
3086 goto error;
3087 }
3088
3089 status = lttng_rotation_schedule_periodic_set_period(periodic,
3090 time_us);
3091 if (status != LTTNG_ROTATION_STATUS_OK) {
3092 ret = -LTTNG_ERR_INVALID;
3093 goto error;
3094 }
3095
3096 status = lttng_session_add_rotation_schedule(name, periodic);
3097 switch (status) {
3098 case LTTNG_ROTATION_STATUS_OK:
3099 ret = 0;
3100 break;
3101 case LTTNG_ROTATION_STATUS_SCHEDULE_ALREADY_SET:
3102 case LTTNG_ROTATION_STATUS_INVALID:
3103 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
3104 break;