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>
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 <libxml/xpath.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
45 #include "session-config.h"
46 #include "config-internal.h"
48 struct handler_filter_args
{
50 config_entry_handler_cb handler
;
54 struct session_config_validation_ctx
{
55 xmlSchemaParserCtxtPtr parser_ctx
;
57 xmlSchemaValidCtxtPtr schema_validation_ctx
;
60 const char * const config_str_yes
= "yes";
61 const char * const config_str_true
= "true";
62 const char * const config_str_on
= "on";
63 const char * const config_str_no
= "no";
64 const char * const config_str_false
= "false";
65 const char * const config_str_off
= "off";
66 const char * const config_xml_encoding
= "UTF-8";
67 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
68 const char * const config_xml_indent_string
= "\t";
69 const char * const config_xml_true
= "true";
70 const char * const config_xml_false
= "false";
72 const char * const config_element_channel
= "channel";
73 const char * const config_element_channels
= "channels";
74 const char * const config_element_domain
= "domain";
75 const char * const config_element_domains
= "domains";
76 const char * const config_element_event
= "event";
77 const char * const config_element_events
= "events";
78 const char * const config_element_context
= "context";
79 const char * const config_element_contexts
= "contexts";
80 const char * const config_element_attributes
= "attributes";
81 const char * const config_element_exclusion
= "exclusion";
82 const char * const config_element_exclusions
= "exclusions";
83 const char * const config_element_function_attributes
= "function_attributes";
84 const char * const config_element_probe_attributes
= "probe_attributes";
85 const char * const config_element_symbol_name
= "symbol_name";
86 const char * const config_element_address
= "address";
87 const char * const config_element_offset
= "offset";
88 const char * const config_element_name
= "name";
89 const char * const config_element_enabled
= "enabled";
90 const char * const config_element_overwrite_mode
= "overwrite_mode";
91 const char * const config_element_subbuf_size
= "subbuffer_size";
92 const char * const config_element_num_subbuf
= "subbuffer_count";
93 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
94 const char * const config_element_read_timer_interval
= "read_timer_interval";
95 const char * const config_element_output
= "output";
96 const char * const config_element_output_type
= "output_type";
97 const char * const config_element_tracefile_size
= "tracefile_size";
98 const char * const config_element_tracefile_count
= "tracefile_count";
99 const char * const config_element_live_timer_interval
= "live_timer_interval";
100 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
101 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
102 const char * const config_element_type
= "type";
103 const char * const config_element_buffer_type
= "buffer_type";
104 const char * const config_element_session
= "session";
105 const char * const config_element_sessions
= "sessions";
106 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
107 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
108 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
109 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
110 const char * const config_element_config
= "config";
111 const char * const config_element_started
= "started";
112 const char * const config_element_snapshot_mode
= "snapshot_mode";
113 const char * const config_element_loglevel
= "loglevel";
114 const char * const config_element_loglevel_type
= "loglevel_type";
115 const char * const config_element_filter
= "filter";
116 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
117 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
118 const char * const config_element_consumer_output
= "consumer_output";
119 const char * const config_element_destination
= "destination";
120 const char * const config_element_path
= "path";
121 const char * const config_element_net_output
= "net_output";
122 const char * const config_element_control_uri
= "control_uri";
123 const char * const config_element_data_uri
= "data_uri";
124 const char * const config_element_max_size
= "max_size";
125 const char * const config_element_pid
= "pid";
126 const char * const config_element_pids
= "pids";
127 const char * const config_element_shared_memory_path
= "shared_memory_path";
128 const char * const config_element_pid_tracker
= "pid_tracker";
129 const char * const config_element_trackers
= "trackers";
130 const char * const config_element_targets
= "targets";
131 const char * const config_element_target_pid
= "pid_target";
132 const char * const config_element_omit_name
= "omit_name";
133 const char * const config_element_omit_output
= "omit_output";
135 const char * const config_domain_type_kernel
= "KERNEL";
136 const char * const config_domain_type_ust
= "UST";
137 const char * const config_domain_type_jul
= "JUL";
138 const char * const config_domain_type_log4j
= "LOG4J";
139 const char * const config_domain_type_python
= "PYTHON";
141 const char * const config_buffer_type_per_pid
= "PER_PID";
142 const char * const config_buffer_type_per_uid
= "PER_UID";
143 const char * const config_buffer_type_global
= "GLOBAL";
145 const char * const config_overwrite_mode_discard
= "DISCARD";
146 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
148 const char * const config_output_type_splice
= "SPLICE";
149 const char * const config_output_type_mmap
= "MMAP";
151 const char * const config_loglevel_type_all
= "ALL";
152 const char * const config_loglevel_type_range
= "RANGE";
153 const char * const config_loglevel_type_single
= "SINGLE";
155 const char * const config_event_type_all
= "ALL";
156 const char * const config_event_type_tracepoint
= "TRACEPOINT";
157 const char * const config_event_type_probe
= "PROBE";
158 const char * const config_event_type_function
= "FUNCTION";
159 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
160 const char * const config_event_type_noop
= "NOOP";
161 const char * const config_event_type_syscall
= "SYSCALL";
162 const char * const config_event_type_kprobe
= "KPROBE";
163 const char * const config_event_type_kretprobe
= "KRETPROBE";
165 const char * const config_event_context_pid
= "PID";
166 const char * const config_event_context_procname
= "PROCNAME";
167 const char * const config_event_context_prio
= "PRIO";
168 const char * const config_event_context_nice
= "NICE";
169 const char * const config_event_context_vpid
= "VPID";
170 const char * const config_event_context_tid
= "TID";
171 const char * const config_event_context_vtid
= "VTID";
172 const char * const config_event_context_ppid
= "PPID";
173 const char * const config_event_context_vppid
= "VPPID";
174 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
175 const char * const config_event_context_hostname
= "HOSTNAME";
176 const char * const config_event_context_ip
= "IP";
177 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
178 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
179 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
180 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
181 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
182 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
184 /* Deprecated symbols */
185 const char * const config_element_perf
;
187 enum process_event_node_phase
{
192 struct consumer_output
{
199 static int config_entry_handler_filter(struct handler_filter_args
*args
,
200 const char *section
, const char *name
, const char *value
)
203 struct config_entry entry
= { section
, name
, value
};
207 if (!section
|| !name
|| !value
) {
213 if (strcmp(args
->section
, section
)) {
218 ret
= args
->handler(&entry
, args
->user_data
);
224 const char *config_get_domain_str(enum lttng_domain_type domain
)
229 case LTTNG_DOMAIN_KERNEL
:
230 str_dom
= config_domain_type_kernel
;
232 case LTTNG_DOMAIN_UST
:
233 str_dom
= config_domain_type_ust
;
235 case LTTNG_DOMAIN_JUL
:
236 str_dom
= config_domain_type_jul
;
238 case LTTNG_DOMAIN_LOG4J
:
239 str_dom
= config_domain_type_log4j
;
241 case LTTNG_DOMAIN_PYTHON
:
242 str_dom
= config_domain_type_python
;
252 int config_get_section_entries(const char *override_path
, const char *section
,
253 config_entry_handler_cb handler
, void *user_data
)
257 FILE *config_file
= NULL
;
258 struct handler_filter_args filter
= { section
, handler
, user_data
};
260 /* First, try system-wide conf. file. */
261 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
263 config_file
= fopen(path
, "r");
265 DBG("Loading daemon conf file at %s", path
);
267 * Return value is not very important here since error or not, we
268 * continue and try the next possible conf. file.
270 (void) ini_parse_file(config_file
,
271 (ini_entry_handler
) config_entry_handler_filter
,
276 /* Second is the user local configuration. */
277 path
= utils_get_home_dir();
279 char fullpath
[PATH_MAX
];
281 ret
= snprintf(fullpath
, sizeof(fullpath
),
282 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
284 PERROR("snprintf user conf. path");
288 config_file
= fopen(fullpath
, "r");
290 DBG("Loading daemon user conf file at %s", path
);
292 * Return value is not very important here since error or not, we
293 * continue and try the next possible conf. file.
295 (void) ini_parse_file(config_file
,
296 (ini_entry_handler
) config_entry_handler_filter
,
302 /* Final path is the one that the user might have provided. */
304 config_file
= fopen(override_path
, "r");
306 DBG("Loading daemon command line conf file at %s", override_path
);
307 (void) ini_parse_file(config_file
,
308 (ini_entry_handler
) config_entry_handler_filter
,
312 ERR("Failed to open daemon configuration file at %s",
319 /* Everything went well. */
327 int config_parse_value(const char *value
)
330 char *endptr
, *lower_str
;
340 v
= strtoul(value
, &endptr
, 10);
341 if (endptr
!= value
) {
346 lower_str
= zmalloc(len
+ 1);
353 for (i
= 0; i
< len
; i
++) {
354 lower_str
[i
] = tolower(value
[i
]);
357 if (!strcmp(lower_str
, config_str_yes
) ||
358 !strcmp(lower_str
, config_str_true
) ||
359 !strcmp(lower_str
, config_str_on
)) {
361 } else if (!strcmp(lower_str
, config_str_no
) ||
362 !strcmp(lower_str
, config_str_false
) ||
363 !strcmp(lower_str
, config_str_off
)) {
375 * Returns a xmlChar string which must be released using xmlFree().
377 static xmlChar
*encode_string(const char *in_str
)
379 xmlChar
*out_str
= NULL
;
380 xmlCharEncodingHandlerPtr handler
;
381 int out_len
, ret
, in_len
;
385 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
387 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
391 in_len
= strlen(in_str
);
393 * Add 1 byte for the NULL terminted character. The factor 4 here is
394 * used because UTF-8 characters can take up to 4 bytes.
396 out_len
= (in_len
* 4) + 1;
397 out_str
= xmlMalloc(out_len
);
402 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
409 /* out_len is now the size of out_str */
410 out_str
[out_len
] = '\0';
416 struct config_writer
*config_writer_create(int fd_output
, int indent
)
419 struct config_writer
*writer
;
420 xmlOutputBufferPtr buffer
;
422 writer
= zmalloc(sizeof(struct config_writer
));
424 PERROR("zmalloc config_writer_create");
428 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
433 writer
->writer
= xmlNewTextWriter(buffer
);
434 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
435 config_xml_encoding
, NULL
);
440 ret
= xmlTextWriterSetIndentString(writer
->writer
,
441 BAD_CAST config_xml_indent_string
);
446 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
454 config_writer_destroy(writer
);
459 int config_writer_destroy(struct config_writer
*writer
)
468 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
469 WARN("Could not close XML document");
473 if (writer
->writer
) {
474 xmlFreeTextWriter(writer
->writer
);
483 int config_writer_open_element(struct config_writer
*writer
,
484 const char *element_name
)
487 xmlChar
*encoded_element_name
;
489 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
494 encoded_element_name
= encode_string(element_name
);
495 if (!encoded_element_name
) {
500 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
501 xmlFree(encoded_element_name
);
503 return ret
>= 0 ? 0 : ret
;
507 int config_writer_write_attribute(struct config_writer
*writer
,
508 const char *name
, const char *value
)
511 xmlChar
*encoded_name
= NULL
;
512 xmlChar
*encoded_value
= NULL
;
514 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
519 encoded_name
= encode_string(name
);
525 encoded_value
= encode_string(value
);
526 if (!encoded_value
) {
531 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
534 xmlFree(encoded_name
);
535 xmlFree(encoded_value
);
536 return ret
>= 0 ? 0 : ret
;
540 int config_writer_close_element(struct config_writer
*writer
)
544 if (!writer
|| !writer
->writer
) {
549 ret
= xmlTextWriterEndElement(writer
->writer
);
551 return ret
>= 0 ? 0 : ret
;
555 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
556 const char *element_name
, uint64_t value
)
559 xmlChar
*encoded_element_name
;
561 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
566 encoded_element_name
= encode_string(element_name
);
567 if (!encoded_element_name
) {
572 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
573 encoded_element_name
, "%" PRIu64
, value
);
574 xmlFree(encoded_element_name
);
576 return ret
>= 0 ? 0 : ret
;
580 int config_writer_write_element_signed_int(struct config_writer
*writer
,
581 const char *element_name
, int64_t value
)
584 xmlChar
*encoded_element_name
;
586 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
591 encoded_element_name
= encode_string(element_name
);
592 if (!encoded_element_name
) {
597 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
598 encoded_element_name
, "%" PRIi64
, value
);
599 xmlFree(encoded_element_name
);
601 return ret
>= 0 ? 0 : ret
;
605 int config_writer_write_element_bool(struct config_writer
*writer
,
606 const char *element_name
, int value
)
608 return config_writer_write_element_string(writer
, element_name
,
609 value
? config_xml_true
: config_xml_false
);
613 int config_writer_write_element_string(struct config_writer
*writer
,
614 const char *element_name
, const char *value
)
617 xmlChar
*encoded_element_name
= NULL
;
618 xmlChar
*encoded_value
= NULL
;
620 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
626 encoded_element_name
= encode_string(element_name
);
627 if (!encoded_element_name
) {
632 encoded_value
= encode_string(value
);
633 if (!encoded_value
) {
638 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
641 xmlFree(encoded_element_name
);
642 xmlFree(encoded_value
);
643 return ret
>= 0 ? 0 : ret
;
647 int config_writer_write_config_element(struct config_writer
*writer
,
648 const struct config_element
*element
)
652 xmlNodePtr local_copy
= NULL
;
653 xmlNodePtr place_holder
= NULL
;
654 xmlDocPtr document
= NULL
;
655 xmlBufferPtr buffer
= NULL
;
658 assert(element
->element
);
661 /* Create a new doc and set root node as passed element */
662 document
= xmlNewDoc(BAD_CAST
"1.0");
663 document
->parseFlags
|= XML_PARSE_NOBLANKS
;
665 ERR("Unable to create xml document");
670 local_copy
= xmlCopyNode(element
->element
, 1);
672 ERR("Unable to copy an xml node");
677 /* Hand off the local_copy to the xmlDoc */
678 place_holder
= xmlDocSetRootElement(document
, local_copy
);
680 ERR("The document root was already set");
681 xmlFreeNode(local_copy
);
686 buffer
= xmlBufferCreate();
692 ret
= xmlNodeDump(buffer
, document
, local_copy
, 0, 0);
693 xmlTextWriterWriteRaw(writer
->writer
, xmlBufferContent(buffer
));
695 xmlBufferFree(buffer
);
696 xmlFreeDoc(document
);
701 void xml_error_handler(void *ctx
, const char *format
, ...)
707 va_start(args
, format
);
708 ret
= vasprintf(&errMsg
, format
, args
);
711 ERR("String allocation failed in xml error handler");
715 fprintf(stderr
, "XML Error: %s", errMsg
);
720 void fini_session_config_validation_ctx(
721 struct session_config_validation_ctx
*ctx
)
723 if (ctx
->parser_ctx
) {
724 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
728 xmlSchemaFree(ctx
->schema
);
731 if (ctx
->schema_validation_ctx
) {
732 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
735 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
739 char *get_session_config_xsd_path()
742 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
743 size_t base_path_len
;
747 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
750 base_path_len
= strlen(base_path
);
751 max_path_len
= base_path_len
+
752 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
753 xsd_path
= zmalloc(max_path_len
);
758 strncpy(xsd_path
, base_path
, max_path_len
);
759 if (xsd_path
[base_path_len
- 1] != '/') {
760 xsd_path
[base_path_len
++] = '/';
763 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
764 max_path_len
- base_path_len
);
770 int init_session_config_validation_ctx(
771 struct session_config_validation_ctx
*ctx
)
774 char *xsd_path
= get_session_config_xsd_path();
777 ret
= -LTTNG_ERR_NOMEM
;
781 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
782 if (!ctx
->parser_ctx
) {
783 ERR("XSD parser context creation failed");
784 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
787 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
788 xml_error_handler
, NULL
);
790 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
792 ERR("XSD parsing failed");
793 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
797 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
798 if (!ctx
->schema_validation_ctx
) {
799 ERR("XSD validation context creation failed");
800 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
804 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
805 xml_error_handler
, NULL
);
810 fini_session_config_validation_ctx(ctx
);
818 int parse_uint(xmlChar
*str
, uint64_t *val
)
828 *val
= strtoull((const char *) str
, &endptr
, 10);
829 if (!endptr
|| *endptr
) {
840 int parse_int(xmlChar
*str
, int64_t *val
)
850 *val
= strtoll((const char *) str
, &endptr
, 10);
851 if (!endptr
|| *endptr
) {
862 int parse_bool(xmlChar
*str
, int *val
)
871 if (!strcmp((const char *) str
, config_xml_true
)) {
873 } else if (!strcmp((const char *) str
, config_xml_false
)) {
876 WARN("Invalid boolean value encoutered (%s).",
885 int get_domain_type(xmlChar
*domain
)
893 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
894 ret
= LTTNG_DOMAIN_KERNEL
;
895 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
896 ret
= LTTNG_DOMAIN_UST
;
897 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
898 ret
= LTTNG_DOMAIN_JUL
;
899 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
900 ret
= LTTNG_DOMAIN_LOG4J
;
901 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
902 ret
= LTTNG_DOMAIN_PYTHON
;
913 int get_buffer_type(xmlChar
*buffer_type
)
921 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
922 ret
= LTTNG_BUFFER_GLOBAL
;
923 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
924 ret
= LTTNG_BUFFER_PER_UID
;
925 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
926 ret
= LTTNG_BUFFER_PER_PID
;
937 int get_overwrite_mode(xmlChar
*overwrite_mode
)
941 if (!overwrite_mode
) {
945 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
947 } else if (!strcmp((char *) overwrite_mode
,
948 config_overwrite_mode_discard
)) {
960 int get_output_type(xmlChar
*output_type
)
968 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
969 ret
= LTTNG_EVENT_MMAP
;
970 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
971 ret
= LTTNG_EVENT_SPLICE
;
982 int config_get_event_type(const char *event_type
)
990 if (!strcmp(event_type
, config_event_type_all
)) {
991 ret
= LTTNG_EVENT_ALL
;
992 } else if (!strcmp(event_type
, config_event_type_tracepoint
)) {
993 ret
= LTTNG_EVENT_TRACEPOINT
;
994 } else if (!strcmp(event_type
, config_event_type_probe
)) {
995 ret
= LTTNG_EVENT_PROBE
;
996 } else if (!strcmp(event_type
, config_event_type_function
)) {
997 ret
= LTTNG_EVENT_FUNCTION
;
998 } else if (!strcmp(event_type
,
999 config_event_type_function_entry
)) {
1000 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
1001 } else if (!strcmp(event_type
, config_event_type_noop
)) {
1002 ret
= LTTNG_EVENT_NOOP
;
1003 } else if (!strcmp(event_type
, config_event_type_syscall
)) {
1004 ret
= LTTNG_EVENT_SYSCALL
;
1015 int get_loglevel_type(xmlChar
*loglevel_type
)
1019 if (!loglevel_type
) {
1023 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
1024 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
1025 } else if (!strcmp((char *) loglevel_type
,
1026 config_loglevel_type_range
)) {
1027 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1028 } else if (!strcmp((char *) loglevel_type
,
1029 config_loglevel_type_single
)) {
1030 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1041 * Return the context type or -1 on error.
1044 int get_context_type(xmlChar
*context_type
)
1048 if (!context_type
) {
1052 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
1053 ret
= LTTNG_EVENT_CONTEXT_PID
;
1054 } else if (!strcmp((char *) context_type
,
1055 config_event_context_procname
)) {
1056 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1057 } else if (!strcmp((char *) context_type
,
1058 config_event_context_prio
)) {
1059 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
1060 } else if (!strcmp((char *) context_type
,
1061 config_event_context_nice
)) {
1062 ret
= LTTNG_EVENT_CONTEXT_NICE
;
1063 } else if (!strcmp((char *) context_type
,
1064 config_event_context_vpid
)) {
1065 ret
= LTTNG_EVENT_CONTEXT_VPID
;
1066 } else if (!strcmp((char *) context_type
,
1067 config_event_context_tid
)) {
1068 ret
= LTTNG_EVENT_CONTEXT_TID
;
1069 } else if (!strcmp((char *) context_type
,
1070 config_event_context_vtid
)) {
1071 ret
= LTTNG_EVENT_CONTEXT_VTID
;
1072 } else if (!strcmp((char *) context_type
,
1073 config_event_context_ppid
)) {
1074 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1075 } else if (!strcmp((char *) context_type
,
1076 config_event_context_vppid
)) {
1077 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1078 } else if (!strcmp((char *) context_type
,
1079 config_event_context_pthread_id
)) {
1080 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1081 } else if (!strcmp((char *) context_type
,
1082 config_event_context_hostname
)) {
1083 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1084 } else if (!strcmp((char *) context_type
,
1085 config_event_context_ip
)) {
1086 ret
= LTTNG_EVENT_CONTEXT_IP
;
1087 } else if (!strcmp((char *) context_type
,
1088 config_event_context_interruptible
)) {
1089 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1090 } else if (!strcmp((char *) context_type
,
1091 config_event_context_preemptible
)) {
1092 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1093 } else if (!strcmp((char *) context_type
,
1094 config_event_context_need_reschedule
)) {
1095 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1096 } else if (!strcmp((char *) context_type
,
1097 config_event_context_migratable
)) {
1098 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1109 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1114 for (node
= xmlFirstElementChild(domain_node
); node
;
1115 node
= xmlNextElementSibling(node
)) {
1116 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1118 xmlChar
*node_content
= xmlNodeGetContent(node
);
1119 if (!node_content
) {
1120 ret
= -LTTNG_ERR_NOMEM
;
1124 ret
= get_domain_type(node_content
);
1127 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1132 } else if (!strcmp((const char *) node
->name
,
1133 config_element_buffer_type
)) {
1135 xmlChar
*node_content
= xmlNodeGetContent(node
);
1136 if (!node_content
) {
1137 ret
= -LTTNG_ERR_NOMEM
;
1141 ret
= get_buffer_type(node_content
);
1144 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1148 domain
->buf_type
= ret
;
1157 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1162 for (node
= xmlFirstElementChild(net_output_node
); node
;
1163 node
= xmlNextElementSibling(node
)) {
1164 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1166 *control_uri
= (char *) xmlNodeGetContent(node
);
1167 if (!*control_uri
) {
1172 *data_uri
= (char *) xmlNodeGetContent(node
);
1179 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1183 int process_consumer_output(xmlNodePtr consumer_output_node
,
1184 struct consumer_output
*output
)
1191 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1192 node
= xmlNextElementSibling(node
)) {
1193 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1194 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1198 ret
= -LTTNG_ERR_NOMEM
;
1202 ret
= parse_bool(enabled_str
, &output
->enabled
);
1208 xmlNodePtr output_type_node
;
1211 output_type_node
= xmlFirstElementChild(node
);
1212 if (!output_type_node
) {
1213 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1217 if (!strcmp((const char *) output_type_node
->name
,
1218 config_element_path
)) {
1220 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1221 if (!output
->path
) {
1222 ret
= -LTTNG_ERR_NOMEM
;
1227 ret
= get_net_output_uris(output_type_node
,
1228 &output
->control_uri
, &output
->data_uri
);
1240 free(output
->control_uri
);
1241 free(output
->data_uri
);
1242 memset(output
, 0, sizeof(struct consumer_output
));
1248 int create_session_net_output(const char *name
, const char *control_uri
,
1249 const char *data_uri
)
1252 struct lttng_handle
*handle
;
1253 const char *uri
= NULL
;
1257 handle
= lttng_create_handle(name
, NULL
);
1259 ret
= -LTTNG_ERR_NOMEM
;
1263 if (!control_uri
|| !data_uri
) {
1264 uri
= control_uri
? control_uri
: data_uri
;
1269 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1270 lttng_destroy_handle(handle
);
1276 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1279 xmlNodePtr node
= NULL
;
1280 xmlNodePtr snapshot_output_list_node
;
1281 xmlNodePtr snapshot_output_node
;
1283 assert(session_name
);
1285 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1294 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1296 /* Parse and create snapshot outputs */
1298 for (snapshot_output_node
=
1299 xmlFirstElementChild(snapshot_output_list_node
);
1300 snapshot_output_node
; snapshot_output_node
=
1301 xmlNextElementSibling(snapshot_output_node
)) {
1303 uint64_t max_size
= DEFAULT_SNAPSHOT_MAX_SIZE
;
1304 struct consumer_output output
= { 0 };
1305 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1307 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1308 node
= xmlNextElementSibling(node
)) {
1309 if (!strcmp((const char *) node
->name
,
1310 config_element_name
)) {
1312 name
= (char *) xmlNodeGetContent(node
);
1314 ret
= -LTTNG_ERR_NOMEM
;
1315 goto error_snapshot_output
;
1317 } else if (!strcmp((const char *) node
->name
,
1318 config_element_max_size
)) {
1319 xmlChar
*content
= xmlNodeGetContent(node
);
1323 ret
= -LTTNG_ERR_NOMEM
;
1324 goto error_snapshot_output
;
1326 ret
= parse_uint(content
, &max_size
);
1329 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1330 goto error_snapshot_output
;
1333 /* consumer_output */
1334 ret
= process_consumer_output(node
, &output
);
1336 goto error_snapshot_output
;
1341 snapshot_output
= lttng_snapshot_output_create();
1342 if (!snapshot_output
) {
1343 ret
= -LTTNG_ERR_NOMEM
;
1344 goto error_snapshot_output
;
1348 /* Generate a default name */
1350 pret
= asprintf(&name
, DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
1351 lttng_snapshot_output_get_id(snapshot_output
));
1354 PERROR("snprintf output name");
1355 goto error_snapshot_output
;
1359 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1361 goto error_snapshot_output
;
1364 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1366 goto error_snapshot_output
;
1370 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1373 goto error_snapshot_output
;
1376 if (output
.control_uri
) {
1377 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1380 goto error_snapshot_output
;
1384 if (output
.data_uri
) {
1385 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1388 goto error_snapshot_output
;
1393 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1394 error_snapshot_output
:
1397 free(output
.control_uri
);
1398 free(output
.data_uri
);
1399 lttng_snapshot_output_destroy(snapshot_output
);
1409 int create_session(const char *name
,
1410 struct lttng_domain
*kernel_domain
,
1411 struct lttng_domain
*ust_domain
,
1412 struct lttng_domain
*jul_domain
,
1413 struct lttng_domain
*log4j_domain
,
1414 xmlNodePtr output_node
,
1415 uint64_t live_timer_interval
)
1418 struct consumer_output output
= { 0 };
1419 xmlNodePtr consumer_output_node
;
1424 consumer_output_node
= xmlFirstElementChild(output_node
);
1425 if (!consumer_output_node
) {
1426 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1430 if (strcmp((const char *) consumer_output_node
->name
,
1431 config_element_consumer_output
)) {
1432 WARN("Invalid output type, expected %s node",
1433 config_element_consumer_output
);
1434 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1438 ret
= process_consumer_output(consumer_output_node
, &output
);
1444 if (live_timer_interval
!= UINT64_MAX
&&
1445 !output
.control_uri
&& !output
.data_uri
) {
1446 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1450 if (output
.control_uri
|| output
.data_uri
) {
1451 /* network destination */
1452 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1454 * URLs are provided for sure since the test above make sure that
1455 * with a live timer the data and control URIs are provided. So,
1456 * NULL is passed here and will be set right after.
1458 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1460 ret
= lttng_create_session(name
, NULL
);
1466 ret
= create_session_net_output(name
, output
.control_uri
,
1473 /* either local output or no output */
1474 ret
= lttng_create_session(name
, output
.path
);
1481 free(output
.control_uri
);
1482 free(output
.data_uri
);
1486 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1487 struct lttng_event_probe_attr
*attr
)
1491 assert(probe_attribute_node
);
1494 if (!strcmp((const char *) probe_attribute_node
->name
,
1495 config_element_address
)) {
1500 content
= xmlNodeGetContent(probe_attribute_node
);
1502 ret
= -LTTNG_ERR_NOMEM
;
1506 ret
= parse_uint(content
, &addr
);
1509 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1514 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1515 config_element_offset
)) {
1517 uint64_t offset
= 0;
1520 content
= xmlNodeGetContent(probe_attribute_node
);
1522 ret
= -LTTNG_ERR_NOMEM
;
1526 ret
= parse_uint(content
, &offset
);
1529 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1533 attr
->offset
= offset
;
1534 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1535 config_element_symbol_name
)) {
1540 content
= xmlNodeGetContent(probe_attribute_node
);
1542 ret
= -LTTNG_ERR_NOMEM
;
1546 name_len
= strlen((char *) content
);
1547 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1548 WARN("symbol_name too long.");
1549 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1554 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1563 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1564 const char *channel_name
, const enum process_event_node_phase phase
)
1568 struct lttng_event event
;
1569 char **exclusions
= NULL
;
1570 unsigned long exclusion_count
= 0;
1571 char *filter_expression
= NULL
;
1575 assert(channel_name
);
1577 memset(&event
, 0, sizeof(event
));
1579 /* Initialize default log level which varies by domain */
1580 switch (handle
->domain
.type
)
1582 case LTTNG_DOMAIN_JUL
:
1583 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1585 case LTTNG_DOMAIN_LOG4J
:
1586 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1588 case LTTNG_DOMAIN_PYTHON
:
1589 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1591 case LTTNG_DOMAIN_UST
:
1592 case LTTNG_DOMAIN_KERNEL
:
1593 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1599 for (node
= xmlFirstElementChild(event_node
); node
;
1600 node
= xmlNextElementSibling(node
)) {
1601 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1606 content
= xmlNodeGetContent(node
);
1608 ret
= -LTTNG_ERR_NOMEM
;
1612 name_len
= strlen((char *) content
);
1613 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1614 WARN("Channel name too long.");
1615 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1620 strncpy(event
.name
, (const char *) content
, name_len
);
1622 } else if (!strcmp((const char *) node
->name
,
1623 config_element_enabled
)) {
1624 xmlChar
*content
= xmlNodeGetContent(node
);
1628 ret
= -LTTNG_ERR_NOMEM
;
1632 ret
= parse_bool(content
, &event
.enabled
);
1635 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1638 } else if (!strcmp((const char *) node
->name
,
1639 config_element_type
)) {
1640 xmlChar
*content
= xmlNodeGetContent(node
);
1644 ret
= -LTTNG_ERR_NOMEM
;
1648 ret
= config_get_event_type((char *) content
);
1651 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1656 } else if (!strcmp((const char *) node
->name
,
1657 config_element_loglevel_type
)) {
1658 xmlChar
*content
= xmlNodeGetContent(node
);
1662 ret
= -LTTNG_ERR_NOMEM
;
1666 ret
= get_loglevel_type(content
);
1669 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1673 event
.loglevel_type
= ret
;
1674 } else if (!strcmp((const char *) node
->name
,
1675 config_element_loglevel
)) {
1677 int64_t loglevel
= 0;
1680 content
= xmlNodeGetContent(node
);
1682 ret
= -LTTNG_ERR_NOMEM
;
1686 ret
= parse_int(content
, &loglevel
);
1689 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1693 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1694 WARN("loglevel out of range.");
1695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1699 event
.loglevel
= loglevel
;
1700 } else if (!strcmp((const char *) node
->name
,
1701 config_element_filter
)) {
1703 xmlNodeGetContent(node
);
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 free(filter_expression
);
1712 filter_expression
= strdup((char *) content
);
1714 if (!filter_expression
) {
1715 ret
= -LTTNG_ERR_NOMEM
;
1718 } else if (!strcmp((const char *) node
->name
,
1719 config_element_exclusions
)) {
1720 xmlNodePtr exclusion_node
;
1721 int exclusion_index
= 0;
1726 * Exclusions has already been initialized,
1729 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1733 exclusion_count
= xmlChildElementCount(node
);
1734 if (!exclusion_count
) {
1738 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1740 exclusion_count
= 0;
1741 ret
= -LTTNG_ERR_NOMEM
;
1745 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1746 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1748 xmlNodeGetContent(exclusion_node
);
1751 ret
= -LTTNG_ERR_NOMEM
;
1755 exclusions
[exclusion_index
] = strdup((const char *) content
);
1757 if (!exclusions
[exclusion_index
]) {
1758 ret
= -LTTNG_ERR_NOMEM
;
1764 event
.exclusion
= 1;
1765 } else if (!strcmp((const char *) node
->name
,
1766 config_element_attributes
)) {
1767 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1770 if (!attribute_node
) {
1771 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1775 if (!strcmp((const char *) node
->name
,
1776 config_element_probe_attributes
)) {
1777 xmlNodePtr probe_attribute_node
;
1779 /* probe_attributes */
1780 for (probe_attribute_node
=
1781 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1782 probe_attribute_node
= xmlNextElementSibling(
1783 probe_attribute_node
)) {
1785 ret
= process_probe_attribute_node(probe_attribute_node
,
1794 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1796 /* function_attributes */
1797 content
= xmlNodeGetContent(symbol_node
);
1799 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1803 sym_len
= strlen((char *) content
);
1804 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1805 WARN("Function name too long.");
1806 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1811 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1818 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1819 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1820 filter_expression
, exclusion_count
, exclusions
);
1822 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1828 for (i
= 0; i
< exclusion_count
; i
++) {
1829 free(exclusions
[i
]);
1833 free(filter_expression
);
1838 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1839 const char *channel_name
)
1842 struct lttng_event event
;
1845 assert(events_node
);
1847 assert(channel_name
);
1849 for (node
= xmlFirstElementChild(events_node
); node
;
1850 node
= xmlNextElementSibling(node
)) {
1851 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1858 * Disable all events to enable only the necessary events.
1859 * Limitations regarding lttng_disable_events and tuple descriptor
1860 * force this approach.
1862 memset(&event
, 0, sizeof(event
));
1863 event
.loglevel
= -1;
1864 event
.type
= LTTNG_EVENT_ALL
;
1865 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1870 for (node
= xmlFirstElementChild(events_node
); node
;
1871 node
= xmlNextElementSibling(node
)) {
1872 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1883 int process_channel_attr_node(xmlNodePtr attr_node
, bool snapshot_mode
,
1884 enum lttng_domain_type domain
, struct lttng_channel
*channel
,
1885 xmlNodePtr
*contexts_node
, xmlNodePtr
*events_node
)
1891 assert(contexts_node
);
1892 assert(events_node
);
1894 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1899 content
= xmlNodeGetContent(attr_node
);
1901 ret
= -LTTNG_ERR_NOMEM
;
1905 name_len
= strlen((char *) content
);
1906 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1907 WARN("Channel name too long.");
1908 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1913 strncpy(channel
->name
, (const char *) content
, name_len
);
1915 } else if (!strcmp((const char *) attr_node
->name
,
1916 config_element_enabled
)) {
1921 content
= xmlNodeGetContent(attr_node
);
1923 ret
= -LTTNG_ERR_NOMEM
;
1927 ret
= parse_bool(content
, &enabled
);
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1934 channel
->enabled
= enabled
;
1935 } else if (!strcmp((const char *) attr_node
->name
,
1936 config_element_overwrite_mode
)) {
1939 /* overwrite_mode */
1940 content
= xmlNodeGetContent(attr_node
);
1942 ret
= -LTTNG_ERR_NOMEM
;
1946 ret
= get_overwrite_mode(content
);
1949 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1953 channel
->attr
.overwrite
= ret
;
1954 } else if (!strcmp((const char *) attr_node
->name
,
1955 config_element_subbuf_size
)) {
1958 /* subbuffer_size */
1959 content
= xmlNodeGetContent(attr_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1971 } else if (!strcmp((const char *) attr_node
->name
,
1972 config_element_num_subbuf
)) {
1975 /* subbuffer_count */
1976 content
= xmlNodeGetContent(attr_node
);
1978 ret
= -LTTNG_ERR_NOMEM
;
1982 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1985 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 } else if (!strcmp((const char *) attr_node
->name
,
1989 config_element_switch_timer_interval
)) {
1991 uint64_t switch_timer_interval
= 0;
1993 /* switch_timer_interval */
1994 content
= xmlNodeGetContent(attr_node
);
1996 ret
= -LTTNG_ERR_NOMEM
;
2000 ret
= parse_uint(content
, &switch_timer_interval
);
2003 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2007 if (switch_timer_interval
> UINT_MAX
) {
2008 WARN("switch_timer_interval out of range.");
2009 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 channel
->attr
.switch_timer_interval
=
2014 switch_timer_interval
;
2015 } else if (!strcmp((const char *) attr_node
->name
,
2016 config_element_read_timer_interval
)) {
2018 uint64_t read_timer_interval
= 0;
2020 /* read_timer_interval */
2021 content
= xmlNodeGetContent(attr_node
);
2023 ret
= -LTTNG_ERR_NOMEM
;
2027 ret
= parse_uint(content
, &read_timer_interval
);
2030 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2034 if (read_timer_interval
> UINT_MAX
) {
2035 WARN("read_timer_interval out of range.");
2036 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2040 channel
->attr
.read_timer_interval
=
2041 read_timer_interval
;
2042 } else if (!strcmp((const char *) attr_node
->name
,
2043 config_element_output_type
)) {
2047 content
= xmlNodeGetContent(attr_node
);
2049 ret
= -LTTNG_ERR_NOMEM
;
2053 ret
= get_output_type(content
);
2056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2060 channel
->attr
.output
= ret
;
2061 } else if (!strcmp((const char *) attr_node
->name
,
2062 config_element_tracefile_size
)) {
2065 /* tracefile_size */
2066 content
= xmlNodeGetContent(attr_node
);
2068 ret
= -LTTNG_ERR_NOMEM
;
2072 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2075 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2078 } else if (!strcmp((const char *) attr_node
->name
,
2079 config_element_tracefile_count
)) {
2082 /* tracefile_count */
2083 content
= xmlNodeGetContent(attr_node
);
2085 ret
= -LTTNG_ERR_NOMEM
;
2089 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2092 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2095 } else if (!strcmp((const char *) attr_node
->name
,
2096 config_element_live_timer_interval
)) {
2098 uint64_t live_timer_interval
= 0;
2100 /* live_timer_interval */
2101 content
= xmlNodeGetContent(attr_node
);
2103 ret
= -LTTNG_ERR_NOMEM
;
2107 ret
= parse_uint(content
, &live_timer_interval
);
2110 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2114 if (live_timer_interval
> UINT_MAX
) {
2115 WARN("live_timer_interval out of range.");
2116 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2120 channel
->attr
.live_timer_interval
=
2121 live_timer_interval
;
2122 } else if (!strcmp((const char *) attr_node
->name
,
2123 config_element_events
)) {
2125 *events_node
= attr_node
;
2128 *contexts_node
= attr_node
;
2137 int process_context_node(xmlNodePtr context_node
,
2138 struct lttng_handle
*handle
, const char *channel_name
)
2141 struct lttng_event_context context
;
2142 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2145 assert(channel_name
);
2147 if (!context_child_node
) {
2148 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2152 memset(&context
, 0, sizeof(context
));
2154 if (!strcmp((const char *) context_child_node
->name
,
2155 config_element_type
)) {
2157 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2160 ret
= -LTTNG_ERR_NOMEM
;
2164 ret
= get_context_type(content
);
2167 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2172 } else if (!strcmp((const char *) context_child_node
->name
,
2173 config_element_context_perf
)) {
2175 xmlNodePtr perf_attr_node
;
2177 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2178 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2179 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2180 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2181 perf_attr_node
; perf_attr_node
=
2182 xmlNextElementSibling(perf_attr_node
)) {
2183 if (!strcmp((const char *) perf_attr_node
->name
,
2184 config_element_type
)) {
2189 content
= xmlNodeGetContent(perf_attr_node
);
2191 ret
= -LTTNG_ERR_NOMEM
;
2195 ret
= parse_uint(content
, &type
);
2198 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2202 if (type
> UINT32_MAX
) {
2203 WARN("perf context type out of range.");
2204 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2208 context
.u
.perf_counter
.type
= type
;
2209 } else if (!strcmp((const char *) perf_attr_node
->name
,
2210 config_element_config
)) {
2212 uint64_t config
= 0;
2215 content
= xmlNodeGetContent(perf_attr_node
);
2217 ret
= -LTTNG_ERR_NOMEM
;
2221 ret
= parse_uint(content
, &config
);
2224 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2228 context
.u
.perf_counter
.config
= config
;
2229 } else if (!strcmp((const char *) perf_attr_node
->name
,
2230 config_element_name
)) {
2235 content
= xmlNodeGetContent(perf_attr_node
);
2237 ret
= -LTTNG_ERR_NOMEM
;
2241 name_len
= strlen((char *) content
);
2242 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2243 WARN("perf context name too long.");
2244 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2249 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2254 } else if (!strcmp((const char *) context_child_node
->name
,
2255 config_element_context_app
)) {
2256 /* application context */
2257 xmlNodePtr app_ctx_node
;
2259 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2260 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2261 app_ctx_node
; app_ctx_node
=
2262 xmlNextElementSibling(app_ctx_node
)) {
2264 char **target
= strcmp(
2265 (const char *) app_ctx_node
->name
,
2266 config_element_context_app_provider_name
) == 0 ?
2267 &context
.u
.app_ctx
.provider_name
:
2268 &context
.u
.app_ctx
.ctx_name
;
2270 content
= xmlNodeGetContent(app_ctx_node
);
2272 ret
= -LTTNG_ERR_NOMEM
;
2276 *target
= (char *) content
;
2279 /* Unrecognized context type */
2280 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2284 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2285 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2286 free(context
.u
.app_ctx
.provider_name
);
2287 free(context
.u
.app_ctx
.ctx_name
);
2294 int process_contexts_node(xmlNodePtr contexts_node
,
2295 struct lttng_handle
*handle
, const char *channel_name
)
2298 xmlNodePtr context_node
;
2300 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2301 context_node
= xmlNextElementSibling(context_node
)) {
2302 ret
= process_context_node(context_node
, handle
, channel_name
);
2312 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2313 struct lttng_handle
*handle
)
2316 xmlNodePtr targets_node
= NULL
;
2320 assert(pid_tracker_node
);
2321 /* get the targets node */
2322 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2323 node
= xmlNextElementSibling(node
)) {
2324 if (!strcmp((const char *) node
->name
,
2325 config_element_targets
)) {
2326 targets_node
= node
;
2331 if (!targets_node
) {
2332 ret
= LTTNG_ERR_INVALID
;
2336 /* Go through all pid_target node */
2337 child
= xmlChildElementCount(targets_node
);
2339 /* The session is explicitly set to target nothing. */
2340 ret
= lttng_untrack_pid(handle
, -1);
2345 for (node
= xmlFirstElementChild(targets_node
); node
;
2346 node
= xmlNextElementSibling(node
)) {
2347 xmlNodePtr pid_target_node
= node
;
2349 /* get pid node and track it */
2350 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2351 node
= xmlNextElementSibling(node
)) {
2352 if (!strcmp((const char *) node
->name
,
2353 config_element_pid
)) {
2355 xmlChar
*content
= NULL
;
2357 content
= xmlNodeGetContent(node
);
2359 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2363 ret
= parse_int(content
, &pid
);
2366 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2370 ret
= lttng_track_pid(handle
, (int) pid
);
2376 node
= pid_target_node
;
2385 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
,
2389 struct lttng_domain domain
= { 0 };
2390 struct lttng_handle
*handle
= NULL
;
2391 xmlNodePtr channels_node
= NULL
;
2392 xmlNodePtr trackers_node
= NULL
;
2393 xmlNodePtr pid_tracker_node
= NULL
;
2396 assert(session_name
);
2398 ret
= init_domain(domain_node
, &domain
);
2403 handle
= lttng_create_handle(session_name
, &domain
);
2405 ret
= -LTTNG_ERR_NOMEM
;
2409 /* get the channels node */
2410 for (node
= xmlFirstElementChild(domain_node
); node
;
2411 node
= xmlNextElementSibling(node
)) {
2412 if (!strcmp((const char *) node
->name
,
2413 config_element_channels
)) {
2414 channels_node
= node
;
2419 if (!channels_node
) {
2423 /* create all channels */
2424 for (node
= xmlFirstElementChild(channels_node
); node
;
2425 node
= xmlNextElementSibling(node
)) {
2426 struct lttng_channel channel
;
2427 xmlNodePtr contexts_node
= NULL
;
2428 xmlNodePtr events_node
= NULL
;
2429 xmlNodePtr channel_attr_node
;
2431 memset(&channel
, 0, sizeof(channel
));
2432 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2434 for (channel_attr_node
= xmlFirstElementChild(node
);
2435 channel_attr_node
; channel_attr_node
=
2436 xmlNextElementSibling(channel_attr_node
)) {
2437 ret
= process_channel_attr_node(channel_attr_node
,
2438 snapshot_mode
, domain
.type
, &channel
,
2439 &contexts_node
, &events_node
);
2445 if (!channel
.name
) {
2446 ERR("Encountered a channel with no name attribute.");
2447 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2451 ret
= lttng_enable_channel(handle
, &channel
);
2456 ret
= process_events_node(events_node
, handle
, channel
.name
);
2461 ret
= process_contexts_node(contexts_node
, handle
,
2468 /* get the trackers node */
2469 for (node
= xmlFirstElementChild(domain_node
); node
;
2470 node
= xmlNextElementSibling(node
)) {
2471 if (!strcmp((const char *) node
->name
,
2472 config_element_trackers
)) {
2473 trackers_node
= node
;
2478 if (!trackers_node
) {
2482 for (node
= xmlFirstElementChild(trackers_node
); node
;
2483 node
= xmlNextElementSibling(node
)) {
2484 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2485 pid_tracker_node
= node
;
2486 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2493 if (!pid_tracker_node
) {
2494 lttng_track_pid(handle
, -1);
2498 lttng_destroy_handle(handle
);
2503 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2506 int ret
, started
= -1, snapshot_mode
= -1;
2507 uint64_t live_timer_interval
= UINT64_MAX
;
2508 xmlChar
*name
= NULL
;
2509 xmlChar
*shm_path
= NULL
;
2510 xmlNodePtr domains_node
= NULL
;
2511 xmlNodePtr output_node
= NULL
;
2513 struct lttng_domain
*kernel_domain
= NULL
;
2514 struct lttng_domain
*ust_domain
= NULL
;
2515 struct lttng_domain
*jul_domain
= NULL
;
2516 struct lttng_domain
*log4j_domain
= NULL
;
2517 struct lttng_domain
*python_domain
= NULL
;
2519 for (node
= xmlFirstElementChild(session_node
); node
;
2520 node
= xmlNextElementSibling(node
)) {
2521 if (!name
&& !strcmp((const char *) node
->name
,
2522 config_element_name
)) {
2524 xmlChar
*node_content
= xmlNodeGetContent(node
);
2525 if (!node_content
) {
2526 ret
= -LTTNG_ERR_NOMEM
;
2530 name
= node_content
;
2531 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2532 config_element_domains
)) {
2534 domains_node
= node
;
2535 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2536 config_element_started
)) {
2538 xmlChar
*node_content
= xmlNodeGetContent(node
);
2539 if (!node_content
) {
2540 ret
= -LTTNG_ERR_NOMEM
;
2544 ret
= parse_bool(node_content
, &started
);
2547 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2550 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2551 config_element_output
)) {
2554 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2555 config_element_shared_memory_path
)) {
2556 /* shared memory path */
2557 xmlChar
*node_content
= xmlNodeGetContent(node
);
2558 if (!node_content
) {
2559 ret
= -LTTNG_ERR_NOMEM
;
2563 shm_path
= node_content
;
2565 /* attributes, snapshot_mode or live_timer_interval */
2566 xmlNodePtr attributes_child
=
2567 xmlFirstElementChild(node
);
2569 if (!strcmp((const char *) attributes_child
->name
,
2570 config_element_snapshot_mode
)) {
2572 xmlChar
*snapshot_mode_content
=
2573 xmlNodeGetContent(attributes_child
);
2574 if (!snapshot_mode_content
) {
2575 ret
= -LTTNG_ERR_NOMEM
;
2579 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2580 free(snapshot_mode_content
);
2582 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2586 /* live_timer_interval */
2587 xmlChar
*timer_interval_content
=
2588 xmlNodeGetContent(attributes_child
);
2589 if (!timer_interval_content
) {
2590 ret
= -LTTNG_ERR_NOMEM
;
2594 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2595 free(timer_interval_content
);
2597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2605 /* Mandatory attribute, as defined in the session XSD */
2606 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2610 if (session_name
&& strcmp((char *) name
, session_name
)) {
2611 /* This is not the session we are looking for */
2612 ret
= -LTTNG_ERR_NO_SESSION
;
2616 /* Init domains to create the session handles */
2617 for (node
= xmlFirstElementChild(domains_node
); node
;
2618 node
= xmlNextElementSibling(node
)) {
2619 struct lttng_domain
*domain
;
2621 domain
= zmalloc(sizeof(*domain
));
2623 ret
= -LTTNG_ERR_NOMEM
;
2627 ret
= init_domain(node
, domain
);
2629 goto domain_init_error
;
2632 switch (domain
->type
) {
2633 case LTTNG_DOMAIN_KERNEL
:
2634 if (kernel_domain
) {
2635 /* Same domain seen twice, invalid! */
2636 goto domain_init_error
;
2638 kernel_domain
= domain
;
2640 case LTTNG_DOMAIN_UST
:
2642 /* Same domain seen twice, invalid! */
2643 goto domain_init_error
;
2645 ust_domain
= domain
;
2647 case LTTNG_DOMAIN_JUL
:
2649 /* Same domain seen twice, invalid! */
2650 goto domain_init_error
;
2652 jul_domain
= domain
;
2654 case LTTNG_DOMAIN_LOG4J
:
2656 /* Same domain seen twice, invalid! */
2657 goto domain_init_error
;
2659 log4j_domain
= domain
;
2661 case LTTNG_DOMAIN_PYTHON
:
2662 if (python_domain
) {
2663 /* Same domain seen twice, invalid! */
2664 goto domain_init_error
;
2666 python_domain
= domain
;
2669 WARN("Invalid domain type");
2670 goto domain_init_error
;
2675 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2680 /* Destroy session if it exists */
2681 ret
= lttng_destroy_session((const char *) name
);
2682 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2683 ERR("Failed to destroy existing session.");
2688 /* Create session type depending on output type */
2689 if (snapshot_mode
&& snapshot_mode
!= -1) {
2690 ret
= create_snapshot_session((const char *) name
, output_node
);
2691 } else if (live_timer_interval
&&
2692 live_timer_interval
!= UINT64_MAX
) {
2693 ret
= create_session((const char *) name
, kernel_domain
,
2694 ust_domain
, jul_domain
, log4j_domain
,
2695 output_node
, live_timer_interval
);
2697 /* regular session */
2698 ret
= create_session((const char *) name
, kernel_domain
,
2699 ust_domain
, jul_domain
, log4j_domain
,
2700 output_node
, UINT64_MAX
);
2707 ret
= lttng_set_session_shm_path((const char *) name
,
2708 (const char *) shm_path
);
2714 for (node
= xmlFirstElementChild(domains_node
); node
;
2715 node
= xmlNextElementSibling(node
)) {
2716 ret
= process_domain_node(node
, (const char *) name
,
2717 snapshot_mode
== 1);
2724 ret
= lttng_start_tracing((const char *) name
);
2732 ERR("Failed to load session %s: %s", (const char *) name
,
2733 lttng_strerror(ret
));
2734 lttng_destroy_session((const char *) name
);
2738 free(kernel_domain
);
2742 free(python_domain
);
2749 * Return 1 if the given path is readable by the current UID or 0 if not.
2750 * Return -1 if the path is EPERM.
2752 static int validate_file_read_creds(const char *path
)
2758 /* Can we read the file. */
2759 ret
= access(path
, R_OK
);
2763 if (errno
== EACCES
) {
2774 int load_session_from_file(const char *path
, const char *session_name
,
2775 struct session_config_validation_ctx
*validation_ctx
, int override
)
2777 int ret
, session_found
= !session_name
;
2778 xmlDocPtr doc
= NULL
;
2779 xmlNodePtr sessions_node
;
2780 xmlNodePtr session_node
;
2783 assert(validation_ctx
);
2785 ret
= validate_file_read_creds(path
);
2788 ret
= -LTTNG_ERR_EPERM
;
2790 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2795 doc
= xmlParseFile(path
);
2797 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2801 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2803 ERR("Session configuration file validation failed");
2804 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2808 sessions_node
= xmlDocGetRootElement(doc
);
2809 if (!sessions_node
) {
2813 for (session_node
= xmlFirstElementChild(sessions_node
);
2814 session_node
; session_node
=
2815 xmlNextElementSibling(session_node
)) {
2816 ret
= process_session_node(session_node
,
2817 session_name
, override
);
2818 if (session_name
&& ret
== 0) {
2819 /* Target session found and loaded */
2827 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2833 int load_session_from_document(struct config_document
*document
, const char *session_name
,
2834 struct session_config_validation_ctx
*validation_ctx
, int override
)
2836 int ret
, session_found
= !session_name
;
2837 xmlNodePtr sessions_node
;
2838 xmlNodePtr session_node
;
2840 assert(validation_ctx
);
2842 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, document
->document
);
2844 ERR("Session configuration file validation failed");
2845 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2849 sessions_node
= xmlDocGetRootElement(document
->document
);
2850 if (!sessions_node
) {
2854 for (session_node
= xmlFirstElementChild(sessions_node
);
2855 session_node
; session_node
=
2856 xmlNextElementSibling(session_node
)) {
2857 ret
= process_session_node(session_node
,
2858 session_name
, override
);
2859 if (session_name
&& ret
== 0) {
2860 /* Target session found and loaded */
2867 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2872 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2874 struct dirent
*alloc_dirent(const char *path
)
2878 struct dirent
*entry
;
2880 name_max
= pathconf(path
, _PC_NAME_MAX
);
2881 if (name_max
== -1) {
2882 name_max
= PATH_MAX
;
2884 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2885 entry
= zmalloc(len
);
2890 int load_session_from_path(const char *path
, const char *session_name
,
2891 struct session_config_validation_ctx
*validation_ctx
, int override
)
2893 int ret
, session_found
= !session_name
;
2894 DIR *directory
= NULL
;
2897 assert(validation_ctx
);
2899 directory
= opendir(path
);
2903 /* Try the file loading. */
2906 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2909 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2914 struct dirent
*entry
;
2915 struct dirent
*result
;
2916 char *file_path
= NULL
;
2917 size_t path_len
= strlen(path
);
2919 if (path_len
>= PATH_MAX
) {
2920 ret
= -LTTNG_ERR_INVALID
;
2924 entry
= alloc_dirent(path
);
2926 ret
= -LTTNG_ERR_NOMEM
;
2930 file_path
= zmalloc(PATH_MAX
);
2932 ret
= -LTTNG_ERR_NOMEM
;
2937 strncpy(file_path
, path
, path_len
);
2938 if (file_path
[path_len
- 1] != '/') {
2939 file_path
[path_len
++] = '/';
2943 /* Search for *.lttng files */
2944 while (!readdir_r(directory
, entry
, &result
) && result
) {
2945 size_t file_name_len
= strlen(result
->d_name
);
2947 if (file_name_len
<=
2948 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2952 if (path_len
+ file_name_len
>= PATH_MAX
) {
2956 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2957 result
->d_name
+ file_name_len
- sizeof(
2958 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2962 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2963 file_path
[path_len
+ file_name_len
] = '\0';
2965 ret
= load_session_from_file(file_path
, session_name
,
2966 validation_ctx
, override
);
2967 if (session_name
&& !ret
) {
2976 ret
= load_session_from_file(path
, session_name
,
2977 validation_ctx
, override
);
2987 if (closedir(directory
)) {
2992 if (session_found
) {
3000 * Validate that the given path's credentials and the current process have the
3001 * same UID. If so, return 1 else return 0 if it does NOT match.
3003 static int validate_path_creds(const char *path
)
3005 int ret
, uid
= getuid();
3014 ret
= stat(path
, &buf
);
3016 if (errno
!= ENOENT
) {
3019 ret
= -LTTNG_ERR_INVALID
;
3023 if (buf
.st_uid
!= uid
) {
3034 int config_load_session(const char *path
, const char *session_name
,
3035 int override
, unsigned int autoload
)
3038 bool session_loaded
= false;
3039 const char *path_ptr
= NULL
;
3040 struct session_config_validation_ctx validation_ctx
= { 0 };
3042 ret
= init_session_config_validation_ctx(&validation_ctx
);
3049 const char *sys_path
;
3052 home_path
= utils_get_home_dir();
3054 char path
[PATH_MAX
];
3057 * Try user session configuration path. Ignore error here so we can
3058 * continue loading the system wide sessions.
3061 ret
= snprintf(path
, sizeof(path
),
3062 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3063 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3065 PERROR("snprintf session autoload home config path");
3070 * Credentials are only validated for the autoload in order to
3071 * avoid any user session daemon to try to load kernel sessions
3072 * automatically and failing all the times.
3074 ret
= validate_path_creds(path
);
3079 ret
= snprintf(path
, sizeof(path
),
3080 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3082 PERROR("snprintf session home config path");
3088 ret
= load_session_from_path(path_ptr
, session_name
,
3089 &validation_ctx
, override
);
3090 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3094 * Continue even if the session was found since we have to try
3095 * the system wide sessions.
3097 session_loaded
= true;
3101 /* Reset path pointer for the system wide dir. */
3104 /* Try system wide configuration directory. */
3106 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3107 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3108 ret
= validate_path_creds(sys_path
);
3110 path_ptr
= sys_path
;
3113 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3114 path_ptr
= sys_path
;
3118 ret
= load_session_from_path(path_ptr
, session_name
,
3119 &validation_ctx
, override
);
3121 session_loaded
= true;
3125 ret
= access(path
, F_OK
);
3130 ret
= -LTTNG_ERR_INVALID
;
3131 WARN("Session configuration path does not exist.");
3134 ret
= -LTTNG_ERR_EPERM
;
3137 ret
= -LTTNG_ERR_UNK
;
3143 ret
= load_session_from_path(path
, session_name
,
3144 &validation_ctx
, override
);
3147 fini_session_config_validation_ctx(&validation_ctx
);
3148 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3150 * Don't report an error if no sessions are found when called
3151 * without a session_name or a search path.
3156 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3157 /* A matching session was found in one of the search paths. */
3164 void __attribute__((destructor
)) session_config_exit(void)
3170 struct config_document
*config_document_get(const char *path
)
3173 struct config_document
*document
= NULL
;
3174 struct session_config_validation_ctx validation_ctx
= { 0 };
3178 ret
= access(path
, F_OK
);
3183 ERR("Configuration document path does not exist.");
3186 ERR("Configuration document permission denied.");
3189 ERR("Configuration document unknown error.");
3195 ret
= init_session_config_validation_ctx(&validation_ctx
);
3200 ret
= validate_file_read_creds(path
);
3203 ERR("Configuration document permission denied.");
3205 ERR("Configuration document does not exist.");
3210 document
= zmalloc(sizeof(struct config_document
));
3216 document
->document
= xmlParseFile(path
);
3217 if (!document
->document
) {
3218 ERR("Configuration document parsing failed.");
3222 ret
= xmlSchemaValidateDoc(validation_ctx
.schema_validation_ctx
,
3223 document
->document
);
3225 ERR("Session configuration file validation failed");
3230 fini_session_config_validation_ctx(&validation_ctx
);
3233 xmlFreeDoc(document
->document
);
3239 void config_document_free(struct config_document
*document
)
3242 xmlFreeDoc(document
->document
);
3243 document
->document
= NULL
;
3250 int config_document_replace_element_value(struct config_document
*document
,
3251 const char *xpath
, const char *value
)
3254 int xpath_result_size
;
3256 xmlXPathContextPtr xpath_context
= NULL
;
3257 xmlXPathObjectPtr xpath_object
= NULL
;
3258 xmlNodeSetPtr xpath_result_set
= NULL
;
3259 xmlChar
*internal_xpath
= NULL
;
3260 xmlChar
*internal_value
= NULL
;
3266 internal_xpath
= encode_string(xpath
);
3267 if (!internal_xpath
) {
3268 ret
= -LTTNG_ERR_NOMEM
;
3269 ERR("Unable to encode xpath string");
3273 internal_value
= encode_string(value
);
3274 if (!internal_value
) {
3275 ret
= -LTTNG_ERR_NOMEM
;
3276 ERR("Unable to encode xpath replace value string");
3280 /* Initialize xpath context */
3281 xpath_context
= xmlXPathNewContext(document
->document
);
3282 if (!xpath_context
) {
3283 ret
= -LTTNG_ERR_NOMEM
;
3284 ERR("Unable to create xpath context");
3288 /* Evaluate de xpath expression */
3289 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3290 if (!xpath_object
) {
3291 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3295 /* TODO: from here could be extracted and previopus could be a step in
3296 * subsequent operation, modify/substitute/delete node.
3298 xpath_result_set
= xpath_object
->nodesetval
;
3299 if (!xpath_result_set
) {
3300 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3304 xpath_result_size
= xpath_result_set
->nodeNr
;
3307 * Reverse traversal since last element could be nested under parent
3308 * element present in the result set.
3310 for (int i
= xpath_result_size
- 1; i
>=0; i
--) {
3311 assert(xpath_result_set
->nodeTab
[i
]);
3312 xmlNodeSetContent(xpath_result_set
->nodeTab
[i
], internal_value
);
3315 * Libxml2 quirk regarding the freing and namesplace node see
3316 * libxml2 example and documentation for more details.
3318 if (xpath_result_set
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) {
3319 xpath_result_set
->nodeTab
[i
] = NULL
;
3324 xmlXPathFreeContext(xpath_context
);
3325 xmlXPathFreeObject(xpath_object
);
3326 xmlFree(internal_xpath
);
3327 xmlFree(internal_value
);
3332 int config_load_configuration_sessions(struct config_document
*document
,
3333 const char *session_name
, int override
)
3336 struct session_config_validation_ctx validation_ctx
= { 0 };
3338 ret
= init_session_config_validation_ctx(&validation_ctx
);
3342 ret
= load_session_from_document(document
, session_name
,
3343 &validation_ctx
, override
);
3345 fini_session_config_validation_ctx(&validation_ctx
);
3350 int config_document_replace_element(struct config_document
*document
,
3351 const char *xpath
, const struct config_element
*element
)
3354 int xpath_result_size
;
3356 xmlXPathContextPtr xpath_context
= NULL
;
3357 xmlXPathObjectPtr xpath_object
= NULL
;
3358 xmlNodeSetPtr xpath_result_set
= NULL
;
3359 xmlChar
*internal_xpath
= NULL
;
3360 xmlNodePtr old_node
= NULL
;
3361 xmlNodePtr copy
= NULL
;
3366 assert(element
->element
);
3368 internal_xpath
= encode_string(xpath
);
3369 if (!internal_xpath
) {
3370 ret
= -LTTNG_ERR_NOMEM
;
3371 ERR("Unable to encode xpath string");
3375 /* Initialize xpath context */
3376 xpath_context
= xmlXPathNewContext(document
->document
);
3377 if (!xpath_context
) {
3378 ret
= -LTTNG_ERR_NOMEM
;
3379 ERR("Unable to create xpath context");
3383 /* Evaluate the xpath expression */
3384 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3385 if (!xpath_object
) {
3386 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3390 xpath_result_set
= xpath_object
->nodesetval
;
3391 if (!xpath_result_set
) {
3392 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3396 xpath_result_size
= xpath_result_set
->nodeNr
;
3398 if (xpath_result_size
> 1) {
3399 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3403 if (xpath_result_size
== 0) {
3404 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3408 assert(xpath_result_set
->nodeTab
[0]);
3410 /* Do a copy of the element to ease caller memory management */
3411 copy
= xmlCopyNode(element
->element
, 1);
3413 ret
= -LTTNG_ERR_NOMEM
;
3414 ERR("Copy failed for node replacement");
3418 old_node
= xmlReplaceNode(xpath_result_set
->nodeTab
[0], copy
);
3419 if (xpath_result_set
->nodeTab
[0]->type
!= XML_NAMESPACE_DECL
)
3420 xpath_result_set
->nodeTab
[0] = NULL
;
3422 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3426 xmlUnlinkNode(old_node
);
3427 xmlFreeNode(old_node
);
3429 xmlXPathFreeContext(xpath_context
);
3430 xmlXPathFreeObject(xpath_object
);
3431 xmlFree(internal_xpath
);
3436 char *config_document_get_element_value(struct config_document
*document
,
3440 int xpath_result_size
;
3441 int value_result_size
;
3443 xmlXPathContextPtr xpath_context
= NULL
;
3444 xmlXPathObjectPtr xpath_object
= NULL
;
3445 xmlNodeSetPtr xpath_result_set
= NULL
;
3446 xmlChar
*internal_xpath
= NULL
;
3447 xmlChar
*internal_value
= NULL
;
3452 internal_xpath
= encode_string(xpath
);
3453 if (!internal_xpath
) {
3455 ERR("Unable to encode xpath string");
3459 /* Initialize xpath context */
3460 xpath_context
= xmlXPathNewContext(document
->document
);
3461 if (!xpath_context
) {
3463 ERR("Unable to create xpath context");
3467 /* Evaluate the xpath expression */
3468 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3469 if (!xpath_object
) {
3471 ERR("Unable to evaluate xpath query (invalid query format)");
3475 xpath_result_set
= xpath_object
->nodesetval
;
3476 if (!xpath_result_set
) {
3481 xpath_result_size
= xpath_result_set
->nodeNr
;
3483 if (xpath_result_size
> 1) {
3484 ERR("To many result while fetching config element value");
3489 if (xpath_result_size
== 0) {
3495 * Reverse traversal since last element could be nested under parent
3496 * element present in the result set.
3498 assert(xpath_result_set
->nodeTab
[0]);
3499 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
3500 if (!internal_value
) {
3505 value_result_size
= xmlStrlen(internal_value
);
3506 value
= calloc(value_result_size
+ 1, sizeof(char));
3507 strncpy(value
, (char *) internal_value
, value_result_size
);
3510 xmlXPathFreeContext(xpath_context
);
3511 xmlXPathFreeObject(xpath_object
);
3512 xmlFree(internal_xpath
);
3513 xmlFree(internal_value
);
3518 int config_document_element_exist(struct config_document
*document
,
3522 int xpath_result_size
;
3524 xmlXPathContextPtr xpath_context
= NULL
;
3525 xmlXPathObjectPtr xpath_object
= NULL
;
3526 xmlNodeSetPtr xpath_result_set
= NULL
;
3527 xmlChar
*internal_xpath
= NULL
;
3530 assert(document
->document
);
3533 internal_xpath
= encode_string(xpath
);
3534 if (!internal_xpath
) {
3535 ERR("Unable to encode xpath string");
3539 /* Initialize xpath context */
3540 xpath_context
= xmlXPathNewContext(document
->document
);
3541 if (!xpath_context
) {
3542 ERR("Unable to create xpath context");
3546 /* Evaluate the xpath expression */
3547 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3548 if (!xpath_object
) {
3549 ERR("Unable to evaluate xpath query (invalid query format)");
3553 xpath_result_set
= xpath_object
->nodesetval
;
3554 if (!xpath_result_set
) {
3558 xpath_result_size
= xpath_result_set
->nodeNr
;
3560 if (xpath_result_size
> 0) {
3564 xmlXPathFreeContext(xpath_context
);
3565 xmlXPathFreeObject(xpath_object
);
3566 xmlFree(internal_xpath
);
3572 struct config_element
*config_element_create(const char *name
,
3575 struct config_element
*element
;
3579 xmlChar
*internal_name
= NULL
;
3580 xmlChar
*internal_value
= NULL
;
3582 internal_name
= encode_string(name
);
3583 if (!internal_name
) {
3584 ERR("Unable to encode config element name string");
3591 internal_value
= encode_string(value
);
3592 if (!internal_value
) {
3593 ERR("Unable to encode config_element value string");
3599 element
= zmalloc(sizeof(struct config_element
));
3604 element
->element
= xmlNewNode(NULL
, internal_name
);
3605 if (!element
->element
) {
3611 if (internal_value
) {
3612 xmlNodeAddContent(element
->element
, internal_value
);
3615 xmlFree(internal_name
);
3616 xmlFree(internal_value
);
3621 int config_element_add_child(struct config_element
*parent
,
3622 const struct config_element
*child
)
3626 assert(parent
->element
);
3627 assert(child
->element
);
3630 xmlNodePtr node
= NULL
;
3631 xmlNodePtr copy
= NULL
;
3633 /* Do a copy to ease the memory management for caller */
3634 copy
= xmlCopyNode(child
->element
, 1);
3636 ret
= -LTTNG_ERR_NOMEM
;
3639 node
= xmlAddChild(parent
->element
, copy
);
3642 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3651 int config_document_insert_element(struct config_document
*document
,
3652 const char *xpath
, const struct config_element
*element
)
3655 int xpath_result_size
;
3657 xmlXPathContextPtr xpath_context
= NULL
;
3658 xmlXPathObjectPtr xpath_object
= NULL
;
3659 xmlNodeSetPtr xpath_result_set
= NULL
;
3660 xmlChar
*internal_xpath
= NULL
;
3661 xmlNodePtr child_node
= NULL
;
3662 xmlNodePtr local_copy
= NULL
;
3667 assert(element
->element
);
3669 internal_xpath
= encode_string(xpath
);
3670 if (!internal_xpath
) {
3671 ret
= -LTTNG_ERR_NOMEM
;
3675 /* Initialize xpath context */
3676 xpath_context
= xmlXPathNewContext(document
->document
);
3677 if (!xpath_context
) {
3678 ret
= -LTTNG_ERR_NOMEM
;
3682 /* Evaluate the xpath expression */
3683 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3684 if (!xpath_object
) {
3685 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3689 xpath_result_set
= xpath_object
->nodesetval
;
3690 if (!xpath_result_set
) {
3691 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3695 xpath_result_size
= xpath_result_set
->nodeNr
;
3697 if (xpath_result_size
> 1) {
3698 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3702 if (xpath_result_size
== 0) {
3703 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3707 assert(xpath_result_set
->nodeTab
[0]);
3708 /* Do a copy to simply memory management */
3709 local_copy
= xmlCopyNode(element
->element
, 1);
3711 ret
= -LTTNG_ERR_NOMEM
;
3712 ERR("Duplication of node to be insert failed");
3716 child_node
= xmlAddChild(xpath_result_set
->nodeTab
[0], local_copy
);
3718 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3719 xmlFreeNode(local_copy
);
3723 xmlXPathFreeContext(xpath_context
);
3724 xmlXPathFreeObject(xpath_object
);
3725 xmlFree(internal_xpath
);
3730 void config_element_free(struct config_element
*element
) {
3731 if (element
&& element
->element
) {
3732 xmlFreeNode(element
->element
);