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 <lttng/lttng.h>
42 #include <lttng/snapshot.h>
44 #include "session-config.h"
45 #include "config-internal.h"
47 struct handler_filter_args
{
49 config_entry_handler_cb handler
;
53 struct session_config_validation_ctx
{
54 xmlSchemaParserCtxtPtr parser_ctx
;
56 xmlSchemaValidCtxtPtr schema_validation_ctx
;
59 const char * const config_element_all
= "all";
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_enabled
= "enabled";
89 const char * const config_element_overwrite_mode
= "overwrite_mode";
90 const char * const config_element_subbuf_size
= "subbuffer_size";
91 const char * const config_element_num_subbuf
= "subbuffer_count";
92 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
93 const char * const config_element_read_timer_interval
= "read_timer_interval";
94 const char * const config_element_output
= "output";
95 const char * const config_element_output_type
= "output_type";
96 const char * const config_element_tracefile_size
= "tracefile_size";
97 const char * const config_element_tracefile_count
= "tracefile_count";
98 const char * const config_element_live_timer_interval
= "live_timer_interval";
99 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
100 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
101 const char * const config_element_type
= "type";
102 const char * const config_element_buffer_type
= "buffer_type";
103 const char * const config_element_session
= "session";
104 const char * const config_element_sessions
= "sessions";
105 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
106 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
107 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
108 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
109 const char * const config_element_config
= "config";
110 const char * const config_element_started
= "started";
111 const char * const config_element_snapshot_mode
= "snapshot_mode";
112 const char * const config_element_loglevel
= "loglevel";
113 const char * const config_element_loglevel_type
= "loglevel_type";
114 const char * const config_element_filter
= "filter";
115 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
116 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
117 const char * const config_element_consumer_output
= "consumer_output";
118 const char * const config_element_destination
= "destination";
119 const char * const config_element_path
= "path";
120 const char * const config_element_net_output
= "net_output";
121 const char * const config_element_control_uri
= "control_uri";
122 const char * const config_element_data_uri
= "data_uri";
123 const char * const config_element_max_size
= "max_size";
124 const char * const config_element_pid
= "pid";
125 const char * const config_element_id
= "id";
126 const char * const config_element_pids
= "pids";
127 const char * const config_element_name
= "name";
128 const char * const config_element_shared_memory_path
= "shared_memory_path";
129 const char * const config_element_pid_tracker
= "pid_tracker";
130 const char * const config_element_vpid_tracker
= "vpid_tracker";
131 const char * const config_element_uid_tracker
= "uid_tracker";
132 const char * const config_element_vuid_tracker
= "vuid_tracker";
133 const char * const config_element_gid_tracker
= "gid_tracker";
134 const char * const config_element_vgid_tracker
= "vgid_tracker";
135 const char * const config_element_trackers
= "trackers";
136 const char * const config_element_targets
= "targets";
137 const char * const config_element_target_type
= "target_type";
138 const char * const config_element_target_pid
= "pid_target";
139 const char * const config_element_target_vpid
= "vpid_target";
140 const char * const config_element_target_uid
= "uid_target";
141 const char * const config_element_target_vuid
= "vuid_target";
142 const char * const config_element_target_gid
= "gid_target";
143 const char * const config_element_target_vgid
= "vgid_target";
144 const char * const config_element_tracker_type
= "tracker_type";
146 const char * const config_domain_type_kernel
= "KERNEL";
147 const char * const config_domain_type_ust
= "UST";
148 const char * const config_domain_type_jul
= "JUL";
149 const char * const config_domain_type_log4j
= "LOG4J";
150 const char * const config_domain_type_python
= "PYTHON";
152 const char * const config_buffer_type_per_pid
= "PER_PID";
153 const char * const config_buffer_type_per_uid
= "PER_UID";
154 const char * const config_buffer_type_global
= "GLOBAL";
156 const char * const config_overwrite_mode_discard
= "DISCARD";
157 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
159 const char * const config_output_type_splice
= "SPLICE";
160 const char * const config_output_type_mmap
= "MMAP";
162 const char * const config_loglevel_type_all
= "ALL";
163 const char * const config_loglevel_type_range
= "RANGE";
164 const char * const config_loglevel_type_single
= "SINGLE";
166 const char * const config_event_type_all
= "ALL";
167 const char * const config_event_type_tracepoint
= "TRACEPOINT";
168 const char * const config_event_type_probe
= "PROBE";
169 const char * const config_event_type_function
= "FUNCTION";
170 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
171 const char * const config_event_type_noop
= "NOOP";
172 const char * const config_event_type_syscall
= "SYSCALL";
173 const char * const config_event_type_kprobe
= "KPROBE";
174 const char * const config_event_type_kretprobe
= "KRETPROBE";
176 const char * const config_event_context_pid
= "PID";
177 const char * const config_event_context_procname
= "PROCNAME";
178 const char * const config_event_context_prio
= "PRIO";
179 const char * const config_event_context_nice
= "NICE";
180 const char * const config_event_context_vpid
= "VPID";
181 const char * const config_event_context_tid
= "TID";
182 const char * const config_event_context_vtid
= "VTID";
183 const char * const config_event_context_ppid
= "PPID";
184 const char * const config_event_context_vppid
= "VPPID";
185 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
186 const char * const config_event_context_hostname
= "HOSTNAME";
187 const char * const config_event_context_ip
= "IP";
188 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
189 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
190 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
191 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
192 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
193 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
195 /* Deprecated symbols */
196 const char * const config_element_perf
;
198 enum process_event_node_phase
{
203 struct consumer_output
{
210 static int config_entry_handler_filter(struct handler_filter_args
*args
,
211 const char *section
, const char *name
, const char *value
)
214 struct config_entry entry
= { section
, name
, value
};
218 if (!section
|| !name
|| !value
) {
224 if (strcmp(args
->section
, section
)) {
229 ret
= args
->handler(&entry
, args
->user_data
);
235 int config_get_section_entries(const char *override_path
, const char *section
,
236 config_entry_handler_cb handler
, void *user_data
)
240 FILE *config_file
= NULL
;
241 struct handler_filter_args filter
= { section
, handler
, user_data
};
243 /* First, try system-wide conf. file. */
244 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
246 config_file
= fopen(path
, "r");
248 DBG("Loading daemon conf file at %s", path
);
250 * Return value is not very important here since error or not, we
251 * continue and try the next possible conf. file.
253 (void) ini_parse_file(config_file
,
254 (ini_entry_handler
) config_entry_handler_filter
,
259 /* Second is the user local configuration. */
260 path
= utils_get_home_dir();
262 char fullpath
[PATH_MAX
];
264 ret
= snprintf(fullpath
, sizeof(fullpath
),
265 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
267 PERROR("snprintf user conf. path");
271 config_file
= fopen(fullpath
, "r");
273 DBG("Loading daemon user conf file at %s", path
);
275 * Return value is not very important here since error or not, we
276 * continue and try the next possible conf. file.
278 (void) ini_parse_file(config_file
,
279 (ini_entry_handler
) config_entry_handler_filter
,
285 /* Final path is the one that the user might have provided. */
287 config_file
= fopen(override_path
, "r");
289 DBG("Loading daemon command line conf file at %s", override_path
);
290 (void) ini_parse_file(config_file
,
291 (ini_entry_handler
) config_entry_handler_filter
,
295 ERR("Failed to open daemon configuration file at %s",
302 /* Everything went well. */
310 int config_parse_value(const char *value
)
313 char *endptr
, *lower_str
;
323 v
= strtoul(value
, &endptr
, 10);
324 if (endptr
!= value
) {
329 lower_str
= zmalloc(len
+ 1);
336 for (i
= 0; i
< len
; i
++) {
337 lower_str
[i
] = tolower(value
[i
]);
340 if (!strcmp(lower_str
, config_str_yes
) ||
341 !strcmp(lower_str
, config_str_true
) ||
342 !strcmp(lower_str
, config_str_on
)) {
344 } else if (!strcmp(lower_str
, config_str_no
) ||
345 !strcmp(lower_str
, config_str_false
) ||
346 !strcmp(lower_str
, config_str_off
)) {
358 * Returns a xmlChar string which must be released using xmlFree().
360 static xmlChar
*encode_string(const char *in_str
)
362 xmlChar
*out_str
= NULL
;
363 xmlCharEncodingHandlerPtr handler
;
364 int out_len
, ret
, in_len
;
368 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
370 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
374 in_len
= strlen(in_str
);
376 * Add 1 byte for the NULL terminted character. The factor 4 here is
377 * used because UTF-8 characters can take up to 4 bytes.
379 out_len
= (in_len
* 4) + 1;
380 out_str
= xmlMalloc(out_len
);
385 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
392 /* out_len is now the size of out_str */
393 out_str
[out_len
] = '\0';
399 struct config_writer
*config_writer_create(int fd_output
, int indent
)
402 struct config_writer
*writer
;
403 xmlOutputBufferPtr buffer
;
405 writer
= zmalloc(sizeof(struct config_writer
));
407 PERROR("zmalloc config_writer_create");
411 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
416 writer
->writer
= xmlNewTextWriter(buffer
);
417 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
418 config_xml_encoding
, NULL
);
423 ret
= xmlTextWriterSetIndentString(writer
->writer
,
424 BAD_CAST config_xml_indent_string
);
429 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
437 config_writer_destroy(writer
);
442 int config_writer_destroy(struct config_writer
*writer
)
451 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
452 WARN("Could not close XML document");
456 if (writer
->writer
) {
457 xmlFreeTextWriter(writer
->writer
);
466 int config_writer_open_element(struct config_writer
*writer
,
467 const char *element_name
)
470 xmlChar
*encoded_element_name
;
472 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
477 encoded_element_name
= encode_string(element_name
);
478 if (!encoded_element_name
) {
483 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
484 xmlFree(encoded_element_name
);
486 return ret
>= 0 ? 0 : ret
;
490 int config_writer_write_attribute(struct config_writer
*writer
,
491 const char *name
, const char *value
)
494 xmlChar
*encoded_name
= NULL
;
495 xmlChar
*encoded_value
= NULL
;
497 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
502 encoded_name
= encode_string(name
);
508 encoded_value
= encode_string(value
);
509 if (!encoded_value
) {
514 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
517 xmlFree(encoded_name
);
518 xmlFree(encoded_value
);
519 return ret
>= 0 ? 0 : ret
;
523 int config_writer_close_element(struct config_writer
*writer
)
527 if (!writer
|| !writer
->writer
) {
532 ret
= xmlTextWriterEndElement(writer
->writer
);
534 return ret
>= 0 ? 0 : ret
;
538 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
539 const char *element_name
, uint64_t value
)
542 xmlChar
*encoded_element_name
;
544 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
549 encoded_element_name
= encode_string(element_name
);
550 if (!encoded_element_name
) {
555 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
556 encoded_element_name
, "%" PRIu64
, value
);
557 xmlFree(encoded_element_name
);
559 return ret
>= 0 ? 0 : ret
;
563 int config_writer_write_element_signed_int(struct config_writer
*writer
,
564 const char *element_name
, int64_t value
)
567 xmlChar
*encoded_element_name
;
569 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
574 encoded_element_name
= encode_string(element_name
);
575 if (!encoded_element_name
) {
580 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
581 encoded_element_name
, "%" PRIi64
, value
);
582 xmlFree(encoded_element_name
);
584 return ret
>= 0 ? 0 : ret
;
588 int config_writer_write_element_bool(struct config_writer
*writer
,
589 const char *element_name
, int value
)
591 return config_writer_write_element_string(writer
, element_name
,
592 value
? config_xml_true
: config_xml_false
);
596 int config_writer_write_element_string(struct config_writer
*writer
,
597 const char *element_name
, const char *value
)
600 xmlChar
*encoded_element_name
= NULL
;
601 xmlChar
*encoded_value
= NULL
;
603 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
609 encoded_element_name
= encode_string(element_name
);
610 if (!encoded_element_name
) {
615 encoded_value
= encode_string(value
);
616 if (!encoded_value
) {
621 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
624 xmlFree(encoded_element_name
);
625 xmlFree(encoded_value
);
626 return ret
>= 0 ? 0 : ret
;
630 void xml_error_handler(void *ctx
, const char *format
, ...)
636 va_start(args
, format
);
637 ret
= vasprintf(&errMsg
, format
, args
);
640 ERR("String allocation failed in xml error handler");
644 fprintf(stderr
, "XML Error: %s", errMsg
);
649 void fini_session_config_validation_ctx(
650 struct session_config_validation_ctx
*ctx
)
652 if (ctx
->parser_ctx
) {
653 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
657 xmlSchemaFree(ctx
->schema
);
660 if (ctx
->schema_validation_ctx
) {
661 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
664 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
668 char *get_session_config_xsd_path()
671 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
672 size_t base_path_len
;
676 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
679 base_path_len
= strlen(base_path
);
680 max_path_len
= base_path_len
+
681 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
682 xsd_path
= zmalloc(max_path_len
);
687 strncpy(xsd_path
, base_path
, max_path_len
);
688 if (xsd_path
[base_path_len
- 1] != '/') {
689 xsd_path
[base_path_len
++] = '/';
692 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
693 max_path_len
- base_path_len
);
699 int init_session_config_validation_ctx(
700 struct session_config_validation_ctx
*ctx
)
703 char *xsd_path
= get_session_config_xsd_path();
706 ret
= -LTTNG_ERR_NOMEM
;
710 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
711 if (!ctx
->parser_ctx
) {
712 ERR("XSD parser context creation failed");
713 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
716 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
717 xml_error_handler
, NULL
);
719 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
721 ERR("XSD parsing failed");
722 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
726 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
727 if (!ctx
->schema_validation_ctx
) {
728 ERR("XSD validation context creation failed");
729 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
733 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
734 xml_error_handler
, NULL
);
739 fini_session_config_validation_ctx(ctx
);
747 int parse_uint(xmlChar
*str
, uint64_t *val
)
757 *val
= strtoull((const char *) str
, &endptr
, 10);
758 if (!endptr
|| *endptr
) {
769 int parse_int(xmlChar
*str
, int64_t *val
)
779 *val
= strtoll((const char *) str
, &endptr
, 10);
780 if (!endptr
|| *endptr
) {
791 int parse_bool(xmlChar
*str
, int *val
)
800 if (!strcmp((const char *) str
, config_xml_true
)) {
802 } else if (!strcmp((const char *) str
, config_xml_false
)) {
805 WARN("Invalid boolean value encoutered (%s).",
814 int get_domain_type(xmlChar
*domain
)
822 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
823 ret
= LTTNG_DOMAIN_KERNEL
;
824 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
825 ret
= LTTNG_DOMAIN_UST
;
826 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
827 ret
= LTTNG_DOMAIN_JUL
;
828 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
829 ret
= LTTNG_DOMAIN_LOG4J
;
830 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
831 ret
= LTTNG_DOMAIN_PYTHON
;
842 int get_buffer_type(xmlChar
*buffer_type
)
850 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
851 ret
= LTTNG_BUFFER_GLOBAL
;
852 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
853 ret
= LTTNG_BUFFER_PER_UID
;
854 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
855 ret
= LTTNG_BUFFER_PER_PID
;
866 int get_overwrite_mode(xmlChar
*overwrite_mode
)
870 if (!overwrite_mode
) {
874 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
876 } else if (!strcmp((char *) overwrite_mode
,
877 config_overwrite_mode_discard
)) {
889 int get_output_type(xmlChar
*output_type
)
897 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
898 ret
= LTTNG_EVENT_MMAP
;
899 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
900 ret
= LTTNG_EVENT_SPLICE
;
911 int get_event_type(xmlChar
*event_type
)
919 if (!strcmp((char *) event_type
, config_event_type_all
)) {
920 ret
= LTTNG_EVENT_ALL
;
921 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
922 ret
= LTTNG_EVENT_TRACEPOINT
;
923 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
924 ret
= LTTNG_EVENT_PROBE
;
925 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
926 ret
= LTTNG_EVENT_FUNCTION
;
927 } else if (!strcmp((char *) event_type
,
928 config_event_type_function_entry
)) {
929 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
930 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
931 ret
= LTTNG_EVENT_NOOP
;
932 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
933 ret
= LTTNG_EVENT_SYSCALL
;
944 int get_loglevel_type(xmlChar
*loglevel_type
)
948 if (!loglevel_type
) {
952 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
953 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
954 } else if (!strcmp((char *) loglevel_type
,
955 config_loglevel_type_range
)) {
956 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
957 } else if (!strcmp((char *) loglevel_type
,
958 config_loglevel_type_single
)) {
959 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
970 * Return the context type or -1 on error.
973 int get_context_type(xmlChar
*context_type
)
981 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
982 ret
= LTTNG_EVENT_CONTEXT_PID
;
983 } else if (!strcmp((char *) context_type
,
984 config_event_context_procname
)) {
985 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
986 } else if (!strcmp((char *) context_type
,
987 config_event_context_prio
)) {
988 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
989 } else if (!strcmp((char *) context_type
,
990 config_event_context_nice
)) {
991 ret
= LTTNG_EVENT_CONTEXT_NICE
;
992 } else if (!strcmp((char *) context_type
,
993 config_event_context_vpid
)) {
994 ret
= LTTNG_EVENT_CONTEXT_VPID
;
995 } else if (!strcmp((char *) context_type
,
996 config_event_context_tid
)) {
997 ret
= LTTNG_EVENT_CONTEXT_TID
;
998 } else if (!strcmp((char *) context_type
,
999 config_event_context_vtid
)) {
1000 ret
= LTTNG_EVENT_CONTEXT_VTID
;
1001 } else if (!strcmp((char *) context_type
,
1002 config_event_context_ppid
)) {
1003 ret
= LTTNG_EVENT_CONTEXT_PPID
;
1004 } else if (!strcmp((char *) context_type
,
1005 config_event_context_vppid
)) {
1006 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
1007 } else if (!strcmp((char *) context_type
,
1008 config_event_context_pthread_id
)) {
1009 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1010 } else if (!strcmp((char *) context_type
,
1011 config_event_context_hostname
)) {
1012 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1013 } else if (!strcmp((char *) context_type
,
1014 config_event_context_ip
)) {
1015 ret
= LTTNG_EVENT_CONTEXT_IP
;
1016 } else if (!strcmp((char *) context_type
,
1017 config_event_context_interruptible
)) {
1018 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1019 } else if (!strcmp((char *) context_type
,
1020 config_event_context_preemptible
)) {
1021 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1022 } else if (!strcmp((char *) context_type
,
1023 config_event_context_need_reschedule
)) {
1024 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1025 } else if (!strcmp((char *) context_type
,
1026 config_event_context_migratable
)) {
1027 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1038 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1043 for (node
= xmlFirstElementChild(domain_node
); node
;
1044 node
= xmlNextElementSibling(node
)) {
1045 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1047 xmlChar
*node_content
= xmlNodeGetContent(node
);
1048 if (!node_content
) {
1049 ret
= -LTTNG_ERR_NOMEM
;
1053 ret
= get_domain_type(node_content
);
1056 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1061 } else if (!strcmp((const char *) node
->name
,
1062 config_element_buffer_type
)) {
1064 xmlChar
*node_content
= xmlNodeGetContent(node
);
1065 if (!node_content
) {
1066 ret
= -LTTNG_ERR_NOMEM
;
1070 ret
= get_buffer_type(node_content
);
1073 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1077 domain
->buf_type
= ret
;
1086 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1091 for (node
= xmlFirstElementChild(net_output_node
); node
;
1092 node
= xmlNextElementSibling(node
)) {
1093 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1095 *control_uri
= (char *) xmlNodeGetContent(node
);
1096 if (!*control_uri
) {
1101 *data_uri
= (char *) xmlNodeGetContent(node
);
1108 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1112 int process_consumer_output(xmlNodePtr consumer_output_node
,
1113 struct consumer_output
*output
)
1120 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1121 node
= xmlNextElementSibling(node
)) {
1122 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1123 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1127 ret
= -LTTNG_ERR_NOMEM
;
1131 ret
= parse_bool(enabled_str
, &output
->enabled
);
1137 xmlNodePtr output_type_node
;
1140 output_type_node
= xmlFirstElementChild(node
);
1141 if (!output_type_node
) {
1142 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1146 if (!strcmp((const char *) output_type_node
->name
,
1147 config_element_path
)) {
1149 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1150 if (!output
->path
) {
1151 ret
= -LTTNG_ERR_NOMEM
;
1156 ret
= get_net_output_uris(output_type_node
,
1157 &output
->control_uri
, &output
->data_uri
);
1169 free(output
->control_uri
);
1170 free(output
->data_uri
);
1171 memset(output
, 0, sizeof(struct consumer_output
));
1177 int create_session_net_output(const char *name
, const char *control_uri
,
1178 const char *data_uri
)
1181 struct lttng_handle
*handle
;
1182 const char *uri
= NULL
;
1186 handle
= lttng_create_handle(name
, NULL
);
1188 ret
= -LTTNG_ERR_NOMEM
;
1192 if (!control_uri
|| !data_uri
) {
1193 uri
= control_uri
? control_uri
: data_uri
;
1198 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1199 lttng_destroy_handle(handle
);
1205 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1206 const struct config_load_session_override_attr
*overrides
)
1209 xmlNodePtr node
= NULL
;
1210 xmlNodePtr snapshot_output_list_node
;
1211 xmlNodePtr snapshot_output_node
;
1213 assert(session_name
);
1215 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1224 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1226 /* Parse and create snapshot outputs */
1228 for (snapshot_output_node
=
1229 xmlFirstElementChild(snapshot_output_list_node
);
1230 snapshot_output_node
; snapshot_output_node
=
1231 xmlNextElementSibling(snapshot_output_node
)) {
1233 uint64_t max_size
= UINT64_MAX
;
1234 struct consumer_output output
= { 0 };
1235 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1236 const char *control_uri
= NULL
;
1237 const char *data_uri
= NULL
;
1238 const char *path
= NULL
;
1240 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1241 node
= xmlNextElementSibling(node
)) {
1242 if (!strcmp((const char *) node
->name
,
1243 config_element_name
)) {
1245 name
= (char *) xmlNodeGetContent(node
);
1247 ret
= -LTTNG_ERR_NOMEM
;
1248 goto error_snapshot_output
;
1250 } else if (!strcmp((const char *) node
->name
,
1251 config_element_max_size
)) {
1252 xmlChar
*content
= xmlNodeGetContent(node
);
1256 ret
= -LTTNG_ERR_NOMEM
;
1257 goto error_snapshot_output
;
1259 ret
= parse_uint(content
, &max_size
);
1262 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1263 goto error_snapshot_output
;
1266 /* consumer_output */
1267 ret
= process_consumer_output(node
, &output
);
1269 goto error_snapshot_output
;
1274 control_uri
= output
.control_uri
;
1275 data_uri
= output
.data_uri
;
1279 if (overrides
->path_url
) {
1280 path
= overrides
->path_url
;
1281 /* Control/data_uri are null */
1285 if (overrides
->ctrl_url
) {
1286 control_uri
= overrides
->ctrl_url
;
1290 if (overrides
->data_url
) {
1291 data_uri
= overrides
->data_url
;
1298 snapshot_output
= lttng_snapshot_output_create();
1299 if (!snapshot_output
) {
1300 ret
= -LTTNG_ERR_NOMEM
;
1301 goto error_snapshot_output
;
1304 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1306 goto error_snapshot_output
;
1309 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1311 goto error_snapshot_output
;
1315 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1318 goto error_snapshot_output
;
1322 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1325 goto error_snapshot_output
;
1330 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1333 goto error_snapshot_output
;
1338 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1339 error_snapshot_output
:
1342 free(output
.control_uri
);
1343 free(output
.data_uri
);
1344 lttng_snapshot_output_destroy(snapshot_output
);
1354 int create_session(const char *name
,
1355 struct lttng_domain
*kernel_domain
,
1356 struct lttng_domain
*ust_domain
,
1357 struct lttng_domain
*jul_domain
,
1358 struct lttng_domain
*log4j_domain
,
1359 xmlNodePtr output_node
,
1360 uint64_t live_timer_interval
,
1361 const struct config_load_session_override_attr
*overrides
)
1364 struct consumer_output output
= { 0 };
1365 xmlNodePtr consumer_output_node
;
1366 const char *control_uri
= NULL
;
1367 const char *data_uri
= NULL
;
1368 const char *path
= NULL
;
1373 consumer_output_node
= xmlFirstElementChild(output_node
);
1374 if (!consumer_output_node
) {
1375 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1379 if (strcmp((const char *) consumer_output_node
->name
,
1380 config_element_consumer_output
)) {
1381 WARN("Invalid output type, expected %s node",
1382 config_element_consumer_output
);
1383 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1387 ret
= process_consumer_output(consumer_output_node
, &output
);
1393 control_uri
= output
.control_uri
;
1394 data_uri
= output
.data_uri
;
1397 /* Check for override and apply them */
1399 if (overrides
->path_url
) {
1400 path
= overrides
->path_url
;
1401 /* control/data_uri are null */;
1405 if (overrides
->ctrl_url
) {
1406 control_uri
= overrides
->ctrl_url
;
1410 if (overrides
->data_url
) {
1411 data_uri
= overrides
->data_url
;
1419 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1420 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1424 if (control_uri
|| data_uri
) {
1425 /* network destination */
1426 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1428 * URLs are provided for sure since the test above make sure that
1429 * with a live timer the data and control URIs are provided. So,
1430 * NULL is passed here and will be set right after.
1432 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1434 ret
= lttng_create_session(name
, NULL
);
1440 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1446 /* either local output or no output */
1447 ret
= lttng_create_session(name
, path
);
1454 free(output
.control_uri
);
1455 free(output
.data_uri
);
1459 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1460 struct lttng_event_probe_attr
*attr
)
1464 assert(probe_attribute_node
);
1467 if (!strcmp((const char *) probe_attribute_node
->name
,
1468 config_element_address
)) {
1473 content
= xmlNodeGetContent(probe_attribute_node
);
1475 ret
= -LTTNG_ERR_NOMEM
;
1479 ret
= parse_uint(content
, &addr
);
1482 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1487 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1488 config_element_offset
)) {
1490 uint64_t offset
= 0;
1493 content
= xmlNodeGetContent(probe_attribute_node
);
1495 ret
= -LTTNG_ERR_NOMEM
;
1499 ret
= parse_uint(content
, &offset
);
1502 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1506 attr
->offset
= offset
;
1507 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1508 config_element_symbol_name
)) {
1513 content
= xmlNodeGetContent(probe_attribute_node
);
1515 ret
= -LTTNG_ERR_NOMEM
;
1519 name_len
= strlen((char *) content
);
1520 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1521 WARN("symbol_name too long.");
1522 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1527 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1536 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1537 const char *channel_name
, const enum process_event_node_phase phase
)
1541 struct lttng_event event
;
1542 char **exclusions
= NULL
;
1543 unsigned long exclusion_count
= 0;
1544 char *filter_expression
= NULL
;
1548 assert(channel_name
);
1550 memset(&event
, 0, sizeof(event
));
1552 /* Initialize default log level which varies by domain */
1553 switch (handle
->domain
.type
)
1555 case LTTNG_DOMAIN_JUL
:
1556 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1558 case LTTNG_DOMAIN_LOG4J
:
1559 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1561 case LTTNG_DOMAIN_PYTHON
:
1562 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1564 case LTTNG_DOMAIN_UST
:
1565 case LTTNG_DOMAIN_KERNEL
:
1566 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1572 for (node
= xmlFirstElementChild(event_node
); node
;
1573 node
= xmlNextElementSibling(node
)) {
1574 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1579 content
= xmlNodeGetContent(node
);
1581 ret
= -LTTNG_ERR_NOMEM
;
1585 name_len
= strlen((char *) content
);
1586 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1587 WARN("Channel name too long.");
1588 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1593 strncpy(event
.name
, (const char *) content
, name_len
);
1595 } else if (!strcmp((const char *) node
->name
,
1596 config_element_enabled
)) {
1597 xmlChar
*content
= xmlNodeGetContent(node
);
1601 ret
= -LTTNG_ERR_NOMEM
;
1605 ret
= parse_bool(content
, &event
.enabled
);
1608 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1611 } else if (!strcmp((const char *) node
->name
,
1612 config_element_type
)) {
1613 xmlChar
*content
= xmlNodeGetContent(node
);
1617 ret
= -LTTNG_ERR_NOMEM
;
1621 ret
= get_event_type(content
);
1624 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1629 } else if (!strcmp((const char *) node
->name
,
1630 config_element_loglevel_type
)) {
1631 xmlChar
*content
= xmlNodeGetContent(node
);
1635 ret
= -LTTNG_ERR_NOMEM
;
1639 ret
= get_loglevel_type(content
);
1642 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1646 event
.loglevel_type
= ret
;
1647 } else if (!strcmp((const char *) node
->name
,
1648 config_element_loglevel
)) {
1650 int64_t loglevel
= 0;
1653 content
= xmlNodeGetContent(node
);
1655 ret
= -LTTNG_ERR_NOMEM
;
1659 ret
= parse_int(content
, &loglevel
);
1662 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1666 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1667 WARN("loglevel out of range.");
1668 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1672 event
.loglevel
= loglevel
;
1673 } else if (!strcmp((const char *) node
->name
,
1674 config_element_filter
)) {
1676 xmlNodeGetContent(node
);
1680 ret
= -LTTNG_ERR_NOMEM
;
1684 free(filter_expression
);
1685 filter_expression
= strdup((char *) content
);
1687 if (!filter_expression
) {
1688 ret
= -LTTNG_ERR_NOMEM
;
1691 } else if (!strcmp((const char *) node
->name
,
1692 config_element_exclusions
)) {
1693 xmlNodePtr exclusion_node
;
1694 int exclusion_index
= 0;
1699 * Exclusions has already been initialized,
1702 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1706 exclusion_count
= xmlChildElementCount(node
);
1707 if (!exclusion_count
) {
1711 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1713 exclusion_count
= 0;
1714 ret
= -LTTNG_ERR_NOMEM
;
1718 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1719 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1721 xmlNodeGetContent(exclusion_node
);
1724 ret
= -LTTNG_ERR_NOMEM
;
1728 exclusions
[exclusion_index
] = strdup((const char *) content
);
1730 if (!exclusions
[exclusion_index
]) {
1731 ret
= -LTTNG_ERR_NOMEM
;
1737 event
.exclusion
= 1;
1738 } else if (!strcmp((const char *) node
->name
,
1739 config_element_attributes
)) {
1740 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1743 if (!attribute_node
) {
1744 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1748 if (!strcmp((const char *) node
->name
,
1749 config_element_probe_attributes
)) {
1750 xmlNodePtr probe_attribute_node
;
1752 /* probe_attributes */
1753 for (probe_attribute_node
=
1754 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1755 probe_attribute_node
= xmlNextElementSibling(
1756 probe_attribute_node
)) {
1758 ret
= process_probe_attribute_node(probe_attribute_node
,
1767 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1769 /* function_attributes */
1770 content
= xmlNodeGetContent(symbol_node
);
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1776 sym_len
= strlen((char *) content
);
1777 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1778 WARN("Function name too long.");
1779 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1784 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1791 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1792 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1793 filter_expression
, exclusion_count
, exclusions
);
1795 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1796 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
)) {
1872 content
= xmlNodeGetContent(attr_node
);
1874 ret
= -LTTNG_ERR_NOMEM
;
1878 name_len
= strlen((char *) content
);
1879 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1880 WARN("Channel name too long.");
1881 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1886 strncpy(channel
->name
, (const char *) content
, name_len
);
1888 } else if (!strcmp((const char *) attr_node
->name
,
1889 config_element_enabled
)) {
1894 content
= xmlNodeGetContent(attr_node
);
1896 ret
= -LTTNG_ERR_NOMEM
;
1900 ret
= parse_bool(content
, &enabled
);
1903 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1907 channel
->enabled
= enabled
;
1908 } else if (!strcmp((const char *) attr_node
->name
,
1909 config_element_overwrite_mode
)) {
1912 /* overwrite_mode */
1913 content
= xmlNodeGetContent(attr_node
);
1915 ret
= -LTTNG_ERR_NOMEM
;
1919 ret
= get_overwrite_mode(content
);
1922 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1926 channel
->attr
.overwrite
= ret
;
1927 } else if (!strcmp((const char *) attr_node
->name
,
1928 config_element_subbuf_size
)) {
1931 /* subbuffer_size */
1932 content
= xmlNodeGetContent(attr_node
);
1934 ret
= -LTTNG_ERR_NOMEM
;
1938 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1941 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1944 } else if (!strcmp((const char *) attr_node
->name
,
1945 config_element_num_subbuf
)) {
1948 /* subbuffer_count */
1949 content
= xmlNodeGetContent(attr_node
);
1951 ret
= -LTTNG_ERR_NOMEM
;
1955 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1958 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1961 } else if (!strcmp((const char *) attr_node
->name
,
1962 config_element_switch_timer_interval
)) {
1964 uint64_t switch_timer_interval
= 0;
1966 /* switch_timer_interval */
1967 content
= xmlNodeGetContent(attr_node
);
1969 ret
= -LTTNG_ERR_NOMEM
;
1973 ret
= parse_uint(content
, &switch_timer_interval
);
1976 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1980 if (switch_timer_interval
> UINT_MAX
) {
1981 WARN("switch_timer_interval out of range.");
1982 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1986 channel
->attr
.switch_timer_interval
=
1987 switch_timer_interval
;
1988 } else if (!strcmp((const char *) attr_node
->name
,
1989 config_element_read_timer_interval
)) {
1991 uint64_t read_timer_interval
= 0;
1993 /* read_timer_interval */
1994 content
= xmlNodeGetContent(attr_node
);
1996 ret
= -LTTNG_ERR_NOMEM
;
2000 ret
= parse_uint(content
, &read_timer_interval
);
2003 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2007 if (read_timer_interval
> UINT_MAX
) {
2008 WARN("read_timer_interval out of range.");
2009 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2013 channel
->attr
.read_timer_interval
=
2014 read_timer_interval
;
2015 } else if (!strcmp((const char *) attr_node
->name
,
2016 config_element_output_type
)) {
2020 content
= xmlNodeGetContent(attr_node
);
2022 ret
= -LTTNG_ERR_NOMEM
;
2026 ret
= get_output_type(content
);
2029 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2033 channel
->attr
.output
= ret
;
2034 } else if (!strcmp((const char *) attr_node
->name
,
2035 config_element_tracefile_size
)) {
2038 /* tracefile_size */
2039 content
= xmlNodeGetContent(attr_node
);
2041 ret
= -LTTNG_ERR_NOMEM
;
2045 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2048 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2051 } else if (!strcmp((const char *) attr_node
->name
,
2052 config_element_tracefile_count
)) {
2055 /* tracefile_count */
2056 content
= xmlNodeGetContent(attr_node
);
2058 ret
= -LTTNG_ERR_NOMEM
;
2062 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2065 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2068 } else if (!strcmp((const char *) attr_node
->name
,
2069 config_element_live_timer_interval
)) {
2071 uint64_t live_timer_interval
= 0;
2073 /* live_timer_interval */
2074 content
= xmlNodeGetContent(attr_node
);
2076 ret
= -LTTNG_ERR_NOMEM
;
2080 ret
= parse_uint(content
, &live_timer_interval
);
2083 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2087 if (live_timer_interval
> UINT_MAX
) {
2088 WARN("live_timer_interval out of range.");
2089 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2093 channel
->attr
.live_timer_interval
=
2094 live_timer_interval
;
2095 } else if (!strcmp((const char *) attr_node
->name
,
2096 config_element_events
)) {
2098 *events_node
= attr_node
;
2101 *contexts_node
= attr_node
;
2109 int process_context_node(xmlNodePtr context_node
,
2110 struct lttng_handle
*handle
, const char *channel_name
)
2113 struct lttng_event_context context
;
2114 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2117 assert(channel_name
);
2119 if (!context_child_node
) {
2120 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2124 memset(&context
, 0, sizeof(context
));
2126 if (!strcmp((const char *) context_child_node
->name
,
2127 config_element_type
)) {
2129 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2132 ret
= -LTTNG_ERR_NOMEM
;
2136 ret
= get_context_type(content
);
2139 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2144 } else if (!strcmp((const char *) context_child_node
->name
,
2145 config_element_context_perf
)) {
2147 xmlNodePtr perf_attr_node
;
2149 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2150 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2151 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2152 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2153 perf_attr_node
; perf_attr_node
=
2154 xmlNextElementSibling(perf_attr_node
)) {
2155 if (!strcmp((const char *) perf_attr_node
->name
,
2156 config_element_type
)) {
2161 content
= xmlNodeGetContent(perf_attr_node
);
2163 ret
= -LTTNG_ERR_NOMEM
;
2167 ret
= parse_uint(content
, &type
);
2170 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2174 if (type
> UINT32_MAX
) {
2175 WARN("perf context type out of range.");
2176 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2180 context
.u
.perf_counter
.type
= type
;
2181 } else if (!strcmp((const char *) perf_attr_node
->name
,
2182 config_element_config
)) {
2184 uint64_t config
= 0;
2187 content
= xmlNodeGetContent(perf_attr_node
);
2189 ret
= -LTTNG_ERR_NOMEM
;
2193 ret
= parse_uint(content
, &config
);
2196 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2200 context
.u
.perf_counter
.config
= config
;
2201 } else if (!strcmp((const char *) perf_attr_node
->name
,
2202 config_element_name
)) {
2207 content
= xmlNodeGetContent(perf_attr_node
);
2209 ret
= -LTTNG_ERR_NOMEM
;
2213 name_len
= strlen((char *) content
);
2214 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2215 WARN("perf context name too long.");
2216 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2221 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2226 } else if (!strcmp((const char *) context_child_node
->name
,
2227 config_element_context_app
)) {
2228 /* application context */
2229 xmlNodePtr app_ctx_node
;
2231 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2232 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2233 app_ctx_node
; app_ctx_node
=
2234 xmlNextElementSibling(app_ctx_node
)) {
2236 char **target
= strcmp(
2237 (const char *) app_ctx_node
->name
,
2238 config_element_context_app_provider_name
) == 0 ?
2239 &context
.u
.app_ctx
.provider_name
:
2240 &context
.u
.app_ctx
.ctx_name
;
2242 content
= xmlNodeGetContent(app_ctx_node
);
2244 ret
= -LTTNG_ERR_NOMEM
;
2248 *target
= (char *) content
;
2251 /* Unrecognized context type */
2252 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2256 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2257 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2258 free(context
.u
.app_ctx
.provider_name
);
2259 free(context
.u
.app_ctx
.ctx_name
);
2266 int process_contexts_node(xmlNodePtr contexts_node
,
2267 struct lttng_handle
*handle
, const char *channel_name
)
2270 xmlNodePtr context_node
;
2272 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2273 context_node
= xmlNextElementSibling(context_node
)) {
2274 ret
= process_context_node(context_node
, handle
, channel_name
);
2284 int get_tracker_elements(enum lttng_tracker_type tracker_type
,
2285 const char **element_id_tracker
,
2286 const char **element_target_id
,
2287 const char **element_id
,
2288 const char **element_id_alias
,
2289 const char **element_name
)
2293 switch (tracker_type
) {
2294 case LTTNG_TRACKER_PID
:
2295 *element_id_tracker
= config_element_pid_tracker
;
2296 *element_target_id
= config_element_target_pid
;
2297 *element_id
= config_element_id
;
2298 *element_id_alias
= config_element_pid
;
2299 *element_name
= NULL
;
2301 case LTTNG_TRACKER_VPID
:
2302 *element_id_tracker
= config_element_vpid_tracker
;
2303 *element_target_id
= config_element_target_vpid
;
2304 *element_id
= config_element_id
;
2305 *element_id_alias
= NULL
;
2306 *element_name
= NULL
;
2308 case LTTNG_TRACKER_UID
:
2309 *element_id_tracker
= config_element_uid_tracker
;
2310 *element_target_id
= config_element_target_uid
;
2311 *element_id
= config_element_id
;
2312 *element_id_alias
= NULL
;
2313 *element_name
= config_element_name
;
2315 case LTTNG_TRACKER_VUID
:
2316 *element_id_tracker
= config_element_vuid_tracker
;
2317 *element_target_id
= config_element_target_vuid
;
2318 *element_id
= config_element_id
;
2319 *element_id_alias
= NULL
;
2320 *element_name
= config_element_name
;
2322 case LTTNG_TRACKER_GID
:
2323 *element_id_tracker
= config_element_gid_tracker
;
2324 *element_target_id
= config_element_target_gid
;
2325 *element_id
= config_element_id
;
2326 *element_id_alias
= NULL
;
2327 *element_name
= config_element_name
;
2329 case LTTNG_TRACKER_VGID
:
2330 *element_id_tracker
= config_element_vgid_tracker
;
2331 *element_target_id
= config_element_target_vgid
;
2332 *element_id
= config_element_id
;
2333 *element_id_alias
= NULL
;
2334 *element_name
= config_element_name
;
2337 ret
= LTTNG_ERR_INVALID
;
2343 int process_id_tracker_node(xmlNodePtr id_tracker_node
,
2344 struct lttng_handle
*handle
, enum lttng_tracker_type tracker_type
)
2347 xmlNodePtr targets_node
= NULL
;
2349 const char *element_id_tracker
;
2350 const char *element_target_id
;
2351 const char *element_id
;
2352 const char *element_id_alias
;
2353 const char *element_name
;
2356 assert(id_tracker_node
);
2358 ret
= get_tracker_elements(tracker_type
, &element_id_tracker
,
2359 &element_target_id
, &element_id
, &element_id_alias
, &element_name
);
2364 /* get the targets node */
2365 for (node
= xmlFirstElementChild(id_tracker_node
); node
;
2366 node
= xmlNextElementSibling(node
)) {
2367 if (!strcmp((const char *) node
->name
,
2368 config_element_targets
)) {
2369 targets_node
= node
;
2374 if (!targets_node
) {
2375 ret
= LTTNG_ERR_INVALID
;
2379 /* Go through all id target node */
2380 child
= xmlChildElementCount(targets_node
);
2382 struct lttng_tracker_id tracker_id
;
2384 tracker_id
.type
= LTTNG_ID_ALL
;
2385 /* The session is explicitly set to target nothing. */
2386 ret
= lttng_untrack_id(handle
, tracker_type
, &tracker_id
);
2391 for (node
= xmlFirstElementChild(targets_node
); node
;
2392 node
= xmlNextElementSibling(node
)) {
2393 xmlNodePtr id_target_node
= node
;
2395 /* get id node and track it */
2396 for (node
= xmlFirstElementChild(id_target_node
); node
;
2397 node
= xmlNextElementSibling(node
)) {
2398 if (!strcmp((const char *) node
->name
, element_id
) ||
2399 (element_id_alias
&& !strcmp((const char *) node
->name
,
2400 element_id_alias
))) {
2402 xmlChar
*content
= NULL
;
2403 struct lttng_tracker_id tracker_id
;
2405 content
= xmlNodeGetContent(node
);
2407 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2411 ret
= parse_int(content
, &id
);
2414 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2418 tracker_id
.type
= LTTNG_ID_VALUE
;
2419 tracker_id
.value
= (int) id
;
2420 ret
= lttng_track_id(handle
, tracker_type
, &tracker_id
);
2425 if (element_name
&& !strcmp((const char *) node
->name
,
2427 xmlChar
*content
= NULL
;
2428 struct lttng_tracker_id tracker_id
;
2430 content
= xmlNodeGetContent(node
);
2432 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2435 tracker_id
.type
= LTTNG_ID_STRING
;
2436 tracker_id
.string
= (char *) content
;
2437 ret
= lttng_track_id(handle
, tracker_type
, &tracker_id
);
2444 node
= id_target_node
;
2453 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2456 struct lttng_domain domain
= { 0 };
2457 struct lttng_handle
*handle
= NULL
;
2458 xmlNodePtr channels_node
= NULL
;
2459 xmlNodePtr trackers_node
= NULL
;
2460 xmlNodePtr pid_tracker_node
= NULL
;
2461 xmlNodePtr vpid_tracker_node
= NULL
;
2462 xmlNodePtr uid_tracker_node
= NULL
;
2463 xmlNodePtr vuid_tracker_node
= NULL
;
2464 xmlNodePtr gid_tracker_node
= NULL
;
2465 xmlNodePtr vgid_tracker_node
= NULL
;
2468 assert(session_name
);
2470 ret
= init_domain(domain_node
, &domain
);
2475 handle
= lttng_create_handle(session_name
, &domain
);
2477 ret
= -LTTNG_ERR_NOMEM
;
2481 /* get the channels node */
2482 for (node
= xmlFirstElementChild(domain_node
); node
;
2483 node
= xmlNextElementSibling(node
)) {
2484 if (!strcmp((const char *) node
->name
,
2485 config_element_channels
)) {
2486 channels_node
= node
;
2491 if (!channels_node
) {
2495 /* create all channels */
2496 for (node
= xmlFirstElementChild(channels_node
); node
;
2497 node
= xmlNextElementSibling(node
)) {
2498 struct lttng_channel channel
;
2499 xmlNodePtr contexts_node
= NULL
;
2500 xmlNodePtr events_node
= NULL
;
2501 xmlNodePtr channel_attr_node
;
2503 memset(&channel
, 0, sizeof(channel
));
2504 lttng_channel_set_default_attr(&domain
, &channel
.attr
);
2506 for (channel_attr_node
= xmlFirstElementChild(node
);
2507 channel_attr_node
; channel_attr_node
=
2508 xmlNextElementSibling(channel_attr_node
)) {
2509 ret
= process_channel_attr_node(channel_attr_node
,
2510 &channel
, &contexts_node
, &events_node
);
2516 ret
= lttng_enable_channel(handle
, &channel
);
2521 ret
= process_events_node(events_node
, handle
, channel
.name
);
2526 ret
= process_contexts_node(contexts_node
, handle
,
2533 /* get the trackers node */
2534 for (node
= xmlFirstElementChild(domain_node
); node
;
2535 node
= xmlNextElementSibling(node
)) {
2536 if (!strcmp((const char *) node
->name
,
2537 config_element_trackers
)) {
2538 trackers_node
= node
;
2543 if (!trackers_node
) {
2547 for (node
= xmlFirstElementChild(trackers_node
); node
;
2548 node
= xmlNextElementSibling(node
)) {
2549 if (!strcmp((const char *)node
->name
, config_element_pid_tracker
)) {
2550 pid_tracker_node
= node
;
2551 ret
= process_id_tracker_node(pid_tracker_node
, handle
,
2557 if (!strcmp((const char *)node
->name
, config_element_vpid_tracker
)) {
2558 vpid_tracker_node
= node
;
2559 ret
= process_id_tracker_node(vpid_tracker_node
, handle
,
2560 LTTNG_TRACKER_VPID
);
2565 if (!strcmp((const char *)node
->name
, config_element_uid_tracker
)) {
2566 uid_tracker_node
= node
;
2567 ret
= process_id_tracker_node(uid_tracker_node
, handle
,
2573 if (!strcmp((const char *)node
->name
, config_element_vuid_tracker
)) {
2574 vuid_tracker_node
= node
;
2575 ret
= process_id_tracker_node(vuid_tracker_node
, handle
,
2576 LTTNG_TRACKER_VUID
);
2581 if (!strcmp((const char *)node
->name
, config_element_gid_tracker
)) {
2582 gid_tracker_node
= node
;
2583 ret
= process_id_tracker_node(gid_tracker_node
, handle
,
2589 if (!strcmp((const char *)node
->name
, config_element_vgid_tracker
)) {
2590 vgid_tracker_node
= node
;
2591 ret
= process_id_tracker_node(vgid_tracker_node
, handle
,
2592 LTTNG_TRACKER_VGID
);
2600 lttng_destroy_handle(handle
);
2605 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2607 const struct config_load_session_override_attr
*overrides
)
2609 int ret
, started
= -1, snapshot_mode
= -1;
2610 uint64_t live_timer_interval
= UINT64_MAX
;
2611 xmlChar
*name
= NULL
;
2612 xmlChar
*shm_path
= NULL
;
2613 xmlNodePtr domains_node
= NULL
;
2614 xmlNodePtr output_node
= NULL
;
2616 struct lttng_domain
*kernel_domain
= NULL
;
2617 struct lttng_domain
*ust_domain
= NULL
;
2618 struct lttng_domain
*jul_domain
= NULL
;
2619 struct lttng_domain
*log4j_domain
= NULL
;
2620 struct lttng_domain
*python_domain
= NULL
;
2622 for (node
= xmlFirstElementChild(session_node
); node
;
2623 node
= xmlNextElementSibling(node
)) {
2624 if (!name
&& !strcmp((const char *) node
->name
,
2625 config_element_name
)) {
2627 xmlChar
*node_content
= xmlNodeGetContent(node
);
2628 if (!node_content
) {
2629 ret
= -LTTNG_ERR_NOMEM
;
2633 name
= node_content
;
2634 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2635 config_element_domains
)) {
2637 domains_node
= node
;
2638 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2639 config_element_started
)) {
2641 xmlChar
*node_content
= xmlNodeGetContent(node
);
2642 if (!node_content
) {
2643 ret
= -LTTNG_ERR_NOMEM
;
2647 ret
= parse_bool(node_content
, &started
);
2650 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2653 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2654 config_element_output
)) {
2657 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2658 config_element_shared_memory_path
)) {
2659 /* shared memory path */
2660 xmlChar
*node_content
= xmlNodeGetContent(node
);
2661 if (!node_content
) {
2662 ret
= -LTTNG_ERR_NOMEM
;
2666 shm_path
= node_content
;
2668 /* attributes, snapshot_mode or live_timer_interval */
2669 xmlNodePtr attributes_child
=
2670 xmlFirstElementChild(node
);
2672 if (!strcmp((const char *) attributes_child
->name
,
2673 config_element_snapshot_mode
)) {
2675 xmlChar
*snapshot_mode_content
=
2676 xmlNodeGetContent(attributes_child
);
2677 if (!snapshot_mode_content
) {
2678 ret
= -LTTNG_ERR_NOMEM
;
2682 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2683 free(snapshot_mode_content
);
2685 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2689 /* live_timer_interval */
2690 xmlChar
*timer_interval_content
=
2691 xmlNodeGetContent(attributes_child
);
2692 if (!timer_interval_content
) {
2693 ret
= -LTTNG_ERR_NOMEM
;
2697 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2698 free(timer_interval_content
);
2700 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2708 /* Mandatory attribute, as defined in the session XSD */
2709 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2713 if (session_name
&& strcmp((char *) name
, session_name
)) {
2714 /* This is not the session we are looking for */
2715 ret
= -LTTNG_ERR_NO_SESSION
;
2719 /* Init domains to create the session handles */
2720 for (node
= xmlFirstElementChild(domains_node
); node
;
2721 node
= xmlNextElementSibling(node
)) {
2722 struct lttng_domain
*domain
;
2724 domain
= zmalloc(sizeof(*domain
));
2726 ret
= -LTTNG_ERR_NOMEM
;
2730 ret
= init_domain(node
, domain
);
2732 goto domain_init_error
;
2735 switch (domain
->type
) {
2736 case LTTNG_DOMAIN_KERNEL
:
2737 if (kernel_domain
) {
2738 /* Same domain seen twice, invalid! */
2739 goto domain_init_error
;
2741 kernel_domain
= domain
;
2743 case LTTNG_DOMAIN_UST
:
2745 /* Same domain seen twice, invalid! */
2746 goto domain_init_error
;
2748 ust_domain
= domain
;
2750 case LTTNG_DOMAIN_JUL
:
2752 /* Same domain seen twice, invalid! */
2753 goto domain_init_error
;
2755 jul_domain
= domain
;
2757 case LTTNG_DOMAIN_LOG4J
:
2759 /* Same domain seen twice, invalid! */
2760 goto domain_init_error
;
2762 log4j_domain
= domain
;
2764 case LTTNG_DOMAIN_PYTHON
:
2765 if (python_domain
) {
2766 /* Same domain seen twice, invalid! */
2767 goto domain_init_error
;
2769 python_domain
= domain
;
2772 WARN("Invalid domain type");
2773 goto domain_init_error
;
2778 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2782 /* Apply overrides */
2784 if (overrides
->session_name
) {
2785 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2786 if (!name_override
) {
2787 ret
= -LTTNG_ERR_NOMEM
;
2791 /* Overrides the session name to the provided name */
2793 name
= name_override
;
2798 /* Destroy session if it exists */
2799 ret
= lttng_destroy_session((const char *) name
);
2800 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2801 ERR("Failed to destroy existing session.");
2806 /* Create session type depending on output type */
2807 if (snapshot_mode
&& snapshot_mode
!= -1) {
2808 ret
= create_snapshot_session((const char *) name
, output_node
,
2810 } else if (live_timer_interval
&&
2811 live_timer_interval
!= UINT64_MAX
) {
2812 ret
= create_session((const char *) name
, kernel_domain
,
2813 ust_domain
, jul_domain
, log4j_domain
,
2814 output_node
, live_timer_interval
, overrides
);
2816 /* regular session */
2817 ret
= create_session((const char *) name
, kernel_domain
,
2818 ust_domain
, jul_domain
, log4j_domain
,
2819 output_node
, UINT64_MAX
, overrides
);
2826 ret
= lttng_set_session_shm_path((const char *) name
,
2827 (const char *) shm_path
);
2833 for (node
= xmlFirstElementChild(domains_node
); node
;
2834 node
= xmlNextElementSibling(node
)) {
2835 ret
= process_domain_node(node
, (const char *) name
);
2842 ret
= lttng_start_tracing((const char *) name
);
2850 ERR("Failed to load session %s: %s", (const char *) name
,
2851 lttng_strerror(ret
));
2852 lttng_destroy_session((const char *) name
);
2856 free(kernel_domain
);
2860 free(python_domain
);
2867 * Return 1 if the given path is readable by the current UID or 0 if not.
2868 * Return -1 if the path is EPERM.
2870 static int validate_file_read_creds(const char *path
)
2876 /* Can we read the file. */
2877 ret
= access(path
, R_OK
);
2881 if (errno
== EACCES
) {
2892 int load_session_from_file(const char *path
, const char *session_name
,
2893 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2894 const struct config_load_session_override_attr
*overrides
)
2896 int ret
, session_found
= !session_name
;
2897 xmlDocPtr doc
= NULL
;
2898 xmlNodePtr sessions_node
;
2899 xmlNodePtr session_node
;
2902 assert(validation_ctx
);
2904 ret
= validate_file_read_creds(path
);
2907 ret
= -LTTNG_ERR_EPERM
;
2909 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2914 doc
= xmlParseFile(path
);
2916 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2920 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2922 ERR("Session configuration file validation failed");
2923 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2927 sessions_node
= xmlDocGetRootElement(doc
);
2928 if (!sessions_node
) {
2929 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2933 for (session_node
= xmlFirstElementChild(sessions_node
);
2934 session_node
; session_node
=
2935 xmlNextElementSibling(session_node
)) {
2936 ret
= process_session_node(session_node
,
2937 session_name
, overwrite
, overrides
);
2938 if (session_name
&& ret
== 0) {
2939 /* Target session found and loaded */
2947 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2949 if (ret
== -LTTNG_ERR_NO_SESSION
) {
2950 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2955 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2957 struct dirent
*alloc_dirent(const char *path
)
2961 struct dirent
*entry
;
2963 name_max
= pathconf(path
, _PC_NAME_MAX
);
2964 if (name_max
== -1) {
2965 name_max
= PATH_MAX
;
2967 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2968 entry
= zmalloc(len
);
2973 int load_session_from_path(const char *path
, const char *session_name
,
2974 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2975 const struct config_load_session_override_attr
*overrides
)
2977 int ret
, session_found
= !session_name
;
2978 DIR *directory
= NULL
;
2981 assert(validation_ctx
);
2983 directory
= opendir(path
);
2987 /* Try the file loading. */
2990 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2993 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2998 struct dirent
*entry
;
2999 struct dirent
*result
;
3000 char *file_path
= NULL
;
3001 size_t path_len
= strlen(path
);
3003 if (path_len
>= PATH_MAX
) {
3004 ret
= -LTTNG_ERR_INVALID
;
3008 entry
= alloc_dirent(path
);
3010 ret
= -LTTNG_ERR_NOMEM
;
3014 file_path
= zmalloc(PATH_MAX
);
3016 ret
= -LTTNG_ERR_NOMEM
;
3021 strncpy(file_path
, path
, path_len
);
3022 if (file_path
[path_len
- 1] != '/') {
3023 file_path
[path_len
++] = '/';
3027 /* Search for *.lttng files */
3028 while (!readdir_r(directory
, entry
, &result
) && result
) {
3029 size_t file_name_len
= strlen(result
->d_name
);
3031 if (file_name_len
<=
3032 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3036 if (path_len
+ file_name_len
>= PATH_MAX
) {
3040 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3041 result
->d_name
+ file_name_len
- sizeof(
3042 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3046 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
3047 file_path
[path_len
+ file_name_len
] = '\0';
3049 ret
= load_session_from_file(file_path
, session_name
,
3050 validation_ctx
, overwrite
, overrides
);
3051 if (session_name
&& (!ret
|| ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
)) {
3059 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3063 ret
= load_session_from_file(path
, session_name
,
3064 validation_ctx
, overwrite
, overrides
);
3074 if (closedir(directory
)) {
3079 if (!ret
&& !session_found
) {
3080 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
3087 * Validate that the given path's credentials and the current process have the
3088 * same UID. If so, return 1 else return 0 if it does NOT match.
3090 static int validate_path_creds(const char *path
)
3092 int ret
, uid
= getuid();
3101 ret
= stat(path
, &buf
);
3103 if (errno
!= ENOENT
) {
3106 ret
= -LTTNG_ERR_INVALID
;
3110 if (buf
.st_uid
!= uid
) {
3121 int config_load_session(const char *path
, const char *session_name
,
3122 int overwrite
, unsigned int autoload
,
3123 const struct config_load_session_override_attr
*overrides
)
3126 bool session_loaded
= false;
3127 const char *path_ptr
= NULL
;
3128 struct session_config_validation_ctx validation_ctx
= { 0 };
3130 ret
= init_session_config_validation_ctx(&validation_ctx
);
3137 const char *sys_path
;
3140 home_path
= utils_get_home_dir();
3142 char path
[PATH_MAX
];
3145 * Try user session configuration path. Ignore error here so we can
3146 * continue loading the system wide sessions.
3149 ret
= snprintf(path
, sizeof(path
),
3150 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3151 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3153 PERROR("snprintf session autoload home config path");
3154 ret
= -LTTNG_ERR_INVALID
;
3159 * Credentials are only validated for the autoload in order to
3160 * avoid any user session daemon to try to load kernel sessions
3161 * automatically and failing all the times.
3163 ret
= validate_path_creds(path
);
3168 ret
= snprintf(path
, sizeof(path
),
3169 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3171 PERROR("snprintf session home config path");
3172 ret
= -LTTNG_ERR_INVALID
;
3178 ret
= load_session_from_path(path_ptr
, session_name
,
3179 &validation_ctx
, overwrite
, overrides
);
3180 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3184 * Continue even if the session was found since we have to try
3185 * the system wide sessions.
3187 session_loaded
= true;
3191 /* Reset path pointer for the system wide dir. */
3194 /* Try system wide configuration directory. */
3196 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3197 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3198 ret
= validate_path_creds(sys_path
);
3200 path_ptr
= sys_path
;
3203 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3204 path_ptr
= sys_path
;
3208 ret
= load_session_from_path(path_ptr
, session_name
,
3209 &validation_ctx
, overwrite
, overrides
);
3211 session_loaded
= true;
3217 ret
= access(path
, F_OK
);
3222 ret
= -LTTNG_ERR_INVALID
;
3223 WARN("Session configuration path does not exist.");
3226 ret
= -LTTNG_ERR_EPERM
;
3229 ret
= -LTTNG_ERR_UNK
;
3235 ret
= load_session_from_path(path
, session_name
,
3236 &validation_ctx
, overwrite
, overrides
);
3239 fini_session_config_validation_ctx(&validation_ctx
);
3240 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3242 * Don't report an error if no sessions are found when called
3243 * without a session_name or a search path.
3248 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3249 /* A matching session was found in one of the search paths. */
3256 void __attribute__((destructor
)) session_config_exit(void)