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