2 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
31 #include <common/defaults.h>
32 #include <common/error.h>
33 #include <common/macros.h>
34 #include <common/utils.h>
35 #include <common/compat/getenv.h>
36 #include <lttng/lttng-error.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xmlschemas.h>
40 #include <libxml/tree.h>
41 #include <lttng/lttng.h>
42 #include <lttng/snapshot.h>
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_str_yes
= "yes";
60 const char * const config_str_true
= "true";
61 const char * const config_str_on
= "on";
62 const char * const config_str_no
= "no";
63 const char * const config_str_false
= "false";
64 const char * const config_str_off
= "off";
65 const char * const config_xml_encoding
= "UTF-8";
66 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
67 const char * const config_xml_indent_string
= "\t";
68 const char * const config_xml_true
= "true";
69 const char * const config_xml_false
= "false";
71 const char * const config_element_channel
= "channel";
72 const char * const config_element_channels
= "channels";
73 const char * const config_element_domain
= "domain";
74 const char * const config_element_domains
= "domains";
75 const char * const config_element_event
= "event";
76 const char * const config_element_events
= "events";
77 const char * const config_element_context
= "context";
78 const char * const config_element_contexts
= "contexts";
79 const char * const config_element_attributes
= "attributes";
80 const char * const config_element_exclusion
= "exclusion";
81 const char * const config_element_exclusions
= "exclusions";
82 const char * const config_element_function_attributes
= "function_attributes";
83 const char * const config_element_probe_attributes
= "probe_attributes";
84 const char * const config_element_symbol_name
= "symbol_name";
85 const char * const config_element_address
= "address";
86 const char * const config_element_offset
= "offset";
87 const char * const config_element_name
= "name";
88 const char * const config_element_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 const char * const config_element_type
= "type";
100 const char * const config_element_buffer_type
= "buffer_type";
101 const char * const config_element_session
= "session";
102 const char * const config_element_sessions
= "sessions";
103 const char * const config_element_perf
= "perf";
104 const char * const config_element_config
= "config";
105 const char * const config_element_started
= "started";
106 const char * const config_element_snapshot_mode
= "snapshot_mode";
107 const char * const config_element_loglevel
= "loglevel";
108 const char * const config_element_loglevel_type
= "loglevel_type";
109 const char * const config_element_filter
= "filter";
110 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
111 const char * const config_element_consumer_output
= "consumer_output";
112 const char * const config_element_destination
= "destination";
113 const char * const config_element_path
= "path";
114 const char * const config_element_net_output
= "net_output";
115 const char * const config_element_control_uri
= "control_uri";
116 const char * const config_element_data_uri
= "data_uri";
117 const char * const config_element_max_size
= "max_size";
118 const char * const config_element_pid
= "pid";
119 const char * const config_element_pids
= "pids";
120 const char * const config_element_shared_memory_path
= "shared_memory_path";
122 const char * const config_domain_type_kernel
= "KERNEL";
123 const char * const config_domain_type_ust
= "UST";
124 const char * const config_domain_type_jul
= "JUL";
125 const char * const config_domain_type_log4j
= "LOG4J";
126 const char * const config_domain_type_python
= "PYTHON";
128 const char * const config_buffer_type_per_pid
= "PER_PID";
129 const char * const config_buffer_type_per_uid
= "PER_UID";
130 const char * const config_buffer_type_global
= "GLOBAL";
132 const char * const config_overwrite_mode_discard
= "DISCARD";
133 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
135 const char * const config_output_type_splice
= "SPLICE";
136 const char * const config_output_type_mmap
= "MMAP";
138 const char * const config_loglevel_type_all
= "ALL";
139 const char * const config_loglevel_type_range
= "RANGE";
140 const char * const config_loglevel_type_single
= "SINGLE";
142 const char * const config_event_type_all
= "ALL";
143 const char * const config_event_type_tracepoint
= "TRACEPOINT";
144 const char * const config_event_type_probe
= "PROBE";
145 const char * const config_event_type_function
= "FUNCTION";
146 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
147 const char * const config_event_type_noop
= "NOOP";
148 const char * const config_event_type_syscall
= "SYSCALL";
149 const char * const config_event_type_kprobe
= "KPROBE";
150 const char * const config_event_type_kretprobe
= "KRETPROBE";
152 const char * const config_event_context_pid
= "PID";
153 const char * const config_event_context_procname
= "PROCNAME";
154 const char * const config_event_context_prio
= "PRIO";
155 const char * const config_event_context_nice
= "NICE";
156 const char * const config_event_context_vpid
= "VPID";
157 const char * const config_event_context_tid
= "TID";
158 const char * const config_event_context_vtid
= "VTID";
159 const char * const config_event_context_ppid
= "PPID";
160 const char * const config_event_context_vppid
= "VPPID";
161 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
162 const char * const config_event_context_hostname
= "HOSTNAME";
163 const char * const config_event_context_ip
= "IP";
164 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
166 struct consumer_output
{
173 static int config_entry_handler_filter(struct handler_filter_args
*args
,
174 const char *section
, const char *name
, const char *value
)
177 struct config_entry entry
= { section
, name
, value
};
181 if (!section
|| !name
|| !value
) {
187 if (strcmp(args
->section
, section
)) {
192 ret
= args
->handler(&entry
, args
->user_data
);
198 int config_get_section_entries(const char *override_path
, const char *section
,
199 config_entry_handler_cb handler
, void *user_data
)
203 FILE *config_file
= NULL
;
204 struct handler_filter_args filter
= { section
, handler
, user_data
};
206 /* First, try system-wide conf. file. */
207 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
209 config_file
= fopen(path
, "r");
211 DBG("Loading daemon conf file at %s", path
);
213 * Return value is not very important here since error or not, we
214 * continue and try the next possible conf. file.
216 (void) ini_parse_file(config_file
,
217 (ini_entry_handler
) config_entry_handler_filter
,
222 /* Second is the user local configuration. */
223 path
= utils_get_home_dir();
225 char fullpath
[PATH_MAX
];
227 ret
= snprintf(fullpath
, sizeof(fullpath
),
228 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
230 PERROR("snprintf user conf. path");
234 config_file
= fopen(fullpath
, "r");
236 DBG("Loading daemon user conf file at %s", path
);
238 * Return value is not very important here since error or not, we
239 * continue and try the next possible conf. file.
241 (void) ini_parse_file(config_file
,
242 (ini_entry_handler
) config_entry_handler_filter
,
248 /* Final path is the one that the user might have provided. */
250 config_file
= fopen(override_path
, "r");
252 DBG("Loading daemon command line conf file at %s", override_path
);
253 (void) ini_parse_file(config_file
,
254 (ini_entry_handler
) config_entry_handler_filter
,
258 ERR("Failed to open daemon configuration file at %s",
265 /* Everything went well. */
273 int config_parse_value(const char *value
)
276 char *endptr
, *lower_str
;
286 v
= strtoul(value
, &endptr
, 10);
287 if (endptr
!= value
) {
292 lower_str
= zmalloc(len
+ 1);
299 for (i
= 0; i
< len
; i
++) {
300 lower_str
[i
] = tolower(value
[i
]);
303 if (!strcmp(lower_str
, config_str_yes
) ||
304 !strcmp(lower_str
, config_str_true
) ||
305 !strcmp(lower_str
, config_str_on
)) {
307 } else if (!strcmp(lower_str
, config_str_no
) ||
308 !strcmp(lower_str
, config_str_false
) ||
309 !strcmp(lower_str
, config_str_off
)) {
321 * Returns a xmlChar string which must be released using xmlFree().
323 static xmlChar
*encode_string(const char *in_str
)
325 xmlChar
*out_str
= NULL
;
326 xmlCharEncodingHandlerPtr handler
;
327 int out_len
, ret
, in_len
;
331 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
333 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
337 in_len
= strlen(in_str
);
339 * Add 1 byte for the NULL terminted character. The factor 2 here is
340 * because UTF-8 can be on two bytes so this fits the worst case for each
343 out_len
= (in_len
* 2) + 1;
344 out_str
= xmlMalloc(out_len
);
349 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
356 /* out_len is now the size of out_str */
357 out_str
[out_len
] = '\0';
363 struct config_writer
*config_writer_create(int fd_output
, int indent
)
366 struct config_writer
*writer
;
367 xmlOutputBufferPtr buffer
;
369 writer
= zmalloc(sizeof(struct config_writer
));
371 PERROR("zmalloc config_writer_create");
375 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
380 writer
->writer
= xmlNewTextWriter(buffer
);
381 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
382 config_xml_encoding
, NULL
);
387 ret
= xmlTextWriterSetIndentString(writer
->writer
,
388 BAD_CAST config_xml_indent_string
);
393 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
401 config_writer_destroy(writer
);
406 int config_writer_destroy(struct config_writer
*writer
)
415 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
416 WARN("Could not close XML document");
420 if (writer
->writer
) {
421 xmlFreeTextWriter(writer
->writer
);
430 int config_writer_open_element(struct config_writer
*writer
,
431 const char *element_name
)
434 xmlChar
*encoded_element_name
;
436 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
441 encoded_element_name
= encode_string(element_name
);
442 if (!encoded_element_name
) {
447 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
448 xmlFree(encoded_element_name
);
450 return ret
> 0 ? 0 : ret
;
454 int config_writer_close_element(struct config_writer
*writer
)
458 if (!writer
|| !writer
->writer
) {
463 ret
= xmlTextWriterEndElement(writer
->writer
);
465 return ret
> 0 ? 0 : ret
;
469 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
470 const char *element_name
, uint64_t value
)
473 xmlChar
*encoded_element_name
;
475 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
480 encoded_element_name
= encode_string(element_name
);
481 if (!encoded_element_name
) {
486 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
487 encoded_element_name
, "%" PRIu64
, value
);
488 xmlFree(encoded_element_name
);
490 return ret
> 0 ? 0 : ret
;
494 int config_writer_write_element_signed_int(struct config_writer
*writer
,
495 const char *element_name
, int64_t value
)
498 xmlChar
*encoded_element_name
;
500 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
505 encoded_element_name
= encode_string(element_name
);
506 if (!encoded_element_name
) {
511 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
512 encoded_element_name
, "%" PRIi64
, value
);
513 xmlFree(encoded_element_name
);
515 return ret
> 0 ? 0 : ret
;
519 int config_writer_write_element_bool(struct config_writer
*writer
,
520 const char *element_name
, int value
)
522 return config_writer_write_element_string(writer
, element_name
,
523 value
? config_xml_true
: config_xml_false
);
527 int config_writer_write_element_string(struct config_writer
*writer
,
528 const char *element_name
, const char *value
)
531 xmlChar
*encoded_element_name
= NULL
;
532 xmlChar
*encoded_value
= NULL
;
534 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
540 encoded_element_name
= encode_string(element_name
);
541 if (!encoded_element_name
) {
546 encoded_value
= encode_string(value
);
547 if (!encoded_value
) {
552 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
555 xmlFree(encoded_element_name
);
556 xmlFree(encoded_value
);
557 return ret
> 0 ? 0 : ret
;
561 void xml_error_handler(void *ctx
, const char *format
, ...)
567 va_start(args
, format
);
568 ret
= vasprintf(&errMsg
, format
, args
);
571 ERR("String allocation failed in xml error handler");
575 fprintf(stderr
, "XML Error: %s", errMsg
);
580 void fini_session_config_validation_ctx(
581 struct session_config_validation_ctx
*ctx
)
583 if (ctx
->parser_ctx
) {
584 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
588 xmlSchemaFree(ctx
->schema
);
591 if (ctx
->schema_validation_ctx
) {
592 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
595 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
599 char *get_session_config_xsd_path()
602 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
603 size_t base_path_len
;
607 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
610 base_path_len
= strlen(base_path
);
611 max_path_len
= base_path_len
+
612 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
613 xsd_path
= zmalloc(max_path_len
);
618 strncpy(xsd_path
, base_path
, max_path_len
);
619 if (xsd_path
[base_path_len
- 1] != '/') {
620 xsd_path
[base_path_len
++] = '/';
623 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
624 max_path_len
- base_path_len
);
630 int init_session_config_validation_ctx(
631 struct session_config_validation_ctx
*ctx
)
634 char *xsd_path
= get_session_config_xsd_path();
637 ret
= -LTTNG_ERR_NOMEM
;
641 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
642 if (!ctx
->parser_ctx
) {
643 ERR("XSD parser context creation failed");
644 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
647 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
648 xml_error_handler
, NULL
);
650 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
652 ERR("XSD parsing failed");
653 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
657 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
658 if (!ctx
->schema_validation_ctx
) {
659 ERR("XSD validation context creation failed");
660 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
664 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
665 xml_error_handler
, NULL
);
670 fini_session_config_validation_ctx(ctx
);
678 int parse_uint(xmlChar
*str
, uint64_t *val
)
688 *val
= strtoull((const char *) str
, &endptr
, 10);
689 if (!endptr
|| *endptr
) {
700 int parse_int(xmlChar
*str
, int64_t *val
)
710 *val
= strtoll((const char *) str
, &endptr
, 10);
711 if (!endptr
|| *endptr
) {
722 int parse_bool(xmlChar
*str
, int *val
)
731 if (!strcmp((const char *) str
, config_xml_true
)) {
733 } else if (!strcmp((const char *) str
, config_xml_false
)) {
736 WARN("Invalid boolean value encoutered (%s).",
745 int get_domain_type(xmlChar
*domain
)
753 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
754 ret
= LTTNG_DOMAIN_KERNEL
;
755 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
756 ret
= LTTNG_DOMAIN_UST
;
757 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
758 ret
= LTTNG_DOMAIN_JUL
;
759 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
760 ret
= LTTNG_DOMAIN_LOG4J
;
761 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
762 ret
= LTTNG_DOMAIN_PYTHON
;
773 int get_buffer_type(xmlChar
*buffer_type
)
781 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
782 ret
= LTTNG_BUFFER_GLOBAL
;
783 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
784 ret
= LTTNG_BUFFER_PER_UID
;
785 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
786 ret
= LTTNG_BUFFER_PER_PID
;
797 int get_overwrite_mode(xmlChar
*overwrite_mode
)
801 if (!overwrite_mode
) {
805 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
807 } else if (!strcmp((char *) overwrite_mode
,
808 config_overwrite_mode_discard
)) {
820 int get_output_type(xmlChar
*output_type
)
828 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
829 ret
= LTTNG_EVENT_MMAP
;
830 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
831 ret
= LTTNG_EVENT_SPLICE
;
842 int get_event_type(xmlChar
*event_type
)
850 if (!strcmp((char *) event_type
, config_event_type_all
)) {
851 ret
= LTTNG_EVENT_ALL
;
852 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
853 ret
= LTTNG_EVENT_TRACEPOINT
;
854 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
855 ret
= LTTNG_EVENT_PROBE
;
856 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
857 ret
= LTTNG_EVENT_FUNCTION
;
858 } else if (!strcmp((char *) event_type
,
859 config_event_type_function_entry
)) {
860 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
861 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
862 ret
= LTTNG_EVENT_NOOP
;
863 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
864 ret
= LTTNG_EVENT_SYSCALL
;
875 int get_loglevel_type(xmlChar
*loglevel_type
)
879 if (!loglevel_type
) {
883 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
884 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
885 } else if (!strcmp((char *) loglevel_type
,
886 config_loglevel_type_range
)) {
887 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
888 } else if (!strcmp((char *) loglevel_type
,
889 config_loglevel_type_single
)) {
890 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
901 * Return the context type or -1 on error.
904 int get_context_type(xmlChar
*context_type
)
912 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
913 ret
= LTTNG_EVENT_CONTEXT_PID
;
914 } else if (!strcmp((char *) context_type
,
915 config_event_context_procname
)) {
916 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
917 } else if (!strcmp((char *) context_type
,
918 config_event_context_prio
)) {
919 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
920 } else if (!strcmp((char *) context_type
,
921 config_event_context_nice
)) {
922 ret
= LTTNG_EVENT_CONTEXT_NICE
;
923 } else if (!strcmp((char *) context_type
,
924 config_event_context_vpid
)) {
925 ret
= LTTNG_EVENT_CONTEXT_VPID
;
926 } else if (!strcmp((char *) context_type
,
927 config_event_context_tid
)) {
928 ret
= LTTNG_EVENT_CONTEXT_TID
;
929 } else if (!strcmp((char *) context_type
,
930 config_event_context_vtid
)) {
931 ret
= LTTNG_EVENT_CONTEXT_VTID
;
932 } else if (!strcmp((char *) context_type
,
933 config_event_context_ppid
)) {
934 ret
= LTTNG_EVENT_CONTEXT_PPID
;
935 } else if (!strcmp((char *) context_type
,
936 config_event_context_vppid
)) {
937 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
938 } else if (!strcmp((char *) context_type
,
939 config_event_context_pthread_id
)) {
940 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
941 } else if (!strcmp((char *) context_type
,
942 config_event_context_hostname
)) {
943 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
944 } else if (!strcmp((char *) context_type
,
945 config_event_context_ip
)) {
946 ret
= LTTNG_EVENT_CONTEXT_IP
;
957 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
962 for (node
= xmlFirstElementChild(domain_node
); node
;
963 node
= xmlNextElementSibling(node
)) {
964 if (!strcmp((const char *) node
->name
, config_element_type
)) {
966 xmlChar
*node_content
= xmlNodeGetContent(node
);
968 ret
= -LTTNG_ERR_NOMEM
;
972 ret
= get_domain_type(node_content
);
975 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
980 } else if (!strcmp((const char *) node
->name
,
981 config_element_buffer_type
)) {
983 xmlChar
*node_content
= xmlNodeGetContent(node
);
985 ret
= -LTTNG_ERR_NOMEM
;
989 ret
= get_buffer_type(node_content
);
992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
996 domain
->buf_type
= ret
;
1005 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1010 for (node
= xmlFirstElementChild(net_output_node
); node
;
1011 node
= xmlNextElementSibling(node
)) {
1012 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1014 *control_uri
= (char *) xmlNodeGetContent(node
);
1015 if (!*control_uri
) {
1020 *data_uri
= (char *) xmlNodeGetContent(node
);
1027 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1031 int process_consumer_output(xmlNodePtr consumer_output_node
,
1032 struct consumer_output
*output
)
1039 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1040 node
= xmlNextElementSibling(node
)) {
1041 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1042 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1046 ret
= -LTTNG_ERR_NOMEM
;
1050 ret
= parse_bool(enabled_str
, &output
->enabled
);
1056 xmlNodePtr output_type_node
;
1059 output_type_node
= xmlFirstElementChild(node
);
1060 if (!output_type_node
) {
1061 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1065 if (!strcmp((const char *) output_type_node
->name
,
1066 config_element_path
)) {
1068 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1069 if (!output
->path
) {
1070 ret
= -LTTNG_ERR_NOMEM
;
1075 ret
= get_net_output_uris(output_type_node
,
1076 &output
->control_uri
, &output
->data_uri
);
1088 free(output
->control_uri
);
1089 free(output
->data_uri
);
1090 memset(output
, 0, sizeof(struct consumer_output
));
1096 int create_session_net_output(const char *name
, struct lttng_domain
*domain
,
1097 const char *control_uri
, const char *data_uri
)
1100 struct lttng_handle
*handle
;
1101 const char *uri
= NULL
;
1106 handle
= lttng_create_handle(name
, domain
);
1108 ret
= -LTTNG_ERR_NOMEM
;
1112 if (!control_uri
|| !data_uri
) {
1113 uri
= control_uri
? control_uri
: data_uri
;
1118 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1119 lttng_destroy_handle(handle
);
1125 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1128 xmlNodePtr node
= NULL
;
1129 xmlNodePtr snapshot_output_list_node
;
1130 xmlNodePtr snapshot_output_node
;
1132 assert(session_name
);
1134 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1143 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1145 /* Parse and create snapshot outputs */
1147 for (snapshot_output_node
=
1148 xmlFirstElementChild(snapshot_output_list_node
);
1149 snapshot_output_node
; snapshot_output_node
=
1150 xmlNextElementSibling(snapshot_output_node
)) {
1152 uint64_t max_size
= UINT64_MAX
;
1153 struct consumer_output output
= { 0 };
1154 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1156 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1157 node
= xmlNextElementSibling(node
)) {
1158 if (!strcmp((const char *) node
->name
,
1159 config_element_name
)) {
1161 name
= (char *) xmlNodeGetContent(node
);
1163 ret
= -LTTNG_ERR_NOMEM
;
1164 goto error_snapshot_output
;
1166 } else if (!strcmp((const char *) node
->name
,
1167 config_element_max_size
)) {
1168 xmlChar
*content
= xmlNodeGetContent(node
);
1172 ret
= -LTTNG_ERR_NOMEM
;
1173 goto error_snapshot_output
;
1175 ret
= parse_uint(content
, &max_size
);
1178 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1179 goto error_snapshot_output
;
1182 /* consumer_output */
1183 ret
= process_consumer_output(node
, &output
);
1185 goto error_snapshot_output
;
1190 snapshot_output
= lttng_snapshot_output_create();
1191 if (!snapshot_output
) {
1192 ret
= -LTTNG_ERR_NOMEM
;
1193 goto error_snapshot_output
;
1196 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1198 goto error_snapshot_output
;
1201 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1203 goto error_snapshot_output
;
1207 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1210 goto error_snapshot_output
;
1213 if (output
.control_uri
) {
1214 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1217 goto error_snapshot_output
;
1221 if (output
.data_uri
) {
1222 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1225 goto error_snapshot_output
;
1230 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1231 error_snapshot_output
:
1234 free(output
.control_uri
);
1235 free(output
.data_uri
);
1236 lttng_snapshot_output_destroy(snapshot_output
);
1246 int create_session(const char *name
,
1247 struct lttng_domain
*kernel_domain
,
1248 struct lttng_domain
*ust_domain
,
1249 struct lttng_domain
*jul_domain
,
1250 struct lttng_domain
*log4j_domain
,
1251 xmlNodePtr output_node
,
1252 uint64_t live_timer_interval
)
1255 struct consumer_output output
= { 0 };
1256 xmlNodePtr consumer_output_node
;
1261 consumer_output_node
= xmlFirstElementChild(output_node
);
1262 if (!consumer_output_node
) {
1263 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1267 if (strcmp((const char *) consumer_output_node
->name
,
1268 config_element_consumer_output
)) {
1269 WARN("Invalid output type, expected %s node",
1270 config_element_consumer_output
);
1271 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1275 ret
= process_consumer_output(consumer_output_node
, &output
);
1281 if (live_timer_interval
!= UINT64_MAX
&&
1282 !output
.control_uri
&& !output
.data_uri
) {
1283 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1287 if (output
.control_uri
|| output
.data_uri
) {
1289 struct lttng_domain
*domain
;
1290 struct lttng_domain
*domains
[] =
1291 { kernel_domain
, ust_domain
, jul_domain
, log4j_domain
};
1293 /* network destination */
1294 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1296 * URLs are provided for sure since the test above make sure that
1297 * with a live timer the data and control URIs are provided. So,
1298 * NULL is passed here and will be set right after.
1300 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1302 ret
= lttng_create_session(name
, NULL
);
1308 for (i
= 0; i
< (sizeof(domains
) / sizeof(domains
[0])); i
++) {
1309 domain
= domains
[i
];
1314 ret
= create_session_net_output(name
, domain
, output
.control_uri
,
1321 /* either local output or no output */
1322 ret
= lttng_create_session(name
, output
.path
);
1329 free(output
.control_uri
);
1330 free(output
.data_uri
);
1334 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1335 struct lttng_event_probe_attr
*attr
)
1339 assert(probe_attribute_node
);
1342 if (!strcmp((const char *) probe_attribute_node
->name
,
1343 config_element_address
)) {
1348 content
= xmlNodeGetContent(probe_attribute_node
);
1350 ret
= -LTTNG_ERR_NOMEM
;
1354 ret
= parse_uint(content
, &addr
);
1357 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1362 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1363 config_element_offset
)) {
1365 uint64_t offset
= 0;
1368 content
= xmlNodeGetContent(probe_attribute_node
);
1370 ret
= -LTTNG_ERR_NOMEM
;
1374 ret
= parse_uint(content
, &offset
);
1377 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1381 attr
->offset
= offset
;
1382 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1383 config_element_symbol_name
)) {
1388 content
= xmlNodeGetContent(probe_attribute_node
);
1390 ret
= -LTTNG_ERR_NOMEM
;
1394 name_len
= strlen((char *) content
);
1395 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1396 WARN("symbol_name too long.");
1397 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1402 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1411 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1412 const char *channel_name
)
1416 struct lttng_event event
;
1417 char **exclusions
= NULL
;
1418 unsigned long exclusion_count
= 0;
1419 char *filter_expression
= NULL
;
1423 assert(channel_name
);
1425 memset(&event
, 0, sizeof(event
));
1427 for (node
= xmlFirstElementChild(event_node
); node
;
1428 node
= xmlNextElementSibling(node
)) {
1429 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1434 content
= xmlNodeGetContent(node
);
1436 ret
= -LTTNG_ERR_NOMEM
;
1440 name_len
= strlen((char *) content
);
1441 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1442 WARN("Channel name too long.");
1443 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1448 strncpy(event
.name
, (const char *) content
, name_len
);
1450 } else if (!strcmp((const char *) node
->name
,
1451 config_element_enabled
)) {
1452 xmlChar
*content
= xmlNodeGetContent(node
);
1456 ret
= -LTTNG_ERR_NOMEM
;
1460 ret
= parse_bool(content
, &event
.enabled
);
1463 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1466 } else if (!strcmp((const char *) node
->name
,
1467 config_element_type
)) {
1468 xmlChar
*content
= xmlNodeGetContent(node
);
1472 ret
= -LTTNG_ERR_NOMEM
;
1476 ret
= get_event_type(content
);
1479 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1484 } else if (!strcmp((const char *) node
->name
,
1485 config_element_loglevel_type
)) {
1486 xmlChar
*content
= xmlNodeGetContent(node
);
1490 ret
= -LTTNG_ERR_NOMEM
;
1494 ret
= get_loglevel_type(content
);
1497 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1501 event
.loglevel_type
= ret
;
1502 } else if (!strcmp((const char *) node
->name
,
1503 config_element_loglevel
)) {
1505 int64_t loglevel
= 0;
1508 content
= xmlNodeGetContent(node
);
1510 ret
= -LTTNG_ERR_NOMEM
;
1514 ret
= parse_int(content
, &loglevel
);
1517 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1521 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1522 WARN("loglevel out of range.");
1523 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1527 event
.loglevel
= loglevel
;
1528 } else if (!strcmp((const char *) node
->name
,
1529 config_element_filter
)) {
1531 xmlNodeGetContent(node
);
1535 ret
= -LTTNG_ERR_NOMEM
;
1539 filter_expression
= strdup((char *) content
);
1541 if (!filter_expression
) {
1542 ret
= -LTTNG_ERR_NOMEM
;
1545 } else if (!strcmp((const char *) node
->name
,
1546 config_element_exclusions
)) {
1547 xmlNodePtr exclusion_node
;
1548 int exclusion_index
= 0;
1553 * Exclusions has already been initialized,
1556 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1560 exclusion_count
= xmlChildElementCount(node
);
1561 if (!exclusion_count
) {
1565 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1567 exclusion_count
= 0;
1568 ret
= -LTTNG_ERR_NOMEM
;
1572 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1573 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1575 xmlNodeGetContent(exclusion_node
);
1578 ret
= -LTTNG_ERR_NOMEM
;
1582 exclusions
[exclusion_index
] = strdup((const char *) content
);
1584 if (!exclusions
[exclusion_index
]) {
1585 ret
= -LTTNG_ERR_NOMEM
;
1591 event
.exclusion
= 1;
1592 } else if (!strcmp((const char *) node
->name
,
1593 config_element_attributes
)) {
1594 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1597 if (!attribute_node
) {
1598 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1602 if (!strcmp((const char *) node
->name
,
1603 config_element_probe_attributes
)) {
1604 xmlNodePtr probe_attribute_node
;
1606 /* probe_attributes */
1607 for (probe_attribute_node
=
1608 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1609 probe_attribute_node
= xmlNextElementSibling(
1610 probe_attribute_node
)) {
1612 ret
= process_probe_attribute_node(probe_attribute_node
,
1621 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1623 /* function_attributes */
1624 content
= xmlNodeGetContent(symbol_node
);
1626 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1630 sym_len
= strlen((char *) content
);
1631 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1632 WARN("Function name too long.");
1633 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1638 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1645 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1646 filter_expression
, exclusion_count
, exclusions
);
1651 if (!event
.enabled
) {
1653 * Note that we should use lttng_disable_event_ext() (2.6+) to
1654 * eliminate the risk of clashing on events of the same
1655 * name (with different event types and loglevels).
1657 * Unfortunately, lttng_disable_event_ext() only performs a
1658 * match on the name and event type and errors out if any other
1659 * event attribute is not set to its default value.
1661 * This will disable all events that match this name.
1663 ret
= lttng_disable_event(handle
, event
.name
, channel_name
);
1666 for (i
= 0; i
< exclusion_count
; i
++) {
1667 free(exclusions
[i
]);
1671 free(filter_expression
);
1676 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1677 const char *channel_name
)
1682 assert(events_node
);
1684 assert(channel_name
);
1686 for (node
= xmlFirstElementChild(events_node
); node
;
1687 node
= xmlNextElementSibling(node
)) {
1688 ret
= process_event_node(node
, handle
, channel_name
);
1698 int process_channel_attr_node(xmlNodePtr attr_node
,
1699 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1700 xmlNodePtr
*events_node
)
1706 assert(contexts_node
);
1707 assert(events_node
);
1709 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1714 content
= xmlNodeGetContent(attr_node
);
1716 ret
= -LTTNG_ERR_NOMEM
;
1720 name_len
= strlen((char *) content
);
1721 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1722 WARN("Channel name too long.");
1723 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1728 strncpy(channel
->name
, (const char *) content
, name_len
);
1730 } else if (!strcmp((const char *) attr_node
->name
,
1731 config_element_enabled
)) {
1736 content
= xmlNodeGetContent(attr_node
);
1738 ret
= -LTTNG_ERR_NOMEM
;
1742 ret
= parse_bool(content
, &enabled
);
1745 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1749 channel
->enabled
= enabled
;
1750 } else if (!strcmp((const char *) attr_node
->name
,
1751 config_element_overwrite_mode
)) {
1754 /* overwrite_mode */
1755 content
= xmlNodeGetContent(attr_node
);
1757 ret
= -LTTNG_ERR_NOMEM
;
1761 ret
= get_overwrite_mode(content
);
1764 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1768 channel
->attr
.overwrite
= ret
;
1769 } else if (!strcmp((const char *) attr_node
->name
,
1770 config_element_subbuf_size
)) {
1773 /* subbuffer_size */
1774 content
= xmlNodeGetContent(attr_node
);
1776 ret
= -LTTNG_ERR_NOMEM
;
1780 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1783 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1786 } else if (!strcmp((const char *) attr_node
->name
,
1787 config_element_num_subbuf
)) {
1790 /* subbuffer_count */
1791 content
= xmlNodeGetContent(attr_node
);
1793 ret
= -LTTNG_ERR_NOMEM
;
1797 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1800 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1803 } else if (!strcmp((const char *) attr_node
->name
,
1804 config_element_switch_timer_interval
)) {
1806 uint64_t switch_timer_interval
= 0;
1808 /* switch_timer_interval */
1809 content
= xmlNodeGetContent(attr_node
);
1811 ret
= -LTTNG_ERR_NOMEM
;
1815 ret
= parse_uint(content
, &switch_timer_interval
);
1818 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1822 if (switch_timer_interval
> UINT_MAX
) {
1823 WARN("switch_timer_interval out of range.");
1824 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1828 channel
->attr
.switch_timer_interval
=
1829 switch_timer_interval
;
1830 } else if (!strcmp((const char *) attr_node
->name
,
1831 config_element_read_timer_interval
)) {
1833 uint64_t read_timer_interval
= 0;
1835 /* read_timer_interval */
1836 content
= xmlNodeGetContent(attr_node
);
1838 ret
= -LTTNG_ERR_NOMEM
;
1842 ret
= parse_uint(content
, &read_timer_interval
);
1845 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1849 if (read_timer_interval
> UINT_MAX
) {
1850 WARN("read_timer_interval out of range.");
1851 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1855 channel
->attr
.read_timer_interval
=
1856 read_timer_interval
;
1857 } else if (!strcmp((const char *) attr_node
->name
,
1858 config_element_output_type
)) {
1862 content
= xmlNodeGetContent(attr_node
);
1864 ret
= -LTTNG_ERR_NOMEM
;
1868 ret
= get_output_type(content
);
1871 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1875 channel
->attr
.output
= ret
;
1876 } else if (!strcmp((const char *) attr_node
->name
,
1877 config_element_tracefile_size
)) {
1880 /* tracefile_size */
1881 content
= xmlNodeGetContent(attr_node
);
1883 ret
= -LTTNG_ERR_NOMEM
;
1887 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1890 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1893 } else if (!strcmp((const char *) attr_node
->name
,
1894 config_element_tracefile_count
)) {
1897 /* tracefile_count */
1898 content
= xmlNodeGetContent(attr_node
);
1900 ret
= -LTTNG_ERR_NOMEM
;
1904 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
1907 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1910 } else if (!strcmp((const char *) attr_node
->name
,
1911 config_element_live_timer_interval
)) {
1913 uint64_t live_timer_interval
= 0;
1915 /* live_timer_interval */
1916 content
= xmlNodeGetContent(attr_node
);
1918 ret
= -LTTNG_ERR_NOMEM
;
1922 ret
= parse_uint(content
, &live_timer_interval
);
1925 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1929 if (live_timer_interval
> UINT_MAX
) {
1930 WARN("live_timer_interval out of range.");
1931 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1935 channel
->attr
.live_timer_interval
=
1936 live_timer_interval
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_events
)) {
1940 *events_node
= attr_node
;
1943 *contexts_node
= attr_node
;
1951 int process_context_node(xmlNodePtr context_node
,
1952 struct lttng_handle
*handle
, const char *channel_name
)
1955 struct lttng_event_context context
;
1956 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
1959 assert(channel_name
);
1961 if (!context_child_node
) {
1962 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1966 memset(&context
, 0, sizeof(context
));
1968 if (!strcmp((const char *) context_child_node
->name
,
1969 config_element_type
)) {
1971 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
1973 ret
= -LTTNG_ERR_NOMEM
;
1977 ret
= get_context_type(content
);
1980 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1986 xmlNodePtr perf_attr_node
;
1989 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
1990 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
1991 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
1992 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
1993 perf_attr_node
; perf_attr_node
=
1994 xmlNextElementSibling(perf_attr_node
)) {
1995 if (!strcmp((const char *) perf_attr_node
->name
,
1996 config_element_type
)) {
2001 content
= xmlNodeGetContent(perf_attr_node
);
2003 ret
= -LTTNG_ERR_NOMEM
;
2007 ret
= parse_uint(content
, &type
);
2010 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2014 if (type
> UINT32_MAX
) {
2015 WARN("perf context type out of range.");
2016 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2020 context
.u
.perf_counter
.type
= type
;
2021 } else if (!strcmp((const char *) perf_attr_node
->name
,
2022 config_element_config
)) {
2024 uint64_t config
= 0;
2027 content
= xmlNodeGetContent(perf_attr_node
);
2029 ret
= -LTTNG_ERR_NOMEM
;
2033 ret
= parse_uint(content
, &config
);
2036 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2040 context
.u
.perf_counter
.config
= config
;
2041 } else if (!strcmp((const char *) perf_attr_node
->name
,
2042 config_element_name
)) {
2047 content
= xmlNodeGetContent(perf_attr_node
);
2049 ret
= -LTTNG_ERR_NOMEM
;
2053 name_len
= strlen((char *) content
);
2054 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2055 WARN("perf context name too long.");
2056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2061 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2068 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2074 int process_contexts_node(xmlNodePtr contexts_node
,
2075 struct lttng_handle
*handle
, const char *channel_name
)
2078 xmlNodePtr context_node
;
2080 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2081 context_node
= xmlNextElementSibling(context_node
)) {
2082 ret
= process_context_node(context_node
, handle
, channel_name
);
2092 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2095 struct lttng_domain domain
= { 0 };
2096 struct lttng_handle
*handle
= NULL
;
2097 xmlNodePtr channels_node
= NULL
;
2100 assert(session_name
);
2102 ret
= init_domain(domain_node
, &domain
);
2107 handle
= lttng_create_handle(session_name
, &domain
);
2109 ret
= -LTTNG_ERR_NOMEM
;
2113 /* get the channels node */
2114 for (node
= xmlFirstElementChild(domain_node
); node
;
2115 node
= xmlNextElementSibling(node
)) {
2116 if (!strcmp((const char *) node
->name
,
2117 config_element_channels
)) {
2118 channels_node
= node
;
2123 if (!channels_node
) {
2127 /* create all channels */
2128 for (node
= xmlFirstElementChild(channels_node
); node
;
2129 node
= xmlNextElementSibling(node
)) {
2130 struct lttng_channel channel
;
2131 xmlNodePtr contexts_node
= NULL
;
2132 xmlNodePtr events_node
= NULL
;
2133 xmlNodePtr channel_attr_node
;
2135 memset(&channel
, 0, sizeof(channel
));
2136 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2138 for (channel_attr_node
= xmlFirstElementChild(node
);
2139 channel_attr_node
; channel_attr_node
=
2140 xmlNextElementSibling(channel_attr_node
)) {
2141 ret
= process_channel_attr_node(channel_attr_node
,
2142 &channel
, &contexts_node
, &events_node
);
2148 ret
= lttng_enable_channel(handle
, &channel
);
2153 ret
= process_events_node(events_node
, handle
, channel
.name
);
2158 ret
= process_contexts_node(contexts_node
, handle
,
2165 lttng_destroy_handle(handle
);
2170 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2173 int ret
, started
= -1, snapshot_mode
= -1;
2174 uint64_t live_timer_interval
= UINT64_MAX
;
2175 xmlChar
*name
= NULL
;
2176 xmlChar
*shm_path
= NULL
;
2177 xmlNodePtr domains_node
= NULL
;
2178 xmlNodePtr output_node
= NULL
;
2180 struct lttng_domain
*kernel_domain
= NULL
;
2181 struct lttng_domain
*ust_domain
= NULL
;
2182 struct lttng_domain
*jul_domain
= NULL
;
2183 struct lttng_domain
*log4j_domain
= NULL
;
2184 struct lttng_domain
*python_domain
= NULL
;
2186 for (node
= xmlFirstElementChild(session_node
); node
;
2187 node
= xmlNextElementSibling(node
)) {
2188 if (!name
&& !strcmp((const char *) node
->name
,
2189 config_element_name
)) {
2191 xmlChar
*node_content
= xmlNodeGetContent(node
);
2192 if (!node_content
) {
2193 ret
= -LTTNG_ERR_NOMEM
;
2197 name
= node_content
;
2198 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2199 config_element_domains
)) {
2201 domains_node
= node
;
2202 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2203 config_element_started
)) {
2205 xmlChar
*node_content
= xmlNodeGetContent(node
);
2206 if (!node_content
) {
2207 ret
= -LTTNG_ERR_NOMEM
;
2211 ret
= parse_bool(node_content
, &started
);
2214 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2217 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2218 config_element_output
)) {
2221 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2222 config_element_shared_memory_path
)) {
2223 /* shared memory path */
2224 xmlChar
*node_content
= xmlNodeGetContent(node
);
2225 if (!node_content
) {
2226 ret
= -LTTNG_ERR_NOMEM
;
2230 shm_path
= node_content
;
2232 /* attributes, snapshot_mode or live_timer_interval */
2233 xmlNodePtr attributes_child
=
2234 xmlFirstElementChild(node
);
2236 if (!strcmp((const char *) attributes_child
->name
,
2237 config_element_snapshot_mode
)) {
2239 xmlChar
*snapshot_mode_content
=
2240 xmlNodeGetContent(attributes_child
);
2241 if (!snapshot_mode_content
) {
2242 ret
= -LTTNG_ERR_NOMEM
;
2246 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2247 free(snapshot_mode_content
);
2249 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2253 /* live_timer_interval */
2254 xmlChar
*timer_interval_content
=
2255 xmlNodeGetContent(attributes_child
);
2256 if (!timer_interval_content
) {
2257 ret
= -LTTNG_ERR_NOMEM
;
2261 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2262 free(timer_interval_content
);
2264 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2272 /* Mandatory attribute, as defined in the session XSD */
2273 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2277 if (session_name
&& strcmp((char *) name
, session_name
)) {
2278 /* This is not the session we are looking for */
2279 ret
= -LTTNG_ERR_NO_SESSION
;
2283 /* Init domains to create the session handles */
2284 for (node
= xmlFirstElementChild(domains_node
); node
;
2285 node
= xmlNextElementSibling(node
)) {
2286 struct lttng_domain
*domain
;
2288 domain
= zmalloc(sizeof(*domain
));
2290 ret
= -LTTNG_ERR_NOMEM
;
2294 ret
= init_domain(node
, domain
);
2296 goto domain_init_error
;
2299 switch (domain
->type
) {
2300 case LTTNG_DOMAIN_KERNEL
:
2301 if (kernel_domain
) {
2302 /* Same domain seen twice, invalid! */
2303 goto domain_init_error
;
2305 kernel_domain
= domain
;
2307 case LTTNG_DOMAIN_UST
:
2309 /* Same domain seen twice, invalid! */
2310 goto domain_init_error
;
2312 ust_domain
= domain
;
2314 case LTTNG_DOMAIN_JUL
:
2316 /* Same domain seen twice, invalid! */
2317 goto domain_init_error
;
2319 jul_domain
= domain
;
2321 case LTTNG_DOMAIN_LOG4J
:
2323 /* Same domain seen twice, invalid! */
2324 goto domain_init_error
;
2326 log4j_domain
= domain
;
2328 case LTTNG_DOMAIN_PYTHON
:
2329 if (python_domain
) {
2330 /* Same domain seen twice, invalid! */
2331 goto domain_init_error
;
2333 python_domain
= domain
;
2336 WARN("Invalid domain type");
2337 goto domain_init_error
;
2342 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2347 /* Destroy session if it exists */
2348 ret
= lttng_destroy_session((const char *) name
);
2349 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2350 ERR("Failed to destroy existing session.");
2355 /* Create session type depending on output type */
2356 if (snapshot_mode
&& snapshot_mode
!= -1) {
2357 ret
= create_snapshot_session((const char *) name
, output_node
);
2358 } else if (live_timer_interval
&&
2359 live_timer_interval
!= UINT64_MAX
) {
2360 ret
= create_session((const char *) name
, kernel_domain
,
2361 ust_domain
, jul_domain
, log4j_domain
,
2362 output_node
, live_timer_interval
);
2364 /* regular session */
2365 ret
= create_session((const char *) name
, kernel_domain
,
2366 ust_domain
, jul_domain
, log4j_domain
,
2367 output_node
, UINT64_MAX
);
2374 ret
= lttng_set_session_shm_path((const char *) name
,
2375 (const char *) shm_path
);
2381 for (node
= xmlFirstElementChild(domains_node
); node
;
2382 node
= xmlNextElementSibling(node
)) {
2383 ret
= process_domain_node(node
, (const char *) name
);
2390 ret
= lttng_start_tracing((const char *) name
);
2398 ERR("Failed to load session %s: %s", (const char *) name
,
2399 lttng_strerror(ret
));
2400 lttng_destroy_session((const char *) name
);
2404 free(kernel_domain
);
2408 free(python_domain
);
2415 * Return 1 if the given path is readable by the current UID or 0 if not.
2416 * Return -1 if the path is EPERM.
2418 static int validate_file_read_creds(const char *path
)
2424 /* Can we read the file. */
2425 ret
= access(path
, R_OK
);
2429 if (errno
== EACCES
) {
2440 int load_session_from_file(const char *path
, const char *session_name
,
2441 struct session_config_validation_ctx
*validation_ctx
, int override
)
2443 int ret
, session_found
= !session_name
;
2444 xmlDocPtr doc
= NULL
;
2445 xmlNodePtr sessions_node
;
2446 xmlNodePtr session_node
;
2449 assert(validation_ctx
);
2451 ret
= validate_file_read_creds(path
);
2454 ret
= -LTTNG_ERR_EPERM
;
2456 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2461 doc
= xmlParseFile(path
);
2463 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2467 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2469 ERR("Session configuration file validation failed");
2470 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2474 sessions_node
= xmlDocGetRootElement(doc
);
2475 if (!sessions_node
) {
2479 for (session_node
= xmlFirstElementChild(sessions_node
);
2480 session_node
; session_node
=
2481 xmlNextElementSibling(session_node
)) {
2482 ret
= process_session_node(session_node
,
2483 session_name
, override
);
2484 if (session_name
&& ret
== 0) {
2485 /* Target session found and loaded */
2493 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2498 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2500 struct dirent
*alloc_dirent(const char *path
)
2504 struct dirent
*entry
;
2506 name_max
= pathconf(path
, _PC_NAME_MAX
);
2507 if (name_max
== -1) {
2508 name_max
= PATH_MAX
;
2510 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2511 entry
= zmalloc(len
);
2516 int load_session_from_path(const char *path
, const char *session_name
,
2517 struct session_config_validation_ctx
*validation_ctx
, int override
)
2519 int ret
, session_found
= !session_name
;
2520 DIR *directory
= NULL
;
2523 assert(validation_ctx
);
2525 directory
= opendir(path
);
2529 /* Try the file loading. */
2532 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2535 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2540 struct dirent
*entry
;
2541 struct dirent
*result
;
2542 char *file_path
= NULL
;
2543 size_t path_len
= strlen(path
);
2545 if (path_len
>= PATH_MAX
) {
2546 ret
= -LTTNG_ERR_INVALID
;
2550 entry
= alloc_dirent(path
);
2552 ret
= -LTTNG_ERR_NOMEM
;
2556 file_path
= zmalloc(PATH_MAX
);
2558 ret
= -LTTNG_ERR_NOMEM
;
2563 strncpy(file_path
, path
, path_len
);
2564 if (file_path
[path_len
- 1] != '/') {
2565 file_path
[path_len
++] = '/';
2569 /* Search for *.lttng files */
2570 while (!readdir_r(directory
, entry
, &result
) && result
) {
2571 size_t file_name_len
= strlen(result
->d_name
);
2573 if (file_name_len
<=
2574 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2578 if (path_len
+ file_name_len
>= PATH_MAX
) {
2582 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2583 result
->d_name
+ file_name_len
- sizeof(
2584 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2588 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2589 file_path
[path_len
+ file_name_len
] = '\0';
2591 ret
= load_session_from_file(file_path
, session_name
,
2592 validation_ctx
, override
);
2593 if (session_name
&& !ret
) {
2602 ret
= load_session_from_file(path
, session_name
,
2603 validation_ctx
, override
);
2613 if (closedir(directory
)) {
2618 if (!session_found
) {
2619 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2626 * Validate that the given path's credentials and the current process have the
2627 * same UID. If so, return 1 else return 0 if it does NOT match.
2629 static int validate_path_creds(const char *path
)
2631 int ret
, uid
= getuid();
2640 ret
= stat(path
, &buf
);
2642 if (errno
!= ENOENT
) {
2645 ret
= -LTTNG_ERR_INVALID
;
2649 if (buf
.st_uid
!= uid
) {
2660 int config_load_session(const char *path
, const char *session_name
,
2661 int override
, unsigned int autoload
)
2664 const char *path_ptr
= NULL
;
2665 struct session_config_validation_ctx validation_ctx
= { 0 };
2667 ret
= init_session_config_validation_ctx(&validation_ctx
);
2674 const char *sys_path
;
2677 home_path
= utils_get_home_dir();
2679 char path
[PATH_MAX
];
2682 * Try user session configuration path. Ignore error here so we can
2683 * continue loading the system wide sessions.
2686 ret
= snprintf(path
, sizeof(path
),
2687 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2688 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2690 PERROR("snprintf session autoload home config path");
2695 * Credentials are only validated for the autoload in order to
2696 * avoid any user session daemon to try to load kernel sessions
2697 * automatically and failing all the times.
2699 ret
= validate_path_creds(path
);
2704 ret
= snprintf(path
, sizeof(path
),
2705 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
2707 PERROR("snprintf session home config path");
2713 ret
= load_session_from_path(path_ptr
, session_name
,
2714 &validation_ctx
, override
);
2715 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
2719 * Continue even if the session was found since we have to try
2720 * the system wide sessions.
2725 /* Reset path pointer for the system wide dir. */
2728 /* Try system wide configuration directory. */
2730 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
2731 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
2732 ret
= validate_path_creds(sys_path
);
2734 path_ptr
= sys_path
;
2737 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
2738 path_ptr
= sys_path
;
2742 ret
= load_session_from_path(path_ptr
, session_name
,
2743 &validation_ctx
, override
);
2746 ret
= access(path
, F_OK
);
2751 ret
= -LTTNG_ERR_INVALID
;
2752 WARN("Session configuration path does not exist.");
2755 ret
= -LTTNG_ERR_EPERM
;
2758 ret
= -LTTNG_ERR_UNK
;
2764 ret
= load_session_from_path(path
, session_name
,
2765 &validation_ctx
, override
);
2768 fini_session_config_validation_ctx(&validation_ctx
);
2769 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
2771 * Don't report an error if no sessions are found when called
2772 * without a session_name or a search path.