Commit | Line | Data |
---|---|---|
1501a7f3 JG |
1 | /* |
2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
1501a7f3 | 19 | #include <assert.h> |
1501a7f3 | 20 | #include <ctype.h> |
1501a7f3 JG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
36f2332b | 24 | #include <inttypes.h> |
dcf266c0 JG |
25 | #include <dirent.h> |
26 | #include <unistd.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
6c66fa0f | 29 | #include <stdbool.h> |
1501a7f3 JG |
30 | |
31 | #include <common/defaults.h> | |
32 | #include <common/error.h> | |
33 | #include <common/macros.h> | |
34 | #include <common/utils.h> | |
cce35f91 | 35 | #include <common/dynamic-buffer.h> |
e8fa9fb0 | 36 | #include <common/compat/getenv.h> |
fb198a11 JG |
37 | #include <lttng/lttng-error.h> |
38 | #include <libxml/parser.h> | |
39 | #include <libxml/valid.h> | |
40 | #include <libxml/xmlschemas.h> | |
dcf266c0 JG |
41 | #include <libxml/tree.h> |
42 | #include <lttng/lttng.h> | |
43 | #include <lttng/snapshot.h> | |
259c2674 | 44 | #include <lttng/rotation.h> |
1501a7f3 | 45 | |
f40ef1d5 | 46 | #include "session-config.h" |
36f2332b | 47 | #include "config-internal.h" |
1501a7f3 JG |
48 | |
49 | struct handler_filter_args { | |
50 | const char* section; | |
51 | config_entry_handler_cb handler; | |
52 | void *user_data; | |
53 | }; | |
54 | ||
dcf266c0 JG |
55 | struct session_config_validation_ctx { |
56 | xmlSchemaParserCtxtPtr parser_ctx; | |
57 | xmlSchemaPtr schema; | |
58 | xmlSchemaValidCtxtPtr schema_validation_ctx; | |
59 | }; | |
60 | ||
1501a7f3 JG |
61 | const char * const config_str_yes = "yes"; |
62 | const char * const config_str_true = "true"; | |
63 | const char * const config_str_on = "on"; | |
64 | const char * const config_str_no = "no"; | |
65 | const char * const config_str_false = "false"; | |
66 | const char * const config_str_off = "off"; | |
36f2332b | 67 | const char * const config_xml_encoding = "UTF-8"; |
fb198a11 | 68 | const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */ |
36f2332b JG |
69 | const char * const config_xml_indent_string = "\t"; |
70 | const char * const config_xml_true = "true"; | |
71 | const char * const config_xml_false = "false"; | |
1501a7f3 | 72 | |
fb198a11 JG |
73 | const char * const config_element_channel = "channel"; |
74 | const char * const config_element_channels = "channels"; | |
75 | const char * const config_element_domain = "domain"; | |
76 | const char * const config_element_domains = "domains"; | |
77 | const char * const config_element_event = "event"; | |
78 | const char * const config_element_events = "events"; | |
79 | const char * const config_element_context = "context"; | |
80 | const char * const config_element_contexts = "contexts"; | |
81 | const char * const config_element_attributes = "attributes"; | |
82 | const char * const config_element_exclusion = "exclusion"; | |
83 | const char * const config_element_exclusions = "exclusions"; | |
84 | const char * const config_element_function_attributes = "function_attributes"; | |
85 | const char * const config_element_probe_attributes = "probe_attributes"; | |
86 | const char * const config_element_symbol_name = "symbol_name"; | |
87 | const char * const config_element_address = "address"; | |
88 | const char * const config_element_offset = "offset"; | |
89 | const char * const config_element_name = "name"; | |
90 | const char * const config_element_enabled = "enabled"; | |
91 | const char * const config_element_overwrite_mode = "overwrite_mode"; | |
92 | const char * const config_element_subbuf_size = "subbuffer_size"; | |
93 | const char * const config_element_num_subbuf = "subbuffer_count"; | |
94 | const char * const config_element_switch_timer_interval = "switch_timer_interval"; | |
95 | const char * const config_element_read_timer_interval = "read_timer_interval"; | |
6ab3e98b JG |
96 | LTTNG_HIDDEN const char * const config_element_monitor_timer_interval = "monitor_timer_interval"; |
97 | LTTNG_HIDDEN const char * const config_element_blocking_timeout = "blocking_timeout"; | |
fb198a11 JG |
98 | const char * const config_element_output = "output"; |
99 | const char * const config_element_output_type = "output_type"; | |
100 | const char * const config_element_tracefile_size = "tracefile_size"; | |
101 | const char * const config_element_tracefile_count = "tracefile_count"; | |
102 | const char * const config_element_live_timer_interval = "live_timer_interval"; | |
ec2ed7da JG |
103 | LTTNG_HIDDEN const char * const config_element_discarded_events = "discarded_events"; |
104 | LTTNG_HIDDEN const char * const config_element_lost_packets = "lost_packets"; | |
fb198a11 JG |
105 | const char * const config_element_type = "type"; |
106 | const char * const config_element_buffer_type = "buffer_type"; | |
107 | const char * const config_element_session = "session"; | |
108 | const char * const config_element_sessions = "sessions"; | |
ec2ed7da JG |
109 | LTTNG_HIDDEN const char * const config_element_context_perf = "perf"; |
110 | LTTNG_HIDDEN const char * const config_element_context_app = "app"; | |
111 | LTTNG_HIDDEN const char * const config_element_context_app_provider_name = "provider_name"; | |
112 | LTTNG_HIDDEN const char * const config_element_context_app_ctx_name = "ctx_name"; | |
fb198a11 JG |
113 | const char * const config_element_config = "config"; |
114 | const char * const config_element_started = "started"; | |
115 | const char * const config_element_snapshot_mode = "snapshot_mode"; | |
116 | const char * const config_element_loglevel = "loglevel"; | |
117 | const char * const config_element_loglevel_type = "loglevel_type"; | |
118 | const char * const config_element_filter = "filter"; | |
ec2ed7da | 119 | LTTNG_HIDDEN const char * const config_element_filter_expression = "filter_expression"; |
fb198a11 JG |
120 | const char * const config_element_snapshot_outputs = "snapshot_outputs"; |
121 | const char * const config_element_consumer_output = "consumer_output"; | |
122 | const char * const config_element_destination = "destination"; | |
123 | const char * const config_element_path = "path"; | |
124 | const char * const config_element_net_output = "net_output"; | |
125 | const char * const config_element_control_uri = "control_uri"; | |
126 | const char * const config_element_data_uri = "data_uri"; | |
127 | const char * const config_element_max_size = "max_size"; | |
ccf10263 MD |
128 | const char * const config_element_pid = "pid"; |
129 | const char * const config_element_pids = "pids"; | |
9e7c9f56 | 130 | const char * const config_element_shared_memory_path = "shared_memory_path"; |
ebbf5ab7 JR |
131 | const char * const config_element_pid_tracker = "pid_tracker"; |
132 | const char * const config_element_trackers = "trackers"; | |
847a5916 JR |
133 | const char * const config_element_targets = "targets"; |
134 | const char * const config_element_target_pid = "pid_target"; | |
fb198a11 | 135 | |
259c2674 | 136 | LTTNG_HIDDEN const char * const config_element_rotation_timer_interval = "rotation_schedule_timer_period"; |
329f3443 | 137 | LTTNG_HIDDEN const char * const config_element_rotation_size = "rotation_schedule_size"; |
259c2674 JD |
138 | LTTNG_HIDDEN const char * const config_element_rotation_schedule = "rotation_schedule"; |
139 | ||
fb198a11 JG |
140 | const char * const config_domain_type_kernel = "KERNEL"; |
141 | const char * const config_domain_type_ust = "UST"; | |
142 | const char * const config_domain_type_jul = "JUL"; | |
5cdb6027 | 143 | const char * const config_domain_type_log4j = "LOG4J"; |
0e115563 | 144 | const char * const config_domain_type_python = "PYTHON"; |
fb198a11 JG |
145 | |
146 | const char * const config_buffer_type_per_pid = "PER_PID"; | |
147 | const char * const config_buffer_type_per_uid = "PER_UID"; | |
148 | const char * const config_buffer_type_global = "GLOBAL"; | |
149 | ||
150 | const char * const config_overwrite_mode_discard = "DISCARD"; | |
151 | const char * const config_overwrite_mode_overwrite = "OVERWRITE"; | |
152 | ||
153 | const char * const config_output_type_splice = "SPLICE"; | |
154 | const char * const config_output_type_mmap = "MMAP"; | |
155 | ||
156 | const char * const config_loglevel_type_all = "ALL"; | |
157 | const char * const config_loglevel_type_range = "RANGE"; | |
158 | const char * const config_loglevel_type_single = "SINGLE"; | |
159 | ||
160 | const char * const config_event_type_all = "ALL"; | |
161 | const char * const config_event_type_tracepoint = "TRACEPOINT"; | |
162 | const char * const config_event_type_probe = "PROBE"; | |
163 | const char * const config_event_type_function = "FUNCTION"; | |
164 | const char * const config_event_type_function_entry = "FUNCTION_ENTRY"; | |
165 | const char * const config_event_type_noop = "NOOP"; | |
166 | const char * const config_event_type_syscall = "SYSCALL"; | |
167 | const char * const config_event_type_kprobe = "KPROBE"; | |
168 | const char * const config_event_type_kretprobe = "KRETPROBE"; | |
169 | ||
170 | const char * const config_event_context_pid = "PID"; | |
171 | const char * const config_event_context_procname = "PROCNAME"; | |
172 | const char * const config_event_context_prio = "PRIO"; | |
173 | const char * const config_event_context_nice = "NICE"; | |
174 | const char * const config_event_context_vpid = "VPID"; | |
175 | const char * const config_event_context_tid = "TID"; | |
176 | const char * const config_event_context_vtid = "VTID"; | |
177 | const char * const config_event_context_ppid = "PPID"; | |
178 | const char * const config_event_context_vppid = "VPPID"; | |
179 | const char * const config_event_context_pthread_id = "PTHREAD_ID"; | |
180 | const char * const config_event_context_hostname = "HOSTNAME"; | |
181 | const char * const config_event_context_ip = "IP"; | |
e885a367 | 182 | const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER"; |
ec2ed7da JG |
183 | LTTNG_HIDDEN const char * const config_event_context_app = "APP"; |
184 | LTTNG_HIDDEN const char * const config_event_context_interruptible = "INTERRUPTIBLE"; | |
185 | LTTNG_HIDDEN const char * const config_event_context_preemptible = "PREEMPTIBLE"; | |
186 | LTTNG_HIDDEN const char * const config_event_context_need_reschedule = "NEED_RESCHEDULE"; | |
187 | LTTNG_HIDDEN const char * const config_event_context_migratable = "MIGRATABLE"; | |
fb198a11 | 188 | |
fbd987c9 JG |
189 | /* Deprecated symbols */ |
190 | const char * const config_element_perf; | |
191 | ||
d7b645e2 JR |
192 | enum process_event_node_phase { |
193 | CREATION = 0, | |
194 | ENABLE = 1, | |
195 | }; | |
196 | ||
dcf266c0 JG |
197 | struct consumer_output { |
198 | int enabled; | |
199 | char *path; | |
200 | char *control_uri; | |
201 | char *data_uri; | |
202 | }; | |
203 | ||
1501a7f3 JG |
204 | static int config_entry_handler_filter(struct handler_filter_args *args, |
205 | const char *section, const char *name, const char *value) | |
206 | { | |
207 | int ret = 0; | |
208 | struct config_entry entry = { section, name, value }; | |
209 | ||
210 | assert(args); | |
211 | ||
212 | if (!section || !name || !value) { | |
213 | ret = -EIO; | |
214 | goto end; | |
215 | } | |
216 | ||
217 | if (args->section) { | |
218 | if (strcmp(args->section, section)) { | |
219 | goto end; | |
220 | } | |
221 | } | |
222 | ||
223 | ret = args->handler(&entry, args->user_data); | |
224 | end: | |
225 | return ret; | |
226 | } | |
227 | ||
228 | LTTNG_HIDDEN | |
229 | int config_get_section_entries(const char *override_path, const char *section, | |
230 | config_entry_handler_cb handler, void *user_data) | |
231 | { | |
232 | int ret = 0; | |
7ab02a27 | 233 | char *path; |
1501a7f3 JG |
234 | FILE *config_file = NULL; |
235 | struct handler_filter_args filter = { section, handler, user_data }; | |
236 | ||
7ab02a27 DG |
237 | /* First, try system-wide conf. file. */ |
238 | path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH; | |
239 | ||
240 | config_file = fopen(path, "r"); | |
241 | if (config_file) { | |
242 | DBG("Loading daemon conf file at %s", path); | |
243 | /* | |
244 | * Return value is not very important here since error or not, we | |
245 | * continue and try the next possible conf. file. | |
246 | */ | |
247 | (void) ini_parse_file(config_file, | |
248 | (ini_entry_handler) config_entry_handler_filter, | |
249 | (void *) &filter); | |
250 | fclose(config_file); | |
251 | } | |
252 | ||
253 | /* Second is the user local configuration. */ | |
254 | path = utils_get_home_dir(); | |
255 | if (path) { | |
256 | char fullpath[PATH_MAX]; | |
257 | ||
258 | ret = snprintf(fullpath, sizeof(fullpath), | |
259 | DEFAULT_DAEMON_HOME_CONFIGPATH, path); | |
260 | if (ret < 0) { | |
261 | PERROR("snprintf user conf. path"); | |
262 | goto error; | |
263 | } | |
264 | ||
265 | config_file = fopen(fullpath, "r"); | |
266 | if (config_file) { | |
267 | DBG("Loading daemon user conf file at %s", path); | |
268 | /* | |
269 | * Return value is not very important here since error or not, we | |
270 | * continue and try the next possible conf. file. | |
271 | */ | |
272 | (void) ini_parse_file(config_file, | |
273 | (ini_entry_handler) config_entry_handler_filter, | |
274 | (void *) &filter); | |
275 | fclose(config_file); | |
276 | } | |
277 | } | |
278 | ||
279 | /* Final path is the one that the user might have provided. */ | |
1501a7f3 JG |
280 | if (override_path) { |
281 | config_file = fopen(override_path, "r"); | |
282 | if (config_file) { | |
7ab02a27 DG |
283 | DBG("Loading daemon command line conf file at %s", override_path); |
284 | (void) ini_parse_file(config_file, | |
285 | (ini_entry_handler) config_entry_handler_filter, | |
286 | (void *) &filter); | |
287 | fclose(config_file); | |
1501a7f3 JG |
288 | } else { |
289 | ERR("Failed to open daemon configuration file at %s", | |
290 | override_path); | |
291 | ret = -ENOENT; | |
7ab02a27 | 292 | goto error; |
1501a7f3 JG |
293 | } |
294 | } | |
295 | ||
7ab02a27 DG |
296 | /* Everything went well. */ |
297 | ret = 0; | |
1501a7f3 | 298 | |
7ab02a27 | 299 | error: |
1501a7f3 JG |
300 | return ret; |
301 | } | |
302 | ||
303 | LTTNG_HIDDEN | |
304 | int config_parse_value(const char *value) | |
305 | { | |
306 | int i, ret = 0; | |
307 | char *endptr, *lower_str; | |
308 | size_t len; | |
309 | unsigned long v; | |
310 | ||
311 | len = strlen(value); | |
312 | if (!len) { | |
313 | ret = -1; | |
314 | goto end; | |
315 | } | |
316 | ||
317 | v = strtoul(value, &endptr, 10); | |
318 | if (endptr != value) { | |
319 | ret = v; | |
320 | goto end; | |
321 | } | |
322 | ||
323 | lower_str = zmalloc(len + 1); | |
324 | if (!lower_str) { | |
325 | PERROR("zmalloc"); | |
326 | ret = -errno; | |
327 | goto end; | |
328 | } | |
329 | ||
330 | for (i = 0; i < len; i++) { | |
331 | lower_str[i] = tolower(value[i]); | |
332 | } | |
333 | ||
334 | if (!strcmp(lower_str, config_str_yes) || | |
335 | !strcmp(lower_str, config_str_true) || | |
336 | !strcmp(lower_str, config_str_on)) { | |
337 | ret = 1; | |
338 | } else if (!strcmp(lower_str, config_str_no) || | |
339 | !strcmp(lower_str, config_str_false) || | |
340 | !strcmp(lower_str, config_str_off)) { | |
341 | ret = 0; | |
342 | } else { | |
343 | ret = -1; | |
344 | } | |
345 | ||
346 | free(lower_str); | |
347 | end: | |
348 | return ret; | |
349 | } | |
36f2332b JG |
350 | |
351 | /* | |
352 | * Returns a xmlChar string which must be released using xmlFree(). | |
353 | */ | |
354 | static xmlChar *encode_string(const char *in_str) | |
355 | { | |
356 | xmlChar *out_str = NULL; | |
357 | xmlCharEncodingHandlerPtr handler; | |
358 | int out_len, ret, in_len; | |
359 | ||
360 | assert(in_str); | |
361 | ||
362 | handler = xmlFindCharEncodingHandler(config_xml_encoding); | |
363 | if (!handler) { | |
364 | ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!"); | |
365 | goto end; | |
366 | } | |
367 | ||
368 | in_len = strlen(in_str); | |
369 | /* | |
f76d886f JG |
370 | * Add 1 byte for the NULL terminted character. The factor 4 here is |
371 | * used because UTF-8 characters can take up to 4 bytes. | |
36f2332b | 372 | */ |
f76d886f | 373 | out_len = (in_len * 4) + 1; |
36f2332b JG |
374 | out_str = xmlMalloc(out_len); |
375 | if (!out_str) { | |
376 | goto end; | |
377 | } | |
378 | ||
379 | ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len); | |
380 | if (ret < 0) { | |
381 | xmlFree(out_str); | |
382 | out_str = NULL; | |
383 | goto end; | |
384 | } | |
385 | ||
386 | /* out_len is now the size of out_str */ | |
387 | out_str[out_len] = '\0'; | |
388 | end: | |
389 | return out_str; | |
390 | } | |
391 | ||
392 | LTTNG_HIDDEN | |
705bb62f | 393 | struct config_writer *config_writer_create(int fd_output, int indent) |
36f2332b JG |
394 | { |
395 | int ret; | |
396 | struct config_writer *writer; | |
397 | xmlOutputBufferPtr buffer; | |
398 | ||
399 | writer = zmalloc(sizeof(struct config_writer)); | |
400 | if (!writer) { | |
401 | PERROR("zmalloc config_writer_create"); | |
402 | goto end; | |
403 | } | |
404 | ||
405 | buffer = xmlOutputBufferCreateFd(fd_output, NULL); | |
406 | if (!buffer) { | |
407 | goto error_destroy; | |
408 | } | |
409 | ||
410 | writer->writer = xmlNewTextWriter(buffer); | |
411 | ret = xmlTextWriterStartDocument(writer->writer, NULL, | |
412 | config_xml_encoding, NULL); | |
413 | if (ret < 0) { | |
414 | goto error_destroy; | |
415 | } | |
416 | ||
417 | ret = xmlTextWriterSetIndentString(writer->writer, | |
418 | BAD_CAST config_xml_indent_string); | |
705bb62f | 419 | if (ret) { |
36f2332b JG |
420 | goto error_destroy; |
421 | } | |
422 | ||
705bb62f JRJ |
423 | ret = xmlTextWriterSetIndent(writer->writer, indent); |
424 | if (ret) { | |
36f2332b JG |
425 | goto error_destroy; |
426 | } | |
427 | ||
428 | end: | |
429 | return writer; | |
430 | error_destroy: | |
431 | config_writer_destroy(writer); | |
432 | return NULL; | |
433 | } | |
434 | ||
435 | LTTNG_HIDDEN | |
436 | int config_writer_destroy(struct config_writer *writer) | |
437 | { | |
438 | int ret = 0; | |
439 | ||
440 | if (!writer) { | |
441 | ret = -EINVAL; | |
442 | goto end; | |
443 | } | |
444 | ||
445 | if (xmlTextWriterEndDocument(writer->writer) < 0) { | |
446 | WARN("Could not close XML document"); | |
447 | ret = -EIO; | |
448 | } | |
449 | ||
450 | if (writer->writer) { | |
451 | xmlFreeTextWriter(writer->writer); | |
452 | } | |
453 | ||
454 | free(writer); | |
455 | end: | |
456 | return ret; | |
457 | } | |
458 | ||
459 | LTTNG_HIDDEN | |
460 | int config_writer_open_element(struct config_writer *writer, | |
461 | const char *element_name) | |
462 | { | |
463 | int ret; | |
464 | xmlChar *encoded_element_name; | |
465 | ||
466 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
467 | ret = -1; | |
468 | goto end; | |
469 | } | |
470 | ||
471 | encoded_element_name = encode_string(element_name); | |
472 | if (!encoded_element_name) { | |
473 | ret = -1; | |
474 | goto end; | |
475 | } | |
476 | ||
477 | ret = xmlTextWriterStartElement(writer->writer, encoded_element_name); | |
478 | xmlFree(encoded_element_name); | |
479 | end: | |
28676a1d | 480 | return ret >= 0 ? 0 : ret; |
36f2332b JG |
481 | } |
482 | ||
e10b6a1c JG |
483 | LTTNG_HIDDEN |
484 | int config_writer_write_attribute(struct config_writer *writer, | |
485 | const char *name, const char *value) | |
486 | { | |
487 | int ret; | |
488 | xmlChar *encoded_name = NULL; | |
489 | xmlChar *encoded_value = NULL; | |
490 | ||
491 | if (!writer || !writer->writer || !name || !name[0]) { | |
492 | ret = -1; | |
493 | goto end; | |
494 | } | |
495 | ||
496 | encoded_name = encode_string(name); | |
497 | if (!encoded_name) { | |
498 | ret = -1; | |
499 | goto end; | |
500 | } | |
501 | ||
502 | encoded_value = encode_string(value); | |
503 | if (!encoded_value) { | |
504 | ret = -1; | |
505 | goto end; | |
506 | } | |
507 | ||
508 | ret = xmlTextWriterWriteAttribute(writer->writer, encoded_name, | |
509 | encoded_value); | |
510 | end: | |
511 | xmlFree(encoded_name); | |
512 | xmlFree(encoded_value); | |
513 | return ret >= 0 ? 0 : ret; | |
514 | } | |
515 | ||
36f2332b JG |
516 | LTTNG_HIDDEN |
517 | int config_writer_close_element(struct config_writer *writer) | |
518 | { | |
519 | int ret; | |
520 | ||
521 | if (!writer || !writer->writer) { | |
522 | ret = -1; | |
523 | goto end; | |
524 | } | |
525 | ||
526 | ret = xmlTextWriterEndElement(writer->writer); | |
527 | end: | |
28676a1d | 528 | return ret >= 0 ? 0 : ret; |
36f2332b JG |
529 | } |
530 | ||
531 | LTTNG_HIDDEN | |
532 | int config_writer_write_element_unsigned_int(struct config_writer *writer, | |
533 | const char *element_name, uint64_t value) | |
534 | { | |
535 | int ret; | |
536 | xmlChar *encoded_element_name; | |
537 | ||
538 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
539 | ret = -1; | |
540 | goto end; | |
541 | } | |
542 | ||
543 | encoded_element_name = encode_string(element_name); | |
544 | if (!encoded_element_name) { | |
545 | ret = -1; | |
546 | goto end; | |
547 | } | |
548 | ||
549 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
550 | encoded_element_name, "%" PRIu64, value); | |
551 | xmlFree(encoded_element_name); | |
552 | end: | |
28676a1d | 553 | return ret >= 0 ? 0 : ret; |
36f2332b JG |
554 | } |
555 | ||
556 | LTTNG_HIDDEN | |
557 | int config_writer_write_element_signed_int(struct config_writer *writer, | |
558 | const char *element_name, int64_t value) | |
559 | { | |
560 | int ret; | |
561 | xmlChar *encoded_element_name; | |
562 | ||
563 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
564 | ret = -1; | |
565 | goto end; | |
566 | } | |
567 | ||
568 | encoded_element_name = encode_string(element_name); | |
569 | if (!encoded_element_name) { | |
570 | ret = -1; | |
571 | goto end; | |
572 | } | |
573 | ||
574 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
575 | encoded_element_name, "%" PRIi64, value); | |
576 | xmlFree(encoded_element_name); | |
577 | end: | |
28676a1d | 578 | return ret >= 0 ? 0 : ret; |
36f2332b JG |
579 | } |
580 | ||
581 | LTTNG_HIDDEN | |
582 | int config_writer_write_element_bool(struct config_writer *writer, | |
583 | const char *element_name, int value) | |
584 | { | |
585 | return config_writer_write_element_string(writer, element_name, | |
586 | value ? config_xml_true : config_xml_false); | |
587 | } | |
588 | ||
589 | LTTNG_HIDDEN | |
590 | int config_writer_write_element_string(struct config_writer *writer, | |
591 | const char *element_name, const char *value) | |
592 | { | |
593 | int ret; | |
594 | xmlChar *encoded_element_name = NULL; | |
595 | xmlChar *encoded_value = NULL; | |
596 | ||
597 | if (!writer || !writer->writer || !element_name || !element_name[0] || | |
598 | !value) { | |
599 | ret = -1; | |
600 | goto end; | |
601 | } | |
602 | ||
603 | encoded_element_name = encode_string(element_name); | |
604 | if (!encoded_element_name) { | |
605 | ret = -1; | |
606 | goto end; | |
607 | } | |
608 | ||
609 | encoded_value = encode_string(value); | |
610 | if (!encoded_value) { | |
611 | ret = -1; | |
612 | goto end; | |
613 | } | |
614 | ||
615 | ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name, | |
dcf266c0 | 616 | encoded_value); |
36f2332b JG |
617 | end: |
618 | xmlFree(encoded_element_name); | |
619 | xmlFree(encoded_value); | |
28676a1d | 620 | return ret >= 0 ? 0 : ret; |
36f2332b | 621 | } |
dcf266c0 JG |
622 | |
623 | static | |
624 | void xml_error_handler(void *ctx, const char *format, ...) | |
625 | { | |
626 | char *errMsg; | |
627 | va_list args; | |
628 | int ret; | |
629 | ||
630 | va_start(args, format); | |
631 | ret = vasprintf(&errMsg, format, args); | |
6c043b48 | 632 | va_end(args); |
dcf266c0 JG |
633 | if (ret == -1) { |
634 | ERR("String allocation failed in xml error handler"); | |
635 | return; | |
636 | } | |
dcf266c0 JG |
637 | |
638 | fprintf(stderr, "XML Error: %s", errMsg); | |
639 | free(errMsg); | |
640 | } | |
641 | ||
642 | static | |
643 | void fini_session_config_validation_ctx( | |
644 | struct session_config_validation_ctx *ctx) | |
645 | { | |
646 | if (ctx->parser_ctx) { | |
647 | xmlSchemaFreeParserCtxt(ctx->parser_ctx); | |
648 | } | |
649 | ||
650 | if (ctx->schema) { | |
651 | xmlSchemaFree(ctx->schema); | |
652 | } | |
653 | ||
654 | if (ctx->schema_validation_ctx) { | |
655 | xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); | |
656 | } | |
657 | ||
658 | memset(ctx, 0, sizeof(struct session_config_validation_ctx)); | |
659 | } | |
660 | ||
54e399cb JG |
661 | static |
662 | char *get_session_config_xsd_path() | |
663 | { | |
664 | char *xsd_path; | |
e8fa9fb0 | 665 | const char *base_path = lttng_secure_getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV); |
54e399cb JG |
666 | size_t base_path_len; |
667 | size_t max_path_len; | |
668 | ||
669 | if (!base_path) { | |
670 | base_path = DEFAULT_SESSION_CONFIG_XSD_PATH; | |
671 | } | |
672 | ||
673 | base_path_len = strlen(base_path); | |
674 | max_path_len = base_path_len + | |
675 | sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1; | |
676 | xsd_path = zmalloc(max_path_len); | |
677 | if (!xsd_path) { | |
678 | goto end; | |
679 | } | |
680 | ||
00a7f6f8 | 681 | strcpy(xsd_path, base_path); |
54e399cb JG |
682 | if (xsd_path[base_path_len - 1] != '/') { |
683 | xsd_path[base_path_len++] = '/'; | |
684 | } | |
685 | ||
00a7f6f8 | 686 | strcpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME); |
54e399cb JG |
687 | end: |
688 | return xsd_path; | |
689 | } | |
690 | ||
dcf266c0 JG |
691 | static |
692 | int init_session_config_validation_ctx( | |
693 | struct session_config_validation_ctx *ctx) | |
694 | { | |
695 | int ret; | |
54e399cb JG |
696 | char *xsd_path = get_session_config_xsd_path(); |
697 | ||
698 | if (!xsd_path) { | |
699 | ret = -LTTNG_ERR_NOMEM; | |
700 | goto end; | |
701 | } | |
dcf266c0 | 702 | |
54e399cb | 703 | ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); |
dcf266c0 JG |
704 | if (!ctx->parser_ctx) { |
705 | ERR("XSD parser context creation failed"); | |
706 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
707 | goto end; | |
708 | } | |
709 | xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, | |
710 | xml_error_handler, NULL); | |
711 | ||
712 | ctx->schema = xmlSchemaParse(ctx->parser_ctx); | |
713 | if (!ctx->schema) { | |
714 | ERR("XSD parsing failed"); | |
715 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
716 | goto end; | |
717 | } | |
718 | ||
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; | |
723 | goto end; | |
724 | } | |
725 | ||
726 | xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, | |
727 | xml_error_handler, NULL); | |
728 | ret = 0; | |
729 | ||
730 | end: | |
731 | if (ret) { | |
732 | fini_session_config_validation_ctx(ctx); | |
733 | } | |
734 | ||
54e399cb | 735 | free(xsd_path); |
dcf266c0 JG |
736 | return ret; |
737 | } | |
738 | ||
739 | static | |
740 | int parse_uint(xmlChar *str, uint64_t *val) | |
741 | { | |
742 | int ret; | |
743 | char *endptr; | |
744 | ||
745 | if (!str || !val) { | |
746 | ret = -1; | |
747 | goto end; | |
748 | } | |
749 | ||
750 | *val = strtoull((const char *) str, &endptr, 10); | |
751 | if (!endptr || *endptr) { | |
752 | ret = -1; | |
753 | } else { | |
754 | ret = 0; | |
755 | } | |
756 | ||
757 | end: | |
758 | return ret; | |
759 | } | |
760 | ||
761 | static | |
762 | int parse_int(xmlChar *str, int64_t *val) | |
763 | { | |
764 | int ret; | |
765 | char *endptr; | |
766 | ||
767 | if (!str || !val) { | |
768 | ret = -1; | |
769 | goto end; | |
770 | } | |
771 | ||
772 | *val = strtoll((const char *) str, &endptr, 10); | |
773 | if (!endptr || *endptr) { | |
774 | ret = -1; | |
775 | } else { | |
776 | ret = 0; | |
777 | } | |
778 | ||
779 | end: | |
780 | return ret; | |
781 | } | |
782 | ||
783 | static | |
784 | int parse_bool(xmlChar *str, int *val) | |
785 | { | |
786 | int ret = 0; | |
787 | ||
788 | if (!str || !val) { | |
789 | ret = -1; | |
790 | goto end; | |
791 | } | |
792 | ||
793 | if (!strcmp((const char *) str, config_xml_true)) { | |
794 | *val = 1; | |
795 | } else if (!strcmp((const char *) str, config_xml_false)) { | |
796 | *val = 0; | |
797 | } else { | |
798 | WARN("Invalid boolean value encoutered (%s).", | |
799 | (const char *) str); | |
800 | ret = -1; | |
801 | } | |
802 | end: | |
803 | return ret; | |
804 | } | |
805 | ||
806 | static | |
807 | int get_domain_type(xmlChar *domain) | |
808 | { | |
809 | int ret; | |
810 | ||
811 | if (!domain) { | |
812 | goto error; | |
813 | } | |
814 | ||
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; | |
5cdb6027 DG |
821 | } else if (!strcmp((char *) domain, config_domain_type_log4j)) { |
822 | ret = LTTNG_DOMAIN_LOG4J; | |
0e115563 DG |
823 | } else if (!strcmp((char *) domain, config_domain_type_python)) { |
824 | ret = LTTNG_DOMAIN_PYTHON; | |
dcf266c0 JG |
825 | } else { |
826 | goto error; | |
827 | } | |
828 | ||
829 | return ret; | |
830 | error: | |
831 | return -1; | |
832 | } | |
833 | ||
834 | static | |
835 | int get_buffer_type(xmlChar *buffer_type) | |
836 | { | |
837 | int ret; | |
838 | ||
839 | if (!buffer_type) { | |
840 | goto error; | |
841 | } | |
842 | ||
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; | |
849 | } else { | |
850 | goto error; | |
851 | } | |
852 | ||
853 | return ret; | |
854 | error: | |
855 | return -1; | |
856 | } | |
857 | ||
858 | static | |
859 | int get_overwrite_mode(xmlChar *overwrite_mode) | |
860 | { | |
861 | int ret; | |
862 | ||
863 | if (!overwrite_mode) { | |
864 | goto error; | |
865 | } | |
866 | ||
867 | if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) { | |
868 | ret = 1; | |
869 | } else if (!strcmp((char *) overwrite_mode, | |
870 | config_overwrite_mode_discard)) { | |
871 | ret = 0; | |
872 | } else { | |
873 | goto error; | |
874 | } | |
875 | ||
876 | return ret; | |
877 | error: | |
878 | return -1; | |
879 | } | |
880 | ||
881 | static | |
882 | int get_output_type(xmlChar *output_type) | |
883 | { | |
884 | int ret; | |
885 | ||
886 | if (!output_type) { | |
887 | goto error; | |
888 | } | |
889 | ||
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; | |
894 | } else { | |
895 | goto error; | |
896 | } | |
897 | ||
898 | return ret; | |
899 | error: | |
900 | return -1; | |
901 | } | |
902 | ||
903 | static | |
904 | int get_event_type(xmlChar *event_type) | |
905 | { | |
906 | int ret; | |
907 | ||
908 | if (!event_type) { | |
909 | goto error; | |
910 | } | |
911 | ||
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; | |
927 | } else { | |
928 | goto error; | |
929 | } | |
930 | ||
931 | return ret; | |
932 | error: | |
933 | return -1; | |
934 | } | |
935 | ||
936 | static | |
937 | int get_loglevel_type(xmlChar *loglevel_type) | |
938 | { | |
939 | int ret; | |
940 | ||
941 | if (!loglevel_type) { | |
942 | goto error; | |
943 | } | |
944 | ||
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; | |
953 | } else { | |
954 | goto error; | |
955 | } | |
956 | ||
957 | return ret; | |
958 | error: | |
959 | return -1; | |
960 | } | |
961 | ||
962 | /* | |
963 | * Return the context type or -1 on error. | |
964 | */ | |
965 | static | |
966 | int get_context_type(xmlChar *context_type) | |
967 | { | |
968 | int ret; | |
969 | ||
970 | if (!context_type) { | |
971 | goto error; | |
972 | } | |
973 | ||
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; | |
1ae5e83e JD |
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; | |
dcf266c0 JG |
1021 | } else { |
1022 | goto error; | |
1023 | } | |
1024 | ||
1025 | return ret; | |
1026 | error: | |
1027 | return -1; | |
1028 | } | |
1029 | ||
1030 | static | |
1031 | int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain) | |
1032 | { | |
1033 | int ret; | |
1034 | xmlNodePtr node; | |
1035 | ||
1036 | for (node = xmlFirstElementChild(domain_node); node; | |
1037 | node = xmlNextElementSibling(node)) { | |
1038 | if (!strcmp((const char *) node->name, config_element_type)) { | |
1039 | /* domain type */ | |
1040 | xmlChar *node_content = xmlNodeGetContent(node); | |
1041 | if (!node_content) { | |
1042 | ret = -LTTNG_ERR_NOMEM; | |
1043 | goto end; | |
1044 | } | |
1045 | ||
1046 | ret = get_domain_type(node_content); | |
1047 | free(node_content); | |
1048 | if (ret < 0) { | |
1049 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1050 | goto end; | |
1051 | } | |
1052 | ||
1053 | domain->type = ret; | |
1054 | } else if (!strcmp((const char *) node->name, | |
1055 | config_element_buffer_type)) { | |
1056 | /* buffer type */ | |
1057 | xmlChar *node_content = xmlNodeGetContent(node); | |
1058 | if (!node_content) { | |
1059 | ret = -LTTNG_ERR_NOMEM; | |
1060 | goto end; | |
1061 | } | |
1062 | ||
1063 | ret = get_buffer_type(node_content); | |
1064 | free(node_content); | |
1065 | if (ret < 0) { | |
1066 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1067 | goto end; | |
1068 | } | |
1069 | ||
1070 | domain->buf_type = ret; | |
1071 | } | |
1072 | } | |
1073 | ret = 0; | |
1074 | end: | |
1075 | return ret; | |
1076 | } | |
1077 | ||
1078 | static | |
1079 | int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri, | |
1080 | char **data_uri) | |
1081 | { | |
1082 | xmlNodePtr node; | |
1083 | ||
1084 | for (node = xmlFirstElementChild(net_output_node); node; | |
1085 | node = xmlNextElementSibling(node)) { | |
1086 | if (!strcmp((const char *) node->name, config_element_control_uri)) { | |
1087 | /* control_uri */ | |
1088 | *control_uri = (char *) xmlNodeGetContent(node); | |
1089 | if (!*control_uri) { | |
1090 | break; | |
1091 | } | |
1092 | } else { | |
1093 | /* data_uri */ | |
1094 | *data_uri = (char *) xmlNodeGetContent(node); | |
1095 | if (!*data_uri) { | |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1102 | } | |
1103 | ||
1104 | static | |
1105 | int process_consumer_output(xmlNodePtr consumer_output_node, | |
1106 | struct consumer_output *output) | |
1107 | { | |
1108 | int ret; | |
1109 | xmlNodePtr node; | |
1110 | ||
1111 | assert(output); | |
1112 | ||
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); | |
1117 | ||
1118 | /* enabled */ | |
1119 | if (!enabled_str) { | |
1120 | ret = -LTTNG_ERR_NOMEM; | |
1121 | goto end; | |
1122 | } | |
1123 | ||
1124 | ret = parse_bool(enabled_str, &output->enabled); | |
1125 | free(enabled_str); | |
1126 | if (ret) { | |
1127 | goto end; | |
1128 | } | |
1129 | } else { | |
1130 | xmlNodePtr output_type_node; | |
1131 | ||
1132 | /* destination */ | |
1133 | output_type_node = xmlFirstElementChild(node); | |
1134 | if (!output_type_node) { | |
1135 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1136 | goto end; | |
1137 | } | |
1138 | ||
1139 | if (!strcmp((const char *) output_type_node->name, | |
1140 | config_element_path)) { | |
1141 | /* path */ | |
1142 | output->path = (char *) xmlNodeGetContent(output_type_node); | |
1143 | if (!output->path) { | |
1144 | ret = -LTTNG_ERR_NOMEM; | |
1145 | goto end; | |
1146 | } | |
1147 | } else { | |
1148 | /* net_output */ | |
1149 | ret = get_net_output_uris(output_type_node, | |
1150 | &output->control_uri, &output->data_uri); | |
1151 | if (ret) { | |
1152 | goto end; | |
1153 | } | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | ret = 0; | |
1158 | ||
1159 | end: | |
1160 | if (ret) { | |
1161 | free(output->path); | |
1162 | free(output->control_uri); | |
1163 | free(output->data_uri); | |
1164 | memset(output, 0, sizeof(struct consumer_output)); | |
1165 | } | |
1166 | return ret; | |
1167 | } | |
1168 | ||
1169 | static | |
95681498 JG |
1170 | int create_session_net_output(const char *name, const char *control_uri, |
1171 | const char *data_uri) | |
dcf266c0 JG |
1172 | { |
1173 | int ret; | |
1174 | struct lttng_handle *handle; | |
1175 | const char *uri = NULL; | |
1176 | ||
1177 | assert(name); | |
dcf266c0 | 1178 | |
95681498 | 1179 | handle = lttng_create_handle(name, NULL); |
dcf266c0 JG |
1180 | if (!handle) { |
1181 | ret = -LTTNG_ERR_NOMEM; | |
1182 | goto end; | |
1183 | } | |
1184 | ||
1185 | if (!control_uri || !data_uri) { | |
1186 | uri = control_uri ? control_uri : data_uri; | |
1187 | control_uri = uri; | |
1188 | data_uri = uri; | |
1189 | } | |
1190 | ||
1191 | ret = lttng_set_consumer_url(handle, control_uri, data_uri); | |
1192 | lttng_destroy_handle(handle); | |
1193 | end: | |
1194 | return ret; | |
1195 | } | |
1196 | ||
1197 | static | |
1b08cbce JR |
1198 | int create_snapshot_session(const char *session_name, xmlNodePtr output_node, |
1199 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
1200 | { |
1201 | int ret; | |
1202 | xmlNodePtr node = NULL; | |
1203 | xmlNodePtr snapshot_output_list_node; | |
1204 | xmlNodePtr snapshot_output_node; | |
1205 | ||
1206 | assert(session_name); | |
1207 | ||
1208 | ret = lttng_create_session_snapshot(session_name, NULL); | |
1209 | if (ret) { | |
1210 | goto end; | |
1211 | } | |
1212 | ||
1213 | if (!output_node) { | |
1214 | goto end; | |
1215 | } | |
1216 | ||
1217 | snapshot_output_list_node = xmlFirstElementChild(output_node); | |
1218 | ||
1219 | /* Parse and create snapshot outputs */ | |
1220 | ||
1221 | for (snapshot_output_node = | |
1222 | xmlFirstElementChild(snapshot_output_list_node); | |
1223 | snapshot_output_node; snapshot_output_node = | |
1224 | xmlNextElementSibling(snapshot_output_node)) { | |
1225 | char *name = NULL; | |
1226 | uint64_t max_size = UINT64_MAX; | |
1227 | struct consumer_output output = { 0 }; | |
1228 | struct lttng_snapshot_output *snapshot_output = NULL; | |
1b08cbce JR |
1229 | const char *control_uri = NULL; |
1230 | const char *data_uri = NULL; | |
1231 | const char *path = NULL; | |
dcf266c0 JG |
1232 | |
1233 | for (node = xmlFirstElementChild(snapshot_output_node); node; | |
1234 | node = xmlNextElementSibling(node)) { | |
1235 | if (!strcmp((const char *) node->name, | |
1236 | config_element_name)) { | |
1237 | /* name */ | |
1238 | name = (char *) xmlNodeGetContent(node); | |
1239 | if (!name) { | |
1240 | ret = -LTTNG_ERR_NOMEM; | |
1241 | goto error_snapshot_output; | |
1242 | } | |
1243 | } else if (!strcmp((const char *) node->name, | |
1244 | config_element_max_size)) { | |
1245 | xmlChar *content = xmlNodeGetContent(node); | |
1246 | ||
1247 | /* max_size */ | |
1248 | if (!content) { | |
1249 | ret = -LTTNG_ERR_NOMEM; | |
1250 | goto error_snapshot_output; | |
1251 | } | |
1252 | ret = parse_uint(content, &max_size); | |
1253 | free(content); | |
1254 | if (ret) { | |
1255 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1256 | goto error_snapshot_output; | |
1257 | } | |
1258 | } else { | |
1259 | /* consumer_output */ | |
1260 | ret = process_consumer_output(node, &output); | |
1261 | if (ret) { | |
1262 | goto error_snapshot_output; | |
1263 | } | |
1264 | } | |
1265 | } | |
1266 | ||
21a3f144 JR |
1267 | control_uri = output.control_uri; |
1268 | data_uri = output.data_uri; | |
1269 | path = output.path; | |
1270 | ||
1b08cbce JR |
1271 | if (overrides) { |
1272 | if (overrides->path_url) { | |
1b08cbce | 1273 | path = overrides->path_url; |
21a3f144 JR |
1274 | /* Control/data_uri are null */ |
1275 | control_uri = NULL; | |
1276 | data_uri = NULL; | |
1b08cbce JR |
1277 | } else { |
1278 | if (overrides->ctrl_url) { | |
1b08cbce | 1279 | control_uri = overrides->ctrl_url; |
21a3f144 JR |
1280 | /* path is null */ |
1281 | path = NULL; | |
1b08cbce JR |
1282 | } |
1283 | if (overrides->data_url) { | |
1b08cbce | 1284 | data_uri = overrides->data_url; |
21a3f144 JR |
1285 | /* path is null */ |
1286 | path = NULL; | |
1b08cbce JR |
1287 | } |
1288 | } | |
1b08cbce JR |
1289 | } |
1290 | ||
dcf266c0 JG |
1291 | snapshot_output = lttng_snapshot_output_create(); |
1292 | if (!snapshot_output) { | |
1293 | ret = -LTTNG_ERR_NOMEM; | |
1294 | goto error_snapshot_output; | |
1295 | } | |
1296 | ||
1297 | ret = lttng_snapshot_output_set_name(name, snapshot_output); | |
1298 | if (ret) { | |
1299 | goto error_snapshot_output; | |
1300 | } | |
1301 | ||
1302 | ret = lttng_snapshot_output_set_size(max_size, snapshot_output); | |
1303 | if (ret) { | |
1304 | goto error_snapshot_output; | |
1305 | } | |
1306 | ||
1b08cbce JR |
1307 | if (path) { |
1308 | ret = lttng_snapshot_output_set_ctrl_url(path, | |
dcf266c0 JG |
1309 | snapshot_output); |
1310 | if (ret) { | |
1311 | goto error_snapshot_output; | |
1312 | } | |
1313 | } else { | |
1b08cbce JR |
1314 | if (control_uri) { |
1315 | ret = lttng_snapshot_output_set_ctrl_url(control_uri, | |
dcf266c0 JG |
1316 | snapshot_output); |
1317 | if (ret) { | |
1318 | goto error_snapshot_output; | |
1319 | } | |
1320 | } | |
1321 | ||
1b08cbce JR |
1322 | if (data_uri) { |
1323 | ret = lttng_snapshot_output_set_data_url(data_uri, | |
dcf266c0 JG |
1324 | snapshot_output); |
1325 | if (ret) { | |
1326 | goto error_snapshot_output; | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | ||
1331 | ret = lttng_snapshot_add_output(session_name, snapshot_output); | |
1332 | error_snapshot_output: | |
1333 | free(name); | |
1334 | free(output.path); | |
1335 | free(output.control_uri); | |
1336 | free(output.data_uri); | |
1337 | lttng_snapshot_output_destroy(snapshot_output); | |
1338 | if (ret) { | |
1339 | goto end; | |
1340 | } | |
1341 | } | |
1342 | end: | |
1343 | return ret; | |
1344 | } | |
1345 | ||
1346 | static | |
1347 | int create_session(const char *name, | |
dcf266c0 | 1348 | xmlNodePtr output_node, |
1b08cbce JR |
1349 | uint64_t live_timer_interval, |
1350 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
1351 | { |
1352 | int ret; | |
1353 | struct consumer_output output = { 0 }; | |
1354 | xmlNodePtr consumer_output_node; | |
1b08cbce JR |
1355 | const char *control_uri = NULL; |
1356 | const char *data_uri = NULL; | |
1357 | const char *path = NULL; | |
dcf266c0 JG |
1358 | |
1359 | assert(name); | |
dcf266c0 JG |
1360 | |
1361 | if (output_node) { | |
1362 | consumer_output_node = xmlFirstElementChild(output_node); | |
1363 | if (!consumer_output_node) { | |
1364 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1365 | goto end; | |
1366 | } | |
1367 | ||
1368 | if (strcmp((const char *) consumer_output_node->name, | |
1369 | config_element_consumer_output)) { | |
1370 | WARN("Invalid output type, expected %s node", | |
1371 | config_element_consumer_output); | |
1372 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1373 | goto end; | |
1374 | } | |
1375 | ||
1376 | ret = process_consumer_output(consumer_output_node, &output); | |
1377 | if (ret) { | |
1378 | goto end; | |
1379 | } | |
1380 | } | |
1381 | ||
21a3f144 JR |
1382 | control_uri = output.control_uri; |
1383 | data_uri = output.data_uri; | |
1384 | path = output.path; | |
1385 | ||
1b08cbce JR |
1386 | /* Check for override and apply them */ |
1387 | if (overrides) { | |
1388 | if (overrides->path_url) { | |
1b08cbce | 1389 | path = overrides->path_url; |
21a3f144 JR |
1390 | /* control/data_uri are null */; |
1391 | control_uri = NULL; | |
1392 | data_uri = NULL; | |
1b08cbce JR |
1393 | } else { |
1394 | if (overrides->ctrl_url) { | |
1b08cbce | 1395 | control_uri = overrides->ctrl_url; |
21a3f144 JR |
1396 | /* path is null */ |
1397 | path = NULL; | |
1b08cbce JR |
1398 | } |
1399 | if (overrides->data_url) { | |
1b08cbce | 1400 | data_uri = overrides->data_url; |
21a3f144 JR |
1401 | /* path is null */ |
1402 | path = NULL; | |
1b08cbce JR |
1403 | } |
1404 | } | |
1b08cbce JR |
1405 | } |
1406 | ||
21a3f144 | 1407 | |
1b08cbce | 1408 | if (live_timer_interval != UINT64_MAX && !control_uri && !data_uri) { |
dcf266c0 JG |
1409 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
1410 | goto end; | |
1411 | } | |
1412 | ||
1b08cbce | 1413 | if (control_uri || data_uri) { |
dcf266c0 JG |
1414 | /* network destination */ |
1415 | if (live_timer_interval && live_timer_interval != UINT64_MAX) { | |
b664f89a DG |
1416 | /* |
1417 | * URLs are provided for sure since the test above make sure that | |
1418 | * with a live timer the data and control URIs are provided. So, | |
1419 | * NULL is passed here and will be set right after. | |
1420 | */ | |
1421 | ret = lttng_create_session_live(name, NULL, live_timer_interval); | |
dcf266c0 JG |
1422 | } else { |
1423 | ret = lttng_create_session(name, NULL); | |
1424 | } | |
1425 | if (ret) { | |
1426 | goto end; | |
1427 | } | |
1428 | ||
1b08cbce | 1429 | ret = create_session_net_output(name, control_uri, data_uri); |
95681498 JG |
1430 | if (ret) { |
1431 | goto end; | |
dcf266c0 | 1432 | } |
95681498 | 1433 | |
dcf266c0 JG |
1434 | } else { |
1435 | /* either local output or no output */ | |
1b08cbce | 1436 | ret = lttng_create_session(name, path); |
dcf266c0 JG |
1437 | if (ret) { |
1438 | goto end; | |
1439 | } | |
1440 | } | |
1441 | end: | |
1442 | free(output.path); | |
1443 | free(output.control_uri); | |
1444 | free(output.data_uri); | |
1445 | return ret; | |
1446 | } | |
1447 | static | |
1448 | int process_probe_attribute_node(xmlNodePtr probe_attribute_node, | |
1449 | struct lttng_event_probe_attr *attr) | |
1450 | { | |
1451 | int ret; | |
1452 | ||
1453 | assert(probe_attribute_node); | |
1454 | assert(attr); | |
1455 | ||
1456 | if (!strcmp((const char *) probe_attribute_node->name, | |
1457 | config_element_address)) { | |
1458 | xmlChar *content; | |
1459 | uint64_t addr = 0; | |
1460 | ||
1461 | /* addr */ | |
1462 | content = xmlNodeGetContent(probe_attribute_node); | |
1463 | if (!content) { | |
1464 | ret = -LTTNG_ERR_NOMEM; | |
1465 | goto end; | |
1466 | } | |
1467 | ||
1468 | ret = parse_uint(content, &addr); | |
1469 | free(content); | |
1470 | if (ret) { | |
1471 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1472 | goto end; | |
1473 | } | |
1474 | ||
1475 | attr->addr = addr; | |
1476 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1477 | config_element_offset)) { | |
1478 | xmlChar *content; | |
1479 | uint64_t offset = 0; | |
1480 | ||
1481 | /* offset */ | |
1482 | content = xmlNodeGetContent(probe_attribute_node); | |
1483 | if (!content) { | |
1484 | ret = -LTTNG_ERR_NOMEM; | |
1485 | goto end; | |
1486 | } | |
1487 | ||
1488 | ret = parse_uint(content, &offset); | |
1489 | free(content); | |
1490 | if (ret) { | |
1491 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1492 | goto end; | |
1493 | } | |
1494 | ||
1495 | attr->offset = offset; | |
1496 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1497 | config_element_symbol_name)) { | |
1498 | xmlChar *content; | |
dcf266c0 JG |
1499 | |
1500 | /* symbol_name */ | |
1501 | content = xmlNodeGetContent(probe_attribute_node); | |
1502 | if (!content) { | |
1503 | ret = -LTTNG_ERR_NOMEM; | |
1504 | goto end; | |
1505 | } | |
1506 | ||
d2e67842 JG |
1507 | ret = lttng_strncpy(attr->symbol_name, |
1508 | (const char *) content, | |
1509 | LTTNG_SYMBOL_NAME_LEN); | |
1510 | if (ret == -1) { | |
1511 | ERR("symbol name \"%s\"'s length (%zu) exceeds the maximal permitted length (%d) in session configuration", | |
1512 | (const char *) content, | |
1513 | strlen((const char *) content), | |
1514 | LTTNG_SYMBOL_NAME_LEN); | |
dcf266c0 JG |
1515 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
1516 | free(content); | |
1517 | goto end; | |
1518 | } | |
dcf266c0 JG |
1519 | free(content); |
1520 | } | |
1521 | ret = 0; | |
1522 | end: | |
1523 | return ret; | |
1524 | } | |
1525 | ||
1526 | static | |
1527 | int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, | |
d7b645e2 | 1528 | const char *channel_name, const enum process_event_node_phase phase) |
dcf266c0 | 1529 | { |
d7b645e2 | 1530 | int ret = 0, i; |
dcf266c0 JG |
1531 | xmlNodePtr node; |
1532 | struct lttng_event event; | |
1533 | char **exclusions = NULL; | |
1534 | unsigned long exclusion_count = 0; | |
1535 | char *filter_expression = NULL; | |
1536 | ||
1537 | assert(event_node); | |
1538 | assert(handle); | |
1539 | assert(channel_name); | |
1540 | ||
1541 | memset(&event, 0, sizeof(event)); | |
1542 | ||
f40eba3d JG |
1543 | /* Initialize default log level which varies by domain */ |
1544 | switch (handle->domain.type) | |
1545 | { | |
1546 | case LTTNG_DOMAIN_JUL: | |
1547 | event.loglevel = LTTNG_LOGLEVEL_JUL_ALL; | |
1548 | break; | |
1549 | case LTTNG_DOMAIN_LOG4J: | |
1550 | event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL; | |
1551 | break; | |
1552 | case LTTNG_DOMAIN_PYTHON: | |
1553 | event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG; | |
1554 | break; | |
1555 | case LTTNG_DOMAIN_UST: | |
1556 | case LTTNG_DOMAIN_KERNEL: | |
1557 | event.loglevel = LTTNG_LOGLEVEL_DEBUG; | |
1558 | break; | |
1559 | default: | |
1560 | assert(0); | |
1561 | } | |
1562 | ||
dcf266c0 JG |
1563 | for (node = xmlFirstElementChild(event_node); node; |
1564 | node = xmlNextElementSibling(node)) { | |
1565 | if (!strcmp((const char *) node->name, config_element_name)) { | |
1566 | xmlChar *content; | |
dcf266c0 JG |
1567 | |
1568 | /* name */ | |
1569 | content = xmlNodeGetContent(node); | |
1570 | if (!content) { | |
1571 | ret = -LTTNG_ERR_NOMEM; | |
1572 | goto end; | |
1573 | } | |
1574 | ||
d2e67842 JG |
1575 | ret = lttng_strncpy(event.name, |
1576 | (const char *) content, | |
1577 | LTTNG_SYMBOL_NAME_LEN); | |
1578 | if (ret == -1) { | |
1579 | WARN("Event \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration", | |
1580 | (const char *) content, | |
1581 | strlen((const char *) content), | |
1582 | LTTNG_SYMBOL_NAME_LEN); | |
dcf266c0 JG |
1583 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
1584 | free(content); | |
1585 | goto end; | |
1586 | } | |
dcf266c0 JG |
1587 | free(content); |
1588 | } else if (!strcmp((const char *) node->name, | |
1589 | config_element_enabled)) { | |
1590 | xmlChar *content = xmlNodeGetContent(node); | |
1591 | ||
1592 | /* enabled */ | |
1593 | if (!content) { | |
1594 | ret = -LTTNG_ERR_NOMEM; | |
1595 | goto end; | |
1596 | } | |
1597 | ||
1598 | ret = parse_bool(content, &event.enabled); | |
1599 | free(content); | |
1600 | if (ret) { | |
1601 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1602 | goto end; | |
1603 | } | |
1604 | } else if (!strcmp((const char *) node->name, | |
1605 | config_element_type)) { | |
1606 | xmlChar *content = xmlNodeGetContent(node); | |
1607 | ||
1608 | /* type */ | |
1609 | if (!content) { | |
1610 | ret = -LTTNG_ERR_NOMEM; | |
1611 | goto end; | |
1612 | } | |
1613 | ||
1614 | ret = get_event_type(content); | |
1615 | free(content); | |
1616 | if (ret < 0) { | |
1617 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1618 | goto end; | |
1619 | } | |
1620 | ||
1621 | event.type = ret; | |
1622 | } else if (!strcmp((const char *) node->name, | |
1623 | config_element_loglevel_type)) { | |
1624 | xmlChar *content = xmlNodeGetContent(node); | |
1625 | ||
1626 | /* loglevel_type */ | |
1627 | if (!content) { | |
1628 | ret = -LTTNG_ERR_NOMEM; | |
1629 | goto end; | |
1630 | } | |
1631 | ||
1632 | ret = get_loglevel_type(content); | |
1633 | free(content); | |
1634 | if (ret < 0) { | |
1635 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1636 | goto end; | |
1637 | } | |
1638 | ||
1639 | event.loglevel_type = ret; | |
1640 | } else if (!strcmp((const char *) node->name, | |
1641 | config_element_loglevel)) { | |
1642 | xmlChar *content; | |
1643 | int64_t loglevel = 0; | |
1644 | ||
1645 | /* loglevel */ | |
1646 | content = xmlNodeGetContent(node); | |
1647 | if (!content) { | |
1648 | ret = -LTTNG_ERR_NOMEM; | |
1649 | goto end; | |
1650 | } | |
1651 | ||
1652 | ret = parse_int(content, &loglevel); | |
1653 | free(content); | |
1654 | if (ret) { | |
1655 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1656 | goto end; | |
1657 | } | |
1658 | ||
1659 | if (loglevel > INT_MAX || loglevel < INT_MIN) { | |
1660 | WARN("loglevel out of range."); | |
1661 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1662 | goto end; | |
1663 | } | |
1664 | ||
1665 | event.loglevel = loglevel; | |
1666 | } else if (!strcmp((const char *) node->name, | |
1667 | config_element_filter)) { | |
1668 | xmlChar *content = | |
1669 | xmlNodeGetContent(node); | |
1670 | ||
1671 | /* filter */ | |
1672 | if (!content) { | |
1673 | ret = -LTTNG_ERR_NOMEM; | |
1674 | goto end; | |
1675 | } | |
1676 | ||
02d8ac3d | 1677 | free(filter_expression); |
dcf266c0 JG |
1678 | filter_expression = strdup((char *) content); |
1679 | free(content); | |
1680 | if (!filter_expression) { | |
1681 | ret = -LTTNG_ERR_NOMEM; | |
1682 | goto end; | |
1683 | } | |
1684 | } else if (!strcmp((const char *) node->name, | |
1685 | config_element_exclusions)) { | |
1686 | xmlNodePtr exclusion_node; | |
1687 | int exclusion_index = 0; | |
1688 | ||
1689 | /* exclusions */ | |
1690 | if (exclusions) { | |
1691 | /* | |
1692 | * Exclusions has already been initialized, | |
1693 | * invalid file. | |
1694 | */ | |
1695 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1696 | goto end; | |
1697 | } | |
1698 | ||
1699 | exclusion_count = xmlChildElementCount(node); | |
1700 | if (!exclusion_count) { | |
1701 | continue; | |
1702 | } | |
1703 | ||
1704 | exclusions = zmalloc(exclusion_count * sizeof(char *)); | |
1705 | if (!exclusions) { | |
1706 | exclusion_count = 0; | |
1707 | ret = -LTTNG_ERR_NOMEM; | |
1708 | goto end; | |
1709 | } | |
1710 | ||
1711 | for (exclusion_node = xmlFirstElementChild(node); exclusion_node; | |
1712 | exclusion_node = xmlNextElementSibling(exclusion_node)) { | |
1713 | xmlChar *content = | |
1714 | xmlNodeGetContent(exclusion_node); | |
1715 | ||
1716 | if (!content) { | |
1717 | ret = -LTTNG_ERR_NOMEM; | |
1718 | goto end; | |
1719 | } | |
1720 | ||
1721 | exclusions[exclusion_index] = strdup((const char *) content); | |
1722 | free(content); | |
1723 | if (!exclusions[exclusion_index]) { | |
1724 | ret = -LTTNG_ERR_NOMEM; | |
1725 | goto end; | |
1726 | } | |
1727 | exclusion_index++; | |
1728 | } | |
1729 | ||
1730 | event.exclusion = 1; | |
1731 | } else if (!strcmp((const char *) node->name, | |
1732 | config_element_attributes)) { | |
1733 | xmlNodePtr attribute_node = xmlFirstElementChild(node); | |
1734 | ||
1735 | /* attributes */ | |
1736 | if (!attribute_node) { | |
1737 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1738 | goto end; | |
1739 | } | |
1740 | ||
1741 | if (!strcmp((const char *) node->name, | |
1742 | config_element_probe_attributes)) { | |
1743 | xmlNodePtr probe_attribute_node; | |
1744 | ||
1745 | /* probe_attributes */ | |
1746 | for (probe_attribute_node = | |
1747 | xmlFirstElementChild(attribute_node); probe_attribute_node; | |
1748 | probe_attribute_node = xmlNextElementSibling( | |
1749 | probe_attribute_node)) { | |
1750 | ||
1751 | ret = process_probe_attribute_node(probe_attribute_node, | |
1752 | &event.attr.probe); | |
1753 | if (ret) { | |
1754 | goto end; | |
1755 | } | |
1756 | } | |
1757 | } else { | |
1758 | size_t sym_len; | |
1759 | xmlChar *content; | |
1760 | xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node); | |
1761 | ||
1762 | /* function_attributes */ | |
1763 | content = xmlNodeGetContent(symbol_node); | |
1764 | if (!content) { | |
1765 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1766 | goto end; | |
1767 | } | |
1768 | ||
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; | |
1773 | free(content); | |
1774 | goto end; | |
1775 | } | |
1776 | ||
d2e67842 JG |
1777 | ret = lttng_strncpy( |
1778 | event.attr.ftrace.symbol_name, | |
1779 | (char *) content, sym_len); | |
1780 | if (ret == -1) { | |
1781 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1782 | free(content); | |
1783 | goto end; | |
1784 | } | |
dcf266c0 JG |
1785 | free(content); |
1786 | } | |
1787 | } | |
1788 | } | |
1789 | ||
d7b645e2 JR |
1790 | if ((event.enabled && phase == ENABLE) || phase == CREATION) { |
1791 | ret = lttng_enable_event_with_exclusions(handle, &event, channel_name, | |
1792 | filter_expression, exclusion_count, exclusions); | |
1793 | if (ret < 0) { | |
1794 | WARN("Enabling event (name:%s) on load failed.", event.name); | |
1795 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1796 | goto end; | |
1797 | } | |
065321e9 | 1798 | } |
d2e67842 | 1799 | ret = 0; |
dcf266c0 JG |
1800 | end: |
1801 | for (i = 0; i < exclusion_count; i++) { | |
1802 | free(exclusions[i]); | |
1803 | } | |
1804 | ||
1805 | free(exclusions); | |
1806 | free(filter_expression); | |
1807 | return ret; | |
1808 | } | |
1809 | ||
1810 | static | |
1811 | int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle, | |
1812 | const char *channel_name) | |
1813 | { | |
1814 | int ret = 0; | |
d7b645e2 | 1815 | struct lttng_event event; |
dcf266c0 JG |
1816 | xmlNodePtr node; |
1817 | ||
1818 | assert(events_node); | |
1819 | assert(handle); | |
1820 | assert(channel_name); | |
1821 | ||
1822 | for (node = xmlFirstElementChild(events_node); node; | |
1823 | node = xmlNextElementSibling(node)) { | |
d7b645e2 | 1824 | ret = process_event_node(node, handle, channel_name, CREATION); |
dcf266c0 JG |
1825 | if (ret) { |
1826 | goto end; | |
1827 | } | |
1828 | } | |
d7b645e2 JR |
1829 | |
1830 | /* | |
1831 | * Disable all events to enable only the necessary events. | |
1832 | * Limitations regarding lttng_disable_events and tuple descriptor | |
1833 | * force this approach. | |
1834 | */ | |
1835 | memset(&event, 0, sizeof(event)); | |
1836 | event.loglevel = -1; | |
1837 | event.type = LTTNG_EVENT_ALL; | |
1838 | ret = lttng_disable_event_ext(handle, &event, channel_name, NULL); | |
1839 | if (ret) { | |
1840 | goto end; | |
1841 | } | |
1842 | ||
1843 | for (node = xmlFirstElementChild(events_node); node; | |
1844 | node = xmlNextElementSibling(node)) { | |
1845 | ret = process_event_node(node, handle, channel_name, ENABLE); | |
1846 | if (ret) { | |
1847 | goto end; | |
1848 | } | |
1849 | } | |
1850 | ||
dcf266c0 JG |
1851 | end: |
1852 | return ret; | |
1853 | } | |
1854 | ||
1855 | static | |
1856 | int process_channel_attr_node(xmlNodePtr attr_node, | |
1857 | struct lttng_channel *channel, xmlNodePtr *contexts_node, | |
1858 | xmlNodePtr *events_node) | |
1859 | { | |
1860 | int ret; | |
1861 | ||
1862 | assert(attr_node); | |
1863 | assert(channel); | |
1864 | assert(contexts_node); | |
1865 | assert(events_node); | |
1866 | ||
1867 | if (!strcmp((const char *) attr_node->name, config_element_name)) { | |
1868 | xmlChar *content; | |
dcf266c0 JG |
1869 | |
1870 | /* name */ | |
1871 | content = xmlNodeGetContent(attr_node); | |
1872 | if (!content) { | |
1873 | ret = -LTTNG_ERR_NOMEM; | |
1874 | goto end; | |
1875 | } | |
1876 | ||
d2e67842 JG |
1877 | ret = lttng_strncpy(channel->name, |
1878 | (const char *) content, | |
1879 | LTTNG_SYMBOL_NAME_LEN); | |
1880 | if (ret == -1) { | |
1881 | WARN("Channel \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration", | |
1882 | (const char *) content, | |
1883 | strlen((const char *) content), | |
1884 | LTTNG_SYMBOL_NAME_LEN); | |
dcf266c0 JG |
1885 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
1886 | free(content); | |
1887 | goto end; | |
1888 | } | |
dcf266c0 JG |
1889 | free(content); |
1890 | } else if (!strcmp((const char *) attr_node->name, | |
1891 | config_element_enabled)) { | |
1892 | xmlChar *content; | |
1893 | int enabled; | |
1894 | ||
1895 | /* enabled */ | |
1896 | content = xmlNodeGetContent(attr_node); | |
1897 | if (!content) { | |
1898 | ret = -LTTNG_ERR_NOMEM; | |
1899 | goto end; | |
1900 | } | |
1901 | ||
1902 | ret = parse_bool(content, &enabled); | |
1903 | free(content); | |
1904 | if (ret) { | |
1905 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1906 | goto end; | |
1907 | } | |
1908 | ||
1909 | channel->enabled = enabled; | |
1910 | } else if (!strcmp((const char *) attr_node->name, | |
1911 | config_element_overwrite_mode)) { | |
1912 | xmlChar *content; | |
1913 | ||
1914 | /* overwrite_mode */ | |
1915 | content = xmlNodeGetContent(attr_node); | |
1916 | if (!content) { | |
1917 | ret = -LTTNG_ERR_NOMEM; | |
1918 | goto end; | |
1919 | } | |
1920 | ||
1921 | ret = get_overwrite_mode(content); | |
1922 | free(content); | |
1923 | if (ret < 0) { | |
1924 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1925 | goto end; | |
1926 | } | |
1927 | ||
1928 | channel->attr.overwrite = ret; | |
1929 | } else if (!strcmp((const char *) attr_node->name, | |
1930 | config_element_subbuf_size)) { | |
1931 | xmlChar *content; | |
1932 | ||
1933 | /* subbuffer_size */ | |
1934 | content = xmlNodeGetContent(attr_node); | |
1935 | if (!content) { | |
1936 | ret = -LTTNG_ERR_NOMEM; | |
1937 | goto end; | |
1938 | } | |
1939 | ||
1940 | ret = parse_uint(content, &channel->attr.subbuf_size); | |
1941 | free(content); | |
1942 | if (ret) { | |
1943 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1944 | goto end; | |
1945 | } | |
1946 | } else if (!strcmp((const char *) attr_node->name, | |
1947 | config_element_num_subbuf)) { | |
1948 | xmlChar *content; | |
1949 | ||
1950 | /* subbuffer_count */ | |
1951 | content = xmlNodeGetContent(attr_node); | |
1952 | if (!content) { | |
1953 | ret = -LTTNG_ERR_NOMEM; | |
1954 | goto end; | |
1955 | } | |
1956 | ||
1957 | ret = parse_uint(content, &channel->attr.num_subbuf); | |
1958 | free(content); | |
1959 | if (ret) { | |
1960 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1961 | goto end; | |
1962 | } | |
1963 | } else if (!strcmp((const char *) attr_node->name, | |
1964 | config_element_switch_timer_interval)) { | |
1965 | xmlChar *content; | |
1966 | uint64_t switch_timer_interval = 0; | |
1967 | ||
1968 | /* switch_timer_interval */ | |
1969 | content = xmlNodeGetContent(attr_node); | |
1970 | if (!content) { | |
1971 | ret = -LTTNG_ERR_NOMEM; | |
1972 | goto end; | |
1973 | } | |
1974 | ||
1975 | ret = parse_uint(content, &switch_timer_interval); | |
1976 | free(content); | |
1977 | if (ret) { | |
1978 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1979 | goto end; | |
1980 | } | |
1981 | ||
1982 | if (switch_timer_interval > UINT_MAX) { | |
1983 | WARN("switch_timer_interval out of range."); | |
1984 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1985 | goto end; | |
1986 | } | |
1987 | ||
1988 | channel->attr.switch_timer_interval = | |
1989 | switch_timer_interval; | |
1990 | } else if (!strcmp((const char *) attr_node->name, | |
1991 | config_element_read_timer_interval)) { | |
1992 | xmlChar *content; | |
1993 | uint64_t read_timer_interval = 0; | |
1994 | ||
1995 | /* read_timer_interval */ | |
1996 | content = xmlNodeGetContent(attr_node); | |
1997 | if (!content) { | |
1998 | ret = -LTTNG_ERR_NOMEM; | |
1999 | goto end; | |
2000 | } | |
2001 | ||
2002 | ret = parse_uint(content, &read_timer_interval); | |
2003 | free(content); | |
2004 | if (ret) { | |
2005 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2006 | goto end; | |
2007 | } | |
2008 | ||
2009 | if (read_timer_interval > UINT_MAX) { | |
2010 | WARN("read_timer_interval out of range."); | |
2011 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2012 | goto end; | |
2013 | } | |
2014 | ||
2015 | channel->attr.read_timer_interval = | |
2016 | read_timer_interval; | |
2017 | } else if (!strcmp((const char *) attr_node->name, | |
2018 | config_element_output_type)) { | |
2019 | xmlChar *content; | |
2020 | ||
2021 | /* output_type */ | |
2022 | content = xmlNodeGetContent(attr_node); | |
2023 | if (!content) { | |
2024 | ret = -LTTNG_ERR_NOMEM; | |
2025 | goto end; | |
2026 | } | |
2027 | ||
2028 | ret = get_output_type(content); | |
2029 | free(content); | |
2030 | if (ret < 0) { | |
2031 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2032 | goto end; | |
2033 | } | |
2034 | ||
2035 | channel->attr.output = ret; | |
2036 | } else if (!strcmp((const char *) attr_node->name, | |
2037 | config_element_tracefile_size)) { | |
2038 | xmlChar *content; | |
2039 | ||
2040 | /* tracefile_size */ | |
2041 | content = xmlNodeGetContent(attr_node); | |
2042 | if (!content) { | |
2043 | ret = -LTTNG_ERR_NOMEM; | |
2044 | goto end; | |
2045 | } | |
2046 | ||
2047 | ret = parse_uint(content, &channel->attr.tracefile_size); | |
2048 | free(content); | |
2049 | if (ret) { | |
2050 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2051 | goto end; | |
2052 | } | |
2053 | } else if (!strcmp((const char *) attr_node->name, | |
2054 | config_element_tracefile_count)) { | |
2055 | xmlChar *content; | |
2056 | ||
2057 | /* tracefile_count */ | |
2058 | content = xmlNodeGetContent(attr_node); | |
2059 | if (!content) { | |
2060 | ret = -LTTNG_ERR_NOMEM; | |
2061 | goto end; | |
2062 | } | |
2063 | ||
2064 | ret = parse_uint(content, &channel->attr.tracefile_count); | |
2065 | free(content); | |
2066 | if (ret) { | |
2067 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2068 | goto end; | |
2069 | } | |
2070 | } else if (!strcmp((const char *) attr_node->name, | |
2071 | config_element_live_timer_interval)) { | |
2072 | xmlChar *content; | |
2073 | uint64_t live_timer_interval = 0; | |
2074 | ||
2075 | /* live_timer_interval */ | |
2076 | content = xmlNodeGetContent(attr_node); | |
2077 | if (!content) { | |
2078 | ret = -LTTNG_ERR_NOMEM; | |
2079 | goto end; | |
2080 | } | |
2081 | ||
2082 | ret = parse_uint(content, &live_timer_interval); | |
2083 | free(content); | |
2084 | if (ret) { | |
2085 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2086 | goto end; | |
2087 | } | |
2088 | ||
2089 | if (live_timer_interval > UINT_MAX) { | |
2090 | WARN("live_timer_interval out of range."); | |
2091 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2092 | goto end; | |
2093 | } | |
2094 | ||
2095 | channel->attr.live_timer_interval = | |
2096 | live_timer_interval; | |
4fc2b126 JR |
2097 | } else if (!strcmp((const char *) attr_node->name, |
2098 | config_element_monitor_timer_interval)) { | |
2099 | xmlChar *content; | |
2100 | uint64_t monitor_timer_interval = 0; | |
2101 | ||
2102 | /* monitor_timer_interval */ | |
2103 | content = xmlNodeGetContent(attr_node); | |
2104 | if (!content) { | |
2105 | ret = -LTTNG_ERR_NOMEM; | |
2106 | goto end; | |
2107 | } | |
2108 | ||
2109 | ret = parse_uint(content, &monitor_timer_interval); | |
2110 | free(content); | |
2111 | if (ret) { | |
2112 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2113 | goto end; | |
2114 | } | |
2115 | ||
2116 | ret = lttng_channel_set_monitor_timer_interval(channel, | |
2117 | monitor_timer_interval); | |
2118 | if (ret) { | |
2119 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2120 | goto end; | |
2121 | } | |
275472aa JR |
2122 | } else if (!strcmp((const char *) attr_node->name, |
2123 | config_element_blocking_timeout)) { | |
2124 | xmlChar *content; | |
2125 | int64_t blocking_timeout = 0; | |
2126 | ||
2127 | /* blocking_timeout */ | |
2128 | content = xmlNodeGetContent(attr_node); | |
2129 | if (!content) { | |
2130 | ret = -LTTNG_ERR_NOMEM; | |
2131 | goto end; | |
2132 | } | |
2133 | ||
2134 | ret = parse_int(content, &blocking_timeout); | |
2135 | free(content); | |
2136 | if (ret) { | |
2137 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2138 | goto end; | |
2139 | } | |
2140 | ||
2141 | ret = lttng_channel_set_blocking_timeout(channel, | |
2142 | blocking_timeout); | |
2143 | if (ret) { | |
2144 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2145 | goto end; | |
2146 | } | |
dcf266c0 JG |
2147 | } else if (!strcmp((const char *) attr_node->name, |
2148 | config_element_events)) { | |
2149 | /* events */ | |
2150 | *events_node = attr_node; | |
2151 | } else { | |
2152 | /* contexts */ | |
2153 | *contexts_node = attr_node; | |
2154 | } | |
2155 | ret = 0; | |
2156 | end: | |
2157 | return ret; | |
2158 | } | |
2159 | ||
2160 | static | |
2161 | int process_context_node(xmlNodePtr context_node, | |
2162 | struct lttng_handle *handle, const char *channel_name) | |
2163 | { | |
2164 | int ret; | |
2165 | struct lttng_event_context context; | |
2166 | xmlNodePtr context_child_node = xmlFirstElementChild(context_node); | |
2167 | ||
2168 | assert(handle); | |
2169 | assert(channel_name); | |
2170 | ||
2171 | if (!context_child_node) { | |
2172 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2173 | goto end; | |
2174 | } | |
2175 | ||
2176 | memset(&context, 0, sizeof(context)); | |
2177 | ||
2178 | if (!strcmp((const char *) context_child_node->name, | |
2179 | config_element_type)) { | |
2180 | /* type */ | |
2181 | xmlChar *content = xmlNodeGetContent(context_child_node); | |
045fc617 | 2182 | |
dcf266c0 JG |
2183 | if (!content) { |
2184 | ret = -LTTNG_ERR_NOMEM; | |
2185 | goto end; | |
2186 | } | |
2187 | ||
2188 | ret = get_context_type(content); | |
2189 | free(content); | |
2190 | if (ret < 0) { | |
2191 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2192 | goto end; | |
2193 | } | |
2194 | ||
2195 | context.ctx = ret; | |
045fc617 JG |
2196 | } else if (!strcmp((const char *) context_child_node->name, |
2197 | config_element_context_perf)) { | |
2198 | /* perf */ | |
dcf266c0 JG |
2199 | xmlNodePtr perf_attr_node; |
2200 | ||
14ce5bd8 JG |
2201 | context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ? |
2202 | LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER : | |
2203 | LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER; | |
dcf266c0 JG |
2204 | for (perf_attr_node = xmlFirstElementChild(context_child_node); |
2205 | perf_attr_node; perf_attr_node = | |
2206 | xmlNextElementSibling(perf_attr_node)) { | |
2207 | if (!strcmp((const char *) perf_attr_node->name, | |
2208 | config_element_type)) { | |
2209 | xmlChar *content; | |
2210 | uint64_t type = 0; | |
2211 | ||
2212 | /* type */ | |
2213 | content = xmlNodeGetContent(perf_attr_node); | |
2214 | if (!content) { | |
2215 | ret = -LTTNG_ERR_NOMEM; | |
2216 | goto end; | |
2217 | } | |
2218 | ||
2219 | ret = parse_uint(content, &type); | |
2220 | free(content); | |
2221 | if (ret) { | |
2222 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2223 | goto end; | |
2224 | } | |
2225 | ||
2226 | if (type > UINT32_MAX) { | |
2227 | WARN("perf context type out of range."); | |
2228 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2229 | goto end; | |
2230 | } | |
2231 | ||
2232 | context.u.perf_counter.type = type; | |
2233 | } else if (!strcmp((const char *) perf_attr_node->name, | |
2234 | config_element_config)) { | |
2235 | xmlChar *content; | |
2236 | uint64_t config = 0; | |
2237 | ||
2238 | /* config */ | |
2239 | content = xmlNodeGetContent(perf_attr_node); | |
2240 | if (!content) { | |
2241 | ret = -LTTNG_ERR_NOMEM; | |
2242 | goto end; | |
2243 | } | |
2244 | ||
2245 | ret = parse_uint(content, &config); | |
2246 | free(content); | |
2247 | if (ret) { | |
2248 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2249 | goto end; | |
2250 | } | |
2251 | ||
2252 | context.u.perf_counter.config = config; | |
2253 | } else if (!strcmp((const char *) perf_attr_node->name, | |
2254 | config_element_name)) { | |
2255 | xmlChar *content; | |
dcf266c0 JG |
2256 | |
2257 | /* name */ | |
2258 | content = xmlNodeGetContent(perf_attr_node); | |
2259 | if (!content) { | |
2260 | ret = -LTTNG_ERR_NOMEM; | |
2261 | goto end; | |
2262 | } | |
2263 | ||
d2e67842 JG |
2264 | ret = lttng_strncpy(context.u.perf_counter.name, |
2265 | (const char *) content, | |
2266 | LTTNG_SYMBOL_NAME_LEN); | |
2267 | if (ret == -1) { | |
2268 | WARN("Perf counter \"%s\"'s name length (%zu) exceeds the maximal permitted length (%d) in session configuration", | |
2269 | (const char *) content, | |
2270 | strlen((const char *) content), | |
2271 | LTTNG_SYMBOL_NAME_LEN); | |
dcf266c0 JG |
2272 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; |
2273 | free(content); | |
2274 | goto end; | |
2275 | } | |
dcf266c0 JG |
2276 | free(content); |
2277 | } | |
2278 | } | |
045fc617 JG |
2279 | } else if (!strcmp((const char *) context_child_node->name, |
2280 | config_element_context_app)) { | |
2281 | /* application context */ | |
2282 | xmlNodePtr app_ctx_node; | |
2283 | ||
2284 | context.ctx = LTTNG_EVENT_CONTEXT_APP_CONTEXT; | |
2285 | for (app_ctx_node = xmlFirstElementChild(context_child_node); | |
2286 | app_ctx_node; app_ctx_node = | |
2287 | xmlNextElementSibling(app_ctx_node)) { | |
2288 | xmlChar *content; | |
2289 | char **target = strcmp( | |
2290 | (const char *) app_ctx_node->name, | |
2291 | config_element_context_app_provider_name) == 0 ? | |
2292 | &context.u.app_ctx.provider_name : | |
2293 | &context.u.app_ctx.ctx_name; | |
2294 | ||
2295 | content = xmlNodeGetContent(app_ctx_node); | |
2296 | if (!content) { | |
2297 | ret = -LTTNG_ERR_NOMEM; | |
2298 | goto end; | |
2299 | } | |
2300 | ||
2301 | *target = (char *) content; | |
2302 | } | |
2303 | } else { | |
2304 | /* Unrecognized context type */ | |
2305 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2306 | goto end; | |
dcf266c0 JG |
2307 | } |
2308 | ||
2309 | ret = lttng_add_context(handle, &context, NULL, channel_name); | |
045fc617 JG |
2310 | if (context.ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
2311 | free(context.u.app_ctx.provider_name); | |
2312 | free(context.u.app_ctx.ctx_name); | |
2313 | } | |
dcf266c0 JG |
2314 | end: |
2315 | return ret; | |
2316 | } | |
2317 | ||
2318 | static | |
2319 | int process_contexts_node(xmlNodePtr contexts_node, | |
2320 | struct lttng_handle *handle, const char *channel_name) | |
2321 | { | |
2322 | int ret = 0; | |
2323 | xmlNodePtr context_node; | |
2324 | ||
2325 | for (context_node = xmlFirstElementChild(contexts_node); context_node; | |
2326 | context_node = xmlNextElementSibling(context_node)) { | |
2327 | ret = process_context_node(context_node, handle, channel_name); | |
2328 | if (ret) { | |
2329 | goto end; | |
2330 | } | |
2331 | } | |
2332 | end: | |
2333 | return ret; | |
2334 | } | |
2335 | ||
847a5916 JR |
2336 | static |
2337 | int process_pid_tracker_node(xmlNodePtr pid_tracker_node, | |
2338 | struct lttng_handle *handle) | |
2339 | { | |
dd49e13f | 2340 | int ret = 0, child; |
847a5916 JR |
2341 | xmlNodePtr targets_node = NULL; |
2342 | xmlNodePtr node; | |
2343 | ||
2344 | assert(handle); | |
2345 | assert(pid_tracker_node); | |
2346 | /* get the targets node */ | |
2347 | for (node = xmlFirstElementChild(pid_tracker_node); node; | |
2348 | node = xmlNextElementSibling(node)) { | |
2349 | if (!strcmp((const char *) node->name, | |
2350 | config_element_targets)) { | |
2351 | targets_node = node; | |
2352 | break; | |
2353 | } | |
2354 | } | |
2355 | ||
2356 | if (!targets_node) { | |
2357 | ret = LTTNG_ERR_INVALID; | |
2358 | goto end; | |
2359 | } | |
2360 | ||
2361 | /* Go through all pid_target node */ | |
2362 | child = xmlChildElementCount(targets_node); | |
2363 | if (child == 0) { | |
2364 | /* The session is explicitly set to target nothing. */ | |
2365 | ret = lttng_untrack_pid(handle, -1); | |
2366 | if (ret) { | |
2367 | goto end; | |
2368 | } | |
2369 | } | |
2370 | for (node = xmlFirstElementChild(targets_node); node; | |
2371 | node = xmlNextElementSibling(node)) { | |
2372 | xmlNodePtr pid_target_node = node; | |
2373 | ||
2374 | /* get pid node and track it */ | |
2375 | for (node = xmlFirstElementChild(pid_target_node); node; | |
2376 | node = xmlNextElementSibling(node)) { | |
2377 | if (!strcmp((const char *) node->name, | |
2378 | config_element_pid)) { | |
2379 | int64_t pid; | |
2380 | xmlChar *content = NULL; | |
2381 | ||
2382 | content = xmlNodeGetContent(node); | |
2383 | if (!content) { | |
2384 | ret = LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2385 | goto end; | |
2386 | } | |
2387 | ||
2388 | ret = parse_int(content, &pid); | |
2389 | free(content); | |
2390 | if (ret) { | |
2391 | ret = LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2392 | goto end; | |
2393 | } | |
2394 | ||
2395 | ret = lttng_track_pid(handle, (int) pid); | |
2396 | if (ret) { | |
2397 | goto end; | |
2398 | } | |
2399 | } | |
2400 | } | |
2401 | node = pid_target_node; | |
2402 | } | |
2403 | ||
2404 | end: | |
2405 | return ret; | |
2406 | } | |
2407 | ||
dcf266c0 JG |
2408 | static |
2409 | int process_domain_node(xmlNodePtr domain_node, const char *session_name) | |
2410 | { | |
2411 | int ret; | |
2412 | struct lttng_domain domain = { 0 }; | |
2413 | struct lttng_handle *handle = NULL; | |
4fc2b126 | 2414 | struct lttng_channel *channel = NULL; |
dcf266c0 | 2415 | xmlNodePtr channels_node = NULL; |
847a5916 JR |
2416 | xmlNodePtr trackers_node = NULL; |
2417 | xmlNodePtr pid_tracker_node = NULL; | |
dcf266c0 JG |
2418 | xmlNodePtr node; |
2419 | ||
2420 | assert(session_name); | |
2421 | ||
2422 | ret = init_domain(domain_node, &domain); | |
2423 | if (ret) { | |
2424 | goto end; | |
2425 | } | |
2426 | ||
2427 | handle = lttng_create_handle(session_name, &domain); | |
2428 | if (!handle) { | |
2429 | ret = -LTTNG_ERR_NOMEM; | |
2430 | goto end; | |
2431 | } | |
2432 | ||
2433 | /* get the channels node */ | |
2434 | for (node = xmlFirstElementChild(domain_node); node; | |
2435 | node = xmlNextElementSibling(node)) { | |
2436 | if (!strcmp((const char *) node->name, | |
2437 | config_element_channels)) { | |
2438 | channels_node = node; | |
2439 | break; | |
2440 | } | |
2441 | } | |
2442 | ||
2443 | if (!channels_node) { | |
2444 | goto end; | |
2445 | } | |
2446 | ||
2447 | /* create all channels */ | |
2448 | for (node = xmlFirstElementChild(channels_node); node; | |
2449 | node = xmlNextElementSibling(node)) { | |
36d1687c | 2450 | const enum lttng_domain_type original_domain = domain.type; |
dcf266c0 JG |
2451 | xmlNodePtr contexts_node = NULL; |
2452 | xmlNodePtr events_node = NULL; | |
2453 | xmlNodePtr channel_attr_node; | |
2454 | ||
36d1687c JG |
2455 | /* |
2456 | * Channels of the "agent" types cannot be created directly. | |
2457 | * They are meant to be created implicitly through the | |
2458 | * activation of events in their domain. However, a user | |
2459 | * can override the default channel configuration attributes | |
2460 | * by creating the underlying UST channel _before_ enabling | |
2461 | * an agent domain event. | |
2462 | * | |
2463 | * Hence, the channel's type is substituted before the creation | |
2464 | * and restored by the time the events are created. | |
2465 | */ | |
2466 | switch (domain.type) { | |
2467 | case LTTNG_DOMAIN_JUL: | |
2468 | case LTTNG_DOMAIN_LOG4J: | |
2469 | case LTTNG_DOMAIN_PYTHON: | |
2470 | domain.type = LTTNG_DOMAIN_UST; | |
2471 | default: | |
2472 | break; | |
2473 | } | |
2474 | ||
4fc2b126 JR |
2475 | channel = lttng_channel_create(&domain); |
2476 | if (!channel) { | |
2477 | ret = -1; | |
2478 | goto end; | |
2479 | } | |
dcf266c0 JG |
2480 | |
2481 | for (channel_attr_node = xmlFirstElementChild(node); | |
2482 | channel_attr_node; channel_attr_node = | |
2483 | xmlNextElementSibling(channel_attr_node)) { | |
2484 | ret = process_channel_attr_node(channel_attr_node, | |
4fc2b126 | 2485 | channel, &contexts_node, &events_node); |
dcf266c0 JG |
2486 | if (ret) { |
2487 | goto end; | |
2488 | } | |
2489 | } | |
2490 | ||
4fc2b126 | 2491 | ret = lttng_enable_channel(handle, channel); |
dcf266c0 JG |
2492 | if (ret < 0) { |
2493 | goto end; | |
2494 | } | |
2495 | ||
36d1687c JG |
2496 | /* Restore the original channel domain. */ |
2497 | domain.type = original_domain; | |
2498 | ||
4fc2b126 | 2499 | ret = process_events_node(events_node, handle, channel->name); |
dcf266c0 JG |
2500 | if (ret) { |
2501 | goto end; | |
2502 | } | |
2503 | ||
2504 | ret = process_contexts_node(contexts_node, handle, | |
4fc2b126 | 2505 | channel->name); |
dcf266c0 JG |
2506 | if (ret) { |
2507 | goto end; | |
2508 | } | |
4fc2b126 JR |
2509 | |
2510 | lttng_channel_destroy(channel); | |
dcf266c0 | 2511 | } |
4fc2b126 | 2512 | channel = NULL; |
847a5916 JR |
2513 | |
2514 | /* get the trackers node */ | |
2515 | for (node = xmlFirstElementChild(domain_node); node; | |
2516 | node = xmlNextElementSibling(node)) { | |
2517 | if (!strcmp((const char *) node->name, | |
2518 | config_element_trackers)) { | |
2519 | trackers_node = node; | |
2520 | break; | |
2521 | } | |
2522 | } | |
2523 | ||
2524 | if (!trackers_node) { | |
2525 | goto end; | |
2526 | } | |
2527 | ||
2528 | for (node = xmlFirstElementChild(trackers_node); node; | |
2529 | node = xmlNextElementSibling(node)) { | |
2530 | if (!strcmp((const char *)node->name,config_element_pid_tracker)) { | |
2531 | pid_tracker_node = node; | |
2532 | ret = process_pid_tracker_node(pid_tracker_node, handle); | |
2533 | if (ret) { | |
2534 | goto end; | |
2535 | } | |
2536 | } | |
2537 | } | |
2538 | ||
2539 | if (!pid_tracker_node) { | |
2540 | lttng_track_pid(handle, -1); | |
2541 | } | |
2542 | ||
dcf266c0 | 2543 | end: |
4fc2b126 | 2544 | lttng_channel_destroy(channel); |
dcf266c0 JG |
2545 | lttng_destroy_handle(handle); |
2546 | return ret; | |
2547 | } | |
2548 | ||
2549 | static | |
2550 | int process_session_node(xmlNodePtr session_node, const char *session_name, | |
1b08cbce JR |
2551 | int overwrite, |
2552 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
2553 | { |
2554 | int ret, started = -1, snapshot_mode = -1; | |
259c2674 | 2555 | uint64_t live_timer_interval = UINT64_MAX, |
90936dcf JD |
2556 | rotation_timer_interval = 0, |
2557 | rotation_size = 0; | |
d324faf7 | 2558 | xmlChar *name = NULL; |
9e7c9f56 | 2559 | xmlChar *shm_path = NULL; |
dcf266c0 JG |
2560 | xmlNodePtr domains_node = NULL; |
2561 | xmlNodePtr output_node = NULL; | |
2562 | xmlNodePtr node; | |
90936dcf | 2563 | xmlNodePtr attributes_child; |
dcf266c0 JG |
2564 | struct lttng_domain *kernel_domain = NULL; |
2565 | struct lttng_domain *ust_domain = NULL; | |
2566 | struct lttng_domain *jul_domain = NULL; | |
5cdb6027 | 2567 | struct lttng_domain *log4j_domain = NULL; |
0e115563 | 2568 | struct lttng_domain *python_domain = NULL; |
dcf266c0 JG |
2569 | |
2570 | for (node = xmlFirstElementChild(session_node); node; | |
2571 | node = xmlNextElementSibling(node)) { | |
2572 | if (!name && !strcmp((const char *) node->name, | |
2573 | config_element_name)) { | |
2574 | /* name */ | |
2575 | xmlChar *node_content = xmlNodeGetContent(node); | |
2576 | if (!node_content) { | |
2577 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2578 | goto error; |
dcf266c0 JG |
2579 | } |
2580 | ||
d324faf7 | 2581 | name = node_content; |
dcf266c0 JG |
2582 | } else if (!domains_node && !strcmp((const char *) node->name, |
2583 | config_element_domains)) { | |
2584 | /* domains */ | |
2585 | domains_node = node; | |
2586 | } else if (started == -1 && !strcmp((const char *) node->name, | |
2587 | config_element_started)) { | |
2588 | /* started */ | |
2589 | xmlChar *node_content = xmlNodeGetContent(node); | |
2590 | if (!node_content) { | |
2591 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2592 | goto error; |
dcf266c0 JG |
2593 | } |
2594 | ||
2595 | ret = parse_bool(node_content, &started); | |
2596 | free(node_content); | |
2597 | if (ret) { | |
2598 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2599 | goto error; |
dcf266c0 JG |
2600 | } |
2601 | } else if (!output_node && !strcmp((const char *) node->name, | |
2602 | config_element_output)) { | |
2603 | /* output */ | |
2604 | output_node = node; | |
9e7c9f56 JR |
2605 | } else if (!shm_path && !strcmp((const char *) node->name, |
2606 | config_element_shared_memory_path)) { | |
2607 | /* shared memory path */ | |
2608 | xmlChar *node_content = xmlNodeGetContent(node); | |
2609 | if (!node_content) { | |
2610 | ret = -LTTNG_ERR_NOMEM; | |
2611 | goto error; | |
2612 | } | |
2613 | ||
2614 | shm_path = node_content; | |
dcf266c0 | 2615 | } else { |
259c2674 JD |
2616 | /* |
2617 | * attributes, snapshot_mode, live_timer_interval, rotation_size, | |
90936dcf JD |
2618 | * rotation_timer_interval. |
2619 | */ | |
2620 | for (attributes_child = xmlFirstElementChild(node); attributes_child; | |
2621 | attributes_child = xmlNextElementSibling(attributes_child)) { | |
2622 | if (!strcmp((const char *) attributes_child->name, | |
2623 | config_element_snapshot_mode)) { | |
2624 | /* snapshot_mode */ | |
2625 | xmlChar *snapshot_mode_content = | |
2626 | xmlNodeGetContent(attributes_child); | |
2627 | if (!snapshot_mode_content) { | |
2628 | ret = -LTTNG_ERR_NOMEM; | |
2629 | goto error; | |
2630 | } | |
dcf266c0 | 2631 | |
90936dcf JD |
2632 | ret = parse_bool(snapshot_mode_content, &snapshot_mode); |
2633 | free(snapshot_mode_content); | |
2634 | if (ret) { | |
2635 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2636 | goto error; | |
2637 | } | |
2638 | } else if (!strcmp((const char *) attributes_child->name, | |
2639 | config_element_live_timer_interval)) { | |
2640 | /* live_timer_interval */ | |
2641 | xmlChar *timer_interval_content = | |
2642 | xmlNodeGetContent(attributes_child); | |
2643 | if (!timer_interval_content) { | |
2644 | ret = -LTTNG_ERR_NOMEM; | |
2645 | goto error; | |
2646 | } | |
dcf266c0 | 2647 | |
90936dcf JD |
2648 | ret = parse_uint(timer_interval_content, &live_timer_interval); |
2649 | free(timer_interval_content); | |
2650 | if (ret) { | |
2651 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2652 | goto error; | |
2653 | } | |
dcf266c0 | 2654 | } |
90936dcf JD |
2655 | if (!strcmp((const char *) attributes_child->name, |
2656 | config_element_rotation_timer_interval)) { | |
2657 | /* rotation_timer_interval */ | |
2658 | xmlChar *timer_interval_content = | |
2659 | xmlNodeGetContent(attributes_child); | |
2660 | if (!timer_interval_content) { | |
2661 | ret = -LTTNG_ERR_NOMEM; | |
2662 | goto error; | |
2663 | } | |
2664 | ||
2665 | ret = parse_uint(timer_interval_content, &rotation_timer_interval); | |
2666 | free(timer_interval_content); | |
2667 | if (ret) { | |
2668 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2669 | goto error; | |
2670 | } | |
259c2674 | 2671 | } |
90936dcf JD |
2672 | if (!strcmp((const char *) attributes_child->name, |
2673 | config_element_rotation_size)) { | |
2674 | /* rotation_size */ | |
2675 | xmlChar *rotation_size_content = | |
2676 | xmlNodeGetContent(attributes_child); | |
2677 | if (!rotation_size_content) { | |
2678 | ret = -LTTNG_ERR_NOMEM; | |
2679 | goto error; | |
2680 | } | |
259c2674 | 2681 | |
90936dcf JD |
2682 | ret = parse_uint(rotation_size_content, &rotation_size); |
2683 | free(rotation_size_content); | |
2684 | if (ret) { | |
2685 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2686 | goto error; | |
2687 | } | |
259c2674 JD |
2688 | } |
2689 | } | |
dcf266c0 JG |
2690 | } |
2691 | } | |
2692 | ||
2693 | if (!name) { | |
2694 | /* Mandatory attribute, as defined in the session XSD */ | |
2695 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2696 | goto error; |
dcf266c0 JG |
2697 | } |
2698 | ||
d324faf7 | 2699 | if (session_name && strcmp((char *) name, session_name)) { |
dcf266c0 | 2700 | /* This is not the session we are looking for */ |
c2da8cde DG |
2701 | ret = -LTTNG_ERR_NO_SESSION; |
2702 | goto error; | |
dcf266c0 JG |
2703 | } |
2704 | ||
2705 | /* Init domains to create the session handles */ | |
2706 | for (node = xmlFirstElementChild(domains_node); node; | |
2707 | node = xmlNextElementSibling(node)) { | |
2708 | struct lttng_domain *domain; | |
2709 | ||
2710 | domain = zmalloc(sizeof(*domain)); | |
2711 | if (!domain) { | |
2712 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2713 | goto error; |
dcf266c0 JG |
2714 | } |
2715 | ||
2716 | ret = init_domain(node, domain); | |
2717 | if (ret) { | |
2718 | goto domain_init_error; | |
2719 | } | |
2720 | ||
2721 | switch (domain->type) { | |
2722 | case LTTNG_DOMAIN_KERNEL: | |
c33e6729 DG |
2723 | if (kernel_domain) { |
2724 | /* Same domain seen twice, invalid! */ | |
2725 | goto domain_init_error; | |
2726 | } | |
dcf266c0 JG |
2727 | kernel_domain = domain; |
2728 | break; | |
2729 | case LTTNG_DOMAIN_UST: | |
c33e6729 DG |
2730 | if (ust_domain) { |
2731 | /* Same domain seen twice, invalid! */ | |
2732 | goto domain_init_error; | |
2733 | } | |
dcf266c0 JG |
2734 | ust_domain = domain; |
2735 | break; | |
2736 | case LTTNG_DOMAIN_JUL: | |
c33e6729 DG |
2737 | if (jul_domain) { |
2738 | /* Same domain seen twice, invalid! */ | |
2739 | goto domain_init_error; | |
2740 | } | |
dcf266c0 JG |
2741 | jul_domain = domain; |
2742 | break; | |
5cdb6027 DG |
2743 | case LTTNG_DOMAIN_LOG4J: |
2744 | if (log4j_domain) { | |
2745 | /* Same domain seen twice, invalid! */ | |
2746 | goto domain_init_error; | |
2747 | } | |
2748 | log4j_domain = domain; | |
2749 | break; | |
0e115563 DG |
2750 | case LTTNG_DOMAIN_PYTHON: |
2751 | if (python_domain) { | |
2752 | /* Same domain seen twice, invalid! */ | |
2753 | goto domain_init_error; | |
2754 | } | |
2755 | python_domain = domain; | |
2756 | break; | |
dcf266c0 JG |
2757 | default: |
2758 | WARN("Invalid domain type"); | |
2759 | goto domain_init_error; | |
2760 | } | |
2761 | continue; | |
2762 | domain_init_error: | |
2763 | free(domain); | |
2764 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2765 | goto error; |
dcf266c0 JG |
2766 | } |
2767 | ||
2aaf5fc7 JR |
2768 | /* Apply overrides */ |
2769 | if (overrides) { | |
2770 | if (overrides->session_name) { | |
2771 | xmlChar *name_override = xmlStrdup(BAD_CAST(overrides->session_name)); | |
2772 | if (!name_override) { | |
2773 | ret = -LTTNG_ERR_NOMEM; | |
2774 | goto error; | |
2775 | } | |
2776 | ||
2777 | /* Overrides the session name to the provided name */ | |
2778 | xmlFree(name); | |
2779 | name = name_override; | |
2780 | } | |
2781 | } | |
2782 | ||
40b4155f | 2783 | if (overwrite) { |
dcf266c0 | 2784 | /* Destroy session if it exists */ |
d324faf7 | 2785 | ret = lttng_destroy_session((const char *) name); |
dcf266c0 JG |
2786 | if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) { |
2787 | ERR("Failed to destroy existing session."); | |
c2da8cde | 2788 | goto error; |
dcf266c0 JG |
2789 | } |
2790 | } | |
2791 | ||
2792 | /* Create session type depending on output type */ | |
2793 | if (snapshot_mode && snapshot_mode != -1) { | |
1b08cbce JR |
2794 | ret = create_snapshot_session((const char *) name, output_node, |
2795 | overrides); | |
dcf266c0 JG |
2796 | } else if (live_timer_interval && |
2797 | live_timer_interval != UINT64_MAX) { | |
194dfca0 | 2798 | ret = create_session((const char *) name, |
1b08cbce | 2799 | output_node, live_timer_interval, overrides); |
dcf266c0 JG |
2800 | } else { |
2801 | /* regular session */ | |
194dfca0 | 2802 | ret = create_session((const char *) name, |
1b08cbce | 2803 | output_node, UINT64_MAX, overrides); |
dcf266c0 | 2804 | } |
dcf266c0 | 2805 | if (ret) { |
c2da8cde | 2806 | goto error; |
dcf266c0 JG |
2807 | } |
2808 | ||
9e7c9f56 JR |
2809 | if (shm_path) { |
2810 | ret = lttng_set_session_shm_path((const char *) name, | |
d324faf7 | 2811 | (const char *) shm_path); |
9e7c9f56 JR |
2812 | if (ret) { |
2813 | goto error; | |
2814 | } | |
2815 | } | |
2816 | ||
dcf266c0 JG |
2817 | for (node = xmlFirstElementChild(domains_node); node; |
2818 | node = xmlNextElementSibling(node)) { | |
d324faf7 | 2819 | ret = process_domain_node(node, (const char *) name); |
dcf266c0 JG |
2820 | if (ret) { |
2821 | goto end; | |
2822 | } | |
2823 | } | |
2824 | ||
90936dcf | 2825 | if (rotation_timer_interval || rotation_size) { |
259c2674 JD |
2826 | struct lttng_rotation_schedule_attr *rotation_attr = lttng_rotation_schedule_attr_create(); |
2827 | ||
2828 | if (!rotation_attr) { | |
2829 | goto error; | |
2830 | } | |
2831 | ret = lttng_rotation_schedule_attr_set_session_name(rotation_attr, (const char *) name); | |
2832 | if (ret) { | |
2833 | lttng_rotation_schedule_attr_destroy(rotation_attr); | |
2834 | goto error; | |
2835 | } | |
2836 | lttng_rotation_schedule_attr_set_timer_period(rotation_attr, | |
2837 | rotation_timer_interval); | |
90936dcf | 2838 | lttng_rotation_schedule_attr_set_size(rotation_attr, rotation_size); |
259c2674 JD |
2839 | ret = lttng_rotation_set_schedule(rotation_attr); |
2840 | lttng_rotation_schedule_attr_destroy(rotation_attr); | |
2841 | if (ret) { | |
2842 | goto error; | |
2843 | } | |
2844 | } | |
2845 | ||
dcf266c0 | 2846 | if (started) { |
d324faf7 | 2847 | ret = lttng_start_tracing((const char *) name); |
dcf266c0 JG |
2848 | if (ret) { |
2849 | goto end; | |
2850 | } | |
2851 | } | |
c2da8cde | 2852 | |
dcf266c0 | 2853 | end: |
b2579dc1 | 2854 | if (ret < 0) { |
d324faf7 JG |
2855 | ERR("Failed to load session %s: %s", (const char *) name, |
2856 | lttng_strerror(ret)); | |
2857 | lttng_destroy_session((const char *) name); | |
b2579dc1 JG |
2858 | } |
2859 | ||
c2da8cde | 2860 | error: |
dcf266c0 JG |
2861 | free(kernel_domain); |
2862 | free(ust_domain); | |
2863 | free(jul_domain); | |
5cdb6027 | 2864 | free(log4j_domain); |
135a3893 | 2865 | free(python_domain); |
d324faf7 | 2866 | xmlFree(name); |
9e7c9f56 | 2867 | xmlFree(shm_path); |
dcf266c0 JG |
2868 | return ret; |
2869 | } | |
2870 | ||
cf53c06d DG |
2871 | /* |
2872 | * Return 1 if the given path is readable by the current UID or 0 if not. | |
2873 | * Return -1 if the path is EPERM. | |
2874 | */ | |
2875 | static int validate_file_read_creds(const char *path) | |
2876 | { | |
2877 | int ret; | |
2878 | ||
2879 | assert(path); | |
2880 | ||
2881 | /* Can we read the file. */ | |
2882 | ret = access(path, R_OK); | |
2883 | if (!ret) { | |
2884 | goto valid; | |
2885 | } | |
2886 | if (errno == EACCES) { | |
2887 | return -1; | |
2888 | } else { | |
2889 | /* Invalid. */ | |
2890 | return 0; | |
2891 | } | |
2892 | valid: | |
2893 | return 1; | |
2894 | } | |
2895 | ||
dcf266c0 JG |
2896 | static |
2897 | int load_session_from_file(const char *path, const char *session_name, | |
1b08cbce JR |
2898 | struct session_config_validation_ctx *validation_ctx, int overwrite, |
2899 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
2900 | { |
2901 | int ret, session_found = !session_name; | |
2902 | xmlDocPtr doc = NULL; | |
2903 | xmlNodePtr sessions_node; | |
2904 | xmlNodePtr session_node; | |
dcf266c0 JG |
2905 | |
2906 | assert(path); | |
2907 | assert(validation_ctx); | |
2908 | ||
cf53c06d DG |
2909 | ret = validate_file_read_creds(path); |
2910 | if (ret != 1) { | |
2911 | if (ret == -1) { | |
2912 | ret = -LTTNG_ERR_EPERM; | |
2913 | } else { | |
2914 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; | |
2915 | } | |
dcf266c0 JG |
2916 | goto end; |
2917 | } | |
2918 | ||
2919 | doc = xmlParseFile(path); | |
2920 | if (!doc) { | |
2921 | ret = -LTTNG_ERR_LOAD_IO_FAIL; | |
2922 | goto end; | |
2923 | } | |
2924 | ||
2925 | ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc); | |
2926 | if (ret) { | |
2927 | ERR("Session configuration file validation failed"); | |
2928 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2929 | goto end; | |
2930 | } | |
2931 | ||
2932 | sessions_node = xmlDocGetRootElement(doc); | |
2933 | if (!sessions_node) { | |
2934 | goto end; | |
2935 | } | |
2936 | ||
2937 | for (session_node = xmlFirstElementChild(sessions_node); | |
2938 | session_node; session_node = | |
2939 | xmlNextElementSibling(session_node)) { | |
2940 | ret = process_session_node(session_node, | |
1b08cbce | 2941 | session_name, overwrite, overrides); |
dcf266c0 JG |
2942 | if (session_name && ret == 0) { |
2943 | /* Target session found and loaded */ | |
2944 | session_found = 1; | |
2945 | break; | |
2946 | } | |
2947 | } | |
2948 | end: | |
2949 | xmlFreeDoc(doc); | |
2950 | if (!ret) { | |
a96bc65d | 2951 | ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT; |
dcf266c0 JG |
2952 | } |
2953 | return ret; | |
2954 | } | |
2955 | ||
2956 | static | |
2957 | int load_session_from_path(const char *path, const char *session_name, | |
1b08cbce JR |
2958 | struct session_config_validation_ctx *validation_ctx, int overwrite, |
2959 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
2960 | { |
2961 | int ret, session_found = !session_name; | |
dcf266c0 | 2962 | DIR *directory = NULL; |
cce35f91 JG |
2963 | struct lttng_dynamic_buffer file_path; |
2964 | size_t path_len; | |
dcf266c0 JG |
2965 | |
2966 | assert(path); | |
2967 | assert(validation_ctx); | |
cce35f91 JG |
2968 | path_len = strlen(path); |
2969 | lttng_dynamic_buffer_init(&file_path); | |
2970 | if (path_len >= LTTNG_PATH_MAX) { | |
2971 | ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)", | |
2972 | path, path_len, LTTNG_PATH_MAX); | |
2973 | ret = -LTTNG_ERR_INVALID; | |
2974 | goto end; | |
2975 | } | |
dcf266c0 | 2976 | |
4af16958 DG |
2977 | directory = opendir(path); |
2978 | if (!directory) { | |
11143783 DG |
2979 | switch (errno) { |
2980 | case ENOTDIR: | |
0f0a81b5 DG |
2981 | /* Try the file loading. */ |
2982 | break; | |
11143783 DG |
2983 | case ENOENT: |
2984 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; | |
2985 | goto end; | |
2986 | default: | |
0f0a81b5 DG |
2987 | ret = -LTTNG_ERR_LOAD_IO_FAIL; |
2988 | goto end; | |
4af16958 | 2989 | } |
dcf266c0 | 2990 | } |
4af16958 | 2991 | if (directory) { |
cce35f91 | 2992 | size_t file_path_root_len; |
dcf266c0 | 2993 | |
cce35f91 JG |
2994 | ret = lttng_dynamic_buffer_set_capacity(&file_path, |
2995 | LTTNG_PATH_MAX); | |
2996 | if (ret) { | |
2997 | ret = -LTTNG_ERR_NOMEM; | |
dcf266c0 JG |
2998 | goto end; |
2999 | } | |
3000 | ||
cce35f91 JG |
3001 | ret = lttng_dynamic_buffer_append(&file_path, path, path_len); |
3002 | if (ret) { | |
dcf266c0 | 3003 | ret = -LTTNG_ERR_NOMEM; |
dcf266c0 JG |
3004 | goto end; |
3005 | } | |
3006 | ||
cce35f91 JG |
3007 | if (file_path.data[file_path.size - 1] != '/') { |
3008 | ret = lttng_dynamic_buffer_append(&file_path, "/", 1); | |
3009 | if (ret) { | |
3010 | ret = -LTTNG_ERR_NOMEM; | |
3011 | goto end; | |
3012 | } | |
dcf266c0 | 3013 | } |
cce35f91 | 3014 | file_path_root_len = file_path.size; |
dcf266c0 JG |
3015 | |
3016 | /* Search for *.lttng files */ | |
9a2df626 MJ |
3017 | for (;;) { |
3018 | size_t file_name_len; | |
3019 | struct dirent *result; | |
3020 | ||
3021 | /* | |
3022 | * When the end of the directory stream is reached, NULL | |
3023 | * is returned and errno is kept unchanged. When an | |
3024 | * error occurs, NULL is returned and errno is set | |
3025 | * accordingly. To distinguish between the two, set | |
3026 | * errno to zero before calling readdir(). | |
3027 | * | |
3028 | * On success, readdir() returns a pointer to a dirent | |
3029 | * structure. This structure may be statically | |
3030 | * allocated, do not attempt to free(3) it. | |
3031 | */ | |
3032 | errno = 0; | |
3033 | result = readdir(directory); | |
3034 | ||
ff86d8d0 | 3035 | /* Reached end of dir stream or error out. */ |
9a2df626 MJ |
3036 | if (!result) { |
3037 | if (errno) { | |
3038 | PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path); | |
3039 | ret = -LTTNG_ERR_LOAD_IO_FAIL; | |
3040 | goto end; | |
3041 | } | |
3042 | break; | |
3043 | } | |
3044 | ||
3045 | file_name_len = strlen(result->d_name); | |
dcf266c0 JG |
3046 | |
3047 | if (file_name_len <= | |
3048 | sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) { | |
3049 | continue; | |
3050 | } | |
3051 | ||
cce35f91 JG |
3052 | if (file_path.size + file_name_len >= LTTNG_PATH_MAX) { |
3053 | WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)", | |
3054 | result->d_name, | |
3055 | /* +1 to account for NULL terminator. */ | |
3056 | file_path.size + file_name_len + 1, | |
3057 | LTTNG_PATH_MAX); | |
dcf266c0 JG |
3058 | continue; |
3059 | } | |
3060 | ||
cce35f91 | 3061 | /* Does the file end with .lttng? */ |
dcf266c0 | 3062 | if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION, |
cce35f91 JG |
3063 | result->d_name + file_name_len - sizeof( |
3064 | DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { | |
dcf266c0 JG |
3065 | continue; |
3066 | } | |
3067 | ||
cce35f91 JG |
3068 | ret = lttng_dynamic_buffer_append(&file_path, result->d_name, |
3069 | file_name_len + 1); | |
3070 | if (ret) { | |
3071 | ret = -LTTNG_ERR_NOMEM; | |
3072 | goto end; | |
3073 | } | |
dcf266c0 | 3074 | |
cce35f91 | 3075 | ret = load_session_from_file(file_path.data, session_name, |
1b08cbce | 3076 | validation_ctx, overwrite, overrides); |
dcf266c0 JG |
3077 | if (session_name && !ret) { |
3078 | session_found = 1; | |
3079 | break; | |
3080 | } | |
cce35f91 JG |
3081 | /* |
3082 | * Reset the buffer's size to the location of the | |
3083 | * path's trailing '/'. | |
3084 | */ | |
3085 | ret = lttng_dynamic_buffer_set_size(&file_path, | |
3086 | file_path_root_len); | |
3087 | if (ret) { | |
3088 | ret = -LTTNG_ERR_UNK; | |
3089 | goto end; | |
3090 | } | |
dcf266c0 JG |
3091 | } |
3092 | ||
dcf266c0 JG |
3093 | } else { |
3094 | ret = load_session_from_file(path, session_name, | |
1b08cbce | 3095 | validation_ctx, overwrite, overrides); |
dcf266c0 JG |
3096 | if (ret) { |
3097 | goto end; | |
3098 | } else { | |
3099 | session_found = 1; | |
3100 | } | |
3101 | } | |
3102 | ||
3103 | end: | |
3104 | if (directory) { | |
3105 | if (closedir(directory)) { | |
3106 | PERROR("closedir"); | |
3107 | } | |
3108 | } | |
464133ea | 3109 | if (session_found && !ret) { |
6c66fa0f | 3110 | ret = 0; |
dcf266c0 | 3111 | } |
cce35f91 | 3112 | lttng_dynamic_buffer_reset(&file_path); |
dcf266c0 JG |
3113 | return ret; |
3114 | } | |
3115 | ||
ab38c13f DG |
3116 | /* |
3117 | * Validate that the given path's credentials and the current process have the | |
cf53c06d | 3118 | * same UID. If so, return 1 else return 0 if it does NOT match. |
ab38c13f DG |
3119 | */ |
3120 | static int validate_path_creds(const char *path) | |
3121 | { | |
3122 | int ret, uid = getuid(); | |
3123 | struct stat buf; | |
3124 | ||
3125 | assert(path); | |
3126 | ||
cf53c06d | 3127 | if (uid == 0) { |
ab38c13f DG |
3128 | goto valid; |
3129 | } | |
3130 | ||
3131 | ret = stat(path, &buf); | |
3132 | if (ret < 0) { | |
3133 | if (errno != ENOENT) { | |
3134 | PERROR("stat"); | |
3135 | } | |
ab38c13f DG |
3136 | goto valid; |
3137 | } | |
3138 | ||
3139 | if (buf.st_uid != uid) { | |
3140 | goto invalid; | |
3141 | } | |
3142 | ||
3143 | valid: | |
ab38c13f | 3144 | return 1; |
cf53c06d DG |
3145 | invalid: |
3146 | return 0; | |
ab38c13f DG |
3147 | } |
3148 | ||
dcf266c0 JG |
3149 | LTTNG_HIDDEN |
3150 | int config_load_session(const char *path, const char *session_name, | |
1b08cbce JR |
3151 | int overwrite, unsigned int autoload, |
3152 | const struct config_load_session_override_attr *overrides) | |
dcf266c0 JG |
3153 | { |
3154 | int ret; | |
6c66fa0f | 3155 | bool session_loaded = false; |
cf53c06d | 3156 | const char *path_ptr = NULL; |
dcf266c0 JG |
3157 | struct session_config_validation_ctx validation_ctx = { 0 }; |
3158 | ||
3159 | ret = init_session_config_validation_ctx(&validation_ctx); | |
3160 | if (ret) { | |
3161 | goto end; | |
3162 | } | |
3163 | ||
3164 | if (!path) { | |
ab38c13f DG |
3165 | char *home_path; |
3166 | const char *sys_path; | |
3167 | ||
dcf266c0 | 3168 | /* Try home path */ |
ab38c13f | 3169 | home_path = utils_get_home_dir(); |
dcf266c0 | 3170 | if (home_path) { |
ab38c13f | 3171 | char path[PATH_MAX]; |
dcf266c0 | 3172 | |
d4fcf703 DG |
3173 | /* |
3174 | * Try user session configuration path. Ignore error here so we can | |
3175 | * continue loading the system wide sessions. | |
3176 | */ | |
ab38c13f DG |
3177 | if (autoload) { |
3178 | ret = snprintf(path, sizeof(path), | |
3179 | DEFAULT_SESSION_HOME_CONFIGPATH "/" | |
3180 | DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path); | |
cf53c06d DG |
3181 | if (ret < 0) { |
3182 | PERROR("snprintf session autoload home config path"); | |
3183 | goto end; | |
3184 | } | |
3185 | ||
3186 | /* | |
3187 | * Credentials are only validated for the autoload in order to | |
3188 | * avoid any user session daemon to try to load kernel sessions | |
3189 | * automatically and failing all the times. | |
3190 | */ | |
3191 | ret = validate_path_creds(path); | |
3192 | if (ret) { | |
3193 | path_ptr = path; | |
3194 | } | |
ab38c13f DG |
3195 | } else { |
3196 | ret = snprintf(path, sizeof(path), | |
3197 | DEFAULT_SESSION_HOME_CONFIGPATH, home_path); | |
cf53c06d DG |
3198 | if (ret < 0) { |
3199 | PERROR("snprintf session home config path"); | |
3200 | goto end; | |
3201 | } | |
3202 | path_ptr = path; | |
ab38c13f | 3203 | } |
cf53c06d DG |
3204 | if (path_ptr) { |
3205 | ret = load_session_from_path(path_ptr, session_name, | |
1b08cbce | 3206 | &validation_ctx, overwrite, overrides); |
d4fcf703 DG |
3207 | if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) { |
3208 | goto end; | |
3209 | } | |
3210 | /* | |
3211 | * Continue even if the session was found since we have to try | |
3212 | * the system wide sessions. | |
3213 | */ | |
6c66fa0f | 3214 | session_loaded = true; |
ab38c13f | 3215 | } |
d4fcf703 | 3216 | } |
ab38c13f | 3217 | |
cf53c06d DG |
3218 | /* Reset path pointer for the system wide dir. */ |
3219 | path_ptr = NULL; | |
3220 | ||
d4fcf703 DG |
3221 | /* Try system wide configuration directory. */ |
3222 | if (autoload) { | |
3223 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/" | |
3224 | DEFAULT_SESSION_CONFIG_AUTOLOAD; | |
cf53c06d DG |
3225 | ret = validate_path_creds(sys_path); |
3226 | if (ret) { | |
3227 | path_ptr = sys_path; | |
3228 | } | |
d4fcf703 | 3229 | } else { |
cf53c06d DG |
3230 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH; |
3231 | path_ptr = sys_path; | |
d4fcf703 DG |
3232 | } |
3233 | ||
cf53c06d DG |
3234 | if (path_ptr) { |
3235 | ret = load_session_from_path(path_ptr, session_name, | |
1b08cbce | 3236 | &validation_ctx, overwrite, overrides); |
6c66fa0f JG |
3237 | if (!ret) { |
3238 | session_loaded = true; | |
3239 | } | |
dcf266c0 JG |
3240 | } |
3241 | } else { | |
3242 | ret = access(path, F_OK); | |
3243 | if (ret < 0) { | |
3244 | PERROR("access"); | |
3245 | switch (errno) { | |
3246 | case ENOENT: | |
3247 | ret = -LTTNG_ERR_INVALID; | |
3248 | WARN("Session configuration path does not exist."); | |
3249 | break; | |
3250 | case EACCES: | |
3251 | ret = -LTTNG_ERR_EPERM; | |
3252 | break; | |
3253 | default: | |
3254 | ret = -LTTNG_ERR_UNK; | |
3255 | break; | |
3256 | } | |
3257 | goto end; | |
3258 | } | |
3259 | ||
3260 | ret = load_session_from_path(path, session_name, | |
1b08cbce | 3261 | &validation_ctx, overwrite, overrides); |
dcf266c0 JG |
3262 | } |
3263 | end: | |
3264 | fini_session_config_validation_ctx(&validation_ctx); | |
d2b6efff JG |
3265 | if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) { |
3266 | /* | |
3267 | * Don't report an error if no sessions are found when called | |
3268 | * without a session_name or a search path. | |
3269 | */ | |
3270 | ret = 0; | |
3271 | } | |
6c66fa0f JG |
3272 | |
3273 | if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) { | |
3274 | /* A matching session was found in one of the search paths. */ | |
3275 | ret = 0; | |
3276 | } | |
dcf266c0 JG |
3277 | return ret; |
3278 | } | |
56766c75 JG |
3279 | |
3280 | static | |
3281 | void __attribute__((destructor)) session_config_exit(void) | |
3282 | { | |
3283 | xmlCleanupParser(); | |
3284 | } |