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>
43 #include <lttng/rotation.h>
45 #include "session-config.h"
46 #include "config-internal.h"
48 struct handler_filter_args
{
50 config_entry_handler_cb handler
;
54 struct session_config_validation_ctx
{
55 xmlSchemaParserCtxtPtr parser_ctx
;
57 xmlSchemaValidCtxtPtr schema_validation_ctx
;
60 const char * const config_str_yes
= "yes";
61 const char * const config_str_true
= "true";
62 const char * const config_str_on
= "on";
63 const char * const config_str_no
= "no";
64 const char * const config_str_false
= "false";
65 const char * const config_str_off
= "off";
66 const char * const config_xml_encoding
= "UTF-8";
67 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
68 const char * const config_xml_indent_string
= "\t";
69 const char * const config_xml_true
= "true";
70 const char * const config_xml_false
= "false";
72 const char * const config_element_channel
= "channel";
73 const char * const config_element_channels
= "channels";
74 const char * const config_element_domain
= "domain";
75 const char * const config_element_domains
= "domains";
76 const char * const config_element_event
= "event";
77 const char * const config_element_events
= "events";
78 const char * const config_element_context
= "context";
79 const char * const config_element_contexts
= "contexts";
80 const char * const config_element_attributes
= "attributes";
81 const char * const config_element_exclusion
= "exclusion";
82 const char * const config_element_exclusions
= "exclusions";
83 const char * const config_element_function_attributes
= "function_attributes";
84 const char * const config_element_probe_attributes
= "probe_attributes";
85 const char * const config_element_symbol_name
= "symbol_name";
86 const char * const config_element_address
= "address";
87 const char * const config_element_offset
= "offset";
88 const char * const config_element_name
= "name";
89 const char * const config_element_enabled
= "enabled";
90 const char * const config_element_overwrite_mode
= "overwrite_mode";
91 const char * const config_element_subbuf_size
= "subbuffer_size";
92 const char * const config_element_num_subbuf
= "subbuffer_count";
93 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
94 const char * const config_element_read_timer_interval
= "read_timer_interval";
95 LTTNG_HIDDEN
const char * const config_element_monitor_timer_interval
= "monitor_timer_interval";
96 LTTNG_HIDDEN
const char * const config_element_blocking_timeout
= "blocking_timeout";
97 const char * const config_element_output
= "output";
98 const char * const config_element_output_type
= "output_type";
99 const char * const config_element_tracefile_size
= "tracefile_size";
100 const char * const config_element_tracefile_count
= "tracefile_count";
101 const char * const config_element_live_timer_interval
= "live_timer_interval";
102 LTTNG_HIDDEN
const char * const config_element_discarded_events
= "discarded_events";
103 LTTNG_HIDDEN
const char * const config_element_lost_packets
= "lost_packets";
104 const char * const config_element_type
= "type";
105 const char * const config_element_buffer_type
= "buffer_type";
106 const char * const config_element_session
= "session";
107 const char * const config_element_sessions
= "sessions";
108 LTTNG_HIDDEN
const char * const config_element_context_perf
= "perf";
109 LTTNG_HIDDEN
const char * const config_element_context_app
= "app";
110 LTTNG_HIDDEN
const char * const config_element_context_app_provider_name
= "provider_name";
111 LTTNG_HIDDEN
const char * const config_element_context_app_ctx_name
= "ctx_name";
112 const char * const config_element_config
= "config";
113 const char * const config_element_started
= "started";
114 const char * const config_element_snapshot_mode
= "snapshot_mode";
115 const char * const config_element_loglevel
= "loglevel";
116 const char * const config_element_loglevel_type
= "loglevel_type";
117 const char * const config_element_filter
= "filter";
118 LTTNG_HIDDEN
const char * const config_element_filter_expression
= "filter_expression";
119 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
120 const char * const config_element_consumer_output
= "consumer_output";
121 const char * const config_element_destination
= "destination";
122 const char * const config_element_path
= "path";
123 const char * const config_element_net_output
= "net_output";
124 const char * const config_element_control_uri
= "control_uri";
125 const char * const config_element_data_uri
= "data_uri";
126 const char * const config_element_max_size
= "max_size";
127 const char * const config_element_pid
= "pid";
128 const char * const config_element_pids
= "pids";
129 const char * const config_element_shared_memory_path
= "shared_memory_path";
130 const char * const config_element_pid_tracker
= "pid_tracker";
131 const char * const config_element_trackers
= "trackers";
132 const char * const config_element_targets
= "targets";
133 const char * const config_element_target_pid
= "pid_target";
135 LTTNG_HIDDEN
const char * const config_element_rotation_timer_interval
= "rotation_schedule_timer_period";
136 LTTNG_HIDDEN
const char * const config_element_rotation_size
= "rotation_schedule_size";
137 LTTNG_HIDDEN
const char * const config_element_rotation_schedule
= "rotation_schedule";
139 const char * const config_domain_type_kernel
= "KERNEL";
140 const char * const config_domain_type_ust
= "UST";
141 const char * const config_domain_type_jul
= "JUL";
142 const char * const config_domain_type_log4j
= "LOG4J";
143 const char * const config_domain_type_python
= "PYTHON";
145 const char * const config_buffer_type_per_pid
= "PER_PID";
146 const char * const config_buffer_type_per_uid
= "PER_UID";
147 const char * const config_buffer_type_global
= "GLOBAL";
149 const char * const config_overwrite_mode_discard
= "DISCARD";
150 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
152 const char * const config_output_type_splice
= "SPLICE";
153 const char * const config_output_type_mmap
= "MMAP";
155 const char * const config_loglevel_type_all
= "ALL";
156 const char * const config_loglevel_type_range
= "RANGE";
157 const char * const config_loglevel_type_single
= "SINGLE";
159 const char * const config_event_type_all
= "ALL";
160 const char * const config_event_type_tracepoint
= "TRACEPOINT";
161 const char * const config_event_type_probe
= "PROBE";
162 const char * const config_event_type_function
= "FUNCTION";
163 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
164 const char * const config_event_type_noop
= "NOOP";
165 const char * const config_event_type_syscall
= "SYSCALL";
166 const char * const config_event_type_kprobe
= "KPROBE";
167 const char * const config_event_type_kretprobe
= "KRETPROBE";
169 const char * const config_event_context_pid
= "PID";
170 const char * const config_event_context_procname
= "PROCNAME";
171 const char * const config_event_context_prio
= "PRIO";
172 const char * const config_event_context_nice
= "NICE";
173 const char * const config_event_context_vpid
= "VPID";
174 const char * const config_event_context_tid
= "TID";
175 const char * const config_event_context_vtid
= "VTID";
176 const char * const config_event_context_ppid
= "PPID";
177 const char * const config_event_context_vppid
= "VPPID";
178 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
179 const char * const config_event_context_hostname
= "HOSTNAME";
180 const char * const config_event_context_ip
= "IP";
181 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
182 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
183 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
184 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
185 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
186 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
188 /* Deprecated symbols */
189 const char * const config_element_perf
;
191 enum process_event_node_phase
{
196 struct consumer_output
{
203 static int config_entry_handler_filter(struct handler_filter_args
*args
,
204 const char *section
, const char *name
, const char *value
)
207 struct config_entry entry
= { section
, name
, value
};
211 if (!section
|| !name
|| !value
) {
217 if (strcmp(args
->section
, section
)) {
222 ret
= args
->handler(&entry
, args
->user_data
);
228 int config_get_section_entries(const char *override_path
, const char *section
,
229 config_entry_handler_cb handler
, void *user_data
)
233 FILE *config_file
= NULL
;
234 struct handler_filter_args filter
= { section
, handler
, user_data
};
236 /* First, try system-wide conf. file. */
237 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
239 config_file
= fopen(path
, "r");
241 DBG("Loading daemon conf file at %s", path
);
243 * Return value is not very important here since error or not, we
244 * continue and try the next possible conf. file.
246 (void) ini_parse_file(config_file
,
247 (ini_entry_handler
) config_entry_handler_filter
,
252 /* Second is the user local configuration. */
253 path
= utils_get_home_dir();
255 char fullpath
[PATH_MAX
];
257 ret
= snprintf(fullpath
, sizeof(fullpath
),
258 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
260 PERROR("snprintf user conf. path");
264 config_file
= fopen(fullpath
, "r");
266 DBG("Loading daemon user conf file at %s", path
);
268 * Return value is not very important here since error or not, we
269 * continue and try the next possible conf. file.
271 (void) ini_parse_file(config_file
,
272 (ini_entry_handler
) config_entry_handler_filter
,
278 /* Final path is the one that the user might have provided. */
280 config_file
= fopen(override_path
, "r");
282 DBG("Loading daemon command line conf file at %s", override_path
);
283 (void) ini_parse_file(config_file
,
284 (ini_entry_handler
) config_entry_handler_filter
,
288 ERR("Failed to open daemon configuration file at %s",
295 /* Everything went well. */
303 int config_parse_value(const char *value
)
306 char *endptr
, *lower_str
;
316 v
= strtoul(value
, &endptr
, 10);
317 if (endptr
!= value
) {
322 lower_str
= zmalloc(len
+ 1);
329 for (i
= 0; i
< len
; i
++) {
330 lower_str
[i
] = tolower(value
[i
]);
333 if (!strcmp(lower_str
, config_str_yes
) ||
334 !strcmp(lower_str
, config_str_true
) ||
335 !strcmp(lower_str
, config_str_on
)) {
337 } else if (!strcmp(lower_str
, config_str_no
) ||
338 !strcmp(lower_str
, config_str_false
) ||
339 !strcmp(lower_str
, config_str_off
)) {
351 * Returns a xmlChar string which must be released using xmlFree().
353 static xmlChar
*encode_string(const char *in_str
)
355 xmlChar
*out_str
= NULL
;
356 xmlCharEncodingHandlerPtr handler
;
357 int out_len
, ret
, in_len
;
361 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
363 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
367 in_len
= strlen(in_str
);
369 * Add 1 byte for the NULL terminted character. The factor 4 here is
370 * used because UTF-8 characters can take up to 4 bytes.
372 out_len
= (in_len
* 4) + 1;
373 out_str
= xmlMalloc(out_len
);
378 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
385 /* out_len is now the size of out_str */
386 out_str
[out_len
] = '\0';
392 struct config_writer
*config_writer_create(int fd_output
, int indent
)
395 struct config_writer
*writer
;
396 xmlOutputBufferPtr buffer
;
398 writer
= zmalloc(sizeof(struct config_writer
));
400 PERROR("zmalloc config_writer_create");
404 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
409 writer
->writer
= xmlNewTextWriter(buffer
);
410 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
411 config_xml_encoding
, NULL
);
416 ret
= xmlTextWriterSetIndentString(writer
->writer
,
417 BAD_CAST config_xml_indent_string
);
422 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
430 config_writer_destroy(writer
);
435 int config_writer_destroy(struct config_writer
*writer
)
444 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
445 WARN("Could not close XML document");
449 if (writer
->writer
) {
450 xmlFreeTextWriter(writer
->writer
);
459 int config_writer_open_element(struct config_writer
*writer
,
460 const char *element_name
)
463 xmlChar
*encoded_element_name
;
465 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
470 encoded_element_name
= encode_string(element_name
);
471 if (!encoded_element_name
) {
476 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
477 xmlFree(encoded_element_name
);
479 return ret
>= 0 ? 0 : ret
;
483 int config_writer_write_attribute(struct config_writer
*writer
,
484 const char *name
, const char *value
)
487 xmlChar
*encoded_name
= NULL
;
488 xmlChar
*encoded_value
= NULL
;
490 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
495 encoded_name
= encode_string(name
);
501 encoded_value
= encode_string(value
);
502 if (!encoded_value
) {
507 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
510 xmlFree(encoded_name
);
511 xmlFree(encoded_value
);
512 return ret
>= 0 ? 0 : ret
;
516 int config_writer_close_element(struct config_writer
*writer
)
520 if (!writer
|| !writer
->writer
) {
525 ret
= xmlTextWriterEndElement(writer
->writer
);
527 return ret
>= 0 ? 0 : ret
;
531 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
532 const char *element_name
, uint64_t value
)
535 xmlChar
*encoded_element_name
;
537 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
542 encoded_element_name
= encode_string(element_name
);
543 if (!encoded_element_name
) {
548 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
549 encoded_element_name
, "%" PRIu64
, value
);
550 xmlFree(encoded_element_name
);
552 return ret
>= 0 ? 0 : ret
;
556 int config_writer_write_element_signed_int(struct config_writer
*writer
,
557 const char *element_name
, int64_t value
)
560 xmlChar
*encoded_element_name
;
562 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
567 encoded_element_name
= encode_string(element_name
);
568 if (!encoded_element_name
) {
573 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
574 encoded_element_name
, "%" PRIi64
, value
);
575 xmlFree(encoded_element_name
);
577 return ret
>= 0 ? 0 : ret
;
581 int config_writer_write_element_bool(struct config_writer
*writer
,
582 const char *element_name
, int value
)
584 return config_writer_write_element_string(writer
, element_name
,
585 value
? config_xml_true
: config_xml_false
);
589 int config_writer_write_element_string(struct config_writer
*writer
,
590 const char *element_name
, const char *value
)
593 xmlChar
*encoded_element_name
= NULL
;
594 xmlChar
*encoded_value
= NULL
;
596 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
602 encoded_element_name
= encode_string(element_name
);
603 if (!encoded_element_name
) {
608 encoded_value
= encode_string(value
);
609 if (!encoded_value
) {
614 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
617 xmlFree(encoded_element_name
);
618 xmlFree(encoded_value
);
619 return ret
>= 0 ? 0 : ret
;
623 void xml_error_handler(void *ctx
, const char *format
, ...)
629 va_start(args
, format
);
630 ret
= vasprintf(&errMsg
, format
, args
);
633 ERR("String allocation failed in xml error handler");
637 fprintf(stderr
, "XML Error: %s", errMsg
);
642 void fini_session_config_validation_ctx(
643 struct session_config_validation_ctx
*ctx
)
645 if (ctx
->parser_ctx
) {
646 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
650 xmlSchemaFree(ctx
->schema
);
653 if (ctx
->schema_validation_ctx
) {
654 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
657 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
661 char *get_session_config_xsd_path()
664 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
665 size_t base_path_len
;
669 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
672 base_path_len
= strlen(base_path
);
673 max_path_len
= base_path_len
+
674 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
675 xsd_path
= zmalloc(max_path_len
);
680 strncpy(xsd_path
, base_path
, max_path_len
);
681 if (xsd_path
[base_path_len
- 1] != '/') {
682 xsd_path
[base_path_len
++] = '/';
685 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
686 max_path_len
- base_path_len
);
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
)) {
1502 content
= xmlNodeGetContent(probe_attribute_node
);
1504 ret
= -LTTNG_ERR_NOMEM
;
1508 name_len
= strlen((char *) content
);
1509 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1510 WARN("symbol_name too long.");
1511 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1516 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1525 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1526 const char *channel_name
, const enum process_event_node_phase phase
)
1530 struct lttng_event event
;
1531 char **exclusions
= NULL
;
1532 unsigned long exclusion_count
= 0;
1533 char *filter_expression
= NULL
;
1537 assert(channel_name
);
1539 memset(&event
, 0, sizeof(event
));
1541 /* Initialize default log level which varies by domain */
1542 switch (handle
->domain
.type
)
1544 case LTTNG_DOMAIN_JUL
:
1545 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1547 case LTTNG_DOMAIN_LOG4J
:
1548 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1550 case LTTNG_DOMAIN_PYTHON
:
1551 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1553 case LTTNG_DOMAIN_UST
:
1554 case LTTNG_DOMAIN_KERNEL
:
1555 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1561 for (node
= xmlFirstElementChild(event_node
); node
;
1562 node
= xmlNextElementSibling(node
)) {
1563 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1568 content
= xmlNodeGetContent(node
);
1570 ret
= -LTTNG_ERR_NOMEM
;
1574 name_len
= strlen((char *) content
);
1575 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1576 WARN("Channel name too long.");
1577 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1582 strncpy(event
.name
, (const char *) content
, name_len
);
1584 } else if (!strcmp((const char *) node
->name
,
1585 config_element_enabled
)) {
1586 xmlChar
*content
= xmlNodeGetContent(node
);
1590 ret
= -LTTNG_ERR_NOMEM
;
1594 ret
= parse_bool(content
, &event
.enabled
);
1597 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1600 } else if (!strcmp((const char *) node
->name
,
1601 config_element_type
)) {
1602 xmlChar
*content
= xmlNodeGetContent(node
);
1606 ret
= -LTTNG_ERR_NOMEM
;
1610 ret
= get_event_type(content
);
1613 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1618 } else if (!strcmp((const char *) node
->name
,
1619 config_element_loglevel_type
)) {
1620 xmlChar
*content
= xmlNodeGetContent(node
);
1624 ret
= -LTTNG_ERR_NOMEM
;
1628 ret
= get_loglevel_type(content
);
1631 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1635 event
.loglevel_type
= ret
;
1636 } else if (!strcmp((const char *) node
->name
,
1637 config_element_loglevel
)) {
1639 int64_t loglevel
= 0;
1642 content
= xmlNodeGetContent(node
);
1644 ret
= -LTTNG_ERR_NOMEM
;
1648 ret
= parse_int(content
, &loglevel
);
1651 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1655 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1656 WARN("loglevel out of range.");
1657 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1661 event
.loglevel
= loglevel
;
1662 } else if (!strcmp((const char *) node
->name
,
1663 config_element_filter
)) {
1665 xmlNodeGetContent(node
);
1669 ret
= -LTTNG_ERR_NOMEM
;
1673 free(filter_expression
);
1674 filter_expression
= strdup((char *) content
);
1676 if (!filter_expression
) {
1677 ret
= -LTTNG_ERR_NOMEM
;
1680 } else if (!strcmp((const char *) node
->name
,
1681 config_element_exclusions
)) {
1682 xmlNodePtr exclusion_node
;
1683 int exclusion_index
= 0;
1688 * Exclusions has already been initialized,
1691 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1695 exclusion_count
= xmlChildElementCount(node
);
1696 if (!exclusion_count
) {
1700 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1702 exclusion_count
= 0;
1703 ret
= -LTTNG_ERR_NOMEM
;
1707 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1708 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1710 xmlNodeGetContent(exclusion_node
);
1713 ret
= -LTTNG_ERR_NOMEM
;
1717 exclusions
[exclusion_index
] = strdup((const char *) content
);
1719 if (!exclusions
[exclusion_index
]) {
1720 ret
= -LTTNG_ERR_NOMEM
;
1726 event
.exclusion
= 1;
1727 } else if (!strcmp((const char *) node
->name
,
1728 config_element_attributes
)) {
1729 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1732 if (!attribute_node
) {
1733 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1737 if (!strcmp((const char *) node
->name
,
1738 config_element_probe_attributes
)) {
1739 xmlNodePtr probe_attribute_node
;
1741 /* probe_attributes */
1742 for (probe_attribute_node
=
1743 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1744 probe_attribute_node
= xmlNextElementSibling(
1745 probe_attribute_node
)) {
1747 ret
= process_probe_attribute_node(probe_attribute_node
,
1756 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1758 /* function_attributes */
1759 content
= xmlNodeGetContent(symbol_node
);
1761 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1765 sym_len
= strlen((char *) content
);
1766 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1767 WARN("Function name too long.");
1768 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1773 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1780 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1781 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1782 filter_expression
, exclusion_count
, exclusions
);
1784 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1785 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1790 for (i
= 0; i
< exclusion_count
; i
++) {
1791 free(exclusions
[i
]);
1795 free(filter_expression
);
1800 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1801 const char *channel_name
)
1804 struct lttng_event event
;
1807 assert(events_node
);
1809 assert(channel_name
);
1811 for (node
= xmlFirstElementChild(events_node
); node
;
1812 node
= xmlNextElementSibling(node
)) {
1813 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1820 * Disable all events to enable only the necessary events.
1821 * Limitations regarding lttng_disable_events and tuple descriptor
1822 * force this approach.
1824 memset(&event
, 0, sizeof(event
));
1825 event
.loglevel
= -1;
1826 event
.type
= LTTNG_EVENT_ALL
;
1827 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1832 for (node
= xmlFirstElementChild(events_node
); node
;
1833 node
= xmlNextElementSibling(node
)) {
1834 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1845 int process_channel_attr_node(xmlNodePtr attr_node
,
1846 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1847 xmlNodePtr
*events_node
)
1853 assert(contexts_node
);
1854 assert(events_node
);
1856 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1861 content
= xmlNodeGetContent(attr_node
);
1863 ret
= -LTTNG_ERR_NOMEM
;
1867 name_len
= strlen((char *) content
);
1868 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1869 WARN("Channel name too long.");
1870 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1875 strncpy(channel
->name
, (const char *) content
, name_len
);
1877 } else if (!strcmp((const char *) attr_node
->name
,
1878 config_element_enabled
)) {
1883 content
= xmlNodeGetContent(attr_node
);
1885 ret
= -LTTNG_ERR_NOMEM
;
1889 ret
= parse_bool(content
, &enabled
);
1892 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1896 channel
->enabled
= enabled
;
1897 } else if (!strcmp((const char *) attr_node
->name
,
1898 config_element_overwrite_mode
)) {
1901 /* overwrite_mode */
1902 content
= xmlNodeGetContent(attr_node
);
1904 ret
= -LTTNG_ERR_NOMEM
;
1908 ret
= get_overwrite_mode(content
);
1911 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1915 channel
->attr
.overwrite
= ret
;
1916 } else if (!strcmp((const char *) attr_node
->name
,
1917 config_element_subbuf_size
)) {
1920 /* subbuffer_size */
1921 content
= xmlNodeGetContent(attr_node
);
1923 ret
= -LTTNG_ERR_NOMEM
;
1927 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1930 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1933 } else if (!strcmp((const char *) attr_node
->name
,
1934 config_element_num_subbuf
)) {
1937 /* subbuffer_count */
1938 content
= xmlNodeGetContent(attr_node
);
1940 ret
= -LTTNG_ERR_NOMEM
;
1944 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1947 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1950 } else if (!strcmp((const char *) attr_node
->name
,
1951 config_element_switch_timer_interval
)) {
1953 uint64_t switch_timer_interval
= 0;
1955 /* switch_timer_interval */
1956 content
= xmlNodeGetContent(attr_node
);
1958 ret
= -LTTNG_ERR_NOMEM
;
1962 ret
= parse_uint(content
, &switch_timer_interval
);
1965 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1969 if (switch_timer_interval
> UINT_MAX
) {
1970 WARN("switch_timer_interval out of range.");
1971 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1975 channel
->attr
.switch_timer_interval
=
1976 switch_timer_interval
;
1977 } else if (!strcmp((const char *) attr_node
->name
,
1978 config_element_read_timer_interval
)) {
1980 uint64_t read_timer_interval
= 0;
1982 /* read_timer_interval */
1983 content
= xmlNodeGetContent(attr_node
);
1985 ret
= -LTTNG_ERR_NOMEM
;
1989 ret
= parse_uint(content
, &read_timer_interval
);
1992 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1996 if (read_timer_interval
> UINT_MAX
) {
1997 WARN("read_timer_interval out of range.");
1998 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2002 channel
->attr
.read_timer_interval
=
2003 read_timer_interval
;
2004 } else if (!strcmp((const char *) attr_node
->name
,
2005 config_element_output_type
)) {
2009 content
= xmlNodeGetContent(attr_node
);
2011 ret
= -LTTNG_ERR_NOMEM
;
2015 ret
= get_output_type(content
);
2018 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2022 channel
->attr
.output
= ret
;
2023 } else if (!strcmp((const char *) attr_node
->name
,
2024 config_element_tracefile_size
)) {
2027 /* tracefile_size */
2028 content
= xmlNodeGetContent(attr_node
);
2030 ret
= -LTTNG_ERR_NOMEM
;
2034 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2037 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2040 } else if (!strcmp((const char *) attr_node
->name
,
2041 config_element_tracefile_count
)) {
2044 /* tracefile_count */
2045 content
= xmlNodeGetContent(attr_node
);
2047 ret
= -LTTNG_ERR_NOMEM
;
2051 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2054 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2057 } else if (!strcmp((const char *) attr_node
->name
,
2058 config_element_live_timer_interval
)) {
2060 uint64_t live_timer_interval
= 0;
2062 /* live_timer_interval */
2063 content
= xmlNodeGetContent(attr_node
);
2065 ret
= -LTTNG_ERR_NOMEM
;
2069 ret
= parse_uint(content
, &live_timer_interval
);
2072 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2076 if (live_timer_interval
> UINT_MAX
) {
2077 WARN("live_timer_interval out of range.");
2078 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2082 channel
->attr
.live_timer_interval
=
2083 live_timer_interval
;
2084 } else if (!strcmp((const char *) attr_node
->name
,
2085 config_element_monitor_timer_interval
)) {
2087 uint64_t monitor_timer_interval
= 0;
2089 /* monitor_timer_interval */
2090 content
= xmlNodeGetContent(attr_node
);
2092 ret
= -LTTNG_ERR_NOMEM
;
2096 ret
= parse_uint(content
, &monitor_timer_interval
);
2099 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2103 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2104 monitor_timer_interval
);
2106 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2109 } else if (!strcmp((const char *) attr_node
->name
,
2110 config_element_blocking_timeout
)) {
2112 int64_t blocking_timeout
= 0;
2114 /* blocking_timeout */
2115 content
= xmlNodeGetContent(attr_node
);
2117 ret
= -LTTNG_ERR_NOMEM
;
2121 ret
= parse_int(content
, &blocking_timeout
);
2124 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2128 ret
= lttng_channel_set_blocking_timeout(channel
,
2131 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2134 } else if (!strcmp((const char *) attr_node
->name
,
2135 config_element_events
)) {
2137 *events_node
= attr_node
;
2140 *contexts_node
= attr_node
;
2148 int process_context_node(xmlNodePtr context_node
,
2149 struct lttng_handle
*handle
, const char *channel_name
)
2152 struct lttng_event_context context
;
2153 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2156 assert(channel_name
);
2158 if (!context_child_node
) {
2159 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2163 memset(&context
, 0, sizeof(context
));
2165 if (!strcmp((const char *) context_child_node
->name
,
2166 config_element_type
)) {
2168 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2171 ret
= -LTTNG_ERR_NOMEM
;
2175 ret
= get_context_type(content
);
2178 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2183 } else if (!strcmp((const char *) context_child_node
->name
,
2184 config_element_context_perf
)) {
2186 xmlNodePtr perf_attr_node
;
2188 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2189 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2190 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2191 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2192 perf_attr_node
; perf_attr_node
=
2193 xmlNextElementSibling(perf_attr_node
)) {
2194 if (!strcmp((const char *) perf_attr_node
->name
,
2195 config_element_type
)) {
2200 content
= xmlNodeGetContent(perf_attr_node
);
2202 ret
= -LTTNG_ERR_NOMEM
;
2206 ret
= parse_uint(content
, &type
);
2209 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2213 if (type
> UINT32_MAX
) {
2214 WARN("perf context type out of range.");
2215 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2219 context
.u
.perf_counter
.type
= type
;
2220 } else if (!strcmp((const char *) perf_attr_node
->name
,
2221 config_element_config
)) {
2223 uint64_t config
= 0;
2226 content
= xmlNodeGetContent(perf_attr_node
);
2228 ret
= -LTTNG_ERR_NOMEM
;
2232 ret
= parse_uint(content
, &config
);
2235 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2239 context
.u
.perf_counter
.config
= config
;
2240 } else if (!strcmp((const char *) perf_attr_node
->name
,
2241 config_element_name
)) {
2246 content
= xmlNodeGetContent(perf_attr_node
);
2248 ret
= -LTTNG_ERR_NOMEM
;
2252 name_len
= strlen((char *) content
);
2253 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2254 WARN("perf context name too long.");
2255 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2260 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2265 } else if (!strcmp((const char *) context_child_node
->name
,
2266 config_element_context_app
)) {
2267 /* application context */
2268 xmlNodePtr app_ctx_node
;
2270 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2271 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2272 app_ctx_node
; app_ctx_node
=
2273 xmlNextElementSibling(app_ctx_node
)) {
2275 char **target
= strcmp(
2276 (const char *) app_ctx_node
->name
,
2277 config_element_context_app_provider_name
) == 0 ?
2278 &context
.u
.app_ctx
.provider_name
:
2279 &context
.u
.app_ctx
.ctx_name
;
2281 content
= xmlNodeGetContent(app_ctx_node
);
2283 ret
= -LTTNG_ERR_NOMEM
;
2287 *target
= (char *) content
;
2290 /* Unrecognized context type */
2291 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2295 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2296 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2297 free(context
.u
.app_ctx
.provider_name
);
2298 free(context
.u
.app_ctx
.ctx_name
);
2305 int process_contexts_node(xmlNodePtr contexts_node
,
2306 struct lttng_handle
*handle
, const char *channel_name
)
2309 xmlNodePtr context_node
;
2311 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2312 context_node
= xmlNextElementSibling(context_node
)) {
2313 ret
= process_context_node(context_node
, handle
, channel_name
);
2323 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2324 struct lttng_handle
*handle
)
2327 xmlNodePtr targets_node
= NULL
;
2331 assert(pid_tracker_node
);
2332 /* get the targets node */
2333 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2334 node
= xmlNextElementSibling(node
)) {
2335 if (!strcmp((const char *) node
->name
,
2336 config_element_targets
)) {
2337 targets_node
= node
;
2342 if (!targets_node
) {
2343 ret
= LTTNG_ERR_INVALID
;
2347 /* Go through all pid_target node */
2348 child
= xmlChildElementCount(targets_node
);
2350 /* The session is explicitly set to target nothing. */
2351 ret
= lttng_untrack_pid(handle
, -1);
2356 for (node
= xmlFirstElementChild(targets_node
); node
;
2357 node
= xmlNextElementSibling(node
)) {
2358 xmlNodePtr pid_target_node
= node
;
2360 /* get pid node and track it */
2361 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2362 node
= xmlNextElementSibling(node
)) {
2363 if (!strcmp((const char *) node
->name
,
2364 config_element_pid
)) {
2366 xmlChar
*content
= NULL
;
2368 content
= xmlNodeGetContent(node
);
2370 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2374 ret
= parse_int(content
, &pid
);
2377 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2381 ret
= lttng_track_pid(handle
, (int) pid
);
2387 node
= pid_target_node
;
2395 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2398 struct lttng_domain domain
= { 0 };
2399 struct lttng_handle
*handle
= NULL
;
2400 struct lttng_channel
*channel
= NULL
;
2401 xmlNodePtr channels_node
= NULL
;
2402 xmlNodePtr trackers_node
= NULL
;
2403 xmlNodePtr pid_tracker_node
= NULL
;
2406 assert(session_name
);
2408 ret
= init_domain(domain_node
, &domain
);
2413 handle
= lttng_create_handle(session_name
, &domain
);
2415 ret
= -LTTNG_ERR_NOMEM
;
2419 /* get the channels node */
2420 for (node
= xmlFirstElementChild(domain_node
); node
;
2421 node
= xmlNextElementSibling(node
)) {
2422 if (!strcmp((const char *) node
->name
,
2423 config_element_channels
)) {
2424 channels_node
= node
;
2429 if (!channels_node
) {
2433 /* create all channels */
2434 for (node
= xmlFirstElementChild(channels_node
); node
;
2435 node
= xmlNextElementSibling(node
)) {
2436 const enum lttng_domain_type original_domain
= domain
.type
;
2437 xmlNodePtr contexts_node
= NULL
;
2438 xmlNodePtr events_node
= NULL
;
2439 xmlNodePtr channel_attr_node
;
2442 * Channels of the "agent" types cannot be created directly.
2443 * They are meant to be created implicitly through the
2444 * activation of events in their domain. However, a user
2445 * can override the default channel configuration attributes
2446 * by creating the underlying UST channel _before_ enabling
2447 * an agent domain event.
2449 * Hence, the channel's type is substituted before the creation
2450 * and restored by the time the events are created.
2452 switch (domain
.type
) {
2453 case LTTNG_DOMAIN_JUL
:
2454 case LTTNG_DOMAIN_LOG4J
:
2455 case LTTNG_DOMAIN_PYTHON
:
2456 domain
.type
= LTTNG_DOMAIN_UST
;
2461 channel
= lttng_channel_create(&domain
);
2467 for (channel_attr_node
= xmlFirstElementChild(node
);
2468 channel_attr_node
; channel_attr_node
=
2469 xmlNextElementSibling(channel_attr_node
)) {
2470 ret
= process_channel_attr_node(channel_attr_node
,
2471 channel
, &contexts_node
, &events_node
);
2477 ret
= lttng_enable_channel(handle
, channel
);
2482 /* Restore the original channel domain. */
2483 domain
.type
= original_domain
;
2485 ret
= process_events_node(events_node
, handle
, channel
->name
);
2490 ret
= process_contexts_node(contexts_node
, handle
,
2496 lttng_channel_destroy(channel
);
2500 /* get the trackers node */
2501 for (node
= xmlFirstElementChild(domain_node
); node
;
2502 node
= xmlNextElementSibling(node
)) {
2503 if (!strcmp((const char *) node
->name
,
2504 config_element_trackers
)) {
2505 trackers_node
= node
;
2510 if (!trackers_node
) {
2514 for (node
= xmlFirstElementChild(trackers_node
); node
;
2515 node
= xmlNextElementSibling(node
)) {
2516 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2517 pid_tracker_node
= node
;
2518 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2525 if (!pid_tracker_node
) {
2526 lttng_track_pid(handle
, -1);
2530 lttng_channel_destroy(channel
);
2531 lttng_destroy_handle(handle
);
2536 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2538 const struct config_load_session_override_attr
*overrides
)
2540 int ret
, started
= -1, snapshot_mode
= -1;
2541 uint64_t live_timer_interval
= UINT64_MAX
,
2542 rotation_timer_interval
= 0,
2544 xmlChar
*name
= NULL
;
2545 xmlChar
*shm_path
= NULL
;
2546 xmlNodePtr domains_node
= NULL
;
2547 xmlNodePtr output_node
= NULL
;
2549 xmlNodePtr attributes_child
;
2550 struct lttng_domain
*kernel_domain
= NULL
;
2551 struct lttng_domain
*ust_domain
= NULL
;
2552 struct lttng_domain
*jul_domain
= NULL
;
2553 struct lttng_domain
*log4j_domain
= NULL
;
2554 struct lttng_domain
*python_domain
= NULL
;
2556 for (node
= xmlFirstElementChild(session_node
); node
;
2557 node
= xmlNextElementSibling(node
)) {
2558 if (!name
&& !strcmp((const char *) node
->name
,
2559 config_element_name
)) {
2561 xmlChar
*node_content
= xmlNodeGetContent(node
);
2562 if (!node_content
) {
2563 ret
= -LTTNG_ERR_NOMEM
;
2567 name
= node_content
;
2568 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2569 config_element_domains
)) {
2571 domains_node
= node
;
2572 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2573 config_element_started
)) {
2575 xmlChar
*node_content
= xmlNodeGetContent(node
);
2576 if (!node_content
) {
2577 ret
= -LTTNG_ERR_NOMEM
;
2581 ret
= parse_bool(node_content
, &started
);
2584 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2587 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2588 config_element_output
)) {
2591 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2592 config_element_shared_memory_path
)) {
2593 /* shared memory path */
2594 xmlChar
*node_content
= xmlNodeGetContent(node
);
2595 if (!node_content
) {
2596 ret
= -LTTNG_ERR_NOMEM
;
2600 shm_path
= node_content
;
2603 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2604 * rotation_timer_interval.
2606 for (attributes_child
= xmlFirstElementChild(node
); attributes_child
;
2607 attributes_child
= xmlNextElementSibling(attributes_child
)) {
2608 if (!strcmp((const char *) attributes_child
->name
,
2609 config_element_snapshot_mode
)) {
2611 xmlChar
*snapshot_mode_content
=
2612 xmlNodeGetContent(attributes_child
);
2613 if (!snapshot_mode_content
) {
2614 ret
= -LTTNG_ERR_NOMEM
;
2618 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2619 free(snapshot_mode_content
);
2621 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2624 } else if (!strcmp((const char *) attributes_child
->name
,
2625 config_element_live_timer_interval
)) {
2626 /* live_timer_interval */
2627 xmlChar
*timer_interval_content
=
2628 xmlNodeGetContent(attributes_child
);
2629 if (!timer_interval_content
) {
2630 ret
= -LTTNG_ERR_NOMEM
;
2634 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2635 free(timer_interval_content
);
2637 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2641 if (!strcmp((const char *) attributes_child
->name
,
2642 config_element_rotation_timer_interval
)) {
2643 /* rotation_timer_interval */
2644 xmlChar
*timer_interval_content
=
2645 xmlNodeGetContent(attributes_child
);
2646 if (!timer_interval_content
) {
2647 ret
= -LTTNG_ERR_NOMEM
;
2651 ret
= parse_uint(timer_interval_content
, &rotation_timer_interval
);
2652 free(timer_interval_content
);
2654 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2658 if (!strcmp((const char *) attributes_child
->name
,
2659 config_element_rotation_size
)) {
2661 xmlChar
*rotation_size_content
=
2662 xmlNodeGetContent(attributes_child
);
2663 if (!rotation_size_content
) {
2664 ret
= -LTTNG_ERR_NOMEM
;
2668 ret
= parse_uint(rotation_size_content
, &rotation_size
);
2669 free(rotation_size_content
);
2671 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2680 /* Mandatory attribute, as defined in the session XSD */
2681 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2685 if (session_name
&& strcmp((char *) name
, session_name
)) {
2686 /* This is not the session we are looking for */
2687 ret
= -LTTNG_ERR_NO_SESSION
;
2691 /* Init domains to create the session handles */
2692 for (node
= xmlFirstElementChild(domains_node
); node
;
2693 node
= xmlNextElementSibling(node
)) {
2694 struct lttng_domain
*domain
;
2696 domain
= zmalloc(sizeof(*domain
));
2698 ret
= -LTTNG_ERR_NOMEM
;
2702 ret
= init_domain(node
, domain
);
2704 goto domain_init_error
;
2707 switch (domain
->type
) {
2708 case LTTNG_DOMAIN_KERNEL
:
2709 if (kernel_domain
) {
2710 /* Same domain seen twice, invalid! */
2711 goto domain_init_error
;
2713 kernel_domain
= domain
;
2715 case LTTNG_DOMAIN_UST
:
2717 /* Same domain seen twice, invalid! */
2718 goto domain_init_error
;
2720 ust_domain
= domain
;
2722 case LTTNG_DOMAIN_JUL
:
2724 /* Same domain seen twice, invalid! */
2725 goto domain_init_error
;
2727 jul_domain
= domain
;
2729 case LTTNG_DOMAIN_LOG4J
:
2731 /* Same domain seen twice, invalid! */
2732 goto domain_init_error
;
2734 log4j_domain
= domain
;
2736 case LTTNG_DOMAIN_PYTHON
:
2737 if (python_domain
) {
2738 /* Same domain seen twice, invalid! */
2739 goto domain_init_error
;
2741 python_domain
= domain
;
2744 WARN("Invalid domain type");
2745 goto domain_init_error
;
2750 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2754 /* Apply overrides */
2756 if (overrides
->session_name
) {
2757 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2758 if (!name_override
) {
2759 ret
= -LTTNG_ERR_NOMEM
;
2763 /* Overrides the session name to the provided name */
2765 name
= name_override
;
2770 /* Destroy session if it exists */
2771 ret
= lttng_destroy_session((const char *) name
);
2772 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2773 ERR("Failed to destroy existing session.");
2778 /* Create session type depending on output type */
2779 if (snapshot_mode
&& snapshot_mode
!= -1) {
2780 ret
= create_snapshot_session((const char *) name
, output_node
,
2782 } else if (live_timer_interval
&&
2783 live_timer_interval
!= UINT64_MAX
) {
2784 ret
= create_session((const char *) name
,
2785 output_node
, live_timer_interval
, overrides
);
2787 /* regular session */
2788 ret
= create_session((const char *) name
,
2789 output_node
, UINT64_MAX
, overrides
);
2796 ret
= lttng_set_session_shm_path((const char *) name
,
2797 (const char *) shm_path
);
2803 for (node
= xmlFirstElementChild(domains_node
); node
;
2804 node
= xmlNextElementSibling(node
)) {
2805 ret
= process_domain_node(node
, (const char *) name
);
2811 if (rotation_timer_interval
|| rotation_size
) {
2812 struct lttng_rotation_schedule_attr
*rotation_attr
= lttng_rotation_schedule_attr_create();
2814 if (!rotation_attr
) {
2817 ret
= lttng_rotation_schedule_attr_set_session_name(rotation_attr
, (const char *) name
);
2819 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2822 lttng_rotation_schedule_attr_set_timer_period(rotation_attr
,
2823 rotation_timer_interval
);
2824 lttng_rotation_schedule_attr_set_size(rotation_attr
, rotation_size
);
2825 ret
= lttng_rotation_set_schedule(rotation_attr
);
2826 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2833 ret
= lttng_start_tracing((const char *) name
);
2841 ERR("Failed to load session %s: %s", (const char *) name
,
2842 lttng_strerror(ret
));
2843 lttng_destroy_session((const char *) name
);
2847 free(kernel_domain
);
2851 free(python_domain
);
2858 * Return 1 if the given path is readable by the current UID or 0 if not.
2859 * Return -1 if the path is EPERM.
2861 static int validate_file_read_creds(const char *path
)
2867 /* Can we read the file. */
2868 ret
= access(path
, R_OK
);
2872 if (errno
== EACCES
) {
2883 int load_session_from_file(const char *path
, const char *session_name
,
2884 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2885 const struct config_load_session_override_attr
*overrides
)
2887 int ret
, session_found
= !session_name
;
2888 xmlDocPtr doc
= NULL
;
2889 xmlNodePtr sessions_node
;
2890 xmlNodePtr session_node
;
2893 assert(validation_ctx
);
2895 ret
= validate_file_read_creds(path
);
2898 ret
= -LTTNG_ERR_EPERM
;
2900 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2905 doc
= xmlParseFile(path
);
2907 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2911 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2913 ERR("Session configuration file validation failed");
2914 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2918 sessions_node
= xmlDocGetRootElement(doc
);
2919 if (!sessions_node
) {
2923 for (session_node
= xmlFirstElementChild(sessions_node
);
2924 session_node
; session_node
=
2925 xmlNextElementSibling(session_node
)) {
2926 ret
= process_session_node(session_node
,
2927 session_name
, overwrite
, overrides
);
2928 if (session_name
&& ret
== 0) {
2929 /* Target session found and loaded */
2937 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2942 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2944 struct dirent
*alloc_dirent(const char *path
)
2948 struct dirent
*entry
;
2950 name_max
= pathconf(path
, _PC_NAME_MAX
);
2951 if (name_max
== -1) {
2952 name_max
= PATH_MAX
;
2954 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2955 entry
= zmalloc(len
);
2960 int load_session_from_path(const char *path
, const char *session_name
,
2961 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2962 const struct config_load_session_override_attr
*overrides
)
2964 int ret
, session_found
= !session_name
;
2965 DIR *directory
= NULL
;
2968 assert(validation_ctx
);
2970 directory
= opendir(path
);
2974 /* Try the file loading. */
2977 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2980 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2985 struct dirent
*entry
;
2986 struct dirent
*result
;
2987 char *file_path
= NULL
;
2988 size_t path_len
= strlen(path
);
2990 if (path_len
>= PATH_MAX
) {
2991 ret
= -LTTNG_ERR_INVALID
;
2995 entry
= alloc_dirent(path
);
2997 ret
= -LTTNG_ERR_NOMEM
;
3001 file_path
= zmalloc(PATH_MAX
);
3003 ret
= -LTTNG_ERR_NOMEM
;
3008 strncpy(file_path
, path
, path_len
);
3009 if (file_path
[path_len
- 1] != '/') {
3010 file_path
[path_len
++] = '/';
3014 /* Search for *.lttng files */
3015 while (!readdir_r(directory
, entry
, &result
) && result
) {
3016 size_t file_name_len
= strlen(result
->d_name
);
3018 if (file_name_len
<=
3019 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
3023 if (path_len
+ file_name_len
>= PATH_MAX
) {
3027 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
3028 result
->d_name
+ file_name_len
- sizeof(
3029 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
3033 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
3034 file_path
[path_len
+ file_name_len
] = '\0';
3036 ret
= load_session_from_file(file_path
, session_name
,
3037 validation_ctx
, overwrite
, overrides
);
3038 if (session_name
&& !ret
) {
3047 ret
= load_session_from_file(path
, session_name
,
3048 validation_ctx
, overwrite
, overrides
);
3058 if (closedir(directory
)) {
3063 if (session_found
&& !ret
) {
3071 * Validate that the given path's credentials and the current process have the
3072 * same UID. If so, return 1 else return 0 if it does NOT match.
3074 static int validate_path_creds(const char *path
)
3076 int ret
, uid
= getuid();
3085 ret
= stat(path
, &buf
);
3087 if (errno
!= ENOENT
) {
3093 if (buf
.st_uid
!= uid
) {
3104 int config_load_session(const char *path
, const char *session_name
,
3105 int overwrite
, unsigned int autoload
,
3106 const struct config_load_session_override_attr
*overrides
)
3109 bool session_loaded
= false;
3110 const char *path_ptr
= NULL
;
3111 struct session_config_validation_ctx validation_ctx
= { 0 };
3113 ret
= init_session_config_validation_ctx(&validation_ctx
);
3120 const char *sys_path
;
3123 home_path
= utils_get_home_dir();
3125 char path
[PATH_MAX
];
3128 * Try user session configuration path. Ignore error here so we can
3129 * continue loading the system wide sessions.
3132 ret
= snprintf(path
, sizeof(path
),
3133 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3134 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3136 PERROR("snprintf session autoload home config path");
3141 * Credentials are only validated for the autoload in order to
3142 * avoid any user session daemon to try to load kernel sessions
3143 * automatically and failing all the times.
3145 ret
= validate_path_creds(path
);
3150 ret
= snprintf(path
, sizeof(path
),
3151 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3153 PERROR("snprintf session home config path");
3159 ret
= load_session_from_path(path_ptr
, session_name
,
3160 &validation_ctx
, overwrite
, overrides
);
3161 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3165 * Continue even if the session was found since we have to try
3166 * the system wide sessions.
3168 session_loaded
= true;
3172 /* Reset path pointer for the system wide dir. */
3175 /* Try system wide configuration directory. */
3177 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3178 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3179 ret
= validate_path_creds(sys_path
);
3181 path_ptr
= sys_path
;
3184 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3185 path_ptr
= sys_path
;
3189 ret
= load_session_from_path(path_ptr
, session_name
,
3190 &validation_ctx
, overwrite
, overrides
);
3192 session_loaded
= true;
3196 ret
= access(path
, F_OK
);
3201 ret
= -LTTNG_ERR_INVALID
;
3202 WARN("Session configuration path does not exist.");
3205 ret
= -LTTNG_ERR_EPERM
;
3208 ret
= -LTTNG_ERR_UNK
;
3214 ret
= load_session_from_path(path
, session_name
,
3215 &validation_ctx
, overwrite
, overrides
);
3218 fini_session_config_validation_ctx(&validation_ctx
);
3219 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3221 * Don't report an error if no sessions are found when called
3222 * without a session_name or a search path.
3227 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3228 /* A matching session was found in one of the search paths. */
3235 void __attribute__((destructor
)) session_config_exit(void)