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