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 int config_get_section_entries(const char *override_path
, const char *section
,
225 config_entry_handler_cb handler
, void *user_data
)
229 FILE *config_file
= NULL
;
230 struct handler_filter_args filter
= { section
, handler
, user_data
};
232 /* First, try system-wide conf. file. */
233 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
235 config_file
= fopen(path
, "r");
237 DBG("Loading daemon conf file at %s", path
);
239 * Return value is not very important here since error or not, we
240 * continue and try the next possible conf. file.
242 (void) ini_parse_file(config_file
,
243 (ini_entry_handler
) config_entry_handler_filter
,
248 /* Second is the user local configuration. */
249 path
= utils_get_home_dir();
251 char fullpath
[PATH_MAX
];
253 ret
= snprintf(fullpath
, sizeof(fullpath
),
254 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
256 PERROR("snprintf user conf. path");
260 config_file
= fopen(fullpath
, "r");
262 DBG("Loading daemon user conf file at %s", path
);
264 * Return value is not very important here since error or not, we
265 * continue and try the next possible conf. file.
267 (void) ini_parse_file(config_file
,
268 (ini_entry_handler
) config_entry_handler_filter
,
274 /* Final path is the one that the user might have provided. */
276 config_file
= fopen(override_path
, "r");
278 DBG("Loading daemon command line conf file at %s", override_path
);
279 (void) ini_parse_file(config_file
,
280 (ini_entry_handler
) config_entry_handler_filter
,
284 ERR("Failed to open daemon configuration file at %s",
291 /* Everything went well. */
299 int config_parse_value(const char *value
)
302 char *endptr
, *lower_str
;
312 v
= strtoul(value
, &endptr
, 10);
313 if (endptr
!= value
) {
318 lower_str
= zmalloc(len
+ 1);
325 for (i
= 0; i
< len
; i
++) {
326 lower_str
[i
] = tolower(value
[i
]);
329 if (!strcmp(lower_str
, config_str_yes
) ||
330 !strcmp(lower_str
, config_str_true
) ||
331 !strcmp(lower_str
, config_str_on
)) {
333 } else if (!strcmp(lower_str
, config_str_no
) ||
334 !strcmp(lower_str
, config_str_false
) ||
335 !strcmp(lower_str
, config_str_off
)) {
347 * Returns a xmlChar string which must be released using xmlFree().
349 static xmlChar
*encode_string(const char *in_str
)
351 xmlChar
*out_str
= NULL
;
352 xmlCharEncodingHandlerPtr handler
;
353 int out_len
, ret
, in_len
;
357 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
359 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
363 in_len
= strlen(in_str
);
365 * Add 1 byte for the NULL terminted character. The factor 4 here is
366 * used because UTF-8 characters can take up to 4 bytes.
368 out_len
= (in_len
* 4) + 1;
369 out_str
= xmlMalloc(out_len
);
374 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
381 /* out_len is now the size of out_str */
382 out_str
[out_len
] = '\0';
388 struct config_writer
*config_writer_create(int fd_output
, int indent
)
391 struct config_writer
*writer
;
392 xmlOutputBufferPtr buffer
;
394 writer
= zmalloc(sizeof(struct config_writer
));
396 PERROR("zmalloc config_writer_create");
400 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
405 writer
->writer
= xmlNewTextWriter(buffer
);
406 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
407 config_xml_encoding
, NULL
);
412 ret
= xmlTextWriterSetIndentString(writer
->writer
,
413 BAD_CAST config_xml_indent_string
);
418 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
426 config_writer_destroy(writer
);
431 int config_writer_destroy(struct config_writer
*writer
)
440 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
441 WARN("Could not close XML document");
445 if (writer
->writer
) {
446 xmlFreeTextWriter(writer
->writer
);
455 int config_writer_open_element(struct config_writer
*writer
,
456 const char *element_name
)
459 xmlChar
*encoded_element_name
;
461 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
466 encoded_element_name
= encode_string(element_name
);
467 if (!encoded_element_name
) {
472 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
473 xmlFree(encoded_element_name
);
475 return ret
>= 0 ? 0 : ret
;
479 int config_writer_write_attribute(struct config_writer
*writer
,
480 const char *name
, const char *value
)
483 xmlChar
*encoded_name
= NULL
;
484 xmlChar
*encoded_value
= NULL
;
486 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
491 encoded_name
= encode_string(name
);
497 encoded_value
= encode_string(value
);
498 if (!encoded_value
) {
503 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
506 xmlFree(encoded_name
);
507 xmlFree(encoded_value
);
508 return ret
>= 0 ? 0 : ret
;
512 int config_writer_close_element(struct config_writer
*writer
)
516 if (!writer
|| !writer
->writer
) {
521 ret
= xmlTextWriterEndElement(writer
->writer
);
523 return ret
>= 0 ? 0 : ret
;
527 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
528 const char *element_name
, uint64_t value
)
531 xmlChar
*encoded_element_name
;
533 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
538 encoded_element_name
= encode_string(element_name
);
539 if (!encoded_element_name
) {
544 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
545 encoded_element_name
, "%" PRIu64
, value
);
546 xmlFree(encoded_element_name
);
548 return ret
>= 0 ? 0 : ret
;
552 int config_writer_write_element_signed_int(struct config_writer
*writer
,
553 const char *element_name
, int64_t value
)
556 xmlChar
*encoded_element_name
;
558 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
563 encoded_element_name
= encode_string(element_name
);
564 if (!encoded_element_name
) {
569 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
570 encoded_element_name
, "%" PRIi64
, value
);
571 xmlFree(encoded_element_name
);
573 return ret
>= 0 ? 0 : ret
;
577 int config_writer_write_element_bool(struct config_writer
*writer
,
578 const char *element_name
, int value
)
580 return config_writer_write_element_string(writer
, element_name
,
581 value
? config_xml_true
: config_xml_false
);
585 int config_writer_write_element_string(struct config_writer
*writer
,
586 const char *element_name
, const char *value
)
589 xmlChar
*encoded_element_name
= NULL
;
590 xmlChar
*encoded_value
= NULL
;
592 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
598 encoded_element_name
= encode_string(element_name
);
599 if (!encoded_element_name
) {
604 encoded_value
= encode_string(value
);
605 if (!encoded_value
) {
610 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
613 xmlFree(encoded_element_name
);
614 xmlFree(encoded_value
);
615 return ret
>= 0 ? 0 : ret
;
619 void xml_error_handler(void *ctx
, const char *format
, ...)
625 va_start(args
, format
);
626 ret
= vasprintf(&errMsg
, format
, args
);
629 ERR("String allocation failed in xml error handler");
633 fprintf(stderr
, "XML Error: %s", errMsg
);
638 void fini_session_config_validation_ctx(
639 struct session_config_validation_ctx
*ctx
)
641 if (ctx
->parser_ctx
) {
642 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
646 xmlSchemaFree(ctx
->schema
);
649 if (ctx
->schema_validation_ctx
) {
650 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
653 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
657 char *get_session_config_xsd_path()
660 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
661 size_t base_path_len
;
665 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
668 base_path_len
= strlen(base_path
);
669 max_path_len
= base_path_len
+
670 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
671 xsd_path
= zmalloc(max_path_len
);
676 strncpy(xsd_path
, base_path
, max_path_len
);
677 if (xsd_path
[base_path_len
- 1] != '/') {
678 xsd_path
[base_path_len
++] = '/';
681 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
682 max_path_len
- base_path_len
);
688 int init_session_config_validation_ctx(
689 struct session_config_validation_ctx
*ctx
)
692 char *xsd_path
= get_session_config_xsd_path();
695 ret
= -LTTNG_ERR_NOMEM
;
699 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
700 if (!ctx
->parser_ctx
) {
701 ERR("XSD parser context creation failed");
702 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
705 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
706 xml_error_handler
, NULL
);
708 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
710 ERR("XSD parsing failed");
711 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
715 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
716 if (!ctx
->schema_validation_ctx
) {
717 ERR("XSD validation context creation failed");
718 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
722 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
723 xml_error_handler
, NULL
);
728 fini_session_config_validation_ctx(ctx
);
736 int parse_uint(xmlChar
*str
, uint64_t *val
)
746 *val
= strtoull((const char *) str
, &endptr
, 10);
747 if (!endptr
|| *endptr
) {
758 int parse_int(xmlChar
*str
, int64_t *val
)
768 *val
= strtoll((const char *) str
, &endptr
, 10);
769 if (!endptr
|| *endptr
) {
780 int parse_bool(xmlChar
*str
, int *val
)
789 if (!strcmp((const char *) str
, config_xml_true
)) {
791 } else if (!strcmp((const char *) str
, config_xml_false
)) {
794 WARN("Invalid boolean value encoutered (%s).",
803 int get_domain_type(xmlChar
*domain
)
811 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
812 ret
= LTTNG_DOMAIN_KERNEL
;
813 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
814 ret
= LTTNG_DOMAIN_UST
;
815 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
816 ret
= LTTNG_DOMAIN_JUL
;
817 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
818 ret
= LTTNG_DOMAIN_LOG4J
;
819 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
820 ret
= LTTNG_DOMAIN_PYTHON
;
831 int get_buffer_type(xmlChar
*buffer_type
)
839 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
840 ret
= LTTNG_BUFFER_GLOBAL
;
841 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
842 ret
= LTTNG_BUFFER_PER_UID
;
843 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
844 ret
= LTTNG_BUFFER_PER_PID
;
855 int get_overwrite_mode(xmlChar
*overwrite_mode
)
859 if (!overwrite_mode
) {
863 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
865 } else if (!strcmp((char *) overwrite_mode
,
866 config_overwrite_mode_discard
)) {
878 int get_output_type(xmlChar
*output_type
)
886 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
887 ret
= LTTNG_EVENT_MMAP
;
888 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
889 ret
= LTTNG_EVENT_SPLICE
;
900 int get_event_type(xmlChar
*event_type
)
908 if (!strcmp((char *) event_type
, config_event_type_all
)) {
909 ret
= LTTNG_EVENT_ALL
;
910 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
911 ret
= LTTNG_EVENT_TRACEPOINT
;
912 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
913 ret
= LTTNG_EVENT_PROBE
;
914 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
915 ret
= LTTNG_EVENT_FUNCTION
;
916 } else if (!strcmp((char *) event_type
,
917 config_event_type_function_entry
)) {
918 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
919 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
920 ret
= LTTNG_EVENT_NOOP
;
921 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
922 ret
= LTTNG_EVENT_SYSCALL
;
933 int get_loglevel_type(xmlChar
*loglevel_type
)
937 if (!loglevel_type
) {
941 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
942 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
943 } else if (!strcmp((char *) loglevel_type
,
944 config_loglevel_type_range
)) {
945 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
946 } else if (!strcmp((char *) loglevel_type
,
947 config_loglevel_type_single
)) {
948 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
959 * Return the context type or -1 on error.
962 int get_context_type(xmlChar
*context_type
)
970 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
971 ret
= LTTNG_EVENT_CONTEXT_PID
;
972 } else if (!strcmp((char *) context_type
,
973 config_event_context_procname
)) {
974 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
975 } else if (!strcmp((char *) context_type
,
976 config_event_context_prio
)) {
977 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
978 } else if (!strcmp((char *) context_type
,
979 config_event_context_nice
)) {
980 ret
= LTTNG_EVENT_CONTEXT_NICE
;
981 } else if (!strcmp((char *) context_type
,
982 config_event_context_vpid
)) {
983 ret
= LTTNG_EVENT_CONTEXT_VPID
;
984 } else if (!strcmp((char *) context_type
,
985 config_event_context_tid
)) {
986 ret
= LTTNG_EVENT_CONTEXT_TID
;
987 } else if (!strcmp((char *) context_type
,
988 config_event_context_vtid
)) {
989 ret
= LTTNG_EVENT_CONTEXT_VTID
;
990 } else if (!strcmp((char *) context_type
,
991 config_event_context_ppid
)) {
992 ret
= LTTNG_EVENT_CONTEXT_PPID
;
993 } else if (!strcmp((char *) context_type
,
994 config_event_context_vppid
)) {
995 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
996 } else if (!strcmp((char *) context_type
,
997 config_event_context_pthread_id
)) {
998 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_hostname
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_ip
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_IP
;
1005 } else if (!strcmp((char *) context_type
,
1006 config_event_context_interruptible
)) {
1007 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1008 } else if (!strcmp((char *) context_type
,
1009 config_event_context_preemptible
)) {
1010 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1011 } else if (!strcmp((char *) context_type
,
1012 config_event_context_need_reschedule
)) {
1013 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1014 } else if (!strcmp((char *) context_type
,
1015 config_event_context_migratable
)) {
1016 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1027 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1032 for (node
= xmlFirstElementChild(domain_node
); node
;
1033 node
= xmlNextElementSibling(node
)) {
1034 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1036 xmlChar
*node_content
= xmlNodeGetContent(node
);
1037 if (!node_content
) {
1038 ret
= -LTTNG_ERR_NOMEM
;
1042 ret
= get_domain_type(node_content
);
1045 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1050 } else if (!strcmp((const char *) node
->name
,
1051 config_element_buffer_type
)) {
1053 xmlChar
*node_content
= xmlNodeGetContent(node
);
1054 if (!node_content
) {
1055 ret
= -LTTNG_ERR_NOMEM
;
1059 ret
= get_buffer_type(node_content
);
1062 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1066 domain
->buf_type
= ret
;
1075 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1080 for (node
= xmlFirstElementChild(net_output_node
); node
;
1081 node
= xmlNextElementSibling(node
)) {
1082 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1084 *control_uri
= (char *) xmlNodeGetContent(node
);
1085 if (!*control_uri
) {
1090 *data_uri
= (char *) xmlNodeGetContent(node
);
1097 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1101 int process_consumer_output(xmlNodePtr consumer_output_node
,
1102 struct consumer_output
*output
)
1109 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1110 node
= xmlNextElementSibling(node
)) {
1111 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1112 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1116 ret
= -LTTNG_ERR_NOMEM
;
1120 ret
= parse_bool(enabled_str
, &output
->enabled
);
1126 xmlNodePtr output_type_node
;
1129 output_type_node
= xmlFirstElementChild(node
);
1130 if (!output_type_node
) {
1131 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1135 if (!strcmp((const char *) output_type_node
->name
,
1136 config_element_path
)) {
1138 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1139 if (!output
->path
) {
1140 ret
= -LTTNG_ERR_NOMEM
;
1145 ret
= get_net_output_uris(output_type_node
,
1146 &output
->control_uri
, &output
->data_uri
);
1158 free(output
->control_uri
);
1159 free(output
->data_uri
);
1160 memset(output
, 0, sizeof(struct consumer_output
));
1166 int create_session_net_output(const char *name
, const char *control_uri
,
1167 const char *data_uri
)
1170 struct lttng_handle
*handle
;
1171 const char *uri
= NULL
;
1175 handle
= lttng_create_handle(name
, NULL
);
1177 ret
= -LTTNG_ERR_NOMEM
;
1181 if (!control_uri
|| !data_uri
) {
1182 uri
= control_uri
? control_uri
: data_uri
;
1187 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1188 lttng_destroy_handle(handle
);
1194 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
)
1197 xmlNodePtr node
= NULL
;
1198 xmlNodePtr snapshot_output_list_node
;
1199 xmlNodePtr snapshot_output_node
;
1201 assert(session_name
);
1203 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1212 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1214 /* Parse and create snapshot outputs */
1216 for (snapshot_output_node
=
1217 xmlFirstElementChild(snapshot_output_list_node
);
1218 snapshot_output_node
; snapshot_output_node
=
1219 xmlNextElementSibling(snapshot_output_node
)) {
1221 uint64_t max_size
= DEFAULT_SNAPSHOT_MAX_SIZE
;
1222 struct consumer_output output
= { 0 };
1223 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1225 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1226 node
= xmlNextElementSibling(node
)) {
1227 if (!strcmp((const char *) node
->name
,
1228 config_element_name
)) {
1230 name
= (char *) xmlNodeGetContent(node
);
1232 ret
= -LTTNG_ERR_NOMEM
;
1233 goto error_snapshot_output
;
1235 } else if (!strcmp((const char *) node
->name
,
1236 config_element_max_size
)) {
1237 xmlChar
*content
= xmlNodeGetContent(node
);
1241 ret
= -LTTNG_ERR_NOMEM
;
1242 goto error_snapshot_output
;
1244 ret
= parse_uint(content
, &max_size
);
1247 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1248 goto error_snapshot_output
;
1251 /* consumer_output */
1252 ret
= process_consumer_output(node
, &output
);
1254 goto error_snapshot_output
;
1259 snapshot_output
= lttng_snapshot_output_create();
1260 if (!snapshot_output
) {
1261 ret
= -LTTNG_ERR_NOMEM
;
1262 goto error_snapshot_output
;
1266 /* Generate a default name */
1268 pret
= asprintf(&name
, DEFAULT_SNAPSHOT_NAME
"-%" PRIu32
,
1269 lttng_snapshot_output_get_id(snapshot_output
));
1272 PERROR("snprintf output name");
1273 goto error_snapshot_output
;
1277 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1279 goto error_snapshot_output
;
1282 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1284 goto error_snapshot_output
;
1288 ret
= lttng_snapshot_output_set_ctrl_url(output
.path
,
1291 goto error_snapshot_output
;
1294 if (output
.control_uri
) {
1295 ret
= lttng_snapshot_output_set_ctrl_url(output
.control_uri
,
1298 goto error_snapshot_output
;
1302 if (output
.data_uri
) {
1303 ret
= lttng_snapshot_output_set_data_url(output
.data_uri
,
1306 goto error_snapshot_output
;
1311 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1312 error_snapshot_output
:
1315 free(output
.control_uri
);
1316 free(output
.data_uri
);
1317 lttng_snapshot_output_destroy(snapshot_output
);
1327 int create_session(const char *name
,
1328 struct lttng_domain
*kernel_domain
,
1329 struct lttng_domain
*ust_domain
,
1330 struct lttng_domain
*jul_domain
,
1331 struct lttng_domain
*log4j_domain
,
1332 xmlNodePtr output_node
,
1333 uint64_t live_timer_interval
)
1336 struct consumer_output output
= { 0 };
1337 xmlNodePtr consumer_output_node
;
1342 consumer_output_node
= xmlFirstElementChild(output_node
);
1343 if (!consumer_output_node
) {
1344 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1348 if (strcmp((const char *) consumer_output_node
->name
,
1349 config_element_consumer_output
)) {
1350 WARN("Invalid output type, expected %s node",
1351 config_element_consumer_output
);
1352 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1356 ret
= process_consumer_output(consumer_output_node
, &output
);
1362 if (live_timer_interval
!= UINT64_MAX
&&
1363 !output
.control_uri
&& !output
.data_uri
) {
1364 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1368 if (output
.control_uri
|| output
.data_uri
) {
1369 /* network destination */
1370 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1372 * URLs are provided for sure since the test above make sure that
1373 * with a live timer the data and control URIs are provided. So,
1374 * NULL is passed here and will be set right after.
1376 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1378 ret
= lttng_create_session(name
, NULL
);
1384 ret
= create_session_net_output(name
, output
.control_uri
,
1391 /* either local output or no output */
1392 ret
= lttng_create_session(name
, output
.path
);
1399 free(output
.control_uri
);
1400 free(output
.data_uri
);
1404 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1405 struct lttng_event_probe_attr
*attr
)
1409 assert(probe_attribute_node
);
1412 if (!strcmp((const char *) probe_attribute_node
->name
,
1413 config_element_address
)) {
1418 content
= xmlNodeGetContent(probe_attribute_node
);
1420 ret
= -LTTNG_ERR_NOMEM
;
1424 ret
= parse_uint(content
, &addr
);
1427 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1432 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1433 config_element_offset
)) {
1435 uint64_t offset
= 0;
1438 content
= xmlNodeGetContent(probe_attribute_node
);
1440 ret
= -LTTNG_ERR_NOMEM
;
1444 ret
= parse_uint(content
, &offset
);
1447 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1451 attr
->offset
= offset
;
1452 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1453 config_element_symbol_name
)) {
1458 content
= xmlNodeGetContent(probe_attribute_node
);
1460 ret
= -LTTNG_ERR_NOMEM
;
1464 name_len
= strlen((char *) content
);
1465 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1466 WARN("symbol_name too long.");
1467 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1472 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1481 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1482 const char *channel_name
, const enum process_event_node_phase phase
)
1486 struct lttng_event event
;
1487 char **exclusions
= NULL
;
1488 unsigned long exclusion_count
= 0;
1489 char *filter_expression
= NULL
;
1493 assert(channel_name
);
1495 memset(&event
, 0, sizeof(event
));
1497 /* Initialize default log level which varies by domain */
1498 switch (handle
->domain
.type
)
1500 case LTTNG_DOMAIN_JUL
:
1501 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1503 case LTTNG_DOMAIN_LOG4J
:
1504 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1506 case LTTNG_DOMAIN_PYTHON
:
1507 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1509 case LTTNG_DOMAIN_UST
:
1510 case LTTNG_DOMAIN_KERNEL
:
1511 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1517 for (node
= xmlFirstElementChild(event_node
); node
;
1518 node
= xmlNextElementSibling(node
)) {
1519 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1524 content
= xmlNodeGetContent(node
);
1526 ret
= -LTTNG_ERR_NOMEM
;
1530 name_len
= strlen((char *) content
);
1531 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1532 WARN("Channel name too long.");
1533 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1538 strncpy(event
.name
, (const char *) content
, name_len
);
1540 } else if (!strcmp((const char *) node
->name
,
1541 config_element_enabled
)) {
1542 xmlChar
*content
= xmlNodeGetContent(node
);
1546 ret
= -LTTNG_ERR_NOMEM
;
1550 ret
= parse_bool(content
, &event
.enabled
);
1553 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1556 } else if (!strcmp((const char *) node
->name
,
1557 config_element_type
)) {
1558 xmlChar
*content
= xmlNodeGetContent(node
);
1562 ret
= -LTTNG_ERR_NOMEM
;
1566 ret
= get_event_type(content
);
1569 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1574 } else if (!strcmp((const char *) node
->name
,
1575 config_element_loglevel_type
)) {
1576 xmlChar
*content
= xmlNodeGetContent(node
);
1580 ret
= -LTTNG_ERR_NOMEM
;
1584 ret
= get_loglevel_type(content
);
1587 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1591 event
.loglevel_type
= ret
;
1592 } else if (!strcmp((const char *) node
->name
,
1593 config_element_loglevel
)) {
1595 int64_t loglevel
= 0;
1598 content
= xmlNodeGetContent(node
);
1600 ret
= -LTTNG_ERR_NOMEM
;
1604 ret
= parse_int(content
, &loglevel
);
1607 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1611 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1612 WARN("loglevel out of range.");
1613 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1617 event
.loglevel
= loglevel
;
1618 } else if (!strcmp((const char *) node
->name
,
1619 config_element_filter
)) {
1621 xmlNodeGetContent(node
);
1625 ret
= -LTTNG_ERR_NOMEM
;
1629 free(filter_expression
);
1630 filter_expression
= strdup((char *) content
);
1632 if (!filter_expression
) {
1633 ret
= -LTTNG_ERR_NOMEM
;
1636 } else if (!strcmp((const char *) node
->name
,
1637 config_element_exclusions
)) {
1638 xmlNodePtr exclusion_node
;
1639 int exclusion_index
= 0;
1644 * Exclusions has already been initialized,
1647 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1651 exclusion_count
= xmlChildElementCount(node
);
1652 if (!exclusion_count
) {
1656 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1658 exclusion_count
= 0;
1659 ret
= -LTTNG_ERR_NOMEM
;
1663 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1664 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1666 xmlNodeGetContent(exclusion_node
);
1669 ret
= -LTTNG_ERR_NOMEM
;
1673 exclusions
[exclusion_index
] = strdup((const char *) content
);
1675 if (!exclusions
[exclusion_index
]) {
1676 ret
= -LTTNG_ERR_NOMEM
;
1682 event
.exclusion
= 1;
1683 } else if (!strcmp((const char *) node
->name
,
1684 config_element_attributes
)) {
1685 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1688 if (!attribute_node
) {
1689 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1693 if (!strcmp((const char *) node
->name
,
1694 config_element_probe_attributes
)) {
1695 xmlNodePtr probe_attribute_node
;
1697 /* probe_attributes */
1698 for (probe_attribute_node
=
1699 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1700 probe_attribute_node
= xmlNextElementSibling(
1701 probe_attribute_node
)) {
1703 ret
= process_probe_attribute_node(probe_attribute_node
,
1712 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1714 /* function_attributes */
1715 content
= xmlNodeGetContent(symbol_node
);
1717 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1721 sym_len
= strlen((char *) content
);
1722 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1723 WARN("Function name too long.");
1724 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1729 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1736 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1737 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1738 filter_expression
, exclusion_count
, exclusions
);
1740 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1741 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1746 for (i
= 0; i
< exclusion_count
; i
++) {
1747 free(exclusions
[i
]);
1751 free(filter_expression
);
1756 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1757 const char *channel_name
)
1760 struct lttng_event event
;
1763 assert(events_node
);
1765 assert(channel_name
);
1767 for (node
= xmlFirstElementChild(events_node
); node
;
1768 node
= xmlNextElementSibling(node
)) {
1769 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1776 * Disable all events to enable only the necessary events.
1777 * Limitations regarding lttng_disable_events and tuple descriptor
1778 * force this approach.
1780 memset(&event
, 0, sizeof(event
));
1781 event
.loglevel
= -1;
1782 event
.type
= LTTNG_EVENT_ALL
;
1783 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1788 for (node
= xmlFirstElementChild(events_node
); node
;
1789 node
= xmlNextElementSibling(node
)) {
1790 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1801 int process_channel_attr_node(xmlNodePtr attr_node
, bool snapshot_mode
,
1802 enum lttng_domain_type domain
, struct lttng_channel
*channel
,
1803 xmlNodePtr
*contexts_node
, xmlNodePtr
*events_node
)
1806 bool name_set
= false;
1807 bool output_type_set
= false;
1811 assert(contexts_node
);
1812 assert(events_node
);
1814 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1819 content
= xmlNodeGetContent(attr_node
);
1821 ret
= -LTTNG_ERR_NOMEM
;
1825 name_len
= strlen((char *) content
);
1826 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1827 WARN("Channel name too long.");
1828 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1833 strncpy(channel
->name
, (const char *) content
, name_len
);
1836 } else if (!strcmp((const char *) attr_node
->name
,
1837 config_element_enabled
)) {
1842 content
= xmlNodeGetContent(attr_node
);
1844 ret
= -LTTNG_ERR_NOMEM
;
1848 ret
= parse_bool(content
, &enabled
);
1851 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1855 channel
->enabled
= enabled
;
1856 } else if (!strcmp((const char *) attr_node
->name
,
1857 config_element_overwrite_mode
)) {
1860 /* overwrite_mode */
1861 content
= xmlNodeGetContent(attr_node
);
1863 ret
= -LTTNG_ERR_NOMEM
;
1867 ret
= get_overwrite_mode(content
);
1870 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1874 channel
->attr
.overwrite
= ret
;
1875 } else if (!strcmp((const char *) attr_node
->name
,
1876 config_element_subbuf_size
)) {
1879 /* subbuffer_size */
1880 content
= xmlNodeGetContent(attr_node
);
1882 ret
= -LTTNG_ERR_NOMEM
;
1886 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1889 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1892 } else if (!strcmp((const char *) attr_node
->name
,
1893 config_element_num_subbuf
)) {
1896 /* subbuffer_count */
1897 content
= xmlNodeGetContent(attr_node
);
1899 ret
= -LTTNG_ERR_NOMEM
;
1903 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1906 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1909 } else if (!strcmp((const char *) attr_node
->name
,
1910 config_element_switch_timer_interval
)) {
1912 uint64_t switch_timer_interval
= 0;
1914 /* switch_timer_interval */
1915 content
= xmlNodeGetContent(attr_node
);
1917 ret
= -LTTNG_ERR_NOMEM
;
1921 ret
= parse_uint(content
, &switch_timer_interval
);
1924 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1928 if (switch_timer_interval
> UINT_MAX
) {
1929 WARN("switch_timer_interval out of range.");
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1934 channel
->attr
.switch_timer_interval
=
1935 switch_timer_interval
;
1936 } else if (!strcmp((const char *) attr_node
->name
,
1937 config_element_read_timer_interval
)) {
1939 uint64_t read_timer_interval
= 0;
1941 /* read_timer_interval */
1942 content
= xmlNodeGetContent(attr_node
);
1944 ret
= -LTTNG_ERR_NOMEM
;
1948 ret
= parse_uint(content
, &read_timer_interval
);
1951 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1955 if (read_timer_interval
> UINT_MAX
) {
1956 WARN("read_timer_interval out of range.");
1957 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1961 channel
->attr
.read_timer_interval
=
1962 read_timer_interval
;
1963 } else if (!strcmp((const char *) attr_node
->name
,
1964 config_element_output_type
)) {
1968 content
= xmlNodeGetContent(attr_node
);
1970 ret
= -LTTNG_ERR_NOMEM
;
1974 ret
= get_output_type(content
);
1977 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1981 channel
->attr
.output
= ret
;
1982 output_type_set
= true;
1983 } else if (!strcmp((const char *) attr_node
->name
,
1984 config_element_tracefile_size
)) {
1987 /* tracefile_size */
1988 content
= xmlNodeGetContent(attr_node
);
1990 ret
= -LTTNG_ERR_NOMEM
;
1994 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
1997 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2000 } else if (!strcmp((const char *) attr_node
->name
,
2001 config_element_tracefile_count
)) {
2004 /* tracefile_count */
2005 content
= xmlNodeGetContent(attr_node
);
2007 ret
= -LTTNG_ERR_NOMEM
;
2011 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2014 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2017 } else if (!strcmp((const char *) attr_node
->name
,
2018 config_element_live_timer_interval
)) {
2020 uint64_t live_timer_interval
= 0;
2022 /* live_timer_interval */
2023 content
= xmlNodeGetContent(attr_node
);
2025 ret
= -LTTNG_ERR_NOMEM
;
2029 ret
= parse_uint(content
, &live_timer_interval
);
2032 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2036 if (live_timer_interval
> UINT_MAX
) {
2037 WARN("live_timer_interval out of range.");
2038 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2042 channel
->attr
.live_timer_interval
=
2043 live_timer_interval
;
2044 } else if (!strcmp((const char *) attr_node
->name
,
2045 config_element_events
)) {
2047 *events_node
= attr_node
;
2050 *contexts_node
= attr_node
;
2054 ERR("Encountered a channel with no name attribute.");
2055 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2059 if (!output_type_set
) {
2060 /* Set default output type associated with the domain. */
2061 channel
->attr
.output
=
2062 (domain
== LTTNG_DOMAIN_KERNEL
&& !snapshot_mode
) ?
2063 LTTNG_EVENT_SPLICE
: LTTNG_EVENT_MMAP
;
2071 int process_context_node(xmlNodePtr context_node
,
2072 struct lttng_handle
*handle
, const char *channel_name
)
2075 struct lttng_event_context context
;
2076 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2079 assert(channel_name
);
2081 if (!context_child_node
) {
2082 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2086 memset(&context
, 0, sizeof(context
));
2088 if (!strcmp((const char *) context_child_node
->name
,
2089 config_element_type
)) {
2091 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2094 ret
= -LTTNG_ERR_NOMEM
;
2098 ret
= get_context_type(content
);
2101 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2106 } else if (!strcmp((const char *) context_child_node
->name
,
2107 config_element_context_perf
)) {
2109 xmlNodePtr perf_attr_node
;
2111 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2112 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2113 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2114 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2115 perf_attr_node
; perf_attr_node
=
2116 xmlNextElementSibling(perf_attr_node
)) {
2117 if (!strcmp((const char *) perf_attr_node
->name
,
2118 config_element_type
)) {
2123 content
= xmlNodeGetContent(perf_attr_node
);
2125 ret
= -LTTNG_ERR_NOMEM
;
2129 ret
= parse_uint(content
, &type
);
2132 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2136 if (type
> UINT32_MAX
) {
2137 WARN("perf context type out of range.");
2138 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2142 context
.u
.perf_counter
.type
= type
;
2143 } else if (!strcmp((const char *) perf_attr_node
->name
,
2144 config_element_config
)) {
2146 uint64_t config
= 0;
2149 content
= xmlNodeGetContent(perf_attr_node
);
2151 ret
= -LTTNG_ERR_NOMEM
;
2155 ret
= parse_uint(content
, &config
);
2158 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2162 context
.u
.perf_counter
.config
= config
;
2163 } else if (!strcmp((const char *) perf_attr_node
->name
,
2164 config_element_name
)) {
2169 content
= xmlNodeGetContent(perf_attr_node
);
2171 ret
= -LTTNG_ERR_NOMEM
;
2175 name_len
= strlen((char *) content
);
2176 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2177 WARN("perf context name too long.");
2178 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2183 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2188 } else if (!strcmp((const char *) context_child_node
->name
,
2189 config_element_context_app
)) {
2190 /* application context */
2191 xmlNodePtr app_ctx_node
;
2193 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2194 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2195 app_ctx_node
; app_ctx_node
=
2196 xmlNextElementSibling(app_ctx_node
)) {
2198 char **target
= strcmp(
2199 (const char *) app_ctx_node
->name
,
2200 config_element_context_app_provider_name
) == 0 ?
2201 &context
.u
.app_ctx
.provider_name
:
2202 &context
.u
.app_ctx
.ctx_name
;
2204 content
= xmlNodeGetContent(app_ctx_node
);
2206 ret
= -LTTNG_ERR_NOMEM
;
2210 *target
= (char *) content
;
2213 /* Unrecognized context type */
2214 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2218 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2219 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2220 free(context
.u
.app_ctx
.provider_name
);
2221 free(context
.u
.app_ctx
.ctx_name
);
2228 int process_contexts_node(xmlNodePtr contexts_node
,
2229 struct lttng_handle
*handle
, const char *channel_name
)
2232 xmlNodePtr context_node
;
2234 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2235 context_node
= xmlNextElementSibling(context_node
)) {
2236 ret
= process_context_node(context_node
, handle
, channel_name
);
2246 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2247 struct lttng_handle
*handle
)
2250 xmlNodePtr targets_node
= NULL
;
2254 assert(pid_tracker_node
);
2255 /* get the targets node */
2256 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2257 node
= xmlNextElementSibling(node
)) {
2258 if (!strcmp((const char *) node
->name
,
2259 config_element_targets
)) {
2260 targets_node
= node
;
2265 if (!targets_node
) {
2266 ret
= LTTNG_ERR_INVALID
;
2270 /* Go through all pid_target node */
2271 child
= xmlChildElementCount(targets_node
);
2273 /* The session is explicitly set to target nothing. */
2274 ret
= lttng_untrack_pid(handle
, -1);
2279 for (node
= xmlFirstElementChild(targets_node
); node
;
2280 node
= xmlNextElementSibling(node
)) {
2281 xmlNodePtr pid_target_node
= node
;
2283 /* get pid node and track it */
2284 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2285 node
= xmlNextElementSibling(node
)) {
2286 if (!strcmp((const char *) node
->name
,
2287 config_element_pid
)) {
2289 xmlChar
*content
= NULL
;
2291 content
= xmlNodeGetContent(node
);
2293 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2297 ret
= parse_int(content
, &pid
);
2300 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2304 ret
= lttng_track_pid(handle
, (int) pid
);
2310 node
= pid_target_node
;
2319 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
,
2323 struct lttng_domain domain
= { 0 };
2324 struct lttng_handle
*handle
= NULL
;
2325 xmlNodePtr channels_node
= NULL
;
2326 xmlNodePtr trackers_node
= NULL
;
2327 xmlNodePtr pid_tracker_node
= NULL
;
2330 assert(session_name
);
2332 ret
= init_domain(domain_node
, &domain
);
2337 handle
= lttng_create_handle(session_name
, &domain
);
2339 ret
= -LTTNG_ERR_NOMEM
;
2343 /* get the channels node */
2344 for (node
= xmlFirstElementChild(domain_node
); node
;
2345 node
= xmlNextElementSibling(node
)) {
2346 if (!strcmp((const char *) node
->name
,
2347 config_element_channels
)) {
2348 channels_node
= node
;
2353 if (!channels_node
) {
2357 /* create all channels */
2358 for (node
= xmlFirstElementChild(channels_node
); node
;
2359 node
= xmlNextElementSibling(node
)) {
2360 struct lttng_channel channel
;
2361 xmlNodePtr contexts_node
= NULL
;
2362 xmlNodePtr events_node
= NULL
;
2363 xmlNodePtr channel_attr_node
;
2365 memset(&channel
, 0, sizeof(channel
));
2366 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2368 for (channel_attr_node
= xmlFirstElementChild(node
);
2369 channel_attr_node
; channel_attr_node
=
2370 xmlNextElementSibling(channel_attr_node
)) {
2371 ret
= process_channel_attr_node(channel_attr_node
,
2372 snapshot_mode
, domain
.type
, &channel
,
2373 &contexts_node
, &events_node
);
2379 ret
= lttng_enable_channel(handle
, &channel
);
2384 ret
= process_events_node(events_node
, handle
, channel
.name
);
2389 ret
= process_contexts_node(contexts_node
, handle
,
2396 /* get the trackers node */
2397 for (node
= xmlFirstElementChild(domain_node
); node
;
2398 node
= xmlNextElementSibling(node
)) {
2399 if (!strcmp((const char *) node
->name
,
2400 config_element_trackers
)) {
2401 trackers_node
= node
;
2406 if (!trackers_node
) {
2410 for (node
= xmlFirstElementChild(trackers_node
); node
;
2411 node
= xmlNextElementSibling(node
)) {
2412 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2413 pid_tracker_node
= node
;
2414 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2421 if (!pid_tracker_node
) {
2422 lttng_track_pid(handle
, -1);
2426 lttng_destroy_handle(handle
);
2431 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2434 int ret
, started
= -1, snapshot_mode
= -1;
2435 uint64_t live_timer_interval
= UINT64_MAX
;
2436 xmlChar
*name
= NULL
;
2437 xmlChar
*shm_path
= NULL
;
2438 xmlNodePtr domains_node
= NULL
;
2439 xmlNodePtr output_node
= NULL
;
2441 struct lttng_domain
*kernel_domain
= NULL
;
2442 struct lttng_domain
*ust_domain
= NULL
;
2443 struct lttng_domain
*jul_domain
= NULL
;
2444 struct lttng_domain
*log4j_domain
= NULL
;
2445 struct lttng_domain
*python_domain
= NULL
;
2447 for (node
= xmlFirstElementChild(session_node
); node
;
2448 node
= xmlNextElementSibling(node
)) {
2449 if (!name
&& !strcmp((const char *) node
->name
,
2450 config_element_name
)) {
2452 xmlChar
*node_content
= xmlNodeGetContent(node
);
2453 if (!node_content
) {
2454 ret
= -LTTNG_ERR_NOMEM
;
2458 name
= node_content
;
2459 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2460 config_element_domains
)) {
2462 domains_node
= node
;
2463 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2464 config_element_started
)) {
2466 xmlChar
*node_content
= xmlNodeGetContent(node
);
2467 if (!node_content
) {
2468 ret
= -LTTNG_ERR_NOMEM
;
2472 ret
= parse_bool(node_content
, &started
);
2475 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2478 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2479 config_element_output
)) {
2482 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2483 config_element_shared_memory_path
)) {
2484 /* shared memory path */
2485 xmlChar
*node_content
= xmlNodeGetContent(node
);
2486 if (!node_content
) {
2487 ret
= -LTTNG_ERR_NOMEM
;
2491 shm_path
= node_content
;
2493 /* attributes, snapshot_mode or live_timer_interval */
2494 xmlNodePtr attributes_child
=
2495 xmlFirstElementChild(node
);
2497 if (!strcmp((const char *) attributes_child
->name
,
2498 config_element_snapshot_mode
)) {
2500 xmlChar
*snapshot_mode_content
=
2501 xmlNodeGetContent(attributes_child
);
2502 if (!snapshot_mode_content
) {
2503 ret
= -LTTNG_ERR_NOMEM
;
2507 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2508 free(snapshot_mode_content
);
2510 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2514 /* live_timer_interval */
2515 xmlChar
*timer_interval_content
=
2516 xmlNodeGetContent(attributes_child
);
2517 if (!timer_interval_content
) {
2518 ret
= -LTTNG_ERR_NOMEM
;
2522 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2523 free(timer_interval_content
);
2525 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2533 /* Mandatory attribute, as defined in the session XSD */
2534 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2538 if (session_name
&& strcmp((char *) name
, session_name
)) {
2539 /* This is not the session we are looking for */
2540 ret
= -LTTNG_ERR_NO_SESSION
;
2544 /* Init domains to create the session handles */
2545 for (node
= xmlFirstElementChild(domains_node
); node
;
2546 node
= xmlNextElementSibling(node
)) {
2547 struct lttng_domain
*domain
;
2549 domain
= zmalloc(sizeof(*domain
));
2551 ret
= -LTTNG_ERR_NOMEM
;
2555 ret
= init_domain(node
, domain
);
2557 goto domain_init_error
;
2560 switch (domain
->type
) {
2561 case LTTNG_DOMAIN_KERNEL
:
2562 if (kernel_domain
) {
2563 /* Same domain seen twice, invalid! */
2564 goto domain_init_error
;
2566 kernel_domain
= domain
;
2568 case LTTNG_DOMAIN_UST
:
2570 /* Same domain seen twice, invalid! */
2571 goto domain_init_error
;
2573 ust_domain
= domain
;
2575 case LTTNG_DOMAIN_JUL
:
2577 /* Same domain seen twice, invalid! */
2578 goto domain_init_error
;
2580 jul_domain
= domain
;
2582 case LTTNG_DOMAIN_LOG4J
:
2584 /* Same domain seen twice, invalid! */
2585 goto domain_init_error
;
2587 log4j_domain
= domain
;
2589 case LTTNG_DOMAIN_PYTHON
:
2590 if (python_domain
) {
2591 /* Same domain seen twice, invalid! */
2592 goto domain_init_error
;
2594 python_domain
= domain
;
2597 WARN("Invalid domain type");
2598 goto domain_init_error
;
2603 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2608 /* Destroy session if it exists */
2609 ret
= lttng_destroy_session((const char *) name
);
2610 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2611 ERR("Failed to destroy existing session.");
2616 /* Create session type depending on output type */
2617 if (snapshot_mode
&& snapshot_mode
!= -1) {
2618 ret
= create_snapshot_session((const char *) name
, output_node
);
2619 } else if (live_timer_interval
&&
2620 live_timer_interval
!= UINT64_MAX
) {
2621 ret
= create_session((const char *) name
, kernel_domain
,
2622 ust_domain
, jul_domain
, log4j_domain
,
2623 output_node
, live_timer_interval
);
2625 /* regular session */
2626 ret
= create_session((const char *) name
, kernel_domain
,
2627 ust_domain
, jul_domain
, log4j_domain
,
2628 output_node
, UINT64_MAX
);
2635 ret
= lttng_set_session_shm_path((const char *) name
,
2636 (const char *) shm_path
);
2642 for (node
= xmlFirstElementChild(domains_node
); node
;
2643 node
= xmlNextElementSibling(node
)) {
2644 ret
= process_domain_node(node
, (const char *) name
,
2645 snapshot_mode
== 1);
2652 ret
= lttng_start_tracing((const char *) name
);
2660 ERR("Failed to load session %s: %s", (const char *) name
,
2661 lttng_strerror(ret
));
2662 lttng_destroy_session((const char *) name
);
2666 free(kernel_domain
);
2670 free(python_domain
);
2677 * Return 1 if the given path is readable by the current UID or 0 if not.
2678 * Return -1 if the path is EPERM.
2680 static int validate_file_read_creds(const char *path
)
2686 /* Can we read the file. */
2687 ret
= access(path
, R_OK
);
2691 if (errno
== EACCES
) {
2702 int load_session_from_file(const char *path
, const char *session_name
,
2703 struct session_config_validation_ctx
*validation_ctx
, int override
)
2705 int ret
, session_found
= !session_name
;
2706 xmlDocPtr doc
= NULL
;
2707 xmlNodePtr sessions_node
;
2708 xmlNodePtr session_node
;
2711 assert(validation_ctx
);
2713 ret
= validate_file_read_creds(path
);
2716 ret
= -LTTNG_ERR_EPERM
;
2718 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2723 doc
= xmlParseFile(path
);
2725 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2729 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2731 ERR("Session configuration file validation failed");
2732 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2736 sessions_node
= xmlDocGetRootElement(doc
);
2737 if (!sessions_node
) {
2741 for (session_node
= xmlFirstElementChild(sessions_node
);
2742 session_node
; session_node
=
2743 xmlNextElementSibling(session_node
)) {
2744 ret
= process_session_node(session_node
,
2745 session_name
, override
);
2746 if (session_name
&& ret
== 0) {
2747 /* Target session found and loaded */
2755 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2761 int load_session_from_document(struct config_document
*document
, const char *session_name
,
2762 struct session_config_validation_ctx
*validation_ctx
, int override
)
2764 int ret
, session_found
= !session_name
;
2765 xmlNodePtr sessions_node
;
2766 xmlNodePtr session_node
;
2768 assert(validation_ctx
);
2770 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, document
->document
);
2772 ERR("Session configuration file validation failed");
2773 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2777 sessions_node
= xmlDocGetRootElement(document
->document
);
2778 if (!sessions_node
) {
2782 for (session_node
= xmlFirstElementChild(sessions_node
);
2783 session_node
; session_node
=
2784 xmlNextElementSibling(session_node
)) {
2785 ret
= process_session_node(session_node
,
2786 session_name
, override
);
2787 if (session_name
&& ret
== 0) {
2788 /* Target session found and loaded */
2795 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2800 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2802 struct dirent
*alloc_dirent(const char *path
)
2806 struct dirent
*entry
;
2808 name_max
= pathconf(path
, _PC_NAME_MAX
);
2809 if (name_max
== -1) {
2810 name_max
= PATH_MAX
;
2812 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2813 entry
= zmalloc(len
);
2818 int load_session_from_path(const char *path
, const char *session_name
,
2819 struct session_config_validation_ctx
*validation_ctx
, int override
)
2821 int ret
, session_found
= !session_name
;
2822 DIR *directory
= NULL
;
2825 assert(validation_ctx
);
2827 directory
= opendir(path
);
2831 /* Try the file loading. */
2834 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2837 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2842 struct dirent
*entry
;
2843 struct dirent
*result
;
2844 char *file_path
= NULL
;
2845 size_t path_len
= strlen(path
);
2847 if (path_len
>= PATH_MAX
) {
2848 ret
= -LTTNG_ERR_INVALID
;
2852 entry
= alloc_dirent(path
);
2854 ret
= -LTTNG_ERR_NOMEM
;
2858 file_path
= zmalloc(PATH_MAX
);
2860 ret
= -LTTNG_ERR_NOMEM
;
2865 strncpy(file_path
, path
, path_len
);
2866 if (file_path
[path_len
- 1] != '/') {
2867 file_path
[path_len
++] = '/';
2871 /* Search for *.lttng files */
2872 while (!readdir_r(directory
, entry
, &result
) && result
) {
2873 size_t file_name_len
= strlen(result
->d_name
);
2875 if (file_name_len
<=
2876 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2880 if (path_len
+ file_name_len
>= PATH_MAX
) {
2884 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2885 result
->d_name
+ file_name_len
- sizeof(
2886 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2890 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2891 file_path
[path_len
+ file_name_len
] = '\0';
2893 ret
= load_session_from_file(file_path
, session_name
,
2894 validation_ctx
, override
);
2895 if (session_name
&& !ret
) {
2904 ret
= load_session_from_file(path
, session_name
,
2905 validation_ctx
, override
);
2915 if (closedir(directory
)) {
2920 if (session_found
) {
2928 * Validate that the given path's credentials and the current process have the
2929 * same UID. If so, return 1 else return 0 if it does NOT match.
2931 static int validate_path_creds(const char *path
)
2933 int ret
, uid
= getuid();
2942 ret
= stat(path
, &buf
);
2944 if (errno
!= ENOENT
) {
2947 ret
= -LTTNG_ERR_INVALID
;
2951 if (buf
.st_uid
!= uid
) {
2962 int config_load_session(const char *path
, const char *session_name
,
2963 int override
, unsigned int autoload
)
2966 bool session_loaded
= false;
2967 const char *path_ptr
= NULL
;
2968 struct session_config_validation_ctx validation_ctx
= { 0 };
2970 ret
= init_session_config_validation_ctx(&validation_ctx
);
2977 const char *sys_path
;
2980 home_path
= utils_get_home_dir();
2982 char path
[PATH_MAX
];
2985 * Try user session configuration path. Ignore error here so we can
2986 * continue loading the system wide sessions.
2989 ret
= snprintf(path
, sizeof(path
),
2990 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
2991 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
2993 PERROR("snprintf session autoload home config path");
2998 * Credentials are only validated for the autoload in order to
2999 * avoid any user session daemon to try to load kernel sessions
3000 * automatically and failing all the times.
3002 ret
= validate_path_creds(path
);
3007 ret
= snprintf(path
, sizeof(path
),
3008 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3010 PERROR("snprintf session home config path");
3016 ret
= load_session_from_path(path_ptr
, session_name
,
3017 &validation_ctx
, override
);
3018 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3022 * Continue even if the session was found since we have to try
3023 * the system wide sessions.
3025 session_loaded
= true;
3029 /* Reset path pointer for the system wide dir. */
3032 /* Try system wide configuration directory. */
3034 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3035 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3036 ret
= validate_path_creds(sys_path
);
3038 path_ptr
= sys_path
;
3041 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3042 path_ptr
= sys_path
;
3046 ret
= load_session_from_path(path_ptr
, session_name
,
3047 &validation_ctx
, override
);
3049 session_loaded
= true;
3053 ret
= access(path
, F_OK
);
3058 ret
= -LTTNG_ERR_INVALID
;
3059 WARN("Session configuration path does not exist.");
3062 ret
= -LTTNG_ERR_EPERM
;
3065 ret
= -LTTNG_ERR_UNK
;
3071 ret
= load_session_from_path(path
, session_name
,
3072 &validation_ctx
, override
);
3075 fini_session_config_validation_ctx(&validation_ctx
);
3076 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3078 * Don't report an error if no sessions are found when called
3079 * without a session_name or a search path.
3084 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3085 /* A matching session was found in one of the search paths. */
3092 void __attribute__((destructor
)) session_config_exit(void)
3098 struct config_document
*config_document_get(const char *path
)
3101 struct config_document
*document
= NULL
;
3102 struct session_config_validation_ctx validation_ctx
= { 0 };
3106 ret
= access(path
, F_OK
);
3111 ERR("Configuration document path does not exist.");
3114 ERR("Configuration document permission denied.");
3117 ERR("Configuration document unknown error.");
3123 ret
= init_session_config_validation_ctx(&validation_ctx
);
3128 ret
= validate_file_read_creds(path
);
3131 ERR("Configuration document permission denied.");
3133 ERR("Configuration document does not exist.");
3138 document
= zmalloc(sizeof(struct config_document
));
3144 document
->document
= xmlParseFile(path
);
3145 if (!document
->document
) {
3146 ERR("Configuration document parsing failed.");
3150 ret
= xmlSchemaValidateDoc(validation_ctx
.schema_validation_ctx
,
3151 document
->document
);
3153 ERR("Session configuration file validation failed");
3158 fini_session_config_validation_ctx(&validation_ctx
);
3161 xmlFreeDoc(document
->document
);
3167 void config_document_free(struct config_document
*document
)
3170 xmlFreeDoc(document
->document
);
3171 document
->document
= NULL
;
3178 int config_document_replace_element_value(struct config_document
*document
,
3179 const char *xpath
, const char *value
)
3182 int xpath_result_size
;
3184 xmlXPathContextPtr xpath_context
= NULL
;
3185 xmlXPathObjectPtr xpath_object
= NULL
;
3186 xmlNodeSetPtr xpath_result_set
= NULL
;
3187 xmlChar
*internal_xpath
= NULL
;
3188 xmlChar
*internal_value
= NULL
;
3194 internal_xpath
= encode_string(xpath
);
3195 if (!internal_xpath
) {
3196 ret
= -LTTNG_ERR_NOMEM
;
3197 ERR("Unable to encode xpath string");
3201 internal_value
= encode_string(value
);
3202 if (!internal_value
) {
3203 ret
= -LTTNG_ERR_NOMEM
;
3204 ERR("Unable to encode xpath replace value string");
3208 /* Initialize xpath context */
3209 xpath_context
= xmlXPathNewContext(document
->document
);
3210 if (!xpath_context
) {
3211 ret
= -LTTNG_ERR_NOMEM
;
3212 ERR("Unable to create xpath context");
3216 /* Evaluate de xpath expression */
3217 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3218 if (!xpath_object
) {
3219 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3223 /* TODO: from here could be extracted and previopus could be a step in
3224 * subsequent operation, modify/substitute/delete node.
3226 xpath_result_set
= xpath_object
->nodesetval
;
3227 if (!xpath_result_set
) {
3228 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3232 xpath_result_size
= xpath_result_set
->nodeNr
;
3235 * Reverse traversal since last element could be nested under parent
3236 * element present in the result set.
3238 for (int i
= xpath_result_size
- 1; i
>=0; i
--) {
3239 assert(xpath_result_set
->nodeTab
[i
]);
3240 xmlNodeSetContent(xpath_result_set
->nodeTab
[i
], internal_value
);
3243 * Libxml2 quirk regarding the freing and namesplace node see
3244 * libxml2 example and documentation for more details.
3246 if (xpath_result_set
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) {
3247 xpath_result_set
->nodeTab
[i
] = NULL
;
3252 xmlXPathFreeContext(xpath_context
);
3253 xmlXPathFreeObject(xpath_object
);
3254 xmlFree(internal_xpath
);
3255 xmlFree(internal_value
);
3260 int config_load_configuration_sessions(struct config_document
*document
,
3261 const char *session_name
, int override
)
3264 struct session_config_validation_ctx validation_ctx
= { 0 };
3266 ret
= init_session_config_validation_ctx(&validation_ctx
);
3270 ret
= load_session_from_document(document
, session_name
,
3271 &validation_ctx
, override
);
3273 fini_session_config_validation_ctx(&validation_ctx
);
3278 int config_document_replace_element(struct config_document
*document
,
3279 const char *xpath
, const struct config_element
*element
)
3282 int xpath_result_size
;
3284 xmlXPathContextPtr xpath_context
= NULL
;
3285 xmlXPathObjectPtr xpath_object
= NULL
;
3286 xmlNodeSetPtr xpath_result_set
= NULL
;
3287 xmlChar
*internal_xpath
= NULL
;
3288 xmlNodePtr old_node
= NULL
;
3289 xmlNodePtr copy
= NULL
;
3294 assert(element
->element
);
3296 internal_xpath
= encode_string(xpath
);
3297 if (!internal_xpath
) {
3298 ret
= -LTTNG_ERR_NOMEM
;
3299 ERR("Unable to encode xpath string");
3303 /* Initialize xpath context */
3304 xpath_context
= xmlXPathNewContext(document
->document
);
3305 if (!xpath_context
) {
3306 ret
= -LTTNG_ERR_NOMEM
;
3307 ERR("Unable to create xpath context");
3311 /* Evaluate the xpath expression */
3312 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3313 if (!xpath_object
) {
3314 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3318 xpath_result_set
= xpath_object
->nodesetval
;
3319 if (!xpath_result_set
) {
3320 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3324 xpath_result_size
= xpath_result_set
->nodeNr
;
3326 if (xpath_result_size
> 1) {
3327 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3331 if (xpath_result_size
== 0) {
3332 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3336 assert(xpath_result_set
->nodeTab
[0]);
3338 /* Do a copy of the element to ease caller memory management */
3339 copy
= xmlCopyNode(element
->element
, 1);
3341 ret
= -LTTNG_ERR_NOMEM
;
3342 ERR("Copy failed for node replacement");
3346 old_node
= xmlReplaceNode(xpath_result_set
->nodeTab
[0], copy
);
3347 if (xpath_result_set
->nodeTab
[0]->type
!= XML_NAMESPACE_DECL
)
3348 xpath_result_set
->nodeTab
[0] = NULL
;
3350 ret
= -LTTNG_ERR_CONFIG_REPLACEMENT
;
3354 xmlUnlinkNode(old_node
);
3355 xmlFreeNode(old_node
);
3357 xmlXPathFreeContext(xpath_context
);
3358 xmlXPathFreeObject(xpath_object
);
3359 xmlFree(internal_xpath
);
3364 char *config_document_get_element_value(struct config_document
*document
,
3368 int xpath_result_size
;
3369 int value_result_size
;
3371 xmlXPathContextPtr xpath_context
= NULL
;
3372 xmlXPathObjectPtr xpath_object
= NULL
;
3373 xmlNodeSetPtr xpath_result_set
= NULL
;
3374 xmlChar
*internal_xpath
= NULL
;
3375 xmlChar
*internal_value
= NULL
;
3380 internal_xpath
= encode_string(xpath
);
3381 if (!internal_xpath
) {
3383 ERR("Unable to encode xpath string");
3387 /* Initialize xpath context */
3388 xpath_context
= xmlXPathNewContext(document
->document
);
3389 if (!xpath_context
) {
3391 ERR("Unable to create xpath context");
3395 /* Evaluate the xpath expression */
3396 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3397 if (!xpath_object
) {
3399 ERR("Unable to evaluate xpath query (invalid query format)");
3403 xpath_result_set
= xpath_object
->nodesetval
;
3404 if (!xpath_result_set
) {
3409 xpath_result_size
= xpath_result_set
->nodeNr
;
3411 if (xpath_result_size
> 1) {
3412 ERR("To many result while fetching config element value");
3417 if (xpath_result_size
== 0) {
3423 * Reverse traversal since last element could be nested under parent
3424 * element present in the result set.
3426 assert(xpath_result_set
->nodeTab
[0]);
3427 internal_value
= xmlNodeGetContent(xpath_result_set
->nodeTab
[0]);
3428 if (!internal_value
) {
3433 value_result_size
= xmlStrlen(internal_value
);
3434 value
= calloc(value_result_size
+ 1, sizeof(char));
3435 strncpy(value
, (char *) internal_value
, value_result_size
);
3438 xmlXPathFreeContext(xpath_context
);
3439 xmlXPathFreeObject(xpath_object
);
3440 xmlFree(internal_xpath
);
3441 xmlFree(internal_value
);
3446 int config_document_element_exist(struct config_document
*document
,
3450 int xpath_result_size
;
3452 xmlXPathContextPtr xpath_context
= NULL
;
3453 xmlXPathObjectPtr xpath_object
= NULL
;
3454 xmlNodeSetPtr xpath_result_set
= NULL
;
3455 xmlChar
*internal_xpath
= NULL
;
3458 assert(document
->document
);
3461 internal_xpath
= encode_string(xpath
);
3462 if (!internal_xpath
) {
3463 ERR("Unable to encode xpath string");
3467 /* Initialize xpath context */
3468 xpath_context
= xmlXPathNewContext(document
->document
);
3469 if (!xpath_context
) {
3470 ERR("Unable to create xpath context");
3474 /* Evaluate the xpath expression */
3475 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3476 if (!xpath_object
) {
3477 ERR("Unable to evaluate xpath query (invalid query format)");
3481 xpath_result_set
= xpath_object
->nodesetval
;
3482 if (!xpath_result_set
) {
3486 xpath_result_size
= xpath_result_set
->nodeNr
;
3488 if (xpath_result_size
> 0) {
3492 xmlXPathFreeContext(xpath_context
);
3493 xmlXPathFreeObject(xpath_object
);
3494 xmlFree(internal_xpath
);
3500 struct config_element
*config_element_create(const char *name
,
3503 struct config_element
*element
;
3507 xmlChar
*internal_name
= NULL
;
3508 xmlChar
*internal_value
= NULL
;
3510 internal_name
= encode_string(name
);
3511 if (!internal_name
) {
3512 ERR("Unable to encode config element name string");
3519 internal_value
= encode_string(value
);
3520 if (!internal_value
) {
3521 ERR("Unable to encode config_element value string");
3527 element
= zmalloc(sizeof(struct config_element
));
3532 element
->element
= xmlNewNode(NULL
, internal_name
);
3533 if (!element
->element
) {
3539 if (internal_value
) {
3540 xmlNodeAddContent(element
->element
, internal_value
);
3543 xmlFree(internal_name
);
3544 xmlFree(internal_value
);
3549 int config_element_add_child(struct config_element
*parent
,
3550 const struct config_element
*child
)
3554 assert(parent
->element
);
3555 assert(child
->element
);
3558 xmlNodePtr node
= NULL
;
3559 xmlNodePtr copy
= NULL
;
3561 /* Do a copy to ease the memory management for caller */
3562 copy
= xmlCopyNode(child
->element
, 1);
3564 ret
= -LTTNG_ERR_NOMEM
;
3567 node
= xmlAddChild(parent
->element
, copy
);
3570 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3579 int config_document_insert_element(struct config_document
*document
,
3580 const char *xpath
, const struct config_element
*element
)
3583 int xpath_result_size
;
3585 xmlXPathContextPtr xpath_context
= NULL
;
3586 xmlXPathObjectPtr xpath_object
= NULL
;
3587 xmlNodeSetPtr xpath_result_set
= NULL
;
3588 xmlChar
*internal_xpath
= NULL
;
3589 xmlNodePtr child_node
= NULL
;
3590 xmlNodePtr local_copy
= NULL
;
3595 assert(element
->element
);
3597 internal_xpath
= encode_string(xpath
);
3598 if (!internal_xpath
) {
3599 ret
= -LTTNG_ERR_NOMEM
;
3603 /* Initialize xpath context */
3604 xpath_context
= xmlXPathNewContext(document
->document
);
3605 if (!xpath_context
) {
3606 ret
= -LTTNG_ERR_NOMEM
;
3610 /* Evaluate the xpath expression */
3611 xpath_object
= xmlXPathEvalExpression(internal_xpath
, xpath_context
);
3612 if (!xpath_object
) {
3613 ret
= -LTTNG_ERR_CONFIG_INVALID_QUERY
;
3617 xpath_result_set
= xpath_object
->nodesetval
;
3618 if (!xpath_result_set
) {
3619 ret
= -LTTNG_ERR_CONFIG_EMPTY_SET
;
3623 xpath_result_size
= xpath_result_set
->nodeNr
;
3625 if (xpath_result_size
> 1) {
3626 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3630 if (xpath_result_size
== 0) {
3631 ret
= -LTTNG_ERR_CONFIG_INVALID_COUNT
;
3635 assert(xpath_result_set
->nodeTab
[0]);
3636 /* Do a copy to simply memory management */
3637 local_copy
= xmlCopyNode(element
->element
, 1);
3639 ret
= -LTTNG_ERR_NOMEM
;
3640 ERR("Duplication of node to be insert failed");
3644 child_node
= xmlAddChild(xpath_result_set
->nodeTab
[0], local_copy
);
3646 ret
= -LTTNG_ERR_CONFIG_ADD_CHILD
;
3647 xmlFreeNode(local_copy
);
3651 xmlXPathFreeContext(xpath_context
);
3652 xmlXPathFreeObject(xpath_object
);
3653 xmlFree(internal_xpath
);
3658 void config_element_free(struct config_element
*element
) {
3659 if (element
&& element
->element
) {
3660 xmlFreeNode(element
->element
);