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