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 struct lttng_domain
*kernel_domain
,
1349 struct lttng_domain
*ust_domain
,
1350 struct lttng_domain
*jul_domain
,
1351 struct lttng_domain
*log4j_domain
,
1352 xmlNodePtr output_node
,
1353 uint64_t live_timer_interval
,
1354 const struct config_load_session_override_attr
*overrides
)
1357 struct consumer_output output
= { 0 };
1358 xmlNodePtr consumer_output_node
;
1359 const char *control_uri
= NULL
;
1360 const char *data_uri
= NULL
;
1361 const char *path
= NULL
;
1366 consumer_output_node
= xmlFirstElementChild(output_node
);
1367 if (!consumer_output_node
) {
1368 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1372 if (strcmp((const char *) consumer_output_node
->name
,
1373 config_element_consumer_output
)) {
1374 WARN("Invalid output type, expected %s node",
1375 config_element_consumer_output
);
1376 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1380 ret
= process_consumer_output(consumer_output_node
, &output
);
1386 control_uri
= output
.control_uri
;
1387 data_uri
= output
.data_uri
;
1390 /* Check for override and apply them */
1392 if (overrides
->path_url
) {
1393 path
= overrides
->path_url
;
1394 /* control/data_uri are null */;
1398 if (overrides
->ctrl_url
) {
1399 control_uri
= overrides
->ctrl_url
;
1403 if (overrides
->data_url
) {
1404 data_uri
= overrides
->data_url
;
1412 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1413 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1417 if (control_uri
|| data_uri
) {
1418 /* network destination */
1419 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1421 * URLs are provided for sure since the test above make sure that
1422 * with a live timer the data and control URIs are provided. So,
1423 * NULL is passed here and will be set right after.
1425 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1427 ret
= lttng_create_session(name
, NULL
);
1433 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1439 /* either local output or no output */
1440 ret
= lttng_create_session(name
, path
);
1447 free(output
.control_uri
);
1448 free(output
.data_uri
);
1452 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1453 struct lttng_event_probe_attr
*attr
)
1457 assert(probe_attribute_node
);
1460 if (!strcmp((const char *) probe_attribute_node
->name
,
1461 config_element_address
)) {
1466 content
= xmlNodeGetContent(probe_attribute_node
);
1468 ret
= -LTTNG_ERR_NOMEM
;
1472 ret
= parse_uint(content
, &addr
);
1475 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1480 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1481 config_element_offset
)) {
1483 uint64_t offset
= 0;
1486 content
= xmlNodeGetContent(probe_attribute_node
);
1488 ret
= -LTTNG_ERR_NOMEM
;
1492 ret
= parse_uint(content
, &offset
);
1495 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1499 attr
->offset
= offset
;
1500 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1501 config_element_symbol_name
)) {
1506 content
= xmlNodeGetContent(probe_attribute_node
);
1508 ret
= -LTTNG_ERR_NOMEM
;
1512 name_len
= strlen((char *) content
);
1513 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1514 WARN("symbol_name too long.");
1515 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1520 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1529 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1530 const char *channel_name
, const enum process_event_node_phase phase
)
1534 struct lttng_event event
;
1535 char **exclusions
= NULL
;
1536 unsigned long exclusion_count
= 0;
1537 char *filter_expression
= NULL
;
1541 assert(channel_name
);
1543 memset(&event
, 0, sizeof(event
));
1545 /* Initialize default log level which varies by domain */
1546 switch (handle
->domain
.type
)
1548 case LTTNG_DOMAIN_JUL
:
1549 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1551 case LTTNG_DOMAIN_LOG4J
:
1552 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1554 case LTTNG_DOMAIN_PYTHON
:
1555 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1557 case LTTNG_DOMAIN_UST
:
1558 case LTTNG_DOMAIN_KERNEL
:
1559 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1565 for (node
= xmlFirstElementChild(event_node
); node
;
1566 node
= xmlNextElementSibling(node
)) {
1567 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1572 content
= xmlNodeGetContent(node
);
1574 ret
= -LTTNG_ERR_NOMEM
;
1578 name_len
= strlen((char *) content
);
1579 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1580 WARN("Channel name too long.");
1581 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1586 strncpy(event
.name
, (const char *) content
, name_len
);
1588 } else if (!strcmp((const char *) node
->name
,
1589 config_element_enabled
)) {
1590 xmlChar
*content
= xmlNodeGetContent(node
);
1594 ret
= -LTTNG_ERR_NOMEM
;
1598 ret
= parse_bool(content
, &event
.enabled
);
1601 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1604 } else if (!strcmp((const char *) node
->name
,
1605 config_element_type
)) {
1606 xmlChar
*content
= xmlNodeGetContent(node
);
1610 ret
= -LTTNG_ERR_NOMEM
;
1614 ret
= get_event_type(content
);
1617 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1622 } else if (!strcmp((const char *) node
->name
,
1623 config_element_loglevel_type
)) {
1624 xmlChar
*content
= xmlNodeGetContent(node
);
1628 ret
= -LTTNG_ERR_NOMEM
;
1632 ret
= get_loglevel_type(content
);
1635 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1639 event
.loglevel_type
= ret
;
1640 } else if (!strcmp((const char *) node
->name
,
1641 config_element_loglevel
)) {
1643 int64_t loglevel
= 0;
1646 content
= xmlNodeGetContent(node
);
1648 ret
= -LTTNG_ERR_NOMEM
;
1652 ret
= parse_int(content
, &loglevel
);
1655 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1659 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1660 WARN("loglevel out of range.");
1661 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1665 event
.loglevel
= loglevel
;
1666 } else if (!strcmp((const char *) node
->name
,
1667 config_element_filter
)) {
1669 xmlNodeGetContent(node
);
1673 ret
= -LTTNG_ERR_NOMEM
;
1677 free(filter_expression
);
1678 filter_expression
= strdup((char *) content
);
1680 if (!filter_expression
) {
1681 ret
= -LTTNG_ERR_NOMEM
;
1684 } else if (!strcmp((const char *) node
->name
,
1685 config_element_exclusions
)) {
1686 xmlNodePtr exclusion_node
;
1687 int exclusion_index
= 0;
1692 * Exclusions has already been initialized,
1695 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1699 exclusion_count
= xmlChildElementCount(node
);
1700 if (!exclusion_count
) {
1704 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1706 exclusion_count
= 0;
1707 ret
= -LTTNG_ERR_NOMEM
;
1711 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1712 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1714 xmlNodeGetContent(exclusion_node
);
1717 ret
= -LTTNG_ERR_NOMEM
;
1721 exclusions
[exclusion_index
] = strdup((const char *) content
);
1723 if (!exclusions
[exclusion_index
]) {
1724 ret
= -LTTNG_ERR_NOMEM
;
1730 event
.exclusion
= 1;
1731 } else if (!strcmp((const char *) node
->name
,
1732 config_element_attributes
)) {
1733 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1736 if (!attribute_node
) {
1737 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1741 if (!strcmp((const char *) node
->name
,
1742 config_element_probe_attributes
)) {
1743 xmlNodePtr probe_attribute_node
;
1745 /* probe_attributes */
1746 for (probe_attribute_node
=
1747 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1748 probe_attribute_node
= xmlNextElementSibling(
1749 probe_attribute_node
)) {
1751 ret
= process_probe_attribute_node(probe_attribute_node
,
1760 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1762 /* function_attributes */
1763 content
= xmlNodeGetContent(symbol_node
);
1765 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1769 sym_len
= strlen((char *) content
);
1770 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1771 WARN("Function name too long.");
1772 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1777 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1784 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1785 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1786 filter_expression
, exclusion_count
, exclusions
);
1788 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1789 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1794 for (i
= 0; i
< exclusion_count
; i
++) {
1795 free(exclusions
[i
]);
1799 free(filter_expression
);
1804 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1805 const char *channel_name
)
1808 struct lttng_event event
;
1811 assert(events_node
);
1813 assert(channel_name
);
1815 for (node
= xmlFirstElementChild(events_node
); node
;
1816 node
= xmlNextElementSibling(node
)) {
1817 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1824 * Disable all events to enable only the necessary events.
1825 * Limitations regarding lttng_disable_events and tuple descriptor
1826 * force this approach.
1828 memset(&event
, 0, sizeof(event
));
1829 event
.loglevel
= -1;
1830 event
.type
= LTTNG_EVENT_ALL
;
1831 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1836 for (node
= xmlFirstElementChild(events_node
); node
;
1837 node
= xmlNextElementSibling(node
)) {
1838 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1849 int process_channel_attr_node(xmlNodePtr attr_node
,
1850 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1851 xmlNodePtr
*events_node
)
1857 assert(contexts_node
);
1858 assert(events_node
);
1860 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1865 content
= xmlNodeGetContent(attr_node
);
1867 ret
= -LTTNG_ERR_NOMEM
;
1871 name_len
= strlen((char *) content
);
1872 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1873 WARN("Channel name too long.");
1874 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1879 strncpy(channel
->name
, (const char *) content
, name_len
);
1881 } else if (!strcmp((const char *) attr_node
->name
,
1882 config_element_enabled
)) {
1887 content
= xmlNodeGetContent(attr_node
);
1889 ret
= -LTTNG_ERR_NOMEM
;
1893 ret
= parse_bool(content
, &enabled
);
1896 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1900 channel
->enabled
= enabled
;
1901 } else if (!strcmp((const char *) attr_node
->name
,
1902 config_element_overwrite_mode
)) {
1905 /* overwrite_mode */
1906 content
= xmlNodeGetContent(attr_node
);
1908 ret
= -LTTNG_ERR_NOMEM
;
1912 ret
= get_overwrite_mode(content
);
1915 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1919 channel
->attr
.overwrite
= ret
;
1920 } else if (!strcmp((const char *) attr_node
->name
,
1921 config_element_subbuf_size
)) {
1924 /* subbuffer_size */
1925 content
= xmlNodeGetContent(attr_node
);
1927 ret
= -LTTNG_ERR_NOMEM
;
1931 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1934 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1937 } else if (!strcmp((const char *) attr_node
->name
,
1938 config_element_num_subbuf
)) {
1941 /* subbuffer_count */
1942 content
= xmlNodeGetContent(attr_node
);
1944 ret
= -LTTNG_ERR_NOMEM
;
1948 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1951 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1954 } else if (!strcmp((const char *) attr_node
->name
,
1955 config_element_switch_timer_interval
)) {
1957 uint64_t switch_timer_interval
= 0;
1959 /* switch_timer_interval */
1960 content
= xmlNodeGetContent(attr_node
);
1962 ret
= -LTTNG_ERR_NOMEM
;
1966 ret
= parse_uint(content
, &switch_timer_interval
);
1969 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1973 if (switch_timer_interval
> UINT_MAX
) {
1974 WARN("switch_timer_interval out of range.");
1975 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1979 channel
->attr
.switch_timer_interval
=
1980 switch_timer_interval
;
1981 } else if (!strcmp((const char *) attr_node
->name
,
1982 config_element_read_timer_interval
)) {
1984 uint64_t read_timer_interval
= 0;
1986 /* read_timer_interval */
1987 content
= xmlNodeGetContent(attr_node
);
1989 ret
= -LTTNG_ERR_NOMEM
;
1993 ret
= parse_uint(content
, &read_timer_interval
);
1996 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2000 if (read_timer_interval
> UINT_MAX
) {
2001 WARN("read_timer_interval out of range.");
2002 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2006 channel
->attr
.read_timer_interval
=
2007 read_timer_interval
;
2008 } else if (!strcmp((const char *) attr_node
->name
,
2009 config_element_output_type
)) {
2013 content
= xmlNodeGetContent(attr_node
);
2015 ret
= -LTTNG_ERR_NOMEM
;
2019 ret
= get_output_type(content
);
2022 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2026 channel
->attr
.output
= ret
;
2027 } else if (!strcmp((const char *) attr_node
->name
,
2028 config_element_tracefile_size
)) {
2031 /* tracefile_size */
2032 content
= xmlNodeGetContent(attr_node
);
2034 ret
= -LTTNG_ERR_NOMEM
;
2038 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2041 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2044 } else if (!strcmp((const char *) attr_node
->name
,
2045 config_element_tracefile_count
)) {
2048 /* tracefile_count */
2049 content
= xmlNodeGetContent(attr_node
);
2051 ret
= -LTTNG_ERR_NOMEM
;
2055 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2058 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2061 } else if (!strcmp((const char *) attr_node
->name
,
2062 config_element_live_timer_interval
)) {
2064 uint64_t live_timer_interval
= 0;
2066 /* live_timer_interval */
2067 content
= xmlNodeGetContent(attr_node
);
2069 ret
= -LTTNG_ERR_NOMEM
;
2073 ret
= parse_uint(content
, &live_timer_interval
);
2076 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2080 if (live_timer_interval
> UINT_MAX
) {
2081 WARN("live_timer_interval out of range.");
2082 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2086 channel
->attr
.live_timer_interval
=
2087 live_timer_interval
;
2088 } else if (!strcmp((const char *) attr_node
->name
,
2089 config_element_monitor_timer_interval
)) {
2091 uint64_t monitor_timer_interval
= 0;
2093 /* monitor_timer_interval */
2094 content
= xmlNodeGetContent(attr_node
);
2096 ret
= -LTTNG_ERR_NOMEM
;
2100 ret
= parse_uint(content
, &monitor_timer_interval
);
2103 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2107 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2108 monitor_timer_interval
);
2110 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2113 } else if (!strcmp((const char *) attr_node
->name
,
2114 config_element_blocking_timeout
)) {
2116 int64_t blocking_timeout
= 0;
2118 /* blocking_timeout */
2119 content
= xmlNodeGetContent(attr_node
);
2121 ret
= -LTTNG_ERR_NOMEM
;
2125 ret
= parse_int(content
, &blocking_timeout
);
2128 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2132 ret
= lttng_channel_set_blocking_timeout(channel
,
2135 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2138 } else if (!strcmp((const char *) attr_node
->name
,
2139 config_element_events
)) {
2141 *events_node
= attr_node
;
2144 *contexts_node
= attr_node
;
2152 int process_context_node(xmlNodePtr context_node
,
2153 struct lttng_handle
*handle
, const char *channel_name
)
2156 struct lttng_event_context context
;
2157 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2160 assert(channel_name
);
2162 if (!context_child_node
) {
2163 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2167 memset(&context
, 0, sizeof(context
));
2169 if (!strcmp((const char *) context_child_node
->name
,
2170 config_element_type
)) {
2172 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2175 ret
= -LTTNG_ERR_NOMEM
;
2179 ret
= get_context_type(content
);
2182 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2187 } else if (!strcmp((const char *) context_child_node
->name
,
2188 config_element_context_perf
)) {
2190 xmlNodePtr perf_attr_node
;
2192 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2193 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2194 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2195 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2196 perf_attr_node
; perf_attr_node
=
2197 xmlNextElementSibling(perf_attr_node
)) {
2198 if (!strcmp((const char *) perf_attr_node
->name
,
2199 config_element_type
)) {
2204 content
= xmlNodeGetContent(perf_attr_node
);
2206 ret
= -LTTNG_ERR_NOMEM
;
2210 ret
= parse_uint(content
, &type
);
2213 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2217 if (type
> UINT32_MAX
) {
2218 WARN("perf context type out of range.");
2219 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2223 context
.u
.perf_counter
.type
= type
;
2224 } else if (!strcmp((const char *) perf_attr_node
->name
,
2225 config_element_config
)) {
2227 uint64_t config
= 0;
2230 content
= xmlNodeGetContent(perf_attr_node
);
2232 ret
= -LTTNG_ERR_NOMEM
;
2236 ret
= parse_uint(content
, &config
);
2239 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2243 context
.u
.perf_counter
.config
= config
;
2244 } else if (!strcmp((const char *) perf_attr_node
->name
,
2245 config_element_name
)) {
2250 content
= xmlNodeGetContent(perf_attr_node
);
2252 ret
= -LTTNG_ERR_NOMEM
;
2256 name_len
= strlen((char *) content
);
2257 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2258 WARN("perf context name too long.");
2259 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2264 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2269 } else if (!strcmp((const char *) context_child_node
->name
,
2270 config_element_context_app
)) {
2271 /* application context */
2272 xmlNodePtr app_ctx_node
;
2274 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2275 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2276 app_ctx_node
; app_ctx_node
=
2277 xmlNextElementSibling(app_ctx_node
)) {
2279 char **target
= strcmp(
2280 (const char *) app_ctx_node
->name
,
2281 config_element_context_app_provider_name
) == 0 ?
2282 &context
.u
.app_ctx
.provider_name
:
2283 &context
.u
.app_ctx
.ctx_name
;
2285 content
= xmlNodeGetContent(app_ctx_node
);
2287 ret
= -LTTNG_ERR_NOMEM
;
2291 *target
= (char *) content
;
2294 /* Unrecognized context type */
2295 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2299 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2300 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2301 free(context
.u
.app_ctx
.provider_name
);
2302 free(context
.u
.app_ctx
.ctx_name
);
2309 int process_contexts_node(xmlNodePtr contexts_node
,
2310 struct lttng_handle
*handle
, const char *channel_name
)
2313 xmlNodePtr context_node
;
2315 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2316 context_node
= xmlNextElementSibling(context_node
)) {
2317 ret
= process_context_node(context_node
, handle
, channel_name
);
2327 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2328 struct lttng_handle
*handle
)
2331 xmlNodePtr targets_node
= NULL
;
2335 assert(pid_tracker_node
);
2336 /* get the targets node */
2337 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2338 node
= xmlNextElementSibling(node
)) {
2339 if (!strcmp((const char *) node
->name
,
2340 config_element_targets
)) {
2341 targets_node
= node
;
2346 if (!targets_node
) {
2347 ret
= LTTNG_ERR_INVALID
;
2351 /* Go through all pid_target node */
2352 child
= xmlChildElementCount(targets_node
);
2354 /* The session is explicitly set to target nothing. */
2355 ret
= lttng_untrack_pid(handle
, -1);
2360 for (node
= xmlFirstElementChild(targets_node
); node
;
2361 node
= xmlNextElementSibling(node
)) {
2362 xmlNodePtr pid_target_node
= node
;
2364 /* get pid node and track it */
2365 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2366 node
= xmlNextElementSibling(node
)) {
2367 if (!strcmp((const char *) node
->name
,
2368 config_element_pid
)) {
2370 xmlChar
*content
= NULL
;
2372 content
= xmlNodeGetContent(node
);
2374 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2378 ret
= parse_int(content
, &pid
);
2381 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2385 ret
= lttng_track_pid(handle
, (int) pid
);
2391 node
= pid_target_node
;
2399 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2402 struct lttng_domain domain
= { 0 };
2403 struct lttng_handle
*handle
= NULL
;
2404 struct lttng_channel
*channel
= NULL
;
2405 xmlNodePtr channels_node
= NULL
;
2406 xmlNodePtr trackers_node
= NULL
;
2407 xmlNodePtr pid_tracker_node
= NULL
;
2410 assert(session_name
);
2412 ret
= init_domain(domain_node
, &domain
);
2417 handle
= lttng_create_handle(session_name
, &domain
);
2419 ret
= -LTTNG_ERR_NOMEM
;
2423 /* get the channels node */
2424 for (node
= xmlFirstElementChild(domain_node
); node
;
2425 node
= xmlNextElementSibling(node
)) {
2426 if (!strcmp((const char *) node
->name
,
2427 config_element_channels
)) {
2428 channels_node
= node
;
2433 if (!channels_node
) {
2437 /* create all channels */
2438 for (node
= xmlFirstElementChild(channels_node
); node
;
2439 node
= xmlNextElementSibling(node
)) {
2440 xmlNodePtr contexts_node
= NULL
;
2441 xmlNodePtr events_node
= NULL
;
2442 xmlNodePtr channel_attr_node
;
2444 channel
= lttng_channel_create(&domain
);
2450 for (channel_attr_node
= xmlFirstElementChild(node
);
2451 channel_attr_node
; channel_attr_node
=
2452 xmlNextElementSibling(channel_attr_node
)) {
2453 ret
= process_channel_attr_node(channel_attr_node
,
2454 channel
, &contexts_node
, &events_node
);
2460 ret
= lttng_enable_channel(handle
, channel
);
2465 ret
= process_events_node(events_node
, handle
, channel
->name
);
2470 ret
= process_contexts_node(contexts_node
, handle
,
2476 lttng_channel_destroy(channel
);
2480 /* get the trackers node */
2481 for (node
= xmlFirstElementChild(domain_node
); node
;
2482 node
= xmlNextElementSibling(node
)) {
2483 if (!strcmp((const char *) node
->name
,
2484 config_element_trackers
)) {
2485 trackers_node
= node
;
2490 if (!trackers_node
) {
2494 for (node
= xmlFirstElementChild(trackers_node
); node
;
2495 node
= xmlNextElementSibling(node
)) {
2496 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2497 pid_tracker_node
= node
;
2498 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2505 if (!pid_tracker_node
) {
2506 lttng_track_pid(handle
, -1);
2510 lttng_channel_destroy(channel
);
2511 lttng_destroy_handle(handle
);
2516 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2518 const struct config_load_session_override_attr
*overrides
)
2520 int ret
, started
= -1, snapshot_mode
= -1;
2521 uint64_t live_timer_interval
= UINT64_MAX
,
2522 rotation_timer_interval
= 0;
2523 xmlChar
*name
= NULL
;
2524 xmlChar
*shm_path
= NULL
;
2525 xmlNodePtr domains_node
= NULL
;
2526 xmlNodePtr output_node
= NULL
;
2528 struct lttng_domain
*kernel_domain
= NULL
;
2529 struct lttng_domain
*ust_domain
= NULL
;
2530 struct lttng_domain
*jul_domain
= NULL
;
2531 struct lttng_domain
*log4j_domain
= NULL
;
2532 struct lttng_domain
*python_domain
= NULL
;
2534 for (node
= xmlFirstElementChild(session_node
); node
;
2535 node
= xmlNextElementSibling(node
)) {
2536 if (!name
&& !strcmp((const char *) node
->name
,
2537 config_element_name
)) {
2539 xmlChar
*node_content
= xmlNodeGetContent(node
);
2540 if (!node_content
) {
2541 ret
= -LTTNG_ERR_NOMEM
;
2545 name
= node_content
;
2546 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2547 config_element_domains
)) {
2549 domains_node
= node
;
2550 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2551 config_element_started
)) {
2553 xmlChar
*node_content
= xmlNodeGetContent(node
);
2554 if (!node_content
) {
2555 ret
= -LTTNG_ERR_NOMEM
;
2559 ret
= parse_bool(node_content
, &started
);
2562 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2565 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2566 config_element_output
)) {
2569 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2570 config_element_shared_memory_path
)) {
2571 /* shared memory path */
2572 xmlChar
*node_content
= xmlNodeGetContent(node
);
2573 if (!node_content
) {
2574 ret
= -LTTNG_ERR_NOMEM
;
2578 shm_path
= node_content
;
2581 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2582 * rotation_timer_interval. */
2583 xmlNodePtr attributes_child
=
2584 xmlFirstElementChild(node
);
2586 if (!strcmp((const char *) attributes_child
->name
,
2587 config_element_snapshot_mode
)) {
2589 xmlChar
*snapshot_mode_content
=
2590 xmlNodeGetContent(attributes_child
);
2591 if (!snapshot_mode_content
) {
2592 ret
= -LTTNG_ERR_NOMEM
;
2596 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2597 free(snapshot_mode_content
);
2599 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2602 } else if (!strcmp((const char *) attributes_child
->name
,
2603 config_element_live_timer_interval
)) {
2604 /* live_timer_interval */
2605 xmlChar
*timer_interval_content
=
2606 xmlNodeGetContent(attributes_child
);
2607 if (!timer_interval_content
) {
2608 ret
= -LTTNG_ERR_NOMEM
;
2612 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2613 free(timer_interval_content
);
2615 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2619 if (!strcmp((const char *) attributes_child
->name
,
2620 config_element_rotation_timer_interval
)) {
2621 /* rotation_timer_interval */
2622 xmlChar
*timer_interval_content
=
2623 xmlNodeGetContent(attributes_child
);
2624 if (!timer_interval_content
) {
2625 ret
= -LTTNG_ERR_NOMEM
;
2629 ret
= parse_uint(timer_interval_content
, &rotation_timer_interval
);
2630 free(timer_interval_content
);
2632 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2640 /* Mandatory attribute, as defined in the session XSD */
2641 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2645 if (session_name
&& strcmp((char *) name
, session_name
)) {
2646 /* This is not the session we are looking for */
2647 ret
= -LTTNG_ERR_NO_SESSION
;
2651 /* Init domains to create the session handles */
2652 for (node
= xmlFirstElementChild(domains_node
); node
;
2653 node
= xmlNextElementSibling(node
)) {
2654 struct lttng_domain
*domain
;
2656 domain
= zmalloc(sizeof(*domain
));
2658 ret
= -LTTNG_ERR_NOMEM
;
2662 ret
= init_domain(node
, domain
);
2664 goto domain_init_error
;
2667 switch (domain
->type
) {
2668 case LTTNG_DOMAIN_KERNEL
:
2669 if (kernel_domain
) {
2670 /* Same domain seen twice, invalid! */
2671 goto domain_init_error
;
2673 kernel_domain
= domain
;
2675 case LTTNG_DOMAIN_UST
:
2677 /* Same domain seen twice, invalid! */
2678 goto domain_init_error
;
2680 ust_domain
= domain
;
2682 case LTTNG_DOMAIN_JUL
:
2684 /* Same domain seen twice, invalid! */
2685 goto domain_init_error
;
2687 jul_domain
= domain
;
2689 case LTTNG_DOMAIN_LOG4J
:
2691 /* Same domain seen twice, invalid! */
2692 goto domain_init_error
;
2694 log4j_domain
= domain
;
2696 case LTTNG_DOMAIN_PYTHON
:
2697 if (python_domain
) {
2698 /* Same domain seen twice, invalid! */
2699 goto domain_init_error
;
2701 python_domain
= domain
;
2704 WARN("Invalid domain type");
2705 goto domain_init_error
;
2710 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2714 /* Apply overrides */
2716 if (overrides
->session_name
) {
2717 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2718 if (!name_override
) {
2719 ret
= -LTTNG_ERR_NOMEM
;
2723 /* Overrides the session name to the provided name */
2725 name
= name_override
;
2730 /* Destroy session if it exists */
2731 ret
= lttng_destroy_session((const char *) name
);
2732 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2733 ERR("Failed to destroy existing session.");
2738 /* Create session type depending on output type */
2739 if (snapshot_mode
&& snapshot_mode
!= -1) {
2740 ret
= create_snapshot_session((const char *) name
, output_node
,
2742 } else if (live_timer_interval
&&
2743 live_timer_interval
!= UINT64_MAX
) {
2744 ret
= create_session((const char *) name
, kernel_domain
,
2745 ust_domain
, jul_domain
, log4j_domain
,
2746 output_node
, live_timer_interval
, overrides
);
2748 /* regular session */
2749 ret
= create_session((const char *) name
, kernel_domain
,
2750 ust_domain
, jul_domain
, log4j_domain
,
2751 output_node
, UINT64_MAX
, overrides
);
2758 ret
= lttng_set_session_shm_path((const char *) name
,
2759 (const char *) shm_path
);
2765 for (node
= xmlFirstElementChild(domains_node
); node
;
2766 node
= xmlNextElementSibling(node
)) {
2767 ret
= process_domain_node(node
, (const char *) name
);
2773 if (rotation_timer_interval
) {
2774 struct lttng_rotation_schedule_attr
*rotation_attr
= lttng_rotation_schedule_attr_create();
2776 if (!rotation_attr
) {
2779 ret
= lttng_rotation_schedule_attr_set_session_name(rotation_attr
, (const char *) name
);
2781 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2784 lttng_rotation_schedule_attr_set_timer_period(rotation_attr
,
2785 rotation_timer_interval
);
2786 ret
= lttng_rotation_set_schedule(rotation_attr
);
2787 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2794 ret
= lttng_start_tracing((const char *) name
);
2802 ERR("Failed to load session %s: %s", (const char *) name
,
2803 lttng_strerror(ret
));
2804 lttng_destroy_session((const char *) name
);
2808 free(kernel_domain
);
2812 free(python_domain
);
2819 * Return 1 if the given path is readable by the current UID or 0 if not.
2820 * Return -1 if the path is EPERM.
2822 static int validate_file_read_creds(const char *path
)
2828 /* Can we read the file. */
2829 ret
= access(path
, R_OK
);
2833 if (errno
== EACCES
) {
2844 int load_session_from_file(const char *path
, const char *session_name
,
2845 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2846 const struct config_load_session_override_attr
*overrides
)
2848 int ret
, session_found
= !session_name
;
2849 xmlDocPtr doc
= NULL
;
2850 xmlNodePtr sessions_node
;
2851 xmlNodePtr session_node
;
2854 assert(validation_ctx
);
2856 ret
= validate_file_read_creds(path
);
2859 ret
= -LTTNG_ERR_EPERM
;
2861 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2866 doc
= xmlParseFile(path
);
2868 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2872 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2874 ERR("Session configuration file validation failed");
2875 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2879 sessions_node
= xmlDocGetRootElement(doc
);
2880 if (!sessions_node
) {
2884 for (session_node
= xmlFirstElementChild(sessions_node
);
2885 session_node
; session_node
=
2886 xmlNextElementSibling(session_node
)) {
2887 ret
= process_session_node(session_node
,
2888 session_name
, overwrite
, overrides
);
2889 if (session_name
&& ret
== 0) {
2890 /* Target session found and loaded */
2898 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2903 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2905 struct dirent
*alloc_dirent(const char *path
)
2909 struct dirent
*entry
;
2911 name_max
= pathconf(path
, _PC_NAME_MAX
);
2912 if (name_max
== -1) {
2913 name_max
= PATH_MAX
;
2915 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2916 entry
= zmalloc(len
);
2921 int load_session_from_path(const char *path
, const char *session_name
,
2922 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2923 const struct config_load_session_override_attr
*overrides
)
2925 int ret
, session_found
= !session_name
;
2926 DIR *directory
= NULL
;
2929 assert(validation_ctx
);
2931 directory
= opendir(path
);
2935 /* Try the file loading. */
2938 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2941 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2946 struct dirent
*entry
;
2947 struct dirent
*result
;
2948 char *file_path
= NULL
;
2949 size_t path_len
= strlen(path
);
2951 if (path_len
>= PATH_MAX
) {
2952 ret
= -LTTNG_ERR_INVALID
;
2956 entry
= alloc_dirent(path
);
2958 ret
= -LTTNG_ERR_NOMEM
;
2962 file_path
= zmalloc(PATH_MAX
);
2964 ret
= -LTTNG_ERR_NOMEM
;
2969 strncpy(file_path
, path
, path_len
);
2970 if (file_path
[path_len
- 1] != '/') {
2971 file_path
[path_len
++] = '/';
2975 /* Search for *.lttng files */
2976 while (!readdir_r(directory
, entry
, &result
) && result
) {
2977 size_t file_name_len
= strlen(result
->d_name
);
2979 if (file_name_len
<=
2980 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2984 if (path_len
+ file_name_len
>= PATH_MAX
) {
2988 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2989 result
->d_name
+ file_name_len
- sizeof(
2990 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2994 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2995 file_path
[path_len
+ file_name_len
] = '\0';
2997 ret
= load_session_from_file(file_path
, session_name
,
2998 validation_ctx
, overwrite
, overrides
);
2999 if (session_name
&& !ret
) {
3008 ret
= load_session_from_file(path
, session_name
,
3009 validation_ctx
, overwrite
, overrides
);
3019 if (closedir(directory
)) {
3024 if (session_found
&& !ret
) {
3032 * Validate that the given path's credentials and the current process have the
3033 * same UID. If so, return 1 else return 0 if it does NOT match.
3035 static int validate_path_creds(const char *path
)
3037 int ret
, uid
= getuid();
3046 ret
= stat(path
, &buf
);
3048 if (errno
!= ENOENT
) {
3054 if (buf
.st_uid
!= uid
) {
3065 int config_load_session(const char *path
, const char *session_name
,
3066 int overwrite
, unsigned int autoload
,
3067 const struct config_load_session_override_attr
*overrides
)
3070 bool session_loaded
= false;
3071 const char *path_ptr
= NULL
;
3072 struct session_config_validation_ctx validation_ctx
= { 0 };
3074 ret
= init_session_config_validation_ctx(&validation_ctx
);
3081 const char *sys_path
;
3084 home_path
= utils_get_home_dir();
3086 char path
[PATH_MAX
];
3089 * Try user session configuration path. Ignore error here so we can
3090 * continue loading the system wide sessions.
3093 ret
= snprintf(path
, sizeof(path
),
3094 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3095 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3097 PERROR("snprintf session autoload home config path");
3102 * Credentials are only validated for the autoload in order to
3103 * avoid any user session daemon to try to load kernel sessions
3104 * automatically and failing all the times.
3106 ret
= validate_path_creds(path
);
3111 ret
= snprintf(path
, sizeof(path
),
3112 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3114 PERROR("snprintf session home config path");
3120 ret
= load_session_from_path(path_ptr
, session_name
,
3121 &validation_ctx
, overwrite
, overrides
);
3122 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3126 * Continue even if the session was found since we have to try
3127 * the system wide sessions.
3129 session_loaded
= true;
3133 /* Reset path pointer for the system wide dir. */
3136 /* Try system wide configuration directory. */
3138 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3139 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3140 ret
= validate_path_creds(sys_path
);
3142 path_ptr
= sys_path
;
3145 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3146 path_ptr
= sys_path
;
3150 ret
= load_session_from_path(path_ptr
, session_name
,
3151 &validation_ctx
, overwrite
, overrides
);
3153 session_loaded
= true;
3157 ret
= access(path
, F_OK
);
3162 ret
= -LTTNG_ERR_INVALID
;
3163 WARN("Session configuration path does not exist.");
3166 ret
= -LTTNG_ERR_EPERM
;
3169 ret
= -LTTNG_ERR_UNK
;
3175 ret
= load_session_from_path(path
, session_name
,
3176 &validation_ctx
, overwrite
, overrides
);
3179 fini_session_config_validation_ctx(&validation_ctx
);
3180 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3182 * Don't report an error if no sessions are found when called
3183 * without a session_name or a search path.
3188 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3189 /* A matching session was found in one of the search paths. */
3196 void __attribute__((destructor
)) session_config_exit(void)