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";
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 const char *config_get_loglevel_type_string(
253 enum lttng_loglevel_type loglevel_type
)
255 const char *loglevel_type_string
;
257 switch (loglevel_type
) {
258 case LTTNG_EVENT_LOGLEVEL_ALL
:
259 loglevel_type_string
= config_loglevel_type_all
;
261 case LTTNG_EVENT_LOGLEVEL_RANGE
:
262 loglevel_type_string
= config_loglevel_type_range
;
264 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
265 loglevel_type_string
= config_loglevel_type_single
;
268 loglevel_type_string
= NULL
;
271 return loglevel_type_string
;
275 int config_get_section_entries(const char *override_path
, const char *section
,
276 config_entry_handler_cb handler
, void *user_data
)
280 FILE *config_file
= NULL
;
281 struct handler_filter_args filter
= { section
, handler
, user_data
};
283 /* First, try system-wide conf. file. */
284 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
286 config_file
= fopen(path
, "r");
288 DBG("Loading daemon conf file at %s", path
);
290 * Return value is not very important here since error or not, we
291 * continue and try the next possible conf. file.
293 (void) ini_parse_file(config_file
,
294 (ini_entry_handler
) config_entry_handler_filter
,
299 /* Second is the user local configuration. */
300 path
= utils_get_home_dir();
302 char fullpath
[PATH_MAX
];
304 ret
= snprintf(fullpath
, sizeof(fullpath
),
305 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
307 PERROR("snprintf user conf. path");
311 config_file
= fopen(fullpath
, "r");
313 DBG("Loading daemon user conf file at %s", path
);
315 * Return value is not very important here since error or not, we
316 * continue and try the next possible conf. file.
318 (void) ini_parse_file(config_file
,
319 (ini_entry_handler
) config_entry_handler_filter
,
325 /* Final path is the one that the user might have provided. */
327 config_file
= fopen(override_path
, "r");
329 DBG("Loading daemon command line conf file at %s", override_path
);
330 (void) ini_parse_file(config_file
,
331 (ini_entry_handler
) config_entry_handler_filter
,
335 ERR("Failed to open daemon configuration file at %s",
342 /* Everything went well. */
350 int config_parse_value(const char *value
)
353 char *endptr
, *lower_str
;
363 v
= strtoul(value
, &endptr
, 10);
364 if (endptr
!= value
) {
369 lower_str
= zmalloc(len
+ 1);
376 for (i
= 0; i
< len
; i
++) {
377 lower_str
[i
] = tolower(value
[i
]);
380 if (!strcmp(lower_str
, config_str_yes
) ||
381 !strcmp(lower_str
, config_str_true
) ||
382 !strcmp(lower_str
, config_str_on
)) {
384 } else if (!strcmp(lower_str
, config_str_no
) ||
385 !strcmp(lower_str
, config_str_false
) ||
386 !strcmp(lower_str
, config_str_off
)) {
398 * Returns a xmlChar string which must be released using xmlFree().
400 static xmlChar
*encode_string(const char *in_str
)
402 xmlChar
*out_str
= NULL
;
403 xmlCharEncodingHandlerPtr handler
;
404 int out_len
, ret
, in_len
;
408 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
410 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
414 in_len
= strlen(in_str
);
416 * Add 1 byte for the NULL terminted character. The factor 4 here is
417 * used because UTF-8 characters can take up to 4 bytes.
419 out_len
= (in_len
* 4) + 1;
420 out_str
= xmlMalloc(out_len
);
425 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
432 /* out_len is now the size of out_str */
433 out_str
[out_len
] = '\0';
439 struct config_writer
*config_writer_create(int fd_output
, int indent
)
442 struct config_writer
*writer
;
443 xmlOutputBufferPtr buffer
;
445 writer
= zmalloc(sizeof(struct config_writer
));
447 PERROR("zmalloc config_writer_create");
451 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
456 writer
->writer
= xmlNewTextWriter(buffer
);
457 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
458 config_xml_encoding
, NULL
);
463 ret
= xmlTextWriterSetIndentString(writer
->writer
,
464 BAD_CAST config_xml_indent_string
);
469 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
477 config_writer_destroy(writer
);
482 int config_writer_destroy(struct config_writer
*writer
)
491 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
492 WARN("Could not close XML document");
496 if (writer
->writer
) {
497 xmlFreeTextWriter(writer
->writer
);
506 int config_writer_open_element(struct config_writer
*writer
,
507 const char *element_name
)
510 xmlChar
*encoded_element_name
;
512 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
517 encoded_element_name
= encode_string(element_name
);
518 if (!encoded_element_name
) {
523 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
524 xmlFree(encoded_element_name
);
526 return ret
>= 0 ? 0 : ret
;
530 int config_writer_write_attribute(struct config_writer
*writer
,
531 const char *name
, const char *value
)
534 xmlChar
*encoded_name
= NULL
;
535 xmlChar
*encoded_value
= NULL
;
537 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
542 encoded_name
= encode_string(name
);
548 encoded_value
= encode_string(value
);
549 if (!encoded_value
) {
554 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
557 xmlFree(encoded_name
);
558 xmlFree(encoded_value
);
559 return ret
>= 0 ? 0 : ret
;
563 int config_writer_close_element(struct config_writer
*writer
)
567 if (!writer
|| !writer
->writer
) {
572 ret
= xmlTextWriterEndElement(writer
->writer
);
574 return ret
>= 0 ? 0 : ret
;
578 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
579 const char *element_name
, uint64_t value
)
582 xmlChar
*encoded_element_name
;
584 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
589 encoded_element_name
= encode_string(element_name
);
590 if (!encoded_element_name
) {
595 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
596 encoded_element_name
, "%" PRIu64
, value
);
597 xmlFree(encoded_element_name
);
599 return ret
>= 0 ? 0 : ret
;
603 int config_writer_write_element_signed_int(struct config_writer
*writer
,
604 const char *element_name
, int64_t value
)
607 xmlChar
*encoded_element_name
;
609 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
614 encoded_element_name
= encode_string(element_name
);
615 if (!encoded_element_name
) {
620 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
621 encoded_element_name
, "%" PRIi64
, value
);
622 xmlFree(encoded_element_name
);
624 return ret
>= 0 ? 0 : ret
;
628 int config_writer_write_element_bool(struct config_writer
*writer
,
629 const char *element_name
, int value
)
631 return config_writer_write_element_string(writer
, element_name
,
632 value
? config_xml_true
: config_xml_false
);
636 int config_writer_write_element_string(struct config_writer
*writer
,
637 const char *element_name
, const char *value
)
640 xmlChar
*encoded_element_name
= NULL
;
641 xmlChar
*encoded_value
= NULL
;
643 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
649 encoded_element_name
= encode_string(element_name
);
650 if (!encoded_element_name
) {
655 encoded_value
= encode_string(value
);
656 if (!encoded_value
) {
661 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
664 xmlFree(encoded_element_name
);
665 xmlFree(encoded_value
);
666 return ret
>= 0 ? 0 : ret
;
670 int config_writer_write_config_element(struct config_writer
*writer
,
671 const struct config_element
*element
)
675 xmlNodePtr local_copy
= NULL
;
676 xmlNodePtr place_holder
= NULL
;
677 xmlDocPtr document
= NULL
;
678 xmlBufferPtr buffer
= NULL
;
681 assert(element
->element
);
684 /* Create a new doc and set root node as passed element */
685 document
= xmlNewDoc(BAD_CAST
"1.0");
686 document
->parseFlags
|= XML_PARSE_NOBLANKS
;
688 ERR("Unable to create xml document");
693 local_copy
= xmlCopyNode(element
->element
, 1);
695 ERR("Unable to copy an xml node");
700 /* Hand off the local_copy to the xmlDoc */
701 place_holder
= xmlDocSetRootElement(document
, local_copy
);
703 ERR("The document root was already set");
704 xmlFreeNode(local_copy
);
709 buffer
= xmlBufferCreate();
715 ret
= xmlNodeDump(buffer
, document
, local_copy
, 0, 0);
716 xmlTextWriterWriteRaw(writer
->writer
, xmlBufferContent(buffer
));
718 xmlBufferFree(buffer
);
719 xmlFreeDoc(document
);
724 void xml_error_handler(void *ctx
, const char *format
, ...)
730 va_start(args
, format
);
731 ret
= vasprintf(&errMsg
, format
, args
);
734 ERR("String allocation failed in xml error handler");
738 fprintf(stderr
, "XML Error: %s", errMsg
);
743 void fini_session_config_validation_ctx(
744 struct session_config_validation_ctx
*ctx
)
746 if (ctx
->parser_ctx
) {
747 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
751 xmlSchemaFree(ctx
->schema
);
754 if (ctx
->schema_validation_ctx
) {
755 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
758 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
762 char *get_session_config_xsd_path()
765 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
766 size_t base_path_len
;
770 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
773 base_path_len
= strlen(base_path
);
774 max_path_len
= base_path_len
+
775 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
776 xsd_path
= zmalloc(max_path_len
);
781 strncpy(xsd_path
, base_path
, max_path_len
);
782 if (xsd_path
[base_path_len
- 1] != '/') {
783 xsd_path
[base_path_len
++] = '/';
786 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
787 max_path_len
- base_path_len
);
793 int init_session_config_validation_ctx(
794 struct session_config_validation_ctx
*ctx
)
797 char *xsd_path
= get_session_config_xsd_path();
800 ret
= -LTTNG_ERR_NOMEM
;
804 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
805 if (!ctx
->parser_ctx
) {
806 ERR("XSD parser context creation failed");
807 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
810 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
811 xml_error_handler
, NULL
);
813 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
815 ERR("XSD parsing failed");
816 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
820 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
821 if (!ctx
->schema_validation_ctx
) {
822 ERR("XSD validation context creation failed");
823 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
827 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
828 xml_error_handler
, NULL
);
833 fini_session_config_validation_ctx(ctx
);
841 int parse_uint(xmlChar
*str
, uint64_t *val
)
851 *val
= strtoull((const char *) str
, &endptr
, 10);
852 if (!endptr
|| *endptr
) {
863 int parse_int(xmlChar
*str
, int64_t *val
)
873 *val
= strtoll((const char *) str
, &endptr
, 10);
874 if (!endptr
|| *endptr
) {
885 int parse_bool(xmlChar
*str
, int *val
)
894 if (!strcmp((const char *) str
, config_xml_true
)) {
896 } else if (!strcmp((const char *) str
, config_xml_false
)) {
899 WARN("Invalid boolean value encoutered (%s).",
908 int get_domain_type(xmlChar
*domain
)
916 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
917 ret
= LTTNG_DOMAIN_KERNEL
;
918 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
919 ret
= LTTNG_DOMAIN_UST
;
920 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
921 ret
= LTTNG_DOMAIN_JUL
;
922 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
923 ret
= LTTNG_DOMAIN_LOG4J
;
924 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
925 ret
= LTTNG_DOMAIN_PYTHON
;
936 int get_buffer_type(xmlChar
*buffer_type
)
944 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
945 ret
= LTTNG_BUFFER_GLOBAL
;
946 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
947 ret
= LTTNG_BUFFER_PER_UID
;
948 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
949 ret
= LTTNG_BUFFER_PER_PID
;
960 int get_overwrite_mode(xmlChar
*overwrite_mode
)
964 if (!overwrite_mode
) {
968 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
970 } else if (!strcmp((char *) overwrite_mode
,
971 config_overwrite_mode_discard
)) {
983 int get_output_type(xmlChar
*output_type
)
991 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
992 ret
= LTTNG_EVENT_MMAP
;
993 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
994 ret
= LTTNG_EVENT_SPLICE
;
1005 int config_get_event_type(const char *event_type
)
1013 if (!strcmp(event_type
, config_event_type_all
)) {
1014 ret
= LTTNG_EVENT_ALL
;
1015 } else if (!strcmp(event_type
, config_event_type_tracepoint
)) {
1016 ret
= LTTNG_EVENT_TRACEPOINT
;
1017 } else if (!strcmp(event_type
, config_event_type_probe
)) {
1018 ret
= LTTNG_EVENT_PROBE
;
1019 } else if (!strcmp(event_type
, config_event_type_function
)) {
1020 ret
= LTTNG_EVENT_FUNCTION
;
1021 } else if (!strcmp(event_type
,
1022 config_event_type_function_entry
)) {
1023 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
1024 } else if (!strcmp(event_type
, config_event_type_noop
)) {
1025 ret
= LTTNG_EVENT_NOOP
;
1026 } else if (!strcmp(event_type
, config_event_type_syscall
)) {
1027 ret
= LTTNG_EVENT_SYSCALL
;
1038 int get_loglevel_type(xmlChar
*loglevel_type
)
1042 if (!loglevel_type
) {
1046 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
1047 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
1048 } else if (!strcmp((char *) loglevel_type
,
1049 config_loglevel_type_range
)) {
1050 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1051 } else if (!strcmp((char *) loglevel_type
,
1052 config_loglevel_type_single
)) {
1053 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1064 * Return the context type or -1 on error.
1067 int get_context_type(xmlChar
*context_type
)
1071 if (!context_type
) {
1075 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
1076 ret
= LTTNG_EVENT_CONTEXT_PID
;
1077 } else if (!strcmp((char *) context_type
,
1078 config_event_context_procname
)) {
1079 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1080 } else if (!strcmp((char *) context_type
,
1081 config_event_context_prio
)) {
1082 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
1083 } else if (!strcmp((char *) context_type
,
1084 config_event_context_nice
)) {
1085 ret
= LTTNG_EVENT_CONTEXT_NICE
;
1086 } else if (!strcmp((char *) context_type
,
1087 config_event_context_vpid
)) {
1088 ret
= LTTNG_EVENT_CONTEXT_VPID
;
1089 } else if (!strcmp((char *) context_type
,
1090 config_event_context_tid
)) {
1091 ret
= LTTNG_EVENT_CONTEXT_TID
;
1092 } else if (!strcmp((char *) context_type
,
1093 config_event_context_vtid
)) {
1094 ret
= LTTNG_EVENT_CONTEXT_VTID
;
1095 } else if (!strcmp((char *) context_type
,
1096 config_event_context_ppid
)) {
1097 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1098 } else if (!strcmp((char *) context_type
,
1099 config_event_context_vppid
)) {
1100 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1101 } else if (!strcmp((char *) context_type
,
1102 config_event_context_pthread_id
)) {
1103 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1104 } else if (!strcmp((char *) context_type
,
1105 config_event_context_hostname
)) {
1106 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1107 } else if (!strcmp((char *) context_type
,
1108 config_event_context_ip
)) {
1109 ret
= LTTNG_EVENT_CONTEXT_IP
;
1110 } else if (!strcmp((char *) context_type
,
1111 config_event_context_interruptible
)) {
1112 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1113 } else if (!strcmp((char *) context_type
,
1114 config_event_context_preemptible
)) {
1115 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1116 } else if (!strcmp((char *) context_type
,
1117 config_event_context_need_reschedule
)) {
1118 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1119 } else if (!strcmp((char *) context_type
,
1120 config_event_context_migratable
)) {
1121 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1132 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1137 for (node
= xmlFirstElementChild(domain_node
); node
;
1138 node
= xmlNextElementSibling(node
)) {
1139 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1141 xmlChar
*node_content
= xmlNodeGetContent(node
);
1142 if (!node_content
) {
1143 ret
= -LTTNG_ERR_NOMEM
;
1147 ret
= get_domain_type(node_content
);
1150 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1155 } else if (!strcmp((const char *) node
->name
,
1156 config_element_buffer_type
)) {
1158 xmlChar
*node_content
= xmlNodeGetContent(node
);
1159 if (!node_content
) {
1160 ret
= -LTTNG_ERR_NOMEM
;
1164 ret
= get_buffer_type(node_content
);
1167 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1171 domain
->buf_type
= ret
;
1180 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1185 for (node
= xmlFirstElementChild(net_output_node
); node
;
1186 node
= xmlNextElementSibling(node
)) {
1187 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1189 *control_uri
= (char *) xmlNodeGetContent(node
);
1190 if (!*control_uri
) {
1195 *data_uri
= (char *) xmlNodeGetContent(node
);
1202 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1206 int process_consumer_output(xmlNodePtr consumer_output_node
,
1207 struct consumer_output
*output
)
1214 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1215 node
= xmlNextElementSibling(node
)) {
1216 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1217 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1221 ret
= -LTTNG_ERR_NOMEM
;
1225 ret
= parse_bool(enabled_str
, &output
->enabled
);
1231 xmlNodePtr output_type_node
;
1234 output_type_node
= xmlFirstElementChild(node
);
1235 if (!output_type_node
) {
1236 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1240 if (!strcmp((const char *) output_type_node
->name
,
1241 config_element_path
)) {
1243 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1244 if (!output
->path
) {
1245 ret
= -LTTNG_ERR_NOMEM
;
1250 ret
= get_net_output_uris(output_type_node
,
1251 &output
->control_uri
, &output
->data_uri
);
1263 free(output
->control_uri
);
1264 free(output
->data_uri
);
1265 memset(output
, 0, sizeof(struct consumer_output
));
1271 int create_session_net_output(const char *name
, const char *control_uri
,
1272 const char *data_uri
)
1275 struct lttng_handle
*handle
;
1276 const char *uri
= NULL
;
1280 handle
= lttng_create_handle(name
, NULL
);
1282 ret
= -LTTNG_ERR_NOMEM
;
1286 if (!control_uri
|| !data_uri
) {
1287 uri
= control_uri
? control_uri
: data_uri
;
1292 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1293 lttng_destroy_handle(handle
);
1299 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1302 xmlNodePtr node
= NULL
;
1303 xmlNodePtr snapshot_output_list_node
;
1304 xmlNodePtr snapshot_output_node
;
1306 assert(session_name
);
1308 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1317 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1319 /* Parse and create snapshot outputs */
1321 for (snapshot_output_node
=
1322 xmlFirstElementChild(snapshot_output_list_node
);
1323 snapshot_output_node
; snapshot_output_node
=
1324 xmlNextElementSibling(snapshot_output_node
)) {
1326 uint64_t max_size
= DEFAULT_SNAPSHOT_MAX_SIZE
;
1327 struct consumer_output output
= { 0 };
1328 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1330 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1331 node
= xmlNextElementSibling(node
)) {
1332 if (!strcmp((const char *) node
->name
,
1333 config_element_name
)) {
1335 name
= (char *) xmlNodeGetContent(node
);
1337 ret
= -LTTNG_ERR_NOMEM
;
1338 goto error_snapshot_output
;
1340 } else if (!strcmp((const char *) node
->name
,
1341 config_element_max_size
)) {
1342 xmlChar
*content
= xmlNodeGetContent(node
);
1346 ret
= -LTTNG_ERR_NOMEM
;
1347 goto error_snapshot_output
;
1349 ret
= parse_uint(content
, &max_size
);
1352 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1353 goto error_snapshot_output
;
1356 /* consumer_output */
1357 ret
= process_consumer_output(node
, &output
);
1359 goto error_snapshot_output
;
1364 snapshot_output
= lttng_snapshot_output_create();
1365 if (!snapshot_output
) {
1366 ret
= -LTTNG_ERR_NOMEM
;
1367 goto error_snapshot_output
;
1371 /* Generate a default name */
1373 pret
= asprintf(&name
, DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
1374 lttng_snapshot_output_get_id(snapshot_output
));
1377 PERROR("snprintf output name");
1378 goto error_snapshot_output
;
1382 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1384 goto error_snapshot_output
;
1387 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1389 goto error_snapshot_output
;
1393 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1396 goto error_snapshot_output
;
1399 if (output
.control_uri
) {
1400 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1403 goto error_snapshot_output
;
1407 if (output
.data_uri
) {
1408 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1411 goto error_snapshot_output
;
1416 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1417 error_snapshot_output
:
1420 free(output
.control_uri
);
1421 free(output
.data_uri
);
1422 lttng_snapshot_output_destroy(snapshot_output
);
1432 int create_session(const char *name
,
1433 struct lttng_domain
*kernel_domain
,
1434 struct lttng_domain
*ust_domain
,
1435 struct lttng_domain
*jul_domain
,
1436 struct lttng_domain
*log4j_domain
,
1437 xmlNodePtr output_node
,
1438 uint64_t live_timer_interval
)
1441 struct consumer_output output
= { 0 };
1442 xmlNodePtr consumer_output_node
;
1447 consumer_output_node
= xmlFirstElementChild(output_node
);
1448 if (!consumer_output_node
) {
1449 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1453 if (strcmp((const char *) consumer_output_node
->name
,
1454 config_element_consumer_output
)) {
1455 WARN("Invalid output type, expected %s node",
1456 config_element_consumer_output
);
1457 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1461 ret
= process_consumer_output(consumer_output_node
, &output
);
1467 if (live_timer_interval
!= UINT64_MAX
&&
1468 !output
.control_uri
&& !output
.data_uri
) {
1469 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1473 if (output
.control_uri
|| output
.data_uri
) {
1474 /* network destination */
1475 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1477 * URLs are provided for sure since the test above make sure that
1478 * with a live timer the data and control URIs are provided. So,
1479 * NULL is passed here and will be set right after.
1481 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1483 ret
= lttng_create_session(name
, NULL
);
1489 ret
= create_session_net_output(name
, output
.control_uri
,
1496 /* either local output or no output */
1497 ret
= lttng_create_session(name
, output
.path
);
1504 free(output
.control_uri
);
1505 free(output
.data_uri
);
1509 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1510 struct lttng_event_probe_attr
*attr
)
1514 assert(probe_attribute_node
);
1517 if (!strcmp((const char *) probe_attribute_node
->name
,
1518 config_element_address
)) {
1523 content
= xmlNodeGetContent(probe_attribute_node
);
1525 ret
= -LTTNG_ERR_NOMEM
;
1529 ret
= parse_uint(content
, &addr
);
1532 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1537 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1538 config_element_offset
)) {
1540 uint64_t offset
= 0;
1543 content
= xmlNodeGetContent(probe_attribute_node
);
1545 ret
= -LTTNG_ERR_NOMEM
;
1549 ret
= parse_uint(content
, &offset
);
1552 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 attr
->offset
= offset
;
1557 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1558 config_element_symbol_name
)) {
1563 content
= xmlNodeGetContent(probe_attribute_node
);
1565 ret
= -LTTNG_ERR_NOMEM
;
1569 name_len
= strlen((char *) content
);
1570 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1571 WARN("symbol_name too long.");
1572 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1577 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1586 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1587 const char *channel_name
, const enum process_event_node_phase phase
)
1591 struct lttng_event event
;
1592 char **exclusions
= NULL
;
1593 unsigned long exclusion_count
= 0;
1594 char *filter_expression
= NULL
;
1599 memset(&event
, 0, sizeof(event
));
1601 /* Initialize default log level which varies by domain */
1602 switch (handle
->domain
.type
)
1604 case LTTNG_DOMAIN_JUL
:
1605 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1607 case LTTNG_DOMAIN_LOG4J
:
1608 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1610 case LTTNG_DOMAIN_PYTHON
:
1611 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1613 case LTTNG_DOMAIN_UST
:
1614 case LTTNG_DOMAIN_KERNEL
:
1615 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1621 for (node
= xmlFirstElementChild(event_node
); node
;
1622 node
= xmlNextElementSibling(node
)) {
1623 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1628 content
= xmlNodeGetContent(node
);
1630 ret
= -LTTNG_ERR_NOMEM
;
1634 name_len
= strlen((char *) content
);
1635 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1636 WARN("Channel name too long.");
1637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1642 strncpy(event
.name
, (const char *) content
, name_len
);
1644 } else if (!strcmp((const char *) node
->name
,
1645 config_element_enabled
)) {
1646 xmlChar
*content
= xmlNodeGetContent(node
);
1650 ret
= -LTTNG_ERR_NOMEM
;
1654 ret
= parse_bool(content
, &event
.enabled
);
1657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1660 } else if (!strcmp((const char *) node
->name
,
1661 config_element_type
)) {
1662 xmlChar
*content
= xmlNodeGetContent(node
);
1666 ret
= -LTTNG_ERR_NOMEM
;
1670 ret
= config_get_event_type((char *) content
);
1673 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1678 } else if (!strcmp((const char *) node
->name
,
1679 config_element_loglevel_type
)) {
1680 xmlChar
*content
= xmlNodeGetContent(node
);
1684 ret
= -LTTNG_ERR_NOMEM
;
1688 ret
= get_loglevel_type(content
);
1691 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1695 event
.loglevel_type
= ret
;
1696 } else if (!strcmp((const char *) node
->name
,
1697 config_element_loglevel
)) {
1699 int64_t loglevel
= 0;
1702 content
= xmlNodeGetContent(node
);
1704 ret
= -LTTNG_ERR_NOMEM
;
1708 ret
= parse_int(content
, &loglevel
);
1711 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1715 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1716 WARN("loglevel out of range.");
1717 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1721 event
.loglevel
= loglevel
;
1722 } else if (!strcmp((const char *) node
->name
,
1723 config_element_filter
)) {
1725 xmlNodeGetContent(node
);
1729 ret
= -LTTNG_ERR_NOMEM
;
1733 free(filter_expression
);
1734 filter_expression
= strdup((char *) content
);
1736 if (!filter_expression
) {
1737 ret
= -LTTNG_ERR_NOMEM
;
1740 } else if (!strcmp((const char *) node
->name
,
1741 config_element_exclusions
)) {
1742 xmlNodePtr exclusion_node
;
1743 int exclusion_index
= 0;
1748 * Exclusions has already been initialized,
1751 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1755 exclusion_count
= xmlChildElementCount(node
);
1756 if (!exclusion_count
) {
1760 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1762 exclusion_count
= 0;
1763 ret
= -LTTNG_ERR_NOMEM
;
1767 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1768 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1770 xmlNodeGetContent(exclusion_node
);
1773 ret
= -LTTNG_ERR_NOMEM
;
1777 exclusions
[exclusion_index
] = strdup((const char *) content
);
1779 if (!exclusions
[exclusion_index
]) {
1780 ret
= -LTTNG_ERR_NOMEM
;
1786 event
.exclusion
= 1;
1787 } else if (!strcmp((const char *) node
->name
,
1788 config_element_attributes
)) {
1789 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1792 if (!attribute_node
) {
1793 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1797 if (!strcmp((const char *) node
->name
,
1798 config_element_probe_attributes
)) {
1799 xmlNodePtr probe_attribute_node
;
1801 /* probe_attributes */
1802 for (probe_attribute_node
=
1803 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1804 probe_attribute_node
= xmlNextElementSibling(
1805 probe_attribute_node
)) {
1807 ret
= process_probe_attribute_node(probe_attribute_node
,
1816 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1818 /* function_attributes */
1819 content
= xmlNodeGetContent(symbol_node
);
1821 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1825 sym_len
= strlen((char *) content
);
1826 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1827 WARN("Function name too long.");
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1833 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1840 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1841 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1842 filter_expression
, exclusion_count
, exclusions
);
1848 for (i
= 0; i
< exclusion_count
; i
++) {
1849 free(exclusions
[i
]);
1853 free(filter_expression
);
1858 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1859 const char *channel_name
)
1862 struct lttng_event event
;
1865 assert(events_node
);
1867 assert(channel_name
);
1869 for (node
= xmlFirstElementChild(events_node
); node
;
1870 node
= xmlNextElementSibling(node
)) {
1871 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1878 * Disable all events to enable only the necessary events.
1879 * Limitations regarding lttng_disable_events and tuple descriptor
1880 * force this approach.
1882 memset(&event
, 0, sizeof(event
));
1883 event
.loglevel
= -1;
1884 event
.type
= LTTNG_EVENT_ALL
;
1885 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1890 for (node
= xmlFirstElementChild(events_node
); node
;
1891 node
= xmlNextElementSibling(node
)) {
1892 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1903 int process_channel_attr_node(xmlNodePtr attr_node
, bool snapshot_mode
,
1904 enum lttng_domain_type domain
, struct lttng_channel
*channel
,
1905 xmlNodePtr
*contexts_node
, xmlNodePtr
*events_node
)
1911 assert(contexts_node
);
1912 assert(events_node
);
1914 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1919 content
= xmlNodeGetContent(attr_node
);
1921 ret
= -LTTNG_ERR_NOMEM
;
1925 name_len
= strlen((char *) content
);
1926 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1927 WARN("Channel name too long.");
1928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1933 strncpy(channel
->name
, (const char *) content
, name_len
);
1935 } else if (!strcmp((const char *) attr_node
->name
,
1936 config_element_enabled
)) {
1941 content
= xmlNodeGetContent(attr_node
);
1943 ret
= -LTTNG_ERR_NOMEM
;
1947 ret
= parse_bool(content
, &enabled
);
1950 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1954 channel
->enabled
= enabled
;
1955 } else if (!strcmp((const char *) attr_node
->name
,
1956 config_element_overwrite_mode
)) {
1959 /* overwrite_mode */
1960 content
= xmlNodeGetContent(attr_node
);
1962 ret
= -LTTNG_ERR_NOMEM
;
1966 ret
= get_overwrite_mode(content
);
1969 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1973 channel
->attr
.overwrite
= ret
;
1974 } else if (!strcmp((const char *) attr_node
->name
,
1975 config_element_subbuf_size
)) {
1978 /* subbuffer_size */
1979 content
= xmlNodeGetContent(attr_node
);
1981 ret
= -LTTNG_ERR_NOMEM
;
1985 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1988 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1991 } else if (!strcmp((const char *) attr_node
->name
,
1992 config_element_num_subbuf
)) {
1995 /* subbuffer_count */
1996 content
= xmlNodeGetContent(attr_node
);
1998 ret
= -LTTNG_ERR_NOMEM
;
2002 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
2005 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2008 } else if (!strcmp((const char *) attr_node
->name
,
2009 config_element_switch_timer_interval
)) {
2011 uint64_t switch_timer_interval
= 0;
2013 /* switch_timer_interval */
2014 content
= xmlNodeGetContent(attr_node
);
2016 ret
= -LTTNG_ERR_NOMEM
;
2020 ret
= parse_uint(content
, &switch_timer_interval
);
2023 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2027 if (switch_timer_interval
> UINT_MAX
) {
2028 WARN("switch_timer_interval out of range.");
2029 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2033 channel
->attr
.switch_timer_interval
=
2034 switch_timer_interval
;
2035 } else if (!strcmp((const char *) attr_node
->name
,
2036 config_element_read_timer_interval
)) {
2038 uint64_t read_timer_interval
= 0;
2040 /* read_timer_interval */
2041 content
= xmlNodeGetContent(attr_node
);
2043 ret
= -LTTNG_ERR_NOMEM
;
2047 ret
= parse_uint(content
, &read_timer_interval
);
2050 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2054 if (read_timer_interval
> UINT_MAX
) {
2055 WARN("read_timer_interval out of range.");
2056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2060 channel
->attr
.read_timer_interval
=
2061 read_timer_interval
;
2062 } else if (!strcmp((const char *) attr_node
->name
,
2063 config_element_output_type
)) {
2067 content
= xmlNodeGetContent(attr_node
);
2069 ret
= -LTTNG_ERR_NOMEM
;
2073 ret
= get_output_type(content
);
2076 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2080 channel
->attr
.output
= ret
;
2081 } else if (!strcmp((const char *) attr_node
->name
,
2082 config_element_tracefile_size
)) {
2085 /* tracefile_size */
2086 content
= xmlNodeGetContent(attr_node
);
2088 ret
= -LTTNG_ERR_NOMEM
;
2092 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2095 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2098 } else if (!strcmp((const char *) attr_node
->name
,
2099 config_element_tracefile_count
)) {
2102 /* tracefile_count */
2103 content
= xmlNodeGetContent(attr_node
);
2105 ret
= -LTTNG_ERR_NOMEM
;
2109 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2112 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2115 } else if (!strcmp((const char *) attr_node
->name
,
2116 config_element_live_timer_interval
)) {
2118 uint64_t live_timer_interval
= 0;
2120 /* live_timer_interval */
2121 content
= xmlNodeGetContent(attr_node
);
2123 ret
= -LTTNG_ERR_NOMEM
;
2127 ret
= parse_uint(content
, &live_timer_interval
);
2130 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2134 if (live_timer_interval
> UINT_MAX
) {
2135 WARN("live_timer_interval out of range.");
2136 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2140 channel
->attr
.live_timer_interval
=
2141 live_timer_interval
;
2142 } else if (!strcmp((const char *) attr_node
->name
,
2143 config_element_events
)) {
2145 *events_node
= attr_node
;
2148 *contexts_node
= attr_node
;
2157 int process_context_node(xmlNodePtr context_node
,
2158 struct lttng_handle
*handle
, const char *channel_name
)
2161 struct lttng_event_context context
;
2162 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2165 assert(channel_name
);
2167 if (!context_child_node
) {
2168 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2172 memset(&context
, 0, sizeof(context
));
2174 if (!strcmp((const char *) context_child_node
->name
,
2175 config_element_type
)) {
2177 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2180 ret
= -LTTNG_ERR_NOMEM
;
2184 ret
= get_context_type(content
);
2187 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2192 } else if (!strcmp((const char *) context_child_node
->name
,
2193 config_element_context_perf
)) {
2195 xmlNodePtr perf_attr_node
;
2197 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2198 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2199 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2200 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2201 perf_attr_node
; perf_attr_node
=
2202 xmlNextElementSibling(perf_attr_node
)) {
2203 if (!strcmp((const char *) perf_attr_node
->name
,
2204 config_element_type
)) {
2209 content
= xmlNodeGetContent(perf_attr_node
);
2211 ret
= -LTTNG_ERR_NOMEM
;
2215 ret
= parse_uint(content
, &type
);
2218 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2222 if (type
> UINT32_MAX
) {
2223 WARN("perf context type out of range.");
2224 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2228 context
.u
.perf_counter
.type
= type
;
2229 } else if (!strcmp((const char *) perf_attr_node
->name
,
2230 config_element_config
)) {
2232 uint64_t config
= 0;
2235 content
= xmlNodeGetContent(perf_attr_node
);
2237 ret
= -LTTNG_ERR_NOMEM
;
2241 ret
= parse_uint(content
, &config
);
2244 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2248 context
.u
.perf_counter
.config
= config
;
2249 } else if (!strcmp((const char *) perf_attr_node
->name
,
2250 config_element_name
)) {
2255 content
= xmlNodeGetContent(perf_attr_node
);
2257 ret
= -LTTNG_ERR_NOMEM
;
2261 name_len
= strlen((char *) content
);
2262 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2263 WARN("perf context name too long.");
2264 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2269 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2274 } else if (!strcmp((const char *) context_child_node
->name
,
2275 config_element_context_app
)) {
2276 /* application context */
2277 xmlNodePtr app_ctx_node
;
2279 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2280 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2281 app_ctx_node
; app_ctx_node
=
2282 xmlNextElementSibling(app_ctx_node
)) {
2284 char **target
= strcmp(
2285 (const char *) app_ctx_node
->name
,
2286 config_element_context_app_provider_name
) == 0 ?
2287 &context
.u
.app_ctx
.provider_name
:
2288 &context
.u
.app_ctx
.ctx_name
;
2290 content
= xmlNodeGetContent(app_ctx_node
);
2292 ret
= -LTTNG_ERR_NOMEM
;
2296 *target
= (char *) content
;
2299 /* Unrecognized context type */
2300 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2304 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2305 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2306 free(context
.u
.app_ctx
.provider_name
);
2307 free(context
.u
.app_ctx
.ctx_name
);
2314 int process_contexts_node(xmlNodePtr contexts_node
,
2315 struct lttng_handle
*handle
, const char *channel_name
)
2318 xmlNodePtr context_node
;
2320 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2321 context_node
= xmlNextElementSibling(context_node
)) {
2322 ret
= process_context_node(context_node
, handle
, channel_name
);
2332 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2333 struct lttng_handle
*handle
)
2336 xmlNodePtr targets_node
= NULL
;
2340 assert(pid_tracker_node
);
2341 /* get the targets node */
2342 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2343 node
= xmlNextElementSibling(node
)) {
2344 if (!strcmp((const char *) node
->name
,
2345 config_element_targets
)) {
2346 targets_node
= node
;
2351 if (!targets_node
) {
2352 ret
= LTTNG_ERR_INVALID
;
2356 /* Go through all pid_target node */
2357 child
= xmlChildElementCount(targets_node
);
2359 /* The session is explicitly set to target nothing. */
2360 ret
= lttng_untrack_pid(handle
, -1);
2365 for (node
= xmlFirstElementChild(targets_node
); node
;
2366 node
= xmlNextElementSibling(node
)) {
2367 xmlNodePtr pid_target_node
= node
;
2369 /* get pid node and track it */
2370 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2371 node
= xmlNextElementSibling(node
)) {
2372 if (!strcmp((const char *) node
->name
,
2373 config_element_pid
)) {
2375 xmlChar
*content
= NULL
;
2377 content
= xmlNodeGetContent(node
);
2379 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2383 ret
= parse_int(content
, &pid
);
2386 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2390 ret
= lttng_track_pid(handle
, (int) pid
);
2396 node
= pid_target_node
;
2405 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
,
2409 struct lttng_domain domain
= { 0 };
2410 struct lttng_handle
*handle
= NULL
;
2411 xmlNodePtr channels_node
= NULL
;
2412 xmlNodePtr trackers_node
= NULL
;
2413 xmlNodePtr pid_tracker_node
= NULL
;
2416 assert(session_name
);
2418 ret
= init_domain(domain_node
, &domain
);
2423 handle
= lttng_create_handle(session_name
, &domain
);
2425 ret
= -LTTNG_ERR_NOMEM
;
2429 /* get the channels node */
2430 for (node
= xmlFirstElementChild(domain_node
); node
;
2431 node
= xmlNextElementSibling(node
)) {
2432 if (!strcmp((const char *) node
->name
,
2433 config_element_channels
)) {
2434 channels_node
= node
;
2439 if (!channels_node
) {
2443 /* create all channels */
2444 for (node
= xmlFirstElementChild(channels_node
); node
;
2445 node
= xmlNextElementSibling(node
)) {
2446 struct lttng_channel channel
;
2447 xmlNodePtr contexts_node
= NULL
;
2448 xmlNodePtr events_node
= NULL
;
2449 xmlNodePtr channel_attr_node
;
2451 memset(&channel
, 0, sizeof(channel
));
2452 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2454 for (channel_attr_node
= xmlFirstElementChild(node
);
2455 channel_attr_node
; channel_attr_node
=
2456 xmlNextElementSibling(channel_attr_node
)) {
2457 ret
= process_channel_attr_node(channel_attr_node
,
2458 snapshot_mode
, domain
.type
, &channel
,
2459 &contexts_node
, &events_node
);
2465 if (!channel
.name
) {
2466 ERR("Encountered a channel with no name attribute.");
2467 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2471 ret
= lttng_enable_channel(handle
, &channel
);
2476 ret
= process_events_node(events_node
, handle
, channel
.name
);
2481 ret
= process_contexts_node(contexts_node
, handle
,
2488 /* get the trackers node */
2489 for (node
= xmlFirstElementChild(domain_node
); node
;
2490 node
= xmlNextElementSibling(node
)) {
2491 if (!strcmp((const char *) node
->name
,
2492 config_element_trackers
)) {
2493 trackers_node
= node
;
2498 if (!trackers_node
) {
2502 for (node
= xmlFirstElementChild(trackers_node
); node
;
2503 node
= xmlNextElementSibling(node
)) {
2504 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2505 pid_tracker_node
= node
;
2506 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2513 if (!pid_tracker_node
) {
2514 lttng_track_pid(handle
, -1);
2518 lttng_destroy_handle(handle
);
2523 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2526 int ret
, started
= -1, snapshot_mode
= -1;
2527 uint64_t live_timer_interval
= UINT64_MAX
;
2528 xmlChar
*name
= NULL
;
2529 xmlChar
*shm_path
= NULL
;
2530 xmlNodePtr domains_node
= NULL
;
2531 xmlNodePtr output_node
= NULL
;
2533 struct lttng_domain
*kernel_domain
= NULL
;
2534 struct lttng_domain
*ust_domain
= NULL
;
2535 struct lttng_domain
*jul_domain
= NULL
;
2536 struct lttng_domain
*log4j_domain
= NULL
;
2537 struct lttng_domain
*python_domain
= NULL
;
2539 for (node
= xmlFirstElementChild(session_node
); node
;
2540 node
= xmlNextElementSibling(node
)) {
2541 if (!name
&& !strcmp((const char *) node
->name
,
2542 config_element_name
)) {
2544 xmlChar
*node_content
= xmlNodeGetContent(node
);
2545 if (!node_content
) {
2546 ret
= -LTTNG_ERR_NOMEM
;
2550 name
= node_content
;
2551 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2552 config_element_domains
)) {
2554 domains_node
= node
;
2555 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2556 config_element_started
)) {
2558 xmlChar
*node_content
= xmlNodeGetContent(node
);
2559 if (!node_content
) {
2560 ret
= -LTTNG_ERR_NOMEM
;
2564 ret
= parse_bool(node_content
, &started
);
2567 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2570 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2571 config_element_output
)) {
2574 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2575 config_element_shared_memory_path
)) {
2576 /* shared memory path */
2577 xmlChar
*node_content
= xmlNodeGetContent(node
);
2578 if (!node_content
) {
2579 ret
= -LTTNG_ERR_NOMEM
;
2583 shm_path
= node_content
;
2585 /* attributes, snapshot_mode or live_timer_interval */
2586 xmlNodePtr attributes_child
=
2587 xmlFirstElementChild(node
);
2589 if (!strcmp((const char *) attributes_child
->name
,
2590 config_element_snapshot_mode
)) {
2592 xmlChar
*snapshot_mode_content
=
2593 xmlNodeGetContent(attributes_child
);
2594 if (!snapshot_mode_content
) {
2595 ret
= -LTTNG_ERR_NOMEM
;
2599 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2600 free(snapshot_mode_content
);
2602 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2606 /* live_timer_interval */
2607 xmlChar
*timer_interval_content
=
2608 xmlNodeGetContent(attributes_child
);
2609 if (!timer_interval_content
) {
2610 ret
= -LTTNG_ERR_NOMEM
;
2614 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2615 free(timer_interval_content
);
2617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2625 /* Mandatory attribute, as defined in the session XSD */
2626 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2630 if (session_name
&& strcmp((char *) name
, session_name
)) {
2631 /* This is not the session we are looking for */
2632 ret
= -LTTNG_ERR_NO_SESSION
;
2636 /* Init domains to create the session handles */
2637 for (node
= xmlFirstElementChild(domains_node
); node
;
2638 node
= xmlNextElementSibling(node
)) {
2639 struct lttng_domain
*domain
;
2641 domain
= zmalloc(sizeof(*domain
));
2643 ret
= -LTTNG_ERR_NOMEM
;
2647 ret
= init_domain(node
, domain
);
2649 goto domain_init_error
;
2652 switch (domain
->type
) {
2653 case LTTNG_DOMAIN_KERNEL
:
2654 if (kernel_domain
) {
2655 /* Same domain seen twice, invalid! */
2656 goto domain_init_error
;
2658 kernel_domain
= domain
;
2660 case LTTNG_DOMAIN_UST
:
2662 /* Same domain seen twice, invalid! */
2663 goto domain_init_error
;
2665 ust_domain
= domain
;
2667 case LTTNG_DOMAIN_JUL
:
2669 /* Same domain seen twice, invalid! */
2670 goto domain_init_error
;
2672 jul_domain
= domain
;
2674 case LTTNG_DOMAIN_LOG4J
:
2676 /* Same domain seen twice, invalid! */
2677 goto domain_init_error
;
2679 log4j_domain
= domain
;
2681 case LTTNG_DOMAIN_PYTHON
:
2682 if (python_domain
) {
2683 /* Same domain seen twice, invalid! */
2684 goto domain_init_error
;
2686 python_domain
= domain
;
2689 WARN("Invalid domain type");
2690 goto domain_init_error
;
2695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2700 /* Destroy session if it exists */
2701 ret
= lttng_destroy_session((const char *) name
);
2702 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2703 ERR("Failed to destroy existing session.");
2708 /* Create session type depending on output type */
2709 if (snapshot_mode
&& snapshot_mode
!= -1) {
2710 ret
= create_snapshot_session((const char *) name
, output_node
);
2711 } else if (live_timer_interval
&&
2712 live_timer_interval
!= UINT64_MAX
) {
2713 ret
= create_session((const char *) name
, kernel_domain
,
2714 ust_domain
, jul_domain
, log4j_domain
,
2715 output_node
, live_timer_interval
);
2717 /* regular session */
2718 ret
= create_session((const char *) name
, kernel_domain
,
2719 ust_domain
, jul_domain
, log4j_domain
,
2720 output_node
, UINT64_MAX
);
2727 ret
= lttng_set_session_shm_path((const char *) name
,
2728 (const char *) shm_path
);
2734 for (node
= xmlFirstElementChild(domains_node
); node
;
2735 node
= xmlNextElementSibling(node
)) {
2736 ret
= process_domain_node(node
, (const char *) name
,
2737 snapshot_mode
== 1);
2744 ret
= lttng_start_tracing((const char *) name
);
2752 ERR("Failed to load session %s: %s", (const char *) name
,
2753 lttng_strerror(ret
));
2754 lttng_destroy_session((const char *) name
);
2758 free(kernel_domain
);
2762 free(python_domain
);
2769 * Return 1 if the given path is readable by the current UID or 0 if not.
2770 * Return -1 if the path is EPERM.
2772 static int validate_file_read_creds(const char *path
)
2778 /* Can we read the file. */
2779 ret
= access(path
, R_OK
);
2783 if (errno
== EACCES
) {
2794 int load_session_from_file(const char *path
, const char *session_name
,
2795 struct session_config_validation_ctx
*validation_ctx
, int override
)
2797 int ret
, session_found
= !session_name
;
2798 xmlDocPtr doc
= NULL
;
2799 xmlNodePtr sessions_node
;
2800 xmlNodePtr session_node
;
2803 assert(validation_ctx
);
2805 ret
= validate_file_read_creds(path
);
2808 ret
= -LTTNG_ERR_EPERM
;
2810 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2815 doc
= xmlParseFile(path
);
2817 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2821 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2823 ERR("Session configuration file validation failed");
2824 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2828 sessions_node
= xmlDocGetRootElement(doc
);
2829 if (!sessions_node
) {
2833 for (session_node
= xmlFirstElementChild(sessions_node
);
2834 session_node
; session_node
=
2835 xmlNextElementSibling(session_node
)) {
2836 ret
= process_session_node(session_node
,
2837 session_name
, override
);
2838 if (session_name
&& ret
== 0) {
2839 /* Target session found and loaded */
2847 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2853 int load_session_from_document(struct config_document
*document
, const char *session_name
,
2854 struct session_config_validation_ctx
*validation_ctx
, int override
)
2856 int ret
, session_found
= !session_name
;
2857 xmlNodePtr sessions_node
;
2858 xmlNodePtr session_node
;
2860 assert(validation_ctx
);
2862 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, document
->document
);
2864 ERR("Session configuration file validation failed");
2865 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2869 sessions_node
= xmlDocGetRootElement(document
->document
);
2870 if (!sessions_node
) {
2874 for (session_node
= xmlFirstElementChild(sessions_node
);
2875 session_node
; session_node
=
2876 xmlNextElementSibling(session_node
)) {
2877 ret
= process_session_node(session_node
,
2878 session_name
, override
);
2879 if (session_name
&& ret
== 0) {
2880 /* Target session found and loaded */
2887 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2892 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2894 struct dirent
*alloc_dirent(const char *path
)
2898 struct dirent
*entry
;
2900 name_max
= pathconf(path
, _PC_NAME_MAX
);
2901 if (name_max
== -1) {
2902 name_max
= PATH_MAX
;
2904 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2905 entry
= zmalloc(len
);
2910 int load_session_from_path(const char *path
, const char *session_name
,
2911 struct session_config_validation_ctx
*validation_ctx
, int override
)
2913 int ret
, session_found
= !session_name
;
2914 DIR *directory
= NULL
;
2917 assert(validation_ctx
);
2919 directory
= opendir(path
);
2923 /* Try the file loading. */
2926 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2929 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2934 struct dirent
*entry
;
2935 struct dirent
*result
;
2936 char *file_path
= NULL
;
2937 size_t path_len
= strlen(path
);
2939 if (path_len
>= PATH_MAX
) {
2940 ret
= -LTTNG_ERR_INVALID
;
2944 entry
= alloc_dirent(path
);
2946 ret
= -LTTNG_ERR_NOMEM
;
2950 file_path
= zmalloc(PATH_MAX
);
2952 ret
= -LTTNG_ERR_NOMEM
;
2957 strncpy(file_path
, path
, path_len
);
2958 if (file_path
[path_len
- 1] != '/') {
2959 file_path
[path_len
++] = '/';
2963 /* Search for *.lttng files */
2964 while (!readdir_r(directory
, entry
, &result
) && result
) {
2965 size_t file_name_len
= strlen(result
->d_name
);
2967 if (file_name_len
<=
2968 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2972 if (path_len
+ file_name_len
>= PATH_MAX
) {
2976 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2977 result
->d_name
+ file_name_len
- sizeof(
2978 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2982 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2983 file_path
[path_len
+ file_name_len
] = '\0';
2985 ret
= load_session_from_file(file_path
, session_name
,
2986 validation_ctx
, override
);
2987 if (session_name
&& !ret
) {
2996 ret
= load_session_from_file(path
, session_name
,
2997 validation_ctx
, override
);
3007 if (closedir(directory
)) {
3012 if (session_found
) {
3020 * Validate that the given path's credentials and the current process have the
3021 * same UID. If so, return 1 else return 0 if it does NOT match.
3023 static int validate_path_creds(const char *path
)
3025 int ret
, uid
= getuid();
3034 ret
= stat(path
, &buf
);
3036 if (errno
!= ENOENT
) {
3039 ret
= -LTTNG_ERR_INVALID
;
3043 if (buf
.st_uid
!= uid
) {
3054 int config_load_session(const char *path
, const char *session_name
,
3055 int override
, unsigned int autoload
)
3058 bool session_loaded
= false;
3059 const char *path_ptr
= NULL
;
3060 struct session_config_validation_ctx validation_ctx
= { 0 };
3062 ret
= init_session_config_validation_ctx(&validation_ctx
);
3069 const char *sys_path
;
3072 home_path
= utils_get_home_dir();
3074 char path
[PATH_MAX
];
3077 * Try user session configuration path. Ignore error here so we can
3078 * continue loading the system wide sessions.
3081 ret
= snprintf(path
, sizeof(path
),
3082 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3083 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3085 PERROR("snprintf session autoload home config path");
3090 * Credentials are only validated for the autoload in order to
3091 * avoid any user session daemon to try to load kernel sessions
3092 * automatically and failing all the times.
3094 ret
= validate_path_creds(path
);
3099 ret
= snprintf(path
, sizeof(path
),
3100 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3102 PERROR("snprintf session home config path");
3108 ret
= load_session_from_path(path_ptr
, session_name
,
3109 &validation_ctx
, override
);
3110 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3114 * Continue even if the session was found since we have to try
3115 * the system wide sessions.
3117 session_loaded
= true;
3121 /* Reset path pointer for the system wide dir. */
3124 /* Try system wide configuration directory. */
3126 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3127 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3128 ret
= validate_path_creds(sys_path
);
3130 path_ptr
= sys_path
;
3133 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3134 path_ptr
= sys_path
;
3138 ret
= load_session_from_path(path_ptr
, session_name
,
3139 &validation_ctx
, override
);
3141 session_loaded
= true;
3145 ret
= access(path
, F_OK
);
3150 ret
= -LTTNG_ERR_INVALID
;
3151 WARN("Session configuration path does not exist.");
3154 ret
= -LTTNG_ERR_EPERM
;
3157 ret
= -LTTNG_ERR_UNK
;
3163 ret
= load_session_from_path(path
, session_name
,
3164 &validation_ctx
, override
);
3167 fini_session_config_validation_ctx(&validation_ctx
);
3168 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3170 * Don't report an error if no sessions are found when called
3171 * without a session_name or a search path.
3176 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3177 /* A matching session was found in one of the search paths. */
3184 void __attribute__((destructor
)) session_config_exit(void)
3190 struct config_document
*config_document_get(const char *path
, int xsd_validation
)
3193 struct config_document
*document
= NULL
;
3194 struct session_config_validation_ctx validation_ctx
= { 0 };
3198 ret
= access(path
, F_OK
);
3203 ERR("Configuration document path does not exist.");
3206 ERR("Configuration document permission denied.");
3209 ERR("Configuration document unknown error.");
3215 ret
= init_session_config_validation_ctx(&validation_ctx
);
3220 ret
= validate_file_read_creds(path
);
3223 ERR("Configuration document permission denied.");
3225 ERR("Configuration document does not exist.");
3230 document
= zmalloc(sizeof(struct config_document
));
3237 * Do not care of blanks and also make sure that direct dump have no
3238 * indentations in them
3240 xmlKeepBlanksDefault(0);
3241 document
->document
= xmlParseFile(path
);
3242 if (!document
->document
) {
3243 ERR("Configuration document parsing failed.");
3249 if (xsd_validation
) {
3250 ret
= xmlSchemaValidateDoc(validation_ctx
.schema_validation_ctx
,
3251 document
->document
);
3254 ERR("Session configuration file validation failed");
3259 fini_session_config_validation_ctx(&validation_ctx
);
3262 xmlFreeDoc(document
->document
);
3268 void config_document_free(struct config_document
*document
)
3271 xmlFreeDoc(document
->document
);
3272 document
->document
= NULL
;
3279 int config_document_replace_element_value(struct config_document
*document
,
3280 const char *xpath
, const char *value
)
3283 int xpath_result_size
;
3285 xmlXPathContextPtr xpath_context
= NULL
;
3286 xmlXPathObjectPtr xpath_object
= NULL
;
3287 xmlNodeSetPtr xpath_result_set
= NULL
;
3288 xmlChar
*internal_xpath
= NULL
;
3289 xmlChar
*internal_value
= NULL
;
3295 internal_xpath
= encode_string(xpath
);
3296 if (!internal_xpath
) {
3297 ret
= -LTTNG_ERR_NOMEM
;
3298 ERR("Unable to encode xpath string");
3302 internal_value
= encode_string(value
);
3303 if (!internal_value
) {
3304 ret
= -LTTNG_ERR_NOMEM
;
3305 ERR("Unable to encode xpath replace value string");
3309 /* Initialize xpath context */
3310 xpath_context
= xmlXPathNewContext(document
->document
);
3311 if (!xpath_context
) {
3312 ret
= -LTTNG_ERR_NOMEM
;
3313 ERR("Unable to create xpath context");
3317 /* Evaluate de xpath expression */
3318 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3319 if (!xpath_object
) {
3320 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3324 /* TODO: from here could be extracted and previopus could be a step in
3325 * subsequent operation, modify/substitute/delete node.
3327 xpath_result_set
= xpath_object
->nodesetval
;
3328 if (!xpath_result_set
) {
3329 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3333 xpath_result_size
= xpath_result_set
->nodeNr
;
3336 * Reverse traversal since last element could be nested under parent
3337 * element present in the result set.
3339 for (int i
= xpath_result_size
- 1; i
>=0; i
--) {
3340 assert(xpath_result_set
->nodeTab
[i
]);
3341 xmlNodeSetContent(xpath_result_set
->nodeTab
[i
], internal_value
);
3344 * Libxml2 quirk regarding the freing and namesplace node see
3345 * libxml2 example and documentation for more details.
3347 if (xpath_result_set
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) {
3348 xpath_result_set
->nodeTab
[i
] = NULL
;
3353 xmlXPathFreeContext(xpath_context
);
3354 xmlXPathFreeObject(xpath_object
);
3355 xmlFree(internal_xpath
);
3356 xmlFree(internal_value
);
3361 int config_load_configuration_sessions(struct config_document
*document
,
3362 const char *session_name
, int override
)
3365 struct session_config_validation_ctx validation_ctx
= { 0 };
3367 ret
= init_session_config_validation_ctx(&validation_ctx
);
3371 ret
= load_session_from_document(document
, session_name
,
3372 &validation_ctx
, override
);
3374 fini_session_config_validation_ctx(&validation_ctx
);
3379 int config_document_replace_element(struct config_document
*document
,
3380 const char *xpath
, const struct config_element
*element
)
3383 int xpath_result_size
;
3385 xmlXPathContextPtr xpath_context
= NULL
;
3386 xmlXPathObjectPtr xpath_object
= NULL
;
3387 xmlNodeSetPtr xpath_result_set
= NULL
;
3388 xmlChar
*internal_xpath
= NULL
;
3389 xmlNodePtr old_node
= NULL
;
3390 xmlNodePtr copy
= NULL
;
3395 assert(element
->element
);
3397 internal_xpath
= encode_string(xpath
);
3398 if (!internal_xpath
) {
3399 ret
= -LTTNG_ERR_NOMEM
;
3400 ERR("Unable to encode xpath string");
3404 /* Initialize xpath context */
3405 xpath_context
= xmlXPathNewContext(document
->document
);
3406 if (!xpath_context
) {
3407 ret
= -LTTNG_ERR_NOMEM
;
3408 ERR("Unable to create xpath context");
3412 /* Evaluate the xpath expression */
3413 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3414 if (!xpath_object
) {
3415 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3419 xpath_result_set
= xpath_object
->nodesetval
;
3420 if (!xpath_result_set
) {
3421 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3425 xpath_result_size
= xpath_result_set
->nodeNr
;
3427 if (xpath_result_size
> 1) {
3428 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3432 if (xpath_result_size
== 0) {
3433 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3437 assert(xpath_result_set
->nodeTab
[0]);
3439 /* Do a copy of the element to ease caller memory management */
3440 copy
= xmlCopyNode(element
->element
, 1);
3442 ret
= -LTTNG_ERR_NOMEM
;
3446 old_node
= xmlReplaceNode(xpath_result_set
->nodeTab
[0], copy
);
3447 if (xpath_result_set
->nodeTab
[0]->type
!= XML_NAMESPACE_DECL
)
3448 xpath_result_set
->nodeTab
[0] = NULL
;
3450 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3454 xmlUnlinkNode(old_node
);
3455 xmlFreeNode(old_node
);
3457 xmlXPathFreeContext(xpath_context
);
3458 xmlXPathFreeObject(xpath_object
);
3459 xmlFree(internal_xpath
);
3464 char *config_document_get_element_value(struct config_document
*document
,
3468 int xpath_result_size
;
3469 int value_result_size
;
3471 xmlXPathContextPtr xpath_context
= NULL
;
3472 xmlXPathObjectPtr xpath_object
= NULL
;
3473 xmlNodeSetPtr xpath_result_set
= NULL
;
3474 xmlChar
*internal_xpath
= NULL
;
3475 xmlChar
*internal_value
= NULL
;
3480 internal_xpath
= encode_string(xpath
);
3481 if (!internal_xpath
) {
3483 ERR("Unable to encode xpath string");
3487 /* Initialize xpath context */
3488 xpath_context
= xmlXPathNewContext(document
->document
);
3489 if (!xpath_context
) {
3491 ERR("Unable to create xpath context");
3495 /* Evaluate the xpath expression */
3496 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3497 if (!xpath_object
) {
3499 ERR("Unable to evaluate xpath query (invalid query format)");
3503 xpath_result_set
= xpath_object
->nodesetval
;
3504 if (!xpath_result_set
) {
3509 xpath_result_size
= xpath_result_set
->nodeNr
;
3511 if (xpath_result_size
> 1) {
3512 ERR("To many result while fetching config element value");
3517 if (xpath_result_size
== 0) {
3523 * Reverse traversal since last element could be nested under parent
3524 * element present in the result set.
3526 assert(xpath_result_set
->nodeTab
[0]);
3527 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
3528 if (!internal_value
) {
3533 value_result_size
= xmlStrlen(internal_value
);
3534 value
= calloc(value_result_size
+ 1, sizeof(char));
3535 strncpy(value
, (char *) internal_value
, value_result_size
);
3538 xmlXPathFreeContext(xpath_context
);
3539 xmlXPathFreeObject(xpath_object
);
3540 xmlFree(internal_xpath
);
3541 xmlFree(internal_value
);
3546 int config_document_element_exist(struct config_document
*document
,
3550 int xpath_result_size
;
3552 xmlXPathContextPtr xpath_context
= NULL
;
3553 xmlXPathObjectPtr xpath_object
= NULL
;
3554 xmlNodeSetPtr xpath_result_set
= NULL
;
3555 xmlChar
*internal_xpath
= NULL
;
3558 assert(document
->document
);
3561 internal_xpath
= encode_string(xpath
);
3562 if (!internal_xpath
) {
3563 ERR("Unable to encode xpath string");
3567 /* Initialize xpath context */
3568 xpath_context
= xmlXPathNewContext(document
->document
);
3569 if (!xpath_context
) {
3570 ERR("Unable to create xpath context");
3574 /* Evaluate the xpath expression */
3575 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3576 if (!xpath_object
) {
3577 ERR("Unable to evaluate xpath query (invalid query format)");
3581 xpath_result_set
= xpath_object
->nodesetval
;
3582 if (!xpath_result_set
) {
3586 xpath_result_size
= xpath_result_set
->nodeNr
;
3588 if (xpath_result_size
> 0) {
3592 xmlXPathFreeContext(xpath_context
);
3593 xmlXPathFreeObject(xpath_object
);
3594 xmlFree(internal_xpath
);
3600 struct config_element
*config_element_create(const char *name
,
3603 struct config_element
*element
;
3607 xmlChar
*internal_name
= NULL
;
3608 xmlChar
*internal_value
= NULL
;
3610 internal_name
= encode_string(name
);
3611 if (!internal_name
) {
3612 ERR("Unable to encode config element name string");
3619 internal_value
= encode_string(value
);
3620 if (!internal_value
) {
3621 ERR("Unable to encode config_element value string");
3627 element
= zmalloc(sizeof(struct config_element
));
3632 element
->element
= xmlNewNode(NULL
, internal_name
);
3633 if (!element
->element
) {
3639 if (internal_value
) {
3640 xmlNodeAddContent(element
->element
, internal_value
);
3643 xmlFree(internal_name
);
3644 xmlFree(internal_value
);
3649 int config_element_add_child(struct config_element
*parent
,
3650 const struct config_element
*child
)
3654 assert(parent
->element
);
3655 assert(child
->element
);
3658 xmlNodePtr node
= NULL
;
3659 xmlNodePtr copy
= NULL
;
3661 /* Do a copy to ease the memory management for caller */
3662 copy
= xmlCopyNode(child
->element
, 1);
3664 ret
= -LTTNG_ERR_NOMEM
;
3667 node
= xmlAddChild(parent
->element
, copy
);
3670 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3679 int config_document_insert_element(struct config_document
*document
,
3680 const char *xpath
, const struct config_element
*element
)
3683 int xpath_result_size
;
3685 xmlXPathContextPtr xpath_context
= NULL
;
3686 xmlXPathObjectPtr xpath_object
= NULL
;
3687 xmlNodeSetPtr xpath_result_set
= NULL
;
3688 xmlChar
*internal_xpath
= NULL
;
3689 xmlNodePtr child_node
= NULL
;
3690 xmlNodePtr local_copy
= NULL
;
3695 assert(element
->element
);
3697 internal_xpath
= encode_string(xpath
);
3698 if (!internal_xpath
) {
3699 ret
= -LTTNG_ERR_NOMEM
;
3703 /* Initialize xpath context */
3704 xpath_context
= xmlXPathNewContext(document
->document
);
3705 if (!xpath_context
) {
3706 ret
= -LTTNG_ERR_NOMEM
;
3710 /* Evaluate the xpath expression */
3711 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3712 if (!xpath_object
) {
3713 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3717 xpath_result_set
= xpath_object
->nodesetval
;
3718 if (!xpath_result_set
) {
3719 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3723 xpath_result_size
= xpath_result_set
->nodeNr
;
3725 if (xpath_result_size
> 1) {
3726 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3730 if (xpath_result_size
== 0) {
3731 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3735 assert(xpath_result_set
->nodeTab
[0]);
3736 /* Do a copy to simply memory management */
3737 local_copy
= xmlCopyNode(element
->element
, 1);
3739 ret
= -LTTNG_ERR_NOMEM
;
3743 child_node
= xmlAddChild(xpath_result_set
->nodeTab
[0], local_copy
);
3745 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3746 xmlFreeNode(local_copy
);
3750 xmlXPathFreeContext(xpath_context
);
3751 xmlXPathFreeObject(xpath_object
);
3752 xmlFree(internal_xpath
);
3757 void config_element_free(struct config_element
*element
) {
3758 if (element
&& element
->element
) {
3759 xmlFreeNode(element
->element
);
3766 void config_element_free_array(struct config_element
**array
, int size
) {
3769 for (int i
= 0; i
< size
; i
++) {
3773 if (array
[i
]->element
) {
3774 xmlFreeNode(array
[i
]->element
);
3783 void config_document_get_element_array(const struct config_document
*document
, const char *xpath
, struct config_element
***element_array
, int *array_size
)
3785 int xpath_result_size
;
3787 xmlXPathContextPtr xpath_context
= NULL
;
3788 xmlXPathObjectPtr xpath_object
= NULL
;
3789 xmlNodeSetPtr xpath_result_set
= NULL
;
3790 xmlChar
*internal_xpath
= NULL
;
3791 xmlChar
*internal_value
= NULL
;
3796 *element_array
=NULL
;
3800 internal_xpath
= encode_string(xpath
);
3801 if (!internal_xpath
) {
3802 ERR("Unable to encode xpath string");
3806 /* Initialize xpath context */
3807 xpath_context
= xmlXPathNewContext(document
->document
);
3808 if (!xpath_context
) {
3809 ERR("Unable to create xpath context");
3813 /* Evaluate the xpath expression */
3814 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3815 if (!xpath_object
) {
3816 ERR("Unable to evaluate xpath query (invalid query format)");
3820 xpath_result_set
= xpath_object
->nodesetval
;
3821 if (!xpath_result_set
) {
3825 xpath_result_size
= xpath_result_set
->nodeNr
;
3827 if (xpath_result_size
== 0) {
3831 /* alloc the array */
3832 *element_array
= calloc(xpath_result_size
, sizeof(struct config_element
*));
3833 *array_size
= xpath_result_size
;
3834 if (!element_array
) {
3835 ERR("Calloc config element array");
3839 for (int i
= 0; i
< xpath_result_size
; i
++) {
3840 xmlNodePtr node_copy
= NULL
;
3841 node_copy
= xmlCopyNode(xpath_result_set
->nodeTab
[i
], 1);
3843 ERR("Xml copy of event node");
3846 /* Hand off ownership */
3847 (*element_array
)[i
] = malloc(sizeof(struct config_element
));
3848 if (!((*element_array
)[i
])){
3849 ERR("Malloc config_element array");
3850 xmlFreeNode(node_copy
);
3853 (*element_array
)[i
]->element
= node_copy
;
3856 xmlXPathFreeContext(xpath_context
);
3857 xmlXPathFreeObject(xpath_object
);
3858 xmlFree(internal_xpath
);
3859 xmlFree(internal_value
);
3863 if (element_array
) {
3864 config_element_free_array(*element_array
, *array_size
);
3869 xmlXPathFreeContext(xpath_context
);
3870 xmlXPathFreeObject(xpath_object
);
3871 xmlFree(internal_xpath
);
3872 xmlFree(internal_value
);
3877 void config_element_get_element_array(const struct config_element
*element
, const char *xpath
, struct config_element
***element_array
, int *array_size
)
3879 int xpath_result_size
;
3881 xmlXPathContextPtr xpath_context
= NULL
;
3882 xmlXPathObjectPtr xpath_object
= NULL
;
3883 xmlNodeSetPtr xpath_result_set
= NULL
;
3884 xmlChar
*internal_xpath
= NULL
;
3885 xmlChar
*internal_value
= NULL
;
3886 xmlNodePtr local_copy
= NULL
;
3887 xmlNodePtr place_holder
= NULL
;
3888 xmlDocPtr document
= NULL
;
3891 assert(element
->element
);
3894 *element_array
=NULL
;
3897 /* Create a new doc and set root node as passed element */
3898 document
= xmlNewDoc(BAD_CAST
"1.0");
3900 ERR("Unable to create xml document");
3904 local_copy
= xmlCopyNode(element
->element
, 1);
3906 ERR("Unable to copy an xml node");
3910 /* Hand off the local_copy to the xmlDoc */
3911 place_holder
= xmlDocSetRootElement(document
, local_copy
);
3913 ERR("The document root was already set");
3914 xmlFreeNode(local_copy
);
3918 internal_xpath
= encode_string(xpath
);
3919 if (!internal_xpath
) {
3920 ERR("Unable to encode xpath string");
3924 /* Initialize xpath context */
3925 xpath_context
= xmlXPathNewContext(document
);
3926 if (!xpath_context
) {
3927 ERR("Unable to create xpath context");
3931 /* Evaluate the xpath expression */
3932 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3933 if (!xpath_object
) {
3934 ERR("Unable to evaluate xpath query (invalid query format)");
3938 xpath_result_set
= xpath_object
->nodesetval
;
3939 if (!xpath_result_set
) {
3943 xpath_result_size
= xpath_result_set
->nodeNr
;
3945 if (xpath_result_size
== 0) {
3949 /* alloc the array */
3950 *element_array
= calloc(xpath_result_size
, sizeof(struct config_element
*));
3951 *array_size
= xpath_result_size
;
3952 if (!element_array
) {
3953 ERR("Calloc config element array");
3957 for (int i
= 0; i
< xpath_result_size
; i
++) {
3958 xmlNodePtr node_copy
= NULL
;
3959 node_copy
= xmlCopyNode(xpath_result_set
->nodeTab
[i
], 1);
3961 ERR("Xml copy of event node");
3964 /* Hand off ownership */
3965 (*element_array
)[i
] = malloc(sizeof(struct config_element
));
3966 if (!((*element_array
)[i
])){
3967 ERR("Malloc config_element array");
3968 xmlFreeNode(node_copy
);
3971 (*element_array
)[i
]->element
= node_copy
;
3974 xmlXPathFreeContext(xpath_context
);
3975 xmlXPathFreeObject(xpath_object
);
3976 xmlFree(internal_xpath
);
3977 xmlFree(internal_value
);
3978 xmlFreeDoc(document
);
3982 if (element_array
) {
3983 config_element_free_array(*element_array
, *array_size
);
3988 xmlXPathFreeContext(xpath_context
);
3989 xmlXPathFreeObject(xpath_object
);
3990 xmlFree(internal_xpath
);
3991 xmlFree(internal_value
);
3994 /* Create an internal copy of the config_element.
3995 * The users is responsible for the freeing of the passed child.
3998 int config_element_add_or_replace_child(struct config_element
*parent
, struct config_element
*child
)
4002 assert(parent
->element
);
4003 assert(child
->element
);
4006 xmlNodePtr local_copy
;
4007 xmlNodePtr x_ret
= NULL
;
4008 xmlNodePtr iterator
= NULL
;
4010 xmlNodePtr x_parent
= parent
->element
;
4011 xmlNodePtr x_child
= child
->element
;
4013 /* Find and delete the node with the same name if present */
4014 iterator
= x_parent
->children
;
4015 while (iterator
!= NULL
) {
4016 if ((!xmlStrcmp(iterator
->name
, x_child
->name
))){
4017 xmlNodePtr del
= iterator
;
4018 iterator
= iterator
->next
;
4022 iterator
= iterator
->next
;
4027 local_copy
= xmlCopyNode(x_child
, 1);
4029 ERR("Duplication of node to be insert failed");
4030 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
4033 x_ret
= xmlAddChild(x_parent
, local_copy
);
4035 xmlFreeNode(local_copy
);
4036 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
4043 char *config_element_get_element_value(const struct config_element
*element
, const char *xpath
)
4046 int xpath_result_size
;
4047 int value_result_size
;
4049 xmlXPathContextPtr xpath_context
= NULL
;
4050 xmlXPathObjectPtr xpath_object
= NULL
;
4051 xmlNodeSetPtr xpath_result_set
= NULL
;
4052 xmlChar
*internal_xpath
= NULL
;
4053 xmlChar
*internal_value
= NULL
;
4054 xmlDocPtr document
= NULL
;
4055 xmlNodePtr local_copy
= NULL
;
4056 xmlNodePtr place_holder
= NULL
;
4059 assert(element
->element
);
4062 /* Create a new doc and set root node as passed element */
4063 document
= xmlNewDoc(BAD_CAST
"1.0");
4066 ERR("Unable to create xml document");
4070 local_copy
= xmlCopyNode(element
->element
, 1);
4073 ERR("Unable to copy an xml node");
4077 /* Hand off the local_copy to the xmlDoc */
4078 place_holder
= xmlDocSetRootElement(document
, local_copy
);
4081 ERR("The document root was already set");
4082 xmlFreeNode(local_copy
);
4086 internal_xpath
= encode_string(xpath
);
4087 if (!internal_xpath
) {
4088 /*TODO set valid error */
4090 ERR("Unable to encode xpath string");
4094 /* Initialize xpath context */
4095 xpath_context
= xmlXPathNewContext(document
);
4096 if (!xpath_context
) {
4097 /*TODO set valid error */
4099 ERR("Unable to create xpath context");
4103 /* Evaluate the xpath expression */
4104 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
4105 if (!xpath_object
) {
4106 /*TODO set valid error */
4108 ERR("Unable to evaluate xpath query (invalid query format)");
4112 xpath_result_set
= xpath_object
->nodesetval
;
4113 if (!xpath_result_set
) {
4114 /*TODO set valid error */
4119 xpath_result_size
= xpath_result_set
->nodeNr
;
4121 if (xpath_result_size
> 1) {
4122 /*TODO set valid error */
4123 ERR("To many result while fetching config element value");
4128 if (xpath_result_size
== 0) {
4133 assert(xpath_result_set
->nodeTab
[0]);
4134 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
4135 if (!internal_value
) {
4140 value_result_size
= xmlStrlen(internal_value
);
4141 value
= calloc(value_result_size
+ 1, sizeof(char));
4142 strncpy(value
, (char *) internal_value
, value_result_size
);
4145 xmlXPathFreeContext(xpath_context
);
4146 xmlXPathFreeObject(xpath_object
);
4147 xmlFree(internal_xpath
);
4148 xmlFree(internal_value
);
4149 xmlFreeDoc(document
);
4155 int config_process_event_element(const struct config_element
*element
, const char *session_name
, int domain_type
, const char *channel_name
) {
4159 struct lttng_domain domain
;
4160 struct lttng_handle
*handle
;
4163 assert(element
->element
);
4165 memset(&domain
, 0, sizeof(struct lttng_domain
));
4168 domain
.type
= domain_type
;
4169 switch (domain
.type
) {
4170 case LTTNG_DOMAIN_KERNEL
:
4171 domain
.buf_type
= LTTNG_BUFFER_GLOBAL
;
4173 case LTTNG_DOMAIN_UST
:
4174 case LTTNG_DOMAIN_JUL
:
4175 case LTTNG_DOMAIN_LOG4J
:
4176 case LTTNG_DOMAIN_PYTHON
:
4177 domain
.buf_type
= LTTNG_BUFFER_PER_UID
;
4179 case LTTNG_DOMAIN_NONE
:
4184 handle
= lttng_create_handle(session_name
, &domain
);
4186 ret
= -LTTNG_ERR_HANDLE_CREATION
;
4191 node
= element
->element
;
4192 /* Check if element is really and event */
4193 if (xmlStrcmp(BAD_CAST config_element_event
, node
->name
)) {
4194 ret
= -LTTNG_ERR_CONFIG_INVALID_ELEMENT
;
4198 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);