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