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 | ||
18 | #define _GNU_SOURCE | |
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> | |
1501a7f3 JG |
29 | |
30 | #include <common/defaults.h> | |
31 | #include <common/error.h> | |
32 | #include <common/macros.h> | |
33 | #include <common/utils.h> | |
fb198a11 JG |
34 | #include <lttng/lttng-error.h> |
35 | #include <libxml/parser.h> | |
36 | #include <libxml/valid.h> | |
37 | #include <libxml/xmlschemas.h> | |
dcf266c0 JG |
38 | #include <libxml/tree.h> |
39 | #include <lttng/lttng.h> | |
40 | #include <lttng/snapshot.h> | |
1501a7f3 JG |
41 | |
42 | #include "config.h" | |
36f2332b | 43 | #include "config-internal.h" |
1501a7f3 JG |
44 | |
45 | struct handler_filter_args { | |
46 | const char* section; | |
47 | config_entry_handler_cb handler; | |
48 | void *user_data; | |
49 | }; | |
50 | ||
dcf266c0 JG |
51 | struct session_config_validation_ctx { |
52 | xmlSchemaParserCtxtPtr parser_ctx; | |
53 | xmlSchemaPtr schema; | |
54 | xmlSchemaValidCtxtPtr schema_validation_ctx; | |
55 | }; | |
56 | ||
1501a7f3 JG |
57 | const char * const config_str_yes = "yes"; |
58 | const char * const config_str_true = "true"; | |
59 | const char * const config_str_on = "on"; | |
60 | const char * const config_str_no = "no"; | |
61 | const char * const config_str_false = "false"; | |
62 | const char * const config_str_off = "off"; | |
36f2332b | 63 | const char * const config_xml_encoding = "UTF-8"; |
fb198a11 | 64 | const size_t config_xml_encoding_bytes_per_char = 2; /* Size of the encoding's largest character */ |
36f2332b JG |
65 | const char * const config_xml_indent_string = "\t"; |
66 | const char * const config_xml_true = "true"; | |
67 | const char * const config_xml_false = "false"; | |
1501a7f3 | 68 | |
fb198a11 JG |
69 | const char * const config_element_channel = "channel"; |
70 | const char * const config_element_channels = "channels"; | |
71 | const char * const config_element_domain = "domain"; | |
72 | const char * const config_element_domains = "domains"; | |
73 | const char * const config_element_event = "event"; | |
74 | const char * const config_element_events = "events"; | |
75 | const char * const config_element_context = "context"; | |
76 | const char * const config_element_contexts = "contexts"; | |
77 | const char * const config_element_attributes = "attributes"; | |
78 | const char * const config_element_exclusion = "exclusion"; | |
79 | const char * const config_element_exclusions = "exclusions"; | |
80 | const char * const config_element_function_attributes = "function_attributes"; | |
81 | const char * const config_element_probe_attributes = "probe_attributes"; | |
82 | const char * const config_element_symbol_name = "symbol_name"; | |
83 | const char * const config_element_address = "address"; | |
84 | const char * const config_element_offset = "offset"; | |
85 | const char * const config_element_name = "name"; | |
86 | const char * const config_element_enabled = "enabled"; | |
87 | const char * const config_element_overwrite_mode = "overwrite_mode"; | |
88 | const char * const config_element_subbuf_size = "subbuffer_size"; | |
89 | const char * const config_element_num_subbuf = "subbuffer_count"; | |
90 | const char * const config_element_switch_timer_interval = "switch_timer_interval"; | |
91 | const char * const config_element_read_timer_interval = "read_timer_interval"; | |
92 | const char * const config_element_output = "output"; | |
93 | const char * const config_element_output_type = "output_type"; | |
94 | const char * const config_element_tracefile_size = "tracefile_size"; | |
95 | const char * const config_element_tracefile_count = "tracefile_count"; | |
96 | const char * const config_element_live_timer_interval = "live_timer_interval"; | |
97 | const char * const config_element_type = "type"; | |
98 | const char * const config_element_buffer_type = "buffer_type"; | |
99 | const char * const config_element_session = "session"; | |
100 | const char * const config_element_sessions = "sessions"; | |
101 | const char * const config_element_perf = "perf"; | |
102 | const char * const config_element_config = "config"; | |
103 | const char * const config_element_started = "started"; | |
104 | const char * const config_element_snapshot_mode = "snapshot_mode"; | |
105 | const char * const config_element_loglevel = "loglevel"; | |
106 | const char * const config_element_loglevel_type = "loglevel_type"; | |
107 | const char * const config_element_filter = "filter"; | |
108 | const char * const config_element_snapshot_outputs = "snapshot_outputs"; | |
109 | const char * const config_element_consumer_output = "consumer_output"; | |
110 | const char * const config_element_destination = "destination"; | |
111 | const char * const config_element_path = "path"; | |
112 | const char * const config_element_net_output = "net_output"; | |
113 | const char * const config_element_control_uri = "control_uri"; | |
114 | const char * const config_element_data_uri = "data_uri"; | |
115 | const char * const config_element_max_size = "max_size"; | |
116 | ||
117 | const char * const config_domain_type_kernel = "KERNEL"; | |
118 | const char * const config_domain_type_ust = "UST"; | |
119 | const char * const config_domain_type_jul = "JUL"; | |
5cdb6027 | 120 | const char * const config_domain_type_log4j = "LOG4J"; |
fb198a11 JG |
121 | |
122 | const char * const config_buffer_type_per_pid = "PER_PID"; | |
123 | const char * const config_buffer_type_per_uid = "PER_UID"; | |
124 | const char * const config_buffer_type_global = "GLOBAL"; | |
125 | ||
126 | const char * const config_overwrite_mode_discard = "DISCARD"; | |
127 | const char * const config_overwrite_mode_overwrite = "OVERWRITE"; | |
128 | ||
129 | const char * const config_output_type_splice = "SPLICE"; | |
130 | const char * const config_output_type_mmap = "MMAP"; | |
131 | ||
132 | const char * const config_loglevel_type_all = "ALL"; | |
133 | const char * const config_loglevel_type_range = "RANGE"; | |
134 | const char * const config_loglevel_type_single = "SINGLE"; | |
135 | ||
136 | const char * const config_event_type_all = "ALL"; | |
137 | const char * const config_event_type_tracepoint = "TRACEPOINT"; | |
138 | const char * const config_event_type_probe = "PROBE"; | |
139 | const char * const config_event_type_function = "FUNCTION"; | |
140 | const char * const config_event_type_function_entry = "FUNCTION_ENTRY"; | |
141 | const char * const config_event_type_noop = "NOOP"; | |
142 | const char * const config_event_type_syscall = "SYSCALL"; | |
143 | const char * const config_event_type_kprobe = "KPROBE"; | |
144 | const char * const config_event_type_kretprobe = "KRETPROBE"; | |
145 | ||
146 | const char * const config_event_context_pid = "PID"; | |
147 | const char * const config_event_context_procname = "PROCNAME"; | |
148 | const char * const config_event_context_prio = "PRIO"; | |
149 | const char * const config_event_context_nice = "NICE"; | |
150 | const char * const config_event_context_vpid = "VPID"; | |
151 | const char * const config_event_context_tid = "TID"; | |
152 | const char * const config_event_context_vtid = "VTID"; | |
153 | const char * const config_event_context_ppid = "PPID"; | |
154 | const char * const config_event_context_vppid = "VPPID"; | |
155 | const char * const config_event_context_pthread_id = "PTHREAD_ID"; | |
156 | const char * const config_event_context_hostname = "HOSTNAME"; | |
157 | const char * const config_event_context_ip = "IP"; | |
e885a367 | 158 | const char * const config_event_context_perf_thread_counter = "PERF_THREAD_COUNTER"; |
fb198a11 | 159 | |
dcf266c0 JG |
160 | struct consumer_output { |
161 | int enabled; | |
162 | char *path; | |
163 | char *control_uri; | |
164 | char *data_uri; | |
165 | }; | |
166 | ||
1501a7f3 JG |
167 | static int config_entry_handler_filter(struct handler_filter_args *args, |
168 | const char *section, const char *name, const char *value) | |
169 | { | |
170 | int ret = 0; | |
171 | struct config_entry entry = { section, name, value }; | |
172 | ||
173 | assert(args); | |
174 | ||
175 | if (!section || !name || !value) { | |
176 | ret = -EIO; | |
177 | goto end; | |
178 | } | |
179 | ||
180 | if (args->section) { | |
181 | if (strcmp(args->section, section)) { | |
182 | goto end; | |
183 | } | |
184 | } | |
185 | ||
186 | ret = args->handler(&entry, args->user_data); | |
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
191 | LTTNG_HIDDEN | |
192 | int config_get_section_entries(const char *override_path, const char *section, | |
193 | config_entry_handler_cb handler, void *user_data) | |
194 | { | |
195 | int ret = 0; | |
7ab02a27 | 196 | char *path; |
1501a7f3 JG |
197 | FILE *config_file = NULL; |
198 | struct handler_filter_args filter = { section, handler, user_data }; | |
199 | ||
7ab02a27 DG |
200 | /* First, try system-wide conf. file. */ |
201 | path = DEFAULT_DAEMON_SYSTEM_CONFIGPATH; | |
202 | ||
203 | config_file = fopen(path, "r"); | |
204 | if (config_file) { | |
205 | DBG("Loading daemon conf file at %s", path); | |
206 | /* | |
207 | * Return value is not very important here since error or not, we | |
208 | * continue and try the next possible conf. file. | |
209 | */ | |
210 | (void) ini_parse_file(config_file, | |
211 | (ini_entry_handler) config_entry_handler_filter, | |
212 | (void *) &filter); | |
213 | fclose(config_file); | |
214 | } | |
215 | ||
216 | /* Second is the user local configuration. */ | |
217 | path = utils_get_home_dir(); | |
218 | if (path) { | |
219 | char fullpath[PATH_MAX]; | |
220 | ||
221 | ret = snprintf(fullpath, sizeof(fullpath), | |
222 | DEFAULT_DAEMON_HOME_CONFIGPATH, path); | |
223 | if (ret < 0) { | |
224 | PERROR("snprintf user conf. path"); | |
225 | goto error; | |
226 | } | |
227 | ||
228 | config_file = fopen(fullpath, "r"); | |
229 | if (config_file) { | |
230 | DBG("Loading daemon user conf file at %s", path); | |
231 | /* | |
232 | * Return value is not very important here since error or not, we | |
233 | * continue and try the next possible conf. file. | |
234 | */ | |
235 | (void) ini_parse_file(config_file, | |
236 | (ini_entry_handler) config_entry_handler_filter, | |
237 | (void *) &filter); | |
238 | fclose(config_file); | |
239 | } | |
240 | } | |
241 | ||
242 | /* Final path is the one that the user might have provided. */ | |
1501a7f3 JG |
243 | if (override_path) { |
244 | config_file = fopen(override_path, "r"); | |
245 | if (config_file) { | |
7ab02a27 DG |
246 | DBG("Loading daemon command line conf file at %s", override_path); |
247 | (void) ini_parse_file(config_file, | |
248 | (ini_entry_handler) config_entry_handler_filter, | |
249 | (void *) &filter); | |
250 | fclose(config_file); | |
1501a7f3 JG |
251 | } else { |
252 | ERR("Failed to open daemon configuration file at %s", | |
253 | override_path); | |
254 | ret = -ENOENT; | |
7ab02a27 | 255 | goto error; |
1501a7f3 JG |
256 | } |
257 | } | |
258 | ||
7ab02a27 DG |
259 | /* Everything went well. */ |
260 | ret = 0; | |
1501a7f3 | 261 | |
7ab02a27 | 262 | error: |
1501a7f3 JG |
263 | return ret; |
264 | } | |
265 | ||
266 | LTTNG_HIDDEN | |
267 | int config_parse_value(const char *value) | |
268 | { | |
269 | int i, ret = 0; | |
270 | char *endptr, *lower_str; | |
271 | size_t len; | |
272 | unsigned long v; | |
273 | ||
274 | len = strlen(value); | |
275 | if (!len) { | |
276 | ret = -1; | |
277 | goto end; | |
278 | } | |
279 | ||
280 | v = strtoul(value, &endptr, 10); | |
281 | if (endptr != value) { | |
282 | ret = v; | |
283 | goto end; | |
284 | } | |
285 | ||
286 | lower_str = zmalloc(len + 1); | |
287 | if (!lower_str) { | |
288 | PERROR("zmalloc"); | |
289 | ret = -errno; | |
290 | goto end; | |
291 | } | |
292 | ||
293 | for (i = 0; i < len; i++) { | |
294 | lower_str[i] = tolower(value[i]); | |
295 | } | |
296 | ||
297 | if (!strcmp(lower_str, config_str_yes) || | |
298 | !strcmp(lower_str, config_str_true) || | |
299 | !strcmp(lower_str, config_str_on)) { | |
300 | ret = 1; | |
301 | } else if (!strcmp(lower_str, config_str_no) || | |
302 | !strcmp(lower_str, config_str_false) || | |
303 | !strcmp(lower_str, config_str_off)) { | |
304 | ret = 0; | |
305 | } else { | |
306 | ret = -1; | |
307 | } | |
308 | ||
309 | free(lower_str); | |
310 | end: | |
311 | return ret; | |
312 | } | |
36f2332b JG |
313 | |
314 | /* | |
315 | * Returns a xmlChar string which must be released using xmlFree(). | |
316 | */ | |
317 | static xmlChar *encode_string(const char *in_str) | |
318 | { | |
319 | xmlChar *out_str = NULL; | |
320 | xmlCharEncodingHandlerPtr handler; | |
321 | int out_len, ret, in_len; | |
322 | ||
323 | assert(in_str); | |
324 | ||
325 | handler = xmlFindCharEncodingHandler(config_xml_encoding); | |
326 | if (!handler) { | |
327 | ERR("xmlFindCharEncodingHandler return NULL!. Configure issue!"); | |
328 | goto end; | |
329 | } | |
330 | ||
331 | in_len = strlen(in_str); | |
332 | /* | |
e0b9045b JG |
333 | * Add 1 byte for the NULL terminted character. The factor 4 here is |
334 | * used because UTF-8 characters can take up to 4 bytes. | |
36f2332b | 335 | */ |
e0b9045b | 336 | out_len = (in_len * 4) + 1; |
36f2332b JG |
337 | out_str = xmlMalloc(out_len); |
338 | if (!out_str) { | |
339 | goto end; | |
340 | } | |
341 | ||
342 | ret = handler->input(out_str, &out_len, (const xmlChar *) in_str, &in_len); | |
343 | if (ret < 0) { | |
344 | xmlFree(out_str); | |
345 | out_str = NULL; | |
346 | goto end; | |
347 | } | |
348 | ||
349 | /* out_len is now the size of out_str */ | |
350 | out_str[out_len] = '\0'; | |
351 | end: | |
352 | return out_str; | |
353 | } | |
354 | ||
355 | LTTNG_HIDDEN | |
705bb62f | 356 | struct config_writer *config_writer_create(int fd_output, int indent) |
36f2332b JG |
357 | { |
358 | int ret; | |
359 | struct config_writer *writer; | |
360 | xmlOutputBufferPtr buffer; | |
361 | ||
362 | writer = zmalloc(sizeof(struct config_writer)); | |
363 | if (!writer) { | |
364 | PERROR("zmalloc config_writer_create"); | |
365 | goto end; | |
366 | } | |
367 | ||
368 | buffer = xmlOutputBufferCreateFd(fd_output, NULL); | |
369 | if (!buffer) { | |
370 | goto error_destroy; | |
371 | } | |
372 | ||
373 | writer->writer = xmlNewTextWriter(buffer); | |
374 | ret = xmlTextWriterStartDocument(writer->writer, NULL, | |
375 | config_xml_encoding, NULL); | |
376 | if (ret < 0) { | |
377 | goto error_destroy; | |
378 | } | |
379 | ||
380 | ret = xmlTextWriterSetIndentString(writer->writer, | |
381 | BAD_CAST config_xml_indent_string); | |
705bb62f | 382 | if (ret) { |
36f2332b JG |
383 | goto error_destroy; |
384 | } | |
385 | ||
705bb62f JRJ |
386 | ret = xmlTextWriterSetIndent(writer->writer, indent); |
387 | if (ret) { | |
36f2332b JG |
388 | goto error_destroy; |
389 | } | |
390 | ||
391 | end: | |
392 | return writer; | |
393 | error_destroy: | |
394 | config_writer_destroy(writer); | |
395 | return NULL; | |
396 | } | |
397 | ||
398 | LTTNG_HIDDEN | |
399 | int config_writer_destroy(struct config_writer *writer) | |
400 | { | |
401 | int ret = 0; | |
402 | ||
403 | if (!writer) { | |
404 | ret = -EINVAL; | |
405 | goto end; | |
406 | } | |
407 | ||
408 | if (xmlTextWriterEndDocument(writer->writer) < 0) { | |
409 | WARN("Could not close XML document"); | |
410 | ret = -EIO; | |
411 | } | |
412 | ||
413 | if (writer->writer) { | |
414 | xmlFreeTextWriter(writer->writer); | |
415 | } | |
416 | ||
417 | free(writer); | |
418 | end: | |
419 | return ret; | |
420 | } | |
421 | ||
422 | LTTNG_HIDDEN | |
423 | int config_writer_open_element(struct config_writer *writer, | |
424 | const char *element_name) | |
425 | { | |
426 | int ret; | |
427 | xmlChar *encoded_element_name; | |
428 | ||
429 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
430 | ret = -1; | |
431 | goto end; | |
432 | } | |
433 | ||
434 | encoded_element_name = encode_string(element_name); | |
435 | if (!encoded_element_name) { | |
436 | ret = -1; | |
437 | goto end; | |
438 | } | |
439 | ||
440 | ret = xmlTextWriterStartElement(writer->writer, encoded_element_name); | |
441 | xmlFree(encoded_element_name); | |
442 | end: | |
443 | return ret > 0 ? 0 : ret; | |
444 | } | |
445 | ||
446 | LTTNG_HIDDEN | |
447 | int config_writer_close_element(struct config_writer *writer) | |
448 | { | |
449 | int ret; | |
450 | ||
451 | if (!writer || !writer->writer) { | |
452 | ret = -1; | |
453 | goto end; | |
454 | } | |
455 | ||
456 | ret = xmlTextWriterEndElement(writer->writer); | |
457 | end: | |
458 | return ret > 0 ? 0 : ret; | |
459 | } | |
460 | ||
461 | LTTNG_HIDDEN | |
462 | int config_writer_write_element_unsigned_int(struct config_writer *writer, | |
463 | const char *element_name, uint64_t value) | |
464 | { | |
465 | int ret; | |
466 | xmlChar *encoded_element_name; | |
467 | ||
468 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
469 | ret = -1; | |
470 | goto end; | |
471 | } | |
472 | ||
473 | encoded_element_name = encode_string(element_name); | |
474 | if (!encoded_element_name) { | |
475 | ret = -1; | |
476 | goto end; | |
477 | } | |
478 | ||
479 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
480 | encoded_element_name, "%" PRIu64, value); | |
481 | xmlFree(encoded_element_name); | |
482 | end: | |
483 | return ret > 0 ? 0 : ret; | |
484 | } | |
485 | ||
486 | LTTNG_HIDDEN | |
487 | int config_writer_write_element_signed_int(struct config_writer *writer, | |
488 | const char *element_name, int64_t value) | |
489 | { | |
490 | int ret; | |
491 | xmlChar *encoded_element_name; | |
492 | ||
493 | if (!writer || !writer->writer || !element_name || !element_name[0]) { | |
494 | ret = -1; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | encoded_element_name = encode_string(element_name); | |
499 | if (!encoded_element_name) { | |
500 | ret = -1; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | ret = xmlTextWriterWriteFormatElement(writer->writer, | |
505 | encoded_element_name, "%" PRIi64, value); | |
506 | xmlFree(encoded_element_name); | |
507 | end: | |
508 | return ret > 0 ? 0 : ret; | |
509 | } | |
510 | ||
511 | LTTNG_HIDDEN | |
512 | int config_writer_write_element_bool(struct config_writer *writer, | |
513 | const char *element_name, int value) | |
514 | { | |
515 | return config_writer_write_element_string(writer, element_name, | |
516 | value ? config_xml_true : config_xml_false); | |
517 | } | |
518 | ||
519 | LTTNG_HIDDEN | |
520 | int config_writer_write_element_string(struct config_writer *writer, | |
521 | const char *element_name, const char *value) | |
522 | { | |
523 | int ret; | |
524 | xmlChar *encoded_element_name = NULL; | |
525 | xmlChar *encoded_value = NULL; | |
526 | ||
527 | if (!writer || !writer->writer || !element_name || !element_name[0] || | |
528 | !value) { | |
529 | ret = -1; | |
530 | goto end; | |
531 | } | |
532 | ||
533 | encoded_element_name = encode_string(element_name); | |
534 | if (!encoded_element_name) { | |
535 | ret = -1; | |
536 | goto end; | |
537 | } | |
538 | ||
539 | encoded_value = encode_string(value); | |
540 | if (!encoded_value) { | |
541 | ret = -1; | |
542 | goto end; | |
543 | } | |
544 | ||
545 | ret = xmlTextWriterWriteElement(writer->writer, encoded_element_name, | |
dcf266c0 | 546 | encoded_value); |
36f2332b JG |
547 | end: |
548 | xmlFree(encoded_element_name); | |
549 | xmlFree(encoded_value); | |
550 | return ret > 0 ? 0 : ret; | |
551 | } | |
dcf266c0 JG |
552 | |
553 | static | |
554 | void xml_error_handler(void *ctx, const char *format, ...) | |
555 | { | |
556 | char *errMsg; | |
557 | va_list args; | |
558 | int ret; | |
559 | ||
560 | va_start(args, format); | |
561 | ret = vasprintf(&errMsg, format, args); | |
6c043b48 | 562 | va_end(args); |
dcf266c0 JG |
563 | if (ret == -1) { |
564 | ERR("String allocation failed in xml error handler"); | |
565 | return; | |
566 | } | |
dcf266c0 JG |
567 | |
568 | fprintf(stderr, "XML Error: %s", errMsg); | |
569 | free(errMsg); | |
570 | } | |
571 | ||
572 | static | |
573 | void fini_session_config_validation_ctx( | |
574 | struct session_config_validation_ctx *ctx) | |
575 | { | |
576 | if (ctx->parser_ctx) { | |
577 | xmlSchemaFreeParserCtxt(ctx->parser_ctx); | |
578 | } | |
579 | ||
580 | if (ctx->schema) { | |
581 | xmlSchemaFree(ctx->schema); | |
582 | } | |
583 | ||
584 | if (ctx->schema_validation_ctx) { | |
585 | xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); | |
586 | } | |
587 | ||
588 | memset(ctx, 0, sizeof(struct session_config_validation_ctx)); | |
589 | } | |
590 | ||
54e399cb JG |
591 | static |
592 | char *get_session_config_xsd_path() | |
593 | { | |
594 | char *xsd_path; | |
595 | const char *base_path = getenv(DEFAULT_SESSION_CONFIG_XSD_PATH_ENV); | |
596 | size_t base_path_len; | |
597 | size_t max_path_len; | |
598 | ||
599 | if (!base_path) { | |
600 | base_path = DEFAULT_SESSION_CONFIG_XSD_PATH; | |
601 | } | |
602 | ||
603 | base_path_len = strlen(base_path); | |
604 | max_path_len = base_path_len + | |
605 | sizeof(DEFAULT_SESSION_CONFIG_XSD_FILENAME) + 1; | |
606 | xsd_path = zmalloc(max_path_len); | |
607 | if (!xsd_path) { | |
608 | goto end; | |
609 | } | |
610 | ||
611 | strncpy(xsd_path, base_path, max_path_len); | |
612 | if (xsd_path[base_path_len - 1] != '/') { | |
613 | xsd_path[base_path_len++] = '/'; | |
614 | } | |
615 | ||
616 | strncpy(xsd_path + base_path_len, DEFAULT_SESSION_CONFIG_XSD_FILENAME, | |
617 | max_path_len - base_path_len); | |
618 | end: | |
619 | return xsd_path; | |
620 | } | |
621 | ||
dcf266c0 JG |
622 | static |
623 | int init_session_config_validation_ctx( | |
624 | struct session_config_validation_ctx *ctx) | |
625 | { | |
626 | int ret; | |
54e399cb JG |
627 | char *xsd_path = get_session_config_xsd_path(); |
628 | ||
629 | if (!xsd_path) { | |
630 | ret = -LTTNG_ERR_NOMEM; | |
631 | goto end; | |
632 | } | |
dcf266c0 | 633 | |
54e399cb | 634 | ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); |
dcf266c0 JG |
635 | if (!ctx->parser_ctx) { |
636 | ERR("XSD parser context creation failed"); | |
637 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
638 | goto end; | |
639 | } | |
640 | xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, | |
641 | xml_error_handler, NULL); | |
642 | ||
643 | ctx->schema = xmlSchemaParse(ctx->parser_ctx); | |
644 | if (!ctx->schema) { | |
645 | ERR("XSD parsing failed"); | |
646 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
647 | goto end; | |
648 | } | |
649 | ||
650 | ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema); | |
651 | if (!ctx->schema_validation_ctx) { | |
652 | ERR("XSD validation context creation failed"); | |
653 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
654 | goto end; | |
655 | } | |
656 | ||
657 | xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, | |
658 | xml_error_handler, NULL); | |
659 | ret = 0; | |
660 | ||
661 | end: | |
662 | if (ret) { | |
663 | fini_session_config_validation_ctx(ctx); | |
664 | } | |
665 | ||
54e399cb | 666 | free(xsd_path); |
dcf266c0 JG |
667 | return ret; |
668 | } | |
669 | ||
670 | static | |
671 | int parse_uint(xmlChar *str, uint64_t *val) | |
672 | { | |
673 | int ret; | |
674 | char *endptr; | |
675 | ||
676 | if (!str || !val) { | |
677 | ret = -1; | |
678 | goto end; | |
679 | } | |
680 | ||
681 | *val = strtoull((const char *) str, &endptr, 10); | |
682 | if (!endptr || *endptr) { | |
683 | ret = -1; | |
684 | } else { | |
685 | ret = 0; | |
686 | } | |
687 | ||
688 | end: | |
689 | return ret; | |
690 | } | |
691 | ||
692 | static | |
693 | int parse_int(xmlChar *str, int64_t *val) | |
694 | { | |
695 | int ret; | |
696 | char *endptr; | |
697 | ||
698 | if (!str || !val) { | |
699 | ret = -1; | |
700 | goto end; | |
701 | } | |
702 | ||
703 | *val = strtoll((const char *) str, &endptr, 10); | |
704 | if (!endptr || *endptr) { | |
705 | ret = -1; | |
706 | } else { | |
707 | ret = 0; | |
708 | } | |
709 | ||
710 | end: | |
711 | return ret; | |
712 | } | |
713 | ||
714 | static | |
715 | int parse_bool(xmlChar *str, int *val) | |
716 | { | |
717 | int ret = 0; | |
718 | ||
719 | if (!str || !val) { | |
720 | ret = -1; | |
721 | goto end; | |
722 | } | |
723 | ||
724 | if (!strcmp((const char *) str, config_xml_true)) { | |
725 | *val = 1; | |
726 | } else if (!strcmp((const char *) str, config_xml_false)) { | |
727 | *val = 0; | |
728 | } else { | |
729 | WARN("Invalid boolean value encoutered (%s).", | |
730 | (const char *) str); | |
731 | ret = -1; | |
732 | } | |
733 | end: | |
734 | return ret; | |
735 | } | |
736 | ||
737 | static | |
738 | int get_domain_type(xmlChar *domain) | |
739 | { | |
740 | int ret; | |
741 | ||
742 | if (!domain) { | |
743 | goto error; | |
744 | } | |
745 | ||
746 | if (!strcmp((char *) domain, config_domain_type_kernel)) { | |
747 | ret = LTTNG_DOMAIN_KERNEL; | |
748 | } else if (!strcmp((char *) domain, config_domain_type_ust)) { | |
749 | ret = LTTNG_DOMAIN_UST; | |
750 | } else if (!strcmp((char *) domain, config_domain_type_jul)) { | |
751 | ret = LTTNG_DOMAIN_JUL; | |
5cdb6027 DG |
752 | } else if (!strcmp((char *) domain, config_domain_type_log4j)) { |
753 | ret = LTTNG_DOMAIN_LOG4J; | |
dcf266c0 JG |
754 | } else { |
755 | goto error; | |
756 | } | |
757 | ||
758 | return ret; | |
759 | error: | |
760 | return -1; | |
761 | } | |
762 | ||
763 | static | |
764 | int get_buffer_type(xmlChar *buffer_type) | |
765 | { | |
766 | int ret; | |
767 | ||
768 | if (!buffer_type) { | |
769 | goto error; | |
770 | } | |
771 | ||
772 | if (!strcmp((char *) buffer_type, config_buffer_type_global)) { | |
773 | ret = LTTNG_BUFFER_GLOBAL; | |
774 | } else if (!strcmp((char *) buffer_type, config_buffer_type_per_uid)) { | |
775 | ret = LTTNG_BUFFER_PER_UID; | |
776 | } else if (!strcmp((char *) buffer_type, config_buffer_type_per_pid)) { | |
777 | ret = LTTNG_BUFFER_PER_PID; | |
778 | } else { | |
779 | goto error; | |
780 | } | |
781 | ||
782 | return ret; | |
783 | error: | |
784 | return -1; | |
785 | } | |
786 | ||
787 | static | |
788 | int get_overwrite_mode(xmlChar *overwrite_mode) | |
789 | { | |
790 | int ret; | |
791 | ||
792 | if (!overwrite_mode) { | |
793 | goto error; | |
794 | } | |
795 | ||
796 | if (!strcmp((char *) overwrite_mode, config_overwrite_mode_overwrite)) { | |
797 | ret = 1; | |
798 | } else if (!strcmp((char *) overwrite_mode, | |
799 | config_overwrite_mode_discard)) { | |
800 | ret = 0; | |
801 | } else { | |
802 | goto error; | |
803 | } | |
804 | ||
805 | return ret; | |
806 | error: | |
807 | return -1; | |
808 | } | |
809 | ||
810 | static | |
811 | int get_output_type(xmlChar *output_type) | |
812 | { | |
813 | int ret; | |
814 | ||
815 | if (!output_type) { | |
816 | goto error; | |
817 | } | |
818 | ||
819 | if (!strcmp((char *) output_type, config_output_type_mmap)) { | |
820 | ret = LTTNG_EVENT_MMAP; | |
821 | } else if (!strcmp((char *) output_type, config_output_type_splice)) { | |
822 | ret = LTTNG_EVENT_SPLICE; | |
823 | } else { | |
824 | goto error; | |
825 | } | |
826 | ||
827 | return ret; | |
828 | error: | |
829 | return -1; | |
830 | } | |
831 | ||
832 | static | |
833 | int get_event_type(xmlChar *event_type) | |
834 | { | |
835 | int ret; | |
836 | ||
837 | if (!event_type) { | |
838 | goto error; | |
839 | } | |
840 | ||
841 | if (!strcmp((char *) event_type, config_event_type_all)) { | |
842 | ret = LTTNG_EVENT_ALL; | |
843 | } else if (!strcmp((char *) event_type, config_event_type_tracepoint)) { | |
844 | ret = LTTNG_EVENT_TRACEPOINT; | |
845 | } else if (!strcmp((char *) event_type, config_event_type_probe)) { | |
846 | ret = LTTNG_EVENT_PROBE; | |
847 | } else if (!strcmp((char *) event_type, config_event_type_function)) { | |
848 | ret = LTTNG_EVENT_FUNCTION; | |
849 | } else if (!strcmp((char *) event_type, | |
850 | config_event_type_function_entry)) { | |
851 | ret = LTTNG_EVENT_FUNCTION_ENTRY; | |
852 | } else if (!strcmp((char *) event_type, config_event_type_noop)) { | |
853 | ret = LTTNG_EVENT_NOOP; | |
854 | } else if (!strcmp((char *) event_type, config_event_type_syscall)) { | |
855 | ret = LTTNG_EVENT_SYSCALL; | |
856 | } else { | |
857 | goto error; | |
858 | } | |
859 | ||
860 | return ret; | |
861 | error: | |
862 | return -1; | |
863 | } | |
864 | ||
865 | static | |
866 | int get_loglevel_type(xmlChar *loglevel_type) | |
867 | { | |
868 | int ret; | |
869 | ||
870 | if (!loglevel_type) { | |
871 | goto error; | |
872 | } | |
873 | ||
874 | if (!strcmp((char *) loglevel_type, config_loglevel_type_all)) { | |
875 | ret = LTTNG_EVENT_LOGLEVEL_ALL; | |
876 | } else if (!strcmp((char *) loglevel_type, | |
877 | config_loglevel_type_range)) { | |
878 | ret = LTTNG_EVENT_LOGLEVEL_RANGE; | |
879 | } else if (!strcmp((char *) loglevel_type, | |
880 | config_loglevel_type_single)) { | |
881 | ret = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
882 | } else { | |
883 | goto error; | |
884 | } | |
885 | ||
886 | return ret; | |
887 | error: | |
888 | return -1; | |
889 | } | |
890 | ||
891 | /* | |
892 | * Return the context type or -1 on error. | |
893 | */ | |
894 | static | |
895 | int get_context_type(xmlChar *context_type) | |
896 | { | |
897 | int ret; | |
898 | ||
899 | if (!context_type) { | |
900 | goto error; | |
901 | } | |
902 | ||
903 | if (!strcmp((char *) context_type, config_event_context_pid)) { | |
904 | ret = LTTNG_EVENT_CONTEXT_PID; | |
905 | } else if (!strcmp((char *) context_type, | |
906 | config_event_context_procname)) { | |
907 | ret = LTTNG_EVENT_CONTEXT_PROCNAME; | |
908 | } else if (!strcmp((char *) context_type, | |
909 | config_event_context_prio)) { | |
910 | ret = LTTNG_EVENT_CONTEXT_PRIO; | |
911 | } else if (!strcmp((char *) context_type, | |
912 | config_event_context_nice)) { | |
913 | ret = LTTNG_EVENT_CONTEXT_NICE; | |
914 | } else if (!strcmp((char *) context_type, | |
915 | config_event_context_vpid)) { | |
916 | ret = LTTNG_EVENT_CONTEXT_VPID; | |
917 | } else if (!strcmp((char *) context_type, | |
918 | config_event_context_tid)) { | |
919 | ret = LTTNG_EVENT_CONTEXT_TID; | |
920 | } else if (!strcmp((char *) context_type, | |
921 | config_event_context_vtid)) { | |
922 | ret = LTTNG_EVENT_CONTEXT_VTID; | |
923 | } else if (!strcmp((char *) context_type, | |
924 | config_event_context_ppid)) { | |
925 | ret = LTTNG_EVENT_CONTEXT_PPID; | |
926 | } else if (!strcmp((char *) context_type, | |
927 | config_event_context_vppid)) { | |
928 | ret = LTTNG_EVENT_CONTEXT_VPPID; | |
929 | } else if (!strcmp((char *) context_type, | |
930 | config_event_context_pthread_id)) { | |
931 | ret = LTTNG_EVENT_CONTEXT_PTHREAD_ID; | |
932 | } else if (!strcmp((char *) context_type, | |
933 | config_event_context_hostname)) { | |
934 | ret = LTTNG_EVENT_CONTEXT_HOSTNAME; | |
935 | } else if (!strcmp((char *) context_type, | |
936 | config_event_context_ip)) { | |
937 | ret = LTTNG_EVENT_CONTEXT_IP; | |
938 | } else { | |
939 | goto error; | |
940 | } | |
941 | ||
942 | return ret; | |
943 | error: | |
944 | return -1; | |
945 | } | |
946 | ||
947 | static | |
948 | int init_domain(xmlNodePtr domain_node, struct lttng_domain *domain) | |
949 | { | |
950 | int ret; | |
951 | xmlNodePtr node; | |
952 | ||
953 | for (node = xmlFirstElementChild(domain_node); node; | |
954 | node = xmlNextElementSibling(node)) { | |
955 | if (!strcmp((const char *) node->name, config_element_type)) { | |
956 | /* domain type */ | |
957 | xmlChar *node_content = xmlNodeGetContent(node); | |
958 | if (!node_content) { | |
959 | ret = -LTTNG_ERR_NOMEM; | |
960 | goto end; | |
961 | } | |
962 | ||
963 | ret = get_domain_type(node_content); | |
964 | free(node_content); | |
965 | if (ret < 0) { | |
966 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
967 | goto end; | |
968 | } | |
969 | ||
970 | domain->type = ret; | |
971 | } else if (!strcmp((const char *) node->name, | |
972 | config_element_buffer_type)) { | |
973 | /* buffer type */ | |
974 | xmlChar *node_content = xmlNodeGetContent(node); | |
975 | if (!node_content) { | |
976 | ret = -LTTNG_ERR_NOMEM; | |
977 | goto end; | |
978 | } | |
979 | ||
980 | ret = get_buffer_type(node_content); | |
981 | free(node_content); | |
982 | if (ret < 0) { | |
983 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
984 | goto end; | |
985 | } | |
986 | ||
987 | domain->buf_type = ret; | |
988 | } | |
989 | } | |
990 | ret = 0; | |
991 | end: | |
992 | return ret; | |
993 | } | |
994 | ||
995 | static | |
996 | int get_net_output_uris(xmlNodePtr net_output_node, char **control_uri, | |
997 | char **data_uri) | |
998 | { | |
999 | xmlNodePtr node; | |
1000 | ||
1001 | for (node = xmlFirstElementChild(net_output_node); node; | |
1002 | node = xmlNextElementSibling(node)) { | |
1003 | if (!strcmp((const char *) node->name, config_element_control_uri)) { | |
1004 | /* control_uri */ | |
1005 | *control_uri = (char *) xmlNodeGetContent(node); | |
1006 | if (!*control_uri) { | |
1007 | break; | |
1008 | } | |
1009 | } else { | |
1010 | /* data_uri */ | |
1011 | *data_uri = (char *) xmlNodeGetContent(node); | |
1012 | if (!*data_uri) { | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | return *control_uri || *data_uri ? 0 : -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1019 | } | |
1020 | ||
1021 | static | |
1022 | int process_consumer_output(xmlNodePtr consumer_output_node, | |
1023 | struct consumer_output *output) | |
1024 | { | |
1025 | int ret; | |
1026 | xmlNodePtr node; | |
1027 | ||
1028 | assert(output); | |
1029 | ||
1030 | for (node = xmlFirstElementChild(consumer_output_node); node; | |
1031 | node = xmlNextElementSibling(node)) { | |
1032 | if (!strcmp((const char *) node->name, config_element_enabled)) { | |
1033 | xmlChar *enabled_str = xmlNodeGetContent(node); | |
1034 | ||
1035 | /* enabled */ | |
1036 | if (!enabled_str) { | |
1037 | ret = -LTTNG_ERR_NOMEM; | |
1038 | goto end; | |
1039 | } | |
1040 | ||
1041 | ret = parse_bool(enabled_str, &output->enabled); | |
1042 | free(enabled_str); | |
1043 | if (ret) { | |
1044 | goto end; | |
1045 | } | |
1046 | } else { | |
1047 | xmlNodePtr output_type_node; | |
1048 | ||
1049 | /* destination */ | |
1050 | output_type_node = xmlFirstElementChild(node); | |
1051 | if (!output_type_node) { | |
1052 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1053 | goto end; | |
1054 | } | |
1055 | ||
1056 | if (!strcmp((const char *) output_type_node->name, | |
1057 | config_element_path)) { | |
1058 | /* path */ | |
1059 | output->path = (char *) xmlNodeGetContent(output_type_node); | |
1060 | if (!output->path) { | |
1061 | ret = -LTTNG_ERR_NOMEM; | |
1062 | goto end; | |
1063 | } | |
1064 | } else { | |
1065 | /* net_output */ | |
1066 | ret = get_net_output_uris(output_type_node, | |
1067 | &output->control_uri, &output->data_uri); | |
1068 | if (ret) { | |
1069 | goto end; | |
1070 | } | |
1071 | } | |
1072 | } | |
1073 | } | |
1074 | ret = 0; | |
1075 | ||
1076 | end: | |
1077 | if (ret) { | |
1078 | free(output->path); | |
1079 | free(output->control_uri); | |
1080 | free(output->data_uri); | |
1081 | memset(output, 0, sizeof(struct consumer_output)); | |
1082 | } | |
1083 | return ret; | |
1084 | } | |
1085 | ||
1086 | static | |
1087 | int create_session_net_output(const char *name, struct lttng_domain *domain, | |
1088 | const char *control_uri, const char *data_uri) | |
1089 | { | |
1090 | int ret; | |
1091 | struct lttng_handle *handle; | |
1092 | const char *uri = NULL; | |
1093 | ||
1094 | assert(name); | |
1095 | assert(domain); | |
1096 | ||
1097 | handle = lttng_create_handle(name, domain); | |
1098 | if (!handle) { | |
1099 | ret = -LTTNG_ERR_NOMEM; | |
1100 | goto end; | |
1101 | } | |
1102 | ||
1103 | if (!control_uri || !data_uri) { | |
1104 | uri = control_uri ? control_uri : data_uri; | |
1105 | control_uri = uri; | |
1106 | data_uri = uri; | |
1107 | } | |
1108 | ||
1109 | ret = lttng_set_consumer_url(handle, control_uri, data_uri); | |
1110 | lttng_destroy_handle(handle); | |
1111 | end: | |
1112 | return ret; | |
1113 | } | |
1114 | ||
1115 | static | |
1116 | int create_snapshot_session(const char *session_name, xmlNodePtr output_node) | |
1117 | { | |
1118 | int ret; | |
1119 | xmlNodePtr node = NULL; | |
1120 | xmlNodePtr snapshot_output_list_node; | |
1121 | xmlNodePtr snapshot_output_node; | |
1122 | ||
1123 | assert(session_name); | |
1124 | ||
1125 | ret = lttng_create_session_snapshot(session_name, NULL); | |
1126 | if (ret) { | |
1127 | goto end; | |
1128 | } | |
1129 | ||
1130 | if (!output_node) { | |
1131 | goto end; | |
1132 | } | |
1133 | ||
1134 | snapshot_output_list_node = xmlFirstElementChild(output_node); | |
1135 | ||
1136 | /* Parse and create snapshot outputs */ | |
1137 | ||
1138 | for (snapshot_output_node = | |
1139 | xmlFirstElementChild(snapshot_output_list_node); | |
1140 | snapshot_output_node; snapshot_output_node = | |
1141 | xmlNextElementSibling(snapshot_output_node)) { | |
1142 | char *name = NULL; | |
1143 | uint64_t max_size = UINT64_MAX; | |
1144 | struct consumer_output output = { 0 }; | |
1145 | struct lttng_snapshot_output *snapshot_output = NULL; | |
1146 | ||
1147 | for (node = xmlFirstElementChild(snapshot_output_node); node; | |
1148 | node = xmlNextElementSibling(node)) { | |
1149 | if (!strcmp((const char *) node->name, | |
1150 | config_element_name)) { | |
1151 | /* name */ | |
1152 | name = (char *) xmlNodeGetContent(node); | |
1153 | if (!name) { | |
1154 | ret = -LTTNG_ERR_NOMEM; | |
1155 | goto error_snapshot_output; | |
1156 | } | |
1157 | } else if (!strcmp((const char *) node->name, | |
1158 | config_element_max_size)) { | |
1159 | xmlChar *content = xmlNodeGetContent(node); | |
1160 | ||
1161 | /* max_size */ | |
1162 | if (!content) { | |
1163 | ret = -LTTNG_ERR_NOMEM; | |
1164 | goto error_snapshot_output; | |
1165 | } | |
1166 | ret = parse_uint(content, &max_size); | |
1167 | free(content); | |
1168 | if (ret) { | |
1169 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1170 | goto error_snapshot_output; | |
1171 | } | |
1172 | } else { | |
1173 | /* consumer_output */ | |
1174 | ret = process_consumer_output(node, &output); | |
1175 | if (ret) { | |
1176 | goto error_snapshot_output; | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | snapshot_output = lttng_snapshot_output_create(); | |
1182 | if (!snapshot_output) { | |
1183 | ret = -LTTNG_ERR_NOMEM; | |
1184 | goto error_snapshot_output; | |
1185 | } | |
1186 | ||
1187 | ret = lttng_snapshot_output_set_name(name, snapshot_output); | |
1188 | if (ret) { | |
1189 | goto error_snapshot_output; | |
1190 | } | |
1191 | ||
1192 | ret = lttng_snapshot_output_set_size(max_size, snapshot_output); | |
1193 | if (ret) { | |
1194 | goto error_snapshot_output; | |
1195 | } | |
1196 | ||
1197 | if (output.path) { | |
1198 | ret = lttng_snapshot_output_set_ctrl_url(output.path, | |
1199 | snapshot_output); | |
1200 | if (ret) { | |
1201 | goto error_snapshot_output; | |
1202 | } | |
1203 | } else { | |
1204 | if (output.control_uri) { | |
1205 | ret = lttng_snapshot_output_set_ctrl_url(output.control_uri, | |
1206 | snapshot_output); | |
1207 | if (ret) { | |
1208 | goto error_snapshot_output; | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | if (output.data_uri) { | |
1213 | ret = lttng_snapshot_output_set_data_url(output.data_uri, | |
1214 | snapshot_output); | |
1215 | if (ret) { | |
1216 | goto error_snapshot_output; | |
1217 | } | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | ret = lttng_snapshot_add_output(session_name, snapshot_output); | |
1222 | error_snapshot_output: | |
1223 | free(name); | |
1224 | free(output.path); | |
1225 | free(output.control_uri); | |
1226 | free(output.data_uri); | |
1227 | lttng_snapshot_output_destroy(snapshot_output); | |
1228 | if (ret) { | |
1229 | goto end; | |
1230 | } | |
1231 | } | |
1232 | end: | |
1233 | return ret; | |
1234 | } | |
1235 | ||
1236 | static | |
1237 | int create_session(const char *name, | |
1238 | struct lttng_domain *kernel_domain, | |
1239 | struct lttng_domain *ust_domain, | |
1240 | struct lttng_domain *jul_domain, | |
5cdb6027 | 1241 | struct lttng_domain *log4j_domain, |
dcf266c0 JG |
1242 | xmlNodePtr output_node, |
1243 | uint64_t live_timer_interval) | |
1244 | { | |
1245 | int ret; | |
1246 | struct consumer_output output = { 0 }; | |
1247 | xmlNodePtr consumer_output_node; | |
1248 | ||
1249 | assert(name); | |
dcf266c0 JG |
1250 | |
1251 | if (output_node) { | |
1252 | consumer_output_node = xmlFirstElementChild(output_node); | |
1253 | if (!consumer_output_node) { | |
1254 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1255 | goto end; | |
1256 | } | |
1257 | ||
1258 | if (strcmp((const char *) consumer_output_node->name, | |
1259 | config_element_consumer_output)) { | |
1260 | WARN("Invalid output type, expected %s node", | |
1261 | config_element_consumer_output); | |
1262 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1263 | goto end; | |
1264 | } | |
1265 | ||
1266 | ret = process_consumer_output(consumer_output_node, &output); | |
1267 | if (ret) { | |
1268 | goto end; | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | if (live_timer_interval != UINT64_MAX && | |
1273 | !output.control_uri && !output.data_uri) { | |
1274 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1275 | goto end; | |
1276 | } | |
1277 | ||
1278 | if (output.control_uri || output.data_uri) { | |
1279 | int i; | |
1280 | struct lttng_domain *domain; | |
1281 | struct lttng_domain *domains[] = | |
5cdb6027 | 1282 | { kernel_domain, ust_domain, jul_domain, log4j_domain}; |
dcf266c0 JG |
1283 | |
1284 | /* network destination */ | |
1285 | if (live_timer_interval && live_timer_interval != UINT64_MAX) { | |
b664f89a DG |
1286 | /* |
1287 | * URLs are provided for sure since the test above make sure that | |
1288 | * with a live timer the data and control URIs are provided. So, | |
1289 | * NULL is passed here and will be set right after. | |
1290 | */ | |
1291 | ret = lttng_create_session_live(name, NULL, live_timer_interval); | |
dcf266c0 JG |
1292 | } else { |
1293 | ret = lttng_create_session(name, NULL); | |
1294 | } | |
1295 | if (ret) { | |
1296 | goto end; | |
1297 | } | |
1298 | ||
301423df | 1299 | for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) { |
dcf266c0 JG |
1300 | domain = domains[i]; |
1301 | if (!domain) { | |
1302 | continue; | |
1303 | } | |
1304 | ||
1305 | ret = create_session_net_output(name, domain, output.control_uri, | |
1306 | output.data_uri); | |
1307 | if (ret) { | |
1308 | goto end; | |
1309 | } | |
1310 | } | |
1311 | } else { | |
1312 | /* either local output or no output */ | |
1313 | ret = lttng_create_session(name, output.path); | |
1314 | if (ret) { | |
1315 | goto end; | |
1316 | } | |
1317 | } | |
1318 | end: | |
1319 | free(output.path); | |
1320 | free(output.control_uri); | |
1321 | free(output.data_uri); | |
1322 | return ret; | |
1323 | } | |
1324 | static | |
1325 | int process_probe_attribute_node(xmlNodePtr probe_attribute_node, | |
1326 | struct lttng_event_probe_attr *attr) | |
1327 | { | |
1328 | int ret; | |
1329 | ||
1330 | assert(probe_attribute_node); | |
1331 | assert(attr); | |
1332 | ||
1333 | if (!strcmp((const char *) probe_attribute_node->name, | |
1334 | config_element_address)) { | |
1335 | xmlChar *content; | |
1336 | uint64_t addr = 0; | |
1337 | ||
1338 | /* addr */ | |
1339 | content = xmlNodeGetContent(probe_attribute_node); | |
1340 | if (!content) { | |
1341 | ret = -LTTNG_ERR_NOMEM; | |
1342 | goto end; | |
1343 | } | |
1344 | ||
1345 | ret = parse_uint(content, &addr); | |
1346 | free(content); | |
1347 | if (ret) { | |
1348 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1349 | goto end; | |
1350 | } | |
1351 | ||
1352 | attr->addr = addr; | |
1353 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1354 | config_element_offset)) { | |
1355 | xmlChar *content; | |
1356 | uint64_t offset = 0; | |
1357 | ||
1358 | /* offset */ | |
1359 | content = xmlNodeGetContent(probe_attribute_node); | |
1360 | if (!content) { | |
1361 | ret = -LTTNG_ERR_NOMEM; | |
1362 | goto end; | |
1363 | } | |
1364 | ||
1365 | ret = parse_uint(content, &offset); | |
1366 | free(content); | |
1367 | if (ret) { | |
1368 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1369 | goto end; | |
1370 | } | |
1371 | ||
1372 | attr->offset = offset; | |
1373 | } else if (!strcmp((const char *) probe_attribute_node->name, | |
1374 | config_element_symbol_name)) { | |
1375 | xmlChar *content; | |
1376 | size_t name_len; | |
1377 | ||
1378 | /* symbol_name */ | |
1379 | content = xmlNodeGetContent(probe_attribute_node); | |
1380 | if (!content) { | |
1381 | ret = -LTTNG_ERR_NOMEM; | |
1382 | goto end; | |
1383 | } | |
1384 | ||
1385 | name_len = strlen((char *) content); | |
1386 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { | |
1387 | WARN("symbol_name too long."); | |
1388 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1389 | free(content); | |
1390 | goto end; | |
1391 | } | |
1392 | ||
1393 | strncpy(attr->symbol_name, (const char *) content, name_len); | |
1394 | free(content); | |
1395 | } | |
1396 | ret = 0; | |
1397 | end: | |
1398 | return ret; | |
1399 | } | |
1400 | ||
1401 | static | |
1402 | int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle, | |
1403 | const char *channel_name) | |
1404 | { | |
1405 | int ret, i; | |
1406 | xmlNodePtr node; | |
1407 | struct lttng_event event; | |
1408 | char **exclusions = NULL; | |
1409 | unsigned long exclusion_count = 0; | |
1410 | char *filter_expression = NULL; | |
1411 | ||
1412 | assert(event_node); | |
1413 | assert(handle); | |
1414 | assert(channel_name); | |
1415 | ||
1416 | memset(&event, 0, sizeof(event)); | |
1417 | ||
1418 | for (node = xmlFirstElementChild(event_node); node; | |
1419 | node = xmlNextElementSibling(node)) { | |
1420 | if (!strcmp((const char *) node->name, config_element_name)) { | |
1421 | xmlChar *content; | |
1422 | size_t name_len; | |
1423 | ||
1424 | /* name */ | |
1425 | content = xmlNodeGetContent(node); | |
1426 | if (!content) { | |
1427 | ret = -LTTNG_ERR_NOMEM; | |
1428 | goto end; | |
1429 | } | |
1430 | ||
1431 | name_len = strlen((char *) content); | |
1432 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { | |
1433 | WARN("Channel name too long."); | |
1434 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1435 | free(content); | |
1436 | goto end; | |
1437 | } | |
1438 | ||
1439 | strncpy(event.name, (const char *) content, name_len); | |
1440 | free(content); | |
1441 | } else if (!strcmp((const char *) node->name, | |
1442 | config_element_enabled)) { | |
1443 | xmlChar *content = xmlNodeGetContent(node); | |
1444 | ||
1445 | /* enabled */ | |
1446 | if (!content) { | |
1447 | ret = -LTTNG_ERR_NOMEM; | |
1448 | goto end; | |
1449 | } | |
1450 | ||
1451 | ret = parse_bool(content, &event.enabled); | |
1452 | free(content); | |
1453 | if (ret) { | |
1454 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1455 | goto end; | |
1456 | } | |
1457 | } else if (!strcmp((const char *) node->name, | |
1458 | config_element_type)) { | |
1459 | xmlChar *content = xmlNodeGetContent(node); | |
1460 | ||
1461 | /* type */ | |
1462 | if (!content) { | |
1463 | ret = -LTTNG_ERR_NOMEM; | |
1464 | goto end; | |
1465 | } | |
1466 | ||
1467 | ret = get_event_type(content); | |
1468 | free(content); | |
1469 | if (ret < 0) { | |
1470 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1471 | goto end; | |
1472 | } | |
1473 | ||
1474 | event.type = ret; | |
1475 | } else if (!strcmp((const char *) node->name, | |
1476 | config_element_loglevel_type)) { | |
1477 | xmlChar *content = xmlNodeGetContent(node); | |
1478 | ||
1479 | /* loglevel_type */ | |
1480 | if (!content) { | |
1481 | ret = -LTTNG_ERR_NOMEM; | |
1482 | goto end; | |
1483 | } | |
1484 | ||
1485 | ret = get_loglevel_type(content); | |
1486 | free(content); | |
1487 | if (ret < 0) { | |
1488 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1489 | goto end; | |
1490 | } | |
1491 | ||
1492 | event.loglevel_type = ret; | |
1493 | } else if (!strcmp((const char *) node->name, | |
1494 | config_element_loglevel)) { | |
1495 | xmlChar *content; | |
1496 | int64_t loglevel = 0; | |
1497 | ||
1498 | /* loglevel */ | |
1499 | content = xmlNodeGetContent(node); | |
1500 | if (!content) { | |
1501 | ret = -LTTNG_ERR_NOMEM; | |
1502 | goto end; | |
1503 | } | |
1504 | ||
1505 | ret = parse_int(content, &loglevel); | |
1506 | free(content); | |
1507 | if (ret) { | |
1508 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1509 | goto end; | |
1510 | } | |
1511 | ||
1512 | if (loglevel > INT_MAX || loglevel < INT_MIN) { | |
1513 | WARN("loglevel out of range."); | |
1514 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1515 | goto end; | |
1516 | } | |
1517 | ||
1518 | event.loglevel = loglevel; | |
1519 | } else if (!strcmp((const char *) node->name, | |
1520 | config_element_filter)) { | |
1521 | xmlChar *content = | |
1522 | xmlNodeGetContent(node); | |
1523 | ||
1524 | /* filter */ | |
1525 | if (!content) { | |
1526 | ret = -LTTNG_ERR_NOMEM; | |
1527 | goto end; | |
1528 | } | |
1529 | ||
1530 | filter_expression = strdup((char *) content); | |
1531 | free(content); | |
1532 | if (!filter_expression) { | |
1533 | ret = -LTTNG_ERR_NOMEM; | |
1534 | goto end; | |
1535 | } | |
1536 | } else if (!strcmp((const char *) node->name, | |
1537 | config_element_exclusions)) { | |
1538 | xmlNodePtr exclusion_node; | |
1539 | int exclusion_index = 0; | |
1540 | ||
1541 | /* exclusions */ | |
1542 | if (exclusions) { | |
1543 | /* | |
1544 | * Exclusions has already been initialized, | |
1545 | * invalid file. | |
1546 | */ | |
1547 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1548 | goto end; | |
1549 | } | |
1550 | ||
1551 | exclusion_count = xmlChildElementCount(node); | |
1552 | if (!exclusion_count) { | |
1553 | continue; | |
1554 | } | |
1555 | ||
1556 | exclusions = zmalloc(exclusion_count * sizeof(char *)); | |
1557 | if (!exclusions) { | |
1558 | exclusion_count = 0; | |
1559 | ret = -LTTNG_ERR_NOMEM; | |
1560 | goto end; | |
1561 | } | |
1562 | ||
1563 | for (exclusion_node = xmlFirstElementChild(node); exclusion_node; | |
1564 | exclusion_node = xmlNextElementSibling(exclusion_node)) { | |
1565 | xmlChar *content = | |
1566 | xmlNodeGetContent(exclusion_node); | |
1567 | ||
1568 | if (!content) { | |
1569 | ret = -LTTNG_ERR_NOMEM; | |
1570 | goto end; | |
1571 | } | |
1572 | ||
1573 | exclusions[exclusion_index] = strdup((const char *) content); | |
1574 | free(content); | |
1575 | if (!exclusions[exclusion_index]) { | |
1576 | ret = -LTTNG_ERR_NOMEM; | |
1577 | goto end; | |
1578 | } | |
1579 | exclusion_index++; | |
1580 | } | |
1581 | ||
1582 | event.exclusion = 1; | |
1583 | } else if (!strcmp((const char *) node->name, | |
1584 | config_element_attributes)) { | |
1585 | xmlNodePtr attribute_node = xmlFirstElementChild(node); | |
1586 | ||
1587 | /* attributes */ | |
1588 | if (!attribute_node) { | |
1589 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1590 | goto end; | |
1591 | } | |
1592 | ||
1593 | if (!strcmp((const char *) node->name, | |
1594 | config_element_probe_attributes)) { | |
1595 | xmlNodePtr probe_attribute_node; | |
1596 | ||
1597 | /* probe_attributes */ | |
1598 | for (probe_attribute_node = | |
1599 | xmlFirstElementChild(attribute_node); probe_attribute_node; | |
1600 | probe_attribute_node = xmlNextElementSibling( | |
1601 | probe_attribute_node)) { | |
1602 | ||
1603 | ret = process_probe_attribute_node(probe_attribute_node, | |
1604 | &event.attr.probe); | |
1605 | if (ret) { | |
1606 | goto end; | |
1607 | } | |
1608 | } | |
1609 | } else { | |
1610 | size_t sym_len; | |
1611 | xmlChar *content; | |
1612 | xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node); | |
1613 | ||
1614 | /* function_attributes */ | |
1615 | content = xmlNodeGetContent(symbol_node); | |
1616 | if (!content) { | |
1617 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1618 | goto end; | |
1619 | } | |
1620 | ||
1621 | sym_len = strlen((char *) content); | |
1622 | if (sym_len >= LTTNG_SYMBOL_NAME_LEN) { | |
1623 | WARN("Function name too long."); | |
1624 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1625 | free(content); | |
1626 | goto end; | |
1627 | } | |
1628 | ||
1629 | strncpy(event.attr.ftrace.symbol_name, (char *) content, | |
1630 | sym_len); | |
1631 | free(content); | |
1632 | } | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | ret = lttng_enable_event_with_exclusions(handle, &event, channel_name, | |
1637 | filter_expression, exclusion_count, exclusions); | |
3278da59 JG |
1638 | if (ret) { |
1639 | goto end; | |
1640 | } | |
1641 | ||
1642 | if (!event.enabled) { | |
1643 | /* | |
1644 | * Note that we should use lttng_disable_event_ext() (2.6+) to | |
1645 | * eliminate the risk of clashing on events of the same | |
1646 | * name (with different event types and loglevels). | |
1647 | * | |
1648 | * Unfortunately, lttng_disable_event_ext() only performs a | |
1649 | * match on the name and event type and errors out if any other | |
1650 | * event attribute is not set to its default value. | |
1651 | * | |
1652 | * This will disable all events that match this name. | |
1653 | */ | |
1654 | ret = lttng_disable_event(handle, event.name, channel_name); | |
1655 | } | |
dcf266c0 JG |
1656 | end: |
1657 | for (i = 0; i < exclusion_count; i++) { | |
1658 | free(exclusions[i]); | |
1659 | } | |
1660 | ||
1661 | free(exclusions); | |
1662 | free(filter_expression); | |
1663 | return ret; | |
1664 | } | |
1665 | ||
1666 | static | |
1667 | int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle, | |
1668 | const char *channel_name) | |
1669 | { | |
1670 | int ret = 0; | |
1671 | xmlNodePtr node; | |
1672 | ||
1673 | assert(events_node); | |
1674 | assert(handle); | |
1675 | assert(channel_name); | |
1676 | ||
1677 | for (node = xmlFirstElementChild(events_node); node; | |
1678 | node = xmlNextElementSibling(node)) { | |
1679 | ret = process_event_node(node, handle, channel_name); | |
1680 | if (ret) { | |
1681 | goto end; | |
1682 | } | |
1683 | } | |
1684 | end: | |
1685 | return ret; | |
1686 | } | |
1687 | ||
1688 | static | |
1689 | int process_channel_attr_node(xmlNodePtr attr_node, | |
1690 | struct lttng_channel *channel, xmlNodePtr *contexts_node, | |
1691 | xmlNodePtr *events_node) | |
1692 | { | |
1693 | int ret; | |
1694 | ||
1695 | assert(attr_node); | |
1696 | assert(channel); | |
1697 | assert(contexts_node); | |
1698 | assert(events_node); | |
1699 | ||
1700 | if (!strcmp((const char *) attr_node->name, config_element_name)) { | |
1701 | xmlChar *content; | |
1702 | size_t name_len; | |
1703 | ||
1704 | /* name */ | |
1705 | content = xmlNodeGetContent(attr_node); | |
1706 | if (!content) { | |
1707 | ret = -LTTNG_ERR_NOMEM; | |
1708 | goto end; | |
1709 | } | |
1710 | ||
1711 | name_len = strlen((char *) content); | |
1712 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { | |
1713 | WARN("Channel name too long."); | |
1714 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1715 | free(content); | |
1716 | goto end; | |
1717 | } | |
1718 | ||
1719 | strncpy(channel->name, (const char *) content, name_len); | |
1720 | free(content); | |
1721 | } else if (!strcmp((const char *) attr_node->name, | |
1722 | config_element_enabled)) { | |
1723 | xmlChar *content; | |
1724 | int enabled; | |
1725 | ||
1726 | /* enabled */ | |
1727 | content = xmlNodeGetContent(attr_node); | |
1728 | if (!content) { | |
1729 | ret = -LTTNG_ERR_NOMEM; | |
1730 | goto end; | |
1731 | } | |
1732 | ||
1733 | ret = parse_bool(content, &enabled); | |
1734 | free(content); | |
1735 | if (ret) { | |
1736 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1737 | goto end; | |
1738 | } | |
1739 | ||
1740 | channel->enabled = enabled; | |
1741 | } else if (!strcmp((const char *) attr_node->name, | |
1742 | config_element_overwrite_mode)) { | |
1743 | xmlChar *content; | |
1744 | ||
1745 | /* overwrite_mode */ | |
1746 | content = xmlNodeGetContent(attr_node); | |
1747 | if (!content) { | |
1748 | ret = -LTTNG_ERR_NOMEM; | |
1749 | goto end; | |
1750 | } | |
1751 | ||
1752 | ret = get_overwrite_mode(content); | |
1753 | free(content); | |
1754 | if (ret < 0) { | |
1755 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1756 | goto end; | |
1757 | } | |
1758 | ||
1759 | channel->attr.overwrite = ret; | |
1760 | } else if (!strcmp((const char *) attr_node->name, | |
1761 | config_element_subbuf_size)) { | |
1762 | xmlChar *content; | |
1763 | ||
1764 | /* subbuffer_size */ | |
1765 | content = xmlNodeGetContent(attr_node); | |
1766 | if (!content) { | |
1767 | ret = -LTTNG_ERR_NOMEM; | |
1768 | goto end; | |
1769 | } | |
1770 | ||
1771 | ret = parse_uint(content, &channel->attr.subbuf_size); | |
1772 | free(content); | |
1773 | if (ret) { | |
1774 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1775 | goto end; | |
1776 | } | |
1777 | } else if (!strcmp((const char *) attr_node->name, | |
1778 | config_element_num_subbuf)) { | |
1779 | xmlChar *content; | |
1780 | ||
1781 | /* subbuffer_count */ | |
1782 | content = xmlNodeGetContent(attr_node); | |
1783 | if (!content) { | |
1784 | ret = -LTTNG_ERR_NOMEM; | |
1785 | goto end; | |
1786 | } | |
1787 | ||
1788 | ret = parse_uint(content, &channel->attr.num_subbuf); | |
1789 | free(content); | |
1790 | if (ret) { | |
1791 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1792 | goto end; | |
1793 | } | |
1794 | } else if (!strcmp((const char *) attr_node->name, | |
1795 | config_element_switch_timer_interval)) { | |
1796 | xmlChar *content; | |
1797 | uint64_t switch_timer_interval = 0; | |
1798 | ||
1799 | /* switch_timer_interval */ | |
1800 | content = xmlNodeGetContent(attr_node); | |
1801 | if (!content) { | |
1802 | ret = -LTTNG_ERR_NOMEM; | |
1803 | goto end; | |
1804 | } | |
1805 | ||
1806 | ret = parse_uint(content, &switch_timer_interval); | |
1807 | free(content); | |
1808 | if (ret) { | |
1809 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1810 | goto end; | |
1811 | } | |
1812 | ||
1813 | if (switch_timer_interval > UINT_MAX) { | |
1814 | WARN("switch_timer_interval out of range."); | |
1815 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1816 | goto end; | |
1817 | } | |
1818 | ||
1819 | channel->attr.switch_timer_interval = | |
1820 | switch_timer_interval; | |
1821 | } else if (!strcmp((const char *) attr_node->name, | |
1822 | config_element_read_timer_interval)) { | |
1823 | xmlChar *content; | |
1824 | uint64_t read_timer_interval = 0; | |
1825 | ||
1826 | /* read_timer_interval */ | |
1827 | content = xmlNodeGetContent(attr_node); | |
1828 | if (!content) { | |
1829 | ret = -LTTNG_ERR_NOMEM; | |
1830 | goto end; | |
1831 | } | |
1832 | ||
1833 | ret = parse_uint(content, &read_timer_interval); | |
1834 | free(content); | |
1835 | if (ret) { | |
1836 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1837 | goto end; | |
1838 | } | |
1839 | ||
1840 | if (read_timer_interval > UINT_MAX) { | |
1841 | WARN("read_timer_interval out of range."); | |
1842 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1843 | goto end; | |
1844 | } | |
1845 | ||
1846 | channel->attr.read_timer_interval = | |
1847 | read_timer_interval; | |
1848 | } else if (!strcmp((const char *) attr_node->name, | |
1849 | config_element_output_type)) { | |
1850 | xmlChar *content; | |
1851 | ||
1852 | /* output_type */ | |
1853 | content = xmlNodeGetContent(attr_node); | |
1854 | if (!content) { | |
1855 | ret = -LTTNG_ERR_NOMEM; | |
1856 | goto end; | |
1857 | } | |
1858 | ||
1859 | ret = get_output_type(content); | |
1860 | free(content); | |
1861 | if (ret < 0) { | |
1862 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1863 | goto end; | |
1864 | } | |
1865 | ||
1866 | channel->attr.output = ret; | |
1867 | } else if (!strcmp((const char *) attr_node->name, | |
1868 | config_element_tracefile_size)) { | |
1869 | xmlChar *content; | |
1870 | ||
1871 | /* tracefile_size */ | |
1872 | content = xmlNodeGetContent(attr_node); | |
1873 | if (!content) { | |
1874 | ret = -LTTNG_ERR_NOMEM; | |
1875 | goto end; | |
1876 | } | |
1877 | ||
1878 | ret = parse_uint(content, &channel->attr.tracefile_size); | |
1879 | free(content); | |
1880 | if (ret) { | |
1881 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1882 | goto end; | |
1883 | } | |
1884 | } else if (!strcmp((const char *) attr_node->name, | |
1885 | config_element_tracefile_count)) { | |
1886 | xmlChar *content; | |
1887 | ||
1888 | /* tracefile_count */ | |
1889 | content = xmlNodeGetContent(attr_node); | |
1890 | if (!content) { | |
1891 | ret = -LTTNG_ERR_NOMEM; | |
1892 | goto end; | |
1893 | } | |
1894 | ||
1895 | ret = parse_uint(content, &channel->attr.tracefile_count); | |
1896 | free(content); | |
1897 | if (ret) { | |
1898 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1899 | goto end; | |
1900 | } | |
1901 | } else if (!strcmp((const char *) attr_node->name, | |
1902 | config_element_live_timer_interval)) { | |
1903 | xmlChar *content; | |
1904 | uint64_t live_timer_interval = 0; | |
1905 | ||
1906 | /* live_timer_interval */ | |
1907 | content = xmlNodeGetContent(attr_node); | |
1908 | if (!content) { | |
1909 | ret = -LTTNG_ERR_NOMEM; | |
1910 | goto end; | |
1911 | } | |
1912 | ||
1913 | ret = parse_uint(content, &live_timer_interval); | |
1914 | free(content); | |
1915 | if (ret) { | |
1916 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1917 | goto end; | |
1918 | } | |
1919 | ||
1920 | if (live_timer_interval > UINT_MAX) { | |
1921 | WARN("live_timer_interval out of range."); | |
1922 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1923 | goto end; | |
1924 | } | |
1925 | ||
1926 | channel->attr.live_timer_interval = | |
1927 | live_timer_interval; | |
1928 | } else if (!strcmp((const char *) attr_node->name, | |
1929 | config_element_events)) { | |
1930 | /* events */ | |
1931 | *events_node = attr_node; | |
1932 | } else { | |
1933 | /* contexts */ | |
1934 | *contexts_node = attr_node; | |
1935 | } | |
1936 | ret = 0; | |
1937 | end: | |
1938 | return ret; | |
1939 | } | |
1940 | ||
1941 | static | |
1942 | int process_context_node(xmlNodePtr context_node, | |
1943 | struct lttng_handle *handle, const char *channel_name) | |
1944 | { | |
1945 | int ret; | |
1946 | struct lttng_event_context context; | |
1947 | xmlNodePtr context_child_node = xmlFirstElementChild(context_node); | |
1948 | ||
1949 | assert(handle); | |
1950 | assert(channel_name); | |
1951 | ||
1952 | if (!context_child_node) { | |
1953 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1954 | goto end; | |
1955 | } | |
1956 | ||
1957 | memset(&context, 0, sizeof(context)); | |
1958 | ||
1959 | if (!strcmp((const char *) context_child_node->name, | |
1960 | config_element_type)) { | |
1961 | /* type */ | |
1962 | xmlChar *content = xmlNodeGetContent(context_child_node); | |
1963 | if (!content) { | |
1964 | ret = -LTTNG_ERR_NOMEM; | |
1965 | goto end; | |
1966 | } | |
1967 | ||
1968 | ret = get_context_type(content); | |
1969 | free(content); | |
1970 | if (ret < 0) { | |
1971 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
1972 | goto end; | |
1973 | } | |
1974 | ||
1975 | context.ctx = ret; | |
1976 | } else { | |
1977 | xmlNodePtr perf_attr_node; | |
1978 | ||
1979 | /* perf */ | |
14ce5bd8 JG |
1980 | context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ? |
1981 | LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER : | |
1982 | LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER; | |
dcf266c0 JG |
1983 | for (perf_attr_node = xmlFirstElementChild(context_child_node); |
1984 | perf_attr_node; perf_attr_node = | |
1985 | xmlNextElementSibling(perf_attr_node)) { | |
1986 | if (!strcmp((const char *) perf_attr_node->name, | |
1987 | config_element_type)) { | |
1988 | xmlChar *content; | |
1989 | uint64_t type = 0; | |
1990 | ||
1991 | /* type */ | |
1992 | content = xmlNodeGetContent(perf_attr_node); | |
1993 | if (!content) { | |
1994 | ret = -LTTNG_ERR_NOMEM; | |
1995 | goto end; | |
1996 | } | |
1997 | ||
1998 | ret = parse_uint(content, &type); | |
1999 | free(content); | |
2000 | if (ret) { | |
2001 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2002 | goto end; | |
2003 | } | |
2004 | ||
2005 | if (type > UINT32_MAX) { | |
2006 | WARN("perf context type out of range."); | |
2007 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2008 | goto end; | |
2009 | } | |
2010 | ||
2011 | context.u.perf_counter.type = type; | |
2012 | } else if (!strcmp((const char *) perf_attr_node->name, | |
2013 | config_element_config)) { | |
2014 | xmlChar *content; | |
2015 | uint64_t config = 0; | |
2016 | ||
2017 | /* config */ | |
2018 | content = xmlNodeGetContent(perf_attr_node); | |
2019 | if (!content) { | |
2020 | ret = -LTTNG_ERR_NOMEM; | |
2021 | goto end; | |
2022 | } | |
2023 | ||
2024 | ret = parse_uint(content, &config); | |
2025 | free(content); | |
2026 | if (ret) { | |
2027 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2028 | goto end; | |
2029 | } | |
2030 | ||
2031 | context.u.perf_counter.config = config; | |
2032 | } else if (!strcmp((const char *) perf_attr_node->name, | |
2033 | config_element_name)) { | |
2034 | xmlChar *content; | |
2035 | size_t name_len; | |
2036 | ||
2037 | /* name */ | |
2038 | content = xmlNodeGetContent(perf_attr_node); | |
2039 | if (!content) { | |
2040 | ret = -LTTNG_ERR_NOMEM; | |
2041 | goto end; | |
2042 | } | |
2043 | ||
2044 | name_len = strlen((char *) content); | |
2045 | if (name_len >= LTTNG_SYMBOL_NAME_LEN) { | |
2046 | WARN("perf context name too long."); | |
2047 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2048 | free(content); | |
2049 | goto end; | |
2050 | } | |
2051 | ||
2052 | strncpy(context.u.perf_counter.name, (const char *) content, | |
2053 | name_len); | |
2054 | free(content); | |
2055 | } | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | ret = lttng_add_context(handle, &context, NULL, channel_name); | |
2060 | end: | |
2061 | return ret; | |
2062 | } | |
2063 | ||
2064 | static | |
2065 | int process_contexts_node(xmlNodePtr contexts_node, | |
2066 | struct lttng_handle *handle, const char *channel_name) | |
2067 | { | |
2068 | int ret = 0; | |
2069 | xmlNodePtr context_node; | |
2070 | ||
2071 | for (context_node = xmlFirstElementChild(contexts_node); context_node; | |
2072 | context_node = xmlNextElementSibling(context_node)) { | |
2073 | ret = process_context_node(context_node, handle, channel_name); | |
2074 | if (ret) { | |
2075 | goto end; | |
2076 | } | |
2077 | } | |
2078 | end: | |
2079 | return ret; | |
2080 | } | |
2081 | ||
2082 | static | |
2083 | int process_domain_node(xmlNodePtr domain_node, const char *session_name) | |
2084 | { | |
2085 | int ret; | |
2086 | struct lttng_domain domain = { 0 }; | |
2087 | struct lttng_handle *handle = NULL; | |
2088 | xmlNodePtr channels_node = NULL; | |
2089 | xmlNodePtr node; | |
2090 | ||
2091 | assert(session_name); | |
2092 | ||
2093 | ret = init_domain(domain_node, &domain); | |
2094 | if (ret) { | |
2095 | goto end; | |
2096 | } | |
2097 | ||
2098 | handle = lttng_create_handle(session_name, &domain); | |
2099 | if (!handle) { | |
2100 | ret = -LTTNG_ERR_NOMEM; | |
2101 | goto end; | |
2102 | } | |
2103 | ||
2104 | /* get the channels node */ | |
2105 | for (node = xmlFirstElementChild(domain_node); node; | |
2106 | node = xmlNextElementSibling(node)) { | |
2107 | if (!strcmp((const char *) node->name, | |
2108 | config_element_channels)) { | |
2109 | channels_node = node; | |
2110 | break; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | if (!channels_node) { | |
2115 | goto end; | |
2116 | } | |
2117 | ||
2118 | /* create all channels */ | |
2119 | for (node = xmlFirstElementChild(channels_node); node; | |
2120 | node = xmlNextElementSibling(node)) { | |
2121 | struct lttng_channel channel; | |
2122 | xmlNodePtr contexts_node = NULL; | |
2123 | xmlNodePtr events_node = NULL; | |
2124 | xmlNodePtr channel_attr_node; | |
2125 | ||
2126 | memset(&channel, 0, sizeof(channel)); | |
2127 | lttng_channel_set_default_attr(&domain, &channel.attr); | |
2128 | ||
2129 | for (channel_attr_node = xmlFirstElementChild(node); | |
2130 | channel_attr_node; channel_attr_node = | |
2131 | xmlNextElementSibling(channel_attr_node)) { | |
2132 | ret = process_channel_attr_node(channel_attr_node, | |
2133 | &channel, &contexts_node, &events_node); | |
2134 | if (ret) { | |
2135 | goto end; | |
2136 | } | |
2137 | } | |
2138 | ||
2139 | ret = lttng_enable_channel(handle, &channel); | |
2140 | if (ret < 0) { | |
2141 | goto end; | |
2142 | } | |
2143 | ||
2144 | ret = process_events_node(events_node, handle, channel.name); | |
2145 | if (ret) { | |
2146 | goto end; | |
2147 | } | |
2148 | ||
2149 | ret = process_contexts_node(contexts_node, handle, | |
2150 | channel.name); | |
2151 | if (ret) { | |
2152 | goto end; | |
2153 | } | |
2154 | } | |
2155 | end: | |
2156 | lttng_destroy_handle(handle); | |
2157 | return ret; | |
2158 | } | |
2159 | ||
2160 | static | |
2161 | int process_session_node(xmlNodePtr session_node, const char *session_name, | |
2162 | int override) | |
2163 | { | |
2164 | int ret, started = -1, snapshot_mode = -1; | |
2165 | uint64_t live_timer_interval = UINT64_MAX; | |
25248c1c | 2166 | xmlChar *name = NULL; |
dcf266c0 JG |
2167 | xmlNodePtr domains_node = NULL; |
2168 | xmlNodePtr output_node = NULL; | |
2169 | xmlNodePtr node; | |
2170 | struct lttng_domain *kernel_domain = NULL; | |
2171 | struct lttng_domain *ust_domain = NULL; | |
2172 | struct lttng_domain *jul_domain = NULL; | |
5cdb6027 | 2173 | struct lttng_domain *log4j_domain = NULL; |
dcf266c0 JG |
2174 | |
2175 | for (node = xmlFirstElementChild(session_node); node; | |
2176 | node = xmlNextElementSibling(node)) { | |
2177 | if (!name && !strcmp((const char *) node->name, | |
2178 | config_element_name)) { | |
2179 | /* name */ | |
2180 | xmlChar *node_content = xmlNodeGetContent(node); | |
2181 | if (!node_content) { | |
2182 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2183 | goto error; |
dcf266c0 JG |
2184 | } |
2185 | ||
25248c1c | 2186 | name = node_content; |
dcf266c0 JG |
2187 | } else if (!domains_node && !strcmp((const char *) node->name, |
2188 | config_element_domains)) { | |
2189 | /* domains */ | |
2190 | domains_node = node; | |
2191 | } else if (started == -1 && !strcmp((const char *) node->name, | |
2192 | config_element_started)) { | |
2193 | /* started */ | |
2194 | xmlChar *node_content = xmlNodeGetContent(node); | |
2195 | if (!node_content) { | |
2196 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2197 | goto error; |
dcf266c0 JG |
2198 | } |
2199 | ||
2200 | ret = parse_bool(node_content, &started); | |
2201 | free(node_content); | |
2202 | if (ret) { | |
2203 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2204 | goto error; |
dcf266c0 JG |
2205 | } |
2206 | } else if (!output_node && !strcmp((const char *) node->name, | |
2207 | config_element_output)) { | |
2208 | /* output */ | |
2209 | output_node = node; | |
2210 | } else { | |
2211 | /* attributes, snapshot_mode or live_timer_interval */ | |
2212 | xmlNodePtr attributes_child = | |
2213 | xmlFirstElementChild(node); | |
2214 | ||
2215 | if (!strcmp((const char *) attributes_child->name, | |
2216 | config_element_snapshot_mode)) { | |
2217 | /* snapshot_mode */ | |
2218 | xmlChar *snapshot_mode_content = | |
2219 | xmlNodeGetContent(attributes_child); | |
2220 | if (!snapshot_mode_content) { | |
2221 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2222 | goto error; |
dcf266c0 JG |
2223 | } |
2224 | ||
2225 | ret = parse_bool(snapshot_mode_content, &snapshot_mode); | |
2226 | free(snapshot_mode_content); | |
2227 | if (ret) { | |
2228 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2229 | goto error; |
dcf266c0 JG |
2230 | } |
2231 | } else { | |
2232 | /* live_timer_interval */ | |
2233 | xmlChar *timer_interval_content = | |
2234 | xmlNodeGetContent(attributes_child); | |
2235 | if (!timer_interval_content) { | |
2236 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2237 | goto error; |
dcf266c0 JG |
2238 | } |
2239 | ||
2240 | ret = parse_uint(timer_interval_content, &live_timer_interval); | |
2241 | free(timer_interval_content); | |
2242 | if (ret) { | |
2243 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2244 | goto error; |
dcf266c0 JG |
2245 | } |
2246 | } | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | if (!name) { | |
2251 | /* Mandatory attribute, as defined in the session XSD */ | |
2252 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2253 | goto error; |
dcf266c0 JG |
2254 | } |
2255 | ||
25248c1c | 2256 | if (session_name && strcmp((char *) name, session_name)) { |
dcf266c0 | 2257 | /* This is not the session we are looking for */ |
c2da8cde DG |
2258 | ret = -LTTNG_ERR_NO_SESSION; |
2259 | goto error; | |
dcf266c0 JG |
2260 | } |
2261 | ||
2262 | /* Init domains to create the session handles */ | |
2263 | for (node = xmlFirstElementChild(domains_node); node; | |
2264 | node = xmlNextElementSibling(node)) { | |
2265 | struct lttng_domain *domain; | |
2266 | ||
2267 | domain = zmalloc(sizeof(*domain)); | |
2268 | if (!domain) { | |
2269 | ret = -LTTNG_ERR_NOMEM; | |
c2da8cde | 2270 | goto error; |
dcf266c0 JG |
2271 | } |
2272 | ||
2273 | ret = init_domain(node, domain); | |
2274 | if (ret) { | |
2275 | goto domain_init_error; | |
2276 | } | |
2277 | ||
2278 | switch (domain->type) { | |
2279 | case LTTNG_DOMAIN_KERNEL: | |
c33e6729 DG |
2280 | if (kernel_domain) { |
2281 | /* Same domain seen twice, invalid! */ | |
2282 | goto domain_init_error; | |
2283 | } | |
dcf266c0 JG |
2284 | kernel_domain = domain; |
2285 | break; | |
2286 | case LTTNG_DOMAIN_UST: | |
c33e6729 DG |
2287 | if (ust_domain) { |
2288 | /* Same domain seen twice, invalid! */ | |
2289 | goto domain_init_error; | |
2290 | } | |
dcf266c0 JG |
2291 | ust_domain = domain; |
2292 | break; | |
2293 | case LTTNG_DOMAIN_JUL: | |
c33e6729 DG |
2294 | if (jul_domain) { |
2295 | /* Same domain seen twice, invalid! */ | |
2296 | goto domain_init_error; | |
2297 | } | |
dcf266c0 JG |
2298 | jul_domain = domain; |
2299 | break; | |
5cdb6027 DG |
2300 | case LTTNG_DOMAIN_LOG4J: |
2301 | if (log4j_domain) { | |
2302 | /* Same domain seen twice, invalid! */ | |
2303 | goto domain_init_error; | |
2304 | } | |
2305 | log4j_domain = domain; | |
2306 | break; | |
dcf266c0 JG |
2307 | default: |
2308 | WARN("Invalid domain type"); | |
2309 | goto domain_init_error; | |
2310 | } | |
2311 | continue; | |
2312 | domain_init_error: | |
2313 | free(domain); | |
2314 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
c2da8cde | 2315 | goto error; |
dcf266c0 JG |
2316 | } |
2317 | ||
2318 | if (override) { | |
2319 | /* Destroy session if it exists */ | |
25248c1c | 2320 | ret = lttng_destroy_session((const char *) name); |
dcf266c0 JG |
2321 | if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) { |
2322 | ERR("Failed to destroy existing session."); | |
c2da8cde | 2323 | goto error; |
dcf266c0 JG |
2324 | } |
2325 | } | |
2326 | ||
2327 | /* Create session type depending on output type */ | |
2328 | if (snapshot_mode && snapshot_mode != -1) { | |
25248c1c | 2329 | ret = create_snapshot_session((const char *) name, output_node); |
dcf266c0 JG |
2330 | } else if (live_timer_interval && |
2331 | live_timer_interval != UINT64_MAX) { | |
25248c1c JG |
2332 | ret = create_session((const char *) name, kernel_domain, |
2333 | ust_domain, jul_domain, log4j_domain, | |
2334 | output_node, live_timer_interval); | |
dcf266c0 JG |
2335 | } else { |
2336 | /* regular session */ | |
25248c1c JG |
2337 | ret = create_session((const char *) name, kernel_domain, |
2338 | ust_domain, jul_domain, log4j_domain, | |
2339 | output_node, UINT64_MAX); | |
dcf266c0 | 2340 | } |
dcf266c0 | 2341 | if (ret) { |
c2da8cde | 2342 | goto error; |
dcf266c0 JG |
2343 | } |
2344 | ||
2345 | for (node = xmlFirstElementChild(domains_node); node; | |
2346 | node = xmlNextElementSibling(node)) { | |
25248c1c | 2347 | ret = process_domain_node(node, (const char *) name); |
dcf266c0 JG |
2348 | if (ret) { |
2349 | goto end; | |
2350 | } | |
2351 | } | |
2352 | ||
2353 | if (started) { | |
25248c1c | 2354 | ret = lttng_start_tracing((const char *) name); |
dcf266c0 JG |
2355 | if (ret) { |
2356 | goto end; | |
2357 | } | |
2358 | } | |
c2da8cde | 2359 | |
dcf266c0 | 2360 | end: |
b2579dc1 | 2361 | if (ret < 0) { |
25248c1c JG |
2362 | ERR("Failed to load session %s: %s", (const char *) name, |
2363 | lttng_strerror(ret)); | |
2364 | lttng_destroy_session((const char *) name); | |
b2579dc1 JG |
2365 | } |
2366 | ||
c2da8cde | 2367 | error: |
dcf266c0 JG |
2368 | free(kernel_domain); |
2369 | free(ust_domain); | |
2370 | free(jul_domain); | |
5cdb6027 | 2371 | free(log4j_domain); |
25248c1c | 2372 | xmlFree(name); |
dcf266c0 JG |
2373 | return ret; |
2374 | } | |
2375 | ||
cf53c06d DG |
2376 | /* |
2377 | * Return 1 if the given path is readable by the current UID or 0 if not. | |
2378 | * Return -1 if the path is EPERM. | |
2379 | */ | |
2380 | static int validate_file_read_creds(const char *path) | |
2381 | { | |
2382 | int ret; | |
2383 | ||
2384 | assert(path); | |
2385 | ||
2386 | /* Can we read the file. */ | |
2387 | ret = access(path, R_OK); | |
2388 | if (!ret) { | |
2389 | goto valid; | |
2390 | } | |
2391 | if (errno == EACCES) { | |
2392 | return -1; | |
2393 | } else { | |
2394 | /* Invalid. */ | |
2395 | return 0; | |
2396 | } | |
2397 | valid: | |
2398 | return 1; | |
2399 | } | |
2400 | ||
dcf266c0 JG |
2401 | static |
2402 | int load_session_from_file(const char *path, const char *session_name, | |
2403 | struct session_config_validation_ctx *validation_ctx, int override) | |
2404 | { | |
2405 | int ret, session_found = !session_name; | |
2406 | xmlDocPtr doc = NULL; | |
2407 | xmlNodePtr sessions_node; | |
2408 | xmlNodePtr session_node; | |
dcf266c0 JG |
2409 | |
2410 | assert(path); | |
2411 | assert(validation_ctx); | |
2412 | ||
cf53c06d DG |
2413 | ret = validate_file_read_creds(path); |
2414 | if (ret != 1) { | |
2415 | if (ret == -1) { | |
2416 | ret = -LTTNG_ERR_EPERM; | |
2417 | } else { | |
2418 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; | |
2419 | } | |
dcf266c0 JG |
2420 | goto end; |
2421 | } | |
2422 | ||
2423 | doc = xmlParseFile(path); | |
2424 | if (!doc) { | |
2425 | ret = -LTTNG_ERR_LOAD_IO_FAIL; | |
2426 | goto end; | |
2427 | } | |
2428 | ||
2429 | ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc); | |
2430 | if (ret) { | |
2431 | ERR("Session configuration file validation failed"); | |
2432 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
2433 | goto end; | |
2434 | } | |
2435 | ||
2436 | sessions_node = xmlDocGetRootElement(doc); | |
2437 | if (!sessions_node) { | |
2438 | goto end; | |
2439 | } | |
2440 | ||
2441 | for (session_node = xmlFirstElementChild(sessions_node); | |
2442 | session_node; session_node = | |
2443 | xmlNextElementSibling(session_node)) { | |
2444 | ret = process_session_node(session_node, | |
2445 | session_name, override); | |
2446 | if (session_name && ret == 0) { | |
2447 | /* Target session found and loaded */ | |
2448 | session_found = 1; | |
2449 | break; | |
2450 | } | |
2451 | } | |
2452 | end: | |
2453 | xmlFreeDoc(doc); | |
2454 | if (!ret) { | |
a96bc65d | 2455 | ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT; |
dcf266c0 JG |
2456 | } |
2457 | return ret; | |
2458 | } | |
2459 | ||
dc5141b9 JG |
2460 | /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ |
2461 | static | |
2462 | struct dirent *alloc_dirent(const char *path) | |
2463 | { | |
2464 | size_t len; | |
2465 | long name_max; | |
2466 | struct dirent *entry; | |
2467 | ||
2468 | name_max = pathconf(path, _PC_NAME_MAX); | |
2469 | if (name_max == -1) { | |
2470 | name_max = PATH_MAX; | |
2471 | } | |
2472 | len = offsetof(struct dirent, d_name) + name_max + 1; | |
2473 | entry = zmalloc(len); | |
2474 | return entry; | |
2475 | } | |
2476 | ||
dcf266c0 JG |
2477 | static |
2478 | int load_session_from_path(const char *path, const char *session_name, | |
2479 | struct session_config_validation_ctx *validation_ctx, int override) | |
2480 | { | |
2481 | int ret, session_found = !session_name; | |
dcf266c0 JG |
2482 | DIR *directory = NULL; |
2483 | ||
2484 | assert(path); | |
2485 | assert(validation_ctx); | |
2486 | ||
4af16958 DG |
2487 | directory = opendir(path); |
2488 | if (!directory) { | |
11143783 DG |
2489 | switch (errno) { |
2490 | case ENOTDIR: | |
0f0a81b5 DG |
2491 | /* Try the file loading. */ |
2492 | break; | |
11143783 DG |
2493 | case ENOENT: |
2494 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; | |
2495 | goto end; | |
2496 | default: | |
0f0a81b5 DG |
2497 | ret = -LTTNG_ERR_LOAD_IO_FAIL; |
2498 | goto end; | |
4af16958 | 2499 | } |
dcf266c0 | 2500 | } |
4af16958 | 2501 | if (directory) { |
dcf266c0 JG |
2502 | struct dirent *entry; |
2503 | struct dirent *result; | |
2504 | char *file_path = NULL; | |
2505 | size_t path_len = strlen(path); | |
2506 | ||
2507 | if (path_len >= PATH_MAX) { | |
2508 | ret = -LTTNG_ERR_INVALID; | |
2509 | goto end; | |
2510 | } | |
2511 | ||
dc5141b9 | 2512 | entry = alloc_dirent(path); |
dcf266c0 JG |
2513 | if (!entry) { |
2514 | ret = -LTTNG_ERR_NOMEM; | |
2515 | goto end; | |
2516 | } | |
2517 | ||
dcf266c0 JG |
2518 | file_path = zmalloc(PATH_MAX); |
2519 | if (!file_path) { | |
2520 | ret = -LTTNG_ERR_NOMEM; | |
2521 | free(entry); | |
2522 | goto end; | |
2523 | } | |
2524 | ||
2525 | strncpy(file_path, path, path_len); | |
2526 | if (file_path[path_len - 1] != '/') { | |
2527 | file_path[path_len++] = '/'; | |
2528 | } | |
2529 | ||
4af16958 | 2530 | ret = 0; |
dcf266c0 JG |
2531 | /* Search for *.lttng files */ |
2532 | while (!readdir_r(directory, entry, &result) && result) { | |
2533 | size_t file_name_len = strlen(result->d_name); | |
2534 | ||
2535 | if (file_name_len <= | |
2536 | sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) { | |
2537 | continue; | |
2538 | } | |
2539 | ||
431f702e | 2540 | if (path_len + file_name_len >= PATH_MAX) { |
dcf266c0 JG |
2541 | continue; |
2542 | } | |
2543 | ||
2544 | if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION, | |
2545 | result->d_name + file_name_len - sizeof( | |
2546 | DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { | |
2547 | continue; | |
2548 | } | |
2549 | ||
2550 | strncpy(file_path + path_len, result->d_name, file_name_len); | |
2551 | file_path[path_len + file_name_len] = '\0'; | |
2552 | ||
2553 | ret = load_session_from_file(file_path, session_name, | |
2554 | validation_ctx, override); | |
2555 | if (session_name && !ret) { | |
2556 | session_found = 1; | |
2557 | break; | |
2558 | } | |
2559 | } | |
2560 | ||
2561 | free(entry); | |
2562 | free(file_path); | |
2563 | } else { | |
2564 | ret = load_session_from_file(path, session_name, | |
2565 | validation_ctx, override); | |
2566 | if (ret) { | |
2567 | goto end; | |
2568 | } else { | |
2569 | session_found = 1; | |
2570 | } | |
2571 | } | |
2572 | ||
2573 | end: | |
2574 | if (directory) { | |
2575 | if (closedir(directory)) { | |
2576 | PERROR("closedir"); | |
2577 | } | |
2578 | } | |
2579 | ||
2580 | if (!session_found) { | |
a96bc65d | 2581 | ret = -LTTNG_ERR_LOAD_SESSION_NOENT; |
dcf266c0 JG |
2582 | } |
2583 | ||
2584 | return ret; | |
2585 | } | |
2586 | ||
ab38c13f DG |
2587 | /* |
2588 | * Validate that the given path's credentials and the current process have the | |
cf53c06d | 2589 | * same UID. If so, return 1 else return 0 if it does NOT match. |
ab38c13f DG |
2590 | */ |
2591 | static int validate_path_creds(const char *path) | |
2592 | { | |
2593 | int ret, uid = getuid(); | |
2594 | struct stat buf; | |
2595 | ||
2596 | assert(path); | |
2597 | ||
cf53c06d | 2598 | if (uid == 0) { |
ab38c13f DG |
2599 | goto valid; |
2600 | } | |
2601 | ||
2602 | ret = stat(path, &buf); | |
2603 | if (ret < 0) { | |
2604 | if (errno != ENOENT) { | |
2605 | PERROR("stat"); | |
2606 | } | |
2607 | ret = -LTTNG_ERR_INVALID; | |
2608 | goto valid; | |
2609 | } | |
2610 | ||
2611 | if (buf.st_uid != uid) { | |
2612 | goto invalid; | |
2613 | } | |
2614 | ||
2615 | valid: | |
ab38c13f | 2616 | return 1; |
cf53c06d DG |
2617 | invalid: |
2618 | return 0; | |
ab38c13f DG |
2619 | } |
2620 | ||
dcf266c0 JG |
2621 | LTTNG_HIDDEN |
2622 | int config_load_session(const char *path, const char *session_name, | |
ab38c13f | 2623 | int override, unsigned int autoload) |
dcf266c0 JG |
2624 | { |
2625 | int ret; | |
cf53c06d | 2626 | const char *path_ptr = NULL; |
dcf266c0 JG |
2627 | struct session_config_validation_ctx validation_ctx = { 0 }; |
2628 | ||
2629 | ret = init_session_config_validation_ctx(&validation_ctx); | |
2630 | if (ret) { | |
2631 | goto end; | |
2632 | } | |
2633 | ||
2634 | if (!path) { | |
ab38c13f DG |
2635 | char *home_path; |
2636 | const char *sys_path; | |
2637 | ||
dcf266c0 | 2638 | /* Try home path */ |
ab38c13f | 2639 | home_path = utils_get_home_dir(); |
dcf266c0 | 2640 | if (home_path) { |
ab38c13f | 2641 | char path[PATH_MAX]; |
dcf266c0 | 2642 | |
d4fcf703 DG |
2643 | /* |
2644 | * Try user session configuration path. Ignore error here so we can | |
2645 | * continue loading the system wide sessions. | |
2646 | */ | |
ab38c13f DG |
2647 | if (autoload) { |
2648 | ret = snprintf(path, sizeof(path), | |
2649 | DEFAULT_SESSION_HOME_CONFIGPATH "/" | |
2650 | DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path); | |
cf53c06d DG |
2651 | if (ret < 0) { |
2652 | PERROR("snprintf session autoload home config path"); | |
2653 | goto end; | |
2654 | } | |
2655 | ||
2656 | /* | |
2657 | * Credentials are only validated for the autoload in order to | |
2658 | * avoid any user session daemon to try to load kernel sessions | |
2659 | * automatically and failing all the times. | |
2660 | */ | |
2661 | ret = validate_path_creds(path); | |
2662 | if (ret) { | |
2663 | path_ptr = path; | |
2664 | } | |
ab38c13f DG |
2665 | } else { |
2666 | ret = snprintf(path, sizeof(path), | |
2667 | DEFAULT_SESSION_HOME_CONFIGPATH, home_path); | |
cf53c06d DG |
2668 | if (ret < 0) { |
2669 | PERROR("snprintf session home config path"); | |
2670 | goto end; | |
2671 | } | |
2672 | path_ptr = path; | |
ab38c13f | 2673 | } |
cf53c06d DG |
2674 | if (path_ptr) { |
2675 | ret = load_session_from_path(path_ptr, session_name, | |
d4fcf703 DG |
2676 | &validation_ctx, override); |
2677 | if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) { | |
2678 | goto end; | |
2679 | } | |
2680 | /* | |
2681 | * Continue even if the session was found since we have to try | |
2682 | * the system wide sessions. | |
2683 | */ | |
ab38c13f | 2684 | } |
d4fcf703 | 2685 | } |
ab38c13f | 2686 | |
cf53c06d DG |
2687 | /* Reset path pointer for the system wide dir. */ |
2688 | path_ptr = NULL; | |
2689 | ||
d4fcf703 DG |
2690 | /* Try system wide configuration directory. */ |
2691 | if (autoload) { | |
2692 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/" | |
2693 | DEFAULT_SESSION_CONFIG_AUTOLOAD; | |
cf53c06d DG |
2694 | ret = validate_path_creds(sys_path); |
2695 | if (ret) { | |
2696 | path_ptr = sys_path; | |
2697 | } | |
d4fcf703 | 2698 | } else { |
cf53c06d DG |
2699 | sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH; |
2700 | path_ptr = sys_path; | |
d4fcf703 DG |
2701 | } |
2702 | ||
cf53c06d DG |
2703 | if (path_ptr) { |
2704 | ret = load_session_from_path(path_ptr, session_name, | |
d4fcf703 | 2705 | &validation_ctx, override); |
dcf266c0 JG |
2706 | } |
2707 | } else { | |
2708 | ret = access(path, F_OK); | |
2709 | if (ret < 0) { | |
2710 | PERROR("access"); | |
2711 | switch (errno) { | |
2712 | case ENOENT: | |
2713 | ret = -LTTNG_ERR_INVALID; | |
2714 | WARN("Session configuration path does not exist."); | |
2715 | break; | |
2716 | case EACCES: | |
2717 | ret = -LTTNG_ERR_EPERM; | |
2718 | break; | |
2719 | default: | |
2720 | ret = -LTTNG_ERR_UNK; | |
2721 | break; | |
2722 | } | |
2723 | goto end; | |
2724 | } | |
2725 | ||
2726 | ret = load_session_from_path(path, session_name, | |
2727 | &validation_ctx, override); | |
2728 | } | |
2729 | end: | |
2730 | fini_session_config_validation_ctx(&validation_ctx); | |
d2b6efff JG |
2731 | if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) { |
2732 | /* | |
2733 | * Don't report an error if no sessions are found when called | |
2734 | * without a session_name or a search path. | |
2735 | */ | |
2736 | ret = 0; | |
2737 | } | |
dcf266c0 JG |
2738 | return ret; |
2739 | } |