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";
118 const char * const config_element_pid
= "pid";
119 const char * const config_element_pids
= "pids";
120 const char * const config_element_shared_memory_path
= "shared_memory_path";
121 const char * const config_element_pid_tracker
= "pid_tracker";
122 const char * const config_element_trackers
= "trackers";
124 const char * const config_domain_type_kernel
= "KERNEL";
125 const char * const config_domain_type_ust
= "UST";
126 const char * const config_domain_type_jul
= "JUL";
127 const char * const config_domain_type_log4j
= "LOG4J";
128 const char * const config_domain_type_python
= "PYTHON";
130 const char * const config_buffer_type_per_pid
= "PER_PID";
131 const char * const config_buffer_type_per_uid
= "PER_UID";
132 const char * const config_buffer_type_global
= "GLOBAL";
134 const char * const config_overwrite_mode_discard
= "DISCARD";
135 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
137 const char * const config_output_type_splice
= "SPLICE";
138 const char * const config_output_type_mmap
= "MMAP";
140 const char * const config_loglevel_type_all
= "ALL";
141 const char * const config_loglevel_type_range
= "RANGE";
142 const char * const config_loglevel_type_single
= "SINGLE";
144 const char * const config_event_type_all
= "ALL";
145 const char * const config_event_type_tracepoint
= "TRACEPOINT";
146 const char * const config_event_type_probe
= "PROBE";
147 const char * const config_event_type_function
= "FUNCTION";
148 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
149 const char * const config_event_type_noop
= "NOOP";
150 const char * const config_event_type_syscall
= "SYSCALL";
151 const char * const config_event_type_kprobe
= "KPROBE";
152 const char * const config_event_type_kretprobe
= "KRETPROBE";
154 const char * const config_event_context_pid
= "PID";
155 const char * const config_event_context_procname
= "PROCNAME";
156 const char * const config_event_context_prio
= "PRIO";
157 const char * const config_event_context_nice
= "NICE";
158 const char * const config_event_context_vpid
= "VPID";
159 const char * const config_event_context_tid
= "TID";
160 const char * const config_event_context_vtid
= "VTID";
161 const char * const config_event_context_ppid
= "PPID";
162 const char * const config_event_context_vppid
= "VPPID";
163 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
164 const char * const config_event_context_hostname
= "HOSTNAME";
165 const char * const config_event_context_ip
= "IP";
166 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
168 struct consumer_output
{
175 static int config_entry_handler_filter(struct handler_filter_args
*args
,
176 const char *section
, const char *name
, const char *value
)
179 struct config_entry entry
= { section
, name
, value
};
183 if (!section
|| !name
|| !value
) {
189 if (strcmp(args
->section
, section
)) {
194 ret
= args
->handler(&entry
, args
->user_data
);
200 int config_get_section_entries(const char *override_path
, const char *section
,
201 config_entry_handler_cb handler
, void *user_data
)
205 FILE *config_file
= NULL
;
206 struct handler_filter_args filter
= { section
, handler
, user_data
};
208 /* First, try system-wide conf. file. */
209 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
211 config_file
= fopen(path
, "r");
213 DBG("Loading daemon conf file at %s", path
);
215 * Return value is not very important here since error or not, we
216 * continue and try the next possible conf. file.
218 (void) ini_parse_file(config_file
,
219 (ini_entry_handler
) config_entry_handler_filter
,
224 /* Second is the user local configuration. */
225 path
= utils_get_home_dir();
227 char fullpath
[PATH_MAX
];
229 ret
= snprintf(fullpath
, sizeof(fullpath
),
230 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
232 PERROR("snprintf user conf. path");
236 config_file
= fopen(fullpath
, "r");
238 DBG("Loading daemon user conf file at %s", path
);
240 * Return value is not very important here since error or not, we
241 * continue and try the next possible conf. file.
243 (void) ini_parse_file(config_file
,
244 (ini_entry_handler
) config_entry_handler_filter
,
250 /* Final path is the one that the user might have provided. */
252 config_file
= fopen(override_path
, "r");
254 DBG("Loading daemon command line conf file at %s", override_path
);
255 (void) ini_parse_file(config_file
,
256 (ini_entry_handler
) config_entry_handler_filter
,
260 ERR("Failed to open daemon configuration file at %s",
267 /* Everything went well. */
275 int config_parse_value(const char *value
)
278 char *endptr
, *lower_str
;
288 v
= strtoul(value
, &endptr
, 10);
289 if (endptr
!= value
) {
294 lower_str
= zmalloc(len
+ 1);
301 for (i
= 0; i
< len
; i
++) {
302 lower_str
[i
] = tolower(value
[i
]);
305 if (!strcmp(lower_str
, config_str_yes
) ||
306 !strcmp(lower_str
, config_str_true
) ||
307 !strcmp(lower_str
, config_str_on
)) {
309 } else if (!strcmp(lower_str
, config_str_no
) ||
310 !strcmp(lower_str
, config_str_false
) ||
311 !strcmp(lower_str
, config_str_off
)) {
323 * Returns a xmlChar string which must be released using xmlFree().
325 static xmlChar
*encode_string(const char *in_str
)
327 xmlChar
*out_str
= NULL
;
328 xmlCharEncodingHandlerPtr handler
;
329 int out_len
, ret
, in_len
;
333 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
335 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
339 in_len
= strlen(in_str
);
341 * Add 1 byte for the NULL terminted character. The factor 2 here is
342 * because UTF-8 can be on two bytes so this fits the worst case for each
345 out_len
= (in_len
* 2) + 1;
346 out_str
= xmlMalloc(out_len
);
351 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
358 /* out_len is now the size of out_str */
359 out_str
[out_len
] = '\0';
365 struct config_writer
*config_writer_create(int fd_output
, int indent
)
368 struct config_writer
*writer
;
369 xmlOutputBufferPtr buffer
;
371 writer
= zmalloc(sizeof(struct config_writer
));
373 PERROR("zmalloc config_writer_create");
377 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
382 writer
->writer
= xmlNewTextWriter(buffer
);
383 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
384 config_xml_encoding
, NULL
);
389 ret
= xmlTextWriterSetIndentString(writer
->writer
,
390 BAD_CAST config_xml_indent_string
);
395 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
403 config_writer_destroy(writer
);
408 int config_writer_destroy(struct config_writer
*writer
)
417 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
418 WARN("Could not close XML document");
422 if (writer
->writer
) {
423 xmlFreeTextWriter(writer
->writer
);
432 int config_writer_open_element(struct config_writer
*writer
,
433 const char *element_name
)
436 xmlChar
*encoded_element_name
;
438 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
443 encoded_element_name
= encode_string(element_name
);
444 if (!encoded_element_name
) {
449 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
450 xmlFree(encoded_element_name
);
452 return ret
> 0 ? 0 : ret
;
456 int config_writer_close_element(struct config_writer
*writer
)
460 if (!writer
|| !writer
->writer
) {
465 ret
= xmlTextWriterEndElement(writer
->writer
);
467 return ret
> 0 ? 0 : ret
;
471 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
472 const char *element_name
, uint64_t value
)
475 xmlChar
*encoded_element_name
;
477 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
482 encoded_element_name
= encode_string(element_name
);
483 if (!encoded_element_name
) {
488 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
489 encoded_element_name
, "%" PRIu64
, value
);
490 xmlFree(encoded_element_name
);
492 return ret
> 0 ? 0 : ret
;
496 int config_writer_write_element_signed_int(struct config_writer
*writer
,
497 const char *element_name
, int64_t value
)
500 xmlChar
*encoded_element_name
;
502 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
507 encoded_element_name
= encode_string(element_name
);
508 if (!encoded_element_name
) {
513 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
514 encoded_element_name
, "%" PRIi64
, value
);
515 xmlFree(encoded_element_name
);
517 return ret
> 0 ? 0 : ret
;
521 int config_writer_write_element_bool(struct config_writer
*writer
,
522 const char *element_name
, int value
)
524 return config_writer_write_element_string(writer
, element_name
,
525 value
? config_xml_true
: config_xml_false
);
529 int config_writer_write_element_string(struct config_writer
*writer
,
530 const char *element_name
, const char *value
)
533 xmlChar
*encoded_element_name
= NULL
;
534 xmlChar
*encoded_value
= NULL
;
536 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
542 encoded_element_name
= encode_string(element_name
);
543 if (!encoded_element_name
) {
548 encoded_value
= encode_string(value
);
549 if (!encoded_value
) {
554 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
557 xmlFree(encoded_element_name
);
558 xmlFree(encoded_value
);
559 return ret
> 0 ? 0 : ret
;
563 void xml_error_handler(void *ctx
, const char *format
, ...)
569 va_start(args
, format
);
570 ret
= vasprintf(&errMsg
, format
, args
);
573 ERR("String allocation failed in xml error handler");
577 fprintf(stderr
, "XML Error: %s", errMsg
);
582 void fini_session_config_validation_ctx(
583 struct session_config_validation_ctx
*ctx
)
585 if (ctx
->parser_ctx
) {
586 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
590 xmlSchemaFree(ctx
->schema
);
593 if (ctx
->schema_validation_ctx
) {
594 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
597 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
601 char *get_session_config_xsd_path()
604 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
605 size_t base_path_len
;
609 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
612 base_path_len
= strlen(base_path
);
613 max_path_len
= base_path_len
+
614 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
615 xsd_path
= zmalloc(max_path_len
);
620 strncpy(xsd_path
, base_path
, max_path_len
);
621 if (xsd_path
[base_path_len
- 1] != '/') {
622 xsd_path
[base_path_len
++] = '/';
625 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
626 max_path_len
- base_path_len
);
632 int init_session_config_validation_ctx(
633 struct session_config_validation_ctx
*ctx
)
636 char *xsd_path
= get_session_config_xsd_path();
639 ret
= -LTTNG_ERR_NOMEM
;
643 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
644 if (!ctx
->parser_ctx
) {
645 ERR("XSD parser context creation failed");
646 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
649 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
650 xml_error_handler
, NULL
);
652 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
654 ERR("XSD parsing failed");
655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
659 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
660 if (!ctx
->schema_validation_ctx
) {
661 ERR("XSD validation context creation failed");
662 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
666 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
667 xml_error_handler
, NULL
);
672 fini_session_config_validation_ctx(ctx
);
680 int parse_uint(xmlChar
*str
, uint64_t *val
)
690 *val
= strtoull((const char *) str
, &endptr
, 10);
691 if (!endptr
|| *endptr
) {
702 int parse_int(xmlChar
*str
, int64_t *val
)
712 *val
= strtoll((const char *) str
, &endptr
, 10);
713 if (!endptr
|| *endptr
) {
724 int parse_bool(xmlChar
*str
, int *val
)
733 if (!strcmp((const char *) str
, config_xml_true
)) {
735 } else if (!strcmp((const char *) str
, config_xml_false
)) {
738 WARN("Invalid boolean value encoutered (%s).",
747 int get_domain_type(xmlChar
*domain
)
755 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
756 ret
= LTTNG_DOMAIN_KERNEL
;
757 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
758 ret
= LTTNG_DOMAIN_UST
;
759 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
760 ret
= LTTNG_DOMAIN_JUL
;
761 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
762 ret
= LTTNG_DOMAIN_LOG4J
;
763 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
764 ret
= LTTNG_DOMAIN_PYTHON
;
775 int get_buffer_type(xmlChar
*buffer_type
)
783 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
784 ret
= LTTNG_BUFFER_GLOBAL
;
785 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
786 ret
= LTTNG_BUFFER_PER_UID
;
787 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
788 ret
= LTTNG_BUFFER_PER_PID
;
799 int get_overwrite_mode(xmlChar
*overwrite_mode
)
803 if (!overwrite_mode
) {
807 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
809 } else if (!strcmp((char *) overwrite_mode
,
810 config_overwrite_mode_discard
)) {
822 int get_output_type(xmlChar
*output_type
)
830 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
831 ret
= LTTNG_EVENT_MMAP
;
832 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
833 ret
= LTTNG_EVENT_SPLICE
;
844 int get_event_type(xmlChar
*event_type
)
852 if (!strcmp((char *) event_type
, config_event_type_all
)) {
853 ret
= LTTNG_EVENT_ALL
;
854 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
855 ret
= LTTNG_EVENT_TRACEPOINT
;
856 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
857 ret
= LTTNG_EVENT_PROBE
;
858 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
859 ret
= LTTNG_EVENT_FUNCTION
;
860 } else if (!strcmp((char *) event_type
,
861 config_event_type_function_entry
)) {
862 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
863 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
864 ret
= LTTNG_EVENT_NOOP
;
865 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
866 ret
= LTTNG_EVENT_SYSCALL
;
877 int get_loglevel_type(xmlChar
*loglevel_type
)
881 if (!loglevel_type
) {
885 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
886 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
887 } else if (!strcmp((char *) loglevel_type
,
888 config_loglevel_type_range
)) {
889 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
890 } else if (!strcmp((char *) loglevel_type
,
891 config_loglevel_type_single
)) {
892 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
903 * Return the context type or -1 on error.
906 int get_context_type(xmlChar
*context_type
)
914 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
915 ret
= LTTNG_EVENT_CONTEXT_PID
;
916 } else if (!strcmp((char *) context_type
,
917 config_event_context_procname
)) {
918 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
919 } else if (!strcmp((char *) context_type
,
920 config_event_context_prio
)) {
921 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
922 } else if (!strcmp((char *) context_type
,
923 config_event_context_nice
)) {
924 ret
= LTTNG_EVENT_CONTEXT_NICE
;
925 } else if (!strcmp((char *) context_type
,
926 config_event_context_vpid
)) {
927 ret
= LTTNG_EVENT_CONTEXT_VPID
;
928 } else if (!strcmp((char *) context_type
,
929 config_event_context_tid
)) {
930 ret
= LTTNG_EVENT_CONTEXT_TID
;
931 } else if (!strcmp((char *) context_type
,
932 config_event_context_vtid
)) {
933 ret
= LTTNG_EVENT_CONTEXT_VTID
;
934 } else if (!strcmp((char *) context_type
,
935 config_event_context_ppid
)) {
936 ret
= LTTNG_EVENT_CONTEXT_PPID
;
937 } else if (!strcmp((char *) context_type
,
938 config_event_context_vppid
)) {
939 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
940 } else if (!strcmp((char *) context_type
,
941 config_event_context_pthread_id
)) {
942 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
943 } else if (!strcmp((char *) context_type
,
944 config_event_context_hostname
)) {
945 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
946 } else if (!strcmp((char *) context_type
,
947 config_event_context_ip
)) {
948 ret
= LTTNG_EVENT_CONTEXT_IP
;
959 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
964 for (node
= xmlFirstElementChild(domain_node
); node
;
965 node
= xmlNextElementSibling(node
)) {
966 if (!strcmp((const char *) node
->name
, config_element_type
)) {
968 xmlChar
*node_content
= xmlNodeGetContent(node
);
970 ret
= -LTTNG_ERR_NOMEM
;
974 ret
= get_domain_type(node_content
);
977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
982 } else if (!strcmp((const char *) node
->name
,
983 config_element_buffer_type
)) {
985 xmlChar
*node_content
= xmlNodeGetContent(node
);
987 ret
= -LTTNG_ERR_NOMEM
;
991 ret
= get_buffer_type(node_content
);
994 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
998 domain
->buf_type
= ret
;
1007 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1012 for (node
= xmlFirstElementChild(net_output_node
); node
;
1013 node
= xmlNextElementSibling(node
)) {
1014 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1016 *control_uri
= (char *) xmlNodeGetContent(node
);
1017 if (!*control_uri
) {
1022 *data_uri
= (char *) xmlNodeGetContent(node
);
1029 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1033 int process_consumer_output(xmlNodePtr consumer_output_node
,
1034 struct consumer_output
*output
)
1041 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1042 node
= xmlNextElementSibling(node
)) {
1043 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1044 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1048 ret
= -LTTNG_ERR_NOMEM
;
1052 ret
= parse_bool(enabled_str
, &output
->enabled
);
1058 xmlNodePtr output_type_node
;
1061 output_type_node
= xmlFirstElementChild(node
);
1062 if (!output_type_node
) {
1063 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1067 if (!strcmp((const char *) output_type_node
->name
,
1068 config_element_path
)) {
1070 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1071 if (!output
->path
) {
1072 ret
= -LTTNG_ERR_NOMEM
;
1077 ret
= get_net_output_uris(output_type_node
,
1078 &output
->control_uri
, &output
->data_uri
);
1090 free(output
->control_uri
);
1091 free(output
->data_uri
);
1092 memset(output
, 0, sizeof(struct consumer_output
));
1098 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1099 const char *control_uri
, const char *data_uri
)
1102 struct lttng_handle
*handle
;
1103 const char *uri
= NULL
;
1108 handle
= lttng_create_handle(name
, domain
);
1110 ret
= -LTTNG_ERR_NOMEM
;
1114 if (!control_uri
|| !data_uri
) {
1115 uri
= control_uri
? control_uri
: data_uri
;
1120 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1121 lttng_destroy_handle(handle
);
1127 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1130 xmlNodePtr node
= NULL
;
1131 xmlNodePtr snapshot_output_list_node
;
1132 xmlNodePtr snapshot_output_node
;
1134 assert(session_name
);
1136 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1145 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1147 /* Parse and create snapshot outputs */
1149 for (snapshot_output_node
=
1150 xmlFirstElementChild(snapshot_output_list_node
);
1151 snapshot_output_node
; snapshot_output_node
=
1152 xmlNextElementSibling(snapshot_output_node
)) {
1154 uint64_t max_size
= UINT64_MAX
;
1155 struct consumer_output output
= { 0 };
1156 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1158 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1159 node
= xmlNextElementSibling(node
)) {
1160 if (!strcmp((const char *) node
->name
,
1161 config_element_name
)) {
1163 name
= (char *) xmlNodeGetContent(node
);
1165 ret
= -LTTNG_ERR_NOMEM
;
1166 goto error_snapshot_output
;
1168 } else if (!strcmp((const char *) node
->name
,
1169 config_element_max_size
)) {
1170 xmlChar
*content
= xmlNodeGetContent(node
);
1174 ret
= -LTTNG_ERR_NOMEM
;
1175 goto error_snapshot_output
;
1177 ret
= parse_uint(content
, &max_size
);
1180 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1181 goto error_snapshot_output
;
1184 /* consumer_output */
1185 ret
= process_consumer_output(node
, &output
);
1187 goto error_snapshot_output
;
1192 snapshot_output
= lttng_snapshot_output_create();
1193 if (!snapshot_output
) {
1194 ret
= -LTTNG_ERR_NOMEM
;
1195 goto error_snapshot_output
;
1198 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1200 goto error_snapshot_output
;
1203 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1205 goto error_snapshot_output
;
1209 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1212 goto error_snapshot_output
;
1215 if (output
.control_uri
) {
1216 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1219 goto error_snapshot_output
;
1223 if (output
.data_uri
) {
1224 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1227 goto error_snapshot_output
;
1232 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1233 error_snapshot_output
:
1236 free(output
.control_uri
);
1237 free(output
.data_uri
);
1238 lttng_snapshot_output_destroy(snapshot_output
);
1248 int create_session(const char *name
,
1249 struct lttng_domain
*kernel_domain
,
1250 struct lttng_domain
*ust_domain
,
1251 struct lttng_domain
*jul_domain
,
1252 struct lttng_domain
*log4j_domain
,
1253 xmlNodePtr output_node
,
1254 uint64_t live_timer_interval
)
1257 struct consumer_output output
= { 0 };
1258 xmlNodePtr consumer_output_node
;
1263 consumer_output_node
= xmlFirstElementChild(output_node
);
1264 if (!consumer_output_node
) {
1265 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1269 if (strcmp((const char *) consumer_output_node
->name
,
1270 config_element_consumer_output
)) {
1271 WARN("Invalid output type, expected %s node",
1272 config_element_consumer_output
);
1273 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1277 ret
= process_consumer_output(consumer_output_node
, &output
);
1283 if (live_timer_interval
!= UINT64_MAX
&&
1284 !output
.control_uri
&& !output
.data_uri
) {
1285 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1289 if (output
.control_uri
|| output
.data_uri
) {
1291 struct lttng_domain
*domain
;
1292 struct lttng_domain
*domains
[] =
1293 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1295 /* network destination */
1296 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1298 * URLs are provided for sure since the test above make sure that
1299 * with a live timer the data and control URIs are provided. So,
1300 * NULL is passed here and will be set right after.
1302 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1304 ret
= lttng_create_session(name
, NULL
);
1310 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1311 domain
= domains
[i
];
1316 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1323 /* either local output or no output */
1324 ret
= lttng_create_session(name
, output
.path
);
1331 free(output
.control_uri
);
1332 free(output
.data_uri
);
1336 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1337 struct lttng_event_probe_attr
*attr
)
1341 assert(probe_attribute_node
);
1344 if (!strcmp((const char *) probe_attribute_node
->name
,
1345 config_element_address
)) {
1350 content
= xmlNodeGetContent(probe_attribute_node
);
1352 ret
= -LTTNG_ERR_NOMEM
;
1356 ret
= parse_uint(content
, &addr
);
1359 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1364 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1365 config_element_offset
)) {
1367 uint64_t offset
= 0;
1370 content
= xmlNodeGetContent(probe_attribute_node
);
1372 ret
= -LTTNG_ERR_NOMEM
;
1376 ret
= parse_uint(content
, &offset
);
1379 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1383 attr
->offset
= offset
;
1384 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1385 config_element_symbol_name
)) {
1390 content
= xmlNodeGetContent(probe_attribute_node
);
1392 ret
= -LTTNG_ERR_NOMEM
;
1396 name_len
= strlen((char *) content
);
1397 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1398 WARN("symbol_name too long.");
1399 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1404 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1413 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1414 const char *channel_name
)
1418 struct lttng_event event
;
1419 char **exclusions
= NULL
;
1420 unsigned long exclusion_count
= 0;
1421 char *filter_expression
= NULL
;
1425 assert(channel_name
);
1427 memset(&event
, 0, sizeof(event
));
1429 for (node
= xmlFirstElementChild(event_node
); node
;
1430 node
= xmlNextElementSibling(node
)) {
1431 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1436 content
= xmlNodeGetContent(node
);
1438 ret
= -LTTNG_ERR_NOMEM
;
1442 name_len
= strlen((char *) content
);
1443 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1444 WARN("Channel name too long.");
1445 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1450 strncpy(event
.name
, (const char *) content
, name_len
);
1452 } else if (!strcmp((const char *) node
->name
,
1453 config_element_enabled
)) {
1454 xmlChar
*content
= xmlNodeGetContent(node
);
1458 ret
= -LTTNG_ERR_NOMEM
;
1462 ret
= parse_bool(content
, &event
.enabled
);
1465 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1468 } else if (!strcmp((const char *) node
->name
,
1469 config_element_type
)) {
1470 xmlChar
*content
= xmlNodeGetContent(node
);
1474 ret
= -LTTNG_ERR_NOMEM
;
1478 ret
= get_event_type(content
);
1481 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1486 } else if (!strcmp((const char *) node
->name
,
1487 config_element_loglevel_type
)) {
1488 xmlChar
*content
= xmlNodeGetContent(node
);
1492 ret
= -LTTNG_ERR_NOMEM
;
1496 ret
= get_loglevel_type(content
);
1499 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1503 event
.loglevel_type
= ret
;
1504 } else if (!strcmp((const char *) node
->name
,
1505 config_element_loglevel
)) {
1507 int64_t loglevel
= 0;
1510 content
= xmlNodeGetContent(node
);
1512 ret
= -LTTNG_ERR_NOMEM
;
1516 ret
= parse_int(content
, &loglevel
);
1519 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1523 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1524 WARN("loglevel out of range.");
1525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1529 event
.loglevel
= loglevel
;
1530 } else if (!strcmp((const char *) node
->name
,
1531 config_element_filter
)) {
1533 xmlNodeGetContent(node
);
1537 ret
= -LTTNG_ERR_NOMEM
;
1541 filter_expression
= strdup((char *) content
);
1543 if (!filter_expression
) {
1544 ret
= -LTTNG_ERR_NOMEM
;
1547 } else if (!strcmp((const char *) node
->name
,
1548 config_element_exclusions
)) {
1549 xmlNodePtr exclusion_node
;
1550 int exclusion_index
= 0;
1555 * Exclusions has already been initialized,
1558 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1562 exclusion_count
= xmlChildElementCount(node
);
1563 if (!exclusion_count
) {
1567 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1569 exclusion_count
= 0;
1570 ret
= -LTTNG_ERR_NOMEM
;
1574 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1575 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1577 xmlNodeGetContent(exclusion_node
);
1580 ret
= -LTTNG_ERR_NOMEM
;
1584 exclusions
[exclusion_index
] = strdup((const char *) content
);
1586 if (!exclusions
[exclusion_index
]) {
1587 ret
= -LTTNG_ERR_NOMEM
;
1593 event
.exclusion
= 1;
1594 } else if (!strcmp((const char *) node
->name
,
1595 config_element_attributes
)) {
1596 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1599 if (!attribute_node
) {
1600 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1604 if (!strcmp((const char *) node
->name
,
1605 config_element_probe_attributes
)) {
1606 xmlNodePtr probe_attribute_node
;
1608 /* probe_attributes */
1609 for (probe_attribute_node
=
1610 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1611 probe_attribute_node
= xmlNextElementSibling(
1612 probe_attribute_node
)) {
1614 ret
= process_probe_attribute_node(probe_attribute_node
,
1623 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1625 /* function_attributes */
1626 content
= xmlNodeGetContent(symbol_node
);
1628 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1632 sym_len
= strlen((char *) content
);
1633 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1634 WARN("Function name too long.");
1635 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1640 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1647 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1648 filter_expression
, exclusion_count
, exclusions
);
1653 if (!event
.enabled
) {
1655 * Note that we should use lttng_disable_event_ext() (2.6+) to
1656 * eliminate the risk of clashing on events of the same
1657 * name (with different event types and loglevels).
1659 * Unfortunately, lttng_disable_event_ext() only performs a
1660 * match on the name and event type and errors out if any other
1661 * event attribute is not set to its default value.
1663 * This will disable all events that match this name.
1665 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1668 for (i
= 0; i
< exclusion_count
; i
++) {
1669 free(exclusions
[i
]);
1673 free(filter_expression
);
1678 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1679 const char *channel_name
)
1684 assert(events_node
);
1686 assert(channel_name
);
1688 for (node
= xmlFirstElementChild(events_node
); node
;
1689 node
= xmlNextElementSibling(node
)) {
1690 ret
= process_event_node(node
, handle
, channel_name
);
1700 int process_channel_attr_node(xmlNodePtr attr_node
,
1701 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1702 xmlNodePtr
*events_node
)
1708 assert(contexts_node
);
1709 assert(events_node
);
1711 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1716 content
= xmlNodeGetContent(attr_node
);
1718 ret
= -LTTNG_ERR_NOMEM
;
1722 name_len
= strlen((char *) content
);
1723 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1724 WARN("Channel name too long.");
1725 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1730 strncpy(channel
->name
, (const char *) content
, name_len
);
1732 } else if (!strcmp((const char *) attr_node
->name
,
1733 config_element_enabled
)) {
1738 content
= xmlNodeGetContent(attr_node
);
1740 ret
= -LTTNG_ERR_NOMEM
;
1744 ret
= parse_bool(content
, &enabled
);
1747 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1751 channel
->enabled
= enabled
;
1752 } else if (!strcmp((const char *) attr_node
->name
,
1753 config_element_overwrite_mode
)) {
1756 /* overwrite_mode */
1757 content
= xmlNodeGetContent(attr_node
);
1759 ret
= -LTTNG_ERR_NOMEM
;
1763 ret
= get_overwrite_mode(content
);
1766 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1770 channel
->attr
.overwrite
= ret
;
1771 } else if (!strcmp((const char *) attr_node
->name
,
1772 config_element_subbuf_size
)) {
1775 /* subbuffer_size */
1776 content
= xmlNodeGetContent(attr_node
);
1778 ret
= -LTTNG_ERR_NOMEM
;
1782 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1785 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1788 } else if (!strcmp((const char *) attr_node
->name
,
1789 config_element_num_subbuf
)) {
1792 /* subbuffer_count */
1793 content
= xmlNodeGetContent(attr_node
);
1795 ret
= -LTTNG_ERR_NOMEM
;
1799 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1802 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1805 } else if (!strcmp((const char *) attr_node
->name
,
1806 config_element_switch_timer_interval
)) {
1808 uint64_t switch_timer_interval
= 0;
1810 /* switch_timer_interval */
1811 content
= xmlNodeGetContent(attr_node
);
1813 ret
= -LTTNG_ERR_NOMEM
;
1817 ret
= parse_uint(content
, &switch_timer_interval
);
1820 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1824 if (switch_timer_interval
> UINT_MAX
) {
1825 WARN("switch_timer_interval out of range.");
1826 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1830 channel
->attr
.switch_timer_interval
=
1831 switch_timer_interval
;
1832 } else if (!strcmp((const char *) attr_node
->name
,
1833 config_element_read_timer_interval
)) {
1835 uint64_t read_timer_interval
= 0;
1837 /* read_timer_interval */
1838 content
= xmlNodeGetContent(attr_node
);
1840 ret
= -LTTNG_ERR_NOMEM
;
1844 ret
= parse_uint(content
, &read_timer_interval
);
1847 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1851 if (read_timer_interval
> UINT_MAX
) {
1852 WARN("read_timer_interval out of range.");
1853 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1857 channel
->attr
.read_timer_interval
=
1858 read_timer_interval
;
1859 } else if (!strcmp((const char *) attr_node
->name
,
1860 config_element_output_type
)) {
1864 content
= xmlNodeGetContent(attr_node
);
1866 ret
= -LTTNG_ERR_NOMEM
;
1870 ret
= get_output_type(content
);
1873 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1877 channel
->attr
.output
= ret
;
1878 } else if (!strcmp((const char *) attr_node
->name
,
1879 config_element_tracefile_size
)) {
1882 /* tracefile_size */
1883 content
= xmlNodeGetContent(attr_node
);
1885 ret
= -LTTNG_ERR_NOMEM
;
1889 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1892 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1895 } else if (!strcmp((const char *) attr_node
->name
,
1896 config_element_tracefile_count
)) {
1899 /* tracefile_count */
1900 content
= xmlNodeGetContent(attr_node
);
1902 ret
= -LTTNG_ERR_NOMEM
;
1906 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1909 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1912 } else if (!strcmp((const char *) attr_node
->name
,
1913 config_element_live_timer_interval
)) {
1915 uint64_t live_timer_interval
= 0;
1917 /* live_timer_interval */
1918 content
= xmlNodeGetContent(attr_node
);
1920 ret
= -LTTNG_ERR_NOMEM
;
1924 ret
= parse_uint(content
, &live_timer_interval
);
1927 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1931 if (live_timer_interval
> UINT_MAX
) {
1932 WARN("live_timer_interval out of range.");
1933 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 channel
->attr
.live_timer_interval
=
1938 live_timer_interval
;
1939 } else if (!strcmp((const char *) attr_node
->name
,
1940 config_element_events
)) {
1942 *events_node
= attr_node
;
1945 *contexts_node
= attr_node
;
1953 int process_context_node(xmlNodePtr context_node
,
1954 struct lttng_handle
*handle
, const char *channel_name
)
1957 struct lttng_event_context context
;
1958 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1961 assert(channel_name
);
1963 if (!context_child_node
) {
1964 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1968 memset(&context
, 0, sizeof(context
));
1970 if (!strcmp((const char *) context_child_node
->name
,
1971 config_element_type
)) {
1973 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1975 ret
= -LTTNG_ERR_NOMEM
;
1979 ret
= get_context_type(content
);
1982 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 xmlNodePtr perf_attr_node
;
1991 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1992 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1993 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1994 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1995 perf_attr_node
; perf_attr_node
=
1996 xmlNextElementSibling(perf_attr_node
)) {
1997 if (!strcmp((const char *) perf_attr_node
->name
,
1998 config_element_type
)) {
2003 content
= xmlNodeGetContent(perf_attr_node
);
2005 ret
= -LTTNG_ERR_NOMEM
;
2009 ret
= parse_uint(content
, &type
);
2012 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2016 if (type
> UINT32_MAX
) {
2017 WARN("perf context type out of range.");
2018 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2022 context
.u
.perf_counter
.type
= type
;
2023 } else if (!strcmp((const char *) perf_attr_node
->name
,
2024 config_element_config
)) {
2026 uint64_t config
= 0;
2029 content
= xmlNodeGetContent(perf_attr_node
);
2031 ret
= -LTTNG_ERR_NOMEM
;
2035 ret
= parse_uint(content
, &config
);
2038 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2042 context
.u
.perf_counter
.config
= config
;
2043 } else if (!strcmp((const char *) perf_attr_node
->name
,
2044 config_element_name
)) {
2049 content
= xmlNodeGetContent(perf_attr_node
);
2051 ret
= -LTTNG_ERR_NOMEM
;
2055 name_len
= strlen((char *) content
);
2056 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2057 WARN("perf context name too long.");
2058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2063 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2070 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2076 int process_contexts_node(xmlNodePtr contexts_node
,
2077 struct lttng_handle
*handle
, const char *channel_name
)
2080 xmlNodePtr context_node
;
2082 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2083 context_node
= xmlNextElementSibling(context_node
)) {
2084 ret
= process_context_node(context_node
, handle
, channel_name
);
2094 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2097 struct lttng_domain domain
= { 0 };
2098 struct lttng_handle
*handle
= NULL
;
2099 xmlNodePtr channels_node
= NULL
;
2102 assert(session_name
);
2104 ret
= init_domain(domain_node
, &domain
);
2109 handle
= lttng_create_handle(session_name
, &domain
);
2111 ret
= -LTTNG_ERR_NOMEM
;
2115 /* get the channels node */
2116 for (node
= xmlFirstElementChild(domain_node
); node
;
2117 node
= xmlNextElementSibling(node
)) {
2118 if (!strcmp((const char *) node
->name
,
2119 config_element_channels
)) {
2120 channels_node
= node
;
2125 if (!channels_node
) {
2129 /* create all channels */
2130 for (node
= xmlFirstElementChild(channels_node
); node
;
2131 node
= xmlNextElementSibling(node
)) {
2132 struct lttng_channel channel
;
2133 xmlNodePtr contexts_node
= NULL
;
2134 xmlNodePtr events_node
= NULL
;
2135 xmlNodePtr channel_attr_node
;
2137 memset(&channel
, 0, sizeof(channel
));
2138 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2140 for (channel_attr_node
= xmlFirstElementChild(node
);
2141 channel_attr_node
; channel_attr_node
=
2142 xmlNextElementSibling(channel_attr_node
)) {
2143 ret
= process_channel_attr_node(channel_attr_node
,
2144 &channel
, &contexts_node
, &events_node
);
2150 ret
= lttng_enable_channel(handle
, &channel
);
2155 ret
= process_events_node(events_node
, handle
, channel
.name
);
2160 ret
= process_contexts_node(contexts_node
, handle
,
2167 lttng_destroy_handle(handle
);
2172 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2175 int ret
, started
= -1, snapshot_mode
= -1;
2176 uint64_t live_timer_interval
= UINT64_MAX
;
2177 xmlChar
*name
= NULL
;
2178 xmlChar
*shm_path
= NULL
;
2179 xmlNodePtr domains_node
= NULL
;
2180 xmlNodePtr output_node
= NULL
;
2182 struct lttng_domain
*kernel_domain
= NULL
;
2183 struct lttng_domain
*ust_domain
= NULL
;
2184 struct lttng_domain
*jul_domain
= NULL
;
2185 struct lttng_domain
*log4j_domain
= NULL
;
2186 struct lttng_domain
*python_domain
= NULL
;
2188 for (node
= xmlFirstElementChild(session_node
); node
;
2189 node
= xmlNextElementSibling(node
)) {
2190 if (!name
&& !strcmp((const char *) node
->name
,
2191 config_element_name
)) {
2193 xmlChar
*node_content
= xmlNodeGetContent(node
);
2194 if (!node_content
) {
2195 ret
= -LTTNG_ERR_NOMEM
;
2199 name
= node_content
;
2200 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2201 config_element_domains
)) {
2203 domains_node
= node
;
2204 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2205 config_element_started
)) {
2207 xmlChar
*node_content
= xmlNodeGetContent(node
);
2208 if (!node_content
) {
2209 ret
= -LTTNG_ERR_NOMEM
;
2213 ret
= parse_bool(node_content
, &started
);
2216 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2219 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2220 config_element_output
)) {
2223 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2224 config_element_shared_memory_path
)) {
2225 /* shared memory path */
2226 xmlChar
*node_content
= xmlNodeGetContent(node
);
2227 if (!node_content
) {
2228 ret
= -LTTNG_ERR_NOMEM
;
2232 shm_path
= node_content
;
2234 /* attributes, snapshot_mode or live_timer_interval */
2235 xmlNodePtr attributes_child
=
2236 xmlFirstElementChild(node
);
2238 if (!strcmp((const char *) attributes_child
->name
,
2239 config_element_snapshot_mode
)) {
2241 xmlChar
*snapshot_mode_content
=
2242 xmlNodeGetContent(attributes_child
);
2243 if (!snapshot_mode_content
) {
2244 ret
= -LTTNG_ERR_NOMEM
;
2248 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2249 free(snapshot_mode_content
);
2251 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2255 /* live_timer_interval */
2256 xmlChar
*timer_interval_content
=
2257 xmlNodeGetContent(attributes_child
);
2258 if (!timer_interval_content
) {
2259 ret
= -LTTNG_ERR_NOMEM
;
2263 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2264 free(timer_interval_content
);
2266 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2274 /* Mandatory attribute, as defined in the session XSD */
2275 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2279 if (session_name
&& strcmp((char *) name
, session_name
)) {
2280 /* This is not the session we are looking for */
2281 ret
= -LTTNG_ERR_NO_SESSION
;
2285 /* Init domains to create the session handles */
2286 for (node
= xmlFirstElementChild(domains_node
); node
;
2287 node
= xmlNextElementSibling(node
)) {
2288 struct lttng_domain
*domain
;
2290 domain
= zmalloc(sizeof(*domain
));
2292 ret
= -LTTNG_ERR_NOMEM
;
2296 ret
= init_domain(node
, domain
);
2298 goto domain_init_error
;
2301 switch (domain
->type
) {
2302 case LTTNG_DOMAIN_KERNEL
:
2303 if (kernel_domain
) {
2304 /* Same domain seen twice, invalid! */
2305 goto domain_init_error
;
2307 kernel_domain
= domain
;
2309 case LTTNG_DOMAIN_UST
:
2311 /* Same domain seen twice, invalid! */
2312 goto domain_init_error
;
2314 ust_domain
= domain
;
2316 case LTTNG_DOMAIN_JUL
:
2318 /* Same domain seen twice, invalid! */
2319 goto domain_init_error
;
2321 jul_domain
= domain
;
2323 case LTTNG_DOMAIN_LOG4J
:
2325 /* Same domain seen twice, invalid! */
2326 goto domain_init_error
;
2328 log4j_domain
= domain
;
2330 case LTTNG_DOMAIN_PYTHON
:
2331 if (python_domain
) {
2332 /* Same domain seen twice, invalid! */
2333 goto domain_init_error
;
2335 python_domain
= domain
;
2338 WARN("Invalid domain type");
2339 goto domain_init_error
;
2344 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2349 /* Destroy session if it exists */
2350 ret
= lttng_destroy_session((const char *) name
);
2351 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2352 ERR("Failed to destroy existing session.");
2357 /* Create session type depending on output type */
2358 if (snapshot_mode
&& snapshot_mode
!= -1) {
2359 ret
= create_snapshot_session((const char *) name
, output_node
);
2360 } else if (live_timer_interval
&&
2361 live_timer_interval
!= UINT64_MAX
) {
2362 ret
= create_session((const char *) name
, kernel_domain
,
2363 ust_domain
, jul_domain
, log4j_domain
,
2364 output_node
, live_timer_interval
);
2366 /* regular session */
2367 ret
= create_session((const char *) name
, kernel_domain
,
2368 ust_domain
, jul_domain
, log4j_domain
,
2369 output_node
, UINT64_MAX
);
2376 ret
= lttng_set_session_shm_path((const char *) name
,
2377 (const char *) shm_path
);
2383 for (node
= xmlFirstElementChild(domains_node
); node
;
2384 node
= xmlNextElementSibling(node
)) {
2385 ret
= process_domain_node(node
, (const char *) name
);
2392 ret
= lttng_start_tracing((const char *) name
);
2400 ERR("Failed to load session %s: %s", (const char *) name
,
2401 lttng_strerror(ret
));
2402 lttng_destroy_session((const char *) name
);
2406 free(kernel_domain
);
2410 free(python_domain
);
2417 * Return 1 if the given path is readable by the current UID or 0 if not.
2418 * Return -1 if the path is EPERM.
2420 static int validate_file_read_creds(const char *path
)
2426 /* Can we read the file. */
2427 ret
= access(path
, R_OK
);
2431 if (errno
== EACCES
) {
2442 int load_session_from_file(const char *path
, const char *session_name
,
2443 struct session_config_validation_ctx
*validation_ctx
, int override
)
2445 int ret
, session_found
= !session_name
;
2446 xmlDocPtr doc
= NULL
;
2447 xmlNodePtr sessions_node
;
2448 xmlNodePtr session_node
;
2451 assert(validation_ctx
);
2453 ret
= validate_file_read_creds(path
);
2456 ret
= -LTTNG_ERR_EPERM
;
2458 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2463 doc
= xmlParseFile(path
);
2465 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2469 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2471 ERR("Session configuration file validation failed");
2472 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2476 sessions_node
= xmlDocGetRootElement(doc
);
2477 if (!sessions_node
) {
2481 for (session_node
= xmlFirstElementChild(sessions_node
);
2482 session_node
; session_node
=
2483 xmlNextElementSibling(session_node
)) {
2484 ret
= process_session_node(session_node
,
2485 session_name
, override
);
2486 if (session_name
&& ret
== 0) {
2487 /* Target session found and loaded */
2495 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2500 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2502 struct dirent
*alloc_dirent(const char *path
)
2506 struct dirent
*entry
;
2508 name_max
= pathconf(path
, _PC_NAME_MAX
);
2509 if (name_max
== -1) {
2510 name_max
= PATH_MAX
;
2512 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2513 entry
= zmalloc(len
);
2518 int load_session_from_path(const char *path
, const char *session_name
,
2519 struct session_config_validation_ctx
*validation_ctx
, int override
)
2521 int ret
, session_found
= !session_name
;
2522 DIR *directory
= NULL
;
2525 assert(validation_ctx
);
2527 directory
= opendir(path
);
2531 /* Try the file loading. */
2534 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2537 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2542 struct dirent
*entry
;
2543 struct dirent
*result
;
2544 char *file_path
= NULL
;
2545 size_t path_len
= strlen(path
);
2547 if (path_len
>= PATH_MAX
) {
2548 ret
= -LTTNG_ERR_INVALID
;
2552 entry
= alloc_dirent(path
);
2554 ret
= -LTTNG_ERR_NOMEM
;
2558 file_path
= zmalloc(PATH_MAX
);
2560 ret
= -LTTNG_ERR_NOMEM
;
2565 strncpy(file_path
, path
, path_len
);
2566 if (file_path
[path_len
- 1] != '/') {
2567 file_path
[path_len
++] = '/';
2571 /* Search for *.lttng files */
2572 while (!readdir_r(directory
, entry
, &result
) && result
) {
2573 size_t file_name_len
= strlen(result
->d_name
);
2575 if (file_name_len
<=
2576 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2580 if (path_len
+ file_name_len
>= PATH_MAX
) {
2584 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2585 result
->d_name
+ file_name_len
- sizeof(
2586 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2590 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2591 file_path
[path_len
+ file_name_len
] = '\0';
2593 ret
= load_session_from_file(file_path
, session_name
,
2594 validation_ctx
, override
);
2595 if (session_name
&& !ret
) {
2604 ret
= load_session_from_file(path
, session_name
,
2605 validation_ctx
, override
);
2615 if (closedir(directory
)) {
2620 if (!session_found
) {
2621 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2628 * Validate that the given path's credentials and the current process have the
2629 * same UID. If so, return 1 else return 0 if it does NOT match.
2631 static int validate_path_creds(const char *path
)
2633 int ret
, uid
= getuid();
2642 ret
= stat(path
, &buf
);
2644 if (errno
!= ENOENT
) {
2647 ret
= -LTTNG_ERR_INVALID
;
2651 if (buf
.st_uid
!= uid
) {
2662 int config_load_session(const char *path
, const char *session_name
,
2663 int override
, unsigned int autoload
)
2666 const char *path_ptr
= NULL
;
2667 struct session_config_validation_ctx validation_ctx
= { 0 };
2669 ret
= init_session_config_validation_ctx(&validation_ctx
);
2676 const char *sys_path
;
2679 home_path
= utils_get_home_dir();
2681 char path
[PATH_MAX
];
2684 * Try user session configuration path. Ignore error here so we can
2685 * continue loading the system wide sessions.
2688 ret
= snprintf(path
, sizeof(path
),
2689 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2690 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2692 PERROR("snprintf session autoload home config path");
2697 * Credentials are only validated for the autoload in order to
2698 * avoid any user session daemon to try to load kernel sessions
2699 * automatically and failing all the times.
2701 ret
= validate_path_creds(path
);
2706 ret
= snprintf(path
, sizeof(path
),
2707 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2709 PERROR("snprintf session home config path");
2715 ret
= load_session_from_path(path_ptr
, session_name
,
2716 &validation_ctx
, override
);
2717 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2721 * Continue even if the session was found since we have to try
2722 * the system wide sessions.
2727 /* Reset path pointer for the system wide dir. */
2730 /* Try system wide configuration directory. */
2732 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2733 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2734 ret
= validate_path_creds(sys_path
);
2736 path_ptr
= sys_path
;
2739 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2740 path_ptr
= sys_path
;
2744 ret
= load_session_from_path(path_ptr
, session_name
,
2745 &validation_ctx
, override
);
2748 ret
= access(path
, F_OK
);
2753 ret
= -LTTNG_ERR_INVALID
;
2754 WARN("Session configuration path does not exist.");
2757 ret
= -LTTNG_ERR_EPERM
;
2760 ret
= -LTTNG_ERR_UNK
;
2766 ret
= load_session_from_path(path
, session_name
,
2767 &validation_ctx
, override
);
2770 fini_session_config_validation_ctx(&validation_ctx
);
2771 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2773 * Don't report an error if no sessions are found when called
2774 * without a session_name or a search path.