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