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.
28 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <lttng/lttng-error.h>
36 #include <libxml/parser.h>
37 #include <libxml/valid.h>
38 #include <libxml/xmlschemas.h>
39 #include <libxml/tree.h>
40 #include <lttng/lttng.h>
41 #include <lttng/snapshot.h>
44 #include "config-internal.h"
46 struct handler_filter_args
{
48 config_entry_handler_cb handler
;
52 struct session_config_validation_ctx
{
53 xmlSchemaParserCtxtPtr parser_ctx
;
55 xmlSchemaValidCtxtPtr schema_validation_ctx
;
58 const char * const config_str_yes
= "yes";
59 const char * const config_str_true
= "true";
60 const char * const config_str_on
= "on";
61 const char * const config_str_no
= "no";
62 const char * const config_str_false
= "false";
63 const char * const config_str_off
= "off";
64 const char * const config_xml_encoding
= "UTF-8";
65 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
66 const char * const config_xml_indent_string
= "\t";
67 const char * const config_xml_true
= "true";
68 const char * const config_xml_false
= "false";
70 const char * const config_element_channel
= "channel";
71 const char * const config_element_channels
= "channels";
72 const char * const config_element_domain
= "domain";
73 const char * const config_element_domains
= "domains";
74 const char * const config_element_event
= "event";
75 const char * const config_element_events
= "events";
76 const char * const config_element_context
= "context";
77 const char * const config_element_contexts
= "contexts";
78 const char * const config_element_attributes
= "attributes";
79 const char * const config_element_exclusion
= "exclusion";
80 const char * const config_element_exclusions
= "exclusions";
81 const char * const config_element_function_attributes
= "function_attributes";
82 const char * const config_element_probe_attributes
= "probe_attributes";
83 const char * const config_element_symbol_name
= "symbol_name";
84 const char * const config_element_address
= "address";
85 const char * const config_element_offset
= "offset";
86 const char * const config_element_name
= "name";
87 const char * const config_element_enabled
= "enabled";
88 const char * const config_element_overwrite_mode
= "overwrite_mode";
89 const char * const config_element_subbuf_size
= "subbuffer_size";
90 const char * const config_element_num_subbuf
= "subbuffer_count";
91 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
92 const char * const config_element_read_timer_interval
= "read_timer_interval";
93 const char * const config_element_output
= "output";
94 const char * const config_element_output_type
= "output_type";
95 const char * const config_element_tracefile_size
= "tracefile_size";
96 const char * const config_element_tracefile_count
= "tracefile_count";
97 const char * const config_element_live_timer_interval
= "live_timer_interval";
98 const char * const config_element_type
= "type";
99 const char * const config_element_buffer_type
= "buffer_type";
100 const char * const config_element_session
= "session";
101 const char * const config_element_sessions
= "sessions";
102 const char * const config_element_perf
= "perf";
103 const char * const config_element_config
= "config";
104 const char * const config_element_started
= "started";
105 const char * const config_element_snapshot_mode
= "snapshot_mode";
106 const char * const config_element_loglevel
= "loglevel";
107 const char * const config_element_loglevel_type
= "loglevel_type";
108 const char * const config_element_filter
= "filter";
109 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
110 const char * const config_element_consumer_output
= "consumer_output";
111 const char * const config_element_destination
= "destination";
112 const char * const config_element_path
= "path";
113 const char * const config_element_net_output
= "net_output";
114 const char * const config_element_control_uri
= "control_uri";
115 const char * const config_element_data_uri
= "data_uri";
116 const char * const config_element_max_size
= "max_size";
118 const char * const config_domain_type_kernel
= "KERNEL";
119 const char * const config_domain_type_ust
= "UST";
120 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_domain_type_log4j
= "LOG4J";
122 const char * const config_domain_type_python
= "PYTHON";
124 const char * const config_buffer_type_per_pid
= "PER_PID";
125 const char * const config_buffer_type_per_uid
= "PER_UID";
126 const char * const config_buffer_type_global
= "GLOBAL";
128 const char * const config_overwrite_mode_discard
= "DISCARD";
129 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
131 const char * const config_output_type_splice
= "SPLICE";
132 const char * const config_output_type_mmap
= "MMAP";
134 const char * const config_loglevel_type_all
= "ALL";
135 const char * const config_loglevel_type_range
= "RANGE";
136 const char * const config_loglevel_type_single
= "SINGLE";
138 const char * const config_event_type_all
= "ALL";
139 const char * const config_event_type_tracepoint
= "TRACEPOINT";
140 const char * const config_event_type_probe
= "PROBE";
141 const char * const config_event_type_function
= "FUNCTION";
142 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
143 const char * const config_event_type_noop
= "NOOP";
144 const char * const config_event_type_syscall
= "SYSCALL";
145 const char * const config_event_type_kprobe
= "KPROBE";
146 const char * const config_event_type_kretprobe
= "KRETPROBE";
148 const char * const config_event_context_pid
= "PID";
149 const char * const config_event_context_procname
= "PROCNAME";
150 const char * const config_event_context_prio
= "PRIO";
151 const char * const config_event_context_nice
= "NICE";
152 const char * const config_event_context_vpid
= "VPID";
153 const char * const config_event_context_tid
= "TID";
154 const char * const config_event_context_vtid
= "VTID";
155 const char * const config_event_context_ppid
= "PPID";
156 const char * const config_event_context_vppid
= "VPPID";
157 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
158 const char * const config_event_context_hostname
= "HOSTNAME";
159 const char * const config_event_context_ip
= "IP";
160 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
162 struct consumer_output
{
169 static int config_entry_handler_filter(struct handler_filter_args
*args
,
170 const char *section
, const char *name
, const char *value
)
173 struct config_entry entry
= { section
, name
, value
};
177 if (!section
|| !name
|| !value
) {
183 if (strcmp(args
->section
, section
)) {
188 ret
= args
->handler(&entry
, args
->user_data
);
194 int config_get_section_entries(const char *override_path
, const char *section
,
195 config_entry_handler_cb handler
, void *user_data
)
199 FILE *config_file
= NULL
;
200 struct handler_filter_args filter
= { section
, handler
, user_data
};
202 /* First, try system-wide conf. file. */
203 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
205 config_file
= fopen(path
, "r");
207 DBG("Loading daemon conf file at %s", path
);
209 * Return value is not very important here since error or not, we
210 * continue and try the next possible conf. file.
212 (void) ini_parse_file(config_file
,
213 (ini_entry_handler
) config_entry_handler_filter
,
218 /* Second is the user local configuration. */
219 path
= utils_get_home_dir();
221 char fullpath
[PATH_MAX
];
223 ret
= snprintf(fullpath
, sizeof(fullpath
),
224 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
226 PERROR("snprintf user conf. path");
230 config_file
= fopen(fullpath
, "r");
232 DBG("Loading daemon user conf file at %s", path
);
234 * Return value is not very important here since error or not, we
235 * continue and try the next possible conf. file.
237 (void) ini_parse_file(config_file
,
238 (ini_entry_handler
) config_entry_handler_filter
,
244 /* Final path is the one that the user might have provided. */
246 config_file
= fopen(override_path
, "r");
248 DBG("Loading daemon command line conf file at %s", override_path
);
249 (void) ini_parse_file(config_file
,
250 (ini_entry_handler
) config_entry_handler_filter
,
254 ERR("Failed to open daemon configuration file at %s",
261 /* Everything went well. */
269 int config_parse_value(const char *value
)
272 char *endptr
, *lower_str
;
282 v
= strtoul(value
, &endptr
, 10);
283 if (endptr
!= value
) {
288 lower_str
= zmalloc(len
+ 1);
295 for (i
= 0; i
< len
; i
++) {
296 lower_str
[i
] = tolower(value
[i
]);
299 if (!strcmp(lower_str
, config_str_yes
) ||
300 !strcmp(lower_str
, config_str_true
) ||
301 !strcmp(lower_str
, config_str_on
)) {
303 } else if (!strcmp(lower_str
, config_str_no
) ||
304 !strcmp(lower_str
, config_str_false
) ||
305 !strcmp(lower_str
, config_str_off
)) {
317 * Returns a xmlChar string which must be released using xmlFree().
319 static xmlChar
*encode_string(const char *in_str
)
321 xmlChar
*out_str
= NULL
;
322 xmlCharEncodingHandlerPtr handler
;
323 int out_len
, ret
, in_len
;
327 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
329 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
333 in_len
= strlen(in_str
);
335 * Add 1 byte for the NULL terminted character. The factor 2 here is
336 * because UTF-8 can be on two bytes so this fits the worst case for each
339 out_len
= (in_len
* 2) + 1;
340 out_str
= xmlMalloc(out_len
);
345 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
352 /* out_len is now the size of out_str */
353 out_str
[out_len
] = '\0';
359 struct config_writer
*config_writer_create(int fd_output
, int indent
)
362 struct config_writer
*writer
;
363 xmlOutputBufferPtr buffer
;
365 writer
= zmalloc(sizeof(struct config_writer
));
367 PERROR("zmalloc config_writer_create");
371 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
376 writer
->writer
= xmlNewTextWriter(buffer
);
377 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
378 config_xml_encoding
, NULL
);
383 ret
= xmlTextWriterSetIndentString(writer
->writer
,
384 BAD_CAST config_xml_indent_string
);
389 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
397 config_writer_destroy(writer
);
402 int config_writer_destroy(struct config_writer
*writer
)
411 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
412 WARN("Could not close XML document");
416 if (writer
->writer
) {
417 xmlFreeTextWriter(writer
->writer
);
426 int config_writer_open_element(struct config_writer
*writer
,
427 const char *element_name
)
430 xmlChar
*encoded_element_name
;
432 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
437 encoded_element_name
= encode_string(element_name
);
438 if (!encoded_element_name
) {
443 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
444 xmlFree(encoded_element_name
);
446 return ret
> 0 ? 0 : ret
;
450 int config_writer_close_element(struct config_writer
*writer
)
454 if (!writer
|| !writer
->writer
) {
459 ret
= xmlTextWriterEndElement(writer
->writer
);
461 return ret
> 0 ? 0 : ret
;
465 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
466 const char *element_name
, uint64_t value
)
469 xmlChar
*encoded_element_name
;
471 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
476 encoded_element_name
= encode_string(element_name
);
477 if (!encoded_element_name
) {
482 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
483 encoded_element_name
, "%" PRIu64
, value
);
484 xmlFree(encoded_element_name
);
486 return ret
> 0 ? 0 : ret
;
490 int config_writer_write_element_signed_int(struct config_writer
*writer
,
491 const char *element_name
, int64_t value
)
494 xmlChar
*encoded_element_name
;
496 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
501 encoded_element_name
= encode_string(element_name
);
502 if (!encoded_element_name
) {
507 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
508 encoded_element_name
, "%" PRIi64
, value
);
509 xmlFree(encoded_element_name
);
511 return ret
> 0 ? 0 : ret
;
515 int config_writer_write_element_bool(struct config_writer
*writer
,
516 const char *element_name
, int value
)
518 return config_writer_write_element_string(writer
, element_name
,
519 value
? config_xml_true
: config_xml_false
);
523 int config_writer_write_element_string(struct config_writer
*writer
,
524 const char *element_name
, const char *value
)
527 xmlChar
*encoded_element_name
= NULL
;
528 xmlChar
*encoded_value
= NULL
;
530 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
536 encoded_element_name
= encode_string(element_name
);
537 if (!encoded_element_name
) {
542 encoded_value
= encode_string(value
);
543 if (!encoded_value
) {
548 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
551 xmlFree(encoded_element_name
);
552 xmlFree(encoded_value
);
553 return ret
> 0 ? 0 : ret
;
557 void xml_error_handler(void *ctx
, const char *format
, ...)
563 va_start(args
, format
);
564 ret
= vasprintf(&errMsg
, format
, args
);
567 ERR("String allocation failed in xml error handler");
571 fprintf(stderr
, "XML Error: %s", errMsg
);
576 void fini_session_config_validation_ctx(
577 struct session_config_validation_ctx
*ctx
)
579 if (ctx
->parser_ctx
) {
580 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
584 xmlSchemaFree(ctx
->schema
);
587 if (ctx
->schema_validation_ctx
) {
588 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
591 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
595 char *get_session_config_xsd_path()
598 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
599 size_t base_path_len
;
603 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
606 base_path_len
= strlen(base_path
);
607 max_path_len
= base_path_len
+
608 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
609 xsd_path
= zmalloc(max_path_len
);
614 strncpy(xsd_path
, base_path
, max_path_len
);
615 if (xsd_path
[base_path_len
- 1] != '/') {
616 xsd_path
[base_path_len
++] = '/';
619 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
620 max_path_len
- base_path_len
);
626 int init_session_config_validation_ctx(
627 struct session_config_validation_ctx
*ctx
)
630 char *xsd_path
= get_session_config_xsd_path();
633 ret
= -LTTNG_ERR_NOMEM
;
637 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
638 if (!ctx
->parser_ctx
) {
639 ERR("XSD parser context creation failed");
640 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
643 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
644 xml_error_handler
, NULL
);
646 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
648 ERR("XSD parsing failed");
649 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
653 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
654 if (!ctx
->schema_validation_ctx
) {
655 ERR("XSD validation context creation failed");
656 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
660 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
661 xml_error_handler
, NULL
);
666 fini_session_config_validation_ctx(ctx
);
674 int parse_uint(xmlChar
*str
, uint64_t *val
)
684 *val
= strtoull((const char *) str
, &endptr
, 10);
685 if (!endptr
|| *endptr
) {
696 int parse_int(xmlChar
*str
, int64_t *val
)
706 *val
= strtoll((const char *) str
, &endptr
, 10);
707 if (!endptr
|| *endptr
) {
718 int parse_bool(xmlChar
*str
, int *val
)
727 if (!strcmp((const char *) str
, config_xml_true
)) {
729 } else if (!strcmp((const char *) str
, config_xml_false
)) {
732 WARN("Invalid boolean value encoutered (%s).",
741 int get_domain_type(xmlChar
*domain
)
749 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
750 ret
= LTTNG_DOMAIN_KERNEL
;
751 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
752 ret
= LTTNG_DOMAIN_UST
;
753 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
754 ret
= LTTNG_DOMAIN_JUL
;
755 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
756 ret
= LTTNG_DOMAIN_LOG4J
;
757 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
758 ret
= LTTNG_DOMAIN_PYTHON
;
769 int get_buffer_type(xmlChar
*buffer_type
)
777 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
778 ret
= LTTNG_BUFFER_GLOBAL
;
779 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
780 ret
= LTTNG_BUFFER_PER_UID
;
781 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
782 ret
= LTTNG_BUFFER_PER_PID
;
793 int get_overwrite_mode(xmlChar
*overwrite_mode
)
797 if (!overwrite_mode
) {
801 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
803 } else if (!strcmp((char *) overwrite_mode
,
804 config_overwrite_mode_discard
)) {
816 int get_output_type(xmlChar
*output_type
)
824 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
825 ret
= LTTNG_EVENT_MMAP
;
826 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
827 ret
= LTTNG_EVENT_SPLICE
;
838 int get_event_type(xmlChar
*event_type
)
846 if (!strcmp((char *) event_type
, config_event_type_all
)) {
847 ret
= LTTNG_EVENT_ALL
;
848 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
849 ret
= LTTNG_EVENT_TRACEPOINT
;
850 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
851 ret
= LTTNG_EVENT_PROBE
;
852 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
853 ret
= LTTNG_EVENT_FUNCTION
;
854 } else if (!strcmp((char *) event_type
,
855 config_event_type_function_entry
)) {
856 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
857 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
858 ret
= LTTNG_EVENT_NOOP
;
859 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
860 ret
= LTTNG_EVENT_SYSCALL
;
871 int get_loglevel_type(xmlChar
*loglevel_type
)
875 if (!loglevel_type
) {
879 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
880 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
881 } else if (!strcmp((char *) loglevel_type
,
882 config_loglevel_type_range
)) {
883 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
884 } else if (!strcmp((char *) loglevel_type
,
885 config_loglevel_type_single
)) {
886 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
897 * Return the context type or -1 on error.
900 int get_context_type(xmlChar
*context_type
)
908 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
909 ret
= LTTNG_EVENT_CONTEXT_PID
;
910 } else if (!strcmp((char *) context_type
,
911 config_event_context_procname
)) {
912 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
913 } else if (!strcmp((char *) context_type
,
914 config_event_context_prio
)) {
915 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
916 } else if (!strcmp((char *) context_type
,
917 config_event_context_nice
)) {
918 ret
= LTTNG_EVENT_CONTEXT_NICE
;
919 } else if (!strcmp((char *) context_type
,
920 config_event_context_vpid
)) {
921 ret
= LTTNG_EVENT_CONTEXT_VPID
;
922 } else if (!strcmp((char *) context_type
,
923 config_event_context_tid
)) {
924 ret
= LTTNG_EVENT_CONTEXT_TID
;
925 } else if (!strcmp((char *) context_type
,
926 config_event_context_vtid
)) {
927 ret
= LTTNG_EVENT_CONTEXT_VTID
;
928 } else if (!strcmp((char *) context_type
,
929 config_event_context_ppid
)) {
930 ret
= LTTNG_EVENT_CONTEXT_PPID
;
931 } else if (!strcmp((char *) context_type
,
932 config_event_context_vppid
)) {
933 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
934 } else if (!strcmp((char *) context_type
,
935 config_event_context_pthread_id
)) {
936 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
937 } else if (!strcmp((char *) context_type
,
938 config_event_context_hostname
)) {
939 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
940 } else if (!strcmp((char *) context_type
,
941 config_event_context_ip
)) {
942 ret
= LTTNG_EVENT_CONTEXT_IP
;
953 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
958 for (node
= xmlFirstElementChild(domain_node
); node
;
959 node
= xmlNextElementSibling(node
)) {
960 if (!strcmp((const char *) node
->name
, config_element_type
)) {
962 xmlChar
*node_content
= xmlNodeGetContent(node
);
964 ret
= -LTTNG_ERR_NOMEM
;
968 ret
= get_domain_type(node_content
);
971 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
976 } else if (!strcmp((const char *) node
->name
,
977 config_element_buffer_type
)) {
979 xmlChar
*node_content
= xmlNodeGetContent(node
);
981 ret
= -LTTNG_ERR_NOMEM
;
985 ret
= get_buffer_type(node_content
);
988 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
992 domain
->buf_type
= ret
;
1001 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1006 for (node
= xmlFirstElementChild(net_output_node
); node
;
1007 node
= xmlNextElementSibling(node
)) {
1008 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1010 *control_uri
= (char *) xmlNodeGetContent(node
);
1011 if (!*control_uri
) {
1016 *data_uri
= (char *) xmlNodeGetContent(node
);
1023 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1027 int process_consumer_output(xmlNodePtr consumer_output_node
,
1028 struct consumer_output
*output
)
1035 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1036 node
= xmlNextElementSibling(node
)) {
1037 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1038 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1042 ret
= -LTTNG_ERR_NOMEM
;
1046 ret
= parse_bool(enabled_str
, &output
->enabled
);
1052 xmlNodePtr output_type_node
;
1055 output_type_node
= xmlFirstElementChild(node
);
1056 if (!output_type_node
) {
1057 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1061 if (!strcmp((const char *) output_type_node
->name
,
1062 config_element_path
)) {
1064 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1065 if (!output
->path
) {
1066 ret
= -LTTNG_ERR_NOMEM
;
1071 ret
= get_net_output_uris(output_type_node
,
1072 &output
->control_uri
, &output
->data_uri
);
1084 free(output
->control_uri
);
1085 free(output
->data_uri
);
1086 memset(output
, 0, sizeof(struct consumer_output
));
1092 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1093 const char *control_uri
, const char *data_uri
)
1096 struct lttng_handle
*handle
;
1097 const char *uri
= NULL
;
1102 handle
= lttng_create_handle(name
, domain
);
1104 ret
= -LTTNG_ERR_NOMEM
;
1108 if (!control_uri
|| !data_uri
) {
1109 uri
= control_uri
? control_uri
: data_uri
;
1114 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1115 lttng_destroy_handle(handle
);
1121 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1124 xmlNodePtr node
= NULL
;
1125 xmlNodePtr snapshot_output_list_node
;
1126 xmlNodePtr snapshot_output_node
;
1128 assert(session_name
);
1130 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1139 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1141 /* Parse and create snapshot outputs */
1143 for (snapshot_output_node
=
1144 xmlFirstElementChild(snapshot_output_list_node
);
1145 snapshot_output_node
; snapshot_output_node
=
1146 xmlNextElementSibling(snapshot_output_node
)) {
1148 uint64_t max_size
= UINT64_MAX
;
1149 struct consumer_output output
= { 0 };
1150 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1152 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1153 node
= xmlNextElementSibling(node
)) {
1154 if (!strcmp((const char *) node
->name
,
1155 config_element_name
)) {
1157 name
= (char *) xmlNodeGetContent(node
);
1159 ret
= -LTTNG_ERR_NOMEM
;
1160 goto error_snapshot_output
;
1162 } else if (!strcmp((const char *) node
->name
,
1163 config_element_max_size
)) {
1164 xmlChar
*content
= xmlNodeGetContent(node
);
1168 ret
= -LTTNG_ERR_NOMEM
;
1169 goto error_snapshot_output
;
1171 ret
= parse_uint(content
, &max_size
);
1174 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1175 goto error_snapshot_output
;
1178 /* consumer_output */
1179 ret
= process_consumer_output(node
, &output
);
1181 goto error_snapshot_output
;
1186 snapshot_output
= lttng_snapshot_output_create();
1187 if (!snapshot_output
) {
1188 ret
= -LTTNG_ERR_NOMEM
;
1189 goto error_snapshot_output
;
1192 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1194 goto error_snapshot_output
;
1197 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1199 goto error_snapshot_output
;
1203 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1206 goto error_snapshot_output
;
1209 if (output
.control_uri
) {
1210 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1213 goto error_snapshot_output
;
1217 if (output
.data_uri
) {
1218 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1221 goto error_snapshot_output
;
1226 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1227 error_snapshot_output
:
1230 free(output
.control_uri
);
1231 free(output
.data_uri
);
1232 lttng_snapshot_output_destroy(snapshot_output
);
1242 int create_session(const char *name
,
1243 struct lttng_domain
*kernel_domain
,
1244 struct lttng_domain
*ust_domain
,
1245 struct lttng_domain
*jul_domain
,
1246 struct lttng_domain
*log4j_domain
,
1247 xmlNodePtr output_node
,
1248 uint64_t live_timer_interval
)
1251 struct consumer_output output
= { 0 };
1252 xmlNodePtr consumer_output_node
;
1257 consumer_output_node
= xmlFirstElementChild(output_node
);
1258 if (!consumer_output_node
) {
1259 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1263 if (strcmp((const char *) consumer_output_node
->name
,
1264 config_element_consumer_output
)) {
1265 WARN("Invalid output type, expected %s node",
1266 config_element_consumer_output
);
1267 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1271 ret
= process_consumer_output(consumer_output_node
, &output
);
1277 if (live_timer_interval
!= UINT64_MAX
&&
1278 !output
.control_uri
&& !output
.data_uri
) {
1279 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1283 if (output
.control_uri
|| output
.data_uri
) {
1285 struct lttng_domain
*domain
;
1286 struct lttng_domain
*domains
[] =
1287 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1289 /* network destination */
1290 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1292 * URLs are provided for sure since the test above make sure that
1293 * with a live timer the data and control URIs are provided. So,
1294 * NULL is passed here and will be set right after.
1296 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1298 ret
= lttng_create_session(name
, NULL
);
1304 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1305 domain
= domains
[i
];
1310 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1317 /* either local output or no output */
1318 ret
= lttng_create_session(name
, output
.path
);
1325 free(output
.control_uri
);
1326 free(output
.data_uri
);
1330 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1331 struct lttng_event_probe_attr
*attr
)
1335 assert(probe_attribute_node
);
1338 if (!strcmp((const char *) probe_attribute_node
->name
,
1339 config_element_address
)) {
1344 content
= xmlNodeGetContent(probe_attribute_node
);
1346 ret
= -LTTNG_ERR_NOMEM
;
1350 ret
= parse_uint(content
, &addr
);
1353 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1358 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1359 config_element_offset
)) {
1361 uint64_t offset
= 0;
1364 content
= xmlNodeGetContent(probe_attribute_node
);
1366 ret
= -LTTNG_ERR_NOMEM
;
1370 ret
= parse_uint(content
, &offset
);
1373 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1377 attr
->offset
= offset
;
1378 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1379 config_element_symbol_name
)) {
1384 content
= xmlNodeGetContent(probe_attribute_node
);
1386 ret
= -LTTNG_ERR_NOMEM
;
1390 name_len
= strlen((char *) content
);
1391 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1392 WARN("symbol_name too long.");
1393 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1398 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1407 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1408 const char *channel_name
)
1412 struct lttng_event event
;
1413 char **exclusions
= NULL
;
1414 unsigned long exclusion_count
= 0;
1415 char *filter_expression
= NULL
;
1419 assert(channel_name
);
1421 memset(&event
, 0, sizeof(event
));
1423 for (node
= xmlFirstElementChild(event_node
); node
;
1424 node
= xmlNextElementSibling(node
)) {
1425 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1430 content
= xmlNodeGetContent(node
);
1432 ret
= -LTTNG_ERR_NOMEM
;
1436 name_len
= strlen((char *) content
);
1437 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1438 WARN("Channel name too long.");
1439 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1444 strncpy(event
.name
, (const char *) content
, name_len
);
1446 } else if (!strcmp((const char *) node
->name
,
1447 config_element_enabled
)) {
1448 xmlChar
*content
= xmlNodeGetContent(node
);
1452 ret
= -LTTNG_ERR_NOMEM
;
1456 ret
= parse_bool(content
, &event
.enabled
);
1459 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1462 } else if (!strcmp((const char *) node
->name
,
1463 config_element_type
)) {
1464 xmlChar
*content
= xmlNodeGetContent(node
);
1468 ret
= -LTTNG_ERR_NOMEM
;
1472 ret
= get_event_type(content
);
1475 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1480 } else if (!strcmp((const char *) node
->name
,
1481 config_element_loglevel_type
)) {
1482 xmlChar
*content
= xmlNodeGetContent(node
);
1486 ret
= -LTTNG_ERR_NOMEM
;
1490 ret
= get_loglevel_type(content
);
1493 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1497 event
.loglevel_type
= ret
;
1498 } else if (!strcmp((const char *) node
->name
,
1499 config_element_loglevel
)) {
1501 int64_t loglevel
= 0;
1504 content
= xmlNodeGetContent(node
);
1506 ret
= -LTTNG_ERR_NOMEM
;
1510 ret
= parse_int(content
, &loglevel
);
1513 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1517 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1518 WARN("loglevel out of range.");
1519 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1523 event
.loglevel
= loglevel
;
1524 } else if (!strcmp((const char *) node
->name
,
1525 config_element_filter
)) {
1527 xmlNodeGetContent(node
);
1531 ret
= -LTTNG_ERR_NOMEM
;
1535 filter_expression
= strdup((char *) content
);
1537 if (!filter_expression
) {
1538 ret
= -LTTNG_ERR_NOMEM
;
1541 } else if (!strcmp((const char *) node
->name
,
1542 config_element_exclusions
)) {
1543 xmlNodePtr exclusion_node
;
1544 int exclusion_index
= 0;
1549 * Exclusions has already been initialized,
1552 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 exclusion_count
= xmlChildElementCount(node
);
1557 if (!exclusion_count
) {
1561 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1563 exclusion_count
= 0;
1564 ret
= -LTTNG_ERR_NOMEM
;
1568 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1569 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1571 xmlNodeGetContent(exclusion_node
);
1574 ret
= -LTTNG_ERR_NOMEM
;
1578 exclusions
[exclusion_index
] = strdup((const char *) content
);
1580 if (!exclusions
[exclusion_index
]) {
1581 ret
= -LTTNG_ERR_NOMEM
;
1587 event
.exclusion
= 1;
1588 } else if (!strcmp((const char *) node
->name
,
1589 config_element_attributes
)) {
1590 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1593 if (!attribute_node
) {
1594 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1598 if (!strcmp((const char *) node
->name
,
1599 config_element_probe_attributes
)) {
1600 xmlNodePtr probe_attribute_node
;
1602 /* probe_attributes */
1603 for (probe_attribute_node
=
1604 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1605 probe_attribute_node
= xmlNextElementSibling(
1606 probe_attribute_node
)) {
1608 ret
= process_probe_attribute_node(probe_attribute_node
,
1617 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1619 /* function_attributes */
1620 content
= xmlNodeGetContent(symbol_node
);
1622 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1626 sym_len
= strlen((char *) content
);
1627 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1628 WARN("Function name too long.");
1629 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1634 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1641 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1642 filter_expression
, exclusion_count
, exclusions
);
1644 for (i
= 0; i
< exclusion_count
; i
++) {
1645 free(exclusions
[i
]);
1649 free(filter_expression
);
1654 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1655 const char *channel_name
)
1660 assert(events_node
);
1662 assert(channel_name
);
1664 for (node
= xmlFirstElementChild(events_node
); node
;
1665 node
= xmlNextElementSibling(node
)) {
1666 ret
= process_event_node(node
, handle
, channel_name
);
1676 int process_channel_attr_node(xmlNodePtr attr_node
,
1677 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1678 xmlNodePtr
*events_node
)
1684 assert(contexts_node
);
1685 assert(events_node
);
1687 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1692 content
= xmlNodeGetContent(attr_node
);
1694 ret
= -LTTNG_ERR_NOMEM
;
1698 name_len
= strlen((char *) content
);
1699 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1700 WARN("Channel name too long.");
1701 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1706 strncpy(channel
->name
, (const char *) content
, name_len
);
1708 } else if (!strcmp((const char *) attr_node
->name
,
1709 config_element_enabled
)) {
1714 content
= xmlNodeGetContent(attr_node
);
1716 ret
= -LTTNG_ERR_NOMEM
;
1720 ret
= parse_bool(content
, &enabled
);
1723 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1727 channel
->enabled
= enabled
;
1728 } else if (!strcmp((const char *) attr_node
->name
,
1729 config_element_overwrite_mode
)) {
1732 /* overwrite_mode */
1733 content
= xmlNodeGetContent(attr_node
);
1735 ret
= -LTTNG_ERR_NOMEM
;
1739 ret
= get_overwrite_mode(content
);
1742 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1746 channel
->attr
.overwrite
= ret
;
1747 } else if (!strcmp((const char *) attr_node
->name
,
1748 config_element_subbuf_size
)) {
1751 /* subbuffer_size */
1752 content
= xmlNodeGetContent(attr_node
);
1754 ret
= -LTTNG_ERR_NOMEM
;
1758 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1761 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1764 } else if (!strcmp((const char *) attr_node
->name
,
1765 config_element_num_subbuf
)) {
1768 /* subbuffer_count */
1769 content
= xmlNodeGetContent(attr_node
);
1771 ret
= -LTTNG_ERR_NOMEM
;
1775 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1778 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1781 } else if (!strcmp((const char *) attr_node
->name
,
1782 config_element_switch_timer_interval
)) {
1784 uint64_t switch_timer_interval
= 0;
1786 /* switch_timer_interval */
1787 content
= xmlNodeGetContent(attr_node
);
1789 ret
= -LTTNG_ERR_NOMEM
;
1793 ret
= parse_uint(content
, &switch_timer_interval
);
1796 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1800 if (switch_timer_interval
> UINT_MAX
) {
1801 WARN("switch_timer_interval out of range.");
1802 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1806 channel
->attr
.switch_timer_interval
=
1807 switch_timer_interval
;
1808 } else if (!strcmp((const char *) attr_node
->name
,
1809 config_element_read_timer_interval
)) {
1811 uint64_t read_timer_interval
= 0;
1813 /* read_timer_interval */
1814 content
= xmlNodeGetContent(attr_node
);
1816 ret
= -LTTNG_ERR_NOMEM
;
1820 ret
= parse_uint(content
, &read_timer_interval
);
1823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1827 if (read_timer_interval
> UINT_MAX
) {
1828 WARN("read_timer_interval out of range.");
1829 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1833 channel
->attr
.read_timer_interval
=
1834 read_timer_interval
;
1835 } else if (!strcmp((const char *) attr_node
->name
,
1836 config_element_output_type
)) {
1840 content
= xmlNodeGetContent(attr_node
);
1842 ret
= -LTTNG_ERR_NOMEM
;
1846 ret
= get_output_type(content
);
1849 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1853 channel
->attr
.output
= ret
;
1854 } else if (!strcmp((const char *) attr_node
->name
,
1855 config_element_tracefile_size
)) {
1858 /* tracefile_size */
1859 content
= xmlNodeGetContent(attr_node
);
1861 ret
= -LTTNG_ERR_NOMEM
;
1865 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1868 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1871 } else if (!strcmp((const char *) attr_node
->name
,
1872 config_element_tracefile_count
)) {
1875 /* tracefile_count */
1876 content
= xmlNodeGetContent(attr_node
);
1878 ret
= -LTTNG_ERR_NOMEM
;
1882 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1885 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1888 } else if (!strcmp((const char *) attr_node
->name
,
1889 config_element_live_timer_interval
)) {
1891 uint64_t live_timer_interval
= 0;
1893 /* live_timer_interval */
1894 content
= xmlNodeGetContent(attr_node
);
1896 ret
= -LTTNG_ERR_NOMEM
;
1900 ret
= parse_uint(content
, &live_timer_interval
);
1903 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1907 if (live_timer_interval
> UINT_MAX
) {
1908 WARN("live_timer_interval out of range.");
1909 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1913 channel
->attr
.live_timer_interval
=
1914 live_timer_interval
;
1915 } else if (!strcmp((const char *) attr_node
->name
,
1916 config_element_events
)) {
1918 *events_node
= attr_node
;
1921 *contexts_node
= attr_node
;
1929 int process_context_node(xmlNodePtr context_node
,
1930 struct lttng_handle
*handle
, const char *channel_name
)
1933 struct lttng_event_context context
;
1934 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1937 assert(channel_name
);
1939 if (!context_child_node
) {
1940 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1944 memset(&context
, 0, sizeof(context
));
1946 if (!strcmp((const char *) context_child_node
->name
,
1947 config_element_type
)) {
1949 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1951 ret
= -LTTNG_ERR_NOMEM
;
1955 ret
= get_context_type(content
);
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1964 xmlNodePtr perf_attr_node
;
1967 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1968 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1969 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1970 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1971 perf_attr_node
; perf_attr_node
=
1972 xmlNextElementSibling(perf_attr_node
)) {
1973 if (!strcmp((const char *) perf_attr_node
->name
,
1974 config_element_type
)) {
1979 content
= xmlNodeGetContent(perf_attr_node
);
1981 ret
= -LTTNG_ERR_NOMEM
;
1985 ret
= parse_uint(content
, &type
);
1988 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1992 if (type
> UINT32_MAX
) {
1993 WARN("perf context type out of range.");
1994 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1998 context
.u
.perf_counter
.type
= type
;
1999 } else if (!strcmp((const char *) perf_attr_node
->name
,
2000 config_element_config
)) {
2002 uint64_t config
= 0;
2005 content
= xmlNodeGetContent(perf_attr_node
);
2007 ret
= -LTTNG_ERR_NOMEM
;
2011 ret
= parse_uint(content
, &config
);
2014 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2018 context
.u
.perf_counter
.config
= config
;
2019 } else if (!strcmp((const char *) perf_attr_node
->name
,
2020 config_element_name
)) {
2025 content
= xmlNodeGetContent(perf_attr_node
);
2027 ret
= -LTTNG_ERR_NOMEM
;
2031 name_len
= strlen((char *) content
);
2032 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2033 WARN("perf context name too long.");
2034 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2039 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2046 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2052 int process_contexts_node(xmlNodePtr contexts_node
,
2053 struct lttng_handle
*handle
, const char *channel_name
)
2056 xmlNodePtr context_node
;
2058 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2059 context_node
= xmlNextElementSibling(context_node
)) {
2060 ret
= process_context_node(context_node
, handle
, channel_name
);
2070 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2073 struct lttng_domain domain
= { 0 };
2074 struct lttng_handle
*handle
= NULL
;
2075 xmlNodePtr channels_node
= NULL
;
2078 assert(session_name
);
2080 ret
= init_domain(domain_node
, &domain
);
2085 handle
= lttng_create_handle(session_name
, &domain
);
2087 ret
= -LTTNG_ERR_NOMEM
;
2091 /* get the channels node */
2092 for (node
= xmlFirstElementChild(domain_node
); node
;
2093 node
= xmlNextElementSibling(node
)) {
2094 if (!strcmp((const char *) node
->name
,
2095 config_element_channels
)) {
2096 channels_node
= node
;
2101 if (!channels_node
) {
2105 /* create all channels */
2106 for (node
= xmlFirstElementChild(channels_node
); node
;
2107 node
= xmlNextElementSibling(node
)) {
2108 struct lttng_channel channel
;
2109 xmlNodePtr contexts_node
= NULL
;
2110 xmlNodePtr events_node
= NULL
;
2111 xmlNodePtr channel_attr_node
;
2113 memset(&channel
, 0, sizeof(channel
));
2114 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2116 for (channel_attr_node
= xmlFirstElementChild(node
);
2117 channel_attr_node
; channel_attr_node
=
2118 xmlNextElementSibling(channel_attr_node
)) {
2119 ret
= process_channel_attr_node(channel_attr_node
,
2120 &channel
, &contexts_node
, &events_node
);
2126 ret
= lttng_enable_channel(handle
, &channel
);
2131 ret
= process_events_node(events_node
, handle
, channel
.name
);
2136 ret
= process_contexts_node(contexts_node
, handle
,
2143 lttng_destroy_handle(handle
);
2148 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2151 int ret
, started
= -1, snapshot_mode
= -1;
2152 uint64_t live_timer_interval
= UINT64_MAX
;
2154 xmlNodePtr domains_node
= NULL
;
2155 xmlNodePtr output_node
= NULL
;
2157 struct lttng_domain
*kernel_domain
= NULL
;
2158 struct lttng_domain
*ust_domain
= NULL
;
2159 struct lttng_domain
*jul_domain
= NULL
;
2160 struct lttng_domain
*log4j_domain
= NULL
;
2161 struct lttng_domain
*python_domain
= NULL
;
2163 for (node
= xmlFirstElementChild(session_node
); node
;
2164 node
= xmlNextElementSibling(node
)) {
2165 if (!name
&& !strcmp((const char *) node
->name
,
2166 config_element_name
)) {
2168 xmlChar
*node_content
= xmlNodeGetContent(node
);
2169 if (!node_content
) {
2170 ret
= -LTTNG_ERR_NOMEM
;
2174 name
= (char *) node_content
;
2175 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2176 config_element_domains
)) {
2178 domains_node
= node
;
2179 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2180 config_element_started
)) {
2182 xmlChar
*node_content
= xmlNodeGetContent(node
);
2183 if (!node_content
) {
2184 ret
= -LTTNG_ERR_NOMEM
;
2188 ret
= parse_bool(node_content
, &started
);
2191 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2194 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2195 config_element_output
)) {
2199 /* attributes, snapshot_mode or live_timer_interval */
2200 xmlNodePtr attributes_child
=
2201 xmlFirstElementChild(node
);
2203 if (!strcmp((const char *) attributes_child
->name
,
2204 config_element_snapshot_mode
)) {
2206 xmlChar
*snapshot_mode_content
=
2207 xmlNodeGetContent(attributes_child
);
2208 if (!snapshot_mode_content
) {
2209 ret
= -LTTNG_ERR_NOMEM
;
2213 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2214 free(snapshot_mode_content
);
2216 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2220 /* live_timer_interval */
2221 xmlChar
*timer_interval_content
=
2222 xmlNodeGetContent(attributes_child
);
2223 if (!timer_interval_content
) {
2224 ret
= -LTTNG_ERR_NOMEM
;
2228 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2229 free(timer_interval_content
);
2231 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2239 /* Mandatory attribute, as defined in the session XSD */
2240 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2244 if (session_name
&& strcmp(name
, session_name
)) {
2245 /* This is not the session we are looking for */
2246 ret
= -LTTNG_ERR_NO_SESSION
;
2250 /* Init domains to create the session handles */
2251 for (node
= xmlFirstElementChild(domains_node
); node
;
2252 node
= xmlNextElementSibling(node
)) {
2253 struct lttng_domain
*domain
;
2255 domain
= zmalloc(sizeof(*domain
));
2257 ret
= -LTTNG_ERR_NOMEM
;
2261 ret
= init_domain(node
, domain
);
2263 goto domain_init_error
;
2266 switch (domain
->type
) {
2267 case LTTNG_DOMAIN_KERNEL
:
2268 if (kernel_domain
) {
2269 /* Same domain seen twice, invalid! */
2270 goto domain_init_error
;
2272 kernel_domain
= domain
;
2274 case LTTNG_DOMAIN_UST
:
2276 /* Same domain seen twice, invalid! */
2277 goto domain_init_error
;
2279 ust_domain
= domain
;
2281 case LTTNG_DOMAIN_JUL
:
2283 /* Same domain seen twice, invalid! */
2284 goto domain_init_error
;
2286 jul_domain
= domain
;
2288 case LTTNG_DOMAIN_LOG4J
:
2290 /* Same domain seen twice, invalid! */
2291 goto domain_init_error
;
2293 log4j_domain
= domain
;
2295 case LTTNG_DOMAIN_PYTHON
:
2296 if (python_domain
) {
2297 /* Same domain seen twice, invalid! */
2298 goto domain_init_error
;
2300 python_domain
= domain
;
2303 WARN("Invalid domain type");
2304 goto domain_init_error
;
2309 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2314 /* Destroy session if it exists */
2315 ret
= lttng_destroy_session(name
);
2316 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2317 ERR("Failed to destroy existing session.");
2322 /* Create session type depending on output type */
2323 if (snapshot_mode
&& snapshot_mode
!= -1) {
2324 ret
= create_snapshot_session(name
, output_node
);
2325 } else if (live_timer_interval
&&
2326 live_timer_interval
!= UINT64_MAX
) {
2327 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2328 log4j_domain
, output_node
, live_timer_interval
);
2330 /* regular session */
2331 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2332 log4j_domain
, output_node
, UINT64_MAX
);
2338 for (node
= xmlFirstElementChild(domains_node
); node
;
2339 node
= xmlNextElementSibling(node
)) {
2340 ret
= process_domain_node(node
, name
);
2347 ret
= lttng_start_tracing(name
);
2355 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2356 lttng_destroy_session(name
);
2360 free(kernel_domain
);
2364 free(python_domain
);
2370 * Return 1 if the given path is readable by the current UID or 0 if not.
2371 * Return -1 if the path is EPERM.
2373 static int validate_file_read_creds(const char *path
)
2379 /* Can we read the file. */
2380 ret
= access(path
, R_OK
);
2384 if (errno
== EACCES
) {
2395 int load_session_from_file(const char *path
, const char *session_name
,
2396 struct session_config_validation_ctx
*validation_ctx
, int override
)
2398 int ret
, session_found
= !session_name
;
2399 xmlDocPtr doc
= NULL
;
2400 xmlNodePtr sessions_node
;
2401 xmlNodePtr session_node
;
2404 assert(validation_ctx
);
2406 ret
= validate_file_read_creds(path
);
2409 ret
= -LTTNG_ERR_EPERM
;
2411 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2416 doc
= xmlParseFile(path
);
2418 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2422 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2424 ERR("Session configuration file validation failed");
2425 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2429 sessions_node
= xmlDocGetRootElement(doc
);
2430 if (!sessions_node
) {
2434 for (session_node
= xmlFirstElementChild(sessions_node
);
2435 session_node
; session_node
=
2436 xmlNextElementSibling(session_node
)) {
2437 ret
= process_session_node(session_node
,
2438 session_name
, override
);
2439 if (session_name
&& ret
== 0) {
2440 /* Target session found and loaded */
2448 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2454 int load_session_from_path(const char *path
, const char *session_name
,
2455 struct session_config_validation_ctx
*validation_ctx
, int override
)
2457 int ret
, session_found
= !session_name
;
2458 DIR *directory
= NULL
;
2461 assert(validation_ctx
);
2463 directory
= opendir(path
);
2467 /* Try the file loading. */
2470 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2473 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2478 struct dirent
*entry
;
2479 struct dirent
*result
;
2480 char *file_path
= NULL
;
2481 size_t path_len
= strlen(path
);
2483 if (path_len
>= PATH_MAX
) {
2484 ret
= -LTTNG_ERR_INVALID
;
2488 entry
= zmalloc(sizeof(*entry
));
2490 ret
= -LTTNG_ERR_NOMEM
;
2494 file_path
= zmalloc(PATH_MAX
);
2496 ret
= -LTTNG_ERR_NOMEM
;
2501 strncpy(file_path
, path
, path_len
);
2502 if (file_path
[path_len
- 1] != '/') {
2503 file_path
[path_len
++] = '/';
2507 /* Search for *.lttng files */
2508 while (!readdir_r(directory
, entry
, &result
) && result
) {
2509 size_t file_name_len
= strlen(result
->d_name
);
2511 if (file_name_len
<=
2512 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2516 if (path_len
+ file_name_len
>= PATH_MAX
) {
2520 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2521 result
->d_name
+ file_name_len
- sizeof(
2522 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2526 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2527 file_path
[path_len
+ file_name_len
] = '\0';
2529 ret
= load_session_from_file(file_path
, session_name
,
2530 validation_ctx
, override
);
2531 if (session_name
&& !ret
) {
2540 ret
= load_session_from_file(path
, session_name
,
2541 validation_ctx
, override
);
2551 if (closedir(directory
)) {
2556 if (!session_found
) {
2557 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2564 * Validate that the given path's credentials and the current process have the
2565 * same UID. If so, return 1 else return 0 if it does NOT match.
2567 static int validate_path_creds(const char *path
)
2569 int ret
, uid
= getuid();
2578 ret
= stat(path
, &buf
);
2580 if (errno
!= ENOENT
) {
2583 ret
= -LTTNG_ERR_INVALID
;
2587 if (buf
.st_uid
!= uid
) {
2598 int config_load_session(const char *path
, const char *session_name
,
2599 int override
, unsigned int autoload
)
2602 const char *path_ptr
= NULL
;
2603 struct session_config_validation_ctx validation_ctx
= { 0 };
2605 ret
= init_session_config_validation_ctx(&validation_ctx
);
2612 const char *sys_path
;
2615 home_path
= utils_get_home_dir();
2617 char path
[PATH_MAX
];
2620 * Try user session configuration path. Ignore error here so we can
2621 * continue loading the system wide sessions.
2624 ret
= snprintf(path
, sizeof(path
),
2625 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2626 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2628 PERROR("snprintf session autoload home config path");
2633 * Credentials are only validated for the autoload in order to
2634 * avoid any user session daemon to try to load kernel sessions
2635 * automatically and failing all the times.
2637 ret
= validate_path_creds(path
);
2642 ret
= snprintf(path
, sizeof(path
),
2643 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2645 PERROR("snprintf session home config path");
2651 ret
= load_session_from_path(path_ptr
, session_name
,
2652 &validation_ctx
, override
);
2653 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2657 * Continue even if the session was found since we have to try
2658 * the system wide sessions.
2663 /* Reset path pointer for the system wide dir. */
2666 /* Try system wide configuration directory. */
2668 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2669 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2670 ret
= validate_path_creds(sys_path
);
2672 path_ptr
= sys_path
;
2675 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2676 path_ptr
= sys_path
;
2680 ret
= load_session_from_path(path_ptr
, session_name
,
2681 &validation_ctx
, override
);
2684 ret
= access(path
, F_OK
);
2689 ret
= -LTTNG_ERR_INVALID
;
2690 WARN("Session configuration path does not exist.");
2693 ret
= -LTTNG_ERR_EPERM
;
2696 ret
= -LTTNG_ERR_UNK
;
2702 ret
= load_session_from_path(path
, session_name
,
2703 &validation_ctx
, override
);
2706 fini_session_config_validation_ctx(&validation_ctx
);
2707 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2709 * Don't report an error if no sessions are found when called
2710 * without a session_name or a search path.