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