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.
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/macros.h>
29 #include <common/utils.h>
30 #include <common/config/config-session-internal.h>
31 #include <lttng/lttng-error.h>
32 #include <libxml/parser.h>
33 #include <libxml/valid.h>
34 #include <libxml/xmlschemas.h>
37 #include "config-internal.h"
39 struct handler_filter_args
{
41 config_entry_handler_cb handler
;
45 const char * const config_str_yes
= "yes";
46 const char * const config_str_true
= "true";
47 const char * const config_str_on
= "on";
48 const char * const config_str_no
= "no";
49 const char * const config_str_false
= "false";
50 const char * const config_str_off
= "off";
51 const char * const config_xml_encoding
= "UTF-8";
52 const size_t config_xml_encoding_bytes_per_char
= 2; /* Size of the encoding's largest character */
53 const char * const config_xml_indent_string
= "\t";
54 const char * const config_xml_true
= "true";
55 const char * const config_xml_false
= "false";
57 const char * const config_element_channel
= "channel";
58 const char * const config_element_channels
= "channels";
59 const char * const config_element_domain
= "domain";
60 const char * const config_element_domains
= "domains";
61 const char * const config_element_event
= "event";
62 const char * const config_element_events
= "events";
63 const char * const config_element_context
= "context";
64 const char * const config_element_contexts
= "contexts";
65 const char * const config_element_attributes
= "attributes";
66 const char * const config_element_exclusion
= "exclusion";
67 const char * const config_element_exclusions
= "exclusions";
68 const char * const config_element_function_attributes
= "function_attributes";
69 const char * const config_element_probe_attributes
= "probe_attributes";
70 const char * const config_element_symbol_name
= "symbol_name";
71 const char * const config_element_address
= "address";
72 const char * const config_element_offset
= "offset";
73 const char * const config_element_name
= "name";
74 const char * const config_element_enabled
= "enabled";
75 const char * const config_element_overwrite_mode
= "overwrite_mode";
76 const char * const config_element_subbuf_size
= "subbuffer_size";
77 const char * const config_element_num_subbuf
= "subbuffer_count";
78 const char * const config_element_switch_timer_interval
= "switch_timer_interval";
79 const char * const config_element_read_timer_interval
= "read_timer_interval";
80 const char * const config_element_output
= "output";
81 const char * const config_element_output_type
= "output_type";
82 const char * const config_element_tracefile_size
= "tracefile_size";
83 const char * const config_element_tracefile_count
= "tracefile_count";
84 const char * const config_element_live_timer_interval
= "live_timer_interval";
85 const char * const config_element_type
= "type";
86 const char * const config_element_buffer_type
= "buffer_type";
87 const char * const config_element_session
= "session";
88 const char * const config_element_sessions
= "sessions";
89 const char * const config_element_perf
= "perf";
90 const char * const config_element_config
= "config";
91 const char * const config_element_started
= "started";
92 const char * const config_element_snapshot_mode
= "snapshot_mode";
93 const char * const config_element_loglevel
= "loglevel";
94 const char * const config_element_loglevel_type
= "loglevel_type";
95 const char * const config_element_filter
= "filter";
96 const char * const config_element_snapshot_outputs
= "snapshot_outputs";
97 const char * const config_element_consumer_output
= "consumer_output";
98 const char * const config_element_destination
= "destination";
99 const char * const config_element_path
= "path";
100 const char * const config_element_net_output
= "net_output";
101 const char * const config_element_control_uri
= "control_uri";
102 const char * const config_element_data_uri
= "data_uri";
103 const char * const config_element_max_size
= "max_size";
105 const char * const config_domain_type_kernel
= "KERNEL";
106 const char * const config_domain_type_ust
= "UST";
107 const char * const config_domain_type_jul
= "JUL";
109 const char * const config_buffer_type_per_pid
= "PER_PID";
110 const char * const config_buffer_type_per_uid
= "PER_UID";
111 const char * const config_buffer_type_global
= "GLOBAL";
113 const char * const config_overwrite_mode_discard
= "DISCARD";
114 const char * const config_overwrite_mode_overwrite
= "OVERWRITE";
116 const char * const config_output_type_splice
= "SPLICE";
117 const char * const config_output_type_mmap
= "MMAP";
119 const char * const config_loglevel_type_all
= "ALL";
120 const char * const config_loglevel_type_range
= "RANGE";
121 const char * const config_loglevel_type_single
= "SINGLE";
123 const char * const config_event_type_all
= "ALL";
124 const char * const config_event_type_tracepoint
= "TRACEPOINT";
125 const char * const config_event_type_probe
= "PROBE";
126 const char * const config_event_type_function
= "FUNCTION";
127 const char * const config_event_type_function_entry
= "FUNCTION_ENTRY";
128 const char * const config_event_type_noop
= "NOOP";
129 const char * const config_event_type_syscall
= "SYSCALL";
130 const char * const config_event_type_kprobe
= "KPROBE";
131 const char * const config_event_type_kretprobe
= "KRETPROBE";
133 const char * const config_event_context_pid
= "PID";
134 const char * const config_event_context_procname
= "PROCNAME";
135 const char * const config_event_context_prio
= "PRIO";
136 const char * const config_event_context_nice
= "NICE";
137 const char * const config_event_context_vpid
= "VPID";
138 const char * const config_event_context_tid
= "TID";
139 const char * const config_event_context_vtid
= "VTID";
140 const char * const config_event_context_ppid
= "PPID";
141 const char * const config_event_context_vppid
= "VPPID";
142 const char * const config_event_context_pthread_id
= "PTHREAD_ID";
143 const char * const config_event_context_hostname
= "HOSTNAME";
144 const char * const config_event_context_ip
= "IP";
146 static int config_entry_handler_filter(struct handler_filter_args
*args
,
147 const char *section
, const char *name
, const char *value
)
150 struct config_entry entry
= { section
, name
, value
};
154 if (!section
|| !name
|| !value
) {
160 if (strcmp(args
->section
, section
)) {
165 ret
= args
->handler(&entry
, args
->user_data
);
171 int config_get_section_entries(const char *override_path
, const char *section
,
172 config_entry_handler_cb handler
, void *user_data
)
175 FILE *config_file
= NULL
;
176 struct handler_filter_args filter
= { section
, handler
, user_data
};
179 config_file
= fopen(override_path
, "r");
181 DBG("Loaded daemon configuration file at %s",
184 ERR("Failed to open daemon configuration file at %s",
190 char *path
= utils_get_home_dir();
192 /* Try to open the user's daemon configuration file */
194 ret
= asprintf(&path
, DEFAULT_DAEMON_HOME_CONFIGPATH
, path
);
200 config_file
= fopen(path
, "r");
202 DBG("Loaded daemon configuration file at %s", path
);
208 /* Try to open the system daemon configuration file */
210 config_file
= fopen(DEFAULT_DAEMON_HOME_CONFIGPATH
, "r");
215 DBG("No daemon configuration file found.");
219 ret
= ini_parse_file(config_file
,
220 (ini_entry_handler
) config_entry_handler_filter
, (void *) &filter
);
227 int config_parse_value(const char *value
)
230 char *endptr
, *lower_str
;
240 v
= strtoul(value
, &endptr
, 10);
241 if (endptr
!= value
) {
246 lower_str
= zmalloc(len
+ 1);
253 for (i
= 0; i
< len
; i
++) {
254 lower_str
[i
] = tolower(value
[i
]);
257 if (!strcmp(lower_str
, config_str_yes
) ||
258 !strcmp(lower_str
, config_str_true
) ||
259 !strcmp(lower_str
, config_str_on
)) {
261 } else if (!strcmp(lower_str
, config_str_no
) ||
262 !strcmp(lower_str
, config_str_false
) ||
263 !strcmp(lower_str
, config_str_off
)) {
275 * Returns a xmlChar string which must be released using xmlFree().
277 static xmlChar
*encode_string(const char *in_str
)
279 xmlChar
*out_str
= NULL
;
280 xmlCharEncodingHandlerPtr handler
;
281 int out_len
, ret
, in_len
;
285 handler
= xmlFindCharEncodingHandler(config_xml_encoding
);
287 ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!");
291 in_len
= strlen(in_str
);
293 * Add 1 byte for the NULL terminted character. The factor 2 here is
294 * because UTF-8 can be on two bytes so this fits the worst case for each
297 out_len
= (in_len
* 2) + 1;
298 out_str
= xmlMalloc(out_len
);
303 ret
= handler
->input(out_str
, &out_len
, (const xmlChar
*) in_str
, &in_len
);
310 /* out_len is now the size of out_str */
311 out_str
[out_len
] = '\0';
317 struct config_writer
*config_writer_create(int fd_output
)
320 struct config_writer
*writer
;
321 xmlOutputBufferPtr buffer
;
323 writer
= zmalloc(sizeof(struct config_writer
));
325 PERROR("zmalloc config_writer_create");
329 buffer
= xmlOutputBufferCreateFd(fd_output
, NULL
);
334 writer
->writer
= xmlNewTextWriter(buffer
);
335 ret
= xmlTextWriterStartDocument(writer
->writer
, NULL
,
336 config_xml_encoding
, NULL
);
341 ret
= xmlTextWriterSetIndentString(writer
->writer
,
342 BAD_CAST config_xml_indent_string
);
347 ret
= xmlTextWriterSetIndent(writer
->writer
, 1);
355 config_writer_destroy(writer
);
360 int config_writer_destroy(struct config_writer
*writer
)
369 if (xmlTextWriterEndDocument(writer
->writer
) < 0) {
370 WARN("Could not close XML document");
374 if (writer
->writer
) {
375 xmlFreeTextWriter(writer
->writer
);
384 int config_writer_open_element(struct config_writer
*writer
,
385 const char *element_name
)
388 xmlChar
*encoded_element_name
;
390 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
395 encoded_element_name
= encode_string(element_name
);
396 if (!encoded_element_name
) {
401 ret
= xmlTextWriterStartElement(writer
->writer
, encoded_element_name
);
402 xmlFree(encoded_element_name
);
404 return ret
> 0 ? 0 : ret
;
408 int config_writer_close_element(struct config_writer
*writer
)
412 if (!writer
|| !writer
->writer
) {
417 ret
= xmlTextWriterEndElement(writer
->writer
);
419 return ret
> 0 ? 0 : ret
;
423 int config_writer_write_element_unsigned_int(struct config_writer
*writer
,
424 const char *element_name
, uint64_t value
)
427 xmlChar
*encoded_element_name
;
429 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
434 encoded_element_name
= encode_string(element_name
);
435 if (!encoded_element_name
) {
440 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
441 encoded_element_name
, "%" PRIu64
, value
);
442 xmlFree(encoded_element_name
);
444 return ret
> 0 ? 0 : ret
;
448 int config_writer_write_element_signed_int(struct config_writer
*writer
,
449 const char *element_name
, int64_t value
)
452 xmlChar
*encoded_element_name
;
454 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0]) {
459 encoded_element_name
= encode_string(element_name
);
460 if (!encoded_element_name
) {
465 ret
= xmlTextWriterWriteFormatElement(writer
->writer
,
466 encoded_element_name
, "%" PRIi64
, value
);
467 xmlFree(encoded_element_name
);
469 return ret
> 0 ? 0 : ret
;
473 int config_writer_write_element_bool(struct config_writer
*writer
,
474 const char *element_name
, int value
)
476 return config_writer_write_element_string(writer
, element_name
,
477 value
? config_xml_true
: config_xml_false
);
481 int config_writer_write_element_string(struct config_writer
*writer
,
482 const char *element_name
, const char *value
)
485 xmlChar
*encoded_element_name
= NULL
;
486 xmlChar
*encoded_value
= NULL
;
488 if (!writer
|| !writer
->writer
|| !element_name
|| !element_name
[0] ||
494 encoded_element_name
= encode_string(element_name
);
495 if (!encoded_element_name
) {
500 encoded_value
= encode_string(value
);
501 if (!encoded_value
) {
506 ret
= xmlTextWriterWriteElement(writer
->writer
, encoded_element_name
,
509 xmlFree(encoded_element_name
);
510 xmlFree(encoded_value
);
511 return ret
> 0 ? 0 : ret
;