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