2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
27 #include <sys/types.h>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <lttng/lttng-error.h>
35 #include <libxml/parser.h>
36 #include <libxml/valid.h>
37 #include <libxml/xmlschemas.h>
38 #include <libxml/tree.h>
39 #include <lttng/lttng.h>
40 #include <lttng/snapshot.h>
43 #include "config-internal.h"
45 struct handler_filter_args
{
47 config_entry_handler_cb handler
;
51 struct session_config_validation_ctx
{
52 xmlSchemaParserCtxtPtr parser_ctx
;
54 xmlSchemaValidCtxtPtr schema_validation_ctx
;
57 const char * const config_str_yes
= "yes";
58 const char * const config_str_true
= "true";
59 const char * const config_str_on
= "on";
60 const char * const config_str_no
= "no";
61 const char * const config_str_false
= "false";
62 const char * const config_str_off
= "off";
63 const char * const config_xml_encoding
= "UTF-8";
64 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
65 const char * const config_xml_indent_string
= "\t";
66 const char * const config_xml_true
= "true";
67 const char * const config_xml_false
= "false";
69 const char * const config_element_channel
= "channel";
70 const char * const config_element_channels
= "channels";
71 const char * const config_element_domain
= "domain";
72 const char * const config_element_domains
= "domains";
73 const char * const config_element_event
= "event";
74 const char * const config_element_events
= "events";
75 const char * const config_element_context
= "context";
76 const char * const config_element_contexts
= "contexts";
77 const char * const config_element_attributes
= "attributes";
78 const char * const config_element_exclusion
= "exclusion";
79 const char * const config_element_exclusions
= "exclusions";
80 const char * const config_element_function_attributes
= "function_attributes";
81 const char * const config_element_probe_attributes
= "probe_attributes";
82 const char * const config_element_symbol_name
= "symbol_name";
83 const char * const config_element_address
= "address";
84 const char * const config_element_offset
= "offset";
85 const char * const config_element_name
= "name";
86 const char * const config_element_enabled
= "enabled";
87 const char * const config_element_overwrite_mode
= "overwrite_mode";
88 const char * const config_element_subbuf_size
= "subbuffer_size";
89 const char * const config_element_num_subbuf
= "subbuffer_count";
90 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
91 const char * const config_element_read_timer_interval
= "read_timer_interval";
92 const char * const config_element_output
= "output";
93 const char * const config_element_output_type
= "output_type";
94 const char * const config_element_tracefile_size
= "tracefile_size";
95 const char * const config_element_tracefile_count
= "tracefile_count";
96 const char * const config_element_live_timer_interval
= "live_timer_interval";
97 const char * const config_element_type
= "type";
98 const char * const config_element_buffer_type
= "buffer_type";
99 const char * const config_element_session
= "session";
100 const char * const config_element_sessions
= "sessions";
101 const char * const config_element_perf
= "perf";
102 const char * const config_element_config
= "config";
103 const char * const config_element_started
= "started";
104 const char * const config_element_snapshot_mode
= "snapshot_mode";
105 const char * const config_element_loglevel
= "loglevel";
106 const char * const config_element_loglevel_type
= "loglevel_type";
107 const char * const config_element_filter
= "filter";
108 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
109 const char * const config_element_consumer_output
= "consumer_output";
110 const char * const config_element_destination
= "destination";
111 const char * const config_element_path
= "path";
112 const char * const config_element_net_output
= "net_output";
113 const char * const config_element_control_uri
= "control_uri";
114 const char * const config_element_data_uri
= "data_uri";
115 const char * const config_element_max_size
= "max_size";
117 const char * const config_domain_type_kernel
= "KERNEL";
118 const char * const config_domain_type_ust
= "UST";
119 const char * const config_domain_type_jul
= "JUL";
120 const char * const config_domain_type_log4j
= "LOG4J";
121 const char * const config_domain_type_python
= "PYTHON";
123 const char * const config_buffer_type_per_pid
= "PER_PID";
124 const char * const config_buffer_type_per_uid
= "PER_UID";
125 const char * const config_buffer_type_global
= "GLOBAL";
127 const char * const config_overwrite_mode_discard
= "DISCARD";
128 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
130 const char * const config_output_type_splice
= "SPLICE";
131 const char * const config_output_type_mmap
= "MMAP";
133 const char * const config_loglevel_type_all
= "ALL";
134 const char * const config_loglevel_type_range
= "RANGE";
135 const char * const config_loglevel_type_single
= "SINGLE";
137 const char * const config_event_type_all
= "ALL";
138 const char * const config_event_type_tracepoint
= "TRACEPOINT";
139 const char * const config_event_type_probe
= "PROBE";
140 const char * const config_event_type_function
= "FUNCTION";
141 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
142 const char * const config_event_type_noop
= "NOOP";
143 const char * const config_event_type_syscall
= "SYSCALL";
144 const char * const config_event_type_kprobe
= "KPROBE";
145 const char * const config_event_type_kretprobe
= "KRETPROBE";
147 const char * const config_event_context_pid
= "PID";
148 const char * const config_event_context_procname
= "PROCNAME";
149 const char * const config_event_context_prio
= "PRIO";
150 const char * const config_event_context_nice
= "NICE";
151 const char * const config_event_context_vpid
= "VPID";
152 const char * const config_event_context_tid
= "TID";
153 const char * const config_event_context_vtid
= "VTID";
154 const char * const config_event_context_ppid
= "PPID";
155 const char * const config_event_context_vppid
= "VPPID";
156 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
157 const char * const config_event_context_hostname
= "HOSTNAME";
158 const char * const config_event_context_ip
= "IP";
159 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
161 struct consumer_output
{
168 static int config_entry_handler_filter(struct handler_filter_args
*args
,
169 const char *section
, const char *name
, const char *value
)
172 struct config_entry entry
= { section
, name
, value
};
176 if (!section
|| !name
|| !value
) {
182 if (strcmp(args
->section
, section
)) {
187 ret
= args
->handler(&entry
, args
->user_data
);
193 int config_get_section_entries(const char *override_path
, const char *section
,
194 config_entry_handler_cb handler
, void *user_data
)
198 FILE *config_file
= NULL
;
199 struct handler_filter_args filter
= { section
, handler
, user_data
};
201 /* First, try system-wide conf. file. */
202 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
204 config_file
= fopen(path
, "r");
206 DBG("Loading daemon conf file at %s", path
);
208 * Return value is not very important here since error or not, we
209 * continue and try the next possible conf. file.
211 (void) ini_parse_file(config_file
,
212 (ini_entry_handler
) config_entry_handler_filter
,
217 /* Second is the user local configuration. */
218 path
= utils_get_home_dir();
220 char fullpath
[PATH_MAX
];
222 ret
= snprintf(fullpath
, sizeof(fullpath
),
223 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
225 PERROR("snprintf user conf. path");
229 config_file
= fopen(fullpath
, "r");
231 DBG("Loading daemon user conf file at %s", path
);
233 * Return value is not very important here since error or not, we
234 * continue and try the next possible conf. file.
236 (void) ini_parse_file(config_file
,
237 (ini_entry_handler
) config_entry_handler_filter
,
243 /* Final path is the one that the user might have provided. */
245 config_file
= fopen(override_path
, "r");
247 DBG("Loading daemon command line conf file at %s", override_path
);
248 (void) ini_parse_file(config_file
,
249 (ini_entry_handler
) config_entry_handler_filter
,
253 ERR("Failed to open daemon configuration file at %s",
260 /* Everything went well. */
268 int config_parse_value(const char *value
)
271 char *endptr
, *lower_str
;
281 v
= strtoul(value
, &endptr
, 10);
282 if (endptr
!= value
) {
287 lower_str
= zmalloc(len
+ 1);
294 for (i
= 0; i
< len
; i
++) {
295 lower_str
[i
] = tolower(value
[i
]);
298 if (!strcmp(lower_str
, config_str_yes
) ||
299 !strcmp(lower_str
, config_str_true
) ||
300 !strcmp(lower_str
, config_str_on
)) {
302 } else if (!strcmp(lower_str
, config_str_no
) ||
303 !strcmp(lower_str
, config_str_false
) ||
304 !strcmp(lower_str
, config_str_off
)) {
316 * Returns a xmlChar string which must be released using xmlFree().
318 static xmlChar
*encode_string(const char *in_str
)
320 xmlChar
*out_str
= NULL
;
321 xmlCharEncodingHandlerPtr handler
;
322 int out_len
, ret
, in_len
;
326 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
328 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
332 in_len
= strlen(in_str
);
334 * Add 1 byte for the NULL terminted character. The factor 2 here is
335 * because UTF-8 can be on two bytes so this fits the worst case for each
338 out_len
= (in_len
* 2) + 1;
339 out_str
= xmlMalloc(out_len
);
344 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
351 /* out_len is now the size of out_str */
352 out_str
[out_len
] = '\0';
358 struct config_writer
*config_writer_create(int fd_output
, int indent
)
361 struct config_writer
*writer
;
362 xmlOutputBufferPtr buffer
;
364 writer
= zmalloc(sizeof(struct config_writer
));
366 PERROR("zmalloc config_writer_create");
370 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
375 writer
->writer
= xmlNewTextWriter(buffer
);
376 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
377 config_xml_encoding
, NULL
);
382 ret
= xmlTextWriterSetIndentString(writer
->writer
,
383 BAD_CAST config_xml_indent_string
);
388 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
396 config_writer_destroy(writer
);
401 int config_writer_destroy(struct config_writer
*writer
)
410 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
411 WARN("Could not close XML document");
415 if (writer
->writer
) {
416 xmlFreeTextWriter(writer
->writer
);
425 int config_writer_open_element(struct config_writer
*writer
,
426 const char *element_name
)
429 xmlChar
*encoded_element_name
;
431 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
436 encoded_element_name
= encode_string(element_name
);
437 if (!encoded_element_name
) {
442 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
443 xmlFree(encoded_element_name
);
445 return ret
> 0 ? 0 : ret
;
449 int config_writer_close_element(struct config_writer
*writer
)
453 if (!writer
|| !writer
->writer
) {
458 ret
= xmlTextWriterEndElement(writer
->writer
);
460 return ret
> 0 ? 0 : ret
;
464 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
465 const char *element_name
, uint64_t value
)
468 xmlChar
*encoded_element_name
;
470 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
475 encoded_element_name
= encode_string(element_name
);
476 if (!encoded_element_name
) {
481 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
482 encoded_element_name
, "%" PRIu64
, value
);
483 xmlFree(encoded_element_name
);
485 return ret
> 0 ? 0 : ret
;
489 int config_writer_write_element_signed_int(struct config_writer
*writer
,
490 const char *element_name
, int64_t value
)
493 xmlChar
*encoded_element_name
;
495 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
500 encoded_element_name
= encode_string(element_name
);
501 if (!encoded_element_name
) {
506 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
507 encoded_element_name
, "%" PRIi64
, value
);
508 xmlFree(encoded_element_name
);
510 return ret
> 0 ? 0 : ret
;
514 int config_writer_write_element_bool(struct config_writer
*writer
,
515 const char *element_name
, int value
)
517 return config_writer_write_element_string(writer
, element_name
,
518 value
? config_xml_true
: config_xml_false
);
522 int config_writer_write_element_string(struct config_writer
*writer
,
523 const char *element_name
, const char *value
)
526 xmlChar
*encoded_element_name
= NULL
;
527 xmlChar
*encoded_value
= NULL
;
529 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
535 encoded_element_name
= encode_string(element_name
);
536 if (!encoded_element_name
) {
541 encoded_value
= encode_string(value
);
542 if (!encoded_value
) {
547 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
550 xmlFree(encoded_element_name
);
551 xmlFree(encoded_value
);
552 return ret
> 0 ? 0 : ret
;
556 void xml_error_handler(void *ctx
, const char *format
, ...)
562 va_start(args
, format
);
563 ret
= vasprintf(&errMsg
, format
, args
);
566 ERR("String allocation failed in xml error handler");
570 fprintf(stderr
, "XML Error: %s", errMsg
);
575 void fini_session_config_validation_ctx(
576 struct session_config_validation_ctx
*ctx
)
578 if (ctx
->parser_ctx
) {
579 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
583 xmlSchemaFree(ctx
->schema
);
586 if (ctx
->schema_validation_ctx
) {
587 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
590 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
594 char *get_session_config_xsd_path()
597 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
598 size_t base_path_len
;
602 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
605 base_path_len
= strlen(base_path
);
606 max_path_len
= base_path_len
+
607 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
608 xsd_path
= zmalloc(max_path_len
);
613 strncpy(xsd_path
, base_path
, max_path_len
);
614 if (xsd_path
[base_path_len
- 1] != '/') {
615 xsd_path
[base_path_len
++] = '/';
618 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
619 max_path_len
- base_path_len
);
625 int init_session_config_validation_ctx(
626 struct session_config_validation_ctx
*ctx
)
629 char *xsd_path
= get_session_config_xsd_path();
632 ret
= -LTTNG_ERR_NOMEM
;
636 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
637 if (!ctx
->parser_ctx
) {
638 ERR("XSD parser context creation failed");
639 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
642 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
643 xml_error_handler
, NULL
);
645 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
647 ERR("XSD parsing failed");
648 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
652 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
653 if (!ctx
->schema_validation_ctx
) {
654 ERR("XSD validation context creation failed");
655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
659 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
660 xml_error_handler
, NULL
);
665 fini_session_config_validation_ctx(ctx
);
673 int parse_uint(xmlChar
*str
, uint64_t *val
)
683 *val
= strtoull((const char *) str
, &endptr
, 10);
684 if (!endptr
|| *endptr
) {
695 int parse_int(xmlChar
*str
, int64_t *val
)
705 *val
= strtoll((const char *) str
, &endptr
, 10);
706 if (!endptr
|| *endptr
) {
717 int parse_bool(xmlChar
*str
, int *val
)
726 if (!strcmp((const char *) str
, config_xml_true
)) {
728 } else if (!strcmp((const char *) str
, config_xml_false
)) {
731 WARN("Invalid boolean value encoutered (%s).",
740 int get_domain_type(xmlChar
*domain
)
748 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
749 ret
= LTTNG_DOMAIN_KERNEL
;
750 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
751 ret
= LTTNG_DOMAIN_UST
;
752 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
753 ret
= LTTNG_DOMAIN_JUL
;
754 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
755 ret
= LTTNG_DOMAIN_LOG4J
;
756 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
757 ret
= LTTNG_DOMAIN_PYTHON
;
768 int get_buffer_type(xmlChar
*buffer_type
)
776 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
777 ret
= LTTNG_BUFFER_GLOBAL
;
778 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
779 ret
= LTTNG_BUFFER_PER_UID
;
780 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
781 ret
= LTTNG_BUFFER_PER_PID
;
792 int get_overwrite_mode(xmlChar
*overwrite_mode
)
796 if (!overwrite_mode
) {
800 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
802 } else if (!strcmp((char *) overwrite_mode
,
803 config_overwrite_mode_discard
)) {
815 int get_output_type(xmlChar
*output_type
)
823 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
824 ret
= LTTNG_EVENT_MMAP
;
825 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
826 ret
= LTTNG_EVENT_SPLICE
;
837 int get_event_type(xmlChar
*event_type
)
845 if (!strcmp((char *) event_type
, config_event_type_all
)) {
846 ret
= LTTNG_EVENT_ALL
;
847 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
848 ret
= LTTNG_EVENT_TRACEPOINT
;
849 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
850 ret
= LTTNG_EVENT_PROBE
;
851 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
852 ret
= LTTNG_EVENT_FUNCTION
;
853 } else if (!strcmp((char *) event_type
,
854 config_event_type_function_entry
)) {
855 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
856 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
857 ret
= LTTNG_EVENT_NOOP
;
858 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
859 ret
= LTTNG_EVENT_SYSCALL
;
870 int get_loglevel_type(xmlChar
*loglevel_type
)
874 if (!loglevel_type
) {
878 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
879 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
880 } else if (!strcmp((char *) loglevel_type
,
881 config_loglevel_type_range
)) {
882 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
883 } else if (!strcmp((char *) loglevel_type
,
884 config_loglevel_type_single
)) {
885 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
896 * Return the context type or -1 on error.
899 int get_context_type(xmlChar
*context_type
)
907 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
908 ret
= LTTNG_EVENT_CONTEXT_PID
;
909 } else if (!strcmp((char *) context_type
,
910 config_event_context_procname
)) {
911 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
912 } else if (!strcmp((char *) context_type
,
913 config_event_context_prio
)) {
914 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
915 } else if (!strcmp((char *) context_type
,
916 config_event_context_nice
)) {
917 ret
= LTTNG_EVENT_CONTEXT_NICE
;
918 } else if (!strcmp((char *) context_type
,
919 config_event_context_vpid
)) {
920 ret
= LTTNG_EVENT_CONTEXT_VPID
;
921 } else if (!strcmp((char *) context_type
,
922 config_event_context_tid
)) {
923 ret
= LTTNG_EVENT_CONTEXT_TID
;
924 } else if (!strcmp((char *) context_type
,
925 config_event_context_vtid
)) {
926 ret
= LTTNG_EVENT_CONTEXT_VTID
;
927 } else if (!strcmp((char *) context_type
,
928 config_event_context_ppid
)) {
929 ret
= LTTNG_EVENT_CONTEXT_PPID
;
930 } else if (!strcmp((char *) context_type
,
931 config_event_context_vppid
)) {
932 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
933 } else if (!strcmp((char *) context_type
,
934 config_event_context_pthread_id
)) {
935 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
936 } else if (!strcmp((char *) context_type
,
937 config_event_context_hostname
)) {
938 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
939 } else if (!strcmp((char *) context_type
,
940 config_event_context_ip
)) {
941 ret
= LTTNG_EVENT_CONTEXT_IP
;
952 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
957 for (node
= xmlFirstElementChild(domain_node
); node
;
958 node
= xmlNextElementSibling(node
)) {
959 if (!strcmp((const char *) node
->name
, config_element_type
)) {
961 xmlChar
*node_content
= xmlNodeGetContent(node
);
963 ret
= -LTTNG_ERR_NOMEM
;
967 ret
= get_domain_type(node_content
);
970 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
975 } else if (!strcmp((const char *) node
->name
,
976 config_element_buffer_type
)) {
978 xmlChar
*node_content
= xmlNodeGetContent(node
);
980 ret
= -LTTNG_ERR_NOMEM
;
984 ret
= get_buffer_type(node_content
);
987 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
991 domain
->buf_type
= ret
;
1000 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1005 for (node
= xmlFirstElementChild(net_output_node
); node
;
1006 node
= xmlNextElementSibling(node
)) {
1007 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1009 *control_uri
= (char *) xmlNodeGetContent(node
);
1010 if (!*control_uri
) {
1015 *data_uri
= (char *) xmlNodeGetContent(node
);
1022 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1026 int process_consumer_output(xmlNodePtr consumer_output_node
,
1027 struct consumer_output
*output
)
1034 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1035 node
= xmlNextElementSibling(node
)) {
1036 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1037 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1041 ret
= -LTTNG_ERR_NOMEM
;
1045 ret
= parse_bool(enabled_str
, &output
->enabled
);
1051 xmlNodePtr output_type_node
;
1054 output_type_node
= xmlFirstElementChild(node
);
1055 if (!output_type_node
) {
1056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1060 if (!strcmp((const char *) output_type_node
->name
,
1061 config_element_path
)) {
1063 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1064 if (!output
->path
) {
1065 ret
= -LTTNG_ERR_NOMEM
;
1070 ret
= get_net_output_uris(output_type_node
,
1071 &output
->control_uri
, &output
->data_uri
);
1083 free(output
->control_uri
);
1084 free(output
->data_uri
);
1085 memset(output
, 0, sizeof(struct consumer_output
));
1091 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1092 const char *control_uri
, const char *data_uri
)
1095 struct lttng_handle
*handle
;
1096 const char *uri
= NULL
;
1101 handle
= lttng_create_handle(name
, domain
);
1103 ret
= -LTTNG_ERR_NOMEM
;
1107 if (!control_uri
|| !data_uri
) {
1108 uri
= control_uri
? control_uri
: data_uri
;
1113 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1114 lttng_destroy_handle(handle
);
1120 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1123 xmlNodePtr node
= NULL
;
1124 xmlNodePtr snapshot_output_list_node
;
1125 xmlNodePtr snapshot_output_node
;
1127 assert(session_name
);
1129 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1138 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1140 /* Parse and create snapshot outputs */
1142 for (snapshot_output_node
=
1143 xmlFirstElementChild(snapshot_output_list_node
);
1144 snapshot_output_node
; snapshot_output_node
=
1145 xmlNextElementSibling(snapshot_output_node
)) {
1147 uint64_t max_size
= UINT64_MAX
;
1148 struct consumer_output output
= { 0 };
1149 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1151 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1152 node
= xmlNextElementSibling(node
)) {
1153 if (!strcmp((const char *) node
->name
,
1154 config_element_name
)) {
1156 name
= (char *) xmlNodeGetContent(node
);
1158 ret
= -LTTNG_ERR_NOMEM
;
1159 goto error_snapshot_output
;
1161 } else if (!strcmp((const char *) node
->name
,
1162 config_element_max_size
)) {
1163 xmlChar
*content
= xmlNodeGetContent(node
);
1167 ret
= -LTTNG_ERR_NOMEM
;
1168 goto error_snapshot_output
;
1170 ret
= parse_uint(content
, &max_size
);
1173 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1174 goto error_snapshot_output
;
1177 /* consumer_output */
1178 ret
= process_consumer_output(node
, &output
);
1180 goto error_snapshot_output
;
1185 snapshot_output
= lttng_snapshot_output_create();
1186 if (!snapshot_output
) {
1187 ret
= -LTTNG_ERR_NOMEM
;
1188 goto error_snapshot_output
;
1191 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1193 goto error_snapshot_output
;
1196 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1198 goto error_snapshot_output
;
1202 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1205 goto error_snapshot_output
;
1208 if (output
.control_uri
) {
1209 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1212 goto error_snapshot_output
;
1216 if (output
.data_uri
) {
1217 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1220 goto error_snapshot_output
;
1225 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1226 error_snapshot_output
:
1229 free(output
.control_uri
);
1230 free(output
.data_uri
);
1231 lttng_snapshot_output_destroy(snapshot_output
);
1241 int create_session(const char *name
,
1242 struct lttng_domain
*kernel_domain
,
1243 struct lttng_domain
*ust_domain
,
1244 struct lttng_domain
*jul_domain
,
1245 struct lttng_domain
*log4j_domain
,
1246 xmlNodePtr output_node
,
1247 uint64_t live_timer_interval
)
1250 struct consumer_output output
= { 0 };
1251 xmlNodePtr consumer_output_node
;
1256 consumer_output_node
= xmlFirstElementChild(output_node
);
1257 if (!consumer_output_node
) {
1258 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1262 if (strcmp((const char *) consumer_output_node
->name
,
1263 config_element_consumer_output
)) {
1264 WARN("Invalid output type, expected %s node",
1265 config_element_consumer_output
);
1266 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1270 ret
= process_consumer_output(consumer_output_node
, &output
);
1276 if (live_timer_interval
!= UINT64_MAX
&&
1277 !output
.control_uri
&& !output
.data_uri
) {
1278 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1282 if (output
.control_uri
|| output
.data_uri
) {
1284 struct lttng_domain
*domain
;
1285 struct lttng_domain
*domains
[] =
1286 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1288 /* network destination */
1289 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1291 * URLs are provided for sure since the test above make sure that
1292 * with a live timer the data and control URIs are provided. So,
1293 * NULL is passed here and will be set right after.
1295 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1297 ret
= lttng_create_session(name
, NULL
);
1303 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1304 domain
= domains
[i
];
1309 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1316 /* either local output or no output */
1317 ret
= lttng_create_session(name
, output
.path
);
1324 free(output
.control_uri
);
1325 free(output
.data_uri
);
1329 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1330 struct lttng_event_probe_attr
*attr
)
1334 assert(probe_attribute_node
);
1337 if (!strcmp((const char *) probe_attribute_node
->name
,
1338 config_element_address
)) {
1343 content
= xmlNodeGetContent(probe_attribute_node
);
1345 ret
= -LTTNG_ERR_NOMEM
;
1349 ret
= parse_uint(content
, &addr
);
1352 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1357 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1358 config_element_offset
)) {
1360 uint64_t offset
= 0;
1363 content
= xmlNodeGetContent(probe_attribute_node
);
1365 ret
= -LTTNG_ERR_NOMEM
;
1369 ret
= parse_uint(content
, &offset
);
1372 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1376 attr
->offset
= offset
;
1377 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1378 config_element_symbol_name
)) {
1383 content
= xmlNodeGetContent(probe_attribute_node
);
1385 ret
= -LTTNG_ERR_NOMEM
;
1389 name_len
= strlen((char *) content
);
1390 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1391 WARN("symbol_name too long.");
1392 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1397 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1406 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1407 const char *channel_name
)
1411 struct lttng_event event
;
1412 char **exclusions
= NULL
;
1413 unsigned long exclusion_count
= 0;
1414 char *filter_expression
= NULL
;
1418 assert(channel_name
);
1420 memset(&event
, 0, sizeof(event
));
1422 for (node
= xmlFirstElementChild(event_node
); node
;
1423 node
= xmlNextElementSibling(node
)) {
1424 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1429 content
= xmlNodeGetContent(node
);
1431 ret
= -LTTNG_ERR_NOMEM
;
1435 name_len
= strlen((char *) content
);
1436 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1437 WARN("Channel name too long.");
1438 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1443 strncpy(event
.name
, (const char *) content
, name_len
);
1445 } else if (!strcmp((const char *) node
->name
,
1446 config_element_enabled
)) {
1447 xmlChar
*content
= xmlNodeGetContent(node
);
1451 ret
= -LTTNG_ERR_NOMEM
;
1455 ret
= parse_bool(content
, &event
.enabled
);
1458 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1461 } else if (!strcmp((const char *) node
->name
,
1462 config_element_type
)) {
1463 xmlChar
*content
= xmlNodeGetContent(node
);
1467 ret
= -LTTNG_ERR_NOMEM
;
1471 ret
= get_event_type(content
);
1474 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1479 } else if (!strcmp((const char *) node
->name
,
1480 config_element_loglevel_type
)) {
1481 xmlChar
*content
= xmlNodeGetContent(node
);
1485 ret
= -LTTNG_ERR_NOMEM
;
1489 ret
= get_loglevel_type(content
);
1492 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1496 event
.loglevel_type
= ret
;
1497 } else if (!strcmp((const char *) node
->name
,
1498 config_element_loglevel
)) {
1500 int64_t loglevel
= 0;
1503 content
= xmlNodeGetContent(node
);
1505 ret
= -LTTNG_ERR_NOMEM
;
1509 ret
= parse_int(content
, &loglevel
);
1512 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1516 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1517 WARN("loglevel out of range.");
1518 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1522 event
.loglevel
= loglevel
;
1523 } else if (!strcmp((const char *) node
->name
,
1524 config_element_filter
)) {
1526 xmlNodeGetContent(node
);
1530 ret
= -LTTNG_ERR_NOMEM
;
1534 filter_expression
= strdup((char *) content
);
1536 if (!filter_expression
) {
1537 ret
= -LTTNG_ERR_NOMEM
;
1540 } else if (!strcmp((const char *) node
->name
,
1541 config_element_exclusions
)) {
1542 xmlNodePtr exclusion_node
;
1543 int exclusion_index
= 0;
1548 * Exclusions has already been initialized,
1551 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1555 exclusion_count
= xmlChildElementCount(node
);
1556 if (!exclusion_count
) {
1560 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1562 exclusion_count
= 0;
1563 ret
= -LTTNG_ERR_NOMEM
;
1567 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1568 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1570 xmlNodeGetContent(exclusion_node
);
1573 ret
= -LTTNG_ERR_NOMEM
;
1577 exclusions
[exclusion_index
] = strdup((const char *) content
);
1579 if (!exclusions
[exclusion_index
]) {
1580 ret
= -LTTNG_ERR_NOMEM
;
1586 event
.exclusion
= 1;
1587 } else if (!strcmp((const char *) node
->name
,
1588 config_element_attributes
)) {
1589 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1592 if (!attribute_node
) {
1593 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1597 if (!strcmp((const char *) node
->name
,
1598 config_element_probe_attributes
)) {
1599 xmlNodePtr probe_attribute_node
;
1601 /* probe_attributes */
1602 for (probe_attribute_node
=
1603 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1604 probe_attribute_node
= xmlNextElementSibling(
1605 probe_attribute_node
)) {
1607 ret
= process_probe_attribute_node(probe_attribute_node
,
1616 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1618 /* function_attributes */
1619 content
= xmlNodeGetContent(symbol_node
);
1621 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1625 sym_len
= strlen((char *) content
);
1626 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1627 WARN("Function name too long.");
1628 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1633 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1640 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1641 filter_expression
, exclusion_count
, exclusions
);
1643 for (i
= 0; i
< exclusion_count
; i
++) {
1644 free(exclusions
[i
]);
1648 free(filter_expression
);
1653 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1654 const char *channel_name
)
1659 assert(events_node
);
1661 assert(channel_name
);
1663 for (node
= xmlFirstElementChild(events_node
); node
;
1664 node
= xmlNextElementSibling(node
)) {
1665 ret
= process_event_node(node
, handle
, channel_name
);
1675 int process_channel_attr_node(xmlNodePtr attr_node
,
1676 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1677 xmlNodePtr
*events_node
)
1683 assert(contexts_node
);
1684 assert(events_node
);
1686 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1691 content
= xmlNodeGetContent(attr_node
);
1693 ret
= -LTTNG_ERR_NOMEM
;
1697 name_len
= strlen((char *) content
);
1698 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1699 WARN("Channel name too long.");
1700 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1705 strncpy(channel
->name
, (const char *) content
, name_len
);
1707 } else if (!strcmp((const char *) attr_node
->name
,
1708 config_element_enabled
)) {
1713 content
= xmlNodeGetContent(attr_node
);
1715 ret
= -LTTNG_ERR_NOMEM
;
1719 ret
= parse_bool(content
, &enabled
);
1722 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1726 channel
->enabled
= enabled
;
1727 } else if (!strcmp((const char *) attr_node
->name
,
1728 config_element_overwrite_mode
)) {
1731 /* overwrite_mode */
1732 content
= xmlNodeGetContent(attr_node
);
1734 ret
= -LTTNG_ERR_NOMEM
;
1738 ret
= get_overwrite_mode(content
);
1741 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1745 channel
->attr
.overwrite
= ret
;
1746 } else if (!strcmp((const char *) attr_node
->name
,
1747 config_element_subbuf_size
)) {
1750 /* subbuffer_size */
1751 content
= xmlNodeGetContent(attr_node
);
1753 ret
= -LTTNG_ERR_NOMEM
;
1757 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1760 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1763 } else if (!strcmp((const char *) attr_node
->name
,
1764 config_element_num_subbuf
)) {
1767 /* subbuffer_count */
1768 content
= xmlNodeGetContent(attr_node
);
1770 ret
= -LTTNG_ERR_NOMEM
;
1774 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1777 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1780 } else if (!strcmp((const char *) attr_node
->name
,
1781 config_element_switch_timer_interval
)) {
1783 uint64_t switch_timer_interval
= 0;
1785 /* switch_timer_interval */
1786 content
= xmlNodeGetContent(attr_node
);
1788 ret
= -LTTNG_ERR_NOMEM
;
1792 ret
= parse_uint(content
, &switch_timer_interval
);
1795 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1799 if (switch_timer_interval
> UINT_MAX
) {
1800 WARN("switch_timer_interval out of range.");
1801 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1805 channel
->attr
.switch_timer_interval
=
1806 switch_timer_interval
;
1807 } else if (!strcmp((const char *) attr_node
->name
,
1808 config_element_read_timer_interval
)) {
1810 uint64_t read_timer_interval
= 0;
1812 /* read_timer_interval */
1813 content
= xmlNodeGetContent(attr_node
);
1815 ret
= -LTTNG_ERR_NOMEM
;
1819 ret
= parse_uint(content
, &read_timer_interval
);
1822 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1826 if (read_timer_interval
> UINT_MAX
) {
1827 WARN("read_timer_interval out of range.");
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1832 channel
->attr
.read_timer_interval
=
1833 read_timer_interval
;
1834 } else if (!strcmp((const char *) attr_node
->name
,
1835 config_element_output_type
)) {
1839 content
= xmlNodeGetContent(attr_node
);
1841 ret
= -LTTNG_ERR_NOMEM
;
1845 ret
= get_output_type(content
);
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 channel
->attr
.output
= ret
;
1853 } else if (!strcmp((const char *) attr_node
->name
,
1854 config_element_tracefile_size
)) {
1857 /* tracefile_size */
1858 content
= xmlNodeGetContent(attr_node
);
1860 ret
= -LTTNG_ERR_NOMEM
;
1864 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1867 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1870 } else if (!strcmp((const char *) attr_node
->name
,
1871 config_element_tracefile_count
)) {
1874 /* tracefile_count */
1875 content
= xmlNodeGetContent(attr_node
);
1877 ret
= -LTTNG_ERR_NOMEM
;
1881 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1884 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1887 } else if (!strcmp((const char *) attr_node
->name
,
1888 config_element_live_timer_interval
)) {
1890 uint64_t live_timer_interval
= 0;
1892 /* live_timer_interval */
1893 content
= xmlNodeGetContent(attr_node
);
1895 ret
= -LTTNG_ERR_NOMEM
;
1899 ret
= parse_uint(content
, &live_timer_interval
);
1902 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1906 if (live_timer_interval
> UINT_MAX
) {
1907 WARN("live_timer_interval out of range.");
1908 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1912 channel
->attr
.live_timer_interval
=
1913 live_timer_interval
;
1914 } else if (!strcmp((const char *) attr_node
->name
,
1915 config_element_events
)) {
1917 *events_node
= attr_node
;
1920 *contexts_node
= attr_node
;
1928 int process_context_node(xmlNodePtr context_node
,
1929 struct lttng_handle
*handle
, const char *channel_name
)
1932 struct lttng_event_context context
;
1933 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1936 assert(channel_name
);
1938 if (!context_child_node
) {
1939 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1943 memset(&context
, 0, sizeof(context
));
1945 if (!strcmp((const char *) context_child_node
->name
,
1946 config_element_type
)) {
1948 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1950 ret
= -LTTNG_ERR_NOMEM
;
1954 ret
= get_context_type(content
);
1957 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1963 xmlNodePtr perf_attr_node
;
1966 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1967 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1968 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1969 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1970 perf_attr_node
; perf_attr_node
=
1971 xmlNextElementSibling(perf_attr_node
)) {
1972 if (!strcmp((const char *) perf_attr_node
->name
,
1973 config_element_type
)) {
1978 content
= xmlNodeGetContent(perf_attr_node
);
1980 ret
= -LTTNG_ERR_NOMEM
;
1984 ret
= parse_uint(content
, &type
);
1987 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1991 if (type
> UINT32_MAX
) {
1992 WARN("perf context type out of range.");
1993 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1997 context
.u
.perf_counter
.type
= type
;
1998 } else if (!strcmp((const char *) perf_attr_node
->name
,
1999 config_element_config
)) {
2001 uint64_t config
= 0;
2004 content
= xmlNodeGetContent(perf_attr_node
);
2006 ret
= -LTTNG_ERR_NOMEM
;
2010 ret
= parse_uint(content
, &config
);
2013 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2017 context
.u
.perf_counter
.config
= config
;
2018 } else if (!strcmp((const char *) perf_attr_node
->name
,
2019 config_element_name
)) {
2024 content
= xmlNodeGetContent(perf_attr_node
);
2026 ret
= -LTTNG_ERR_NOMEM
;
2030 name_len
= strlen((char *) content
);
2031 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2032 WARN("perf context name too long.");
2033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2038 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2045 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2051 int process_contexts_node(xmlNodePtr contexts_node
,
2052 struct lttng_handle
*handle
, const char *channel_name
)
2055 xmlNodePtr context_node
;
2057 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2058 context_node
= xmlNextElementSibling(context_node
)) {
2059 ret
= process_context_node(context_node
, handle
, channel_name
);
2069 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2072 struct lttng_domain domain
= { 0 };
2073 struct lttng_handle
*handle
= NULL
;
2074 xmlNodePtr channels_node
= NULL
;
2077 assert(session_name
);
2079 ret
= init_domain(domain_node
, &domain
);
2084 handle
= lttng_create_handle(session_name
, &domain
);
2086 ret
= -LTTNG_ERR_NOMEM
;
2090 /* get the channels node */
2091 for (node
= xmlFirstElementChild(domain_node
); node
;
2092 node
= xmlNextElementSibling(node
)) {
2093 if (!strcmp((const char *) node
->name
,
2094 config_element_channels
)) {
2095 channels_node
= node
;
2100 if (!channels_node
) {
2104 /* create all channels */
2105 for (node
= xmlFirstElementChild(channels_node
); node
;
2106 node
= xmlNextElementSibling(node
)) {
2107 struct lttng_channel channel
;
2108 xmlNodePtr contexts_node
= NULL
;
2109 xmlNodePtr events_node
= NULL
;
2110 xmlNodePtr channel_attr_node
;
2112 memset(&channel
, 0, sizeof(channel
));
2113 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2115 for (channel_attr_node
= xmlFirstElementChild(node
);
2116 channel_attr_node
; channel_attr_node
=
2117 xmlNextElementSibling(channel_attr_node
)) {
2118 ret
= process_channel_attr_node(channel_attr_node
,
2119 &channel
, &contexts_node
, &events_node
);
2125 ret
= lttng_enable_channel(handle
, &channel
);
2130 ret
= process_events_node(events_node
, handle
, channel
.name
);
2135 ret
= process_contexts_node(contexts_node
, handle
,
2142 lttng_destroy_handle(handle
);
2147 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2150 int ret
, started
= -1, snapshot_mode
= -1;
2151 uint64_t live_timer_interval
= UINT64_MAX
;
2153 xmlNodePtr domains_node
= NULL
;
2154 xmlNodePtr output_node
= NULL
;
2156 struct lttng_domain
*kernel_domain
= NULL
;
2157 struct lttng_domain
*ust_domain
= NULL
;
2158 struct lttng_domain
*jul_domain
= NULL
;
2159 struct lttng_domain
*log4j_domain
= NULL
;
2160 struct lttng_domain
*python_domain
= NULL
;
2162 for (node
= xmlFirstElementChild(session_node
); node
;
2163 node
= xmlNextElementSibling(node
)) {
2164 if (!name
&& !strcmp((const char *) node
->name
,
2165 config_element_name
)) {
2167 xmlChar
*node_content
= xmlNodeGetContent(node
);
2168 if (!node_content
) {
2169 ret
= -LTTNG_ERR_NOMEM
;
2173 name
= (char *) node_content
;
2174 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2175 config_element_domains
)) {
2177 domains_node
= node
;
2178 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2179 config_element_started
)) {
2181 xmlChar
*node_content
= xmlNodeGetContent(node
);
2182 if (!node_content
) {
2183 ret
= -LTTNG_ERR_NOMEM
;
2187 ret
= parse_bool(node_content
, &started
);
2190 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2193 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2194 config_element_output
)) {
2198 /* attributes, snapshot_mode or live_timer_interval */
2199 xmlNodePtr attributes_child
=
2200 xmlFirstElementChild(node
);
2202 if (!strcmp((const char *) attributes_child
->name
,
2203 config_element_snapshot_mode
)) {
2205 xmlChar
*snapshot_mode_content
=
2206 xmlNodeGetContent(attributes_child
);
2207 if (!snapshot_mode_content
) {
2208 ret
= -LTTNG_ERR_NOMEM
;
2212 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2213 free(snapshot_mode_content
);
2215 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2219 /* live_timer_interval */
2220 xmlChar
*timer_interval_content
=
2221 xmlNodeGetContent(attributes_child
);
2222 if (!timer_interval_content
) {
2223 ret
= -LTTNG_ERR_NOMEM
;
2227 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2228 free(timer_interval_content
);
2230 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2238 /* Mandatory attribute, as defined in the session XSD */
2239 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2243 if (session_name
&& strcmp(name
, session_name
)) {
2244 /* This is not the session we are looking for */
2245 ret
= -LTTNG_ERR_NO_SESSION
;
2249 /* Init domains to create the session handles */
2250 for (node
= xmlFirstElementChild(domains_node
); node
;
2251 node
= xmlNextElementSibling(node
)) {
2252 struct lttng_domain
*domain
;
2254 domain
= zmalloc(sizeof(*domain
));
2256 ret
= -LTTNG_ERR_NOMEM
;
2260 ret
= init_domain(node
, domain
);
2262 goto domain_init_error
;
2265 switch (domain
->type
) {
2266 case LTTNG_DOMAIN_KERNEL
:
2267 if (kernel_domain
) {
2268 /* Same domain seen twice, invalid! */
2269 goto domain_init_error
;
2271 kernel_domain
= domain
;
2273 case LTTNG_DOMAIN_UST
:
2275 /* Same domain seen twice, invalid! */
2276 goto domain_init_error
;
2278 ust_domain
= domain
;
2280 case LTTNG_DOMAIN_JUL
:
2282 /* Same domain seen twice, invalid! */
2283 goto domain_init_error
;
2285 jul_domain
= domain
;
2287 case LTTNG_DOMAIN_LOG4J
:
2289 /* Same domain seen twice, invalid! */
2290 goto domain_init_error
;
2292 log4j_domain
= domain
;
2294 case LTTNG_DOMAIN_PYTHON
:
2295 if (python_domain
) {
2296 /* Same domain seen twice, invalid! */
2297 goto domain_init_error
;
2299 python_domain
= domain
;
2302 WARN("Invalid domain type");
2303 goto domain_init_error
;
2308 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2313 /* Destroy session if it exists */
2314 ret
= lttng_destroy_session(name
);
2315 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2316 ERR("Failed to destroy existing session.");
2321 /* Create session type depending on output type */
2322 if (snapshot_mode
&& snapshot_mode
!= -1) {
2323 ret
= create_snapshot_session(name
, output_node
);
2324 } else if (live_timer_interval
&&
2325 live_timer_interval
!= UINT64_MAX
) {
2326 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2327 log4j_domain
, output_node
, live_timer_interval
);
2329 /* regular session */
2330 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2331 log4j_domain
, output_node
, UINT64_MAX
);
2337 for (node
= xmlFirstElementChild(domains_node
); node
;
2338 node
= xmlNextElementSibling(node
)) {
2339 ret
= process_domain_node(node
, name
);
2346 ret
= lttng_start_tracing(name
);
2354 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2355 lttng_destroy_session(name
);
2359 free(kernel_domain
);
2368 * Return 1 if the given path is readable by the current UID or 0 if not.
2369 * Return -1 if the path is EPERM.
2371 static int validate_file_read_creds(const char *path
)
2377 /* Can we read the file. */
2378 ret
= access(path
, R_OK
);
2382 if (errno
== EACCES
) {
2393 int load_session_from_file(const char *path
, const char *session_name
,
2394 struct session_config_validation_ctx
*validation_ctx
, int override
)
2396 int ret
, session_found
= !session_name
;
2397 xmlDocPtr doc
= NULL
;
2398 xmlNodePtr sessions_node
;
2399 xmlNodePtr session_node
;
2402 assert(validation_ctx
);
2404 ret
= validate_file_read_creds(path
);
2407 ret
= -LTTNG_ERR_EPERM
;
2409 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2414 doc
= xmlParseFile(path
);
2416 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2420 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2422 ERR("Session configuration file validation failed");
2423 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2427 sessions_node
= xmlDocGetRootElement(doc
);
2428 if (!sessions_node
) {
2432 for (session_node
= xmlFirstElementChild(sessions_node
);
2433 session_node
; session_node
=
2434 xmlNextElementSibling(session_node
)) {
2435 ret
= process_session_node(session_node
,
2436 session_name
, override
);
2437 if (session_name
&& ret
== 0) {
2438 /* Target session found and loaded */
2446 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2452 int load_session_from_path(const char *path
, const char *session_name
,
2453 struct session_config_validation_ctx
*validation_ctx
, int override
)
2455 int ret
, session_found
= !session_name
;
2456 DIR *directory
= NULL
;
2459 assert(validation_ctx
);
2461 directory
= opendir(path
);
2465 /* Try the file loading. */
2468 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2471 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2476 struct dirent
*entry
;
2477 struct dirent
*result
;
2478 char *file_path
= NULL
;
2479 size_t path_len
= strlen(path
);
2481 if (path_len
>= PATH_MAX
) {
2482 ret
= -LTTNG_ERR_INVALID
;
2486 entry
= zmalloc(sizeof(*entry
));
2488 ret
= -LTTNG_ERR_NOMEM
;
2492 file_path
= zmalloc(PATH_MAX
);
2494 ret
= -LTTNG_ERR_NOMEM
;
2499 strncpy(file_path
, path
, path_len
);
2500 if (file_path
[path_len
- 1] != '/') {
2501 file_path
[path_len
++] = '/';
2505 /* Search for *.lttng files */
2506 while (!readdir_r(directory
, entry
, &result
) && result
) {
2507 size_t file_name_len
= strlen(result
->d_name
);
2509 if (file_name_len
<=
2510 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2514 if (path_len
+ file_name_len
>= PATH_MAX
) {
2518 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2519 result
->d_name
+ file_name_len
- sizeof(
2520 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2524 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2525 file_path
[path_len
+ file_name_len
] = '\0';
2527 ret
= load_session_from_file(file_path
, session_name
,
2528 validation_ctx
, override
);
2529 if (session_name
&& !ret
) {
2538 ret
= load_session_from_file(path
, session_name
,
2539 validation_ctx
, override
);
2549 if (closedir(directory
)) {
2554 if (!session_found
) {
2555 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2562 * Validate that the given path's credentials and the current process have the
2563 * same UID. If so, return 1 else return 0 if it does NOT match.
2565 static int validate_path_creds(const char *path
)
2567 int ret
, uid
= getuid();
2576 ret
= stat(path
, &buf
);
2578 if (errno
!= ENOENT
) {
2581 ret
= -LTTNG_ERR_INVALID
;
2585 if (buf
.st_uid
!= uid
) {
2596 int config_load_session(const char *path
, const char *session_name
,
2597 int override
, unsigned int autoload
)
2600 const char *path_ptr
= NULL
;
2601 struct session_config_validation_ctx validation_ctx
= { 0 };
2603 ret
= init_session_config_validation_ctx(&validation_ctx
);
2610 const char *sys_path
;
2613 home_path
= utils_get_home_dir();
2615 char path
[PATH_MAX
];
2618 * Try user session configuration path. Ignore error here so we can
2619 * continue loading the system wide sessions.
2622 ret
= snprintf(path
, sizeof(path
),
2623 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2624 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2626 PERROR("snprintf session autoload home config path");
2631 * Credentials are only validated for the autoload in order to
2632 * avoid any user session daemon to try to load kernel sessions
2633 * automatically and failing all the times.
2635 ret
= validate_path_creds(path
);
2640 ret
= snprintf(path
, sizeof(path
),
2641 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2643 PERROR("snprintf session home config path");
2649 ret
= load_session_from_path(path_ptr
, session_name
,
2650 &validation_ctx
, override
);
2651 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2655 * Continue even if the session was found since we have to try
2656 * the system wide sessions.
2661 /* Reset path pointer for the system wide dir. */
2664 /* Try system wide configuration directory. */
2666 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2667 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2668 ret
= validate_path_creds(sys_path
);
2670 path_ptr
= sys_path
;
2673 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2674 path_ptr
= sys_path
;
2678 ret
= load_session_from_path(path_ptr
, session_name
,
2679 &validation_ctx
, override
);
2682 ret
= access(path
, F_OK
);
2687 ret
= -LTTNG_ERR_INVALID
;
2688 WARN("Session configuration path does not exist.");
2691 ret
= -LTTNG_ERR_EPERM
;
2694 ret
= -LTTNG_ERR_UNK
;
2700 ret
= load_session_from_path(path
, session_name
,
2701 &validation_ctx
, override
);
2704 fini_session_config_validation_ctx(&validation_ctx
);
2705 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2707 * Don't report an error if no sessions are found when called
2708 * without a session_name or a search path.