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