Add session configuration save feature to session daemon
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 22 Jan 2014 19:36:01 +0000 (14:36 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Mon, 10 Mar 2014 16:31:54 +0000 (12:31 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
include/lttng/lttng-error.h
src/bin/lttng-sessiond/Makefile.am
src/bin/lttng-sessiond/main.c
src/bin/lttng-sessiond/save.c [new file with mode: 0644]
src/bin/lttng-sessiond/save.h [new file with mode: 0644]
src/common/config/config-session-internal.h [new file with mode: 0644]
src/common/config/config.c
src/common/defaults.h
src/common/utils.c
src/common/utils.h

index 94096ca7f31a445933fe7bd5821afcb06e682e0f..8184817712026e246698e74ebb511ad59d060fcb 100644 (file)
@@ -142,6 +142,7 @@ enum lttng_error_code {
        LTTNG_ERR_NO_CONSUMER            = 109, /* No consumer exist for the session */
        LTTNG_ERR_EXCLUSION_INVAL        = 110, /* Invalid event exclusion data */
        LTTNG_ERR_EXCLUSION_NOMEM        = 111, /* Lack of memory while processing event exclusions */
+       LTTNG_ERR_SAVE_IO_FAIL           = 112, /* IO error while writting session configuration */
 
        /* MUST be last element */
        LTTNG_ERR_NR,                           /* Last element */
index 53433d0333015a18431a152765d388d66d23965b..71df92678fe772067de250128b0771e08ac8f943 100644 (file)
@@ -27,7 +27,8 @@ lttng_sessiond_SOURCES = utils.c utils.h \
                        buffer-registry.c buffer-registry.h \
                        testpoint.h ht-cleanup.c \
                        snapshot.c snapshot.h \
-                       jul.c jul.h
+                       jul.c jul.h \
+                       save.h save.c
 
 if HAVE_LIBLTTNG_UST_CTL
 lttng_sessiond_SOURCES += trace-ust.c ust-registry.c ust-app.c \
index 4d81896bfd121cbe8163bafcc62fe65f3c134c08..1a5eeb07e01dce2b1fcd22fdbb11f9b639b99ff6 100644 (file)
@@ -68,6 +68,7 @@
 #include "testpoint.h"
 #include "ust-thread.h"
 #include "jul-thread.h"
+#include "save.h"
 
 #define CONSUMERD_FILE "lttng-consumerd"
 
@@ -2718,6 +2719,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
        case LTTNG_SNAPSHOT_DEL_OUTPUT:
        case LTTNG_SNAPSHOT_LIST_OUTPUT:
        case LTTNG_SNAPSHOT_RECORD:
+       case LTTNG_SAVE_SESSION:
                need_domain = 0;
                break;
        default:
@@ -2776,6 +2778,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
        case LTTNG_LIST_SESSIONS:
        case LTTNG_LIST_TRACEPOINTS:
        case LTTNG_LIST_TRACEPOINT_FIELDS:
+       case LTTNG_SAVE_SESSION:
                need_tracing_session = 0;
                break;
        default:
@@ -3607,6 +3610,12 @@ skip_domain:
                free(uris);
                break;
        }
+       case LTTNG_SAVE_SESSION:
+       {
+               ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
+                       &cmd_ctx->creds);
+               break;
+       }
        default:
                ret = LTTNG_ERR_UND;
                break;
diff --git a/src/bin/lttng-sessiond/save.c b/src/bin/lttng-sessiond/save.c
new file mode 100644 (file)
index 0000000..8599d2d
--- /dev/null
@@ -0,0 +1,1624 @@
+/*
+ * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <inttypes.h>
+#include <string.h>
+#include <urcu/uatomic.h>
+#include <unistd.h>
+
+#include <common/defaults.h>
+#include <common/error.h>
+#include <common/config/config.h>
+#include <common/config/config-session-internal.h>
+#include <common/utils.h>
+#include <common/runas.h>
+#include <lttng/save-internal.h>
+
+#include "save.h"
+#include "session.h"
+#include "trace-ust.h"
+
+static
+int save_kernel_channel_attributes(struct config_writer *writer,
+       struct lttng_channel_attr *attr)
+{
+       int ret;
+
+       ret = config_writer_write_element_string(writer,
+               config_element_overwrite_mode,
+               attr->overwrite ? config_overwrite_mode_overwrite :
+                       config_overwrite_mode_discard);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_subbuf_size, attr->subbuf_size);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_num_subbuf,
+               attr->num_subbuf);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_switch_timer_interval,
+               attr->switch_timer_interval);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_read_timer_interval,
+               attr->read_timer_interval);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer,
+               config_element_output_type,
+               attr->output == LTTNG_EVENT_SPLICE ?
+               config_output_type_splice : config_output_type_mmap);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_tracefile_size, attr->tracefile_size);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_tracefile_count,
+               attr->tracefile_count);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_live_timer_interval,
+               attr->live_timer_interval);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0;
+}
+
+static
+int save_ust_channel_attributes(struct config_writer *writer,
+       struct lttng_ust_channel_attr *attr)
+{
+       int ret;
+
+       ret = config_writer_write_element_string(writer,
+               config_element_overwrite_mode,
+               attr->overwrite ? config_overwrite_mode_overwrite :
+                       config_overwrite_mode_discard);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_subbuf_size, attr->subbuf_size);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_num_subbuf,
+               attr->num_subbuf);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_switch_timer_interval,
+               attr->switch_timer_interval);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_read_timer_interval,
+               attr->read_timer_interval);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer,
+               config_element_output_type,
+               attr->output == LTTNG_UST_MMAP ?
+               config_output_type_mmap : config_output_type_splice);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0;
+}
+
+static
+const char *get_kernel_instrumentation_string(
+       enum lttng_kernel_instrumentation instrumentation)
+{
+       const char *instrumentation_string;
+
+       switch (instrumentation) {
+       case LTTNG_KERNEL_ALL:
+               instrumentation_string = config_event_type_all;
+               break;
+       case LTTNG_KERNEL_TRACEPOINT:
+               instrumentation_string = config_event_type_tracepoint;
+               break;
+       case LTTNG_KERNEL_KPROBE:
+               instrumentation_string = config_event_type_kprobe;
+               break;
+       case LTTNG_KERNEL_FUNCTION:
+               instrumentation_string = config_event_type_function;
+               break;
+       case LTTNG_KERNEL_KRETPROBE:
+               instrumentation_string = config_event_type_kretprobe;
+               break;
+       case LTTNG_KERNEL_NOOP:
+               instrumentation_string = config_event_type_noop;
+               break;
+       case LTTNG_KERNEL_SYSCALL:
+               instrumentation_string = config_event_type_syscall;
+               break;
+       default:
+               instrumentation_string = NULL;
+       }
+
+       return instrumentation_string;
+}
+
+static
+const char *get_kernel_context_type_string(
+       enum lttng_kernel_context_type context_type)
+{
+       const char *context_type_string;
+
+       switch (context_type) {
+       case LTTNG_KERNEL_CONTEXT_PID:
+               context_type_string = config_event_context_pid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_PROCNAME:
+               context_type_string = config_event_context_procname;
+               break;
+       case LTTNG_KERNEL_CONTEXT_PRIO:
+               context_type_string = config_event_context_prio;
+               break;
+       case LTTNG_KERNEL_CONTEXT_NICE:
+               context_type_string = config_event_context_nice;
+               break;
+       case LTTNG_KERNEL_CONTEXT_VPID:
+               context_type_string = config_event_context_vpid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_TID:
+               context_type_string = config_event_context_tid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_VTID:
+               context_type_string = config_event_context_vtid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_PPID:
+               context_type_string = config_event_context_ppid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_VPPID:
+               context_type_string = config_event_context_vppid;
+               break;
+       case LTTNG_KERNEL_CONTEXT_HOSTNAME:
+               context_type_string = config_event_context_hostname;
+               break;
+       default:
+               context_type_string = NULL;
+       }
+
+       return context_type_string;
+}
+
+static
+const char *get_ust_context_type_string(
+       enum lttng_ust_context_type context_type)
+{
+       const char *context_type_string;
+
+       switch (context_type) {
+       case LTTNG_UST_CONTEXT_PROCNAME:
+               context_type_string = config_event_context_procname;
+               break;
+       case LTTNG_UST_CONTEXT_VPID:
+               context_type_string = config_event_context_vpid;
+               break;
+       case LTTNG_UST_CONTEXT_VTID:
+               context_type_string = config_event_context_vtid;
+               break;
+       case LTTNG_UST_CONTEXT_IP:
+               context_type_string = config_event_context_ip;
+               break;
+       case LTTNG_UST_CONTEXT_PTHREAD_ID:
+               context_type_string = config_event_context_pthread_id;
+               break;
+       default:
+               context_type_string = NULL;
+       }
+
+       return context_type_string;
+}
+
+static
+const char *get_buffer_type_string(
+       enum lttng_buffer_type buffer_type)
+{
+       const char *buffer_type_string;
+
+       switch (buffer_type) {
+       case LTTNG_BUFFER_PER_PID:
+               buffer_type_string = config_buffer_type_per_pid;
+               break;
+       case LTTNG_BUFFER_PER_UID:
+               buffer_type_string = config_buffer_type_per_uid;
+               break;
+       case LTTNG_BUFFER_GLOBAL:
+               buffer_type_string = config_buffer_type_global;
+               break;
+       default:
+               buffer_type_string = NULL;
+       }
+
+       return buffer_type_string;
+}
+
+static
+const char *get_loglevel_type_string(
+       enum lttng_ust_loglevel_type loglevel_type)
+{
+       const char *loglevel_type_string;
+
+       switch (loglevel_type) {
+       case LTTNG_UST_LOGLEVEL_ALL:
+               loglevel_type_string = config_loglevel_type_all;
+               break;
+       case LTTNG_UST_LOGLEVEL_RANGE:
+               loglevel_type_string = config_loglevel_type_range;
+               break;
+       case LTTNG_UST_LOGLEVEL_SINGLE:
+               loglevel_type_string = config_loglevel_type_single;
+               break;
+       default:
+               loglevel_type_string = NULL;
+       }
+
+       return loglevel_type_string;
+}
+
+static
+int save_kernel_event(struct config_writer *writer,
+       struct ltt_kernel_event *event)
+{
+       int ret;
+       const char *instrumentation_type;
+
+       ret = config_writer_open_element(writer, config_element_event);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (event->event->name[0]) {
+               ret = config_writer_write_element_string(writer,
+                       config_element_name, event->event->name);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_enabled,
+               event->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       instrumentation_type = get_kernel_instrumentation_string(
+               event->event->instrumentation);
+       if (!instrumentation_type) {
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer, config_element_type,
+               instrumentation_type);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (event->event->instrumentation == LTTNG_KERNEL_FUNCTION ||
+               event->event->instrumentation == LTTNG_KERNEL_KPROBE ||
+               event->event->instrumentation == LTTNG_KERNEL_KRETPROBE) {
+
+               ret = config_writer_open_element(writer,
+                       config_element_attributes);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               switch (event->event->instrumentation) {
+               case LTTNG_KERNEL_FUNCTION:
+                       ret = config_writer_open_element(writer,
+                               config_element_function_attributes);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+
+                       ret = config_writer_write_element_string(writer,
+                               config_element_name,
+                               event->event->u.ftrace.symbol_name);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+
+                       /* /function attributes */
+                       ret = config_writer_close_element(writer);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+                       break;
+               case LTTNG_KERNEL_KPROBE:
+               case LTTNG_KERNEL_KRETPROBE:
+               {
+                       const char *symbol_name;
+                       uint64_t addr;
+                       uint64_t offset;
+
+                       if (event->event->instrumentation ==
+                               LTTNG_KERNEL_KPROBE) {
+                               /*
+                                * Comments in lttng-kernel.h mention that
+                                * either addr or symbol_name are set, not both.
+                                */
+                               addr = event->event->u.kprobe.addr;
+                               offset = event->event->u.kprobe.offset;
+                               symbol_name = addr ? NULL :
+                                       event->event->u.kprobe.symbol_name;
+                       } else {
+                               symbol_name =
+                                       event->event->u.kretprobe.symbol_name;
+                               addr = event->event->u.kretprobe.addr;
+                               offset = event->event->u.kretprobe.offset;
+                       }
+
+                       ret = config_writer_open_element(writer,
+                               config_element_probe_attributes);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+
+                       if (symbol_name) {
+                               ret = config_writer_write_element_string(writer,
+                                       config_element_symbol_name,
+                                       symbol_name);
+                               if (ret) {
+                                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                                       goto end;
+                               }
+                       }
+
+                       if (addr) {
+                               ret = config_writer_write_element_unsigned_int(
+                                       writer, config_element_address, addr);
+                               if (ret) {
+                                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                                       goto end;
+                               }
+                       }
+
+                       if (offset) {
+                               ret = config_writer_write_element_unsigned_int(
+                                       writer, config_element_offset, offset);
+                               if (ret) {
+                                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                                       goto end;
+                               }
+                       }
+
+                       ret = config_writer_close_element(writer);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+                       break;
+               }
+               default:
+                       ERR("Unsupported kernel instrumentation type.");
+                       ret = LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               /* /attributes */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       /* /event */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_kernel_events(struct config_writer *writer,
+       struct ltt_kernel_event_list *event_list)
+{
+       int ret;
+       struct ltt_kernel_event *event;
+
+       ret = config_writer_open_element(writer, config_element_events);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       cds_list_for_each_entry(event, &event_list->head, list) {
+               ret = save_kernel_event(writer, event);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* /events */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_ust_event(struct config_writer *writer,
+       struct ltt_ust_event *event)
+{
+       int ret;
+       const char *loglevel_type_string;
+
+       ret = config_writer_open_element(writer, config_element_event);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (event->attr.name[0]) {
+               ret = config_writer_write_element_string(writer,
+                       config_element_name, event->attr.name);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_enabled,
+               event->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (event->attr.instrumentation != LTTNG_UST_TRACEPOINT) {
+               ERR("Unsupported UST instrumentation type.");
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+       ret = config_writer_write_element_string(writer, config_element_type,
+               config_event_type_tracepoint);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       loglevel_type_string = get_loglevel_type_string(
+               event->attr.loglevel_type);
+       if (!loglevel_type_string) {
+               ERR("Unsupported UST loglevel type.");
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer,
+               config_element_loglevel_type, loglevel_type_string);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_signed_int(writer,
+               config_element_loglevel, event->attr.loglevel);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (event->filter_expression) {
+               ret = config_writer_write_element_string(writer,
+                       config_element_filter, event->filter_expression);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       if (event->exclusion && event->exclusion->count) {
+               uint32_t i;
+
+               ret = config_writer_open_element(writer,
+                       config_element_exclusions);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               for (i = 0; i < event->exclusion->count; i++) {
+                       ret = config_writer_write_element_string(writer,
+                               config_element_exclusion,
+                               &event->exclusion->names[0][i]);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+               }
+
+               /* /exclusions */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       /* /event */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_ust_events(struct config_writer *writer,
+       struct lttng_ht *events)
+{
+       int ret;
+       struct ltt_ust_event *event;
+       struct lttng_ht_node_str *node;
+       struct lttng_ht_iter iter;
+
+       ret = config_writer_open_element(writer, config_element_events);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       rcu_read_lock();
+       cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
+               event = caa_container_of(node, struct ltt_ust_event, node);
+
+               ret = save_ust_event(writer, event);
+               if (ret) {
+                       rcu_read_unlock();
+                       goto end;
+               }
+       }
+       rcu_read_unlock();
+
+       /* /events */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_kernel_context(struct config_writer *writer,
+       struct lttng_kernel_context *ctx)
+{
+       int ret = 0;
+
+       if (!ctx) {
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_contexts);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_context);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
+               ret = config_writer_open_element(writer, config_element_perf);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = config_writer_write_element_unsigned_int(writer,
+                       config_element_type, ctx->u.perf_counter.type);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = config_writer_write_element_unsigned_int(writer,
+                       config_element_config, ctx->u.perf_counter.config);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = config_writer_write_element_string(writer,
+                       config_element_name, ctx->u.perf_counter.name);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               /* /perf */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       } else {
+               const char *context_type_string =
+                       get_kernel_context_type_string(ctx->ctx);
+
+               if (!context_type_string) {
+                       ERR("Unsupported kernel context type.");
+                       ret = LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               ret = config_writer_write_element_string(writer,
+                       config_element_type, context_type_string);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       /* /context */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       /* /contexts */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_ust_context(struct config_writer *writer,
+       struct cds_list_head *ctx_list)
+{
+       int ret;
+       struct ltt_ust_context *ctx;
+
+       assert(writer);
+       assert(ctx_list);
+
+       ret = config_writer_open_element(writer, config_element_contexts);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       cds_list_for_each_entry(ctx, ctx_list, list) {
+               const char *context_type_string;
+
+               context_type_string = get_ust_context_type_string(ctx->ctx.ctx);
+               if (!context_type_string) {
+                       ERR("Unsupported UST context type.")
+                       ret = LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               ret = config_writer_open_element(writer,
+                       config_element_context);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = config_writer_write_element_string(writer,
+                       config_element_type, context_type_string);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               /* /context */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       /* /contexts */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_kernel_channel(struct config_writer *writer,
+       struct ltt_kernel_channel *kchan)
+{
+       int ret;
+
+       assert(writer);
+       assert(kchan);
+
+       ret = config_writer_open_element(writer, config_element_channel);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer, config_element_name,
+               kchan->channel->name);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_enabled,
+               kchan->channel->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = save_kernel_channel_attributes(writer, &kchan->channel->attr);
+       if (ret) {
+               goto end;
+       }
+
+       ret = save_kernel_events(writer, &kchan->events_list);
+       if (ret) {
+               goto end;
+       }
+
+       ret = save_kernel_context(writer, kchan->ctx);
+       if (ret) {
+               goto end;
+       }
+
+       /* /channel */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_ust_channel(struct config_writer *writer,
+       struct ltt_ust_channel *ust_chan,
+       struct ltt_ust_session *session)
+{
+       int ret;
+
+       assert(writer);
+       assert(ust_chan);
+       assert(session);
+
+       ret = config_writer_open_element(writer, config_element_channel);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer, config_element_name,
+               ust_chan->name);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_enabled,
+               ust_chan->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = save_ust_channel_attributes(writer, &ust_chan->attr);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_tracefile_size, ust_chan->tracefile_size);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_tracefile_count, ust_chan->tracefile_count);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_unsigned_int(writer,
+               config_element_live_timer_interval,
+               session->live_timer_interval);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = save_ust_events(writer, ust_chan->events);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = save_ust_context(writer, &ust_chan->ctx_list);
+       if (ret) {
+               goto end;
+       }
+
+       /* /channel */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_kernel_session(struct config_writer *writer,
+       struct ltt_session *session)
+{
+       int ret;
+       struct ltt_kernel_channel *kchan;
+
+       assert(writer);
+       assert(session);
+
+       ret = config_writer_write_element_string(writer, config_element_type,
+               config_domain_type_kernel);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer,
+               config_element_buffer_type, config_buffer_type_global);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer,
+               config_element_channels);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head,
+                       list) {
+               ret = save_kernel_channel(writer, kchan);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /* /channels */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_ust_session(struct config_writer *writer,
+       struct ltt_session *session, int save_jul)
+{
+       int ret;
+       struct ltt_ust_channel *ust_chan;
+       const char *buffer_type_string;
+       struct lttng_ht_node_str *node;
+       struct lttng_ht_iter iter;
+
+       assert(writer);
+       assert(session);
+
+       ret = config_writer_write_element_string(writer, config_element_type,
+                       save_jul ? config_domain_type_jul : config_domain_type_ust);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       buffer_type_string = get_buffer_type_string(
+                       session->ust_session->buffer_type);
+       if (!buffer_type_string) {
+               ERR("Unsupported buffer type.");
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer,
+                       config_element_buffer_type, buffer_type_string);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_channels);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       rcu_read_lock();
+       cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
+                       &iter.iter, node, node) {
+               int jul_channel;
+
+               ust_chan = caa_container_of(node, struct ltt_ust_channel, node);
+               jul_channel = !strcmp(DEFAULT_JUL_CHANNEL_NAME, ust_chan->name);
+               if (!(save_jul ^ jul_channel)) {
+                       ret = save_ust_channel(writer, ust_chan, session->ust_session);
+                       if (ret) {
+                               rcu_read_unlock();
+                               goto end;
+                       }
+               }
+       }
+       rcu_read_unlock();
+
+       /* /channels */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_domains(struct config_writer *writer, struct ltt_session *session)
+{
+       int ret = 0;
+
+       assert(writer);
+       assert(session);
+
+       if (!session->kernel_session && !session->ust_session) {
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_domains);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+
+       if (session->kernel_session) {
+               ret = config_writer_open_element(writer,
+                       config_element_domain);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = save_kernel_session(writer, session);
+               if (ret) {
+                       goto end;
+               }
+
+               /* /domain */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       if (session->ust_session) {
+               ret = config_writer_open_element(writer,
+                       config_element_domain);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = save_ust_session(writer, session, 0);
+               if (ret) {
+                       goto end;
+               }
+
+               /* /domain */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       if (session->ust_session &&
+               session->ust_session->domain_jul.being_used) {
+               ret = config_writer_open_element(writer,
+                       config_element_domain);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               ret = save_ust_session(writer, session, 1);
+               if (ret) {
+                       goto end;
+               }
+
+               /* /domain */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       /* /domains */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_consumer_output(struct config_writer *writer,
+       struct consumer_output *output)
+{
+       int ret;
+
+       assert(writer);
+       assert(output);
+
+       ret = config_writer_open_element(writer, config_element_consumer_output);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_enabled,
+                       output->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_destination);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       switch (output->type) {
+       case CONSUMER_DST_LOCAL:
+               ret = config_writer_write_element_string(writer,
+                       config_element_path, output->dst.trace_path);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+               break;
+       case CONSUMER_DST_NET:
+       {
+               char *uri;
+
+               uri = zmalloc(PATH_MAX);
+               if (!uri) {
+                       ret = LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               ret = config_writer_open_element(writer, config_element_net_output);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end_net_output;
+               }
+
+               if (output->dst.net.control_isset &&
+                       output->dst.net.data_isset) {
+                       ret = uri_to_str_url(&output->dst.net.control, uri, PATH_MAX);
+                       if (ret < 0) {
+                               ret = LTTNG_ERR_INVALID;
+                               goto end_net_output;
+                       }
+
+                       ret = config_writer_write_element_string(writer,
+                                       config_element_control_uri, uri);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end_net_output;
+                       }
+
+                       ret = uri_to_str_url(&output->dst.net.data, uri, PATH_MAX);
+                       if (ret < 0) {
+                               ret = LTTNG_ERR_INVALID;
+                               goto end_net_output;
+                       }
+
+                       ret = config_writer_write_element_string(writer,
+                                       config_element_data_uri, uri);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end_net_output;
+                       }
+
+end_net_output:
+                       free(uri);
+                       if (ret) {
+                               goto end;
+                       }
+               } else {
+                       ret = !output->dst.net.control_isset ?
+                               LTTNG_ERR_URL_CTRL_MISS :
+                               LTTNG_ERR_URL_DATA_MISS;
+                       goto end;
+               }
+
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+               break;
+       }
+       default:
+               ERR("Unsupported consumer output type.");
+               ret = LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /* /destination */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       /* /consumer_output */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static
+int save_snapshot_outputs(struct config_writer *writer,
+       struct snapshot *snapshot)
+{
+       int ret;
+       struct lttng_ht_iter iter;
+       struct snapshot_output *output;
+
+       assert(writer);
+       assert(snapshot);
+
+       ret = config_writer_open_element(writer, config_element_snapshot_outputs);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       rcu_read_lock();
+       cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output,
+                       node.node) {
+               ret = config_writer_open_element(writer,
+                       config_element_output);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end_unlock;
+               }
+
+               ret = config_writer_write_element_string(writer,
+                       config_element_name, output->name);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end_unlock;
+               }
+
+               ret = config_writer_write_element_unsigned_int(writer,
+                       config_element_max_size, output->max_size);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end_unlock;
+               }
+
+               ret = save_consumer_output(writer, output->consumer);
+               if (ret) {
+                       goto end_unlock;
+               }
+
+               /* /output */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end_unlock;
+               }
+       }
+       rcu_read_unlock();
+
+       /* /snapshot_outputs */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+end:
+       return ret;
+end_unlock:
+       rcu_read_unlock();
+       return ret;
+}
+
+static
+int save_session_output(struct config_writer *writer,
+       struct ltt_session *session)
+{
+       int ret;
+
+       assert(writer);
+       assert(session);
+
+       if ((session->snapshot_mode && session->snapshot.nb_output == 0) ||
+               (!session->snapshot_mode && !session->consumer)) {
+               /* Session is in no output mode */
+               ret = 0;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_output);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (session->snapshot_mode) {
+               ret = save_snapshot_outputs(writer, &session->snapshot);
+               if (ret) {
+                       goto end;
+               }
+       } else {
+               if (session->consumer) {
+                       ret = save_consumer_output(writer, session->consumer);
+                       if (ret) {
+                               goto end;
+                       }
+               }
+       }
+
+       /* /output */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+/*
+ * Save the given session.
+ *
+ * Return 0 on success else a LTTNG_ERR* code.
+ */
+static
+int save_session(struct ltt_session *session,
+       struct lttng_save_session_attr *attr, lttng_sock_cred *creds)
+{
+       int ret, fd;
+       char config_file_path[PATH_MAX];
+       size_t len;
+       struct config_writer *writer = NULL;
+       size_t session_name_len;
+       const char *provided_path;
+
+       assert(session);
+       assert(attr);
+       assert(creds);
+
+       session_name_len = strlen(session->name);
+
+       if (!session_access_ok(session,
+               LTTNG_SOCK_GET_UID_CRED(creds),
+               LTTNG_SOCK_GET_GID_CRED(creds))) {
+               ret = LTTNG_ERR_EPERM;
+               goto end;
+       }
+
+       provided_path = lttng_save_session_attr_get_output_url(attr);
+       if (provided_path) {
+               len = strlen(provided_path);
+               if (len > PATH_MAX) {
+                       ret = LTTNG_ERR_SET_URL;
+                       goto end;
+               }
+               strncpy(config_file_path, provided_path, len);
+       } else {
+               char *home_dir = utils_get_user_home_dir(
+                       LTTNG_SOCK_GET_UID_CRED(creds));
+               if (!home_dir) {
+                       ret = LTTNG_ERR_SET_URL;
+                       goto end;
+               }
+
+               len = snprintf(config_file_path, PATH_MAX,
+                               DEFAULT_SESSION_HOME_CONFIGPATH, home_dir);
+               free(home_dir);
+               if (len < 0) {
+                       PERROR("snprintf save session");
+                       ret = LTTNG_ERR_SET_URL;
+                       goto end;
+               }
+       }
+
+       /*
+        * Check the path fits in PATH_MAX, including the / followed by trailing
+        * .lttng extension and the NULL terminated string.
+        */
+       if (len + session_name_len + 2 +
+                       sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION) > PATH_MAX) {
+               ret = LTTNG_ERR_SET_URL;
+               goto end;
+       }
+
+       ret = run_as_mkdir_recursive(config_file_path, S_IRWXU | S_IRWXG,
+                       LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
+       if (ret) {
+               ret = LTTNG_ERR_SET_URL;
+               goto end;
+       }
+
+       config_file_path[len++] = '/';
+       strncpy(config_file_path + len, session->name, session_name_len);
+       len += session_name_len;
+       strcpy(config_file_path + len, DEFAULT_SESSION_CONFIG_FILE_EXTENSION);
+
+       if (!access(config_file_path, F_OK) && !attr->overwrite) {
+               /* A file with the same name already exists, skip */
+               goto end;
+       }
+
+       fd = run_as_open(config_file_path, O_CREAT | O_WRONLY | O_TRUNC,
+               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
+               LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
+       if (fd < 0) {
+               PERROR("Could not create configuration file");
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       writer = config_writer_create(fd);
+       if (!writer) {
+               ret = LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_sessions);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_open_element(writer, config_element_session);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = config_writer_write_element_string(writer, config_element_name,
+                       session->name);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       ret = save_domains(writer, session);
+       if (ret) {
+               goto end;
+       }
+
+       ret = config_writer_write_element_bool(writer, config_element_started,
+                       session->enabled);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       if (session->snapshot_mode || session->live_timer) {
+               ret = config_writer_open_element(writer, config_element_attributes);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+
+               if (session->snapshot_mode) {
+                       ret = config_writer_write_element_bool(writer,
+                                       config_element_snapshot_mode, 1);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+               } else {
+                       ret = config_writer_write_element_signed_int(writer,
+                                       config_element_live_timer_interval, session->live_timer);
+                       if (ret) {
+                               ret = LTTNG_ERR_SAVE_IO_FAIL;
+                               goto end;
+                       }
+               }
+
+               /* /attributes */
+               ret = config_writer_close_element(writer);
+               if (ret) {
+                       ret = LTTNG_ERR_SAVE_IO_FAIL;
+                       goto end;
+               }
+       }
+
+       ret = save_session_output(writer, session);
+       if (ret) {
+               goto end;
+       }
+
+       /* /session */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+
+       /* /sessions */
+       ret = config_writer_close_element(writer);
+       if (ret) {
+               ret = LTTNG_ERR_SAVE_IO_FAIL;
+               goto end;
+       }
+end:
+       if (writer && config_writer_destroy(writer)) {
+               /* Preserve the original error code */
+               ret = ret ? ret : LTTNG_ERR_SAVE_IO_FAIL;
+       }
+       if (ret) {
+               /* Delete file in case of error */
+               if (unlink(config_file_path)) {
+                       PERROR("Unlinking XML session configuration.");
+               }
+       }
+
+       return ret;
+}
+
+int cmd_save_sessions(struct lttng_save_session_attr *attr,
+       lttng_sock_cred *creds)
+{
+       int ret;
+       const char *session_name;
+       struct ltt_session *session;
+
+       session_lock_list();
+
+       session_name = lttng_save_session_attr_get_session_name(attr);
+       if (session_name) {
+               session = session_find_by_name(session_name);
+               if (!session) {
+                       ret = LTTNG_ERR_SESS_NOT_FOUND;
+                       goto end;
+               }
+
+               session_lock(session);
+               ret = save_session(session, attr, creds);
+               session_unlock(session);
+               if (ret) {
+                       goto end;
+               }
+       } else {
+               struct ltt_session_list *list = session_get_list();
+
+               cds_list_for_each_entry(session, &list->head, list) {
+                       session_lock(session);
+                       ret = save_session(session, attr, creds);
+                       session_unlock(session);
+
+                       /* Don't abort if we don't have the required permissions. */
+                       if (ret && ret != LTTNG_ERR_EPERM) {
+                               goto end;
+                       }
+               }
+       }
+       ret = LTTNG_OK;
+
+end:
+       session_unlock_list();
+       return ret;
+}
diff --git a/src/bin/lttng-sessiond/save.h b/src/bin/lttng-sessiond/save.h
new file mode 100644 (file)
index 0000000..fce9a8d
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef SAVE_H
+#define SAVE_H
+
+#include <lttng/save.h>
+#include <common/compat/socket.h>
+
+int cmd_save_sessions(struct lttng_save_session_attr *attr,
+       lttng_sock_cred *creds);
+
+#endif /* SAVE_H */
diff --git a/src/common/config/config-session-internal.h b/src/common/config/config-session-internal.h
new file mode 100644 (file)
index 0000000..3c51883
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef CONFIG_SESSION_INTERNAL_H
+#define CONFIG_SESSION_INTERNAL_H
+
+const char * const config_element_channel;
+const char * const config_element_channels;
+const char * const config_element_domain;
+const char * const config_element_domains;
+const char * const config_element_event;
+const char * const config_element_events;
+const char * const config_element_context;
+const char * const config_element_contexts;
+const char * const config_element_attributes;
+const char * const config_element_exclusion;
+const char * const config_element_exclusions;
+const char * const config_element_function_attributes;
+const char * const config_element_probe_attributes;
+const char * const config_element_symbol_name;
+const char * const config_element_address;
+const char * const config_element_offset;
+const char * const config_element_name;
+const char * const config_element_enabled;
+const char * const config_element_overwrite_mode;
+const char * const config_element_subbuf_size;
+const char * const config_element_num_subbuf;
+const char * const config_element_switch_timer_interval;
+const char * const config_element_read_timer_interval;
+const char * const config_element_output;
+const char * const config_element_output_type;
+const char * const config_element_tracefile_size;
+const char * const config_element_tracefile_count;
+const char * const config_element_live_timer_interval;
+const char * const config_element_type;
+const char * const config_element_buffer_type;
+const char * const config_element_session;
+const char * const config_element_sessions;
+const char * const config_element_perf;
+const char * const config_element_config;
+const char * const config_element_started;
+const char * const config_element_snapshot_mode;
+const char * const config_element_loglevel;
+const char * const config_element_loglevel_type;
+const char * const config_element_filter;
+const char * const config_element_snapshot_outputs;
+const char * const config_element_consumer_output;
+const char * const config_element_destination;
+const char * const config_element_path;
+const char * const config_element_net_output;
+const char * const config_element_control_uri;
+const char * const config_element_data_uri;
+const char * const config_element_max_size;
+
+const char * const config_domain_type_kernel;
+const char * const config_domain_type_ust;
+const char * const config_domain_type_jul;
+
+const char * const config_buffer_type_per_pid;
+const char * const config_buffer_type_per_uid;
+const char * const config_buffer_type_global;
+
+const char * const config_overwrite_mode_discard;
+const char * const config_overwrite_mode_overwrite;
+
+const char * const config_output_type_splice;
+const char * const config_output_type_mmap;
+
+const char * const config_loglevel_type_all;
+const char * const config_loglevel_type_range;
+const char * const config_loglevel_type_single;
+
+const char * const config_event_type_all;
+const char * const config_event_type_tracepoint;
+const char * const config_event_type_probe;
+const char * const config_event_type_function;
+const char * const config_event_type_function_entry;
+const char * const config_event_type_noop;
+const char * const config_event_type_syscall;
+const char * const config_event_type_kprobe;
+const char * const config_event_type_kretprobe;
+
+const char * const config_event_context_pid;
+const char * const config_event_context_procname;
+const char * const config_event_context_prio;
+const char * const config_event_context_nice;
+const char * const config_event_context_vpid;
+const char * const config_event_context_tid;
+const char * const config_event_context_vtid;
+const char * const config_event_context_ppid;
+const char * const config_event_context_vppid;
+const char * const config_event_context_pthread_id;
+const char * const config_event_context_hostname;
+const char * const config_event_context_ip;
+
+#endif /* CONFIG_SESSION_INTERNAL_H */
index 18baf6bd5626c3c05bb620876b7d66ce195eb5bb..d4385738fd3d37c2c287677718adc4a201389862 100644 (file)
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/utils.h>
+#include <common/config/config-session-internal.h>
+#include <lttng/lttng-error.h>
+#include <libxml/parser.h>
+#include <libxml/valid.h>
+#include <libxml/xmlschemas.h>
 
 #include "config.h"
 #include "config-internal.h"
@@ -44,12 +49,100 @@ const char * const config_str_no = "no";
 const char * const config_str_false = "false";
 const char * const config_str_off = "off";
 const char * const config_xml_encoding = "UTF-8";
-/* Size of the encoding's largest character */
-const size_t config_xml_encoding_bytes_per_char = 2;
+const size_t config_xml_encoding_bytes_per_char = 2;   /* Size of the encoding's largest character */
 const char * const config_xml_indent_string = "\t";
 const char * const config_xml_true = "true";
 const char * const config_xml_false = "false";
 
+const char * const config_element_channel = "channel";
+const char * const config_element_channels = "channels";
+const char * const config_element_domain = "domain";
+const char * const config_element_domains = "domains";
+const char * const config_element_event = "event";
+const char * const config_element_events = "events";
+const char * const config_element_context = "context";
+const char * const config_element_contexts = "contexts";
+const char * const config_element_attributes = "attributes";
+const char * const config_element_exclusion = "exclusion";
+const char * const config_element_exclusions = "exclusions";
+const char * const config_element_function_attributes = "function_attributes";
+const char * const config_element_probe_attributes = "probe_attributes";
+const char * const config_element_symbol_name = "symbol_name";
+const char * const config_element_address = "address";
+const char * const config_element_offset = "offset";
+const char * const config_element_name = "name";
+const char * const config_element_enabled = "enabled";
+const char * const config_element_overwrite_mode = "overwrite_mode";
+const char * const config_element_subbuf_size = "subbuffer_size";
+const char * const config_element_num_subbuf = "subbuffer_count";
+const char * const config_element_switch_timer_interval = "switch_timer_interval";
+const char * const config_element_read_timer_interval = "read_timer_interval";
+const char * const config_element_output = "output";
+const char * const config_element_output_type = "output_type";
+const char * const config_element_tracefile_size = "tracefile_size";
+const char * const config_element_tracefile_count = "tracefile_count";
+const char * const config_element_live_timer_interval = "live_timer_interval";
+const char * const config_element_type = "type";
+const char * const config_element_buffer_type = "buffer_type";
+const char * const config_element_session = "session";
+const char * const config_element_sessions = "sessions";
+const char * const config_element_perf = "perf";
+const char * const config_element_config = "config";
+const char * const config_element_started = "started";
+const char * const config_element_snapshot_mode = "snapshot_mode";
+const char * const config_element_loglevel = "loglevel";
+const char * const config_element_loglevel_type = "loglevel_type";
+const char * const config_element_filter = "filter";
+const char * const config_element_snapshot_outputs = "snapshot_outputs";
+const char * const config_element_consumer_output = "consumer_output";
+const char * const config_element_destination = "destination";
+const char * const config_element_path = "path";
+const char * const config_element_net_output = "net_output";
+const char * const config_element_control_uri = "control_uri";
+const char * const config_element_data_uri = "data_uri";
+const char * const config_element_max_size = "max_size";
+
+const char * const config_domain_type_kernel = "KERNEL";
+const char * const config_domain_type_ust = "UST";
+const char * const config_domain_type_jul = "JUL";
+
+const char * const config_buffer_type_per_pid = "PER_PID";
+const char * const config_buffer_type_per_uid = "PER_UID";
+const char * const config_buffer_type_global = "GLOBAL";
+
+const char * const config_overwrite_mode_discard = "DISCARD";
+const char * const config_overwrite_mode_overwrite = "OVERWRITE";
+
+const char * const config_output_type_splice = "SPLICE";
+const char * const config_output_type_mmap = "MMAP";
+
+const char * const config_loglevel_type_all = "ALL";
+const char * const config_loglevel_type_range = "RANGE";
+const char * const config_loglevel_type_single = "SINGLE";
+
+const char * const config_event_type_all = "ALL";
+const char * const config_event_type_tracepoint = "TRACEPOINT";
+const char * const config_event_type_probe = "PROBE";
+const char * const config_event_type_function = "FUNCTION";
+const char * const config_event_type_function_entry = "FUNCTION_ENTRY";
+const char * const config_event_type_noop = "NOOP";
+const char * const config_event_type_syscall = "SYSCALL";
+const char * const config_event_type_kprobe = "KPROBE";
+const char * const config_event_type_kretprobe = "KRETPROBE";
+
+const char * const config_event_context_pid = "PID";
+const char * const config_event_context_procname = "PROCNAME";
+const char * const config_event_context_prio = "PRIO";
+const char * const config_event_context_nice = "NICE";
+const char * const config_event_context_vpid = "VPID";
+const char * const config_event_context_tid = "TID";
+const char * const config_event_context_vtid = "VTID";
+const char * const config_event_context_ppid = "PPID";
+const char * const config_event_context_vppid = "VPPID";
+const char * const config_event_context_pthread_id = "PTHREAD_ID";
+const char * const config_event_context_hostname = "HOSTNAME";
+const char * const config_event_context_ip = "IP";
+
 static int config_entry_handler_filter(struct handler_filter_args *args,
                const char *section, const char *name, const char *value)
 {
index 8a798744456f6420fa441ac7a5d5e2effce56d09..221bb4811be02ce5a81d35cb40b6fa4c156cbba2 100644 (file)
 #define DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK            DEFAULT_LTTNG_HOME_RUNDIR "/relayd/health-%d"
 
 /* Default daemon configuration file path */
+#define DEFAULT_SYSTEM_CONFIGPATH               CONFIG_LTTNG_SYSTEM_CONFIGDIR \
+       "/lttng"
+
 #define DEFAULT_DAEMON_CONFIG_FILE              "lttng.conf"
 #define DEFAULT_DAEMON_HOME_CONFIGPATH          DEFAULT_LTTNG_HOME_RUNDIR "/" \
        DEFAULT_DAEMON_CONFIG_FILE
-#define DEFAULT_DAEMON_SYSTEM_CONFIGPATH        CONFIG_LTTNG_SYSTEM_CONFIGDIR \
-       "/lttng/" DEFAULT_DAEMON_CONFIG_FILE
+#define DEFAULT_DAEMON_SYSTEM_CONFIGPATH        DEFAULT_SYSTEM_CONFIGPATH "/" \
+       DEFAULT_DAEMON_CONFIG_FILE
+
+/* Default session configuration file path */
+#define DEFAULT_SESSION_PATH                    "sessions"
+#define DEFAULT_SESSION_HOME_CONFIGPATH         DEFAULT_LTTNG_HOME_RUNDIR "/" \
+       DEFAULT_SESSION_PATH
+#define DEFAULT_SESSION_SYSTEM_CONFIGPATH       DEFAULT_SYSTEM_CONFIGPATH "/" \
+       DEFAULT_SESSION_PATH
+#define DEFAULT_SESSION_CONFIG_FILE_EXTENSION   ".lttng"
 
 #define DEFAULT_GLOBAL_APPS_UNIX_SOCK \
        DEFAULT_LTTNG_RUNDIR "/" LTTNG_UST_SOCK_FILENAME
index ea1e5db0ee31dd192d7661dd2537f9a9c5c30298..31596ddd36c3a3314daa2de1db6db19690b7800a 100644 (file)
@@ -30,6 +30,7 @@
 #include <inttypes.h>
 #include <regex.h>
 #include <grp.h>
+#include <pwd.h>
 
 #include <common/common.h>
 #include <common/runas.h>
@@ -854,6 +855,46 @@ char *utils_get_home_dir(void)
        return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR);
 }
 
+/**
+ * Get user's home directory. Dynamically allocated, must be freed
+ * by the caller.
+ */
+LTTNG_HIDDEN
+char *utils_get_user_home_dir(uid_t uid)
+{
+       struct passwd pwd;
+       struct passwd *result;
+       char *home_dir = NULL;
+       char *buf = NULL;
+       long buflen;
+       int ret;
+
+       buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+       if (buflen == -1) {
+               goto end;
+       }
+retry:
+       buf = zmalloc(buflen);
+       if (!buf) {
+               goto end;
+       }
+
+       ret = getpwuid_r(uid, &pwd, buf, buflen, &result);
+       if (ret || !result) {
+               if (ret == ERANGE) {
+                       free(buf);
+                       buflen *= 2;
+                       goto retry;
+               }
+               goto end;
+       }
+
+       home_dir = strdup(pwd.pw_dir);
+end:
+       free(buf);
+       return home_dir;
+}
+
 /*
  * With the given format, fill dst with the time of len maximum siz.
  *
index e92ca7006542e2e8cd5276e27eac197f29e7c681..63290a7bd2b7098f758a90e3b936cb6fd6b18d5a 100644 (file)
@@ -46,6 +46,7 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
 int utils_parse_size_suffix(char *str, uint64_t *size);
 int utils_get_count_order_u32(uint32_t x);
 char *utils_get_home_dir(void);
+char *utils_get_user_home_dir(uid_t uid);
 size_t utils_get_current_time_str(const char *format, char *dst, size_t len);
 gid_t utils_get_group_id(const char *name);
 char *utils_generate_optstring(const struct option *long_options,
This page took 0.048537 seconds and 4 git commands to generate.