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