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