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_schedule
= "rotation_schedule";
138 const char * const config_domain_type_kernel
= "KERNEL";
139 const char * const config_domain_type_ust
= "UST";
140 const char * const config_domain_type_jul
= "JUL";
141 const char * const config_domain_type_log4j
= "LOG4J";
142 const char * const config_domain_type_python
= "PYTHON";
144 const char * const config_buffer_type_per_pid
= "PER_PID";
145 const char * const config_buffer_type_per_uid
= "PER_UID";
146 const char * const config_buffer_type_global
= "GLOBAL";
148 const char * const config_overwrite_mode_discard
= "DISCARD";
149 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
151 const char * const config_output_type_splice
= "SPLICE";
152 const char * const config_output_type_mmap
= "MMAP";
154 const char * const config_loglevel_type_all
= "ALL";
155 const char * const config_loglevel_type_range
= "RANGE";
156 const char * const config_loglevel_type_single
= "SINGLE";
158 const char * const config_event_type_all
= "ALL";
159 const char * const config_event_type_tracepoint
= "TRACEPOINT";
160 const char * const config_event_type_probe
= "PROBE";
161 const char * const config_event_type_function
= "FUNCTION";
162 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
163 const char * const config_event_type_noop
= "NOOP";
164 const char * const config_event_type_syscall
= "SYSCALL";
165 const char * const config_event_type_kprobe
= "KPROBE";
166 const char * const config_event_type_kretprobe
= "KRETPROBE";
168 const char * const config_event_context_pid
= "PID";
169 const char * const config_event_context_procname
= "PROCNAME";
170 const char * const config_event_context_prio
= "PRIO";
171 const char * const config_event_context_nice
= "NICE";
172 const char * const config_event_context_vpid
= "VPID";
173 const char * const config_event_context_tid
= "TID";
174 const char * const config_event_context_vtid
= "VTID";
175 const char * const config_event_context_ppid
= "PPID";
176 const char * const config_event_context_vppid
= "VPPID";
177 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
178 const char * const config_event_context_hostname
= "HOSTNAME";
179 const char * const config_event_context_ip
= "IP";
180 const char * const config_event_context_perf_thread_counter
= "PERF_THREAD_COUNTER";
181 LTTNG_HIDDEN
const char * const config_event_context_app
= "APP";
182 LTTNG_HIDDEN
const char * const config_event_context_interruptible
= "INTERRUPTIBLE";
183 LTTNG_HIDDEN
const char * const config_event_context_preemptible
= "PREEMPTIBLE";
184 LTTNG_HIDDEN
const char * const config_event_context_need_reschedule
= "NEED_RESCHEDULE";
185 LTTNG_HIDDEN
const char * const config_event_context_migratable
= "MIGRATABLE";
187 /* Deprecated symbols */
188 const char * const config_element_perf
;
190 enum process_event_node_phase
{
195 struct consumer_output
{
202 static int config_entry_handler_filter(struct handler_filter_args
*args
,
203 const char *section
, const char *name
, const char *value
)
206 struct config_entry entry
= { section
, name
, value
};
210 if (!section
|| !name
|| !value
) {
216 if (strcmp(args
->section
, section
)) {
221 ret
= args
->handler(&entry
, args
->user_data
);
227 int config_get_section_entries(const char *override_path
, const char *section
,
228 config_entry_handler_cb handler
, void *user_data
)
232 FILE *config_file
= NULL
;
233 struct handler_filter_args filter
= { section
, handler
, user_data
};
235 /* First, try system-wide conf. file. */
236 path
= DEFAULT_DAEMON_SYSTEM_CONFIGPATH
;
238 config_file
= fopen(path
, "r");
240 DBG("Loading daemon conf file at %s", path
);
242 * Return value is not very important here since error or not, we
243 * continue and try the next possible conf. file.
245 (void) ini_parse_file(config_file
,
246 (ini_entry_handler
) config_entry_handler_filter
,
251 /* Second is the user local configuration. */
252 path
= utils_get_home_dir();
254 char fullpath
[PATH_MAX
];
256 ret
= snprintf(fullpath
, sizeof(fullpath
),
257 DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
259 PERROR("snprintf user conf. path");
263 config_file
= fopen(fullpath
, "r");
265 DBG("Loading daemon user conf file at %s", path
);
267 * Return value is not very important here since error or not, we
268 * continue and try the next possible conf. file.
270 (void) ini_parse_file(config_file
,
271 (ini_entry_handler
) config_entry_handler_filter
,
277 /* Final path is the one that the user might have provided. */
279 config_file
= fopen(override_path
, "r");
281 DBG("Loading daemon command line conf file at %s", override_path
);
282 (void) ini_parse_file(config_file
,
283 (ini_entry_handler
) config_entry_handler_filter
,
287 ERR("Failed to open daemon configuration file at %s",
294 /* Everything went well. */
302 int config_parse_value(const char *value
)
305 char *endptr
, *lower_str
;
315 v
= strtoul(value
, &endptr
, 10);
316 if (endptr
!= value
) {
321 lower_str
= zmalloc(len
+ 1);
328 for (i
= 0; i
< len
; i
++) {
329 lower_str
[i
] = tolower(value
[i
]);
332 if (!strcmp(lower_str
, config_str_yes
) ||
333 !strcmp(lower_str
, config_str_true
) ||
334 !strcmp(lower_str
, config_str_on
)) {
336 } else if (!strcmp(lower_str
, config_str_no
) ||
337 !strcmp(lower_str
, config_str_false
) ||
338 !strcmp(lower_str
, config_str_off
)) {
350 * Returns a xmlChar string which must be released using xmlFree().
352 static xmlChar
*encode_string(const char *in_str
)
354 xmlChar
*out_str
= NULL
;
355 xmlCharEncodingHandlerPtr handler
;
356 int out_len
, ret
, in_len
;
360 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
362 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
366 in_len
= strlen(in_str
);
368 * Add 1 byte for the NULL terminted character. The factor 4 here is
369 * used because UTF-8 characters can take up to 4 bytes.
371 out_len
= (in_len
* 4) + 1;
372 out_str
= xmlMalloc(out_len
);
377 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
384 /* out_len is now the size of out_str */
385 out_str
[out_len
] = '\0';
391 struct config_writer
*config_writer_create(int fd_output
, int indent
)
394 struct config_writer
*writer
;
395 xmlOutputBufferPtr buffer
;
397 writer
= zmalloc(sizeof(struct config_writer
));
399 PERROR("zmalloc config_writer_create");
403 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
408 writer
->writer
= xmlNewTextWriter(buffer
);
409 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
410 config_xml_encoding
, NULL
);
415 ret
= xmlTextWriterSetIndentString(writer
->writer
,
416 BAD_CAST config_xml_indent_string
);
421 ret
= xmlTextWriterSetIndent(writer
->writer
, indent
);
429 config_writer_destroy(writer
);
434 int config_writer_destroy(struct config_writer
*writer
)
443 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
444 WARN("Could not close XML document");
448 if (writer
->writer
) {
449 xmlFreeTextWriter(writer
->writer
);
458 int config_writer_open_element(struct config_writer
*writer
,
459 const char *element_name
)
462 xmlChar
*encoded_element_name
;
464 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
469 encoded_element_name
= encode_string(element_name
);
470 if (!encoded_element_name
) {
475 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
476 xmlFree(encoded_element_name
);
478 return ret
>= 0 ? 0 : ret
;
482 int config_writer_write_attribute(struct config_writer
*writer
,
483 const char *name
, const char *value
)
486 xmlChar
*encoded_name
= NULL
;
487 xmlChar
*encoded_value
= NULL
;
489 if (!writer
|| !writer
->writer
|| !name
|| !name
[0]) {
494 encoded_name
= encode_string(name
);
500 encoded_value
= encode_string(value
);
501 if (!encoded_value
) {
506 ret
= xmlTextWriterWriteAttribute(writer
->writer
, encoded_name
,
509 xmlFree(encoded_name
);
510 xmlFree(encoded_value
);
511 return ret
>= 0 ? 0 : ret
;
515 int config_writer_close_element(struct config_writer
*writer
)
519 if (!writer
|| !writer
->writer
) {
524 ret
= xmlTextWriterEndElement(writer
->writer
);
526 return ret
>= 0 ? 0 : ret
;
530 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
531 const char *element_name
, uint64_t value
)
534 xmlChar
*encoded_element_name
;
536 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
541 encoded_element_name
= encode_string(element_name
);
542 if (!encoded_element_name
) {
547 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
548 encoded_element_name
, "%" PRIu64
, value
);
549 xmlFree(encoded_element_name
);
551 return ret
>= 0 ? 0 : ret
;
555 int config_writer_write_element_signed_int(struct config_writer
*writer
,
556 const char *element_name
, int64_t value
)
559 xmlChar
*encoded_element_name
;
561 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
566 encoded_element_name
= encode_string(element_name
);
567 if (!encoded_element_name
) {
572 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
573 encoded_element_name
, "%" PRIi64
, value
);
574 xmlFree(encoded_element_name
);
576 return ret
>= 0 ? 0 : ret
;
580 int config_writer_write_element_bool(struct config_writer
*writer
,
581 const char *element_name
, int value
)
583 return config_writer_write_element_string(writer
, element_name
,
584 value
? config_xml_true
: config_xml_false
);
588 int config_writer_write_element_string(struct config_writer
*writer
,
589 const char *element_name
, const char *value
)
592 xmlChar
*encoded_element_name
= NULL
;
593 xmlChar
*encoded_value
= NULL
;
595 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
601 encoded_element_name
= encode_string(element_name
);
602 if (!encoded_element_name
) {
607 encoded_value
= encode_string(value
);
608 if (!encoded_value
) {
613 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
616 xmlFree(encoded_element_name
);
617 xmlFree(encoded_value
);
618 return ret
>= 0 ? 0 : ret
;
622 void xml_error_handler(void *ctx
, const char *format
, ...)
628 va_start(args
, format
);
629 ret
= vasprintf(&errMsg
, format
, args
);
632 ERR("String allocation failed in xml error handler");
636 fprintf(stderr
, "XML Error: %s", errMsg
);
641 void fini_session_config_validation_ctx(
642 struct session_config_validation_ctx
*ctx
)
644 if (ctx
->parser_ctx
) {
645 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
649 xmlSchemaFree(ctx
->schema
);
652 if (ctx
->schema_validation_ctx
) {
653 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
656 memset(ctx
, 0, sizeof(struct session_config_validation_ctx
));
660 char *get_session_config_xsd_path()
663 const char *base_path
= lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV
);
664 size_t base_path_len
;
668 base_path
= DEFAULT_SESSION_CONFIG_XSD_PATH
;
671 base_path_len
= strlen(base_path
);
672 max_path_len
= base_path_len
+
673 sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME
) + 1;
674 xsd_path
= zmalloc(max_path_len
);
679 strncpy(xsd_path
, base_path
, max_path_len
);
680 if (xsd_path
[base_path_len
- 1] != '/') {
681 xsd_path
[base_path_len
++] = '/';
684 strncpy(xsd_path
+ base_path_len
, DEFAULT_SESSION_CONFIG_XSD_FILENAME
,
685 max_path_len
- base_path_len
);
691 int init_session_config_validation_ctx(
692 struct session_config_validation_ctx
*ctx
)
695 char *xsd_path
= get_session_config_xsd_path();
698 ret
= -LTTNG_ERR_NOMEM
;
702 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
703 if (!ctx
->parser_ctx
) {
704 ERR("XSD parser context creation failed");
705 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
708 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
709 xml_error_handler
, NULL
);
711 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
713 ERR("XSD parsing failed");
714 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
718 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
719 if (!ctx
->schema_validation_ctx
) {
720 ERR("XSD validation context creation failed");
721 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
725 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
726 xml_error_handler
, NULL
);
731 fini_session_config_validation_ctx(ctx
);
739 int parse_uint(xmlChar
*str
, uint64_t *val
)
749 *val
= strtoull((const char *) str
, &endptr
, 10);
750 if (!endptr
|| *endptr
) {
761 int parse_int(xmlChar
*str
, int64_t *val
)
771 *val
= strtoll((const char *) str
, &endptr
, 10);
772 if (!endptr
|| *endptr
) {
783 int parse_bool(xmlChar
*str
, int *val
)
792 if (!strcmp((const char *) str
, config_xml_true
)) {
794 } else if (!strcmp((const char *) str
, config_xml_false
)) {
797 WARN("Invalid boolean value encoutered (%s).",
806 int get_domain_type(xmlChar
*domain
)
814 if (!strcmp((char *) domain
, config_domain_type_kernel
)) {
815 ret
= LTTNG_DOMAIN_KERNEL
;
816 } else if (!strcmp((char *) domain
, config_domain_type_ust
)) {
817 ret
= LTTNG_DOMAIN_UST
;
818 } else if (!strcmp((char *) domain
, config_domain_type_jul
)) {
819 ret
= LTTNG_DOMAIN_JUL
;
820 } else if (!strcmp((char *) domain
, config_domain_type_log4j
)) {
821 ret
= LTTNG_DOMAIN_LOG4J
;
822 } else if (!strcmp((char *) domain
, config_domain_type_python
)) {
823 ret
= LTTNG_DOMAIN_PYTHON
;
834 int get_buffer_type(xmlChar
*buffer_type
)
842 if (!strcmp((char *) buffer_type
, config_buffer_type_global
)) {
843 ret
= LTTNG_BUFFER_GLOBAL
;
844 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_uid
)) {
845 ret
= LTTNG_BUFFER_PER_UID
;
846 } else if (!strcmp((char *) buffer_type
, config_buffer_type_per_pid
)) {
847 ret
= LTTNG_BUFFER_PER_PID
;
858 int get_overwrite_mode(xmlChar
*overwrite_mode
)
862 if (!overwrite_mode
) {
866 if (!strcmp((char *) overwrite_mode
, config_overwrite_mode_overwrite
)) {
868 } else if (!strcmp((char *) overwrite_mode
,
869 config_overwrite_mode_discard
)) {
881 int get_output_type(xmlChar
*output_type
)
889 if (!strcmp((char *) output_type
, config_output_type_mmap
)) {
890 ret
= LTTNG_EVENT_MMAP
;
891 } else if (!strcmp((char *) output_type
, config_output_type_splice
)) {
892 ret
= LTTNG_EVENT_SPLICE
;
903 int get_event_type(xmlChar
*event_type
)
911 if (!strcmp((char *) event_type
, config_event_type_all
)) {
912 ret
= LTTNG_EVENT_ALL
;
913 } else if (!strcmp((char *) event_type
, config_event_type_tracepoint
)) {
914 ret
= LTTNG_EVENT_TRACEPOINT
;
915 } else if (!strcmp((char *) event_type
, config_event_type_probe
)) {
916 ret
= LTTNG_EVENT_PROBE
;
917 } else if (!strcmp((char *) event_type
, config_event_type_function
)) {
918 ret
= LTTNG_EVENT_FUNCTION
;
919 } else if (!strcmp((char *) event_type
,
920 config_event_type_function_entry
)) {
921 ret
= LTTNG_EVENT_FUNCTION_ENTRY
;
922 } else if (!strcmp((char *) event_type
, config_event_type_noop
)) {
923 ret
= LTTNG_EVENT_NOOP
;
924 } else if (!strcmp((char *) event_type
, config_event_type_syscall
)) {
925 ret
= LTTNG_EVENT_SYSCALL
;
936 int get_loglevel_type(xmlChar
*loglevel_type
)
940 if (!loglevel_type
) {
944 if (!strcmp((char *) loglevel_type
, config_loglevel_type_all
)) {
945 ret
= LTTNG_EVENT_LOGLEVEL_ALL
;
946 } else if (!strcmp((char *) loglevel_type
,
947 config_loglevel_type_range
)) {
948 ret
= LTTNG_EVENT_LOGLEVEL_RANGE
;
949 } else if (!strcmp((char *) loglevel_type
,
950 config_loglevel_type_single
)) {
951 ret
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
962 * Return the context type or -1 on error.
965 int get_context_type(xmlChar
*context_type
)
973 if (!strcmp((char *) context_type
, config_event_context_pid
)) {
974 ret
= LTTNG_EVENT_CONTEXT_PID
;
975 } else if (!strcmp((char *) context_type
,
976 config_event_context_procname
)) {
977 ret
= LTTNG_EVENT_CONTEXT_PROCNAME
;
978 } else if (!strcmp((char *) context_type
,
979 config_event_context_prio
)) {
980 ret
= LTTNG_EVENT_CONTEXT_PRIO
;
981 } else if (!strcmp((char *) context_type
,
982 config_event_context_nice
)) {
983 ret
= LTTNG_EVENT_CONTEXT_NICE
;
984 } else if (!strcmp((char *) context_type
,
985 config_event_context_vpid
)) {
986 ret
= LTTNG_EVENT_CONTEXT_VPID
;
987 } else if (!strcmp((char *) context_type
,
988 config_event_context_tid
)) {
989 ret
= LTTNG_EVENT_CONTEXT_TID
;
990 } else if (!strcmp((char *) context_type
,
991 config_event_context_vtid
)) {
992 ret
= LTTNG_EVENT_CONTEXT_VTID
;
993 } else if (!strcmp((char *) context_type
,
994 config_event_context_ppid
)) {
995 ret
= LTTNG_EVENT_CONTEXT_PPID
;
996 } else if (!strcmp((char *) context_type
,
997 config_event_context_vppid
)) {
998 ret
= LTTNG_EVENT_CONTEXT_VPPID
;
999 } else if (!strcmp((char *) context_type
,
1000 config_event_context_pthread_id
)) {
1001 ret
= LTTNG_EVENT_CONTEXT_PTHREAD_ID
;
1002 } else if (!strcmp((char *) context_type
,
1003 config_event_context_hostname
)) {
1004 ret
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1005 } else if (!strcmp((char *) context_type
,
1006 config_event_context_ip
)) {
1007 ret
= LTTNG_EVENT_CONTEXT_IP
;
1008 } else if (!strcmp((char *) context_type
,
1009 config_event_context_interruptible
)) {
1010 ret
= LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
;
1011 } else if (!strcmp((char *) context_type
,
1012 config_event_context_preemptible
)) {
1013 ret
= LTTNG_EVENT_CONTEXT_PREEMPTIBLE
;
1014 } else if (!strcmp((char *) context_type
,
1015 config_event_context_need_reschedule
)) {
1016 ret
= LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
;
1017 } else if (!strcmp((char *) context_type
,
1018 config_event_context_migratable
)) {
1019 ret
= LTTNG_EVENT_CONTEXT_MIGRATABLE
;
1030 int init_domain(xmlNodePtr domain_node
, struct lttng_domain
*domain
)
1035 for (node
= xmlFirstElementChild(domain_node
); node
;
1036 node
= xmlNextElementSibling(node
)) {
1037 if (!strcmp((const char *) node
->name
, config_element_type
)) {
1039 xmlChar
*node_content
= xmlNodeGetContent(node
);
1040 if (!node_content
) {
1041 ret
= -LTTNG_ERR_NOMEM
;
1045 ret
= get_domain_type(node_content
);
1048 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1053 } else if (!strcmp((const char *) node
->name
,
1054 config_element_buffer_type
)) {
1056 xmlChar
*node_content
= xmlNodeGetContent(node
);
1057 if (!node_content
) {
1058 ret
= -LTTNG_ERR_NOMEM
;
1062 ret
= get_buffer_type(node_content
);
1065 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1069 domain
->buf_type
= ret
;
1078 int get_net_output_uris(xmlNodePtr net_output_node
, char **control_uri
,
1083 for (node
= xmlFirstElementChild(net_output_node
); node
;
1084 node
= xmlNextElementSibling(node
)) {
1085 if (!strcmp((const char *) node
->name
, config_element_control_uri
)) {
1087 *control_uri
= (char *) xmlNodeGetContent(node
);
1088 if (!*control_uri
) {
1093 *data_uri
= (char *) xmlNodeGetContent(node
);
1100 return *control_uri
|| *data_uri
? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1104 int process_consumer_output(xmlNodePtr consumer_output_node
,
1105 struct consumer_output
*output
)
1112 for (node
= xmlFirstElementChild(consumer_output_node
); node
;
1113 node
= xmlNextElementSibling(node
)) {
1114 if (!strcmp((const char *) node
->name
, config_element_enabled
)) {
1115 xmlChar
*enabled_str
= xmlNodeGetContent(node
);
1119 ret
= -LTTNG_ERR_NOMEM
;
1123 ret
= parse_bool(enabled_str
, &output
->enabled
);
1129 xmlNodePtr output_type_node
;
1132 output_type_node
= xmlFirstElementChild(node
);
1133 if (!output_type_node
) {
1134 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1138 if (!strcmp((const char *) output_type_node
->name
,
1139 config_element_path
)) {
1141 output
->path
= (char *) xmlNodeGetContent(output_type_node
);
1142 if (!output
->path
) {
1143 ret
= -LTTNG_ERR_NOMEM
;
1148 ret
= get_net_output_uris(output_type_node
,
1149 &output
->control_uri
, &output
->data_uri
);
1161 free(output
->control_uri
);
1162 free(output
->data_uri
);
1163 memset(output
, 0, sizeof(struct consumer_output
));
1169 int create_session_net_output(const char *name
, const char *control_uri
,
1170 const char *data_uri
)
1173 struct lttng_handle
*handle
;
1174 const char *uri
= NULL
;
1178 handle
= lttng_create_handle(name
, NULL
);
1180 ret
= -LTTNG_ERR_NOMEM
;
1184 if (!control_uri
|| !data_uri
) {
1185 uri
= control_uri
? control_uri
: data_uri
;
1190 ret
= lttng_set_consumer_url(handle
, control_uri
, data_uri
);
1191 lttng_destroy_handle(handle
);
1197 int create_snapshot_session(const char *session_name
, xmlNodePtr output_node
,
1198 const struct config_load_session_override_attr
*overrides
)
1201 xmlNodePtr node
= NULL
;
1202 xmlNodePtr snapshot_output_list_node
;
1203 xmlNodePtr snapshot_output_node
;
1205 assert(session_name
);
1207 ret
= lttng_create_session_snapshot(session_name
, NULL
);
1216 snapshot_output_list_node
= xmlFirstElementChild(output_node
);
1218 /* Parse and create snapshot outputs */
1220 for (snapshot_output_node
=
1221 xmlFirstElementChild(snapshot_output_list_node
);
1222 snapshot_output_node
; snapshot_output_node
=
1223 xmlNextElementSibling(snapshot_output_node
)) {
1225 uint64_t max_size
= UINT64_MAX
;
1226 struct consumer_output output
= { 0 };
1227 struct lttng_snapshot_output
*snapshot_output
= NULL
;
1228 const char *control_uri
= NULL
;
1229 const char *data_uri
= NULL
;
1230 const char *path
= NULL
;
1232 for (node
= xmlFirstElementChild(snapshot_output_node
); node
;
1233 node
= xmlNextElementSibling(node
)) {
1234 if (!strcmp((const char *) node
->name
,
1235 config_element_name
)) {
1237 name
= (char *) xmlNodeGetContent(node
);
1239 ret
= -LTTNG_ERR_NOMEM
;
1240 goto error_snapshot_output
;
1242 } else if (!strcmp((const char *) node
->name
,
1243 config_element_max_size
)) {
1244 xmlChar
*content
= xmlNodeGetContent(node
);
1248 ret
= -LTTNG_ERR_NOMEM
;
1249 goto error_snapshot_output
;
1251 ret
= parse_uint(content
, &max_size
);
1254 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1255 goto error_snapshot_output
;
1258 /* consumer_output */
1259 ret
= process_consumer_output(node
, &output
);
1261 goto error_snapshot_output
;
1266 control_uri
= output
.control_uri
;
1267 data_uri
= output
.data_uri
;
1271 if (overrides
->path_url
) {
1272 path
= overrides
->path_url
;
1273 /* Control/data_uri are null */
1277 if (overrides
->ctrl_url
) {
1278 control_uri
= overrides
->ctrl_url
;
1282 if (overrides
->data_url
) {
1283 data_uri
= overrides
->data_url
;
1290 snapshot_output
= lttng_snapshot_output_create();
1291 if (!snapshot_output
) {
1292 ret
= -LTTNG_ERR_NOMEM
;
1293 goto error_snapshot_output
;
1296 ret
= lttng_snapshot_output_set_name(name
, snapshot_output
);
1298 goto error_snapshot_output
;
1301 ret
= lttng_snapshot_output_set_size(max_size
, snapshot_output
);
1303 goto error_snapshot_output
;
1307 ret
= lttng_snapshot_output_set_ctrl_url(path
,
1310 goto error_snapshot_output
;
1314 ret
= lttng_snapshot_output_set_ctrl_url(control_uri
,
1317 goto error_snapshot_output
;
1322 ret
= lttng_snapshot_output_set_data_url(data_uri
,
1325 goto error_snapshot_output
;
1330 ret
= lttng_snapshot_add_output(session_name
, snapshot_output
);
1331 error_snapshot_output
:
1334 free(output
.control_uri
);
1335 free(output
.data_uri
);
1336 lttng_snapshot_output_destroy(snapshot_output
);
1346 int create_session(const char *name
,
1347 struct lttng_domain
*kernel_domain
,
1348 struct lttng_domain
*ust_domain
,
1349 struct lttng_domain
*jul_domain
,
1350 struct lttng_domain
*log4j_domain
,
1351 xmlNodePtr output_node
,
1352 uint64_t live_timer_interval
,
1353 const struct config_load_session_override_attr
*overrides
)
1356 struct consumer_output output
= { 0 };
1357 xmlNodePtr consumer_output_node
;
1358 const char *control_uri
= NULL
;
1359 const char *data_uri
= NULL
;
1360 const char *path
= NULL
;
1365 consumer_output_node
= xmlFirstElementChild(output_node
);
1366 if (!consumer_output_node
) {
1367 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1371 if (strcmp((const char *) consumer_output_node
->name
,
1372 config_element_consumer_output
)) {
1373 WARN("Invalid output type, expected %s node",
1374 config_element_consumer_output
);
1375 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1379 ret
= process_consumer_output(consumer_output_node
, &output
);
1385 control_uri
= output
.control_uri
;
1386 data_uri
= output
.data_uri
;
1389 /* Check for override and apply them */
1391 if (overrides
->path_url
) {
1392 path
= overrides
->path_url
;
1393 /* control/data_uri are null */;
1397 if (overrides
->ctrl_url
) {
1398 control_uri
= overrides
->ctrl_url
;
1402 if (overrides
->data_url
) {
1403 data_uri
= overrides
->data_url
;
1411 if (live_timer_interval
!= UINT64_MAX
&& !control_uri
&& !data_uri
) {
1412 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1416 if (control_uri
|| data_uri
) {
1417 /* network destination */
1418 if (live_timer_interval
&& live_timer_interval
!= UINT64_MAX
) {
1420 * URLs are provided for sure since the test above make sure that
1421 * with a live timer the data and control URIs are provided. So,
1422 * NULL is passed here and will be set right after.
1424 ret
= lttng_create_session_live(name
, NULL
, live_timer_interval
);
1426 ret
= lttng_create_session(name
, NULL
);
1432 ret
= create_session_net_output(name
, control_uri
, data_uri
);
1438 /* either local output or no output */
1439 ret
= lttng_create_session(name
, path
);
1446 free(output
.control_uri
);
1447 free(output
.data_uri
);
1451 int process_probe_attribute_node(xmlNodePtr probe_attribute_node
,
1452 struct lttng_event_probe_attr
*attr
)
1456 assert(probe_attribute_node
);
1459 if (!strcmp((const char *) probe_attribute_node
->name
,
1460 config_element_address
)) {
1465 content
= xmlNodeGetContent(probe_attribute_node
);
1467 ret
= -LTTNG_ERR_NOMEM
;
1471 ret
= parse_uint(content
, &addr
);
1474 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1479 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1480 config_element_offset
)) {
1482 uint64_t offset
= 0;
1485 content
= xmlNodeGetContent(probe_attribute_node
);
1487 ret
= -LTTNG_ERR_NOMEM
;
1491 ret
= parse_uint(content
, &offset
);
1494 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1498 attr
->offset
= offset
;
1499 } else if (!strcmp((const char *) probe_attribute_node
->name
,
1500 config_element_symbol_name
)) {
1505 content
= xmlNodeGetContent(probe_attribute_node
);
1507 ret
= -LTTNG_ERR_NOMEM
;
1511 name_len
= strlen((char *) content
);
1512 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1513 WARN("symbol_name too long.");
1514 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1519 strncpy(attr
->symbol_name
, (const char *) content
, name_len
);
1528 int process_event_node(xmlNodePtr event_node
, struct lttng_handle
*handle
,
1529 const char *channel_name
, const enum process_event_node_phase phase
)
1533 struct lttng_event event
;
1534 char **exclusions
= NULL
;
1535 unsigned long exclusion_count
= 0;
1536 char *filter_expression
= NULL
;
1540 assert(channel_name
);
1542 memset(&event
, 0, sizeof(event
));
1544 /* Initialize default log level which varies by domain */
1545 switch (handle
->domain
.type
)
1547 case LTTNG_DOMAIN_JUL
:
1548 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1550 case LTTNG_DOMAIN_LOG4J
:
1551 event
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1553 case LTTNG_DOMAIN_PYTHON
:
1554 event
.loglevel
= LTTNG_LOGLEVEL_PYTHON_DEBUG
;
1556 case LTTNG_DOMAIN_UST
:
1557 case LTTNG_DOMAIN_KERNEL
:
1558 event
.loglevel
= LTTNG_LOGLEVEL_DEBUG
;
1564 for (node
= xmlFirstElementChild(event_node
); node
;
1565 node
= xmlNextElementSibling(node
)) {
1566 if (!strcmp((const char *) node
->name
, config_element_name
)) {
1571 content
= xmlNodeGetContent(node
);
1573 ret
= -LTTNG_ERR_NOMEM
;
1577 name_len
= strlen((char *) content
);
1578 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1579 WARN("Channel name too long.");
1580 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1585 strncpy(event
.name
, (const char *) content
, name_len
);
1587 } else if (!strcmp((const char *) node
->name
,
1588 config_element_enabled
)) {
1589 xmlChar
*content
= xmlNodeGetContent(node
);
1593 ret
= -LTTNG_ERR_NOMEM
;
1597 ret
= parse_bool(content
, &event
.enabled
);
1600 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1603 } else if (!strcmp((const char *) node
->name
,
1604 config_element_type
)) {
1605 xmlChar
*content
= xmlNodeGetContent(node
);
1609 ret
= -LTTNG_ERR_NOMEM
;
1613 ret
= get_event_type(content
);
1616 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1621 } else if (!strcmp((const char *) node
->name
,
1622 config_element_loglevel_type
)) {
1623 xmlChar
*content
= xmlNodeGetContent(node
);
1627 ret
= -LTTNG_ERR_NOMEM
;
1631 ret
= get_loglevel_type(content
);
1634 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1638 event
.loglevel_type
= ret
;
1639 } else if (!strcmp((const char *) node
->name
,
1640 config_element_loglevel
)) {
1642 int64_t loglevel
= 0;
1645 content
= xmlNodeGetContent(node
);
1647 ret
= -LTTNG_ERR_NOMEM
;
1651 ret
= parse_int(content
, &loglevel
);
1654 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1658 if (loglevel
> INT_MAX
|| loglevel
< INT_MIN
) {
1659 WARN("loglevel out of range.");
1660 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1664 event
.loglevel
= loglevel
;
1665 } else if (!strcmp((const char *) node
->name
,
1666 config_element_filter
)) {
1668 xmlNodeGetContent(node
);
1672 ret
= -LTTNG_ERR_NOMEM
;
1676 free(filter_expression
);
1677 filter_expression
= strdup((char *) content
);
1679 if (!filter_expression
) {
1680 ret
= -LTTNG_ERR_NOMEM
;
1683 } else if (!strcmp((const char *) node
->name
,
1684 config_element_exclusions
)) {
1685 xmlNodePtr exclusion_node
;
1686 int exclusion_index
= 0;
1691 * Exclusions has already been initialized,
1694 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1698 exclusion_count
= xmlChildElementCount(node
);
1699 if (!exclusion_count
) {
1703 exclusions
= zmalloc(exclusion_count
* sizeof(char *));
1705 exclusion_count
= 0;
1706 ret
= -LTTNG_ERR_NOMEM
;
1710 for (exclusion_node
= xmlFirstElementChild(node
); exclusion_node
;
1711 exclusion_node
= xmlNextElementSibling(exclusion_node
)) {
1713 xmlNodeGetContent(exclusion_node
);
1716 ret
= -LTTNG_ERR_NOMEM
;
1720 exclusions
[exclusion_index
] = strdup((const char *) content
);
1722 if (!exclusions
[exclusion_index
]) {
1723 ret
= -LTTNG_ERR_NOMEM
;
1729 event
.exclusion
= 1;
1730 } else if (!strcmp((const char *) node
->name
,
1731 config_element_attributes
)) {
1732 xmlNodePtr attribute_node
= xmlFirstElementChild(node
);
1735 if (!attribute_node
) {
1736 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1740 if (!strcmp((const char *) node
->name
,
1741 config_element_probe_attributes
)) {
1742 xmlNodePtr probe_attribute_node
;
1744 /* probe_attributes */
1745 for (probe_attribute_node
=
1746 xmlFirstElementChild(attribute_node
); probe_attribute_node
;
1747 probe_attribute_node
= xmlNextElementSibling(
1748 probe_attribute_node
)) {
1750 ret
= process_probe_attribute_node(probe_attribute_node
,
1759 xmlNodePtr symbol_node
= xmlFirstElementChild(attribute_node
);
1761 /* function_attributes */
1762 content
= xmlNodeGetContent(symbol_node
);
1764 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1768 sym_len
= strlen((char *) content
);
1769 if (sym_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1770 WARN("Function name too long.");
1771 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1776 strncpy(event
.attr
.ftrace
.symbol_name
, (char *) content
,
1783 if ((event
.enabled
&& phase
== ENABLE
) || phase
== CREATION
) {
1784 ret
= lttng_enable_event_with_exclusions(handle
, &event
, channel_name
,
1785 filter_expression
, exclusion_count
, exclusions
);
1787 WARN("Enabling event (name:%s) on load failed.", event
.name
);
1788 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1793 for (i
= 0; i
< exclusion_count
; i
++) {
1794 free(exclusions
[i
]);
1798 free(filter_expression
);
1803 int process_events_node(xmlNodePtr events_node
, struct lttng_handle
*handle
,
1804 const char *channel_name
)
1807 struct lttng_event event
;
1810 assert(events_node
);
1812 assert(channel_name
);
1814 for (node
= xmlFirstElementChild(events_node
); node
;
1815 node
= xmlNextElementSibling(node
)) {
1816 ret
= process_event_node(node
, handle
, channel_name
, CREATION
);
1823 * Disable all events to enable only the necessary events.
1824 * Limitations regarding lttng_disable_events and tuple descriptor
1825 * force this approach.
1827 memset(&event
, 0, sizeof(event
));
1828 event
.loglevel
= -1;
1829 event
.type
= LTTNG_EVENT_ALL
;
1830 ret
= lttng_disable_event_ext(handle
, &event
, channel_name
, NULL
);
1835 for (node
= xmlFirstElementChild(events_node
); node
;
1836 node
= xmlNextElementSibling(node
)) {
1837 ret
= process_event_node(node
, handle
, channel_name
, ENABLE
);
1848 int process_channel_attr_node(xmlNodePtr attr_node
,
1849 struct lttng_channel
*channel
, xmlNodePtr
*contexts_node
,
1850 xmlNodePtr
*events_node
)
1856 assert(contexts_node
);
1857 assert(events_node
);
1859 if (!strcmp((const char *) attr_node
->name
, config_element_name
)) {
1864 content
= xmlNodeGetContent(attr_node
);
1866 ret
= -LTTNG_ERR_NOMEM
;
1870 name_len
= strlen((char *) content
);
1871 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
1872 WARN("Channel name too long.");
1873 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1878 strncpy(channel
->name
, (const char *) content
, name_len
);
1880 } else if (!strcmp((const char *) attr_node
->name
,
1881 config_element_enabled
)) {
1886 content
= xmlNodeGetContent(attr_node
);
1888 ret
= -LTTNG_ERR_NOMEM
;
1892 ret
= parse_bool(content
, &enabled
);
1895 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1899 channel
->enabled
= enabled
;
1900 } else if (!strcmp((const char *) attr_node
->name
,
1901 config_element_overwrite_mode
)) {
1904 /* overwrite_mode */
1905 content
= xmlNodeGetContent(attr_node
);
1907 ret
= -LTTNG_ERR_NOMEM
;
1911 ret
= get_overwrite_mode(content
);
1914 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1918 channel
->attr
.overwrite
= ret
;
1919 } else if (!strcmp((const char *) attr_node
->name
,
1920 config_element_subbuf_size
)) {
1923 /* subbuffer_size */
1924 content
= xmlNodeGetContent(attr_node
);
1926 ret
= -LTTNG_ERR_NOMEM
;
1930 ret
= parse_uint(content
, &channel
->attr
.subbuf_size
);
1933 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1936 } else if (!strcmp((const char *) attr_node
->name
,
1937 config_element_num_subbuf
)) {
1940 /* subbuffer_count */
1941 content
= xmlNodeGetContent(attr_node
);
1943 ret
= -LTTNG_ERR_NOMEM
;
1947 ret
= parse_uint(content
, &channel
->attr
.num_subbuf
);
1950 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1953 } else if (!strcmp((const char *) attr_node
->name
,
1954 config_element_switch_timer_interval
)) {
1956 uint64_t switch_timer_interval
= 0;
1958 /* switch_timer_interval */
1959 content
= xmlNodeGetContent(attr_node
);
1961 ret
= -LTTNG_ERR_NOMEM
;
1965 ret
= parse_uint(content
, &switch_timer_interval
);
1968 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1972 if (switch_timer_interval
> UINT_MAX
) {
1973 WARN("switch_timer_interval out of range.");
1974 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1978 channel
->attr
.switch_timer_interval
=
1979 switch_timer_interval
;
1980 } else if (!strcmp((const char *) attr_node
->name
,
1981 config_element_read_timer_interval
)) {
1983 uint64_t read_timer_interval
= 0;
1985 /* read_timer_interval */
1986 content
= xmlNodeGetContent(attr_node
);
1988 ret
= -LTTNG_ERR_NOMEM
;
1992 ret
= parse_uint(content
, &read_timer_interval
);
1995 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
1999 if (read_timer_interval
> UINT_MAX
) {
2000 WARN("read_timer_interval out of range.");
2001 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2005 channel
->attr
.read_timer_interval
=
2006 read_timer_interval
;
2007 } else if (!strcmp((const char *) attr_node
->name
,
2008 config_element_output_type
)) {
2012 content
= xmlNodeGetContent(attr_node
);
2014 ret
= -LTTNG_ERR_NOMEM
;
2018 ret
= get_output_type(content
);
2021 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2025 channel
->attr
.output
= ret
;
2026 } else if (!strcmp((const char *) attr_node
->name
,
2027 config_element_tracefile_size
)) {
2030 /* tracefile_size */
2031 content
= xmlNodeGetContent(attr_node
);
2033 ret
= -LTTNG_ERR_NOMEM
;
2037 ret
= parse_uint(content
, &channel
->attr
.tracefile_size
);
2040 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2043 } else if (!strcmp((const char *) attr_node
->name
,
2044 config_element_tracefile_count
)) {
2047 /* tracefile_count */
2048 content
= xmlNodeGetContent(attr_node
);
2050 ret
= -LTTNG_ERR_NOMEM
;
2054 ret
= parse_uint(content
, &channel
->attr
.tracefile_count
);
2057 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2060 } else if (!strcmp((const char *) attr_node
->name
,
2061 config_element_live_timer_interval
)) {
2063 uint64_t live_timer_interval
= 0;
2065 /* live_timer_interval */
2066 content
= xmlNodeGetContent(attr_node
);
2068 ret
= -LTTNG_ERR_NOMEM
;
2072 ret
= parse_uint(content
, &live_timer_interval
);
2075 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2079 if (live_timer_interval
> UINT_MAX
) {
2080 WARN("live_timer_interval out of range.");
2081 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2085 channel
->attr
.live_timer_interval
=
2086 live_timer_interval
;
2087 } else if (!strcmp((const char *) attr_node
->name
,
2088 config_element_monitor_timer_interval
)) {
2090 uint64_t monitor_timer_interval
= 0;
2092 /* monitor_timer_interval */
2093 content
= xmlNodeGetContent(attr_node
);
2095 ret
= -LTTNG_ERR_NOMEM
;
2099 ret
= parse_uint(content
, &monitor_timer_interval
);
2102 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2106 ret
= lttng_channel_set_monitor_timer_interval(channel
,
2107 monitor_timer_interval
);
2109 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2112 } else if (!strcmp((const char *) attr_node
->name
,
2113 config_element_blocking_timeout
)) {
2115 int64_t blocking_timeout
= 0;
2117 /* blocking_timeout */
2118 content
= xmlNodeGetContent(attr_node
);
2120 ret
= -LTTNG_ERR_NOMEM
;
2124 ret
= parse_int(content
, &blocking_timeout
);
2127 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2131 ret
= lttng_channel_set_blocking_timeout(channel
,
2134 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2137 } else if (!strcmp((const char *) attr_node
->name
,
2138 config_element_events
)) {
2140 *events_node
= attr_node
;
2143 *contexts_node
= attr_node
;
2151 int process_context_node(xmlNodePtr context_node
,
2152 struct lttng_handle
*handle
, const char *channel_name
)
2155 struct lttng_event_context context
;
2156 xmlNodePtr context_child_node
= xmlFirstElementChild(context_node
);
2159 assert(channel_name
);
2161 if (!context_child_node
) {
2162 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2166 memset(&context
, 0, sizeof(context
));
2168 if (!strcmp((const char *) context_child_node
->name
,
2169 config_element_type
)) {
2171 xmlChar
*content
= xmlNodeGetContent(context_child_node
);
2174 ret
= -LTTNG_ERR_NOMEM
;
2178 ret
= get_context_type(content
);
2181 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2186 } else if (!strcmp((const char *) context_child_node
->name
,
2187 config_element_context_perf
)) {
2189 xmlNodePtr perf_attr_node
;
2191 context
.ctx
= handle
->domain
.type
== LTTNG_DOMAIN_KERNEL
?
2192 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
2193 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER
;
2194 for (perf_attr_node
= xmlFirstElementChild(context_child_node
);
2195 perf_attr_node
; perf_attr_node
=
2196 xmlNextElementSibling(perf_attr_node
)) {
2197 if (!strcmp((const char *) perf_attr_node
->name
,
2198 config_element_type
)) {
2203 content
= xmlNodeGetContent(perf_attr_node
);
2205 ret
= -LTTNG_ERR_NOMEM
;
2209 ret
= parse_uint(content
, &type
);
2212 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2216 if (type
> UINT32_MAX
) {
2217 WARN("perf context type out of range.");
2218 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2222 context
.u
.perf_counter
.type
= type
;
2223 } else if (!strcmp((const char *) perf_attr_node
->name
,
2224 config_element_config
)) {
2226 uint64_t config
= 0;
2229 content
= xmlNodeGetContent(perf_attr_node
);
2231 ret
= -LTTNG_ERR_NOMEM
;
2235 ret
= parse_uint(content
, &config
);
2238 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2242 context
.u
.perf_counter
.config
= config
;
2243 } else if (!strcmp((const char *) perf_attr_node
->name
,
2244 config_element_name
)) {
2249 content
= xmlNodeGetContent(perf_attr_node
);
2251 ret
= -LTTNG_ERR_NOMEM
;
2255 name_len
= strlen((char *) content
);
2256 if (name_len
>= LTTNG_SYMBOL_NAME_LEN
) {
2257 WARN("perf context name too long.");
2258 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2263 strncpy(context
.u
.perf_counter
.name
, (const char *) content
,
2268 } else if (!strcmp((const char *) context_child_node
->name
,
2269 config_element_context_app
)) {
2270 /* application context */
2271 xmlNodePtr app_ctx_node
;
2273 context
.ctx
= LTTNG_EVENT_CONTEXT_APP_CONTEXT
;
2274 for (app_ctx_node
= xmlFirstElementChild(context_child_node
);
2275 app_ctx_node
; app_ctx_node
=
2276 xmlNextElementSibling(app_ctx_node
)) {
2278 char **target
= strcmp(
2279 (const char *) app_ctx_node
->name
,
2280 config_element_context_app_provider_name
) == 0 ?
2281 &context
.u
.app_ctx
.provider_name
:
2282 &context
.u
.app_ctx
.ctx_name
;
2284 content
= xmlNodeGetContent(app_ctx_node
);
2286 ret
= -LTTNG_ERR_NOMEM
;
2290 *target
= (char *) content
;
2293 /* Unrecognized context type */
2294 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2298 ret
= lttng_add_context(handle
, &context
, NULL
, channel_name
);
2299 if (context
.ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2300 free(context
.u
.app_ctx
.provider_name
);
2301 free(context
.u
.app_ctx
.ctx_name
);
2308 int process_contexts_node(xmlNodePtr contexts_node
,
2309 struct lttng_handle
*handle
, const char *channel_name
)
2312 xmlNodePtr context_node
;
2314 for (context_node
= xmlFirstElementChild(contexts_node
); context_node
;
2315 context_node
= xmlNextElementSibling(context_node
)) {
2316 ret
= process_context_node(context_node
, handle
, channel_name
);
2326 int process_pid_tracker_node(xmlNodePtr pid_tracker_node
,
2327 struct lttng_handle
*handle
)
2330 xmlNodePtr targets_node
= NULL
;
2334 assert(pid_tracker_node
);
2335 /* get the targets node */
2336 for (node
= xmlFirstElementChild(pid_tracker_node
); node
;
2337 node
= xmlNextElementSibling(node
)) {
2338 if (!strcmp((const char *) node
->name
,
2339 config_element_targets
)) {
2340 targets_node
= node
;
2345 if (!targets_node
) {
2346 ret
= LTTNG_ERR_INVALID
;
2350 /* Go through all pid_target node */
2351 child
= xmlChildElementCount(targets_node
);
2353 /* The session is explicitly set to target nothing. */
2354 ret
= lttng_untrack_pid(handle
, -1);
2359 for (node
= xmlFirstElementChild(targets_node
); node
;
2360 node
= xmlNextElementSibling(node
)) {
2361 xmlNodePtr pid_target_node
= node
;
2363 /* get pid node and track it */
2364 for (node
= xmlFirstElementChild(pid_target_node
); node
;
2365 node
= xmlNextElementSibling(node
)) {
2366 if (!strcmp((const char *) node
->name
,
2367 config_element_pid
)) {
2369 xmlChar
*content
= NULL
;
2371 content
= xmlNodeGetContent(node
);
2373 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2377 ret
= parse_int(content
, &pid
);
2380 ret
= LTTNG_ERR_LOAD_INVALID_CONFIG
;
2384 ret
= lttng_track_pid(handle
, (int) pid
);
2390 node
= pid_target_node
;
2398 int process_domain_node(xmlNodePtr domain_node
, const char *session_name
)
2401 struct lttng_domain domain
= { 0 };
2402 struct lttng_handle
*handle
= NULL
;
2403 struct lttng_channel
*channel
= NULL
;
2404 xmlNodePtr channels_node
= NULL
;
2405 xmlNodePtr trackers_node
= NULL
;
2406 xmlNodePtr pid_tracker_node
= NULL
;
2409 assert(session_name
);
2411 ret
= init_domain(domain_node
, &domain
);
2416 handle
= lttng_create_handle(session_name
, &domain
);
2418 ret
= -LTTNG_ERR_NOMEM
;
2422 /* get the channels node */
2423 for (node
= xmlFirstElementChild(domain_node
); node
;
2424 node
= xmlNextElementSibling(node
)) {
2425 if (!strcmp((const char *) node
->name
,
2426 config_element_channels
)) {
2427 channels_node
= node
;
2432 if (!channels_node
) {
2436 /* create all channels */
2437 for (node
= xmlFirstElementChild(channels_node
); node
;
2438 node
= xmlNextElementSibling(node
)) {
2439 xmlNodePtr contexts_node
= NULL
;
2440 xmlNodePtr events_node
= NULL
;
2441 xmlNodePtr channel_attr_node
;
2443 channel
= lttng_channel_create(&domain
);
2449 for (channel_attr_node
= xmlFirstElementChild(node
);
2450 channel_attr_node
; channel_attr_node
=
2451 xmlNextElementSibling(channel_attr_node
)) {
2452 ret
= process_channel_attr_node(channel_attr_node
,
2453 channel
, &contexts_node
, &events_node
);
2459 ret
= lttng_enable_channel(handle
, channel
);
2464 ret
= process_events_node(events_node
, handle
, channel
->name
);
2469 ret
= process_contexts_node(contexts_node
, handle
,
2475 lttng_channel_destroy(channel
);
2479 /* get the trackers node */
2480 for (node
= xmlFirstElementChild(domain_node
); node
;
2481 node
= xmlNextElementSibling(node
)) {
2482 if (!strcmp((const char *) node
->name
,
2483 config_element_trackers
)) {
2484 trackers_node
= node
;
2489 if (!trackers_node
) {
2493 for (node
= xmlFirstElementChild(trackers_node
); node
;
2494 node
= xmlNextElementSibling(node
)) {
2495 if (!strcmp((const char *)node
->name
,config_element_pid_tracker
)) {
2496 pid_tracker_node
= node
;
2497 ret
= process_pid_tracker_node(pid_tracker_node
, handle
);
2504 if (!pid_tracker_node
) {
2505 lttng_track_pid(handle
, -1);
2509 lttng_channel_destroy(channel
);
2510 lttng_destroy_handle(handle
);
2515 int process_session_node(xmlNodePtr session_node
, const char *session_name
,
2517 const struct config_load_session_override_attr
*overrides
)
2519 int ret
, started
= -1, snapshot_mode
= -1;
2520 uint64_t live_timer_interval
= UINT64_MAX
,
2521 rotation_timer_interval
= 0;
2522 xmlChar
*name
= NULL
;
2523 xmlChar
*shm_path
= NULL
;
2524 xmlNodePtr domains_node
= NULL
;
2525 xmlNodePtr output_node
= NULL
;
2527 struct lttng_domain
*kernel_domain
= NULL
;
2528 struct lttng_domain
*ust_domain
= NULL
;
2529 struct lttng_domain
*jul_domain
= NULL
;
2530 struct lttng_domain
*log4j_domain
= NULL
;
2531 struct lttng_domain
*python_domain
= NULL
;
2533 for (node
= xmlFirstElementChild(session_node
); node
;
2534 node
= xmlNextElementSibling(node
)) {
2535 if (!name
&& !strcmp((const char *) node
->name
,
2536 config_element_name
)) {
2538 xmlChar
*node_content
= xmlNodeGetContent(node
);
2539 if (!node_content
) {
2540 ret
= -LTTNG_ERR_NOMEM
;
2544 name
= node_content
;
2545 } else if (!domains_node
&& !strcmp((const char *) node
->name
,
2546 config_element_domains
)) {
2548 domains_node
= node
;
2549 } else if (started
== -1 && !strcmp((const char *) node
->name
,
2550 config_element_started
)) {
2552 xmlChar
*node_content
= xmlNodeGetContent(node
);
2553 if (!node_content
) {
2554 ret
= -LTTNG_ERR_NOMEM
;
2558 ret
= parse_bool(node_content
, &started
);
2561 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2564 } else if (!output_node
&& !strcmp((const char *) node
->name
,
2565 config_element_output
)) {
2568 } else if (!shm_path
&& !strcmp((const char *) node
->name
,
2569 config_element_shared_memory_path
)) {
2570 /* shared memory path */
2571 xmlChar
*node_content
= xmlNodeGetContent(node
);
2572 if (!node_content
) {
2573 ret
= -LTTNG_ERR_NOMEM
;
2577 shm_path
= node_content
;
2580 * attributes, snapshot_mode, live_timer_interval, rotation_size,
2581 * rotation_timer_interval. */
2582 xmlNodePtr attributes_child
=
2583 xmlFirstElementChild(node
);
2585 if (!strcmp((const char *) attributes_child
->name
,
2586 config_element_snapshot_mode
)) {
2588 xmlChar
*snapshot_mode_content
=
2589 xmlNodeGetContent(attributes_child
);
2590 if (!snapshot_mode_content
) {
2591 ret
= -LTTNG_ERR_NOMEM
;
2595 ret
= parse_bool(snapshot_mode_content
, &snapshot_mode
);
2596 free(snapshot_mode_content
);
2598 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2601 } else if (!strcmp((const char *) attributes_child
->name
,
2602 config_element_live_timer_interval
)) {
2603 /* live_timer_interval */
2604 xmlChar
*timer_interval_content
=
2605 xmlNodeGetContent(attributes_child
);
2606 if (!timer_interval_content
) {
2607 ret
= -LTTNG_ERR_NOMEM
;
2611 ret
= parse_uint(timer_interval_content
, &live_timer_interval
);
2612 free(timer_interval_content
);
2614 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2618 if (!strcmp((const char *) attributes_child
->name
,
2619 config_element_rotation_timer_interval
)) {
2620 /* rotation_timer_interval */
2621 xmlChar
*timer_interval_content
=
2622 xmlNodeGetContent(attributes_child
);
2623 if (!timer_interval_content
) {
2624 ret
= -LTTNG_ERR_NOMEM
;
2628 ret
= parse_uint(timer_interval_content
, &rotation_timer_interval
);
2629 free(timer_interval_content
);
2631 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2639 /* Mandatory attribute, as defined in the session XSD */
2640 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2644 if (session_name
&& strcmp((char *) name
, session_name
)) {
2645 /* This is not the session we are looking for */
2646 ret
= -LTTNG_ERR_NO_SESSION
;
2650 /* Init domains to create the session handles */
2651 for (node
= xmlFirstElementChild(domains_node
); node
;
2652 node
= xmlNextElementSibling(node
)) {
2653 struct lttng_domain
*domain
;
2655 domain
= zmalloc(sizeof(*domain
));
2657 ret
= -LTTNG_ERR_NOMEM
;
2661 ret
= init_domain(node
, domain
);
2663 goto domain_init_error
;
2666 switch (domain
->type
) {
2667 case LTTNG_DOMAIN_KERNEL
:
2668 if (kernel_domain
) {
2669 /* Same domain seen twice, invalid! */
2670 goto domain_init_error
;
2672 kernel_domain
= domain
;
2674 case LTTNG_DOMAIN_UST
:
2676 /* Same domain seen twice, invalid! */
2677 goto domain_init_error
;
2679 ust_domain
= domain
;
2681 case LTTNG_DOMAIN_JUL
:
2683 /* Same domain seen twice, invalid! */
2684 goto domain_init_error
;
2686 jul_domain
= domain
;
2688 case LTTNG_DOMAIN_LOG4J
:
2690 /* Same domain seen twice, invalid! */
2691 goto domain_init_error
;
2693 log4j_domain
= domain
;
2695 case LTTNG_DOMAIN_PYTHON
:
2696 if (python_domain
) {
2697 /* Same domain seen twice, invalid! */
2698 goto domain_init_error
;
2700 python_domain
= domain
;
2703 WARN("Invalid domain type");
2704 goto domain_init_error
;
2709 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2713 /* Apply overrides */
2715 if (overrides
->session_name
) {
2716 xmlChar
*name_override
= xmlStrdup(BAD_CAST(overrides
->session_name
));
2717 if (!name_override
) {
2718 ret
= -LTTNG_ERR_NOMEM
;
2722 /* Overrides the session name to the provided name */
2724 name
= name_override
;
2729 /* Destroy session if it exists */
2730 ret
= lttng_destroy_session((const char *) name
);
2731 if (ret
&& ret
!= -LTTNG_ERR_SESS_NOT_FOUND
) {
2732 ERR("Failed to destroy existing session.");
2737 /* Create session type depending on output type */
2738 if (snapshot_mode
&& snapshot_mode
!= -1) {
2739 ret
= create_snapshot_session((const char *) name
, output_node
,
2741 } else if (live_timer_interval
&&
2742 live_timer_interval
!= UINT64_MAX
) {
2743 ret
= create_session((const char *) name
, kernel_domain
,
2744 ust_domain
, jul_domain
, log4j_domain
,
2745 output_node
, live_timer_interval
, overrides
);
2747 /* regular session */
2748 ret
= create_session((const char *) name
, kernel_domain
,
2749 ust_domain
, jul_domain
, log4j_domain
,
2750 output_node
, UINT64_MAX
, overrides
);
2757 ret
= lttng_set_session_shm_path((const char *) name
,
2758 (const char *) shm_path
);
2764 for (node
= xmlFirstElementChild(domains_node
); node
;
2765 node
= xmlNextElementSibling(node
)) {
2766 ret
= process_domain_node(node
, (const char *) name
);
2772 if (rotation_timer_interval
) {
2773 struct lttng_rotation_schedule_attr
*rotation_attr
= lttng_rotation_schedule_attr_create();
2775 if (!rotation_attr
) {
2778 ret
= lttng_rotation_schedule_attr_set_session_name(rotation_attr
, (const char *) name
);
2780 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2783 lttng_rotation_schedule_attr_set_timer_period(rotation_attr
,
2784 rotation_timer_interval
);
2785 ret
= lttng_rotation_set_schedule(rotation_attr
);
2786 lttng_rotation_schedule_attr_destroy(rotation_attr
);
2793 ret
= lttng_start_tracing((const char *) name
);
2801 ERR("Failed to load session %s: %s", (const char *) name
,
2802 lttng_strerror(ret
));
2803 lttng_destroy_session((const char *) name
);
2807 free(kernel_domain
);
2811 free(python_domain
);
2818 * Return 1 if the given path is readable by the current UID or 0 if not.
2819 * Return -1 if the path is EPERM.
2821 static int validate_file_read_creds(const char *path
)
2827 /* Can we read the file. */
2828 ret
= access(path
, R_OK
);
2832 if (errno
== EACCES
) {
2843 int load_session_from_file(const char *path
, const char *session_name
,
2844 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2845 const struct config_load_session_override_attr
*overrides
)
2847 int ret
, session_found
= !session_name
;
2848 xmlDocPtr doc
= NULL
;
2849 xmlNodePtr sessions_node
;
2850 xmlNodePtr session_node
;
2853 assert(validation_ctx
);
2855 ret
= validate_file_read_creds(path
);
2858 ret
= -LTTNG_ERR_EPERM
;
2860 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2865 doc
= xmlParseFile(path
);
2867 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2871 ret
= xmlSchemaValidateDoc(validation_ctx
->schema_validation_ctx
, doc
);
2873 ERR("Session configuration file validation failed");
2874 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
2878 sessions_node
= xmlDocGetRootElement(doc
);
2879 if (!sessions_node
) {
2883 for (session_node
= xmlFirstElementChild(sessions_node
);
2884 session_node
; session_node
=
2885 xmlNextElementSibling(session_node
)) {
2886 ret
= process_session_node(session_node
,
2887 session_name
, overwrite
, overrides
);
2888 if (session_name
&& ret
== 0) {
2889 /* Target session found and loaded */
2897 ret
= session_found
? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT
;
2902 /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2904 struct dirent
*alloc_dirent(const char *path
)
2908 struct dirent
*entry
;
2910 name_max
= pathconf(path
, _PC_NAME_MAX
);
2911 if (name_max
== -1) {
2912 name_max
= PATH_MAX
;
2914 len
= offsetof(struct dirent
, d_name
) + name_max
+ 1;
2915 entry
= zmalloc(len
);
2920 int load_session_from_path(const char *path
, const char *session_name
,
2921 struct session_config_validation_ctx
*validation_ctx
, int overwrite
,
2922 const struct config_load_session_override_attr
*overrides
)
2924 int ret
, session_found
= !session_name
;
2925 DIR *directory
= NULL
;
2928 assert(validation_ctx
);
2930 directory
= opendir(path
);
2934 /* Try the file loading. */
2937 ret
= -LTTNG_ERR_LOAD_SESSION_NOENT
;
2940 ret
= -LTTNG_ERR_LOAD_IO_FAIL
;
2945 struct dirent
*entry
;
2946 struct dirent
*result
;
2947 char *file_path
= NULL
;
2948 size_t path_len
= strlen(path
);
2950 if (path_len
>= PATH_MAX
) {
2951 ret
= -LTTNG_ERR_INVALID
;
2955 entry
= alloc_dirent(path
);
2957 ret
= -LTTNG_ERR_NOMEM
;
2961 file_path
= zmalloc(PATH_MAX
);
2963 ret
= -LTTNG_ERR_NOMEM
;
2968 strncpy(file_path
, path
, path_len
);
2969 if (file_path
[path_len
- 1] != '/') {
2970 file_path
[path_len
++] = '/';
2974 /* Search for *.lttng files */
2975 while (!readdir_r(directory
, entry
, &result
) && result
) {
2976 size_t file_name_len
= strlen(result
->d_name
);
2978 if (file_name_len
<=
2979 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
)) {
2983 if (path_len
+ file_name_len
>= PATH_MAX
) {
2987 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION
,
2988 result
->d_name
+ file_name_len
- sizeof(
2989 DEFAULT_SESSION_CONFIG_FILE_EXTENSION
) + 1)) {
2993 strncpy(file_path
+ path_len
, result
->d_name
, file_name_len
);
2994 file_path
[path_len
+ file_name_len
] = '\0';
2996 ret
= load_session_from_file(file_path
, session_name
,
2997 validation_ctx
, overwrite
, overrides
);
2998 if (session_name
&& !ret
) {
3007 ret
= load_session_from_file(path
, session_name
,
3008 validation_ctx
, overwrite
, overrides
);
3018 if (closedir(directory
)) {
3023 if (session_found
&& !ret
) {
3031 * Validate that the given path's credentials and the current process have the
3032 * same UID. If so, return 1 else return 0 if it does NOT match.
3034 static int validate_path_creds(const char *path
)
3036 int ret
, uid
= getuid();
3045 ret
= stat(path
, &buf
);
3047 if (errno
!= ENOENT
) {
3053 if (buf
.st_uid
!= uid
) {
3064 int config_load_session(const char *path
, const char *session_name
,
3065 int overwrite
, unsigned int autoload
,
3066 const struct config_load_session_override_attr
*overrides
)
3069 bool session_loaded
= false;
3070 const char *path_ptr
= NULL
;
3071 struct session_config_validation_ctx validation_ctx
= { 0 };
3073 ret
= init_session_config_validation_ctx(&validation_ctx
);
3080 const char *sys_path
;
3083 home_path
= utils_get_home_dir();
3085 char path
[PATH_MAX
];
3088 * Try user session configuration path. Ignore error here so we can
3089 * continue loading the system wide sessions.
3092 ret
= snprintf(path
, sizeof(path
),
3093 DEFAULT_SESSION_HOME_CONFIGPATH
"/"
3094 DEFAULT_SESSION_CONFIG_AUTOLOAD
, home_path
);
3096 PERROR("snprintf session autoload home config path");
3101 * Credentials are only validated for the autoload in order to
3102 * avoid any user session daemon to try to load kernel sessions
3103 * automatically and failing all the times.
3105 ret
= validate_path_creds(path
);
3110 ret
= snprintf(path
, sizeof(path
),
3111 DEFAULT_SESSION_HOME_CONFIGPATH
, home_path
);
3113 PERROR("snprintf session home config path");
3119 ret
= load_session_from_path(path_ptr
, session_name
,
3120 &validation_ctx
, overwrite
, overrides
);
3121 if (ret
&& ret
!= -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3125 * Continue even if the session was found since we have to try
3126 * the system wide sessions.
3128 session_loaded
= true;
3132 /* Reset path pointer for the system wide dir. */
3135 /* Try system wide configuration directory. */
3137 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
"/"
3138 DEFAULT_SESSION_CONFIG_AUTOLOAD
;
3139 ret
= validate_path_creds(sys_path
);
3141 path_ptr
= sys_path
;
3144 sys_path
= DEFAULT_SESSION_SYSTEM_CONFIGPATH
;
3145 path_ptr
= sys_path
;
3149 ret
= load_session_from_path(path_ptr
, session_name
,
3150 &validation_ctx
, overwrite
, overrides
);
3152 session_loaded
= true;
3156 ret
= access(path
, F_OK
);
3161 ret
= -LTTNG_ERR_INVALID
;
3162 WARN("Session configuration path does not exist.");
3165 ret
= -LTTNG_ERR_EPERM
;
3168 ret
= -LTTNG_ERR_UNK
;
3174 ret
= load_session_from_path(path
, session_name
,
3175 &validation_ctx
, overwrite
, overrides
);
3178 fini_session_config_validation_ctx(&validation_ctx
);
3179 if (ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
&& !session_name
&& !path
) {
3181 * Don't report an error if no sessions are found when called
3182 * without a session_name or a search path.
3187 if (session_loaded
&& ret
== -LTTNG_ERR_LOAD_SESSION_NOENT
) {
3188 /* A matching session was found in one of the search paths. */
3195 void __attribute__((destructor
)) session_config_exit(void)