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
);
1647 if (!event
.enabled
) {
1649 * Note that we should use lttng_disable_event_ext() (2.6+) to
1650 * eliminate the risk of clashing on events of the same
1651 * name (with different event types and loglevels).
1653 * Unfortunately, lttng_disable_event_ext() only performs a
1654 * match on the name and event type and errors out if any other
1655 * event attribute is not set to its default value.
1657 * This will disable all events that match this name.
1659 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1662 for (i
= 0; i
< exclusion_count
; i
++) {
1663 free(exclusions
[i
]);
1667 free(filter_expression
);
1672 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1673 const char *channel_name
)
1678 assert(events_node
);
1680 assert(channel_name
);
1682 for (node
= xmlFirstElementChild(events_node
); node
;
1683 node
= xmlNextElementSibling(node
)) {
1684 ret
= process_event_node(node
, handle
, channel_name
);
1694 int process_channel_attr_node(xmlNodePtr attr_node
,
1695 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1696 xmlNodePtr
*events_node
)
1702 assert(contexts_node
);
1703 assert(events_node
);
1705 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1710 content
= xmlNodeGetContent(attr_node
);
1712 ret
= -LTTNG_ERR_NOMEM
;
1716 name_len
= strlen((char *) content
);
1717 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1718 WARN("Channel name too long.");
1719 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1724 strncpy(channel
->name
, (const char *) content
, name_len
);
1726 } else if (!strcmp((const char *) attr_node
->name
,
1727 config_element_enabled
)) {
1732 content
= xmlNodeGetContent(attr_node
);
1734 ret
= -LTTNG_ERR_NOMEM
;
1738 ret
= parse_bool(content
, &enabled
);
1741 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1745 channel
->enabled
= enabled
;
1746 } else if (!strcmp((const char *) attr_node
->name
,
1747 config_element_overwrite_mode
)) {
1750 /* overwrite_mode */
1751 content
= xmlNodeGetContent(attr_node
);
1753 ret
= -LTTNG_ERR_NOMEM
;
1757 ret
= get_overwrite_mode(content
);
1760 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1764 channel
->attr
.overwrite
= ret
;
1765 } else if (!strcmp((const char *) attr_node
->name
,
1766 config_element_subbuf_size
)) {
1769 /* subbuffer_size */
1770 content
= xmlNodeGetContent(attr_node
);
1772 ret
= -LTTNG_ERR_NOMEM
;
1776 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1779 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1782 } else if (!strcmp((const char *) attr_node
->name
,
1783 config_element_num_subbuf
)) {
1786 /* subbuffer_count */
1787 content
= xmlNodeGetContent(attr_node
);
1789 ret
= -LTTNG_ERR_NOMEM
;
1793 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1796 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1799 } else if (!strcmp((const char *) attr_node
->name
,
1800 config_element_switch_timer_interval
)) {
1802 uint64_t switch_timer_interval
= 0;
1804 /* switch_timer_interval */
1805 content
= xmlNodeGetContent(attr_node
);
1807 ret
= -LTTNG_ERR_NOMEM
;
1811 ret
= parse_uint(content
, &switch_timer_interval
);
1814 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1818 if (switch_timer_interval
> UINT_MAX
) {
1819 WARN("switch_timer_interval out of range.");
1820 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1824 channel
->attr
.switch_timer_interval
=
1825 switch_timer_interval
;
1826 } else if (!strcmp((const char *) attr_node
->name
,
1827 config_element_read_timer_interval
)) {
1829 uint64_t read_timer_interval
= 0;
1831 /* read_timer_interval */
1832 content
= xmlNodeGetContent(attr_node
);
1834 ret
= -LTTNG_ERR_NOMEM
;
1838 ret
= parse_uint(content
, &read_timer_interval
);
1841 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1845 if (read_timer_interval
> UINT_MAX
) {
1846 WARN("read_timer_interval out of range.");
1847 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1851 channel
->attr
.read_timer_interval
=
1852 read_timer_interval
;
1853 } else if (!strcmp((const char *) attr_node
->name
,
1854 config_element_output_type
)) {
1858 content
= xmlNodeGetContent(attr_node
);
1860 ret
= -LTTNG_ERR_NOMEM
;
1864 ret
= get_output_type(content
);
1867 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1871 channel
->attr
.output
= ret
;
1872 } else if (!strcmp((const char *) attr_node
->name
,
1873 config_element_tracefile_size
)) {
1876 /* tracefile_size */
1877 content
= xmlNodeGetContent(attr_node
);
1879 ret
= -LTTNG_ERR_NOMEM
;
1883 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1886 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1889 } else if (!strcmp((const char *) attr_node
->name
,
1890 config_element_tracefile_count
)) {
1893 /* tracefile_count */
1894 content
= xmlNodeGetContent(attr_node
);
1896 ret
= -LTTNG_ERR_NOMEM
;
1900 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1903 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1906 } else if (!strcmp((const char *) attr_node
->name
,
1907 config_element_live_timer_interval
)) {
1909 uint64_t live_timer_interval
= 0;
1911 /* live_timer_interval */
1912 content
= xmlNodeGetContent(attr_node
);
1914 ret
= -LTTNG_ERR_NOMEM
;
1918 ret
= parse_uint(content
, &live_timer_interval
);
1921 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1925 if (live_timer_interval
> UINT_MAX
) {
1926 WARN("live_timer_interval out of range.");
1927 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1931 channel
->attr
.live_timer_interval
=
1932 live_timer_interval
;
1933 } else if (!strcmp((const char *) attr_node
->name
,
1934 config_element_events
)) {
1936 *events_node
= attr_node
;
1939 *contexts_node
= attr_node
;
1947 int process_context_node(xmlNodePtr context_node
,
1948 struct lttng_handle
*handle
, const char *channel_name
)
1951 struct lttng_event_context context
;
1952 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1955 assert(channel_name
);
1957 if (!context_child_node
) {
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1962 memset(&context
, 0, sizeof(context
));
1964 if (!strcmp((const char *) context_child_node
->name
,
1965 config_element_type
)) {
1967 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1969 ret
= -LTTNG_ERR_NOMEM
;
1973 ret
= get_context_type(content
);
1976 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1982 xmlNodePtr perf_attr_node
;
1985 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1986 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1987 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1988 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1989 perf_attr_node
; perf_attr_node
=
1990 xmlNextElementSibling(perf_attr_node
)) {
1991 if (!strcmp((const char *) perf_attr_node
->name
,
1992 config_element_type
)) {
1997 content
= xmlNodeGetContent(perf_attr_node
);
1999 ret
= -LTTNG_ERR_NOMEM
;
2003 ret
= parse_uint(content
, &type
);
2006 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2010 if (type
> UINT32_MAX
) {
2011 WARN("perf context type out of range.");
2012 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2016 context
.u
.perf_counter
.type
= type
;
2017 } else if (!strcmp((const char *) perf_attr_node
->name
,
2018 config_element_config
)) {
2020 uint64_t config
= 0;
2023 content
= xmlNodeGetContent(perf_attr_node
);
2025 ret
= -LTTNG_ERR_NOMEM
;
2029 ret
= parse_uint(content
, &config
);
2032 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2036 context
.u
.perf_counter
.config
= config
;
2037 } else if (!strcmp((const char *) perf_attr_node
->name
,
2038 config_element_name
)) {
2043 content
= xmlNodeGetContent(perf_attr_node
);
2045 ret
= -LTTNG_ERR_NOMEM
;
2049 name_len
= strlen((char *) content
);
2050 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2051 WARN("perf context name too long.");
2052 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2057 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2064 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2070 int process_contexts_node(xmlNodePtr contexts_node
,
2071 struct lttng_handle
*handle
, const char *channel_name
)
2074 xmlNodePtr context_node
;
2076 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2077 context_node
= xmlNextElementSibling(context_node
)) {
2078 ret
= process_context_node(context_node
, handle
, channel_name
);
2088 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2091 struct lttng_domain domain
= { 0 };
2092 struct lttng_handle
*handle
= NULL
;
2093 xmlNodePtr channels_node
= NULL
;
2096 assert(session_name
);
2098 ret
= init_domain(domain_node
, &domain
);
2103 handle
= lttng_create_handle(session_name
, &domain
);
2105 ret
= -LTTNG_ERR_NOMEM
;
2109 /* get the channels node */
2110 for (node
= xmlFirstElementChild(domain_node
); node
;
2111 node
= xmlNextElementSibling(node
)) {
2112 if (!strcmp((const char *) node
->name
,
2113 config_element_channels
)) {
2114 channels_node
= node
;
2119 if (!channels_node
) {
2123 /* create all channels */
2124 for (node
= xmlFirstElementChild(channels_node
); node
;
2125 node
= xmlNextElementSibling(node
)) {
2126 struct lttng_channel channel
;
2127 xmlNodePtr contexts_node
= NULL
;
2128 xmlNodePtr events_node
= NULL
;
2129 xmlNodePtr channel_attr_node
;
2131 memset(&channel
, 0, sizeof(channel
));
2132 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2134 for (channel_attr_node
= xmlFirstElementChild(node
);
2135 channel_attr_node
; channel_attr_node
=
2136 xmlNextElementSibling(channel_attr_node
)) {
2137 ret
= process_channel_attr_node(channel_attr_node
,
2138 &channel
, &contexts_node
, &events_node
);
2144 ret
= lttng_enable_channel(handle
, &channel
);
2149 ret
= process_events_node(events_node
, handle
, channel
.name
);
2154 ret
= process_contexts_node(contexts_node
, handle
,
2161 lttng_destroy_handle(handle
);
2166 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2169 int ret
, started
= -1, snapshot_mode
= -1;
2170 uint64_t live_timer_interval
= UINT64_MAX
;
2172 xmlNodePtr domains_node
= NULL
;
2173 xmlNodePtr output_node
= NULL
;
2175 struct lttng_domain
*kernel_domain
= NULL
;
2176 struct lttng_domain
*ust_domain
= NULL
;
2177 struct lttng_domain
*jul_domain
= NULL
;
2178 struct lttng_domain
*log4j_domain
= NULL
;
2179 struct lttng_domain
*python_domain
= NULL
;
2181 for (node
= xmlFirstElementChild(session_node
); node
;
2182 node
= xmlNextElementSibling(node
)) {
2183 if (!name
&& !strcmp((const char *) node
->name
,
2184 config_element_name
)) {
2186 xmlChar
*node_content
= xmlNodeGetContent(node
);
2187 if (!node_content
) {
2188 ret
= -LTTNG_ERR_NOMEM
;
2192 name
= (char *) node_content
;
2193 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2194 config_element_domains
)) {
2196 domains_node
= node
;
2197 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2198 config_element_started
)) {
2200 xmlChar
*node_content
= xmlNodeGetContent(node
);
2201 if (!node_content
) {
2202 ret
= -LTTNG_ERR_NOMEM
;
2206 ret
= parse_bool(node_content
, &started
);
2209 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2212 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2213 config_element_output
)) {
2217 /* attributes, snapshot_mode or live_timer_interval */
2218 xmlNodePtr attributes_child
=
2219 xmlFirstElementChild(node
);
2221 if (!strcmp((const char *) attributes_child
->name
,
2222 config_element_snapshot_mode
)) {
2224 xmlChar
*snapshot_mode_content
=
2225 xmlNodeGetContent(attributes_child
);
2226 if (!snapshot_mode_content
) {
2227 ret
= -LTTNG_ERR_NOMEM
;
2231 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2232 free(snapshot_mode_content
);
2234 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2238 /* live_timer_interval */
2239 xmlChar
*timer_interval_content
=
2240 xmlNodeGetContent(attributes_child
);
2241 if (!timer_interval_content
) {
2242 ret
= -LTTNG_ERR_NOMEM
;
2246 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2247 free(timer_interval_content
);
2249 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2257 /* Mandatory attribute, as defined in the session XSD */
2258 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2262 if (session_name
&& strcmp(name
, session_name
)) {
2263 /* This is not the session we are looking for */
2264 ret
= -LTTNG_ERR_NO_SESSION
;
2268 /* Init domains to create the session handles */
2269 for (node
= xmlFirstElementChild(domains_node
); node
;
2270 node
= xmlNextElementSibling(node
)) {
2271 struct lttng_domain
*domain
;
2273 domain
= zmalloc(sizeof(*domain
));
2275 ret
= -LTTNG_ERR_NOMEM
;
2279 ret
= init_domain(node
, domain
);
2281 goto domain_init_error
;
2284 switch (domain
->type
) {
2285 case LTTNG_DOMAIN_KERNEL
:
2286 if (kernel_domain
) {
2287 /* Same domain seen twice, invalid! */
2288 goto domain_init_error
;
2290 kernel_domain
= domain
;
2292 case LTTNG_DOMAIN_UST
:
2294 /* Same domain seen twice, invalid! */
2295 goto domain_init_error
;
2297 ust_domain
= domain
;
2299 case LTTNG_DOMAIN_JUL
:
2301 /* Same domain seen twice, invalid! */
2302 goto domain_init_error
;
2304 jul_domain
= domain
;
2306 case LTTNG_DOMAIN_LOG4J
:
2308 /* Same domain seen twice, invalid! */
2309 goto domain_init_error
;
2311 log4j_domain
= domain
;
2313 case LTTNG_DOMAIN_PYTHON
:
2314 if (python_domain
) {
2315 /* Same domain seen twice, invalid! */
2316 goto domain_init_error
;
2318 python_domain
= domain
;
2321 WARN("Invalid domain type");
2322 goto domain_init_error
;
2327 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2332 /* Destroy session if it exists */
2333 ret
= lttng_destroy_session(name
);
2334 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2335 ERR("Failed to destroy existing session.");
2340 /* Create session type depending on output type */
2341 if (snapshot_mode
&& snapshot_mode
!= -1) {
2342 ret
= create_snapshot_session(name
, output_node
);
2343 } else if (live_timer_interval
&&
2344 live_timer_interval
!= UINT64_MAX
) {
2345 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2346 log4j_domain
, output_node
, live_timer_interval
);
2348 /* regular session */
2349 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2350 log4j_domain
, output_node
, UINT64_MAX
);
2356 for (node
= xmlFirstElementChild(domains_node
); node
;
2357 node
= xmlNextElementSibling(node
)) {
2358 ret
= process_domain_node(node
, name
);
2365 ret
= lttng_start_tracing(name
);
2373 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2374 lttng_destroy_session(name
);
2378 free(kernel_domain
);
2382 free(python_domain
);
2388 * Return 1 if the given path is readable by the current UID or 0 if not.
2389 * Return -1 if the path is EPERM.
2391 static int validate_file_read_creds(const char *path
)
2397 /* Can we read the file. */
2398 ret
= access(path
, R_OK
);
2402 if (errno
== EACCES
) {
2413 int load_session_from_file(const char *path
, const char *session_name
,
2414 struct session_config_validation_ctx
*validation_ctx
, int override
)
2416 int ret
, session_found
= !session_name
;
2417 xmlDocPtr doc
= NULL
;
2418 xmlNodePtr sessions_node
;
2419 xmlNodePtr session_node
;
2422 assert(validation_ctx
);
2424 ret
= validate_file_read_creds(path
);
2427 ret
= -LTTNG_ERR_EPERM
;
2429 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2434 doc
= xmlParseFile(path
);
2436 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2440 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2442 ERR("Session configuration file validation failed");
2443 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2447 sessions_node
= xmlDocGetRootElement(doc
);
2448 if (!sessions_node
) {
2452 for (session_node
= xmlFirstElementChild(sessions_node
);
2453 session_node
; session_node
=
2454 xmlNextElementSibling(session_node
)) {
2455 ret
= process_session_node(session_node
,
2456 session_name
, override
);
2457 if (session_name
&& ret
== 0) {
2458 /* Target session found and loaded */
2466 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2472 int load_session_from_path(const char *path
, const char *session_name
,
2473 struct session_config_validation_ctx
*validation_ctx
, int override
)
2475 int ret
, session_found
= !session_name
;
2476 DIR *directory
= NULL
;
2479 assert(validation_ctx
);
2481 directory
= opendir(path
);
2485 /* Try the file loading. */
2488 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2491 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2496 struct dirent
*entry
;
2497 struct dirent
*result
;
2498 char *file_path
= NULL
;
2499 size_t path_len
= strlen(path
);
2501 if (path_len
>= PATH_MAX
) {
2502 ret
= -LTTNG_ERR_INVALID
;
2506 entry
= zmalloc(sizeof(*entry
));
2508 ret
= -LTTNG_ERR_NOMEM
;
2512 file_path
= zmalloc(PATH_MAX
);
2514 ret
= -LTTNG_ERR_NOMEM
;
2519 strncpy(file_path
, path
, path_len
);
2520 if (file_path
[path_len
- 1] != '/') {
2521 file_path
[path_len
++] = '/';
2525 /* Search for *.lttng files */
2526 while (!readdir_r(directory
, entry
, &result
) && result
) {
2527 size_t file_name_len
= strlen(result
->d_name
);
2529 if (file_name_len
<=
2530 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2534 if (path_len
+ file_name_len
>= PATH_MAX
) {
2538 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2539 result
->d_name
+ file_name_len
- sizeof(
2540 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2544 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2545 file_path
[path_len
+ file_name_len
] = '\0';
2547 ret
= load_session_from_file(file_path
, session_name
,
2548 validation_ctx
, override
);
2549 if (session_name
&& !ret
) {
2558 ret
= load_session_from_file(path
, session_name
,
2559 validation_ctx
, override
);
2569 if (closedir(directory
)) {
2574 if (!session_found
) {
2575 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2582 * Validate that the given path's credentials and the current process have the
2583 * same UID. If so, return 1 else return 0 if it does NOT match.
2585 static int validate_path_creds(const char *path
)
2587 int ret
, uid
= getuid();
2596 ret
= stat(path
, &buf
);
2598 if (errno
!= ENOENT
) {
2601 ret
= -LTTNG_ERR_INVALID
;
2605 if (buf
.st_uid
!= uid
) {
2616 int config_load_session(const char *path
, const char *session_name
,
2617 int override
, unsigned int autoload
)
2620 const char *path_ptr
= NULL
;
2621 struct session_config_validation_ctx validation_ctx
= { 0 };
2623 ret
= init_session_config_validation_ctx(&validation_ctx
);
2630 const char *sys_path
;
2633 home_path
= utils_get_home_dir();
2635 char path
[PATH_MAX
];
2638 * Try user session configuration path. Ignore error here so we can
2639 * continue loading the system wide sessions.
2642 ret
= snprintf(path
, sizeof(path
),
2643 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2644 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2646 PERROR("snprintf session autoload home config path");
2651 * Credentials are only validated for the autoload in order to
2652 * avoid any user session daemon to try to load kernel sessions
2653 * automatically and failing all the times.
2655 ret
= validate_path_creds(path
);
2660 ret
= snprintf(path
, sizeof(path
),
2661 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2663 PERROR("snprintf session home config path");
2669 ret
= load_session_from_path(path_ptr
, session_name
,
2670 &validation_ctx
, override
);
2671 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2675 * Continue even if the session was found since we have to try
2676 * the system wide sessions.
2681 /* Reset path pointer for the system wide dir. */
2684 /* Try system wide configuration directory. */
2686 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2687 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2688 ret
= validate_path_creds(sys_path
);
2690 path_ptr
= sys_path
;
2693 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2694 path_ptr
= sys_path
;
2698 ret
= load_session_from_path(path_ptr
, session_name
,
2699 &validation_ctx
, override
);
2702 ret
= access(path
, F_OK
);
2707 ret
= -LTTNG_ERR_INVALID
;
2708 WARN("Session configuration path does not exist.");
2711 ret
= -LTTNG_ERR_EPERM
;
2714 ret
= -LTTNG_ERR_UNK
;
2720 ret
= load_session_from_path(path
, session_name
,
2721 &validation_ctx
, override
);
2724 fini_session_config_validation_ctx(&validation_ctx
);
2725 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2727 * Don't report an error if no sessions are found when called
2728 * without a session_name or a search path.