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