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/dynamic-buffer.h>
36 #include <common/compat/getenv.h>
37 #include <lttng/lttng-error.h>
38 #include <libxml/parser.h>
39 #include <libxml/valid.h>
40 #include <libxml/xmlschemas.h>
41 #include <libxml/tree.h>
42 #include <lttng/lttng.h>
43 #include <lttng/snapshot.h>
44 #include <lttng/rotation.h>
46 #include "session-config.h"
47 #include "config-internal.h"
49 struct handler_filter_args
{
51 config_entry_handler_cb handler
;
55 struct session_config_validation_ctx
{
56 xmlSchemaParserCtxtPtr parser_ctx
;
58 xmlSchemaValidCtxtPtr schema_validation_ctx
;
61 const char * const config_str_yes
= "yes";
62 const char * const config_str_true
= "true";
63 const char * const config_str_on
= "on";
64 const char * const config_str_no
= "no";
65 const char * const config_str_false
= "false";
66 const char * const config_str_off
= "off";
67 const char * const config_xml_encoding
= "UTF-8";
68 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
69 const char * const config_xml_indent_string
= "\t";
70 const char * const config_xml_true
= "true";
71 const char * const config_xml_false
= "false";
73 const char * const config_element_channel
= "channel";
74 const char * const config_element_channels
= "channels";
75 const char * const config_element_domain
= "domain";
76 const char * const config_element_domains
= "domains";
77 const char * const config_element_event
= "event";
78 const char * const config_element_events
= "events";
79 const char * const config_element_context
= "context";
80 const char * const config_element_contexts
= "contexts";
81 const char * const config_element_attributes
= "attributes";
82 const char * const config_element_exclusion
= "exclusion";
83 const char * const config_element_exclusions
= "exclusions";
84 const char * const config_element_function_attributes
= "function_attributes";
85 const char * const config_element_probe_attributes
= "probe_attributes";
86 const char * const config_element_symbol_name
= "symbol_name";
87 const char * const config_element_address
= "address";
88 const char * const config_element_offset
= "offset";
89 const char * const config_element_name
= "name";
90 const char * const config_element_enabled
= "enabled";
91 const char * const config_element_overwrite_mode
= "overwrite_mode";
92 const char * const config_element_subbuf_size
= "subbuffer_size";
93 const char * const config_element_num_subbuf
= "subbuffer_count";
94 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
95 const char * const config_element_read_timer_interval
= "read_timer_interval";
96 LTTNG_HIDDEN
const char * const config_element_monitor_timer_interval
= "monitor_timer_interval";
97 LTTNG_HIDDEN
const char * const config_element_blocking_timeout
= "blocking_timeout";
98 const char * const config_element_output
= "output";
99 const char * const config_element_output_type
= "output_type";
100 const char * const config_element_tracefile_size
= "tracefile_size";
101 const char * const config_element_tracefile_count
= "tracefile_count";
102 const char * const config_element_live_timer_interval
= "live_timer_interval";
103 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
104 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
105 const char * const config_element_type
= "type";
106 const char * const config_element_buffer_type
= "buffer_type";
107 const char * const config_element_session
= "session";
108 const char * const config_element_sessions
= "sessions";
109 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
110 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
111 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
112 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
113 const char * const config_element_config
= "config";
114 const char * const config_element_started
= "started";
115 const char * const config_element_snapshot_mode
= "snapshot_mode";
116 const char * const config_element_loglevel
= "loglevel";
117 const char * const config_element_loglevel_type
= "loglevel_type";
118 const char * const config_element_filter
= "filter";
119 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
120 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
121 const char * const config_element_consumer_output
= "consumer_output";
122 const char * const config_element_destination
= "destination";
123 const char * const config_element_path
= "path";
124 const char * const config_element_net_output
= "net_output";
125 const char * const config_element_control_uri
= "control_uri";
126 const char * const config_element_data_uri
= "data_uri";
127 const char * const config_element_max_size
= "max_size";
128 const char * const config_element_pid
= "pid";
129 const char * const config_element_pids
= "pids";
130 const char * const config_element_shared_memory_path
= "shared_memory_path";
131 const char * const config_element_pid_tracker
= "pid_tracker";
132 const char * const config_element_trackers
= "trackers";
133 const char * const config_element_targets
= "targets";
134 const char * const config_element_target_pid
= "pid_target";
136 LTTNG_HIDDEN
const char * const config_element_rotation_timer_interval
= "rotation_schedule_timer_period";
137 LTTNG_HIDDEN
const char * const config_element_rotation_size
= "rotation_schedule_size";
138 LTTNG_HIDDEN
const char * const config_element_rotation_schedule
= "rotation_schedule";
140 const char * const config_domain_type_kernel
= "KERNEL";
141 const char * const config_domain_type_ust
= "UST";
142 const char * const config_domain_type_jul
= "JUL";
143 const char * const config_domain_type_log4j
= "LOG4J";
144 const char * const config_domain_type_python
= "PYTHON";
146 const char * const config_buffer_type_per_pid
= "PER_PID";
147 const char * const config_buffer_type_per_uid
= "PER_UID";
148 const char * const config_buffer_type_global
= "GLOBAL";
150 const char * const config_overwrite_mode_discard
= "DISCARD";
151 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
153 const char * const config_output_type_splice
= "SPLICE";
154 const char * const config_output_type_mmap
= "MMAP";
156 const char * const config_loglevel_type_all
= "ALL";
157 const char * const config_loglevel_type_range
= "RANGE";
158 const char * const config_loglevel_type_single
= "SINGLE";
160 const char * const config_event_type_all
= "ALL";
161 const char * const config_event_type_tracepoint
= "TRACEPOINT";
162 const char * const config_event_type_probe
= "PROBE";
163 const char * const config_event_type_function
= "FUNCTION";
164 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
165 const char * const config_event_type_noop
= "NOOP";
166 const char * const config_event_type_syscall
= "SYSCALL";
167 const char * const config_event_type_kprobe
= "KPROBE";
168 const char * const config_event_type_kretprobe
= "KRETPROBE";
170 const char * const config_event_context_pid
= "PID";
171 const char * const config_event_context_procname
= "PROCNAME";
172 const char * const config_event_context_prio
= "PRIO";
173 const char * const config_event_context_nice
= "NICE";
174 const char * const config_event_context_vpid
= "VPID";
175 const char * const config_event_context_tid
= "TID";
176 const char * const config_event_context_vtid
= "VTID";
177 const char * const config_event_context_ppid
= "PPID";
178 const char * const config_event_context_vppid
= "VPPID";
179 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
180 const char * const config_event_context_hostname
= "HOSTNAME";
181 const char * const config_event_context_ip
= "IP";
182 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
183 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
184 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
185 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
186 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
187 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
189 /* Deprecated symbols */
190 const char * const config_element_perf
;
192 enum process_event_node_phase
{
197 struct consumer_output
{
204 static int config_entry_handler_filter(struct handler_filter_args
*args
,
205 const char *section
, const char *name
, const char *value
)
208 struct config_entry entry
= { section
, name
, value
};
212 if (!section
|| !name
|| !value
) {
218 if (strcmp(args
->section
, section
)) {
223 ret
= args
->handler(&entry
, args
->user_data
);
229 int config_get_section_entries(const char *override_path
, const char *section
,
230 config_entry_handler_cb handler
, void *user_data
)
234 FILE *config_file
= NULL
;
235 struct handler_filter_args filter
= { section
, handler
, user_data
};
237 /* First, try system-wide conf. file. */
238 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
240 config_file
= fopen(path
, "r");
242 DBG("Loading daemon conf file at %s", path
);
244 * Return value is not very important here since error or not, we
245 * continue and try the next possible conf. file.
247 (void) ini_parse_file(config_file
,
248 (ini_entry_handler
) config_entry_handler_filter
,
253 /* Second is the user local configuration. */
254 path
= utils_get_home_dir();
256 char fullpath
[PATH_MAX
];
258 ret
= snprintf(fullpath
, sizeof(fullpath
),
259 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
261 PERROR("snprintf user conf. path");
265 config_file
= fopen(fullpath
, "r");
267 DBG("Loading daemon user conf file at %s", path
);
269 * Return value is not very important here since error or not, we
270 * continue and try the next possible conf. file.
272 (void) ini_parse_file(config_file
,
273 (ini_entry_handler
) config_entry_handler_filter
,
279 /* Final path is the one that the user might have provided. */
281 config_file
= fopen(override_path
, "r");
283 DBG("Loading daemon command line conf file at %s", override_path
);
284 (void) ini_parse_file(config_file
,
285 (ini_entry_handler
) config_entry_handler_filter
,
289 ERR("Failed to open daemon configuration file at %s",
296 /* Everything went well. */
304 int config_parse_value(const char *value
)
307 char *endptr
, *lower_str
;
317 v
= strtoul(value
, &endptr
, 10);
318 if (endptr
!= value
) {
323 lower_str
= zmalloc(len
+ 1);
330 for (i
= 0; i
< len
; i
++) {
331 lower_str
[i
] = tolower(value
[i
]);
334 if (!strcmp(lower_str
, config_str_yes
) ||
335 !strcmp(lower_str
, config_str_true
) ||
336 !strcmp(lower_str
, config_str_on
)) {
338 } else if (!strcmp(lower_str
, config_str_no
) ||
339 !strcmp(lower_str
, config_str_false
) ||
340 !strcmp(lower_str
, config_str_off
)) {
352 * Returns a xmlChar string which must be released using xmlFree().
354 static xmlChar
*encode_string(const char *in_str
)
356 xmlChar
*out_str
= NULL
;
357 xmlCharEncodingHandlerPtr handler
;
358 int out_len
, ret
, in_len
;
362 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
364 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
368 in_len
= strlen(in_str
);
370 * Add 1 byte for the NULL terminted character. The factor 4 here is
371 * used because UTF-8 characters can take up to 4 bytes.
373 out_len
= (in_len
* 4) + 1;
374 out_str
= xmlMalloc(out_len
);
379 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
386 /* out_len is now the size of out_str */
387 out_str
[out_len
] = '\0';
393 struct config_writer
*config_writer_create(int fd_output
, int indent
)
396 struct config_writer
*writer
;
397 xmlOutputBufferPtr buffer
;
399 writer
= zmalloc(sizeof(struct config_writer
));
401 PERROR("zmalloc config_writer_create");
405 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
410 writer
->writer
= xmlNewTextWriter(buffer
);
411 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
412 config_xml_encoding
, NULL
);
417 ret
= xmlTextWriterSetIndentString(writer
->writer
,
418 BAD_CAST config_xml_indent_string
);
423 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
431 config_writer_destroy(writer
);
436 int config_writer_destroy(struct config_writer
*writer
)
445 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
446 WARN("Could not close XML document");
450 if (writer
->writer
) {
451 xmlFreeTextWriter(writer
->writer
);
460 int config_writer_open_element(struct config_writer
*writer
,
461 const char *element_name
)
464 xmlChar
*encoded_element_name
;
466 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
471 encoded_element_name
= encode_string(element_name
);
472 if (!encoded_element_name
) {
477 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
478 xmlFree(encoded_element_name
);
480 return ret
>= 0 ? 0 : ret
;
484 int config_writer_write_attribute(struct config_writer
*writer
,
485 const char *name
, const char *value
)
488 xmlChar
*encoded_name
= NULL
;
489 xmlChar
*encoded_value
= NULL
;
491 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
496 encoded_name
= encode_string(name
);
502 encoded_value
= encode_string(value
);
503 if (!encoded_value
) {
508 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
511 xmlFree(encoded_name
);
512 xmlFree(encoded_value
);
513 return ret
>= 0 ? 0 : ret
;
517 int config_writer_close_element(struct config_writer
*writer
)
521 if (!writer
|| !writer
->writer
) {
526 ret
= xmlTextWriterEndElement(writer
->writer
);
528 return ret
>= 0 ? 0 : ret
;
532 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
533 const char *element_name
, uint64_t value
)
536 xmlChar
*encoded_element_name
;
538 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
543 encoded_element_name
= encode_string(element_name
);
544 if (!encoded_element_name
) {
549 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
550 encoded_element_name
, "%" PRIu64
, value
);
551 xmlFree(encoded_element_name
);
553 return ret
>= 0 ? 0 : ret
;
557 int config_writer_write_element_signed_int(struct config_writer
*writer
,
558 const char *element_name
, int64_t value
)
561 xmlChar
*encoded_element_name
;
563 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
568 encoded_element_name
= encode_string(element_name
);
569 if (!encoded_element_name
) {
574 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
575 encoded_element_name
, "%" PRIi64
, value
);
576 xmlFree(encoded_element_name
);
578 return ret
>= 0 ? 0 : ret
;
582 int config_writer_write_element_bool(struct config_writer
*writer
,
583 const char *element_name
, int value
)
585 return config_writer_write_element_string(writer
, element_name
,
586 value
? config_xml_true
: config_xml_false
);
590 int config_writer_write_element_string(struct config_writer
*writer
,
591 const char *element_name
, const char *value
)
594 xmlChar
*encoded_element_name
= NULL
;
595 xmlChar
*encoded_value
= NULL
;
597 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
603 encoded_element_name
= encode_string(element_name
);
604 if (!encoded_element_name
) {
609 encoded_value
= encode_string(value
);
610 if (!encoded_value
) {
615 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
618 xmlFree(encoded_element_name
);
619 xmlFree(encoded_value
);
620 return ret
>= 0 ? 0 : ret
;
624 void xml_error_handler(void *ctx
, const char *format
, ...)
630 va_start(args
, format
);
631 ret
= vasprintf(&errMsg
, format
, args
);
634 ERR("String allocation failed in xml error handler");
638 fprintf(stderr
, "XML Error: %s", errMsg
);
643 void fini_session_config_validation_ctx(
644 struct session_config_validation_ctx
*ctx
)
646 if (ctx
->parser_ctx
) {
647 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
651 xmlSchemaFree(ctx
->schema
);
654 if (ctx
->schema_validation_ctx
) {
655 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
658 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
662 char *get_session_config_xsd_path()
665 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
666 size_t base_path_len
;
670 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
673 base_path_len
= strlen(base_path
);
674 max_path_len
= base_path_len
+
675 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
676 xsd_path
= zmalloc(max_path_len
);
681 strcpy(xsd_path
, base_path
);
682 if (xsd_path
[base_path_len
- 1] != '/') {
683 xsd_path
[base_path_len
++] = '/';
686 strcpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
);
692 int init_session_config_validation_ctx(
693 struct session_config_validation_ctx
*ctx
)
696 char *xsd_path
= get_session_config_xsd_path();
699 ret
= -LTTNG_ERR_NOMEM
;
703 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
704 if (!ctx
->parser_ctx
) {
705 ERR("XSD parser context creation failed");
706 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
709 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
710 xml_error_handler
, NULL
);
712 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
714 ERR("XSD parsing failed");
715 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
719 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
720 if (!ctx
->schema_validation_ctx
) {
721 ERR("XSD validation context creation failed");
722 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
726 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
727 xml_error_handler
, NULL
);
732 fini_session_config_validation_ctx(ctx
);
740 int parse_uint(xmlChar
*str
, uint64_t *val
)
750 *val
= strtoull((const char *) str
, &endptr
, 10);
751 if (!endptr
|| *endptr
) {
762 int parse_int(xmlChar
*str
, int64_t *val
)
772 *val
= strtoll((const char *) str
, &endptr
, 10);
773 if (!endptr
|| *endptr
) {
784 int parse_bool(xmlChar
*str
, int *val
)
793 if (!strcmp((const char *) str
, config_xml_true
)) {
795 } else if (!strcmp((const char *) str
, config_xml_false
)) {
798 WARN("Invalid boolean value encoutered (%s).",
807 int get_domain_type(xmlChar
*domain
)
815 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
816 ret
= LTTNG_DOMAIN_KERNEL
;
817 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
818 ret
= LTTNG_DOMAIN_UST
;
819 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
820 ret
= LTTNG_DOMAIN_JUL
;
821 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
822 ret
= LTTNG_DOMAIN_LOG4J
;
823 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
824 ret
= LTTNG_DOMAIN_PYTHON
;
835 int get_buffer_type(xmlChar
*buffer_type
)
843 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
844 ret
= LTTNG_BUFFER_GLOBAL
;
845 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
846 ret
= LTTNG_BUFFER_PER_UID
;
847 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
848 ret
= LTTNG_BUFFER_PER_PID
;
859 int get_overwrite_mode(xmlChar
*overwrite_mode
)
863 if (!overwrite_mode
) {
867 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
869 } else if (!strcmp((char *) overwrite_mode
,
870 config_overwrite_mode_discard
)) {
882 int get_output_type(xmlChar
*output_type
)
890 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
891 ret
= LTTNG_EVENT_MMAP
;
892 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
893 ret
= LTTNG_EVENT_SPLICE
;
904 int get_event_type(xmlChar
*event_type
)
912 if (!strcmp((char *) event_type
, config_event_type_all
)) {
913 ret
= LTTNG_EVENT_ALL
;
914 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
915 ret
= LTTNG_EVENT_TRACEPOINT
;
916 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
917 ret
= LTTNG_EVENT_PROBE
;
918 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
919 ret
= LTTNG_EVENT_FUNCTION
;
920 } else if (!strcmp((char *) event_type
,
921 config_event_type_function_entry
)) {
922 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
923 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
924 ret
= LTTNG_EVENT_NOOP
;
925 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
926 ret
= LTTNG_EVENT_SYSCALL
;
937 int get_loglevel_type(xmlChar
*loglevel_type
)
941 if (!loglevel_type
) {
945 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
946 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
947 } else if (!strcmp((char *) loglevel_type
,
948 config_loglevel_type_range
)) {
949 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
950 } else if (!strcmp((char *) loglevel_type
,
951 config_loglevel_type_single
)) {
952 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
963 * Return the context type or -1 on error.
966 int get_context_type(xmlChar
*context_type
)
974 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
975 ret
= LTTNG_EVENT_CONTEXT_PID
;
976 } else if (!strcmp((char *) context_type
,
977 config_event_context_procname
)) {
978 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
979 } else if (!strcmp((char *) context_type
,
980 config_event_context_prio
)) {
981 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
982 } else if (!strcmp((char *) context_type
,
983 config_event_context_nice
)) {
984 ret
= LTTNG_EVENT_CONTEXT_NICE
;
985 } else if (!strcmp((char *) context_type
,
986 config_event_context_vpid
)) {
987 ret
= LTTNG_EVENT_CONTEXT_VPID
;
988 } else if (!strcmp((char *) context_type
,
989 config_event_context_tid
)) {
990 ret
= LTTNG_EVENT_CONTEXT_TID
;
991 } else if (!strcmp((char *) context_type
,
992 config_event_context_vtid
)) {
993 ret
= LTTNG_EVENT_CONTEXT_VTID
;
994 } else if (!strcmp((char *) context_type
,
995 config_event_context_ppid
)) {
996 ret
= LTTNG_EVENT_CONTEXT_PPID
;
997 } else if (!strcmp((char *) context_type
,
998 config_event_context_vppid
)) {
999 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1000 } else if (!strcmp((char *) context_type
,
1001 config_event_context_pthread_id
)) {
1002 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1003 } else if (!strcmp((char *) context_type
,
1004 config_event_context_hostname
)) {
1005 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1006 } else if (!strcmp((char *) context_type
,
1007 config_event_context_ip
)) {
1008 ret
= LTTNG_EVENT_CONTEXT_IP
;
1009 } else if (!strcmp((char *) context_type
,
1010 config_event_context_interruptible
)) {
1011 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1012 } else if (!strcmp((char *) context_type
,
1013 config_event_context_preemptible
)) {
1014 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1015 } else if (!strcmp((char *) context_type
,
1016 config_event_context_need_reschedule
)) {
1017 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1018 } else if (!strcmp((char *) context_type
,
1019 config_event_context_migratable
)) {
1020 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1031 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1036 for (node
= xmlFirstElementChild(domain_node
); node
;
1037 node
= xmlNextElementSibling(node
)) {
1038 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1040 xmlChar
*node_content
= xmlNodeGetContent(node
);
1041 if (!node_content
) {
1042 ret
= -LTTNG_ERR_NOMEM
;
1046 ret
= get_domain_type(node_content
);
1049 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1054 } else if (!strcmp((const char *) node
->name
,
1055 config_element_buffer_type
)) {
1057 xmlChar
*node_content
= xmlNodeGetContent(node
);
1058 if (!node_content
) {
1059 ret
= -LTTNG_ERR_NOMEM
;
1063 ret
= get_buffer_type(node_content
);
1066 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1070 domain
->buf_type
= ret
;
1079 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1084 for (node
= xmlFirstElementChild(net_output_node
); node
;
1085 node
= xmlNextElementSibling(node
)) {
1086 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1088 *control_uri
= (char *) xmlNodeGetContent(node
);
1089 if (!*control_uri
) {
1094 *data_uri
= (char *) xmlNodeGetContent(node
);
1101 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1105 int process_consumer_output(xmlNodePtr consumer_output_node
,
1106 struct consumer_output
*output
)
1113 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1114 node
= xmlNextElementSibling(node
)) {
1115 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1116 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1120 ret
= -LTTNG_ERR_NOMEM
;
1124 ret
= parse_bool(enabled_str
, &output
->enabled
);
1130 xmlNodePtr output_type_node
;
1133 output_type_node
= xmlFirstElementChild(node
);
1134 if (!output_type_node
) {
1135 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1139 if (!strcmp((const char *) output_type_node
->name
,
1140 config_element_path
)) {
1142 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1143 if (!output
->path
) {
1144 ret
= -LTTNG_ERR_NOMEM
;
1149 ret
= get_net_output_uris(output_type_node
,
1150 &output
->control_uri
, &output
->data_uri
);
1162 free(output
->control_uri
);
1163 free(output
->data_uri
);
1164 memset(output
, 0, sizeof(struct consumer_output
));
1170 int create_session_net_output(const char *name
, const char *control_uri
,
1171 const char *data_uri
)
1174 struct lttng_handle
*handle
;
1175 const char *uri
= NULL
;
1179 handle
= lttng_create_handle(name
, NULL
);
1181 ret
= -LTTNG_ERR_NOMEM
;
1185 if (!control_uri
|| !data_uri
) {
1186 uri
= control_uri
? control_uri
: data_uri
;
1191 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1192 lttng_destroy_handle(handle
);
1198 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1199 const struct config_load_session_override_attr
*overrides
)
1202 xmlNodePtr node
= NULL
;
1203 xmlNodePtr snapshot_output_list_node
;
1204 xmlNodePtr snapshot_output_node
;
1206 assert(session_name
);
1208 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1217 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1219 /* Parse and create snapshot outputs */
1221 for (snapshot_output_node
=
1222 xmlFirstElementChild(snapshot_output_list_node
);
1223 snapshot_output_node
; snapshot_output_node
=
1224 xmlNextElementSibling(snapshot_output_node
)) {
1226 uint64_t max_size
= UINT64_MAX
;
1227 struct consumer_output output
= { 0 };
1228 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1229 const char *control_uri
= NULL
;
1230 const char *data_uri
= NULL
;
1231 const char *path
= NULL
;
1233 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1234 node
= xmlNextElementSibling(node
)) {
1235 if (!strcmp((const char *) node
->name
,
1236 config_element_name
)) {
1238 name
= (char *) xmlNodeGetContent(node
);
1240 ret
= -LTTNG_ERR_NOMEM
;
1241 goto error_snapshot_output
;
1243 } else if (!strcmp((const char *) node
->name
,
1244 config_element_max_size
)) {
1245 xmlChar
*content
= xmlNodeGetContent(node
);
1249 ret
= -LTTNG_ERR_NOMEM
;
1250 goto error_snapshot_output
;
1252 ret
= parse_uint(content
, &max_size
);
1255 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1256 goto error_snapshot_output
;
1259 /* consumer_output */
1260 ret
= process_consumer_output(node
, &output
);
1262 goto error_snapshot_output
;
1267 control_uri
= output
.control_uri
;
1268 data_uri
= output
.data_uri
;
1272 if (overrides
->path_url
) {
1273 path
= overrides
->path_url
;
1274 /* Control/data_uri are null */
1278 if (overrides
->ctrl_url
) {
1279 control_uri
= overrides
->ctrl_url
;
1283 if (overrides
->data_url
) {
1284 data_uri
= overrides
->data_url
;
1291 snapshot_output
= lttng_snapshot_output_create();
1292 if (!snapshot_output
) {
1293 ret
= -LTTNG_ERR_NOMEM
;
1294 goto error_snapshot_output
;
1297 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1299 goto error_snapshot_output
;
1302 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1304 goto error_snapshot_output
;
1308 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1311 goto error_snapshot_output
;
1315 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1318 goto error_snapshot_output
;
1323 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1326 goto error_snapshot_output
;
1331 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1332 error_snapshot_output
:
1335 free(output
.control_uri
);
1336 free(output
.data_uri
);
1337 lttng_snapshot_output_destroy(snapshot_output
);
1347 int create_session(const char *name
,
1348 xmlNodePtr output_node
,
1349 uint64_t live_timer_interval
,
1350 const struct config_load_session_override_attr
*overrides
)
1353 struct consumer_output output
= { 0 };
1354 xmlNodePtr consumer_output_node
;
1355 const char *control_uri
= NULL
;
1356 const char *data_uri
= NULL
;
1357 const char *path
= NULL
;
1362 consumer_output_node
= xmlFirstElementChild(output_node
);
1363 if (!consumer_output_node
) {
1364 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1368 if (strcmp((const char *) consumer_output_node
->name
,
1369 config_element_consumer_output
)) {
1370 WARN("Invalid output type, expected %s node",
1371 config_element_consumer_output
);
1372 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1376 ret
= process_consumer_output(consumer_output_node
, &output
);
1382 control_uri
= output
.control_uri
;
1383 data_uri
= output
.data_uri
;
1386 /* Check for override and apply them */
1388 if (overrides
->path_url
) {
1389 path
= overrides
->path_url
;
1390 /* control/data_uri are null */;
1394 if (overrides
->ctrl_url
) {
1395 control_uri
= overrides
->ctrl_url
;
1399 if (overrides
->data_url
) {
1400 data_uri
= overrides
->data_url
;
1408 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1409 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1413 if (control_uri
|| data_uri
) {
1414 /* network destination */
1415 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1417 * URLs are provided for sure since the test above make sure that
1418 * with a live timer the data and control URIs are provided. So,
1419 * NULL is passed here and will be set right after.
1421 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1423 ret
= lttng_create_session(name
, NULL
);
1429 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1435 /* either local output or no output */
1436 ret
= lttng_create_session(name
, path
);
1443 free(output
.control_uri
);
1444 free(output
.data_uri
);
1448 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1449 struct lttng_event_probe_attr
*attr
)
1453 assert(probe_attribute_node
);
1456 if (!strcmp((const char *) probe_attribute_node
->name
,
1457 config_element_address
)) {
1462 content
= xmlNodeGetContent(probe_attribute_node
);
1464 ret
= -LTTNG_ERR_NOMEM
;
1468 ret
= parse_uint(content
, &addr
);
1471 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1476 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1477 config_element_offset
)) {
1479 uint64_t offset
= 0;
1482 content
= xmlNodeGetContent(probe_attribute_node
);
1484 ret
= -LTTNG_ERR_NOMEM
;
1488 ret
= parse_uint(content
, &offset
);
1491 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1495 attr
->offset
= offset
;
1496 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1497 config_element_symbol_name
)) {
1501 content
= xmlNodeGetContent(probe_attribute_node
);
1503 ret
= -LTTNG_ERR_NOMEM
;
1507 ret
= lttng_strncpy(attr
->symbol_name
,
1508 (const char *) content
,
1509 LTTNG_SYMBOL_NAME_LEN
);
1511 ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1512 (const char *) content
,
1513 strlen((const char *) content
),
1514 LTTNG_SYMBOL_NAME_LEN
);
1515 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1527 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1528 const char *channel_name
, const enum process_event_node_phase phase
)
1532 struct lttng_event event
;
1533 char **exclusions
= NULL
;
1534 unsigned long exclusion_count
= 0;
1535 char *filter_expression
= NULL
;
1539 assert(channel_name
);
1541 memset(&event
, 0, sizeof(event
));
1543 /* Initialize default log level which varies by domain */
1544 switch (handle
->domain
.type
)
1546 case LTTNG_DOMAIN_JUL
:
1547 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1549 case LTTNG_DOMAIN_LOG4J
:
1550 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1552 case LTTNG_DOMAIN_PYTHON
:
1553 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1555 case LTTNG_DOMAIN_UST
:
1556 case LTTNG_DOMAIN_KERNEL
:
1557 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1563 for (node
= xmlFirstElementChild(event_node
); node
;
1564 node
= xmlNextElementSibling(node
)) {
1565 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1569 content
= xmlNodeGetContent(node
);
1571 ret
= -LTTNG_ERR_NOMEM
;
1575 ret
= lttng_strncpy(event
.name
,
1576 (const char *) content
,
1577 LTTNG_SYMBOL_NAME_LEN
);
1579 WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1580 (const char *) content
,
1581 strlen((const char *) content
),
1582 LTTNG_SYMBOL_NAME_LEN
);
1583 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1588 } else if (!strcmp((const char *) node
->name
,
1589 config_element_enabled
)) {
1590 xmlChar
*content
= xmlNodeGetContent(node
);
1594 ret
= -LTTNG_ERR_NOMEM
;
1598 ret
= parse_bool(content
, &event
.enabled
);
1601 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1604 } else if (!strcmp((const char *) node
->name
,
1605 config_element_type
)) {
1606 xmlChar
*content
= xmlNodeGetContent(node
);
1610 ret
= -LTTNG_ERR_NOMEM
;
1614 ret
= get_event_type(content
);
1617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1622 } else if (!strcmp((const char *) node
->name
,
1623 config_element_loglevel_type
)) {
1624 xmlChar
*content
= xmlNodeGetContent(node
);
1628 ret
= -LTTNG_ERR_NOMEM
;
1632 ret
= get_loglevel_type(content
);
1635 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1639 event
.loglevel_type
= ret
;
1640 } else if (!strcmp((const char *) node
->name
,
1641 config_element_loglevel
)) {
1643 int64_t loglevel
= 0;
1646 content
= xmlNodeGetContent(node
);
1648 ret
= -LTTNG_ERR_NOMEM
;
1652 ret
= parse_int(content
, &loglevel
);
1655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1659 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1660 WARN("loglevel out of range.");
1661 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1665 event
.loglevel
= loglevel
;
1666 } else if (!strcmp((const char *) node
->name
,
1667 config_element_filter
)) {
1669 xmlNodeGetContent(node
);
1673 ret
= -LTTNG_ERR_NOMEM
;
1677 free(filter_expression
);
1678 filter_expression
= strdup((char *) content
);
1680 if (!filter_expression
) {
1681 ret
= -LTTNG_ERR_NOMEM
;
1684 } else if (!strcmp((const char *) node
->name
,
1685 config_element_exclusions
)) {
1686 xmlNodePtr exclusion_node
;
1687 int exclusion_index
= 0;
1692 * Exclusions has already been initialized,
1695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1699 exclusion_count
= xmlChildElementCount(node
);
1700 if (!exclusion_count
) {
1704 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1706 exclusion_count
= 0;
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1712 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1714 xmlNodeGetContent(exclusion_node
);
1717 ret
= -LTTNG_ERR_NOMEM
;
1721 exclusions
[exclusion_index
] = strdup((const char *) content
);
1723 if (!exclusions
[exclusion_index
]) {
1724 ret
= -LTTNG_ERR_NOMEM
;
1730 event
.exclusion
= 1;
1731 } else if (!strcmp((const char *) node
->name
,
1732 config_element_attributes
)) {
1733 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1736 if (!attribute_node
) {
1737 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1741 if (!strcmp((const char *) node
->name
,
1742 config_element_probe_attributes
)) {
1743 xmlNodePtr probe_attribute_node
;
1745 /* probe_attributes */
1746 for (probe_attribute_node
=
1747 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1748 probe_attribute_node
= xmlNextElementSibling(
1749 probe_attribute_node
)) {
1751 ret
= process_probe_attribute_node(probe_attribute_node
,
1760 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1762 /* function_attributes */
1763 content
= xmlNodeGetContent(symbol_node
);
1765 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1769 sym_len
= strlen((char *) content
);
1770 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1771 WARN("Function name too long.");
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1777 ret
= lttng_strncpy(
1778 event
.attr
.ftrace
.symbol_name
,
1779 (char *) content
, sym_len
);
1781 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1790 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1791 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1792 filter_expression
, exclusion_count
, exclusions
);
1794 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1795 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1801 for (i
= 0; i
< exclusion_count
; i
++) {
1802 free(exclusions
[i
]);
1806 free(filter_expression
);
1811 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1812 const char *channel_name
)
1815 struct lttng_event event
;
1818 assert(events_node
);
1820 assert(channel_name
);
1822 for (node
= xmlFirstElementChild(events_node
); node
;
1823 node
= xmlNextElementSibling(node
)) {
1824 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1831 * Disable all events to enable only the necessary events.
1832 * Limitations regarding lttng_disable_events and tuple descriptor
1833 * force this approach.
1835 memset(&event
, 0, sizeof(event
));
1836 event
.loglevel
= -1;
1837 event
.type
= LTTNG_EVENT_ALL
;
1838 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1843 for (node
= xmlFirstElementChild(events_node
); node
;
1844 node
= xmlNextElementSibling(node
)) {
1845 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1856 int process_channel_attr_node(xmlNodePtr attr_node
,
1857 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1858 xmlNodePtr
*events_node
)
1864 assert(contexts_node
);
1865 assert(events_node
);
1867 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1871 content
= xmlNodeGetContent(attr_node
);
1873 ret
= -LTTNG_ERR_NOMEM
;
1877 ret
= lttng_strncpy(channel
->name
,
1878 (const char *) content
,
1879 LTTNG_SYMBOL_NAME_LEN
);
1881 WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
1882 (const char *) content
,
1883 strlen((const char *) content
),
1884 LTTNG_SYMBOL_NAME_LEN
);
1885 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1890 } else if (!strcmp((const char *) attr_node
->name
,
1891 config_element_enabled
)) {
1896 content
= xmlNodeGetContent(attr_node
);
1898 ret
= -LTTNG_ERR_NOMEM
;
1902 ret
= parse_bool(content
, &enabled
);
1905 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1909 channel
->enabled
= enabled
;
1910 } else if (!strcmp((const char *) attr_node
->name
,
1911 config_element_overwrite_mode
)) {
1914 /* overwrite_mode */
1915 content
= xmlNodeGetContent(attr_node
);
1917 ret
= -LTTNG_ERR_NOMEM
;
1921 ret
= get_overwrite_mode(content
);
1924 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1928 channel
->attr
.overwrite
= ret
;
1929 } else if (!strcmp((const char *) attr_node
->name
,
1930 config_element_subbuf_size
)) {
1933 /* subbuffer_size */
1934 content
= xmlNodeGetContent(attr_node
);
1936 ret
= -LTTNG_ERR_NOMEM
;
1940 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1943 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1946 } else if (!strcmp((const char *) attr_node
->name
,
1947 config_element_num_subbuf
)) {
1950 /* subbuffer_count */
1951 content
= xmlNodeGetContent(attr_node
);
1953 ret
= -LTTNG_ERR_NOMEM
;
1957 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1960 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1963 } else if (!strcmp((const char *) attr_node
->name
,
1964 config_element_switch_timer_interval
)) {
1966 uint64_t switch_timer_interval
= 0;
1968 /* switch_timer_interval */
1969 content
= xmlNodeGetContent(attr_node
);
1971 ret
= -LTTNG_ERR_NOMEM
;
1975 ret
= parse_uint(content
, &switch_timer_interval
);
1978 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1982 if (switch_timer_interval
> UINT_MAX
) {
1983 WARN("switch_timer_interval out of range.");
1984 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1988 channel
->attr
.switch_timer_interval
=
1989 switch_timer_interval
;
1990 } else if (!strcmp((const char *) attr_node
->name
,
1991 config_element_read_timer_interval
)) {
1993 uint64_t read_timer_interval
= 0;
1995 /* read_timer_interval */
1996 content
= xmlNodeGetContent(attr_node
);
1998 ret
= -LTTNG_ERR_NOMEM
;
2002 ret
= parse_uint(content
, &read_timer_interval
);
2005 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2009 if (read_timer_interval
> UINT_MAX
) {
2010 WARN("read_timer_interval out of range.");
2011 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2015 channel
->attr
.read_timer_interval
=
2016 read_timer_interval
;
2017 } else if (!strcmp((const char *) attr_node
->name
,
2018 config_element_output_type
)) {
2022 content
= xmlNodeGetContent(attr_node
);
2024 ret
= -LTTNG_ERR_NOMEM
;
2028 ret
= get_output_type(content
);
2031 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2035 channel
->attr
.output
= ret
;
2036 } else if (!strcmp((const char *) attr_node
->name
,
2037 config_element_tracefile_size
)) {
2040 /* tracefile_size */
2041 content
= xmlNodeGetContent(attr_node
);
2043 ret
= -LTTNG_ERR_NOMEM
;
2047 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2050 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2053 } else if (!strcmp((const char *) attr_node
->name
,
2054 config_element_tracefile_count
)) {
2057 /* tracefile_count */
2058 content
= xmlNodeGetContent(attr_node
);
2060 ret
= -LTTNG_ERR_NOMEM
;
2064 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2067 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2070 } else if (!strcmp((const char *) attr_node
->name
,
2071 config_element_live_timer_interval
)) {
2073 uint64_t live_timer_interval
= 0;
2075 /* live_timer_interval */
2076 content
= xmlNodeGetContent(attr_node
);
2078 ret
= -LTTNG_ERR_NOMEM
;
2082 ret
= parse_uint(content
, &live_timer_interval
);
2085 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2089 if (live_timer_interval
> UINT_MAX
) {
2090 WARN("live_timer_interval out of range.");
2091 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2095 channel
->attr
.live_timer_interval
=
2096 live_timer_interval
;
2097 } else if (!strcmp((const char *) attr_node
->name
,
2098 config_element_monitor_timer_interval
)) {
2100 uint64_t monitor_timer_interval
= 0;
2102 /* monitor_timer_interval */
2103 content
= xmlNodeGetContent(attr_node
);
2105 ret
= -LTTNG_ERR_NOMEM
;
2109 ret
= parse_uint(content
, &monitor_timer_interval
);
2112 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2116 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2117 monitor_timer_interval
);
2119 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2122 } else if (!strcmp((const char *) attr_node
->name
,
2123 config_element_blocking_timeout
)) {
2125 int64_t blocking_timeout
= 0;
2127 /* blocking_timeout */
2128 content
= xmlNodeGetContent(attr_node
);
2130 ret
= -LTTNG_ERR_NOMEM
;
2134 ret
= parse_int(content
, &blocking_timeout
);
2137 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2141 ret
= lttng_channel_set_blocking_timeout(channel
,
2144 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2147 } else if (!strcmp((const char *) attr_node
->name
,
2148 config_element_events
)) {
2150 *events_node
= attr_node
;
2153 *contexts_node
= attr_node
;
2161 int process_context_node(xmlNodePtr context_node
,
2162 struct lttng_handle
*handle
, const char *channel_name
)
2165 struct lttng_event_context context
;
2166 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2169 assert(channel_name
);
2171 if (!context_child_node
) {
2172 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2176 memset(&context
, 0, sizeof(context
));
2178 if (!strcmp((const char *) context_child_node
->name
,
2179 config_element_type
)) {
2181 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2184 ret
= -LTTNG_ERR_NOMEM
;
2188 ret
= get_context_type(content
);
2191 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2196 } else if (!strcmp((const char *) context_child_node
->name
,
2197 config_element_context_perf
)) {
2199 xmlNodePtr perf_attr_node
;
2201 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2202 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2203 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2204 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2205 perf_attr_node
; perf_attr_node
=
2206 xmlNextElementSibling(perf_attr_node
)) {
2207 if (!strcmp((const char *) perf_attr_node
->name
,
2208 config_element_type
)) {
2213 content
= xmlNodeGetContent(perf_attr_node
);
2215 ret
= -LTTNG_ERR_NOMEM
;
2219 ret
= parse_uint(content
, &type
);
2222 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2226 if (type
> UINT32_MAX
) {
2227 WARN("perf context type out of range.");
2228 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2232 context
.u
.perf_counter
.type
= type
;
2233 } else if (!strcmp((const char *) perf_attr_node
->name
,
2234 config_element_config
)) {
2236 uint64_t config
= 0;
2239 content
= xmlNodeGetContent(perf_attr_node
);
2241 ret
= -LTTNG_ERR_NOMEM
;
2245 ret
= parse_uint(content
, &config
);
2248 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2252 context
.u
.perf_counter
.config
= config
;
2253 } else if (!strcmp((const char *) perf_attr_node
->name
,
2254 config_element_name
)) {
2258 content
= xmlNodeGetContent(perf_attr_node
);
2260 ret
= -LTTNG_ERR_NOMEM
;
2264 ret
= lttng_strncpy(context
.u
.perf_counter
.name
,
2265 (const char *) content
,
2266 LTTNG_SYMBOL_NAME_LEN
);
2268 WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration",
2269 (const char *) content
,
2270 strlen((const char *) content
),
2271 LTTNG_SYMBOL_NAME_LEN
);
2272 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2279 } else if (!strcmp((const char *) context_child_node
->name
,
2280 config_element_context_app
)) {
2281 /* application context */
2282 xmlNodePtr app_ctx_node
;
2284 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2285 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2286 app_ctx_node
; app_ctx_node
=
2287 xmlNextElementSibling(app_ctx_node
)) {
2289 char **target
= strcmp(
2290 (const char *) app_ctx_node
->name
,
2291 config_element_context_app_provider_name
) == 0 ?
2292 &context
.u
.app_ctx
.provider_name
:
2293 &context
.u
.app_ctx
.ctx_name
;
2295 content
= xmlNodeGetContent(app_ctx_node
);
2297 ret
= -LTTNG_ERR_NOMEM
;
2301 *target
= (char *) content
;
2304 /* Unrecognized context type */
2305 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2309 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2310 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2311 free(context
.u
.app_ctx
.provider_name
);
2312 free(context
.u
.app_ctx
.ctx_name
);
2319 int process_contexts_node(xmlNodePtr contexts_node
,
2320 struct lttng_handle
*handle
, const char *channel_name
)
2323 xmlNodePtr context_node
;
2325 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2326 context_node
= xmlNextElementSibling(context_node
)) {
2327 ret
= process_context_node(context_node
, handle
, channel_name
);
2337 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2338 struct lttng_handle
*handle
)
2341 xmlNodePtr targets_node
= NULL
;
2345 assert(pid_tracker_node
);
2346 /* get the targets node */
2347 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2348 node
= xmlNextElementSibling(node
)) {
2349 if (!strcmp((const char *) node
->name
,
2350 config_element_targets
)) {
2351 targets_node
= node
;
2356 if (!targets_node
) {
2357 ret
= LTTNG_ERR_INVALID
;
2361 /* Go through all pid_target node */
2362 child
= xmlChildElementCount(targets_node
);
2364 /* The session is explicitly set to target nothing. */
2365 ret
= lttng_untrack_pid(handle
, -1);
2370 for (node
= xmlFirstElementChild(targets_node
); node
;
2371 node
= xmlNextElementSibling(node
)) {
2372 xmlNodePtr pid_target_node
= node
;
2374 /* get pid node and track it */
2375 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2376 node
= xmlNextElementSibling(node
)) {
2377 if (!strcmp((const char *) node
->name
,
2378 config_element_pid
)) {
2380 xmlChar
*content
= NULL
;
2382 content
= xmlNodeGetContent(node
);
2384 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2388 ret
= parse_int(content
, &pid
);
2391 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2395 ret
= lttng_track_pid(handle
, (int) pid
);
2401 node
= pid_target_node
;
2409 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2412 struct lttng_domain domain
= { 0 };
2413 struct lttng_handle
*handle
= NULL
;
2414 struct lttng_channel
*channel
= NULL
;
2415 xmlNodePtr channels_node
= NULL
;
2416 xmlNodePtr trackers_node
= NULL
;
2417 xmlNodePtr pid_tracker_node
= NULL
;
2420 assert(session_name
);
2422 ret
= init_domain(domain_node
, &domain
);
2427 handle
= lttng_create_handle(session_name
, &domain
);
2429 ret
= -LTTNG_ERR_NOMEM
;
2433 /* get the channels node */
2434 for (node
= xmlFirstElementChild(domain_node
); node
;
2435 node
= xmlNextElementSibling(node
)) {
2436 if (!strcmp((const char *) node
->name
,
2437 config_element_channels
)) {
2438 channels_node
= node
;
2443 if (!channels_node
) {
2447 /* create all channels */
2448 for (node
= xmlFirstElementChild(channels_node
); node
;
2449 node
= xmlNextElementSibling(node
)) {
2450 const enum lttng_domain_type original_domain
= domain
.type
;
2451 xmlNodePtr contexts_node
= NULL
;
2452 xmlNodePtr events_node
= NULL
;
2453 xmlNodePtr channel_attr_node
;
2456 * Channels of the "agent" types cannot be created directly.
2457 * They are meant to be created implicitly through the
2458 * activation of events in their domain. However, a user
2459 * can override the default channel configuration attributes
2460 * by creating the underlying UST channel _before_ enabling
2461 * an agent domain event.
2463 * Hence, the channel's type is substituted before the creation
2464 * and restored by the time the events are created.
2466 switch (domain
.type
) {
2467 case LTTNG_DOMAIN_JUL
:
2468 case LTTNG_DOMAIN_LOG4J
:
2469 case LTTNG_DOMAIN_PYTHON
:
2470 domain
.type
= LTTNG_DOMAIN_UST
;
2475 channel
= lttng_channel_create(&domain
);
2481 for (channel_attr_node
= xmlFirstElementChild(node
);
2482 channel_attr_node
; channel_attr_node
=
2483 xmlNextElementSibling(channel_attr_node
)) {
2484 ret
= process_channel_attr_node(channel_attr_node
,
2485 channel
, &contexts_node
, &events_node
);
2491 ret
= lttng_enable_channel(handle
, channel
);
2496 /* Restore the original channel domain. */
2497 domain
.type
= original_domain
;
2499 ret
= process_events_node(events_node
, handle
, channel
->name
);
2504 ret
= process_contexts_node(contexts_node
, handle
,
2510 lttng_channel_destroy(channel
);
2514 /* get the trackers node */
2515 for (node
= xmlFirstElementChild(domain_node
); node
;
2516 node
= xmlNextElementSibling(node
)) {
2517 if (!strcmp((const char *) node
->name
,
2518 config_element_trackers
)) {
2519 trackers_node
= node
;
2524 if (!trackers_node
) {
2528 for (node
= xmlFirstElementChild(trackers_node
); node
;
2529 node
= xmlNextElementSibling(node
)) {
2530 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2531 pid_tracker_node
= node
;
2532 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2539 if (!pid_tracker_node
) {
2540 lttng_track_pid(handle
, -1);
2544 lttng_channel_destroy(channel
);
2545 lttng_destroy_handle(handle
);
2550 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2552 const struct config_load_session_override_attr
*overrides
)
2554 int ret
, started
= -1, snapshot_mode
= -1;
2555 uint64_t live_timer_interval
= UINT64_MAX
,
2556 rotation_timer_interval
= 0,
2558 xmlChar
*name
= NULL
;
2559 xmlChar
*shm_path
= NULL
;
2560 xmlNodePtr domains_node
= NULL
;
2561 xmlNodePtr output_node
= NULL
;
2563 xmlNodePtr attributes_child
;
2564 struct lttng_domain
*kernel_domain
= NULL
;
2565 struct lttng_domain
*ust_domain
= NULL
;
2566 struct lttng_domain
*jul_domain
= NULL
;
2567 struct lttng_domain
*log4j_domain
= NULL
;
2568 struct lttng_domain
*python_domain
= NULL
;
2570 for (node
= xmlFirstElementChild(session_node
); node
;
2571 node
= xmlNextElementSibling(node
)) {
2572 if (!name
&& !strcmp((const char *) node
->name
,
2573 config_element_name
)) {
2575 xmlChar
*node_content
= xmlNodeGetContent(node
);
2576 if (!node_content
) {
2577 ret
= -LTTNG_ERR_NOMEM
;
2581 name
= node_content
;
2582 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2583 config_element_domains
)) {
2585 domains_node
= node
;
2586 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2587 config_element_started
)) {
2589 xmlChar
*node_content
= xmlNodeGetContent(node
);
2590 if (!node_content
) {
2591 ret
= -LTTNG_ERR_NOMEM
;
2595 ret
= parse_bool(node_content
, &started
);
2598 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2601 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2602 config_element_output
)) {
2605 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2606 config_element_shared_memory_path
)) {
2607 /* shared memory path */
2608 xmlChar
*node_content
= xmlNodeGetContent(node
);
2609 if (!node_content
) {
2610 ret
= -LTTNG_ERR_NOMEM
;
2614 shm_path
= node_content
;
2617 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2618 * rotation_timer_interval.
2620 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
2621 attributes_child
= xmlNextElementSibling(attributes_child
)) {
2622 if (!strcmp((const char *) attributes_child
->name
,
2623 config_element_snapshot_mode
)) {
2625 xmlChar
*snapshot_mode_content
=
2626 xmlNodeGetContent(attributes_child
);
2627 if (!snapshot_mode_content
) {
2628 ret
= -LTTNG_ERR_NOMEM
;
2632 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2633 free(snapshot_mode_content
);
2635 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2638 } else if (!strcmp((const char *) attributes_child
->name
,
2639 config_element_live_timer_interval
)) {
2640 /* live_timer_interval */
2641 xmlChar
*timer_interval_content
=
2642 xmlNodeGetContent(attributes_child
);
2643 if (!timer_interval_content
) {
2644 ret
= -LTTNG_ERR_NOMEM
;
2648 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2649 free(timer_interval_content
);
2651 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2655 if (!strcmp((const char *) attributes_child
->name
,
2656 config_element_rotation_timer_interval
)) {
2657 /* rotation_timer_interval */
2658 xmlChar
*timer_interval_content
=
2659 xmlNodeGetContent(attributes_child
);
2660 if (!timer_interval_content
) {
2661 ret
= -LTTNG_ERR_NOMEM
;
2665 ret
= parse_uint(timer_interval_content
, &rotation_timer_interval
);
2666 free(timer_interval_content
);
2668 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2672 if (!strcmp((const char *) attributes_child
->name
,
2673 config_element_rotation_size
)) {
2675 xmlChar
*rotation_size_content
=
2676 xmlNodeGetContent(attributes_child
);
2677 if (!rotation_size_content
) {
2678 ret
= -LTTNG_ERR_NOMEM
;
2682 ret
= parse_uint(rotation_size_content
, &rotation_size
);
2683 free(rotation_size_content
);
2685 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2694 /* Mandatory attribute, as defined in the session XSD */
2695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2699 if (session_name
&& strcmp((char *) name
, session_name
)) {
2700 /* This is not the session we are looking for */
2701 ret
= -LTTNG_ERR_NO_SESSION
;
2705 /* Init domains to create the session handles */
2706 for (node
= xmlFirstElementChild(domains_node
); node
;
2707 node
= xmlNextElementSibling(node
)) {
2708 struct lttng_domain
*domain
;
2710 domain
= zmalloc(sizeof(*domain
));
2712 ret
= -LTTNG_ERR_NOMEM
;
2716 ret
= init_domain(node
, domain
);
2718 goto domain_init_error
;
2721 switch (domain
->type
) {
2722 case LTTNG_DOMAIN_KERNEL
:
2723 if (kernel_domain
) {
2724 /* Same domain seen twice, invalid! */
2725 goto domain_init_error
;
2727 kernel_domain
= domain
;
2729 case LTTNG_DOMAIN_UST
:
2731 /* Same domain seen twice, invalid! */
2732 goto domain_init_error
;
2734 ust_domain
= domain
;
2736 case LTTNG_DOMAIN_JUL
:
2738 /* Same domain seen twice, invalid! */
2739 goto domain_init_error
;
2741 jul_domain
= domain
;
2743 case LTTNG_DOMAIN_LOG4J
:
2745 /* Same domain seen twice, invalid! */
2746 goto domain_init_error
;
2748 log4j_domain
= domain
;
2750 case LTTNG_DOMAIN_PYTHON
:
2751 if (python_domain
) {
2752 /* Same domain seen twice, invalid! */
2753 goto domain_init_error
;
2755 python_domain
= domain
;
2758 WARN("Invalid domain type");
2759 goto domain_init_error
;
2764 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2768 /* Apply overrides */
2770 if (overrides
->session_name
) {
2771 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2772 if (!name_override
) {
2773 ret
= -LTTNG_ERR_NOMEM
;
2777 /* Overrides the session name to the provided name */
2779 name
= name_override
;
2784 /* Destroy session if it exists */
2785 ret
= lttng_destroy_session((const char *) name
);
2786 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2787 ERR("Failed to destroy existing session.");
2792 /* Create session type depending on output type */
2793 if (snapshot_mode
&& snapshot_mode
!= -1) {
2794 ret
= create_snapshot_session((const char *) name
, output_node
,
2796 } else if (live_timer_interval
&&
2797 live_timer_interval
!= UINT64_MAX
) {
2798 ret
= create_session((const char *) name
,
2799 output_node
, live_timer_interval
, overrides
);
2801 /* regular session */
2802 ret
= create_session((const char *) name
,
2803 output_node
, UINT64_MAX
, overrides
);
2810 ret
= lttng_set_session_shm_path((const char *) name
,
2811 (const char *) shm_path
);
2817 for (node
= xmlFirstElementChild(domains_node
); node
;
2818 node
= xmlNextElementSibling(node
)) {
2819 ret
= process_domain_node(node
, (const char *) name
);
2825 if (rotation_timer_interval
|| rotation_size
) {
2826 struct lttng_rotation_schedule_attr
*rotation_attr
= lttng_rotation_schedule_attr_create();
2828 if (!rotation_attr
) {
2831 ret
= lttng_rotation_schedule_attr_set_session_name(rotation_attr
, (const char *) name
);
2833 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2836 lttng_rotation_schedule_attr_set_timer_period(rotation_attr
,
2837 rotation_timer_interval
);
2838 lttng_rotation_schedule_attr_set_size(rotation_attr
, rotation_size
);
2839 ret
= lttng_rotation_set_schedule(rotation_attr
);
2840 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2847 ret
= lttng_start_tracing((const char *) name
);
2855 ERR("Failed to load session %s: %s", (const char *) name
,
2856 lttng_strerror(ret
));
2857 lttng_destroy_session((const char *) name
);
2861 free(kernel_domain
);
2865 free(python_domain
);
2872 * Return 1 if the given path is readable by the current UID or 0 if not.
2873 * Return -1 if the path is EPERM.
2875 static int validate_file_read_creds(const char *path
)
2881 /* Can we read the file. */
2882 ret
= access(path
, R_OK
);
2886 if (errno
== EACCES
) {
2897 int load_session_from_file(const char *path
, const char *session_name
,
2898 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2899 const struct config_load_session_override_attr
*overrides
)
2901 int ret
, session_found
= !session_name
;
2902 xmlDocPtr doc
= NULL
;
2903 xmlNodePtr sessions_node
;
2904 xmlNodePtr session_node
;
2907 assert(validation_ctx
);
2909 ret
= validate_file_read_creds(path
);
2912 ret
= -LTTNG_ERR_EPERM
;
2914 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2919 doc
= xmlParseFile(path
);
2921 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2925 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2927 ERR("Session configuration file validation failed");
2928 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2932 sessions_node
= xmlDocGetRootElement(doc
);
2933 if (!sessions_node
) {
2937 for (session_node
= xmlFirstElementChild(sessions_node
);
2938 session_node
; session_node
=
2939 xmlNextElementSibling(session_node
)) {
2940 ret
= process_session_node(session_node
,
2941 session_name
, overwrite
, overrides
);
2942 if (session_name
&& ret
== 0) {
2943 /* Target session found and loaded */
2951 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2957 int load_session_from_path(const char *path
, const char *session_name
,
2958 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2959 const struct config_load_session_override_attr
*overrides
)
2961 int ret
, session_found
= !session_name
;
2962 DIR *directory
= NULL
;
2963 struct lttng_dynamic_buffer file_path
;
2967 assert(validation_ctx
);
2968 path_len
= strlen(path
);
2969 lttng_dynamic_buffer_init(&file_path
);
2970 if (path_len
>= LTTNG_PATH_MAX
) {
2971 ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)",
2972 path
, path_len
, LTTNG_PATH_MAX
);
2973 ret
= -LTTNG_ERR_INVALID
;
2977 directory
= opendir(path
);
2981 /* Try the file loading. */
2984 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2987 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2992 size_t file_path_root_len
;
2994 ret
= lttng_dynamic_buffer_set_capacity(&file_path
,
2997 ret
= -LTTNG_ERR_NOMEM
;
3001 ret
= lttng_dynamic_buffer_append(&file_path
, path
, path_len
);
3003 ret
= -LTTNG_ERR_NOMEM
;
3007 if (file_path
.data
[file_path
.size
- 1] != '/') {
3008 ret
= lttng_dynamic_buffer_append(&file_path
, "/", 1);
3010 ret
= -LTTNG_ERR_NOMEM
;
3014 file_path_root_len
= file_path
.size
;
3016 /* Search for *.lttng files */
3018 size_t file_name_len
;
3019 struct dirent
*result
;
3022 * When the end of the directory stream is reached, NULL
3023 * is returned and errno is kept unchanged. When an
3024 * error occurs, NULL is returned and errno is set
3025 * accordingly. To distinguish between the two, set
3026 * errno to zero before calling readdir().
3028 * On success, readdir() returns a pointer to a dirent
3029 * structure. This structure may be statically
3030 * allocated, do not attempt to free(3) it.
3033 result
= readdir(directory
);
3035 /* Reached end of dir stream or error out. */
3038 PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path
);
3039 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
3045 file_name_len
= strlen(result
->d_name
);
3047 if (file_name_len
<=
3048 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3052 if (file_path
.size
+ file_name_len
>= LTTNG_PATH_MAX
) {
3053 WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)",
3055 /* +1 to account for NULL terminator. */
3056 file_path
.size
+ file_name_len
+ 1,
3061 /* Does the file end with .lttng? */
3062 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3063 result
->d_name
+ file_name_len
- sizeof(
3064 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3068 ret
= lttng_dynamic_buffer_append(&file_path
, result
->d_name
,
3071 ret
= -LTTNG_ERR_NOMEM
;
3075 ret
= load_session_from_file(file_path
.data
, session_name
,
3076 validation_ctx
, overwrite
, overrides
);
3077 if (session_name
&& !ret
) {
3082 * Reset the buffer's size to the location of the
3083 * path's trailing '/'.
3085 ret
= lttng_dynamic_buffer_set_size(&file_path
,
3086 file_path_root_len
);
3088 ret
= -LTTNG_ERR_UNK
;
3094 ret
= load_session_from_file(path
, session_name
,
3095 validation_ctx
, overwrite
, overrides
);
3105 if (closedir(directory
)) {
3109 if (session_found
&& !ret
) {
3112 lttng_dynamic_buffer_reset(&file_path
);
3117 * Validate that the given path's credentials and the current process have the
3118 * same UID. If so, return 1 else return 0 if it does NOT match.
3120 static int validate_path_creds(const char *path
)
3122 int ret
, uid
= getuid();
3131 ret
= stat(path
, &buf
);
3133 if (errno
!= ENOENT
) {
3139 if (buf
.st_uid
!= uid
) {
3150 int config_load_session(const char *path
, const char *session_name
,
3151 int overwrite
, unsigned int autoload
,
3152 const struct config_load_session_override_attr
*overrides
)
3155 bool session_loaded
= false;
3156 const char *path_ptr
= NULL
;
3157 struct session_config_validation_ctx validation_ctx
= { 0 };
3159 ret
= init_session_config_validation_ctx(&validation_ctx
);
3166 const char *sys_path
;
3169 home_path
= utils_get_home_dir();
3171 char path
[PATH_MAX
];
3174 * Try user session configuration path. Ignore error here so we can
3175 * continue loading the system wide sessions.
3178 ret
= snprintf(path
, sizeof(path
),
3179 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3180 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3182 PERROR("snprintf session autoload home config path");
3187 * Credentials are only validated for the autoload in order to
3188 * avoid any user session daemon to try to load kernel sessions
3189 * automatically and failing all the times.
3191 ret
= validate_path_creds(path
);
3196 ret
= snprintf(path
, sizeof(path
),
3197 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3199 PERROR("snprintf session home config path");
3205 ret
= load_session_from_path(path_ptr
, session_name
,
3206 &validation_ctx
, overwrite
, overrides
);
3207 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3211 * Continue even if the session was found since we have to try
3212 * the system wide sessions.
3214 session_loaded
= true;
3218 /* Reset path pointer for the system wide dir. */
3221 /* Try system wide configuration directory. */
3223 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3224 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3225 ret
= validate_path_creds(sys_path
);
3227 path_ptr
= sys_path
;
3230 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3231 path_ptr
= sys_path
;
3235 ret
= load_session_from_path(path_ptr
, session_name
,
3236 &validation_ctx
, overwrite
, overrides
);
3238 session_loaded
= true;
3242 ret
= access(path
, F_OK
);
3247 ret
= -LTTNG_ERR_INVALID
;
3248 WARN("Session configuration path does not exist.");
3251 ret
= -LTTNG_ERR_EPERM
;
3254 ret
= -LTTNG_ERR_UNK
;
3260 ret
= load_session_from_path(path
, session_name
,
3261 &validation_ctx
, overwrite
, overrides
);
3264 fini_session_config_validation_ctx(&validation_ctx
);
3265 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3267 * Don't report an error if no sessions are found when called
3268 * without a session_name or a search path.
3273 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3274 /* A matching session was found in one of the search paths. */
3281 void __attribute__((destructor
)) session_config_exit(void)