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