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