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
);
1639 if (!event
.enabled
) {
1641 * Note that we should use lttng_disable_event_ext() (2.6+) to
1642 * eliminate the risk of clashing on events of the same
1643 * name (with different event types and loglevels).
1645 * Unfortunately, lttng_disable_event_ext() only performs a
1646 * match on the name and event type and errors out if any other
1647 * event attribute is not set to its default value.
1649 * This will disable all events that match this name.
1651 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1654 for (i
= 0; i
< exclusion_count
; i
++) {
1655 free(exclusions
[i
]);
1659 free(filter_expression
);
1664 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1665 const char *channel_name
)
1670 assert(events_node
);
1672 assert(channel_name
);
1674 for (node
= xmlFirstElementChild(events_node
); node
;
1675 node
= xmlNextElementSibling(node
)) {
1676 ret
= process_event_node(node
, handle
, channel_name
);
1686 int process_channel_attr_node(xmlNodePtr attr_node
,
1687 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1688 xmlNodePtr
*events_node
)
1694 assert(contexts_node
);
1695 assert(events_node
);
1697 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1702 content
= xmlNodeGetContent(attr_node
);
1704 ret
= -LTTNG_ERR_NOMEM
;
1708 name_len
= strlen((char *) content
);
1709 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1710 WARN("Channel name too long.");
1711 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1716 strncpy(channel
->name
, (const char *) content
, name_len
);
1718 } else if (!strcmp((const char *) attr_node
->name
,
1719 config_element_enabled
)) {
1724 content
= xmlNodeGetContent(attr_node
);
1726 ret
= -LTTNG_ERR_NOMEM
;
1730 ret
= parse_bool(content
, &enabled
);
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1737 channel
->enabled
= enabled
;
1738 } else if (!strcmp((const char *) attr_node
->name
,
1739 config_element_overwrite_mode
)) {
1742 /* overwrite_mode */
1743 content
= xmlNodeGetContent(attr_node
);
1745 ret
= -LTTNG_ERR_NOMEM
;
1749 ret
= get_overwrite_mode(content
);
1752 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1756 channel
->attr
.overwrite
= ret
;
1757 } else if (!strcmp((const char *) attr_node
->name
,
1758 config_element_subbuf_size
)) {
1761 /* subbuffer_size */
1762 content
= xmlNodeGetContent(attr_node
);
1764 ret
= -LTTNG_ERR_NOMEM
;
1768 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1771 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1774 } else if (!strcmp((const char *) attr_node
->name
,
1775 config_element_num_subbuf
)) {
1778 /* subbuffer_count */
1779 content
= xmlNodeGetContent(attr_node
);
1781 ret
= -LTTNG_ERR_NOMEM
;
1785 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1788 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1791 } else if (!strcmp((const char *) attr_node
->name
,
1792 config_element_switch_timer_interval
)) {
1794 uint64_t switch_timer_interval
= 0;
1796 /* switch_timer_interval */
1797 content
= xmlNodeGetContent(attr_node
);
1799 ret
= -LTTNG_ERR_NOMEM
;
1803 ret
= parse_uint(content
, &switch_timer_interval
);
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1810 if (switch_timer_interval
> UINT_MAX
) {
1811 WARN("switch_timer_interval out of range.");
1812 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1816 channel
->attr
.switch_timer_interval
=
1817 switch_timer_interval
;
1818 } else if (!strcmp((const char *) attr_node
->name
,
1819 config_element_read_timer_interval
)) {
1821 uint64_t read_timer_interval
= 0;
1823 /* read_timer_interval */
1824 content
= xmlNodeGetContent(attr_node
);
1826 ret
= -LTTNG_ERR_NOMEM
;
1830 ret
= parse_uint(content
, &read_timer_interval
);
1833 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1837 if (read_timer_interval
> UINT_MAX
) {
1838 WARN("read_timer_interval out of range.");
1839 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1843 channel
->attr
.read_timer_interval
=
1844 read_timer_interval
;
1845 } else if (!strcmp((const char *) attr_node
->name
,
1846 config_element_output_type
)) {
1850 content
= xmlNodeGetContent(attr_node
);
1852 ret
= -LTTNG_ERR_NOMEM
;
1856 ret
= get_output_type(content
);
1859 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1863 channel
->attr
.output
= ret
;
1864 } else if (!strcmp((const char *) attr_node
->name
,
1865 config_element_tracefile_size
)) {
1868 /* tracefile_size */
1869 content
= xmlNodeGetContent(attr_node
);
1871 ret
= -LTTNG_ERR_NOMEM
;
1875 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1878 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1881 } else if (!strcmp((const char *) attr_node
->name
,
1882 config_element_tracefile_count
)) {
1885 /* tracefile_count */
1886 content
= xmlNodeGetContent(attr_node
);
1888 ret
= -LTTNG_ERR_NOMEM
;
1892 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1898 } else if (!strcmp((const char *) attr_node
->name
,
1899 config_element_live_timer_interval
)) {
1901 uint64_t live_timer_interval
= 0;
1903 /* live_timer_interval */
1904 content
= xmlNodeGetContent(attr_node
);
1906 ret
= -LTTNG_ERR_NOMEM
;
1910 ret
= parse_uint(content
, &live_timer_interval
);
1913 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1917 if (live_timer_interval
> UINT_MAX
) {
1918 WARN("live_timer_interval out of range.");
1919 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1923 channel
->attr
.live_timer_interval
=
1924 live_timer_interval
;
1925 } else if (!strcmp((const char *) attr_node
->name
,
1926 config_element_events
)) {
1928 *events_node
= attr_node
;
1931 *contexts_node
= attr_node
;
1939 int process_context_node(xmlNodePtr context_node
,
1940 struct lttng_handle
*handle
, const char *channel_name
)
1943 struct lttng_event_context context
;
1944 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1947 assert(channel_name
);
1949 if (!context_child_node
) {
1950 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1954 memset(&context
, 0, sizeof(context
));
1956 if (!strcmp((const char *) context_child_node
->name
,
1957 config_element_type
)) {
1959 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 ret
= get_context_type(content
);
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1974 xmlNodePtr perf_attr_node
;
1977 context
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1978 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1979 perf_attr_node
; perf_attr_node
=
1980 xmlNextElementSibling(perf_attr_node
)) {
1981 if (!strcmp((const char *) perf_attr_node
->name
,
1982 config_element_type
)) {
1987 content
= xmlNodeGetContent(perf_attr_node
);
1989 ret
= -LTTNG_ERR_NOMEM
;
1993 ret
= parse_uint(content
, &type
);
1996 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2000 if (type
> UINT32_MAX
) {
2001 WARN("perf context type out of range.");
2002 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2006 context
.u
.perf_counter
.type
= type
;
2007 } else if (!strcmp((const char *) perf_attr_node
->name
,
2008 config_element_config
)) {
2010 uint64_t config
= 0;
2013 content
= xmlNodeGetContent(perf_attr_node
);
2015 ret
= -LTTNG_ERR_NOMEM
;
2019 ret
= parse_uint(content
, &config
);
2022 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2026 context
.u
.perf_counter
.config
= config
;
2027 } else if (!strcmp((const char *) perf_attr_node
->name
,
2028 config_element_name
)) {
2033 content
= xmlNodeGetContent(perf_attr_node
);
2035 ret
= -LTTNG_ERR_NOMEM
;
2039 name_len
= strlen((char *) content
);
2040 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2041 WARN("perf context name too long.");
2042 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2047 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2054 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2060 int process_contexts_node(xmlNodePtr contexts_node
,
2061 struct lttng_handle
*handle
, const char *channel_name
)
2064 xmlNodePtr context_node
;
2066 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2067 context_node
= xmlNextElementSibling(context_node
)) {
2068 ret
= process_context_node(context_node
, handle
, channel_name
);
2078 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2081 struct lttng_domain domain
= { 0 };
2082 struct lttng_handle
*handle
= NULL
;
2083 xmlNodePtr channels_node
= NULL
;
2086 assert(session_name
);
2088 ret
= init_domain(domain_node
, &domain
);
2093 handle
= lttng_create_handle(session_name
, &domain
);
2095 ret
= -LTTNG_ERR_NOMEM
;
2099 /* get the channels node */
2100 for (node
= xmlFirstElementChild(domain_node
); node
;
2101 node
= xmlNextElementSibling(node
)) {
2102 if (!strcmp((const char *) node
->name
,
2103 config_element_channels
)) {
2104 channels_node
= node
;
2109 if (!channels_node
) {
2113 /* create all channels */
2114 for (node
= xmlFirstElementChild(channels_node
); node
;
2115 node
= xmlNextElementSibling(node
)) {
2116 struct lttng_channel channel
;
2117 xmlNodePtr contexts_node
= NULL
;
2118 xmlNodePtr events_node
= NULL
;
2119 xmlNodePtr channel_attr_node
;
2121 memset(&channel
, 0, sizeof(channel
));
2122 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2124 for (channel_attr_node
= xmlFirstElementChild(node
);
2125 channel_attr_node
; channel_attr_node
=
2126 xmlNextElementSibling(channel_attr_node
)) {
2127 ret
= process_channel_attr_node(channel_attr_node
,
2128 &channel
, &contexts_node
, &events_node
);
2134 ret
= lttng_enable_channel(handle
, &channel
);
2139 ret
= process_events_node(events_node
, handle
, channel
.name
);
2144 ret
= process_contexts_node(contexts_node
, handle
,
2151 lttng_destroy_handle(handle
);
2156 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2159 int ret
, started
= -1, snapshot_mode
= -1;
2160 uint64_t live_timer_interval
= UINT64_MAX
;
2161 xmlChar
*name
= NULL
;
2162 xmlNodePtr domains_node
= NULL
;
2163 xmlNodePtr output_node
= NULL
;
2165 struct lttng_domain
*kernel_domain
= NULL
;
2166 struct lttng_domain
*ust_domain
= NULL
;
2167 struct lttng_domain
*jul_domain
= NULL
;
2169 for (node
= xmlFirstElementChild(session_node
); node
;
2170 node
= xmlNextElementSibling(node
)) {
2171 if (!name
&& !strcmp((const char *) node
->name
,
2172 config_element_name
)) {
2174 xmlChar
*node_content
= xmlNodeGetContent(node
);
2175 if (!node_content
) {
2176 ret
= -LTTNG_ERR_NOMEM
;
2180 name
= node_content
;
2181 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2182 config_element_domains
)) {
2184 domains_node
= node
;
2185 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2186 config_element_started
)) {
2188 xmlChar
*node_content
= xmlNodeGetContent(node
);
2189 if (!node_content
) {
2190 ret
= -LTTNG_ERR_NOMEM
;
2194 ret
= parse_bool(node_content
, &started
);
2197 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2200 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2201 config_element_output
)) {
2205 /* attributes, snapshot_mode or live_timer_interval */
2206 xmlNodePtr attributes_child
=
2207 xmlFirstElementChild(node
);
2209 if (!strcmp((const char *) attributes_child
->name
,
2210 config_element_snapshot_mode
)) {
2212 xmlChar
*snapshot_mode_content
=
2213 xmlNodeGetContent(attributes_child
);
2214 if (!snapshot_mode_content
) {
2215 ret
= -LTTNG_ERR_NOMEM
;
2219 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2220 free(snapshot_mode_content
);
2222 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2226 /* live_timer_interval */
2227 xmlChar
*timer_interval_content
=
2228 xmlNodeGetContent(attributes_child
);
2229 if (!timer_interval_content
) {
2230 ret
= -LTTNG_ERR_NOMEM
;
2234 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2235 free(timer_interval_content
);
2237 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2245 /* Mandatory attribute, as defined in the session XSD */
2246 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2250 if (session_name
&& strcmp((char *) name
, session_name
)) {
2251 /* This is not the session we are looking for */
2252 ret
= -LTTNG_ERR_NO_SESSION
;
2256 /* Init domains to create the session handles */
2257 for (node
= xmlFirstElementChild(domains_node
); node
;
2258 node
= xmlNextElementSibling(node
)) {
2259 struct lttng_domain
*domain
;
2261 domain
= zmalloc(sizeof(*domain
));
2263 ret
= -LTTNG_ERR_NOMEM
;
2267 ret
= init_domain(node
, domain
);
2269 goto domain_init_error
;
2272 switch (domain
->type
) {
2273 case LTTNG_DOMAIN_KERNEL
:
2274 if (kernel_domain
) {
2275 /* Same domain seen twice, invalid! */
2276 goto domain_init_error
;
2278 kernel_domain
= domain
;
2280 case LTTNG_DOMAIN_UST
:
2282 /* Same domain seen twice, invalid! */
2283 goto domain_init_error
;
2285 ust_domain
= domain
;
2287 case LTTNG_DOMAIN_JUL
:
2289 /* Same domain seen twice, invalid! */
2290 goto domain_init_error
;
2292 jul_domain
= domain
;
2295 WARN("Invalid domain type");
2296 goto domain_init_error
;
2301 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2306 /* Destroy session if it exists */
2307 ret
= lttng_destroy_session((const char *) name
);
2308 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2309 ERR("Failed to destroy existing session.");
2314 /* Create session type depending on output type */
2315 if (snapshot_mode
&& snapshot_mode
!= -1) {
2316 ret
= create_snapshot_session((const char *) name
, output_node
);
2317 } else if (live_timer_interval
&&
2318 live_timer_interval
!= UINT64_MAX
) {
2319 ret
= create_session((const char *) name
, kernel_domain
,
2320 ust_domain
, jul_domain
, output_node
,
2321 live_timer_interval
);
2323 /* regular session */
2324 ret
= create_session((const char *) name
, kernel_domain
,
2325 ust_domain
, jul_domain
, output_node
,
2332 for (node
= xmlFirstElementChild(domains_node
); node
;
2333 node
= xmlNextElementSibling(node
)) {
2334 ret
= process_domain_node(node
, (const char *) name
);
2341 ret
= lttng_start_tracing((const char *) name
);
2349 ERR("Failed to load session %s: %s", (const char *) name
,
2350 lttng_strerror(ret
));
2351 lttng_destroy_session((const char *) name
);
2355 free(kernel_domain
);
2363 * Return 1 if the given path is readable by the current UID or 0 if not.
2364 * Return -1 if the path is EPERM.
2366 static int validate_file_read_creds(const char *path
)
2372 /* Can we read the file. */
2373 ret
= access(path
, R_OK
);
2377 if (errno
== EACCES
) {
2388 int load_session_from_file(const char *path
, const char *session_name
,
2389 struct session_config_validation_ctx
*validation_ctx
, int override
)
2391 int ret
, session_found
= !session_name
;
2392 xmlDocPtr doc
= NULL
;
2393 xmlNodePtr sessions_node
;
2394 xmlNodePtr session_node
;
2397 assert(validation_ctx
);
2399 ret
= validate_file_read_creds(path
);
2402 ret
= -LTTNG_ERR_EPERM
;
2404 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2409 doc
= xmlParseFile(path
);
2411 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2415 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2417 ERR("Session configuration file validation failed");
2418 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2422 sessions_node
= xmlDocGetRootElement(doc
);
2423 if (!sessions_node
) {
2427 for (session_node
= xmlFirstElementChild(sessions_node
);
2428 session_node
; session_node
=
2429 xmlNextElementSibling(session_node
)) {
2430 ret
= process_session_node(session_node
,
2431 session_name
, override
);
2432 if (session_name
&& ret
== 0) {
2433 /* Target session found and loaded */
2441 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2447 int load_session_from_path(const char *path
, const char *session_name
,
2448 struct session_config_validation_ctx
*validation_ctx
, int override
)
2450 int ret
, session_found
= !session_name
;
2451 DIR *directory
= NULL
;
2454 assert(validation_ctx
);
2456 directory
= opendir(path
);
2460 /* Try the file loading. */
2463 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2466 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2471 struct dirent
*entry
;
2472 struct dirent
*result
;
2473 char *file_path
= NULL
;
2474 size_t path_len
= strlen(path
);
2476 if (path_len
>= PATH_MAX
) {
2477 ret
= -LTTNG_ERR_INVALID
;
2481 entry
= zmalloc(sizeof(*entry
));
2483 ret
= -LTTNG_ERR_NOMEM
;
2487 file_path
= zmalloc(PATH_MAX
);
2489 ret
= -LTTNG_ERR_NOMEM
;
2494 strncpy(file_path
, path
, path_len
);
2495 if (file_path
[path_len
- 1] != '/') {
2496 file_path
[path_len
++] = '/';
2500 /* Search for *.lttng files */
2501 while (!readdir_r(directory
, entry
, &result
) && result
) {
2502 size_t file_name_len
= strlen(result
->d_name
);
2504 if (file_name_len
<=
2505 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2509 if (path_len
+ file_name_len
>= PATH_MAX
) {
2513 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2514 result
->d_name
+ file_name_len
- sizeof(
2515 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2519 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2520 file_path
[path_len
+ file_name_len
] = '\0';
2522 ret
= load_session_from_file(file_path
, session_name
,
2523 validation_ctx
, override
);
2524 if (session_name
&& !ret
) {
2533 ret
= load_session_from_file(path
, session_name
,
2534 validation_ctx
, override
);
2544 if (closedir(directory
)) {
2549 if (!session_found
) {
2550 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2557 * Validate that the given path's credentials and the current process have the
2558 * same UID. If so, return 1 else return 0 if it does NOT match.
2560 static int validate_path_creds(const char *path
)
2562 int ret
, uid
= getuid();
2571 ret
= stat(path
, &buf
);
2573 if (errno
!= ENOENT
) {
2576 ret
= -LTTNG_ERR_INVALID
;
2580 if (buf
.st_uid
!= uid
) {
2591 int config_load_session(const char *path
, const char *session_name
,
2592 int override
, unsigned int autoload
)
2595 const char *path_ptr
= NULL
;
2596 struct session_config_validation_ctx validation_ctx
= { 0 };
2598 ret
= init_session_config_validation_ctx(&validation_ctx
);
2605 const char *sys_path
;
2608 home_path
= utils_get_home_dir();
2610 char path
[PATH_MAX
];
2613 * Try user session configuration path. Ignore error here so we can
2614 * continue loading the system wide sessions.
2617 ret
= snprintf(path
, sizeof(path
),
2618 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2619 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2621 PERROR("snprintf session autoload home config path");
2626 * Credentials are only validated for the autoload in order to
2627 * avoid any user session daemon to try to load kernel sessions
2628 * automatically and failing all the times.
2630 ret
= validate_path_creds(path
);
2635 ret
= snprintf(path
, sizeof(path
),
2636 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2638 PERROR("snprintf session home config path");
2644 ret
= load_session_from_path(path_ptr
, session_name
,
2645 &validation_ctx
, override
);
2646 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2650 * Continue even if the session was found since we have to try
2651 * the system wide sessions.
2656 /* Reset path pointer for the system wide dir. */
2659 /* Try system wide configuration directory. */
2661 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2662 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2663 ret
= validate_path_creds(sys_path
);
2665 path_ptr
= sys_path
;
2668 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2669 path_ptr
= sys_path
;
2673 ret
= load_session_from_path(path_ptr
, session_name
,
2674 &validation_ctx
, override
);
2677 ret
= access(path
, F_OK
);
2682 ret
= -LTTNG_ERR_INVALID
;
2683 WARN("Session configuration path does not exist.");
2686 ret
= -LTTNG_ERR_EPERM
;
2689 ret
= -LTTNG_ERR_UNK
;
2695 ret
= load_session_from_path(path
, session_name
,
2696 &validation_ctx
, override
);
2699 fini_session_config_validation_ctx(&validation_ctx
);
2700 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2702 * Don't report an error if no sessions are found when called
2703 * without a session_name or a search path.