+ DBG("Cleaning up all session");
+ /* Cleanup ALL session */
+ cds_list_for_each_entry(sess, <t_session_list.head, list) {
+ teardown_kernel_session(sess);
+ // TODO complete session cleanup (including UST)
+ }
+
+ close(kernel_tracer_fd);
+}
+
+/*
+ * send_unix_sock
+ *
+ * Send data on a unix socket using the liblttsessiondcomm API.
+ *
+ * Return lttcomm error code.
+ */
+static int send_unix_sock(int sock, void *buf, size_t len)
+{
+ /* Check valid length */
+ if (len <= 0) {
+ return -1;
+ }
+
+ return lttcomm_send_unix_sock(sock, buf, len);
+}
+
+/*
+ * clean_command_ctx
+ *
+ * Free memory of a command context structure.
+ */
+static void clean_command_ctx(struct command_ctx *cmd_ctx)
+{
+ DBG("Clean command context structure %p", cmd_ctx);
+ if (cmd_ctx) {
+ if (cmd_ctx->llm) {
+ free(cmd_ctx->llm);
+ }
+ if (cmd_ctx->lsm) {
+ free(cmd_ctx->lsm);
+ }
+ free(cmd_ctx);
+ cmd_ctx = NULL;
+ }
+}
+
+/*
+ * ust_connect_app
+ *
+ * Return a socket connected to the libust communication socket
+ * of the application identified by the pid.
+ *
+ * If the pid is not found in the traceable list,
+ * return -1 to indicate error.
+ */
+static int ust_connect_app(pid_t pid)
+{
+ int sock;
+ struct ltt_traceable_app *lta;
+
+ DBG("Connect to application pid %d", pid);
+
+ lta = find_app_by_pid(pid);
+ if (lta == NULL) {
+ /* App not found */
+ DBG("Application pid %d not found", pid);
+ return -1;
+ }
+
+ sock = ustctl_connect_pid(lta->pid);
+ if (sock < 0) {
+ ERR("Fail connecting to the PID %d\n", pid);
+ }
+
+ return sock;
+}
+
+/*
+ * notify_apps
+ *
+ * Notify apps by writing 42 to a named pipe using name.
+ * Every applications waiting for a ltt-sessiond will be notified
+ * and re-register automatically to the session daemon.
+ *
+ * Return open or write error value.
+ */
+static int notify_apps(const char *name)
+{
+ int fd;
+ int ret = -1;
+
+ DBG("Notify the global application pipe");
+
+ /* Try opening the global pipe */
+ fd = open(name, O_WRONLY);
+ if (fd < 0) {
+ goto error;
+ }
+
+ /* Notify by writing on the pipe */
+ ret = write(fd, "42", 2);
+ if (ret < 0) {
+ perror("write");
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * setup_lttng_msg
+ *
+ * Setup the outgoing data buffer for the response (llm) by allocating the
+ * right amount of memory and copying the original information from the lsm
+ * structure.
+ *
+ * Return total size of the buffer pointed by buf.
+ */
+static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
+{
+ int ret, buf_size, trace_name_size;
+
+ /*
+ * Check for the trace_name. If defined, it's part of the payload data of
+ * the llm structure.
+ */
+ trace_name_size = strlen(cmd_ctx->lsm->trace_name);
+ buf_size = trace_name_size + size;
+
+ cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
+ if (cmd_ctx->llm == NULL) {
+ perror("malloc");
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ /* Copy common data */
+ cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
+ cmd_ctx->llm->pid = cmd_ctx->lsm->pid;
+ if (!uuid_is_null(cmd_ctx->lsm->session_uuid)) {
+ uuid_copy(cmd_ctx->llm->session_uuid, cmd_ctx->lsm->session_uuid);
+ }
+
+ cmd_ctx->llm->trace_name_offset = trace_name_size;
+ cmd_ctx->llm->data_size = size;
+ cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
+
+ /* Copy trace name to the llm structure. Begining of the payload. */
+ memcpy(cmd_ctx->llm->payload, cmd_ctx->lsm->trace_name, trace_name_size);
+
+ return buf_size;
+
+error:
+ return ret;
+}