Fix: load event state (enabled/disabled) correctly
[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
1105int create_session_net_output(const char *name, struct lttng_domain *domain,
1106 const char *control_uri, const char *data_uri)
1107{
1108 int ret;
1109 struct lttng_handle *handle;
1110 const char *uri = NULL;
1111
1112 assert(name);
1113 assert(domain);
1114
1115 handle = lttng_create_handle(name, domain);
1116 if (!handle) {
1117 ret = -LTTNG_ERR_NOMEM;
1118 goto end;
1119 }
1120
1121 if (!control_uri || !data_uri) {
1122 uri = control_uri ? control_uri : data_uri;
1123 control_uri = uri;
1124 data_uri = uri;
1125 }
1126
1127 ret = lttng_set_consumer_url(handle, control_uri, data_uri);
1128 lttng_destroy_handle(handle);
1129end:
1130 return ret;
1131}
1132
1133static
1134int create_snapshot_session(const char *session_name, xmlNodePtr output_node)
1135{
1136 int ret;
1137 xmlNodePtr node = NULL;
1138 xmlNodePtr snapshot_output_list_node;
1139 xmlNodePtr snapshot_output_node;
1140
1141 assert(session_name);
1142
1143 ret = lttng_create_session_snapshot(session_name, NULL);
1144 if (ret) {
1145 goto end;
1146 }
1147
1148 if (!output_node) {
1149 goto end;
1150 }
1151
1152 snapshot_output_list_node = xmlFirstElementChild(output_node);
1153
1154 /* Parse and create snapshot outputs */
1155
1156 for (snapshot_output_node =
1157 xmlFirstElementChild(snapshot_output_list_node);
1158 snapshot_output_node; snapshot_output_node =
1159 xmlNextElementSibling(snapshot_output_node)) {
1160 char *name = NULL;
1161 uint64_t max_size = UINT64_MAX;
1162 struct consumer_output output = { 0 };
1163 struct lttng_snapshot_output *snapshot_output = NULL;
1164
1165 for (node = xmlFirstElementChild(snapshot_output_node); node;
1166 node = xmlNextElementSibling(node)) {
1167 if (!strcmp((const char *) node->name,
1168 config_element_name)) {
1169 /* name */
1170 name = (char *) xmlNodeGetContent(node);
1171 if (!name) {
1172 ret = -LTTNG_ERR_NOMEM;
1173 goto error_snapshot_output;
1174 }
1175 } else if (!strcmp((const char *) node->name,
1176 config_element_max_size)) {
1177 xmlChar *content = xmlNodeGetContent(node);
1178
1179 /* max_size */
1180 if (!content) {
1181 ret = -LTTNG_ERR_NOMEM;
1182 goto error_snapshot_output;
1183 }
1184 ret = parse_uint(content, &max_size);
1185 free(content);
1186 if (ret) {
1187 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1188 goto error_snapshot_output;
1189 }
1190 } else {
1191 /* consumer_output */
1192 ret = process_consumer_output(node, &output);
1193 if (ret) {
1194 goto error_snapshot_output;
1195 }
1196 }
1197 }
1198
1199 snapshot_output = lttng_snapshot_output_create();
1200 if (!snapshot_output) {
1201 ret = -LTTNG_ERR_NOMEM;
1202 goto error_snapshot_output;
1203 }
1204
1205 ret = lttng_snapshot_output_set_name(name, snapshot_output);
1206 if (ret) {
1207 goto error_snapshot_output;
1208 }
1209
1210 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1211 if (ret) {
1212 goto error_snapshot_output;
1213 }
1214
1215 if (output.path) {
1216 ret = lttng_snapshot_output_set_ctrl_url(output.path,
1217 snapshot_output);
1218 if (ret) {
1219 goto error_snapshot_output;
1220 }
1221 } else {
1222 if (output.control_uri) {
1223 ret = lttng_snapshot_output_set_ctrl_url(output.control_uri,
1224 snapshot_output);
1225 if (ret) {
1226 goto error_snapshot_output;
1227 }
1228 }
1229
1230 if (output.data_uri) {
1231 ret = lttng_snapshot_output_set_data_url(output.data_uri,
1232 snapshot_output);
1233 if (ret) {
1234 goto error_snapshot_output;
1235 }
1236 }
1237 }
1238
1239 ret = lttng_snapshot_add_output(session_name, snapshot_output);
1240error_snapshot_output:
1241 free(name);
1242 free(output.path);
1243 free(output.control_uri);
1244 free(output.data_uri);
1245 lttng_snapshot_output_destroy(snapshot_output);
1246 if (ret) {
1247 goto end;
1248 }
1249 }
1250end:
1251 return ret;
1252}
1253
1254static
1255int create_session(const char *name,
1256 struct lttng_domain *kernel_domain,
1257 struct lttng_domain *ust_domain,
1258 struct lttng_domain *jul_domain,
5cdb6027 1259 struct lttng_domain *log4j_domain,
dcf266c0
JG
1260 xmlNodePtr output_node,
1261 uint64_t live_timer_interval)
1262{
1263 int ret;
1264 struct consumer_output output = { 0 };
1265 xmlNodePtr consumer_output_node;
1266
1267 assert(name);
dcf266c0
JG
1268
1269 if (output_node) {
1270 consumer_output_node = xmlFirstElementChild(output_node);
1271 if (!consumer_output_node) {
1272 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1273 goto end;
1274 }
1275
1276 if (strcmp((const char *) consumer_output_node->name,
1277 config_element_consumer_output)) {
1278 WARN("Invalid output type, expected %s node",
1279 config_element_consumer_output);
1280 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1281 goto end;
1282 }
1283
1284 ret = process_consumer_output(consumer_output_node, &output);
1285 if (ret) {
1286 goto end;
1287 }
1288 }
1289
1290 if (live_timer_interval != UINT64_MAX &&
1291 !output.control_uri && !output.data_uri) {
1292 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1293 goto end;
1294 }
1295
1296 if (output.control_uri || output.data_uri) {
1297 int i;
1298 struct lttng_domain *domain;
1299 struct lttng_domain *domains[] =
c8c5f7ce 1300 { kernel_domain, ust_domain, jul_domain, log4j_domain };
dcf266c0
JG
1301
1302 /* network destination */
1303 if (live_timer_interval && live_timer_interval != UINT64_MAX) {
b664f89a
DG
1304 /*
1305 * URLs are provided for sure since the test above make sure that
1306 * with a live timer the data and control URIs are provided. So,
1307 * NULL is passed here and will be set right after.
1308 */
1309 ret = lttng_create_session_live(name, NULL, live_timer_interval);
dcf266c0
JG
1310 } else {
1311 ret = lttng_create_session(name, NULL);
1312 }
1313 if (ret) {
1314 goto end;
1315 }
1316
301423df 1317 for (i = 0; i < (sizeof(domains) / sizeof(domains[0])); i++) {
dcf266c0
JG
1318 domain = domains[i];
1319 if (!domain) {
1320 continue;
1321 }
1322
1323 ret = create_session_net_output(name, domain, output.control_uri,
1324 output.data_uri);
1325 if (ret) {
1326 goto end;
1327 }
1328 }
1329 } else {
1330 /* either local output or no output */
1331 ret = lttng_create_session(name, output.path);
1332 if (ret) {
1333 goto end;
1334 }
1335 }
1336end:
1337 free(output.path);
1338 free(output.control_uri);
1339 free(output.data_uri);
1340 return ret;
1341}
1342static
1343int process_probe_attribute_node(xmlNodePtr probe_attribute_node,
1344 struct lttng_event_probe_attr *attr)
1345{
1346 int ret;
1347
1348 assert(probe_attribute_node);
1349 assert(attr);
1350
1351 if (!strcmp((const char *) probe_attribute_node->name,
1352 config_element_address)) {
1353 xmlChar *content;
1354 uint64_t addr = 0;
1355
1356 /* addr */
1357 content = xmlNodeGetContent(probe_attribute_node);
1358 if (!content) {
1359 ret = -LTTNG_ERR_NOMEM;
1360 goto end;
1361 }
1362
1363 ret = parse_uint(content, &addr);
1364 free(content);
1365 if (ret) {
1366 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1367 goto end;
1368 }
1369
1370 attr->addr = addr;
1371 } else if (!strcmp((const char *) probe_attribute_node->name,
1372 config_element_offset)) {
1373 xmlChar *content;
1374 uint64_t offset = 0;
1375
1376 /* offset */
1377 content = xmlNodeGetContent(probe_attribute_node);
1378 if (!content) {
1379 ret = -LTTNG_ERR_NOMEM;
1380 goto end;
1381 }
1382
1383 ret = parse_uint(content, &offset);
1384 free(content);
1385 if (ret) {
1386 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1387 goto end;
1388 }
1389
1390 attr->offset = offset;
1391 } else if (!strcmp((const char *) probe_attribute_node->name,
1392 config_element_symbol_name)) {
1393 xmlChar *content;
1394 size_t name_len;
1395
1396 /* symbol_name */
1397 content = xmlNodeGetContent(probe_attribute_node);
1398 if (!content) {
1399 ret = -LTTNG_ERR_NOMEM;
1400 goto end;
1401 }
1402
1403 name_len = strlen((char *) content);
1404 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1405 WARN("symbol_name too long.");
1406 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1407 free(content);
1408 goto end;
1409 }
1410
1411 strncpy(attr->symbol_name, (const char *) content, name_len);
1412 free(content);
1413 }
1414 ret = 0;
1415end:
1416 return ret;
1417}
1418
1419static
1420int process_event_node(xmlNodePtr event_node, struct lttng_handle *handle,
4da99cdc 1421 const char *channel_name, const enum process_event_node_phase phase)
dcf266c0 1422{
4da99cdc 1423 int ret = 0, i;
dcf266c0
JG
1424 xmlNodePtr node;
1425 struct lttng_event event;
1426 char **exclusions = NULL;
1427 unsigned long exclusion_count = 0;
1428 char *filter_expression = NULL;
1429
1430 assert(event_node);
1431 assert(handle);
1432 assert(channel_name);
1433
1434 memset(&event, 0, sizeof(event));
1435
039c1d19
JG
1436 /* Initialize default log level which varies by domain */
1437 switch (handle->domain.type)
1438 {
1439 case LTTNG_DOMAIN_JUL:
1440 event.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
1441 break;
1442 case LTTNG_DOMAIN_LOG4J:
1443 event.loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
1444 break;
1445 case LTTNG_DOMAIN_PYTHON:
1446 event.loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
1447 break;
1448 case LTTNG_DOMAIN_UST:
1449 case LTTNG_DOMAIN_KERNEL:
1450 event.loglevel = LTTNG_LOGLEVEL_DEBUG;
1451 break;
1452 default:
1453 assert(0);
1454 }
1455
dcf266c0
JG
1456 for (node = xmlFirstElementChild(event_node); node;
1457 node = xmlNextElementSibling(node)) {
1458 if (!strcmp((const char *) node->name, config_element_name)) {
1459 xmlChar *content;
1460 size_t name_len;
1461
1462 /* name */
1463 content = xmlNodeGetContent(node);
1464 if (!content) {
1465 ret = -LTTNG_ERR_NOMEM;
1466 goto end;
1467 }
1468
1469 name_len = strlen((char *) content);
1470 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1471 WARN("Channel name too long.");
1472 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1473 free(content);
1474 goto end;
1475 }
1476
1477 strncpy(event.name, (const char *) content, name_len);
1478 free(content);
1479 } else if (!strcmp((const char *) node->name,
1480 config_element_enabled)) {
1481 xmlChar *content = xmlNodeGetContent(node);
1482
1483 /* enabled */
1484 if (!content) {
1485 ret = -LTTNG_ERR_NOMEM;
1486 goto end;
1487 }
1488
1489 ret = parse_bool(content, &event.enabled);
1490 free(content);
1491 if (ret) {
1492 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1493 goto end;
1494 }
1495 } else if (!strcmp((const char *) node->name,
1496 config_element_type)) {
1497 xmlChar *content = xmlNodeGetContent(node);
1498
1499 /* type */
1500 if (!content) {
1501 ret = -LTTNG_ERR_NOMEM;
1502 goto end;
1503 }
1504
1505 ret = get_event_type(content);
1506 free(content);
1507 if (ret < 0) {
1508 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1509 goto end;
1510 }
1511
1512 event.type = ret;
1513 } else if (!strcmp((const char *) node->name,
1514 config_element_loglevel_type)) {
1515 xmlChar *content = xmlNodeGetContent(node);
1516
1517 /* loglevel_type */
1518 if (!content) {
1519 ret = -LTTNG_ERR_NOMEM;
1520 goto end;
1521 }
1522
1523 ret = get_loglevel_type(content);
1524 free(content);
1525 if (ret < 0) {
1526 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1527 goto end;
1528 }
1529
1530 event.loglevel_type = ret;
1531 } else if (!strcmp((const char *) node->name,
1532 config_element_loglevel)) {
1533 xmlChar *content;
1534 int64_t loglevel = 0;
1535
1536 /* loglevel */
1537 content = xmlNodeGetContent(node);
1538 if (!content) {
1539 ret = -LTTNG_ERR_NOMEM;
1540 goto end;
1541 }
1542
1543 ret = parse_int(content, &loglevel);
1544 free(content);
1545 if (ret) {
1546 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1547 goto end;
1548 }
1549
1550 if (loglevel > INT_MAX || loglevel < INT_MIN) {
1551 WARN("loglevel out of range.");
1552 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1553 goto end;
1554 }
1555
1556 event.loglevel = loglevel;
1557 } else if (!strcmp((const char *) node->name,
1558 config_element_filter)) {
1559 xmlChar *content =
1560 xmlNodeGetContent(node);
1561
1562 /* filter */
1563 if (!content) {
1564 ret = -LTTNG_ERR_NOMEM;
1565 goto end;
1566 }
1567
6bd1b5e0 1568 free(filter_expression);
dcf266c0
JG
1569 filter_expression = strdup((char *) content);
1570 free(content);
1571 if (!filter_expression) {
1572 ret = -LTTNG_ERR_NOMEM;
1573 goto end;
1574 }
1575 } else if (!strcmp((const char *) node->name,
1576 config_element_exclusions)) {
1577 xmlNodePtr exclusion_node;
1578 int exclusion_index = 0;
1579
1580 /* exclusions */
1581 if (exclusions) {
1582 /*
1583 * Exclusions has already been initialized,
1584 * invalid file.
1585 */
1586 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1587 goto end;
1588 }
1589
1590 exclusion_count = xmlChildElementCount(node);
1591 if (!exclusion_count) {
1592 continue;
1593 }
1594
1595 exclusions = zmalloc(exclusion_count * sizeof(char *));
1596 if (!exclusions) {
1597 exclusion_count = 0;
1598 ret = -LTTNG_ERR_NOMEM;
1599 goto end;
1600 }
1601
1602 for (exclusion_node = xmlFirstElementChild(node); exclusion_node;
1603 exclusion_node = xmlNextElementSibling(exclusion_node)) {
1604 xmlChar *content =
1605 xmlNodeGetContent(exclusion_node);
1606
1607 if (!content) {
1608 ret = -LTTNG_ERR_NOMEM;
1609 goto end;
1610 }
1611
1612 exclusions[exclusion_index] = strdup((const char *) content);
1613 free(content);
1614 if (!exclusions[exclusion_index]) {
1615 ret = -LTTNG_ERR_NOMEM;
1616 goto end;
1617 }
1618 exclusion_index++;
1619 }
1620
1621 event.exclusion = 1;
1622 } else if (!strcmp((const char *) node->name,
1623 config_element_attributes)) {
1624 xmlNodePtr attribute_node = xmlFirstElementChild(node);
1625
1626 /* attributes */
1627 if (!attribute_node) {
1628 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1629 goto end;
1630 }
1631
1632 if (!strcmp((const char *) node->name,
1633 config_element_probe_attributes)) {
1634 xmlNodePtr probe_attribute_node;
1635
1636 /* probe_attributes */
1637 for (probe_attribute_node =
1638 xmlFirstElementChild(attribute_node); probe_attribute_node;
1639 probe_attribute_node = xmlNextElementSibling(
1640 probe_attribute_node)) {
1641
1642 ret = process_probe_attribute_node(probe_attribute_node,
1643 &event.attr.probe);
1644 if (ret) {
1645 goto end;
1646 }
1647 }
1648 } else {
1649 size_t sym_len;
1650 xmlChar *content;
1651 xmlNodePtr symbol_node = xmlFirstElementChild(attribute_node);
1652
1653 /* function_attributes */
1654 content = xmlNodeGetContent(symbol_node);
1655 if (!content) {
1656 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1657 goto end;
1658 }
1659
1660 sym_len = strlen((char *) content);
1661 if (sym_len >= LTTNG_SYMBOL_NAME_LEN) {
1662 WARN("Function name too long.");
1663 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1664 free(content);
1665 goto end;
1666 }
1667
1668 strncpy(event.attr.ftrace.symbol_name, (char *) content,
1669 sym_len);
1670 free(content);
1671 }
1672 }
1673 }
1674
4da99cdc
JR
1675 if ((event.enabled && phase == ENABLE) || phase == CREATION) {
1676 ret = lttng_enable_event_with_exclusions(handle, &event, channel_name,
1677 filter_expression, exclusion_count, exclusions);
1678 if (ret < 0) {
1679 WARN("Enabling event (name:%s) on load failed.", event.name);
1680 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1681 goto end;
1682 }
065321e9 1683 }
dcf266c0
JG
1684end:
1685 for (i = 0; i < exclusion_count; i++) {
1686 free(exclusions[i]);
1687 }
1688
1689 free(exclusions);
1690 free(filter_expression);
1691 return ret;
1692}
1693
1694static
1695int process_events_node(xmlNodePtr events_node, struct lttng_handle *handle,
1696 const char *channel_name)
1697{
1698 int ret = 0;
4da99cdc 1699 struct lttng_event event;
dcf266c0
JG
1700 xmlNodePtr node;
1701
1702 assert(events_node);
1703 assert(handle);
1704 assert(channel_name);
1705
1706 for (node = xmlFirstElementChild(events_node); node;
1707 node = xmlNextElementSibling(node)) {
4da99cdc 1708 ret = process_event_node(node, handle, channel_name, CREATION);
dcf266c0
JG
1709 if (ret) {
1710 goto end;
1711 }
1712 }
4da99cdc
JR
1713
1714 /*
1715 * Disable all events to enable only the necessary events.
1716 * Limitations regarding lttng_disable_events and tuple descriptor
1717 * force this approach.
1718 */
1719 memset(&event, 0, sizeof(event));
1720 event.loglevel = -1;
1721 event.type = LTTNG_EVENT_ALL;
1722 ret = lttng_disable_event_ext(handle, &event, channel_name, NULL);
1723 if (ret) {
1724 goto end;
1725 }
1726
1727 for (node = xmlFirstElementChild(events_node); node;
1728 node = xmlNextElementSibling(node)) {
1729 ret = process_event_node(node, handle, channel_name, ENABLE);
1730 if (ret) {
1731 goto end;
1732 }
1733 }
1734
dcf266c0
JG
1735end:
1736 return ret;
1737}
1738
1739static
1740int process_channel_attr_node(xmlNodePtr attr_node,
1741 struct lttng_channel *channel, xmlNodePtr *contexts_node,
1742 xmlNodePtr *events_node)
1743{
1744 int ret;
1745
1746 assert(attr_node);
1747 assert(channel);
1748 assert(contexts_node);
1749 assert(events_node);
1750
1751 if (!strcmp((const char *) attr_node->name, config_element_name)) {
1752 xmlChar *content;
1753 size_t name_len;
1754
1755 /* name */
1756 content = xmlNodeGetContent(attr_node);
1757 if (!content) {
1758 ret = -LTTNG_ERR_NOMEM;
1759 goto end;
1760 }
1761
1762 name_len = strlen((char *) content);
1763 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
1764 WARN("Channel name too long.");
1765 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1766 free(content);
1767 goto end;
1768 }
1769
1770 strncpy(channel->name, (const char *) content, name_len);
1771 free(content);
1772 } else if (!strcmp((const char *) attr_node->name,
1773 config_element_enabled)) {
1774 xmlChar *content;
1775 int enabled;
1776
1777 /* enabled */
1778 content = xmlNodeGetContent(attr_node);
1779 if (!content) {
1780 ret = -LTTNG_ERR_NOMEM;
1781 goto end;
1782 }
1783
1784 ret = parse_bool(content, &enabled);
1785 free(content);
1786 if (ret) {
1787 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1788 goto end;
1789 }
1790
1791 channel->enabled = enabled;
1792 } else if (!strcmp((const char *) attr_node->name,
1793 config_element_overwrite_mode)) {
1794 xmlChar *content;
1795
1796 /* overwrite_mode */
1797 content = xmlNodeGetContent(attr_node);
1798 if (!content) {
1799 ret = -LTTNG_ERR_NOMEM;
1800 goto end;
1801 }
1802
1803 ret = get_overwrite_mode(content);
1804 free(content);
1805 if (ret < 0) {
1806 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1807 goto end;
1808 }
1809
1810 channel->attr.overwrite = ret;
1811 } else if (!strcmp((const char *) attr_node->name,
1812 config_element_subbuf_size)) {
1813 xmlChar *content;
1814
1815 /* subbuffer_size */
1816 content = xmlNodeGetContent(attr_node);
1817 if (!content) {
1818 ret = -LTTNG_ERR_NOMEM;
1819 goto end;
1820 }
1821
1822 ret = parse_uint(content, &channel->attr.subbuf_size);
1823 free(content);
1824 if (ret) {
1825 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1826 goto end;
1827 }
1828 } else if (!strcmp((const char *) attr_node->name,
1829 config_element_num_subbuf)) {
1830 xmlChar *content;
1831
1832 /* subbuffer_count */
1833 content = xmlNodeGetContent(attr_node);
1834 if (!content) {
1835 ret = -LTTNG_ERR_NOMEM;
1836 goto end;
1837 }
1838
1839 ret = parse_uint(content, &channel->attr.num_subbuf);
1840 free(content);
1841 if (ret) {
1842 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1843 goto end;
1844 }
1845 } else if (!strcmp((const char *) attr_node->name,
1846 config_element_switch_timer_interval)) {
1847 xmlChar *content;
1848 uint64_t switch_timer_interval = 0;
1849
1850 /* switch_timer_interval */
1851 content = xmlNodeGetContent(attr_node);
1852 if (!content) {
1853 ret = -LTTNG_ERR_NOMEM;
1854 goto end;
1855 }
1856
1857 ret = parse_uint(content, &switch_timer_interval);
1858 free(content);
1859 if (ret) {
1860 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1861 goto end;
1862 }
1863
1864 if (switch_timer_interval > UINT_MAX) {
1865 WARN("switch_timer_interval out of range.");
1866 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1867 goto end;
1868 }
1869
1870 channel->attr.switch_timer_interval =
1871 switch_timer_interval;
1872 } else if (!strcmp((const char *) attr_node->name,
1873 config_element_read_timer_interval)) {
1874 xmlChar *content;
1875 uint64_t read_timer_interval = 0;
1876
1877 /* read_timer_interval */
1878 content = xmlNodeGetContent(attr_node);
1879 if (!content) {
1880 ret = -LTTNG_ERR_NOMEM;
1881 goto end;
1882 }
1883
1884 ret = parse_uint(content, &read_timer_interval);
1885 free(content);
1886 if (ret) {
1887 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1888 goto end;
1889 }
1890
1891 if (read_timer_interval > UINT_MAX) {
1892 WARN("read_timer_interval out of range.");
1893 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1894 goto end;
1895 }
1896
1897 channel->attr.read_timer_interval =
1898 read_timer_interval;
1899 } else if (!strcmp((const char *) attr_node->name,
1900 config_element_output_type)) {
1901 xmlChar *content;
1902
1903 /* output_type */
1904 content = xmlNodeGetContent(attr_node);
1905 if (!content) {
1906 ret = -LTTNG_ERR_NOMEM;
1907 goto end;
1908 }
1909
1910 ret = get_output_type(content);
1911 free(content);
1912 if (ret < 0) {
1913 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1914 goto end;
1915 }
1916
1917 channel->attr.output = ret;
1918 } else if (!strcmp((const char *) attr_node->name,
1919 config_element_tracefile_size)) {
1920 xmlChar *content;
1921
1922 /* tracefile_size */
1923 content = xmlNodeGetContent(attr_node);
1924 if (!content) {
1925 ret = -LTTNG_ERR_NOMEM;
1926 goto end;
1927 }
1928
1929 ret = parse_uint(content, &channel->attr.tracefile_size);
1930 free(content);
1931 if (ret) {
1932 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1933 goto end;
1934 }
1935 } else if (!strcmp((const char *) attr_node->name,
1936 config_element_tracefile_count)) {
1937 xmlChar *content;
1938
1939 /* tracefile_count */
1940 content = xmlNodeGetContent(attr_node);
1941 if (!content) {
1942 ret = -LTTNG_ERR_NOMEM;
1943 goto end;
1944 }
1945
1946 ret = parse_uint(content, &channel->attr.tracefile_count);
1947 free(content);
1948 if (ret) {
1949 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1950 goto end;
1951 }
1952 } else if (!strcmp((const char *) attr_node->name,
1953 config_element_live_timer_interval)) {
1954 xmlChar *content;
1955 uint64_t live_timer_interval = 0;
1956
1957 /* live_timer_interval */
1958 content = xmlNodeGetContent(attr_node);
1959 if (!content) {
1960 ret = -LTTNG_ERR_NOMEM;
1961 goto end;
1962 }
1963
1964 ret = parse_uint(content, &live_timer_interval);
1965 free(content);
1966 if (ret) {
1967 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1968 goto end;
1969 }
1970
1971 if (live_timer_interval > UINT_MAX) {
1972 WARN("live_timer_interval out of range.");
1973 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
1974 goto end;
1975 }
1976
1977 channel->attr.live_timer_interval =
1978 live_timer_interval;
1979 } else if (!strcmp((const char *) attr_node->name,
1980 config_element_events)) {
1981 /* events */
1982 *events_node = attr_node;
1983 } else {
1984 /* contexts */
1985 *contexts_node = attr_node;
1986 }
1987 ret = 0;
1988end:
1989 return ret;
1990}
1991
1992static
1993int process_context_node(xmlNodePtr context_node,
1994 struct lttng_handle *handle, const char *channel_name)
1995{
1996 int ret;
1997 struct lttng_event_context context;
1998 xmlNodePtr context_child_node = xmlFirstElementChild(context_node);
1999
2000 assert(handle);
2001 assert(channel_name);
2002
2003 if (!context_child_node) {
2004 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2005 goto end;
2006 }
2007
2008 memset(&context, 0, sizeof(context));
2009
2010 if (!strcmp((const char *) context_child_node->name,
2011 config_element_type)) {
2012 /* type */
2013 xmlChar *content = xmlNodeGetContent(context_child_node);
2014 if (!content) {
2015 ret = -LTTNG_ERR_NOMEM;
2016 goto end;
2017 }
2018
2019 ret = get_context_type(content);
2020 free(content);
2021 if (ret < 0) {
2022 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2023 goto end;
2024 }
2025
2026 context.ctx = ret;
2027 } else {
2028 xmlNodePtr perf_attr_node;
2029
2030 /* perf */
14ce5bd8
JG
2031 context.ctx = handle->domain.type == LTTNG_DOMAIN_KERNEL ?
2032 LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER :
2033 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER;
dcf266c0
JG
2034 for (perf_attr_node = xmlFirstElementChild(context_child_node);
2035 perf_attr_node; perf_attr_node =
2036 xmlNextElementSibling(perf_attr_node)) {
2037 if (!strcmp((const char *) perf_attr_node->name,
2038 config_element_type)) {
2039 xmlChar *content;
2040 uint64_t type = 0;
2041
2042 /* type */
2043 content = xmlNodeGetContent(perf_attr_node);
2044 if (!content) {
2045 ret = -LTTNG_ERR_NOMEM;
2046 goto end;
2047 }
2048
2049 ret = parse_uint(content, &type);
2050 free(content);
2051 if (ret) {
2052 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2053 goto end;
2054 }
2055
2056 if (type > UINT32_MAX) {
2057 WARN("perf context type out of range.");
2058 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2059 goto end;
2060 }
2061
2062 context.u.perf_counter.type = type;
2063 } else if (!strcmp((const char *) perf_attr_node->name,
2064 config_element_config)) {
2065 xmlChar *content;
2066 uint64_t config = 0;
2067
2068 /* config */
2069 content = xmlNodeGetContent(perf_attr_node);
2070 if (!content) {
2071 ret = -LTTNG_ERR_NOMEM;
2072 goto end;
2073 }
2074
2075 ret = parse_uint(content, &config);
2076 free(content);
2077 if (ret) {
2078 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2079 goto end;
2080 }
2081
2082 context.u.perf_counter.config = config;
2083 } else if (!strcmp((const char *) perf_attr_node->name,
2084 config_element_name)) {
2085 xmlChar *content;
2086 size_t name_len;
2087
2088 /* name */
2089 content = xmlNodeGetContent(perf_attr_node);
2090 if (!content) {
2091 ret = -LTTNG_ERR_NOMEM;
2092 goto end;
2093 }
2094
2095 name_len = strlen((char *) content);
2096 if (name_len >= LTTNG_SYMBOL_NAME_LEN) {
2097 WARN("perf context name too long.");
2098 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2099 free(content);
2100 goto end;
2101 }
2102
2103 strncpy(context.u.perf_counter.name, (const char *) content,
2104 name_len);
2105 free(content);
2106 }
2107 }
2108 }
2109
2110 ret = lttng_add_context(handle, &context, NULL, channel_name);
2111end:
2112 return ret;
2113}
2114
2115static
2116int process_contexts_node(xmlNodePtr contexts_node,
2117 struct lttng_handle *handle, const char *channel_name)
2118{
2119 int ret = 0;
2120 xmlNodePtr context_node;
2121
2122 for (context_node = xmlFirstElementChild(contexts_node); context_node;
2123 context_node = xmlNextElementSibling(context_node)) {
2124 ret = process_context_node(context_node, handle, channel_name);
2125 if (ret) {
2126 goto end;
2127 }
2128 }
2129end:
2130 return ret;
2131}
2132
35460079
JR
2133static
2134int process_pid_tracker_node(xmlNodePtr pid_tracker_node,
2135 struct lttng_handle *handle)
2136{
2137 int ret, child;
2138 xmlNodePtr targets_node = NULL;
2139 xmlNodePtr node;
2140
2141 assert(handle);
2142 assert(pid_tracker_node);
2143 /* get the targets node */
2144 for (node = xmlFirstElementChild(pid_tracker_node); node;
2145 node = xmlNextElementSibling(node)) {
2146 if (!strcmp((const char *) node->name,
2147 config_element_targets)) {
2148 targets_node = node;
2149 break;
2150 }
2151 }
2152
2153 if (!targets_node) {
2154 ret = LTTNG_ERR_INVALID;
2155 goto end;
2156 }
2157
2158 /* Go through all pid_target node */
2159 child = xmlChildElementCount(targets_node);
2160 if (child == 0) {
2161 /* The session is explicitly set to target nothing. */
2162 ret = lttng_untrack_pid(handle, -1);
2163 if (ret) {
2164 goto end;
2165 }
2166 }
2167 for (node = xmlFirstElementChild(targets_node); node;
2168 node = xmlNextElementSibling(node)) {
2169 xmlNodePtr pid_target_node = node;
2170
2171 /* get pid node and track it */
2172 for (node = xmlFirstElementChild(pid_target_node); node;
2173 node = xmlNextElementSibling(node)) {
2174 if (!strcmp((const char *) node->name,
2175 config_element_pid)) {
2176 int64_t pid;
2177 xmlChar *content = NULL;
2178
2179 content = xmlNodeGetContent(node);
2180 if (!content) {
2181 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2182 goto end;
2183 }
2184
2185 ret = parse_int(content, &pid);
2186 free(content);
2187 if (ret) {
2188 ret = LTTNG_ERR_LOAD_INVALID_CONFIG;
2189 goto end;
2190 }
2191
2192 ret = lttng_track_pid(handle, (int) pid);
2193 if (ret) {
2194 goto end;
2195 }
2196 }
2197 }
2198 node = pid_target_node;
2199 }
2200
2201end:
2202 return ret;
2203}
2204
2205
dcf266c0
JG
2206static
2207int process_domain_node(xmlNodePtr domain_node, const char *session_name)
2208{
2209 int ret;
2210 struct lttng_domain domain = { 0 };
2211 struct lttng_handle *handle = NULL;
2212 xmlNodePtr channels_node = NULL;
35460079
JR
2213 xmlNodePtr trackers_node = NULL;
2214 xmlNodePtr pid_tracker_node = NULL;
dcf266c0
JG
2215 xmlNodePtr node;
2216
2217 assert(session_name);
2218
2219 ret = init_domain(domain_node, &domain);
2220 if (ret) {
2221 goto end;
2222 }
2223
2224 handle = lttng_create_handle(session_name, &domain);
2225 if (!handle) {
2226 ret = -LTTNG_ERR_NOMEM;
2227 goto end;
2228 }
2229
2230 /* get the channels node */
2231 for (node = xmlFirstElementChild(domain_node); node;
2232 node = xmlNextElementSibling(node)) {
2233 if (!strcmp((const char *) node->name,
2234 config_element_channels)) {
2235 channels_node = node;
2236 break;
2237 }
2238 }
2239
2240 if (!channels_node) {
2241 goto end;
2242 }
2243
2244 /* create all channels */
2245 for (node = xmlFirstElementChild(channels_node); node;
2246 node = xmlNextElementSibling(node)) {
2247 struct lttng_channel channel;
2248 xmlNodePtr contexts_node = NULL;
2249 xmlNodePtr events_node = NULL;
2250 xmlNodePtr channel_attr_node;
2251
2252 memset(&channel, 0, sizeof(channel));
2253 lttng_channel_set_default_attr(&domain, &channel.attr);
2254
2255 for (channel_attr_node = xmlFirstElementChild(node);
2256 channel_attr_node; channel_attr_node =
2257 xmlNextElementSibling(channel_attr_node)) {
2258 ret = process_channel_attr_node(channel_attr_node,
2259 &channel, &contexts_node, &events_node);
2260 if (ret) {
2261 goto end;
2262 }
2263 }
2264
2265 ret = lttng_enable_channel(handle, &channel);
2266 if (ret < 0) {
2267 goto end;
2268 }
2269
2270 ret = process_events_node(events_node, handle, channel.name);
2271 if (ret) {
2272 goto end;
2273 }
2274
2275 ret = process_contexts_node(contexts_node, handle,
2276 channel.name);
2277 if (ret) {
2278 goto end;
2279 }
2280 }
35460079
JR
2281
2282 /* get the trackers node */
2283 for (node = xmlFirstElementChild(domain_node); node;
2284 node = xmlNextElementSibling(node)) {
2285 if (!strcmp((const char *) node->name,
2286 config_element_trackers)) {
2287 trackers_node = node;
2288 break;
2289 }
2290 }
2291
2292 if (!trackers_node) {
2293 goto end;
2294 }
2295
2296 for (node = xmlFirstElementChild(trackers_node); node;
2297 node = xmlNextElementSibling(node)) {
2298 if (!strcmp((const char *)node->name,config_element_pid_tracker)) {
2299 pid_tracker_node = node;
2300 ret = process_pid_tracker_node(pid_tracker_node, handle);
2301 if (ret) {
2302 goto end;
2303 }
2304 }
2305 }
2306
2307 if (!pid_tracker_node) {
2308 lttng_track_pid(handle, -1);
2309 }
2310
dcf266c0
JG
2311end:
2312 lttng_destroy_handle(handle);
2313 return ret;
2314}
2315
2316static
2317int process_session_node(xmlNodePtr session_node, const char *session_name,
2318 int override)
2319{
2320 int ret, started = -1, snapshot_mode = -1;
2321 uint64_t live_timer_interval = UINT64_MAX;
d324faf7 2322 xmlChar *name = NULL;
9e7c9f56 2323 xmlChar *shm_path = NULL;
dcf266c0
JG
2324 xmlNodePtr domains_node = NULL;
2325 xmlNodePtr output_node = NULL;
2326 xmlNodePtr node;
2327 struct lttng_domain *kernel_domain = NULL;
2328 struct lttng_domain *ust_domain = NULL;
2329 struct lttng_domain *jul_domain = NULL;
5cdb6027 2330 struct lttng_domain *log4j_domain = NULL;
0e115563 2331 struct lttng_domain *python_domain = NULL;
dcf266c0
JG
2332
2333 for (node = xmlFirstElementChild(session_node); node;
2334 node = xmlNextElementSibling(node)) {
2335 if (!name && !strcmp((const char *) node->name,
2336 config_element_name)) {
2337 /* name */
2338 xmlChar *node_content = xmlNodeGetContent(node);
2339 if (!node_content) {
2340 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2341 goto error;
dcf266c0
JG
2342 }
2343
d324faf7 2344 name = node_content;
dcf266c0
JG
2345 } else if (!domains_node && !strcmp((const char *) node->name,
2346 config_element_domains)) {
2347 /* domains */
2348 domains_node = node;
2349 } else if (started == -1 && !strcmp((const char *) node->name,
2350 config_element_started)) {
2351 /* started */
2352 xmlChar *node_content = xmlNodeGetContent(node);
2353 if (!node_content) {
2354 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2355 goto error;
dcf266c0
JG
2356 }
2357
2358 ret = parse_bool(node_content, &started);
2359 free(node_content);
2360 if (ret) {
2361 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2362 goto error;
dcf266c0
JG
2363 }
2364 } else if (!output_node && !strcmp((const char *) node->name,
2365 config_element_output)) {
2366 /* output */
2367 output_node = node;
9e7c9f56
JR
2368 } else if (!shm_path && !strcmp((const char *) node->name,
2369 config_element_shared_memory_path)) {
2370 /* shared memory path */
2371 xmlChar *node_content = xmlNodeGetContent(node);
2372 if (!node_content) {
2373 ret = -LTTNG_ERR_NOMEM;
2374 goto error;
2375 }
2376
2377 shm_path = node_content;
dcf266c0
JG
2378 } else {
2379 /* attributes, snapshot_mode or live_timer_interval */
2380 xmlNodePtr attributes_child =
2381 xmlFirstElementChild(node);
2382
2383 if (!strcmp((const char *) attributes_child->name,
2384 config_element_snapshot_mode)) {
2385 /* snapshot_mode */
2386 xmlChar *snapshot_mode_content =
2387 xmlNodeGetContent(attributes_child);
2388 if (!snapshot_mode_content) {
2389 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2390 goto error;
dcf266c0
JG
2391 }
2392
2393 ret = parse_bool(snapshot_mode_content, &snapshot_mode);
2394 free(snapshot_mode_content);
2395 if (ret) {
2396 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2397 goto error;
dcf266c0
JG
2398 }
2399 } else {
2400 /* live_timer_interval */
2401 xmlChar *timer_interval_content =
2402 xmlNodeGetContent(attributes_child);
2403 if (!timer_interval_content) {
2404 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2405 goto error;
dcf266c0
JG
2406 }
2407
2408 ret = parse_uint(timer_interval_content, &live_timer_interval);
2409 free(timer_interval_content);
2410 if (ret) {
2411 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2412 goto error;
dcf266c0
JG
2413 }
2414 }
2415 }
2416 }
2417
2418 if (!name) {
2419 /* Mandatory attribute, as defined in the session XSD */
2420 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2421 goto error;
dcf266c0
JG
2422 }
2423
d324faf7 2424 if (session_name && strcmp((char *) name, session_name)) {
dcf266c0 2425 /* This is not the session we are looking for */
c2da8cde
DG
2426 ret = -LTTNG_ERR_NO_SESSION;
2427 goto error;
dcf266c0
JG
2428 }
2429
2430 /* Init domains to create the session handles */
2431 for (node = xmlFirstElementChild(domains_node); node;
2432 node = xmlNextElementSibling(node)) {
2433 struct lttng_domain *domain;
2434
2435 domain = zmalloc(sizeof(*domain));
2436 if (!domain) {
2437 ret = -LTTNG_ERR_NOMEM;
c2da8cde 2438 goto error;
dcf266c0
JG
2439 }
2440
2441 ret = init_domain(node, domain);
2442 if (ret) {
2443 goto domain_init_error;
2444 }
2445
2446 switch (domain->type) {
2447 case LTTNG_DOMAIN_KERNEL:
c33e6729
DG
2448 if (kernel_domain) {
2449 /* Same domain seen twice, invalid! */
2450 goto domain_init_error;
2451 }
dcf266c0
JG
2452 kernel_domain = domain;
2453 break;
2454 case LTTNG_DOMAIN_UST:
c33e6729
DG
2455 if (ust_domain) {
2456 /* Same domain seen twice, invalid! */
2457 goto domain_init_error;
2458 }
dcf266c0
JG
2459 ust_domain = domain;
2460 break;
2461 case LTTNG_DOMAIN_JUL:
c33e6729
DG
2462 if (jul_domain) {
2463 /* Same domain seen twice, invalid! */
2464 goto domain_init_error;
2465 }
dcf266c0
JG
2466 jul_domain = domain;
2467 break;
5cdb6027
DG
2468 case LTTNG_DOMAIN_LOG4J:
2469 if (log4j_domain) {
2470 /* Same domain seen twice, invalid! */
2471 goto domain_init_error;
2472 }
2473 log4j_domain = domain;
2474 break;
0e115563
DG
2475 case LTTNG_DOMAIN_PYTHON:
2476 if (python_domain) {
2477 /* Same domain seen twice, invalid! */
2478 goto domain_init_error;
2479 }
2480 python_domain = domain;
2481 break;
dcf266c0
JG
2482 default:
2483 WARN("Invalid domain type");
2484 goto domain_init_error;
2485 }
2486 continue;
2487domain_init_error:
2488 free(domain);
2489 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
c2da8cde 2490 goto error;
dcf266c0
JG
2491 }
2492
2493 if (override) {
2494 /* Destroy session if it exists */
d324faf7 2495 ret = lttng_destroy_session((const char *) name);
dcf266c0
JG
2496 if (ret && ret != -LTTNG_ERR_SESS_NOT_FOUND) {
2497 ERR("Failed to destroy existing session.");
c2da8cde 2498 goto error;
dcf266c0
JG
2499 }
2500 }
2501
2502 /* Create session type depending on output type */
2503 if (snapshot_mode && snapshot_mode != -1) {
d324faf7 2504 ret = create_snapshot_session((const char *) name, output_node);
dcf266c0
JG
2505 } else if (live_timer_interval &&
2506 live_timer_interval != UINT64_MAX) {
d324faf7
JG
2507 ret = create_session((const char *) name, kernel_domain,
2508 ust_domain, jul_domain, log4j_domain,
2509 output_node, live_timer_interval);
dcf266c0
JG
2510 } else {
2511 /* regular session */
d324faf7
JG
2512 ret = create_session((const char *) name, kernel_domain,
2513 ust_domain, jul_domain, log4j_domain,
2514 output_node, UINT64_MAX);
dcf266c0 2515 }
dcf266c0 2516 if (ret) {
c2da8cde 2517 goto error;
dcf266c0
JG
2518 }
2519
9e7c9f56
JR
2520 if (shm_path) {
2521 ret = lttng_set_session_shm_path((const char *) name,
d324faf7 2522 (const char *) shm_path);
9e7c9f56
JR
2523 if (ret) {
2524 goto error;
2525 }
2526 }
2527
dcf266c0
JG
2528 for (node = xmlFirstElementChild(domains_node); node;
2529 node = xmlNextElementSibling(node)) {
d324faf7 2530 ret = process_domain_node(node, (const char *) name);
dcf266c0
JG
2531 if (ret) {
2532 goto end;
2533 }
2534 }
2535
2536 if (started) {
d324faf7 2537 ret = lttng_start_tracing((const char *) name);
dcf266c0
JG
2538 if (ret) {
2539 goto end;
2540 }
2541 }
c2da8cde 2542
dcf266c0 2543end:
b2579dc1 2544 if (ret < 0) {
d324faf7
JG
2545 ERR("Failed to load session %s: %s", (const char *) name,
2546 lttng_strerror(ret));
2547 lttng_destroy_session((const char *) name);
b2579dc1
JG
2548 }
2549
c2da8cde 2550error:
dcf266c0
JG
2551 free(kernel_domain);
2552 free(ust_domain);
2553 free(jul_domain);
5cdb6027 2554 free(log4j_domain);
135a3893 2555 free(python_domain);
d324faf7 2556 xmlFree(name);
9e7c9f56 2557 xmlFree(shm_path);
dcf266c0
JG
2558 return ret;
2559}
2560
cf53c06d
DG
2561/*
2562 * Return 1 if the given path is readable by the current UID or 0 if not.
2563 * Return -1 if the path is EPERM.
2564 */
2565static int validate_file_read_creds(const char *path)
2566{
2567 int ret;
2568
2569 assert(path);
2570
2571 /* Can we read the file. */
2572 ret = access(path, R_OK);
2573 if (!ret) {
2574 goto valid;
2575 }
2576 if (errno == EACCES) {
2577 return -1;
2578 } else {
2579 /* Invalid. */
2580 return 0;
2581 }
2582valid:
2583 return 1;
2584}
2585
dcf266c0
JG
2586static
2587int load_session_from_file(const char *path, const char *session_name,
2588 struct session_config_validation_ctx *validation_ctx, int override)
2589{
2590 int ret, session_found = !session_name;
2591 xmlDocPtr doc = NULL;
2592 xmlNodePtr sessions_node;
2593 xmlNodePtr session_node;
dcf266c0
JG
2594
2595 assert(path);
2596 assert(validation_ctx);
2597
cf53c06d
DG
2598 ret = validate_file_read_creds(path);
2599 if (ret != 1) {
2600 if (ret == -1) {
2601 ret = -LTTNG_ERR_EPERM;
2602 } else {
2603 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2604 }
dcf266c0
JG
2605 goto end;
2606 }
2607
2608 doc = xmlParseFile(path);
2609 if (!doc) {
2610 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2611 goto end;
2612 }
2613
2614 ret = xmlSchemaValidateDoc(validation_ctx->schema_validation_ctx, doc);
2615 if (ret) {
2616 ERR("Session configuration file validation failed");
2617 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
2618 goto end;
2619 }
2620
2621 sessions_node = xmlDocGetRootElement(doc);
2622 if (!sessions_node) {
2623 goto end;
2624 }
2625
2626 for (session_node = xmlFirstElementChild(sessions_node);
2627 session_node; session_node =
2628 xmlNextElementSibling(session_node)) {
2629 ret = process_session_node(session_node,
2630 session_name, override);
2631 if (session_name && ret == 0) {
2632 /* Target session found and loaded */
2633 session_found = 1;
2634 break;
2635 }
2636 }
2637end:
2638 xmlFreeDoc(doc);
2639 if (!ret) {
a96bc65d 2640 ret = session_found ? 0 : -LTTNG_ERR_LOAD_SESSION_NOENT;
dcf266c0
JG
2641 }
2642 return ret;
2643}
2644
60f70355
JG
2645/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */
2646static
2647struct dirent *alloc_dirent(const char *path)
2648{
2649 size_t len;
2650 long name_max;
2651 struct dirent *entry;
2652
2653 name_max = pathconf(path, _PC_NAME_MAX);
2654 if (name_max == -1) {
2655 name_max = PATH_MAX;
2656 }
2657 len = offsetof(struct dirent, d_name) + name_max + 1;
2658 entry = zmalloc(len);
2659 return entry;
2660}
2661
dcf266c0
JG
2662static
2663int load_session_from_path(const char *path, const char *session_name,
2664 struct session_config_validation_ctx *validation_ctx, int override)
2665{
2666 int ret, session_found = !session_name;
dcf266c0
JG
2667 DIR *directory = NULL;
2668
2669 assert(path);
2670 assert(validation_ctx);
2671
4af16958
DG
2672 directory = opendir(path);
2673 if (!directory) {
11143783
DG
2674 switch (errno) {
2675 case ENOTDIR:
0f0a81b5
DG
2676 /* Try the file loading. */
2677 break;
11143783
DG
2678 case ENOENT:
2679 ret = -LTTNG_ERR_LOAD_SESSION_NOENT;
2680 goto end;
2681 default:
0f0a81b5
DG
2682 ret = -LTTNG_ERR_LOAD_IO_FAIL;
2683 goto end;
4af16958 2684 }
dcf266c0 2685 }
4af16958 2686 if (directory) {
dcf266c0
JG
2687 struct dirent *entry;
2688 struct dirent *result;
2689 char *file_path = NULL;
2690 size_t path_len = strlen(path);
2691
2692 if (path_len >= PATH_MAX) {
2693 ret = -LTTNG_ERR_INVALID;
2694 goto end;
2695 }
2696
60f70355 2697 entry = alloc_dirent(path);
dcf266c0
JG
2698 if (!entry) {
2699 ret = -LTTNG_ERR_NOMEM;
2700 goto end;
2701 }
2702
dcf266c0
JG
2703 file_path = zmalloc(PATH_MAX);
2704 if (!file_path) {
2705 ret = -LTTNG_ERR_NOMEM;
2706 free(entry);
2707 goto end;
2708 }
2709
2710 strncpy(file_path, path, path_len);
2711 if (file_path[path_len - 1] != '/') {
2712 file_path[path_len++] = '/';
2713 }
2714
4af16958 2715 ret = 0;
dcf266c0
JG
2716 /* Search for *.lttng files */
2717 while (!readdir_r(directory, entry, &result) && result) {
2718 size_t file_name_len = strlen(result->d_name);
2719
2720 if (file_name_len <=
2721 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) {
2722 continue;
2723 }
2724
431f702e 2725 if (path_len + file_name_len >= PATH_MAX) {
dcf266c0
JG
2726 continue;
2727 }
2728
2729 if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION,
2730 result->d_name + file_name_len - sizeof(
2731 DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) {
2732 continue;
2733 }
2734
2735 strncpy(file_path + path_len, result->d_name, file_name_len);
2736 file_path[path_len + file_name_len] = '\0';
2737
2738 ret = load_session_from_file(file_path, session_name,
2739 validation_ctx, override);
2740 if (session_name && !ret) {
2741 session_found = 1;
2742 break;
2743 }
2744 }
2745
2746 free(entry);
2747 free(file_path);
2748 } else {
2749 ret = load_session_from_file(path, session_name,
2750 validation_ctx, override);
2751 if (ret) {
2752 goto end;
2753 } else {
2754 session_found = 1;
2755 }
2756 }
2757
2758end:
2759 if (directory) {
2760 if (closedir(directory)) {
2761 PERROR("closedir");
2762 }
2763 }
2764
5f75dfa0
JG
2765 if (session_found) {
2766 ret = 0;
dcf266c0
JG
2767 }
2768
2769 return ret;
2770}
2771
ab38c13f
DG
2772/*
2773 * Validate that the given path's credentials and the current process have the
cf53c06d 2774 * same UID. If so, return 1 else return 0 if it does NOT match.
ab38c13f
DG
2775 */
2776static int validate_path_creds(const char *path)
2777{
2778 int ret, uid = getuid();
2779 struct stat buf;
2780
2781 assert(path);
2782
cf53c06d 2783 if (uid == 0) {
ab38c13f
DG
2784 goto valid;
2785 }
2786
2787 ret = stat(path, &buf);
2788 if (ret < 0) {
2789 if (errno != ENOENT) {
2790 PERROR("stat");
2791 }
2792 ret = -LTTNG_ERR_INVALID;
2793 goto valid;
2794 }
2795
2796 if (buf.st_uid != uid) {
2797 goto invalid;
2798 }
2799
2800valid:
ab38c13f 2801 return 1;
cf53c06d
DG
2802invalid:
2803 return 0;
ab38c13f
DG
2804}
2805
dcf266c0
JG
2806LTTNG_HIDDEN
2807int config_load_session(const char *path, const char *session_name,
ab38c13f 2808 int override, unsigned int autoload)
dcf266c0
JG
2809{
2810 int ret;
5f75dfa0 2811 bool session_loaded = false;
cf53c06d 2812 const char *path_ptr = NULL;
dcf266c0
JG
2813 struct session_config_validation_ctx validation_ctx = { 0 };
2814
2815 ret = init_session_config_validation_ctx(&validation_ctx);
2816 if (ret) {
2817 goto end;
2818 }
2819
2820 if (!path) {
ab38c13f
DG
2821 char *home_path;
2822 const char *sys_path;
2823
dcf266c0 2824 /* Try home path */
ab38c13f 2825 home_path = utils_get_home_dir();
dcf266c0 2826 if (home_path) {
ab38c13f 2827 char path[PATH_MAX];
dcf266c0 2828
d4fcf703
DG
2829 /*
2830 * Try user session configuration path. Ignore error here so we can
2831 * continue loading the system wide sessions.
2832 */
ab38c13f
DG
2833 if (autoload) {
2834 ret = snprintf(path, sizeof(path),
2835 DEFAULT_SESSION_HOME_CONFIGPATH "/"
2836 DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
cf53c06d
DG
2837 if (ret < 0) {
2838 PERROR("snprintf session autoload home config path");
2839 goto end;
2840 }
2841
2842 /*
2843 * Credentials are only validated for the autoload in order to
2844 * avoid any user session daemon to try to load kernel sessions
2845 * automatically and failing all the times.
2846 */
2847 ret = validate_path_creds(path);
2848 if (ret) {
2849 path_ptr = path;
2850 }
ab38c13f
DG
2851 } else {
2852 ret = snprintf(path, sizeof(path),
2853 DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
cf53c06d
DG
2854 if (ret < 0) {
2855 PERROR("snprintf session home config path");
2856 goto end;
2857 }
2858 path_ptr = path;
ab38c13f 2859 }
cf53c06d
DG
2860 if (path_ptr) {
2861 ret = load_session_from_path(path_ptr, session_name,
d4fcf703
DG
2862 &validation_ctx, override);
2863 if (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT) {
2864 goto end;
2865 }
2866 /*
2867 * Continue even if the session was found since we have to try
2868 * the system wide sessions.
2869 */
5f75dfa0 2870 session_loaded = true;
ab38c13f 2871 }
d4fcf703 2872 }
ab38c13f 2873
cf53c06d
DG
2874 /* Reset path pointer for the system wide dir. */
2875 path_ptr = NULL;
2876
d4fcf703
DG
2877 /* Try system wide configuration directory. */
2878 if (autoload) {
2879 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
2880 DEFAULT_SESSION_CONFIG_AUTOLOAD;
cf53c06d
DG
2881 ret = validate_path_creds(sys_path);
2882 if (ret) {
2883 path_ptr = sys_path;
2884 }
d4fcf703 2885 } else {
cf53c06d
DG
2886 sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH;
2887 path_ptr = sys_path;
d4fcf703
DG
2888 }
2889
cf53c06d
DG
2890 if (path_ptr) {
2891 ret = load_session_from_path(path_ptr, session_name,
d4fcf703 2892 &validation_ctx, override);
5f75dfa0
JG
2893 if (!ret) {
2894 session_loaded = true;
2895 }
dcf266c0
JG
2896 }
2897 } else {
2898 ret = access(path, F_OK);
2899 if (ret < 0) {
2900 PERROR("access");
2901 switch (errno) {
2902 case ENOENT:
2903 ret = -LTTNG_ERR_INVALID;
2904 WARN("Session configuration path does not exist.");
2905 break;
2906 case EACCES:
2907 ret = -LTTNG_ERR_EPERM;
2908 break;
2909 default:
2910 ret = -LTTNG_ERR_UNK;
2911 break;
2912 }
2913 goto end;
2914 }
2915
2916 ret = load_session_from_path(path, session_name,
2917 &validation_ctx, override);
2918 }
2919end:
2920 fini_session_config_validation_ctx(&validation_ctx);
d2b6efff
JG
2921 if (ret == -LTTNG_ERR_LOAD_SESSION_NOENT && !session_name && !path) {
2922 /*
2923 * Don't report an error if no sessions are found when called
2924 * without a session_name or a search path.
2925 */
2926 ret = 0;
2927 }
5f75dfa0
JG
2928
2929 if (session_loaded && ret == -LTTNG_ERR_LOAD_SESSION_NOENT) {
2930 /* A matching session was found in one of the search paths. */
2931 ret = 0;
2932 }
dcf266c0
JG
2933 return ret;
2934}
This page took 0.153284 seconds and 4 git commands to generate.