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 <common/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <lttng/lttng.h>
42 #include <lttng/snapshot.h>
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_str_yes
= "yes";
60 const char * const config_str_true
= "true";
61 const char * const config_str_on
= "on";
62 const char * const config_str_no
= "no";
63 const char * const config_str_false
= "false";
64 const char * const config_str_off
= "off";
65 const char * const config_xml_encoding
= "UTF-8";
66 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
67 const char * const config_xml_indent_string
= "\t";
68 const char * const config_xml_true
= "true";
69 const char * const config_xml_false
= "false";
71 const char * const config_element_channel
= "channel";
72 const char * const config_element_channels
= "channels";
73 const char * const config_element_domain
= "domain";
74 const char * const config_element_domains
= "domains";
75 const char * const config_element_event
= "event";
76 const char * const config_element_events
= "events";
77 const char * const config_element_context
= "context";
78 const char * const config_element_contexts
= "contexts";
79 const char * const config_element_attributes
= "attributes";
80 const char * const config_element_exclusion
= "exclusion";
81 const char * const config_element_exclusions
= "exclusions";
82 const char * const config_element_function_attributes
= "function_attributes";
83 const char * const config_element_probe_attributes
= "probe_attributes";
84 const char * const config_element_symbol_name
= "symbol_name";
85 const char * const config_element_address
= "address";
86 const char * const config_element_offset
= "offset";
87 const char * const config_element_name
= "name";
88 const char * const config_element_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 const char * const config_element_type
= "type";
100 const char * const config_element_buffer_type
= "buffer_type";
101 const char * const config_element_session
= "session";
102 const char * const config_element_sessions
= "sessions";
103 const char * const config_element_perf
= "perf";
104 const char * const config_element_config
= "config";
105 const char * const config_element_started
= "started";
106 const char * const config_element_snapshot_mode
= "snapshot_mode";
107 const char * const config_element_loglevel
= "loglevel";
108 const char * const config_element_loglevel_type
= "loglevel_type";
109 const char * const config_element_filter
= "filter";
110 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
111 const char * const config_element_consumer_output
= "consumer_output";
112 const char * const config_element_destination
= "destination";
113 const char * const config_element_path
= "path";
114 const char * const config_element_net_output
= "net_output";
115 const char * const config_element_control_uri
= "control_uri";
116 const char * const config_element_data_uri
= "data_uri";
117 const char * const config_element_max_size
= "max_size";
119 const char * const config_domain_type_kernel
= "KERNEL";
120 const char * const config_domain_type_ust
= "UST";
121 const char * const config_domain_type_jul
= "JUL";
122 const char * const config_domain_type_log4j
= "LOG4J";
123 const char * const config_domain_type_python
= "PYTHON";
125 const char * const config_buffer_type_per_pid
= "PER_PID";
126 const char * const config_buffer_type_per_uid
= "PER_UID";
127 const char * const config_buffer_type_global
= "GLOBAL";
129 const char * const config_overwrite_mode_discard
= "DISCARD";
130 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
132 const char * const config_output_type_splice
= "SPLICE";
133 const char * const config_output_type_mmap
= "MMAP";
135 const char * const config_loglevel_type_all
= "ALL";
136 const char * const config_loglevel_type_range
= "RANGE";
137 const char * const config_loglevel_type_single
= "SINGLE";
139 const char * const config_event_type_all
= "ALL";
140 const char * const config_event_type_tracepoint
= "TRACEPOINT";
141 const char * const config_event_type_probe
= "PROBE";
142 const char * const config_event_type_function
= "FUNCTION";
143 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
144 const char * const config_event_type_noop
= "NOOP";
145 const char * const config_event_type_syscall
= "SYSCALL";
146 const char * const config_event_type_kprobe
= "KPROBE";
147 const char * const config_event_type_kretprobe
= "KRETPROBE";
149 const char * const config_event_context_pid
= "PID";
150 const char * const config_event_context_procname
= "PROCNAME";
151 const char * const config_event_context_prio
= "PRIO";
152 const char * const config_event_context_nice
= "NICE";
153 const char * const config_event_context_vpid
= "VPID";
154 const char * const config_event_context_tid
= "TID";
155 const char * const config_event_context_vtid
= "VTID";
156 const char * const config_event_context_ppid
= "PPID";
157 const char * const config_event_context_vppid
= "VPPID";
158 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
159 const char * const config_event_context_hostname
= "HOSTNAME";
160 const char * const config_event_context_ip
= "IP";
161 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
163 struct consumer_output
{
170 static int config_entry_handler_filter(struct handler_filter_args
*args
,
171 const char *section
, const char *name
, const char *value
)
174 struct config_entry entry
= { section
, name
, value
};
178 if (!section
|| !name
|| !value
) {
184 if (strcmp(args
->section
, section
)) {
189 ret
= args
->handler(&entry
, args
->user_data
);
195 int config_get_section_entries(const char *override_path
, const char *section
,
196 config_entry_handler_cb handler
, void *user_data
)
200 FILE *config_file
= NULL
;
201 struct handler_filter_args filter
= { section
, handler
, user_data
};
203 /* First, try system-wide conf. file. */
204 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
206 config_file
= fopen(path
, "r");
208 DBG("Loading daemon conf file at %s", path
);
210 * Return value is not very important here since error or not, we
211 * continue and try the next possible conf. file.
213 (void) ini_parse_file(config_file
,
214 (ini_entry_handler
) config_entry_handler_filter
,
219 /* Second is the user local configuration. */
220 path
= utils_get_home_dir();
222 char fullpath
[PATH_MAX
];
224 ret
= snprintf(fullpath
, sizeof(fullpath
),
225 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
227 PERROR("snprintf user conf. path");
231 config_file
= fopen(fullpath
, "r");
233 DBG("Loading daemon user conf file at %s", path
);
235 * Return value is not very important here since error or not, we
236 * continue and try the next possible conf. file.
238 (void) ini_parse_file(config_file
,
239 (ini_entry_handler
) config_entry_handler_filter
,
245 /* Final path is the one that the user might have provided. */
247 config_file
= fopen(override_path
, "r");
249 DBG("Loading daemon command line conf file at %s", override_path
);
250 (void) ini_parse_file(config_file
,
251 (ini_entry_handler
) config_entry_handler_filter
,
255 ERR("Failed to open daemon configuration file at %s",
262 /* Everything went well. */
270 int config_parse_value(const char *value
)
273 char *endptr
, *lower_str
;
283 v
= strtoul(value
, &endptr
, 10);
284 if (endptr
!= value
) {
289 lower_str
= zmalloc(len
+ 1);
296 for (i
= 0; i
< len
; i
++) {
297 lower_str
[i
] = tolower(value
[i
]);
300 if (!strcmp(lower_str
, config_str_yes
) ||
301 !strcmp(lower_str
, config_str_true
) ||
302 !strcmp(lower_str
, config_str_on
)) {
304 } else if (!strcmp(lower_str
, config_str_no
) ||
305 !strcmp(lower_str
, config_str_false
) ||
306 !strcmp(lower_str
, config_str_off
)) {
318 * Returns a xmlChar string which must be released using xmlFree().
320 static xmlChar
*encode_string(const char *in_str
)
322 xmlChar
*out_str
= NULL
;
323 xmlCharEncodingHandlerPtr handler
;
324 int out_len
, ret
, in_len
;
328 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
330 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
334 in_len
= strlen(in_str
);
336 * Add 1 byte for the NULL terminted character. The factor 2 here is
337 * because UTF-8 can be on two bytes so this fits the worst case for each
340 out_len
= (in_len
* 2) + 1;
341 out_str
= xmlMalloc(out_len
);
346 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
353 /* out_len is now the size of out_str */
354 out_str
[out_len
] = '\0';
360 struct config_writer
*config_writer_create(int fd_output
, int indent
)
363 struct config_writer
*writer
;
364 xmlOutputBufferPtr buffer
;
366 writer
= zmalloc(sizeof(struct config_writer
));
368 PERROR("zmalloc config_writer_create");
372 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
377 writer
->writer
= xmlNewTextWriter(buffer
);
378 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
379 config_xml_encoding
, NULL
);
384 ret
= xmlTextWriterSetIndentString(writer
->writer
,
385 BAD_CAST config_xml_indent_string
);
390 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
398 config_writer_destroy(writer
);
403 int config_writer_destroy(struct config_writer
*writer
)
412 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
413 WARN("Could not close XML document");
417 if (writer
->writer
) {
418 xmlFreeTextWriter(writer
->writer
);
427 int config_writer_open_element(struct config_writer
*writer
,
428 const char *element_name
)
431 xmlChar
*encoded_element_name
;
433 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
438 encoded_element_name
= encode_string(element_name
);
439 if (!encoded_element_name
) {
444 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
445 xmlFree(encoded_element_name
);
447 return ret
> 0 ? 0 : ret
;
451 int config_writer_close_element(struct config_writer
*writer
)
455 if (!writer
|| !writer
->writer
) {
460 ret
= xmlTextWriterEndElement(writer
->writer
);
462 return ret
> 0 ? 0 : ret
;
466 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
467 const char *element_name
, uint64_t value
)
470 xmlChar
*encoded_element_name
;
472 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
477 encoded_element_name
= encode_string(element_name
);
478 if (!encoded_element_name
) {
483 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
484 encoded_element_name
, "%" PRIu64
, value
);
485 xmlFree(encoded_element_name
);
487 return ret
> 0 ? 0 : ret
;
491 int config_writer_write_element_signed_int(struct config_writer
*writer
,
492 const char *element_name
, int64_t value
)
495 xmlChar
*encoded_element_name
;
497 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
502 encoded_element_name
= encode_string(element_name
);
503 if (!encoded_element_name
) {
508 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
509 encoded_element_name
, "%" PRIi64
, value
);
510 xmlFree(encoded_element_name
);
512 return ret
> 0 ? 0 : ret
;
516 int config_writer_write_element_bool(struct config_writer
*writer
,
517 const char *element_name
, int value
)
519 return config_writer_write_element_string(writer
, element_name
,
520 value
? config_xml_true
: config_xml_false
);
524 int config_writer_write_element_string(struct config_writer
*writer
,
525 const char *element_name
, const char *value
)
528 xmlChar
*encoded_element_name
= NULL
;
529 xmlChar
*encoded_value
= NULL
;
531 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
537 encoded_element_name
= encode_string(element_name
);
538 if (!encoded_element_name
) {
543 encoded_value
= encode_string(value
);
544 if (!encoded_value
) {
549 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
552 xmlFree(encoded_element_name
);
553 xmlFree(encoded_value
);
554 return ret
> 0 ? 0 : ret
;
558 void xml_error_handler(void *ctx
, const char *format
, ...)
564 va_start(args
, format
);
565 ret
= vasprintf(&errMsg
, format
, args
);
568 ERR("String allocation failed in xml error handler");
572 fprintf(stderr
, "XML Error: %s", errMsg
);
577 void fini_session_config_validation_ctx(
578 struct session_config_validation_ctx
*ctx
)
580 if (ctx
->parser_ctx
) {
581 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
585 xmlSchemaFree(ctx
->schema
);
588 if (ctx
->schema_validation_ctx
) {
589 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
592 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
596 char *get_session_config_xsd_path()
599 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
600 size_t base_path_len
;
604 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
607 base_path_len
= strlen(base_path
);
608 max_path_len
= base_path_len
+
609 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
610 xsd_path
= zmalloc(max_path_len
);
615 strncpy(xsd_path
, base_path
, max_path_len
);
616 if (xsd_path
[base_path_len
- 1] != '/') {
617 xsd_path
[base_path_len
++] = '/';
620 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
621 max_path_len
- base_path_len
);
627 int init_session_config_validation_ctx(
628 struct session_config_validation_ctx
*ctx
)
631 char *xsd_path
= get_session_config_xsd_path();
634 ret
= -LTTNG_ERR_NOMEM
;
638 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
639 if (!ctx
->parser_ctx
) {
640 ERR("XSD parser context creation failed");
641 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
644 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
645 xml_error_handler
, NULL
);
647 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
649 ERR("XSD parsing failed");
650 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
654 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
655 if (!ctx
->schema_validation_ctx
) {
656 ERR("XSD validation context creation failed");
657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
661 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
662 xml_error_handler
, NULL
);
667 fini_session_config_validation_ctx(ctx
);
675 int parse_uint(xmlChar
*str
, uint64_t *val
)
685 *val
= strtoull((const char *) str
, &endptr
, 10);
686 if (!endptr
|| *endptr
) {
697 int parse_int(xmlChar
*str
, int64_t *val
)
707 *val
= strtoll((const char *) str
, &endptr
, 10);
708 if (!endptr
|| *endptr
) {
719 int parse_bool(xmlChar
*str
, int *val
)
728 if (!strcmp((const char *) str
, config_xml_true
)) {
730 } else if (!strcmp((const char *) str
, config_xml_false
)) {
733 WARN("Invalid boolean value encoutered (%s).",
742 int get_domain_type(xmlChar
*domain
)
750 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
751 ret
= LTTNG_DOMAIN_KERNEL
;
752 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
753 ret
= LTTNG_DOMAIN_UST
;
754 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
755 ret
= LTTNG_DOMAIN_JUL
;
756 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
757 ret
= LTTNG_DOMAIN_LOG4J
;
758 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
759 ret
= LTTNG_DOMAIN_PYTHON
;
770 int get_buffer_type(xmlChar
*buffer_type
)
778 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
779 ret
= LTTNG_BUFFER_GLOBAL
;
780 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
781 ret
= LTTNG_BUFFER_PER_UID
;
782 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
783 ret
= LTTNG_BUFFER_PER_PID
;
794 int get_overwrite_mode(xmlChar
*overwrite_mode
)
798 if (!overwrite_mode
) {
802 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
804 } else if (!strcmp((char *) overwrite_mode
,
805 config_overwrite_mode_discard
)) {
817 int get_output_type(xmlChar
*output_type
)
825 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
826 ret
= LTTNG_EVENT_MMAP
;
827 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
828 ret
= LTTNG_EVENT_SPLICE
;
839 int get_event_type(xmlChar
*event_type
)
847 if (!strcmp((char *) event_type
, config_event_type_all
)) {
848 ret
= LTTNG_EVENT_ALL
;
849 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
850 ret
= LTTNG_EVENT_TRACEPOINT
;
851 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
852 ret
= LTTNG_EVENT_PROBE
;
853 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
854 ret
= LTTNG_EVENT_FUNCTION
;
855 } else if (!strcmp((char *) event_type
,
856 config_event_type_function_entry
)) {
857 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
858 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
859 ret
= LTTNG_EVENT_NOOP
;
860 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
861 ret
= LTTNG_EVENT_SYSCALL
;
872 int get_loglevel_type(xmlChar
*loglevel_type
)
876 if (!loglevel_type
) {
880 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
881 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
882 } else if (!strcmp((char *) loglevel_type
,
883 config_loglevel_type_range
)) {
884 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
885 } else if (!strcmp((char *) loglevel_type
,
886 config_loglevel_type_single
)) {
887 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
898 * Return the context type or -1 on error.
901 int get_context_type(xmlChar
*context_type
)
909 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
910 ret
= LTTNG_EVENT_CONTEXT_PID
;
911 } else if (!strcmp((char *) context_type
,
912 config_event_context_procname
)) {
913 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_prio
)) {
916 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_nice
)) {
919 ret
= LTTNG_EVENT_CONTEXT_NICE
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_vpid
)) {
922 ret
= LTTNG_EVENT_CONTEXT_VPID
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_tid
)) {
925 ret
= LTTNG_EVENT_CONTEXT_TID
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_vtid
)) {
928 ret
= LTTNG_EVENT_CONTEXT_VTID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_ppid
)) {
931 ret
= LTTNG_EVENT_CONTEXT_PPID
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_vppid
)) {
934 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_pthread_id
)) {
937 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
938 } else if (!strcmp((char *) context_type
,
939 config_event_context_hostname
)) {
940 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
941 } else if (!strcmp((char *) context_type
,
942 config_event_context_ip
)) {
943 ret
= LTTNG_EVENT_CONTEXT_IP
;
954 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
959 for (node
= xmlFirstElementChild(domain_node
); node
;
960 node
= xmlNextElementSibling(node
)) {
961 if (!strcmp((const char *) node
->name
, config_element_type
)) {
963 xmlChar
*node_content
= xmlNodeGetContent(node
);
965 ret
= -LTTNG_ERR_NOMEM
;
969 ret
= get_domain_type(node_content
);
972 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
977 } else if (!strcmp((const char *) node
->name
,
978 config_element_buffer_type
)) {
980 xmlChar
*node_content
= xmlNodeGetContent(node
);
982 ret
= -LTTNG_ERR_NOMEM
;
986 ret
= get_buffer_type(node_content
);
989 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
993 domain
->buf_type
= ret
;
1002 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1007 for (node
= xmlFirstElementChild(net_output_node
); node
;
1008 node
= xmlNextElementSibling(node
)) {
1009 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1011 *control_uri
= (char *) xmlNodeGetContent(node
);
1012 if (!*control_uri
) {
1017 *data_uri
= (char *) xmlNodeGetContent(node
);
1024 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1028 int process_consumer_output(xmlNodePtr consumer_output_node
,
1029 struct consumer_output
*output
)
1036 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1037 node
= xmlNextElementSibling(node
)) {
1038 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1039 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1043 ret
= -LTTNG_ERR_NOMEM
;
1047 ret
= parse_bool(enabled_str
, &output
->enabled
);
1053 xmlNodePtr output_type_node
;
1056 output_type_node
= xmlFirstElementChild(node
);
1057 if (!output_type_node
) {
1058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1062 if (!strcmp((const char *) output_type_node
->name
,
1063 config_element_path
)) {
1065 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1066 if (!output
->path
) {
1067 ret
= -LTTNG_ERR_NOMEM
;
1072 ret
= get_net_output_uris(output_type_node
,
1073 &output
->control_uri
, &output
->data_uri
);
1085 free(output
->control_uri
);
1086 free(output
->data_uri
);
1087 memset(output
, 0, sizeof(struct consumer_output
));
1093 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1094 const char *control_uri
, const char *data_uri
)
1097 struct lttng_handle
*handle
;
1098 const char *uri
= NULL
;
1103 handle
= lttng_create_handle(name
, domain
);
1105 ret
= -LTTNG_ERR_NOMEM
;
1109 if (!control_uri
|| !data_uri
) {
1110 uri
= control_uri
? control_uri
: data_uri
;
1115 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1116 lttng_destroy_handle(handle
);
1122 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1125 xmlNodePtr node
= NULL
;
1126 xmlNodePtr snapshot_output_list_node
;
1127 xmlNodePtr snapshot_output_node
;
1129 assert(session_name
);
1131 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1140 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1142 /* Parse and create snapshot outputs */
1144 for (snapshot_output_node
=
1145 xmlFirstElementChild(snapshot_output_list_node
);
1146 snapshot_output_node
; snapshot_output_node
=
1147 xmlNextElementSibling(snapshot_output_node
)) {
1149 uint64_t max_size
= UINT64_MAX
;
1150 struct consumer_output output
= { 0 };
1151 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1153 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1154 node
= xmlNextElementSibling(node
)) {
1155 if (!strcmp((const char *) node
->name
,
1156 config_element_name
)) {
1158 name
= (char *) xmlNodeGetContent(node
);
1160 ret
= -LTTNG_ERR_NOMEM
;
1161 goto error_snapshot_output
;
1163 } else if (!strcmp((const char *) node
->name
,
1164 config_element_max_size
)) {
1165 xmlChar
*content
= xmlNodeGetContent(node
);
1169 ret
= -LTTNG_ERR_NOMEM
;
1170 goto error_snapshot_output
;
1172 ret
= parse_uint(content
, &max_size
);
1175 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1176 goto error_snapshot_output
;
1179 /* consumer_output */
1180 ret
= process_consumer_output(node
, &output
);
1182 goto error_snapshot_output
;
1187 snapshot_output
= lttng_snapshot_output_create();
1188 if (!snapshot_output
) {
1189 ret
= -LTTNG_ERR_NOMEM
;
1190 goto error_snapshot_output
;
1193 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1195 goto error_snapshot_output
;
1198 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1200 goto error_snapshot_output
;
1204 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1207 goto error_snapshot_output
;
1210 if (output
.control_uri
) {
1211 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1214 goto error_snapshot_output
;
1218 if (output
.data_uri
) {
1219 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1222 goto error_snapshot_output
;
1227 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1228 error_snapshot_output
:
1231 free(output
.control_uri
);
1232 free(output
.data_uri
);
1233 lttng_snapshot_output_destroy(snapshot_output
);
1243 int create_session(const char *name
,
1244 struct lttng_domain
*kernel_domain
,
1245 struct lttng_domain
*ust_domain
,
1246 struct lttng_domain
*jul_domain
,
1247 struct lttng_domain
*log4j_domain
,
1248 xmlNodePtr output_node
,
1249 uint64_t live_timer_interval
)
1252 struct consumer_output output
= { 0 };
1253 xmlNodePtr consumer_output_node
;
1258 consumer_output_node
= xmlFirstElementChild(output_node
);
1259 if (!consumer_output_node
) {
1260 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1264 if (strcmp((const char *) consumer_output_node
->name
,
1265 config_element_consumer_output
)) {
1266 WARN("Invalid output type, expected %s node",
1267 config_element_consumer_output
);
1268 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1272 ret
= process_consumer_output(consumer_output_node
, &output
);
1278 if (live_timer_interval
!= UINT64_MAX
&&
1279 !output
.control_uri
&& !output
.data_uri
) {
1280 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1284 if (output
.control_uri
|| output
.data_uri
) {
1286 struct lttng_domain
*domain
;
1287 struct lttng_domain
*domains
[] =
1288 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1290 /* network destination */
1291 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1293 * URLs are provided for sure since the test above make sure that
1294 * with a live timer the data and control URIs are provided. So,
1295 * NULL is passed here and will be set right after.
1297 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1299 ret
= lttng_create_session(name
, NULL
);
1305 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1306 domain
= domains
[i
];
1311 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1318 /* either local output or no output */
1319 ret
= lttng_create_session(name
, output
.path
);
1326 free(output
.control_uri
);
1327 free(output
.data_uri
);
1331 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1332 struct lttng_event_probe_attr
*attr
)
1336 assert(probe_attribute_node
);
1339 if (!strcmp((const char *) probe_attribute_node
->name
,
1340 config_element_address
)) {
1345 content
= xmlNodeGetContent(probe_attribute_node
);
1347 ret
= -LTTNG_ERR_NOMEM
;
1351 ret
= parse_uint(content
, &addr
);
1354 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1359 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1360 config_element_offset
)) {
1362 uint64_t offset
= 0;
1365 content
= xmlNodeGetContent(probe_attribute_node
);
1367 ret
= -LTTNG_ERR_NOMEM
;
1371 ret
= parse_uint(content
, &offset
);
1374 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1378 attr
->offset
= offset
;
1379 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1380 config_element_symbol_name
)) {
1385 content
= xmlNodeGetContent(probe_attribute_node
);
1387 ret
= -LTTNG_ERR_NOMEM
;
1391 name_len
= strlen((char *) content
);
1392 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1393 WARN("symbol_name too long.");
1394 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1399 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1408 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1409 const char *channel_name
)
1413 struct lttng_event event
;
1414 char **exclusions
= NULL
;
1415 unsigned long exclusion_count
= 0;
1416 char *filter_expression
= NULL
;
1420 assert(channel_name
);
1422 memset(&event
, 0, sizeof(event
));
1424 for (node
= xmlFirstElementChild(event_node
); node
;
1425 node
= xmlNextElementSibling(node
)) {
1426 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1431 content
= xmlNodeGetContent(node
);
1433 ret
= -LTTNG_ERR_NOMEM
;
1437 name_len
= strlen((char *) content
);
1438 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1439 WARN("Channel name too long.");
1440 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1445 strncpy(event
.name
, (const char *) content
, name_len
);
1447 } else if (!strcmp((const char *) node
->name
,
1448 config_element_enabled
)) {
1449 xmlChar
*content
= xmlNodeGetContent(node
);
1453 ret
= -LTTNG_ERR_NOMEM
;
1457 ret
= parse_bool(content
, &event
.enabled
);
1460 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1463 } else if (!strcmp((const char *) node
->name
,
1464 config_element_type
)) {
1465 xmlChar
*content
= xmlNodeGetContent(node
);
1469 ret
= -LTTNG_ERR_NOMEM
;
1473 ret
= get_event_type(content
);
1476 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1481 } else if (!strcmp((const char *) node
->name
,
1482 config_element_loglevel_type
)) {
1483 xmlChar
*content
= xmlNodeGetContent(node
);
1487 ret
= -LTTNG_ERR_NOMEM
;
1491 ret
= get_loglevel_type(content
);
1494 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1498 event
.loglevel_type
= ret
;
1499 } else if (!strcmp((const char *) node
->name
,
1500 config_element_loglevel
)) {
1502 int64_t loglevel
= 0;
1505 content
= xmlNodeGetContent(node
);
1507 ret
= -LTTNG_ERR_NOMEM
;
1511 ret
= parse_int(content
, &loglevel
);
1514 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1518 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1519 WARN("loglevel out of range.");
1520 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1524 event
.loglevel
= loglevel
;
1525 } else if (!strcmp((const char *) node
->name
,
1526 config_element_filter
)) {
1528 xmlNodeGetContent(node
);
1532 ret
= -LTTNG_ERR_NOMEM
;
1536 filter_expression
= strdup((char *) content
);
1538 if (!filter_expression
) {
1539 ret
= -LTTNG_ERR_NOMEM
;
1542 } else if (!strcmp((const char *) node
->name
,
1543 config_element_exclusions
)) {
1544 xmlNodePtr exclusion_node
;
1545 int exclusion_index
= 0;
1550 * Exclusions has already been initialized,
1553 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1557 exclusion_count
= xmlChildElementCount(node
);
1558 if (!exclusion_count
) {
1562 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1564 exclusion_count
= 0;
1565 ret
= -LTTNG_ERR_NOMEM
;
1569 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1570 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1572 xmlNodeGetContent(exclusion_node
);
1575 ret
= -LTTNG_ERR_NOMEM
;
1579 exclusions
[exclusion_index
] = strdup((const char *) content
);
1581 if (!exclusions
[exclusion_index
]) {
1582 ret
= -LTTNG_ERR_NOMEM
;
1588 event
.exclusion
= 1;
1589 } else if (!strcmp((const char *) node
->name
,
1590 config_element_attributes
)) {
1591 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1594 if (!attribute_node
) {
1595 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1599 if (!strcmp((const char *) node
->name
,
1600 config_element_probe_attributes
)) {
1601 xmlNodePtr probe_attribute_node
;
1603 /* probe_attributes */
1604 for (probe_attribute_node
=
1605 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1606 probe_attribute_node
= xmlNextElementSibling(
1607 probe_attribute_node
)) {
1609 ret
= process_probe_attribute_node(probe_attribute_node
,
1618 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1620 /* function_attributes */
1621 content
= xmlNodeGetContent(symbol_node
);
1623 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1627 sym_len
= strlen((char *) content
);
1628 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1629 WARN("Function name too long.");
1630 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1635 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1642 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1643 filter_expression
, exclusion_count
, exclusions
);
1648 if (!event
.enabled
) {
1650 * Note that we should use lttng_disable_event_ext() (2.6+) to
1651 * eliminate the risk of clashing on events of the same
1652 * name (with different event types and loglevels).
1654 * Unfortunately, lttng_disable_event_ext() only performs a
1655 * match on the name and event type and errors out if any other
1656 * event attribute is not set to its default value.
1658 * This will disable all events that match this name.
1660 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1663 for (i
= 0; i
< exclusion_count
; i
++) {
1664 free(exclusions
[i
]);
1668 free(filter_expression
);
1673 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1674 const char *channel_name
)
1679 assert(events_node
);
1681 assert(channel_name
);
1683 for (node
= xmlFirstElementChild(events_node
); node
;
1684 node
= xmlNextElementSibling(node
)) {
1685 ret
= process_event_node(node
, handle
, channel_name
);
1695 int process_channel_attr_node(xmlNodePtr attr_node
,
1696 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1697 xmlNodePtr
*events_node
)
1703 assert(contexts_node
);
1704 assert(events_node
);
1706 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1711 content
= xmlNodeGetContent(attr_node
);
1713 ret
= -LTTNG_ERR_NOMEM
;
1717 name_len
= strlen((char *) content
);
1718 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1719 WARN("Channel name too long.");
1720 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1725 strncpy(channel
->name
, (const char *) content
, name_len
);
1727 } else if (!strcmp((const char *) attr_node
->name
,
1728 config_element_enabled
)) {
1733 content
= xmlNodeGetContent(attr_node
);
1735 ret
= -LTTNG_ERR_NOMEM
;
1739 ret
= parse_bool(content
, &enabled
);
1742 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1746 channel
->enabled
= enabled
;
1747 } else if (!strcmp((const char *) attr_node
->name
,
1748 config_element_overwrite_mode
)) {
1751 /* overwrite_mode */
1752 content
= xmlNodeGetContent(attr_node
);
1754 ret
= -LTTNG_ERR_NOMEM
;
1758 ret
= get_overwrite_mode(content
);
1761 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1765 channel
->attr
.overwrite
= ret
;
1766 } else if (!strcmp((const char *) attr_node
->name
,
1767 config_element_subbuf_size
)) {
1770 /* subbuffer_size */
1771 content
= xmlNodeGetContent(attr_node
);
1773 ret
= -LTTNG_ERR_NOMEM
;
1777 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1780 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1783 } else if (!strcmp((const char *) attr_node
->name
,
1784 config_element_num_subbuf
)) {
1787 /* subbuffer_count */
1788 content
= xmlNodeGetContent(attr_node
);
1790 ret
= -LTTNG_ERR_NOMEM
;
1794 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1797 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1800 } else if (!strcmp((const char *) attr_node
->name
,
1801 config_element_switch_timer_interval
)) {
1803 uint64_t switch_timer_interval
= 0;
1805 /* switch_timer_interval */
1806 content
= xmlNodeGetContent(attr_node
);
1808 ret
= -LTTNG_ERR_NOMEM
;
1812 ret
= parse_uint(content
, &switch_timer_interval
);
1815 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1819 if (switch_timer_interval
> UINT_MAX
) {
1820 WARN("switch_timer_interval out of range.");
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 channel
->attr
.switch_timer_interval
=
1826 switch_timer_interval
;
1827 } else if (!strcmp((const char *) attr_node
->name
,
1828 config_element_read_timer_interval
)) {
1830 uint64_t read_timer_interval
= 0;
1832 /* read_timer_interval */
1833 content
= xmlNodeGetContent(attr_node
);
1835 ret
= -LTTNG_ERR_NOMEM
;
1839 ret
= parse_uint(content
, &read_timer_interval
);
1842 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1846 if (read_timer_interval
> UINT_MAX
) {
1847 WARN("read_timer_interval out of range.");
1848 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1852 channel
->attr
.read_timer_interval
=
1853 read_timer_interval
;
1854 } else if (!strcmp((const char *) attr_node
->name
,
1855 config_element_output_type
)) {
1859 content
= xmlNodeGetContent(attr_node
);
1861 ret
= -LTTNG_ERR_NOMEM
;
1865 ret
= get_output_type(content
);
1868 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1872 channel
->attr
.output
= ret
;
1873 } else if (!strcmp((const char *) attr_node
->name
,
1874 config_element_tracefile_size
)) {
1877 /* tracefile_size */
1878 content
= xmlNodeGetContent(attr_node
);
1880 ret
= -LTTNG_ERR_NOMEM
;
1884 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1887 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1890 } else if (!strcmp((const char *) attr_node
->name
,
1891 config_element_tracefile_count
)) {
1894 /* tracefile_count */
1895 content
= xmlNodeGetContent(attr_node
);
1897 ret
= -LTTNG_ERR_NOMEM
;
1901 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1904 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1907 } else if (!strcmp((const char *) attr_node
->name
,
1908 config_element_live_timer_interval
)) {
1910 uint64_t live_timer_interval
= 0;
1912 /* live_timer_interval */
1913 content
= xmlNodeGetContent(attr_node
);
1915 ret
= -LTTNG_ERR_NOMEM
;
1919 ret
= parse_uint(content
, &live_timer_interval
);
1922 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1926 if (live_timer_interval
> UINT_MAX
) {
1927 WARN("live_timer_interval out of range.");
1928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1932 channel
->attr
.live_timer_interval
=
1933 live_timer_interval
;
1934 } else if (!strcmp((const char *) attr_node
->name
,
1935 config_element_events
)) {
1937 *events_node
= attr_node
;
1940 *contexts_node
= attr_node
;
1948 int process_context_node(xmlNodePtr context_node
,
1949 struct lttng_handle
*handle
, const char *channel_name
)
1952 struct lttng_event_context context
;
1953 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1956 assert(channel_name
);
1958 if (!context_child_node
) {
1959 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1963 memset(&context
, 0, sizeof(context
));
1965 if (!strcmp((const char *) context_child_node
->name
,
1966 config_element_type
)) {
1968 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1970 ret
= -LTTNG_ERR_NOMEM
;
1974 ret
= get_context_type(content
);
1977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1983 xmlNodePtr perf_attr_node
;
1986 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1987 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1988 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1989 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1990 perf_attr_node
; perf_attr_node
=
1991 xmlNextElementSibling(perf_attr_node
)) {
1992 if (!strcmp((const char *) perf_attr_node
->name
,
1993 config_element_type
)) {
1998 content
= xmlNodeGetContent(perf_attr_node
);
2000 ret
= -LTTNG_ERR_NOMEM
;
2004 ret
= parse_uint(content
, &type
);
2007 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2011 if (type
> UINT32_MAX
) {
2012 WARN("perf context type out of range.");
2013 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2017 context
.u
.perf_counter
.type
= type
;
2018 } else if (!strcmp((const char *) perf_attr_node
->name
,
2019 config_element_config
)) {
2021 uint64_t config
= 0;
2024 content
= xmlNodeGetContent(perf_attr_node
);
2026 ret
= -LTTNG_ERR_NOMEM
;
2030 ret
= parse_uint(content
, &config
);
2033 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2037 context
.u
.perf_counter
.config
= config
;
2038 } else if (!strcmp((const char *) perf_attr_node
->name
,
2039 config_element_name
)) {
2044 content
= xmlNodeGetContent(perf_attr_node
);
2046 ret
= -LTTNG_ERR_NOMEM
;
2050 name_len
= strlen((char *) content
);
2051 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2052 WARN("perf context name too long.");
2053 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2058 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2065 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2071 int process_contexts_node(xmlNodePtr contexts_node
,
2072 struct lttng_handle
*handle
, const char *channel_name
)
2075 xmlNodePtr context_node
;
2077 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2078 context_node
= xmlNextElementSibling(context_node
)) {
2079 ret
= process_context_node(context_node
, handle
, channel_name
);
2089 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2092 struct lttng_domain domain
= { 0 };
2093 struct lttng_handle
*handle
= NULL
;
2094 xmlNodePtr channels_node
= NULL
;
2097 assert(session_name
);
2099 ret
= init_domain(domain_node
, &domain
);
2104 handle
= lttng_create_handle(session_name
, &domain
);
2106 ret
= -LTTNG_ERR_NOMEM
;
2110 /* get the channels node */
2111 for (node
= xmlFirstElementChild(domain_node
); node
;
2112 node
= xmlNextElementSibling(node
)) {
2113 if (!strcmp((const char *) node
->name
,
2114 config_element_channels
)) {
2115 channels_node
= node
;
2120 if (!channels_node
) {
2124 /* create all channels */
2125 for (node
= xmlFirstElementChild(channels_node
); node
;
2126 node
= xmlNextElementSibling(node
)) {
2127 struct lttng_channel channel
;
2128 xmlNodePtr contexts_node
= NULL
;
2129 xmlNodePtr events_node
= NULL
;
2130 xmlNodePtr channel_attr_node
;
2132 memset(&channel
, 0, sizeof(channel
));
2133 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2135 for (channel_attr_node
= xmlFirstElementChild(node
);
2136 channel_attr_node
; channel_attr_node
=
2137 xmlNextElementSibling(channel_attr_node
)) {
2138 ret
= process_channel_attr_node(channel_attr_node
,
2139 &channel
, &contexts_node
, &events_node
);
2145 ret
= lttng_enable_channel(handle
, &channel
);
2150 ret
= process_events_node(events_node
, handle
, channel
.name
);
2155 ret
= process_contexts_node(contexts_node
, handle
,
2162 lttng_destroy_handle(handle
);
2167 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2170 int ret
, started
= -1, snapshot_mode
= -1;
2171 uint64_t live_timer_interval
= UINT64_MAX
;
2173 xmlNodePtr domains_node
= NULL
;
2174 xmlNodePtr output_node
= NULL
;
2176 struct lttng_domain
*kernel_domain
= NULL
;
2177 struct lttng_domain
*ust_domain
= NULL
;
2178 struct lttng_domain
*jul_domain
= NULL
;
2179 struct lttng_domain
*log4j_domain
= NULL
;
2180 struct lttng_domain
*python_domain
= NULL
;
2182 for (node
= xmlFirstElementChild(session_node
); node
;
2183 node
= xmlNextElementSibling(node
)) {
2184 if (!name
&& !strcmp((const char *) node
->name
,
2185 config_element_name
)) {
2187 xmlChar
*node_content
= xmlNodeGetContent(node
);
2188 if (!node_content
) {
2189 ret
= -LTTNG_ERR_NOMEM
;
2193 name
= (char *) node_content
;
2194 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2195 config_element_domains
)) {
2197 domains_node
= node
;
2198 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2199 config_element_started
)) {
2201 xmlChar
*node_content
= xmlNodeGetContent(node
);
2202 if (!node_content
) {
2203 ret
= -LTTNG_ERR_NOMEM
;
2207 ret
= parse_bool(node_content
, &started
);
2210 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2213 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2214 config_element_output
)) {
2218 /* attributes, snapshot_mode or live_timer_interval */
2219 xmlNodePtr attributes_child
=
2220 xmlFirstElementChild(node
);
2222 if (!strcmp((const char *) attributes_child
->name
,
2223 config_element_snapshot_mode
)) {
2225 xmlChar
*snapshot_mode_content
=
2226 xmlNodeGetContent(attributes_child
);
2227 if (!snapshot_mode_content
) {
2228 ret
= -LTTNG_ERR_NOMEM
;
2232 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2233 free(snapshot_mode_content
);
2235 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2239 /* live_timer_interval */
2240 xmlChar
*timer_interval_content
=
2241 xmlNodeGetContent(attributes_child
);
2242 if (!timer_interval_content
) {
2243 ret
= -LTTNG_ERR_NOMEM
;
2247 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2248 free(timer_interval_content
);
2250 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2258 /* Mandatory attribute, as defined in the session XSD */
2259 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2263 if (session_name
&& strcmp(name
, session_name
)) {
2264 /* This is not the session we are looking for */
2265 ret
= -LTTNG_ERR_NO_SESSION
;
2269 /* Init domains to create the session handles */
2270 for (node
= xmlFirstElementChild(domains_node
); node
;
2271 node
= xmlNextElementSibling(node
)) {
2272 struct lttng_domain
*domain
;
2274 domain
= zmalloc(sizeof(*domain
));
2276 ret
= -LTTNG_ERR_NOMEM
;
2280 ret
= init_domain(node
, domain
);
2282 goto domain_init_error
;
2285 switch (domain
->type
) {
2286 case LTTNG_DOMAIN_KERNEL
:
2287 if (kernel_domain
) {
2288 /* Same domain seen twice, invalid! */
2289 goto domain_init_error
;
2291 kernel_domain
= domain
;
2293 case LTTNG_DOMAIN_UST
:
2295 /* Same domain seen twice, invalid! */
2296 goto domain_init_error
;
2298 ust_domain
= domain
;
2300 case LTTNG_DOMAIN_JUL
:
2302 /* Same domain seen twice, invalid! */
2303 goto domain_init_error
;
2305 jul_domain
= domain
;
2307 case LTTNG_DOMAIN_LOG4J
:
2309 /* Same domain seen twice, invalid! */
2310 goto domain_init_error
;
2312 log4j_domain
= domain
;
2314 case LTTNG_DOMAIN_PYTHON
:
2315 if (python_domain
) {
2316 /* Same domain seen twice, invalid! */
2317 goto domain_init_error
;
2319 python_domain
= domain
;
2322 WARN("Invalid domain type");
2323 goto domain_init_error
;
2328 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2333 /* Destroy session if it exists */
2334 ret
= lttng_destroy_session(name
);
2335 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2336 ERR("Failed to destroy existing session.");
2341 /* Create session type depending on output type */
2342 if (snapshot_mode
&& snapshot_mode
!= -1) {
2343 ret
= create_snapshot_session(name
, output_node
);
2344 } else if (live_timer_interval
&&
2345 live_timer_interval
!= UINT64_MAX
) {
2346 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2347 log4j_domain
, output_node
, live_timer_interval
);
2349 /* regular session */
2350 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2351 log4j_domain
, output_node
, UINT64_MAX
);
2357 for (node
= xmlFirstElementChild(domains_node
); node
;
2358 node
= xmlNextElementSibling(node
)) {
2359 ret
= process_domain_node(node
, name
);
2366 ret
= lttng_start_tracing(name
);
2374 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2375 lttng_destroy_session(name
);
2379 free(kernel_domain
);
2383 free(python_domain
);
2389 * Return 1 if the given path is readable by the current UID or 0 if not.
2390 * Return -1 if the path is EPERM.
2392 static int validate_file_read_creds(const char *path
)
2398 /* Can we read the file. */
2399 ret
= access(path
, R_OK
);
2403 if (errno
== EACCES
) {
2414 int load_session_from_file(const char *path
, const char *session_name
,
2415 struct session_config_validation_ctx
*validation_ctx
, int override
)
2417 int ret
, session_found
= !session_name
;
2418 xmlDocPtr doc
= NULL
;
2419 xmlNodePtr sessions_node
;
2420 xmlNodePtr session_node
;
2423 assert(validation_ctx
);
2425 ret
= validate_file_read_creds(path
);
2428 ret
= -LTTNG_ERR_EPERM
;
2430 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2435 doc
= xmlParseFile(path
);
2437 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2441 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2443 ERR("Session configuration file validation failed");
2444 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2448 sessions_node
= xmlDocGetRootElement(doc
);
2449 if (!sessions_node
) {
2453 for (session_node
= xmlFirstElementChild(sessions_node
);
2454 session_node
; session_node
=
2455 xmlNextElementSibling(session_node
)) {
2456 ret
= process_session_node(session_node
,
2457 session_name
, override
);
2458 if (session_name
&& ret
== 0) {
2459 /* Target session found and loaded */
2467 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2473 int load_session_from_path(const char *path
, const char *session_name
,
2474 struct session_config_validation_ctx
*validation_ctx
, int override
)
2476 int ret
, session_found
= !session_name
;
2477 DIR *directory
= NULL
;
2480 assert(validation_ctx
);
2482 directory
= opendir(path
);
2486 /* Try the file loading. */
2489 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2492 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2497 struct dirent
*entry
;
2498 struct dirent
*result
;
2499 char *file_path
= NULL
;
2500 size_t path_len
= strlen(path
);
2502 if (path_len
>= PATH_MAX
) {
2503 ret
= -LTTNG_ERR_INVALID
;
2507 entry
= zmalloc(sizeof(*entry
));
2509 ret
= -LTTNG_ERR_NOMEM
;
2513 file_path
= zmalloc(PATH_MAX
);
2515 ret
= -LTTNG_ERR_NOMEM
;
2520 strncpy(file_path
, path
, path_len
);
2521 if (file_path
[path_len
- 1] != '/') {
2522 file_path
[path_len
++] = '/';
2526 /* Search for *.lttng files */
2527 while (!readdir_r(directory
, entry
, &result
) && result
) {
2528 size_t file_name_len
= strlen(result
->d_name
);
2530 if (file_name_len
<=
2531 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2535 if (path_len
+ file_name_len
>= PATH_MAX
) {
2539 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2540 result
->d_name
+ file_name_len
- sizeof(
2541 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2545 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2546 file_path
[path_len
+ file_name_len
] = '\0';
2548 ret
= load_session_from_file(file_path
, session_name
,
2549 validation_ctx
, override
);
2550 if (session_name
&& !ret
) {
2559 ret
= load_session_from_file(path
, session_name
,
2560 validation_ctx
, override
);
2570 if (closedir(directory
)) {
2575 if (!session_found
) {
2576 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2583 * Validate that the given path's credentials and the current process have the
2584 * same UID. If so, return 1 else return 0 if it does NOT match.
2586 static int validate_path_creds(const char *path
)
2588 int ret
, uid
= getuid();
2597 ret
= stat(path
, &buf
);
2599 if (errno
!= ENOENT
) {
2602 ret
= -LTTNG_ERR_INVALID
;
2606 if (buf
.st_uid
!= uid
) {
2617 int config_load_session(const char *path
, const char *session_name
,
2618 int override
, unsigned int autoload
)
2621 const char *path_ptr
= NULL
;
2622 struct session_config_validation_ctx validation_ctx
= { 0 };
2624 ret
= init_session_config_validation_ctx(&validation_ctx
);
2631 const char *sys_path
;
2634 home_path
= utils_get_home_dir();
2636 char path
[PATH_MAX
];
2639 * Try user session configuration path. Ignore error here so we can
2640 * continue loading the system wide sessions.
2643 ret
= snprintf(path
, sizeof(path
),
2644 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2645 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2647 PERROR("snprintf session autoload home config path");
2652 * Credentials are only validated for the autoload in order to
2653 * avoid any user session daemon to try to load kernel sessions
2654 * automatically and failing all the times.
2656 ret
= validate_path_creds(path
);
2661 ret
= snprintf(path
, sizeof(path
),
2662 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2664 PERROR("snprintf session home config path");
2670 ret
= load_session_from_path(path_ptr
, session_name
,
2671 &validation_ctx
, override
);
2672 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2676 * Continue even if the session was found since we have to try
2677 * the system wide sessions.
2682 /* Reset path pointer for the system wide dir. */
2685 /* Try system wide configuration directory. */
2687 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2688 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2689 ret
= validate_path_creds(sys_path
);
2691 path_ptr
= sys_path
;
2694 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2695 path_ptr
= sys_path
;
2699 ret
= load_session_from_path(path_ptr
, session_name
,
2700 &validation_ctx
, override
);
2703 ret
= access(path
, F_OK
);
2708 ret
= -LTTNG_ERR_INVALID
;
2709 WARN("Session configuration path does not exist.");
2712 ret
= -LTTNG_ERR_EPERM
;
2715 ret
= -LTTNG_ERR_UNK
;
2721 ret
= load_session_from_path(path
, session_name
,
2722 &validation_ctx
, override
);
2725 fini_session_config_validation_ctx(&validation_ctx
);
2726 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2728 * Don't report an error if no sessions are found when called
2729 * without a session_name or a search path.