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