2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
30 #include <common/defaults.h>
31 #include <common/error.h>
32 #include <common/macros.h>
33 #include <common/utils.h>
34 #include <lttng/lttng-error.h>
35 #include <libxml/parser.h>
36 #include <libxml/valid.h>
37 #include <libxml/xmlschemas.h>
38 #include <libxml/tree.h>
39 #include <lttng/lttng.h>
40 #include <lttng/snapshot.h>
43 #include "config-internal.h"
45 struct handler_filter_args
{
47 config_entry_handler_cb handler
;
51 struct session_config_validation_ctx
{
52 xmlSchemaParserCtxtPtr parser_ctx
;
54 xmlSchemaValidCtxtPtr schema_validation_ctx
;
57 const char * const config_str_yes
= "yes";
58 const char * const config_str_true
= "true";
59 const char * const config_str_on
= "on";
60 const char * const config_str_no
= "no";
61 const char * const config_str_false
= "false";
62 const char * const config_str_off
= "off";
63 const char * const config_xml_encoding
= "UTF-8";
64 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
65 const char * const config_xml_indent_string
= "\t";
66 const char * const config_xml_true
= "true";
67 const char * const config_xml_false
= "false";
69 const char * const config_element_channel
= "channel";
70 const char * const config_element_channels
= "channels";
71 const char * const config_element_domain
= "domain";
72 const char * const config_element_domains
= "domains";
73 const char * const config_element_event
= "event";
74 const char * const config_element_events
= "events";
75 const char * const config_element_context
= "context";
76 const char * const config_element_contexts
= "contexts";
77 const char * const config_element_attributes
= "attributes";
78 const char * const config_element_exclusion
= "exclusion";
79 const char * const config_element_exclusions
= "exclusions";
80 const char * const config_element_function_attributes
= "function_attributes";
81 const char * const config_element_probe_attributes
= "probe_attributes";
82 const char * const config_element_symbol_name
= "symbol_name";
83 const char * const config_element_address
= "address";
84 const char * const config_element_offset
= "offset";
85 const char * const config_element_name
= "name";
86 const char * const config_element_enabled
= "enabled";
87 const char * const config_element_overwrite_mode
= "overwrite_mode";
88 const char * const config_element_subbuf_size
= "subbuffer_size";
89 const char * const config_element_num_subbuf
= "subbuffer_count";
90 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
91 const char * const config_element_read_timer_interval
= "read_timer_interval";
92 const char * const config_element_output
= "output";
93 const char * const config_element_output_type
= "output_type";
94 const char * const config_element_tracefile_size
= "tracefile_size";
95 const char * const config_element_tracefile_count
= "tracefile_count";
96 const char * const config_element_live_timer_interval
= "live_timer_interval";
97 const char * const config_element_type
= "type";
98 const char * const config_element_buffer_type
= "buffer_type";
99 const char * const config_element_session
= "session";
100 const char * const config_element_sessions
= "sessions";
101 const char * const config_element_perf
= "perf";
102 const char * const config_element_config
= "config";
103 const char * const config_element_started
= "started";
104 const char * const config_element_snapshot_mode
= "snapshot_mode";
105 const char * const config_element_loglevel
= "loglevel";
106 const char * const config_element_loglevel_type
= "loglevel_type";
107 const char * const config_element_filter
= "filter";
108 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
109 const char * const config_element_consumer_output
= "consumer_output";
110 const char * const config_element_destination
= "destination";
111 const char * const config_element_path
= "path";
112 const char * const config_element_net_output
= "net_output";
113 const char * const config_element_control_uri
= "control_uri";
114 const char * const config_element_data_uri
= "data_uri";
115 const char * const config_element_max_size
= "max_size";
117 const char * const config_domain_type_kernel
= "KERNEL";
118 const char * const config_domain_type_ust
= "UST";
119 const char * const config_domain_type_jul
= "JUL";
121 const char * const config_buffer_type_per_pid
= "PER_PID";
122 const char * const config_buffer_type_per_uid
= "PER_UID";
123 const char * const config_buffer_type_global
= "GLOBAL";
125 const char * const config_overwrite_mode_discard
= "DISCARD";
126 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
128 const char * const config_output_type_splice
= "SPLICE";
129 const char * const config_output_type_mmap
= "MMAP";
131 const char * const config_loglevel_type_all
= "ALL";
132 const char * const config_loglevel_type_range
= "RANGE";
133 const char * const config_loglevel_type_single
= "SINGLE";
135 const char * const config_event_type_all
= "ALL";
136 const char * const config_event_type_tracepoint
= "TRACEPOINT";
137 const char * const config_event_type_probe
= "PROBE";
138 const char * const config_event_type_function
= "FUNCTION";
139 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
140 const char * const config_event_type_noop
= "NOOP";
141 const char * const config_event_type_syscall
= "SYSCALL";
142 const char * const config_event_type_kprobe
= "KPROBE";
143 const char * const config_event_type_kretprobe
= "KRETPROBE";
145 const char * const config_event_context_pid
= "PID";
146 const char * const config_event_context_procname
= "PROCNAME";
147 const char * const config_event_context_prio
= "PRIO";
148 const char * const config_event_context_nice
= "NICE";
149 const char * const config_event_context_vpid
= "VPID";
150 const char * const config_event_context_tid
= "TID";
151 const char * const config_event_context_vtid
= "VTID";
152 const char * const config_event_context_ppid
= "PPID";
153 const char * const config_event_context_vppid
= "VPPID";
154 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
155 const char * const config_event_context_hostname
= "HOSTNAME";
156 const char * const config_event_context_ip
= "IP";
157 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
159 struct consumer_output
{
166 static int config_entry_handler_filter(struct handler_filter_args
*args
,
167 const char *section
, const char *name
, const char *value
)
170 struct config_entry entry
= { section
, name
, value
};
174 if (!section
|| !name
|| !value
) {
180 if (strcmp(args
->section
, section
)) {
185 ret
= args
->handler(&entry
, args
->user_data
);
191 int config_get_section_entries(const char *override_path
, const char *section
,
192 config_entry_handler_cb handler
, void *user_data
)
196 FILE *config_file
= NULL
;
197 struct handler_filter_args filter
= { section
, handler
, user_data
};
199 /* First, try system-wide conf. file. */
200 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
202 config_file
= fopen(path
, "r");
204 DBG("Loading daemon conf file at %s", path
);
206 * Return value is not very important here since error or not, we
207 * continue and try the next possible conf. file.
209 (void) ini_parse_file(config_file
,
210 (ini_entry_handler
) config_entry_handler_filter
,
215 /* Second is the user local configuration. */
216 path
= utils_get_home_dir();
218 char fullpath
[PATH_MAX
];
220 ret
= snprintf(fullpath
, sizeof(fullpath
),
221 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
223 PERROR("snprintf user conf. path");
227 config_file
= fopen(fullpath
, "r");
229 DBG("Loading daemon user conf file at %s", path
);
231 * Return value is not very important here since error or not, we
232 * continue and try the next possible conf. file.
234 (void) ini_parse_file(config_file
,
235 (ini_entry_handler
) config_entry_handler_filter
,
241 /* Final path is the one that the user might have provided. */
243 config_file
= fopen(override_path
, "r");
245 DBG("Loading daemon command line conf file at %s", override_path
);
246 (void) ini_parse_file(config_file
,
247 (ini_entry_handler
) config_entry_handler_filter
,
251 ERR("Failed to open daemon configuration file at %s",
258 /* Everything went well. */
266 int config_parse_value(const char *value
)
269 char *endptr
, *lower_str
;
279 v
= strtoul(value
, &endptr
, 10);
280 if (endptr
!= value
) {
285 lower_str
= zmalloc(len
+ 1);
292 for (i
= 0; i
< len
; i
++) {
293 lower_str
[i
] = tolower(value
[i
]);
296 if (!strcmp(lower_str
, config_str_yes
) ||
297 !strcmp(lower_str
, config_str_true
) ||
298 !strcmp(lower_str
, config_str_on
)) {
300 } else if (!strcmp(lower_str
, config_str_no
) ||
301 !strcmp(lower_str
, config_str_false
) ||
302 !strcmp(lower_str
, config_str_off
)) {
314 * Returns a xmlChar string which must be released using xmlFree().
316 static xmlChar
*encode_string(const char *in_str
)
318 xmlChar
*out_str
= NULL
;
319 xmlCharEncodingHandlerPtr handler
;
320 int out_len
, ret
, in_len
;
324 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
326 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
330 in_len
= strlen(in_str
);
332 * Add 1 byte for the NULL terminted character. The factor 2 here is
333 * because UTF-8 can be on two bytes so this fits the worst case for each
336 out_len
= (in_len
* 2) + 1;
337 out_str
= xmlMalloc(out_len
);
342 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
349 /* out_len is now the size of out_str */
350 out_str
[out_len
] = '\0';
356 struct config_writer
*config_writer_create(int fd_output
)
359 struct config_writer
*writer
;
360 xmlOutputBufferPtr buffer
;
362 writer
= zmalloc(sizeof(struct config_writer
));
364 PERROR("zmalloc config_writer_create");
368 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
373 writer
->writer
= xmlNewTextWriter(buffer
);
374 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
375 config_xml_encoding
, NULL
);
380 ret
= xmlTextWriterSetIndentString(writer
->writer
,
381 BAD_CAST config_xml_indent_string
);
386 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
394 config_writer_destroy(writer
);
399 int config_writer_destroy(struct config_writer
*writer
)
408 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
409 WARN("Could not close XML document");
413 if (writer
->writer
) {
414 xmlFreeTextWriter(writer
->writer
);
423 int config_writer_open_element(struct config_writer
*writer
,
424 const char *element_name
)
427 xmlChar
*encoded_element_name
;
429 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
434 encoded_element_name
= encode_string(element_name
);
435 if (!encoded_element_name
) {
440 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
441 xmlFree(encoded_element_name
);
443 return ret
> 0 ? 0 : ret
;
447 int config_writer_close_element(struct config_writer
*writer
)
451 if (!writer
|| !writer
->writer
) {
456 ret
= xmlTextWriterEndElement(writer
->writer
);
458 return ret
> 0 ? 0 : ret
;
462 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
463 const char *element_name
, uint64_t value
)
466 xmlChar
*encoded_element_name
;
468 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
473 encoded_element_name
= encode_string(element_name
);
474 if (!encoded_element_name
) {
479 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
480 encoded_element_name
, "%" PRIu64
, value
);
481 xmlFree(encoded_element_name
);
483 return ret
> 0 ? 0 : ret
;
487 int config_writer_write_element_signed_int(struct config_writer
*writer
,
488 const char *element_name
, int64_t value
)
491 xmlChar
*encoded_element_name
;
493 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
498 encoded_element_name
= encode_string(element_name
);
499 if (!encoded_element_name
) {
504 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
505 encoded_element_name
, "%" PRIi64
, value
);
506 xmlFree(encoded_element_name
);
508 return ret
> 0 ? 0 : ret
;
512 int config_writer_write_element_bool(struct config_writer
*writer
,
513 const char *element_name
, int value
)
515 return config_writer_write_element_string(writer
, element_name
,
516 value
? config_xml_true
: config_xml_false
);
520 int config_writer_write_element_string(struct config_writer
*writer
,
521 const char *element_name
, const char *value
)
524 xmlChar
*encoded_element_name
= NULL
;
525 xmlChar
*encoded_value
= NULL
;
527 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
533 encoded_element_name
= encode_string(element_name
);
534 if (!encoded_element_name
) {
539 encoded_value
= encode_string(value
);
540 if (!encoded_value
) {
545 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
548 xmlFree(encoded_element_name
);
549 xmlFree(encoded_value
);
550 return ret
> 0 ? 0 : ret
;
554 void xml_error_handler(void *ctx
, const char *format
, ...)
560 va_start(args
, format
);
561 ret
= vasprintf(&errMsg
, format
, args
);
564 ERR("String allocation failed in xml error handler");
568 fprintf(stderr
, "XML Error: %s", errMsg
);
573 void fini_session_config_validation_ctx(
574 struct session_config_validation_ctx
*ctx
)
576 if (ctx
->parser_ctx
) {
577 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
581 xmlSchemaFree(ctx
->schema
);
584 if (ctx
->schema_validation_ctx
) {
585 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
588 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
592 char *get_session_config_xsd_path()
595 const char *base_path
= getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
596 size_t base_path_len
;
600 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
603 base_path_len
= strlen(base_path
);
604 max_path_len
= base_path_len
+
605 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
606 xsd_path
= zmalloc(max_path_len
);
611 strncpy(xsd_path
, base_path
, max_path_len
);
612 if (xsd_path
[base_path_len
- 1] != '/') {
613 xsd_path
[base_path_len
++] = '/';
616 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
617 max_path_len
- base_path_len
);
623 int init_session_config_validation_ctx(
624 struct session_config_validation_ctx
*ctx
)
627 char *xsd_path
= get_session_config_xsd_path();
630 ret
= -LTTNG_ERR_NOMEM
;
634 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
635 if (!ctx
->parser_ctx
) {
636 ERR("XSD parser context creation failed");
637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
640 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
641 xml_error_handler
, NULL
);
643 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
645 ERR("XSD parsing failed");
646 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
650 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
651 if (!ctx
->schema_validation_ctx
) {
652 ERR("XSD validation context creation failed");
653 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
657 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
658 xml_error_handler
, NULL
);
663 fini_session_config_validation_ctx(ctx
);
671 int parse_uint(xmlChar
*str
, uint64_t *val
)
681 *val
= strtoull((const char *) str
, &endptr
, 10);
682 if (!endptr
|| *endptr
) {
693 int parse_int(xmlChar
*str
, int64_t *val
)
703 *val
= strtoll((const char *) str
, &endptr
, 10);
704 if (!endptr
|| *endptr
) {
715 int parse_bool(xmlChar
*str
, int *val
)
724 if (!strcmp((const char *) str
, config_xml_true
)) {
726 } else if (!strcmp((const char *) str
, config_xml_false
)) {
729 WARN("Invalid boolean value encoutered (%s).",
738 int get_domain_type(xmlChar
*domain
)
746 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
747 ret
= LTTNG_DOMAIN_KERNEL
;
748 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
749 ret
= LTTNG_DOMAIN_UST
;
750 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
751 ret
= LTTNG_DOMAIN_JUL
;
762 int get_buffer_type(xmlChar
*buffer_type
)
770 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
771 ret
= LTTNG_BUFFER_GLOBAL
;
772 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
773 ret
= LTTNG_BUFFER_PER_UID
;
774 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
775 ret
= LTTNG_BUFFER_PER_PID
;
786 int get_overwrite_mode(xmlChar
*overwrite_mode
)
790 if (!overwrite_mode
) {
794 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
796 } else if (!strcmp((char *) overwrite_mode
,
797 config_overwrite_mode_discard
)) {
809 int get_output_type(xmlChar
*output_type
)
817 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
818 ret
= LTTNG_EVENT_MMAP
;
819 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
820 ret
= LTTNG_EVENT_SPLICE
;
831 int get_event_type(xmlChar
*event_type
)
839 if (!strcmp((char *) event_type
, config_event_type_all
)) {
840 ret
= LTTNG_EVENT_ALL
;
841 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
842 ret
= LTTNG_EVENT_TRACEPOINT
;
843 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
844 ret
= LTTNG_EVENT_PROBE
;
845 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
846 ret
= LTTNG_EVENT_FUNCTION
;
847 } else if (!strcmp((char *) event_type
,
848 config_event_type_function_entry
)) {
849 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
850 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
851 ret
= LTTNG_EVENT_NOOP
;
852 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
853 ret
= LTTNG_EVENT_SYSCALL
;
864 int get_loglevel_type(xmlChar
*loglevel_type
)
868 if (!loglevel_type
) {
872 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
873 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
874 } else if (!strcmp((char *) loglevel_type
,
875 config_loglevel_type_range
)) {
876 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
877 } else if (!strcmp((char *) loglevel_type
,
878 config_loglevel_type_single
)) {
879 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
890 * Return the context type or -1 on error.
893 int get_context_type(xmlChar
*context_type
)
901 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
902 ret
= LTTNG_EVENT_CONTEXT_PID
;
903 } else if (!strcmp((char *) context_type
,
904 config_event_context_procname
)) {
905 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
906 } else if (!strcmp((char *) context_type
,
907 config_event_context_prio
)) {
908 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
909 } else if (!strcmp((char *) context_type
,
910 config_event_context_nice
)) {
911 ret
= LTTNG_EVENT_CONTEXT_NICE
;
912 } else if (!strcmp((char *) context_type
,
913 config_event_context_vpid
)) {
914 ret
= LTTNG_EVENT_CONTEXT_VPID
;
915 } else if (!strcmp((char *) context_type
,
916 config_event_context_tid
)) {
917 ret
= LTTNG_EVENT_CONTEXT_TID
;
918 } else if (!strcmp((char *) context_type
,
919 config_event_context_vtid
)) {
920 ret
= LTTNG_EVENT_CONTEXT_VTID
;
921 } else if (!strcmp((char *) context_type
,
922 config_event_context_ppid
)) {
923 ret
= LTTNG_EVENT_CONTEXT_PPID
;
924 } else if (!strcmp((char *) context_type
,
925 config_event_context_vppid
)) {
926 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
927 } else if (!strcmp((char *) context_type
,
928 config_event_context_pthread_id
)) {
929 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
930 } else if (!strcmp((char *) context_type
,
931 config_event_context_hostname
)) {
932 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
933 } else if (!strcmp((char *) context_type
,
934 config_event_context_ip
)) {
935 ret
= LTTNG_EVENT_CONTEXT_IP
;
946 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
951 for (node
= xmlFirstElementChild(domain_node
); node
;
952 node
= xmlNextElementSibling(node
)) {
953 if (!strcmp((const char *) node
->name
, config_element_type
)) {
955 xmlChar
*node_content
= xmlNodeGetContent(node
);
957 ret
= -LTTNG_ERR_NOMEM
;
961 ret
= get_domain_type(node_content
);
964 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
969 } else if (!strcmp((const char *) node
->name
,
970 config_element_buffer_type
)) {
972 xmlChar
*node_content
= xmlNodeGetContent(node
);
974 ret
= -LTTNG_ERR_NOMEM
;
978 ret
= get_buffer_type(node_content
);
981 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
985 domain
->buf_type
= ret
;
994 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
999 for (node
= xmlFirstElementChild(net_output_node
); node
;
1000 node
= xmlNextElementSibling(node
)) {
1001 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1003 *control_uri
= (char *) xmlNodeGetContent(node
);
1004 if (!*control_uri
) {
1009 *data_uri
= (char *) xmlNodeGetContent(node
);
1016 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1020 int process_consumer_output(xmlNodePtr consumer_output_node
,
1021 struct consumer_output
*output
)
1028 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1029 node
= xmlNextElementSibling(node
)) {
1030 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1031 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1035 ret
= -LTTNG_ERR_NOMEM
;
1039 ret
= parse_bool(enabled_str
, &output
->enabled
);
1045 xmlNodePtr output_type_node
;
1048 output_type_node
= xmlFirstElementChild(node
);
1049 if (!output_type_node
) {
1050 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1054 if (!strcmp((const char *) output_type_node
->name
,
1055 config_element_path
)) {
1057 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1058 if (!output
->path
) {
1059 ret
= -LTTNG_ERR_NOMEM
;
1064 ret
= get_net_output_uris(output_type_node
,
1065 &output
->control_uri
, &output
->data_uri
);
1077 free(output
->control_uri
);
1078 free(output
->data_uri
);
1079 memset(output
, 0, sizeof(struct consumer_output
));
1085 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1086 const char *control_uri
, const char *data_uri
)
1089 struct lttng_handle
*handle
;
1090 const char *uri
= NULL
;
1095 handle
= lttng_create_handle(name
, domain
);
1097 ret
= -LTTNG_ERR_NOMEM
;
1101 if (!control_uri
|| !data_uri
) {
1102 uri
= control_uri
? control_uri
: data_uri
;
1107 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1108 lttng_destroy_handle(handle
);
1114 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1117 xmlNodePtr node
= NULL
;
1118 xmlNodePtr snapshot_output_list_node
;
1119 xmlNodePtr snapshot_output_node
;
1121 assert(session_name
);
1123 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1132 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1134 /* Parse and create snapshot outputs */
1136 for (snapshot_output_node
=
1137 xmlFirstElementChild(snapshot_output_list_node
);
1138 snapshot_output_node
; snapshot_output_node
=
1139 xmlNextElementSibling(snapshot_output_node
)) {
1141 uint64_t max_size
= UINT64_MAX
;
1142 struct consumer_output output
= { 0 };
1143 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1145 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1146 node
= xmlNextElementSibling(node
)) {
1147 if (!strcmp((const char *) node
->name
,
1148 config_element_name
)) {
1150 name
= (char *) xmlNodeGetContent(node
);
1152 ret
= -LTTNG_ERR_NOMEM
;
1153 goto error_snapshot_output
;
1155 } else if (!strcmp((const char *) node
->name
,
1156 config_element_max_size
)) {
1157 xmlChar
*content
= xmlNodeGetContent(node
);
1161 ret
= -LTTNG_ERR_NOMEM
;
1162 goto error_snapshot_output
;
1164 ret
= parse_uint(content
, &max_size
);
1167 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1168 goto error_snapshot_output
;
1171 /* consumer_output */
1172 ret
= process_consumer_output(node
, &output
);
1174 goto error_snapshot_output
;
1179 snapshot_output
= lttng_snapshot_output_create();
1180 if (!snapshot_output
) {
1181 ret
= -LTTNG_ERR_NOMEM
;
1182 goto error_snapshot_output
;
1185 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1187 goto error_snapshot_output
;
1190 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1192 goto error_snapshot_output
;
1196 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1199 goto error_snapshot_output
;
1202 if (output
.control_uri
) {
1203 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1206 goto error_snapshot_output
;
1210 if (output
.data_uri
) {
1211 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1214 goto error_snapshot_output
;
1219 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1220 error_snapshot_output
:
1223 free(output
.control_uri
);
1224 free(output
.data_uri
);
1225 lttng_snapshot_output_destroy(snapshot_output
);
1235 int create_session(const char *name
,
1236 struct lttng_domain
*kernel_domain
,
1237 struct lttng_domain
*ust_domain
,
1238 struct lttng_domain
*jul_domain
,
1239 xmlNodePtr output_node
,
1240 uint64_t live_timer_interval
)
1243 struct consumer_output output
= { 0 };
1244 xmlNodePtr consumer_output_node
;
1249 consumer_output_node
= xmlFirstElementChild(output_node
);
1250 if (!consumer_output_node
) {
1251 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1255 if (strcmp((const char *) consumer_output_node
->name
,
1256 config_element_consumer_output
)) {
1257 WARN("Invalid output type, expected %s node",
1258 config_element_consumer_output
);
1259 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1263 ret
= process_consumer_output(consumer_output_node
, &output
);
1269 if (live_timer_interval
!= UINT64_MAX
&&
1270 !output
.control_uri
&& !output
.data_uri
) {
1271 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1275 if (output
.control_uri
|| output
.data_uri
) {
1277 struct lttng_domain
*domain
;
1278 struct lttng_domain
*domains
[] =
1279 { kernel_domain
, ust_domain
, jul_domain
};
1281 /* network destination */
1282 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1284 * URLs are provided for sure since the test above make sure that
1285 * with a live timer the data and control URIs are provided. So,
1286 * NULL is passed here and will be set right after.
1288 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1290 ret
= lttng_create_session(name
, NULL
);
1296 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1297 domain
= domains
[i
];
1302 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1309 /* either local output or no output */
1310 ret
= lttng_create_session(name
, output
.path
);
1317 free(output
.control_uri
);
1318 free(output
.data_uri
);
1322 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1323 struct lttng_event_probe_attr
*attr
)
1327 assert(probe_attribute_node
);
1330 if (!strcmp((const char *) probe_attribute_node
->name
,
1331 config_element_address
)) {
1336 content
= xmlNodeGetContent(probe_attribute_node
);
1338 ret
= -LTTNG_ERR_NOMEM
;
1342 ret
= parse_uint(content
, &addr
);
1345 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1350 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1351 config_element_offset
)) {
1353 uint64_t offset
= 0;
1356 content
= xmlNodeGetContent(probe_attribute_node
);
1358 ret
= -LTTNG_ERR_NOMEM
;
1362 ret
= parse_uint(content
, &offset
);
1365 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1369 attr
->offset
= offset
;
1370 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1371 config_element_symbol_name
)) {
1376 content
= xmlNodeGetContent(probe_attribute_node
);
1378 ret
= -LTTNG_ERR_NOMEM
;
1382 name_len
= strlen((char *) content
);
1383 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1384 WARN("symbol_name too long.");
1385 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1390 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1399 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1400 const char *channel_name
)
1404 struct lttng_event event
;
1405 char **exclusions
= NULL
;
1406 unsigned long exclusion_count
= 0;
1407 char *filter_expression
= NULL
;
1411 assert(channel_name
);
1413 memset(&event
, 0, sizeof(event
));
1415 for (node
= xmlFirstElementChild(event_node
); node
;
1416 node
= xmlNextElementSibling(node
)) {
1417 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1422 content
= xmlNodeGetContent(node
);
1424 ret
= -LTTNG_ERR_NOMEM
;
1428 name_len
= strlen((char *) content
);
1429 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1430 WARN("Channel name too long.");
1431 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1436 strncpy(event
.name
, (const char *) content
, name_len
);
1438 } else if (!strcmp((const char *) node
->name
,
1439 config_element_enabled
)) {
1440 xmlChar
*content
= xmlNodeGetContent(node
);
1444 ret
= -LTTNG_ERR_NOMEM
;
1448 ret
= parse_bool(content
, &event
.enabled
);
1451 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1454 } else if (!strcmp((const char *) node
->name
,
1455 config_element_type
)) {
1456 xmlChar
*content
= xmlNodeGetContent(node
);
1460 ret
= -LTTNG_ERR_NOMEM
;
1464 ret
= get_event_type(content
);
1467 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1472 } else if (!strcmp((const char *) node
->name
,
1473 config_element_loglevel_type
)) {
1474 xmlChar
*content
= xmlNodeGetContent(node
);
1478 ret
= -LTTNG_ERR_NOMEM
;
1482 ret
= get_loglevel_type(content
);
1485 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1489 event
.loglevel_type
= ret
;
1490 } else if (!strcmp((const char *) node
->name
,
1491 config_element_loglevel
)) {
1493 int64_t loglevel
= 0;
1496 content
= xmlNodeGetContent(node
);
1498 ret
= -LTTNG_ERR_NOMEM
;
1502 ret
= parse_int(content
, &loglevel
);
1505 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1509 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1510 WARN("loglevel out of range.");
1511 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1515 event
.loglevel
= loglevel
;
1516 } else if (!strcmp((const char *) node
->name
,
1517 config_element_filter
)) {
1519 xmlNodeGetContent(node
);
1523 ret
= -LTTNG_ERR_NOMEM
;
1527 filter_expression
= strdup((char *) content
);
1529 if (!filter_expression
) {
1530 ret
= -LTTNG_ERR_NOMEM
;
1533 } else if (!strcmp((const char *) node
->name
,
1534 config_element_exclusions
)) {
1535 xmlNodePtr exclusion_node
;
1536 int exclusion_index
= 0;
1541 * Exclusions has already been initialized,
1544 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1548 exclusion_count
= xmlChildElementCount(node
);
1549 if (!exclusion_count
) {
1553 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1555 exclusion_count
= 0;
1556 ret
= -LTTNG_ERR_NOMEM
;
1560 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1561 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1563 xmlNodeGetContent(exclusion_node
);
1566 ret
= -LTTNG_ERR_NOMEM
;
1570 exclusions
[exclusion_index
] = strdup((const char *) content
);
1572 if (!exclusions
[exclusion_index
]) {
1573 ret
= -LTTNG_ERR_NOMEM
;
1579 event
.exclusion
= 1;
1580 } else if (!strcmp((const char *) node
->name
,
1581 config_element_attributes
)) {
1582 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1585 if (!attribute_node
) {
1586 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1590 if (!strcmp((const char *) node
->name
,
1591 config_element_probe_attributes
)) {
1592 xmlNodePtr probe_attribute_node
;
1594 /* probe_attributes */
1595 for (probe_attribute_node
=
1596 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1597 probe_attribute_node
= xmlNextElementSibling(
1598 probe_attribute_node
)) {
1600 ret
= process_probe_attribute_node(probe_attribute_node
,
1609 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1611 /* function_attributes */
1612 content
= xmlNodeGetContent(symbol_node
);
1614 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1618 sym_len
= strlen((char *) content
);
1619 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1620 WARN("Function name too long.");
1621 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1626 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1633 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1634 filter_expression
, exclusion_count
, exclusions
);
1636 for (i
= 0; i
< exclusion_count
; i
++) {
1637 free(exclusions
[i
]);
1641 free(filter_expression
);
1646 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1647 const char *channel_name
)
1652 assert(events_node
);
1654 assert(channel_name
);
1656 for (node
= xmlFirstElementChild(events_node
); node
;
1657 node
= xmlNextElementSibling(node
)) {
1658 ret
= process_event_node(node
, handle
, channel_name
);
1668 int process_channel_attr_node(xmlNodePtr attr_node
,
1669 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1670 xmlNodePtr
*events_node
)
1676 assert(contexts_node
);
1677 assert(events_node
);
1679 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1684 content
= xmlNodeGetContent(attr_node
);
1686 ret
= -LTTNG_ERR_NOMEM
;
1690 name_len
= strlen((char *) content
);
1691 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1692 WARN("Channel name too long.");
1693 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1698 strncpy(channel
->name
, (const char *) content
, name_len
);
1700 } else if (!strcmp((const char *) attr_node
->name
,
1701 config_element_enabled
)) {
1706 content
= xmlNodeGetContent(attr_node
);
1708 ret
= -LTTNG_ERR_NOMEM
;
1712 ret
= parse_bool(content
, &enabled
);
1715 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1719 channel
->enabled
= enabled
;
1720 } else if (!strcmp((const char *) attr_node
->name
,
1721 config_element_overwrite_mode
)) {
1724 /* overwrite_mode */
1725 content
= xmlNodeGetContent(attr_node
);
1727 ret
= -LTTNG_ERR_NOMEM
;
1731 ret
= get_overwrite_mode(content
);
1734 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1738 channel
->attr
.overwrite
= ret
;
1739 } else if (!strcmp((const char *) attr_node
->name
,
1740 config_element_subbuf_size
)) {
1743 /* subbuffer_size */
1744 content
= xmlNodeGetContent(attr_node
);
1746 ret
= -LTTNG_ERR_NOMEM
;
1750 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1753 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1756 } else if (!strcmp((const char *) attr_node
->name
,
1757 config_element_num_subbuf
)) {
1760 /* subbuffer_count */
1761 content
= xmlNodeGetContent(attr_node
);
1763 ret
= -LTTNG_ERR_NOMEM
;
1767 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1770 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1773 } else if (!strcmp((const char *) attr_node
->name
,
1774 config_element_switch_timer_interval
)) {
1776 uint64_t switch_timer_interval
= 0;
1778 /* switch_timer_interval */
1779 content
= xmlNodeGetContent(attr_node
);
1781 ret
= -LTTNG_ERR_NOMEM
;
1785 ret
= parse_uint(content
, &switch_timer_interval
);
1788 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1792 if (switch_timer_interval
> UINT_MAX
) {
1793 WARN("switch_timer_interval out of range.");
1794 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1798 channel
->attr
.switch_timer_interval
=
1799 switch_timer_interval
;
1800 } else if (!strcmp((const char *) attr_node
->name
,
1801 config_element_read_timer_interval
)) {
1803 uint64_t read_timer_interval
= 0;
1805 /* read_timer_interval */
1806 content
= xmlNodeGetContent(attr_node
);
1808 ret
= -LTTNG_ERR_NOMEM
;
1812 ret
= parse_uint(content
, &read_timer_interval
);
1815 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1819 if (read_timer_interval
> UINT_MAX
) {
1820 WARN("read_timer_interval out of range.");
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 channel
->attr
.read_timer_interval
=
1826 read_timer_interval
;
1827 } else if (!strcmp((const char *) attr_node
->name
,
1828 config_element_output_type
)) {
1832 content
= xmlNodeGetContent(attr_node
);
1834 ret
= -LTTNG_ERR_NOMEM
;
1838 ret
= get_output_type(content
);
1841 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1845 channel
->attr
.output
= ret
;
1846 } else if (!strcmp((const char *) attr_node
->name
,
1847 config_element_tracefile_size
)) {
1850 /* tracefile_size */
1851 content
= xmlNodeGetContent(attr_node
);
1853 ret
= -LTTNG_ERR_NOMEM
;
1857 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1860 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1863 } else if (!strcmp((const char *) attr_node
->name
,
1864 config_element_tracefile_count
)) {
1867 /* tracefile_count */
1868 content
= xmlNodeGetContent(attr_node
);
1870 ret
= -LTTNG_ERR_NOMEM
;
1874 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1877 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1880 } else if (!strcmp((const char *) attr_node
->name
,
1881 config_element_live_timer_interval
)) {
1883 uint64_t live_timer_interval
= 0;
1885 /* live_timer_interval */
1886 content
= xmlNodeGetContent(attr_node
);
1888 ret
= -LTTNG_ERR_NOMEM
;
1892 ret
= parse_uint(content
, &live_timer_interval
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1899 if (live_timer_interval
> UINT_MAX
) {
1900 WARN("live_timer_interval out of range.");
1901 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1905 channel
->attr
.live_timer_interval
=
1906 live_timer_interval
;
1907 } else if (!strcmp((const char *) attr_node
->name
,
1908 config_element_events
)) {
1910 *events_node
= attr_node
;
1913 *contexts_node
= attr_node
;
1921 int process_context_node(xmlNodePtr context_node
,
1922 struct lttng_handle
*handle
, const char *channel_name
)
1925 struct lttng_event_context context
;
1926 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1929 assert(channel_name
);
1931 if (!context_child_node
) {
1932 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 memset(&context
, 0, sizeof(context
));
1938 if (!strcmp((const char *) context_child_node
->name
,
1939 config_element_type
)) {
1941 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1943 ret
= -LTTNG_ERR_NOMEM
;
1947 ret
= get_context_type(content
);
1950 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1956 xmlNodePtr perf_attr_node
;
1959 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1960 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1961 perf_attr_node
; perf_attr_node
=
1962 xmlNextElementSibling(perf_attr_node
)) {
1963 if (!strcmp((const char *) perf_attr_node
->name
,
1964 config_element_type
)) {
1969 content
= xmlNodeGetContent(perf_attr_node
);
1971 ret
= -LTTNG_ERR_NOMEM
;
1975 ret
= parse_uint(content
, &type
);
1978 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1982 if (type
> UINT32_MAX
) {
1983 WARN("perf context type out of range.");
1984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 context
.u
.perf_counter
.type
= type
;
1989 } else if (!strcmp((const char *) perf_attr_node
->name
,
1990 config_element_config
)) {
1992 uint64_t config
= 0;
1995 content
= xmlNodeGetContent(perf_attr_node
);
1997 ret
= -LTTNG_ERR_NOMEM
;
2001 ret
= parse_uint(content
, &config
);
2004 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2008 context
.u
.perf_counter
.config
= config
;
2009 } else if (!strcmp((const char *) perf_attr_node
->name
,
2010 config_element_name
)) {
2015 content
= xmlNodeGetContent(perf_attr_node
);
2017 ret
= -LTTNG_ERR_NOMEM
;
2021 name_len
= strlen((char *) content
);
2022 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2023 WARN("perf context name too long.");
2024 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2029 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2036 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2042 int process_contexts_node(xmlNodePtr contexts_node
,
2043 struct lttng_handle
*handle
, const char *channel_name
)
2046 xmlNodePtr context_node
;
2048 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2049 context_node
= xmlNextElementSibling(context_node
)) {
2050 ret
= process_context_node(context_node
, handle
, channel_name
);
2060 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2063 struct lttng_domain domain
= { 0 };
2064 struct lttng_handle
*handle
= NULL
;
2065 xmlNodePtr channels_node
= NULL
;
2068 assert(session_name
);
2070 ret
= init_domain(domain_node
, &domain
);
2075 handle
= lttng_create_handle(session_name
, &domain
);
2077 ret
= -LTTNG_ERR_NOMEM
;
2081 /* get the channels node */
2082 for (node
= xmlFirstElementChild(domain_node
); node
;
2083 node
= xmlNextElementSibling(node
)) {
2084 if (!strcmp((const char *) node
->name
,
2085 config_element_channels
)) {
2086 channels_node
= node
;
2091 if (!channels_node
) {
2095 /* create all channels */
2096 for (node
= xmlFirstElementChild(channels_node
); node
;
2097 node
= xmlNextElementSibling(node
)) {
2098 struct lttng_channel channel
;
2099 xmlNodePtr contexts_node
= NULL
;
2100 xmlNodePtr events_node
= NULL
;
2101 xmlNodePtr channel_attr_node
;
2103 memset(&channel
, 0, sizeof(channel
));
2104 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2106 for (channel_attr_node
= xmlFirstElementChild(node
);
2107 channel_attr_node
; channel_attr_node
=
2108 xmlNextElementSibling(channel_attr_node
)) {
2109 ret
= process_channel_attr_node(channel_attr_node
,
2110 &channel
, &contexts_node
, &events_node
);
2116 ret
= lttng_enable_channel(handle
, &channel
);
2121 ret
= process_events_node(events_node
, handle
, channel
.name
);
2126 ret
= process_contexts_node(contexts_node
, handle
,
2133 lttng_destroy_handle(handle
);
2138 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2141 int ret
, started
= -1, snapshot_mode
= -1;
2142 uint64_t live_timer_interval
= UINT64_MAX
;
2144 xmlNodePtr domains_node
= NULL
;
2145 xmlNodePtr output_node
= NULL
;
2147 struct lttng_domain
*kernel_domain
= NULL
;
2148 struct lttng_domain
*ust_domain
= NULL
;
2149 struct lttng_domain
*jul_domain
= NULL
;
2151 for (node
= xmlFirstElementChild(session_node
); node
;
2152 node
= xmlNextElementSibling(node
)) {
2153 if (!name
&& !strcmp((const char *) node
->name
,
2154 config_element_name
)) {
2156 xmlChar
*node_content
= xmlNodeGetContent(node
);
2157 if (!node_content
) {
2158 ret
= -LTTNG_ERR_NOMEM
;
2162 name
= (char *) node_content
;
2163 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2164 config_element_domains
)) {
2166 domains_node
= node
;
2167 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2168 config_element_started
)) {
2170 xmlChar
*node_content
= xmlNodeGetContent(node
);
2171 if (!node_content
) {
2172 ret
= -LTTNG_ERR_NOMEM
;
2176 ret
= parse_bool(node_content
, &started
);
2179 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2182 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2183 config_element_output
)) {
2187 /* attributes, snapshot_mode or live_timer_interval */
2188 xmlNodePtr attributes_child
=
2189 xmlFirstElementChild(node
);
2191 if (!strcmp((const char *) attributes_child
->name
,
2192 config_element_snapshot_mode
)) {
2194 xmlChar
*snapshot_mode_content
=
2195 xmlNodeGetContent(attributes_child
);
2196 if (!snapshot_mode_content
) {
2197 ret
= -LTTNG_ERR_NOMEM
;
2201 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2202 free(snapshot_mode_content
);
2204 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2208 /* live_timer_interval */
2209 xmlChar
*timer_interval_content
=
2210 xmlNodeGetContent(attributes_child
);
2211 if (!timer_interval_content
) {
2212 ret
= -LTTNG_ERR_NOMEM
;
2216 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2217 free(timer_interval_content
);
2219 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2227 /* Mandatory attribute, as defined in the session XSD */
2228 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2232 if (session_name
&& strcmp(name
, session_name
)) {
2233 /* This is not the session we are looking for */
2234 ret
= -LTTNG_ERR_NO_SESSION
;
2238 /* Init domains to create the session handles */
2239 for (node
= xmlFirstElementChild(domains_node
); node
;
2240 node
= xmlNextElementSibling(node
)) {
2241 struct lttng_domain
*domain
;
2243 domain
= zmalloc(sizeof(*domain
));
2245 ret
= -LTTNG_ERR_NOMEM
;
2249 ret
= init_domain(node
, domain
);
2251 goto domain_init_error
;
2254 switch (domain
->type
) {
2255 case LTTNG_DOMAIN_KERNEL
:
2256 if (kernel_domain
) {
2257 /* Same domain seen twice, invalid! */
2258 goto domain_init_error
;
2260 kernel_domain
= domain
;
2262 case LTTNG_DOMAIN_UST
:
2264 /* Same domain seen twice, invalid! */
2265 goto domain_init_error
;
2267 ust_domain
= domain
;
2269 case LTTNG_DOMAIN_JUL
:
2271 /* Same domain seen twice, invalid! */
2272 goto domain_init_error
;
2274 jul_domain
= domain
;
2277 WARN("Invalid domain type");
2278 goto domain_init_error
;
2283 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2288 /* Destroy session if it exists */
2289 ret
= lttng_destroy_session(name
);
2290 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2291 ERR("Failed to destroy existing session.");
2296 /* Create session type depending on output type */
2297 if (snapshot_mode
&& snapshot_mode
!= -1) {
2298 ret
= create_snapshot_session(name
, output_node
);
2299 } else if (live_timer_interval
&&
2300 live_timer_interval
!= UINT64_MAX
) {
2301 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2302 output_node
, live_timer_interval
);
2304 /* regular session */
2305 ret
= create_session(name
, kernel_domain
, ust_domain
, jul_domain
,
2306 output_node
, UINT64_MAX
);
2312 for (node
= xmlFirstElementChild(domains_node
); node
;
2313 node
= xmlNextElementSibling(node
)) {
2314 ret
= process_domain_node(node
, name
);
2321 ret
= lttng_start_tracing(name
);
2329 ERR("Failed to load session %s: %s", name
, lttng_strerror(ret
));
2330 lttng_destroy_session(name
);
2334 free(kernel_domain
);
2342 int load_session_from_file(const char *path
, const char *session_name
,
2343 struct session_config_validation_ctx
*validation_ctx
, int override
)
2345 int ret
, session_found
= !session_name
;
2346 xmlDocPtr doc
= NULL
;
2347 xmlNodePtr sessions_node
;
2348 xmlNodePtr session_node
;
2352 assert(validation_ctx
);
2354 ret
= stat(path
, &sb
);
2356 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2360 doc
= xmlParseFile(path
);
2362 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2366 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2368 ERR("Session configuration file validation failed");
2369 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2373 sessions_node
= xmlDocGetRootElement(doc
);
2374 if (!sessions_node
) {
2378 for (session_node
= xmlFirstElementChild(sessions_node
);
2379 session_node
; session_node
=
2380 xmlNextElementSibling(session_node
)) {
2381 ret
= process_session_node(session_node
,
2382 session_name
, override
);
2383 if (session_name
&& ret
== 0) {
2384 /* Target session found and loaded */
2392 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2398 int load_session_from_path(const char *path
, const char *session_name
,
2399 struct session_config_validation_ctx
*validation_ctx
, int override
)
2401 int ret
, session_found
= !session_name
;
2402 DIR *directory
= NULL
;
2405 assert(validation_ctx
);
2407 directory
= opendir(path
);
2411 /* Try the file loading. */
2414 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2417 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2422 struct dirent
*entry
;
2423 struct dirent
*result
;
2424 char *file_path
= NULL
;
2425 size_t path_len
= strlen(path
);
2427 if (path_len
>= PATH_MAX
) {
2428 ret
= -LTTNG_ERR_INVALID
;
2432 entry
= zmalloc(sizeof(*entry
));
2434 ret
= -LTTNG_ERR_NOMEM
;
2438 file_path
= zmalloc(PATH_MAX
);
2440 ret
= -LTTNG_ERR_NOMEM
;
2445 strncpy(file_path
, path
, path_len
);
2446 if (file_path
[path_len
- 1] != '/') {
2447 file_path
[path_len
++] = '/';
2451 /* Search for *.lttng files */
2452 while (!readdir_r(directory
, entry
, &result
) && result
) {
2453 size_t file_name_len
= strlen(result
->d_name
);
2455 if (file_name_len
<=
2456 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2460 if (path_len
+ file_name_len
>= PATH_MAX
) {
2464 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2465 result
->d_name
+ file_name_len
- sizeof(
2466 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2470 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2471 file_path
[path_len
+ file_name_len
] = '\0';
2473 ret
= load_session_from_file(file_path
, session_name
,
2474 validation_ctx
, override
);
2475 if (session_name
&& !ret
) {
2484 ret
= load_session_from_file(path
, session_name
,
2485 validation_ctx
, override
);
2495 if (closedir(directory
)) {
2500 if (!session_found
) {
2501 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2508 * Validate that the given path's credentials and the current process have the
2509 * same UID. If so, return 0 else return 1 if it does NOT match.
2511 static int validate_path_creds(const char *path
)
2513 int ret
, uid
= getuid();
2522 ret
= stat(path
, &buf
);
2524 if (errno
!= ENOENT
) {
2527 ret
= -LTTNG_ERR_INVALID
;
2531 if (buf
.st_uid
!= uid
) {
2542 int config_load_session(const char *path
, const char *session_name
,
2543 int override
, unsigned int autoload
)
2546 struct session_config_validation_ctx validation_ctx
= { 0 };
2548 ret
= init_session_config_validation_ctx(&validation_ctx
);
2555 const char *sys_path
;
2558 * Try system session configuration path. Ignore error here so we can
2559 * continue loading the home sessions. The above call should print an
2560 * error to inform the user.
2563 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2564 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2566 sys_path
= DEFAULT_SESSION_HOME_CONFIGPATH
;
2569 ret
= validate_path_creds(sys_path
);
2570 if (!ret
&& autoload
) {
2571 (void) load_session_from_path(sys_path
, session_name
,
2572 &validation_ctx
, override
);
2576 home_path
= utils_get_home_dir();
2578 char path
[PATH_MAX
];
2581 ret
= snprintf(path
, sizeof(path
),
2582 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2583 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2585 ret
= snprintf(path
, sizeof(path
),
2586 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2592 ret
= validate_path_creds(path
);
2593 if (ret
&& autoload
) {
2598 ret
= load_session_from_path(path
, session_name
,
2599 &validation_ctx
, override
);
2600 if (!ret
|| (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
2601 /* Session found or an error occured */
2606 ret
= access(path
, F_OK
);
2611 ret
= -LTTNG_ERR_INVALID
;
2612 WARN("Session configuration path does not exist.");
2615 ret
= -LTTNG_ERR_EPERM
;
2618 ret
= -LTTNG_ERR_UNK
;
2624 ret
= load_session_from_path(path
, session_name
,
2625 &validation_ctx
, override
);
2628 fini_session_config_validation_ctx(&validation_ctx
);
2629 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2631 * Don't report an error if no sessions are found when called
2632 * without a session_name or a search path.