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