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