+ DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
+
+ /* Check command that needs a session */
+ switch (cmd_ctx->lsm->cmd_type) {
+ case LTTNG_CREATE_SESSION:
+ case LTTNG_LIST_SESSIONS:
+ case UST_LIST_APPS:
+ break;
+ default:
+ cmd_ctx->session = find_session_by_uuid(cmd_ctx->lsm->session_uuid);
+ if (cmd_ctx->session == NULL) {
+ ret = LTTCOMM_SELECT_SESS;
+ goto error;
+ }
+ break;
+ }
+
+ /* Check command for kernel tracing */
+ switch (cmd_ctx->lsm->cmd_type) {
+ case KERNEL_CREATE_SESSION:
+ case KERNEL_CREATE_CHANNEL:
+ case KERNEL_CREATE_STREAM:
+ case KERNEL_DISABLE_EVENT:
+ case KERNEL_ENABLE_EVENT:
+ case KERNEL_OPEN_METADATA:
+ case KERNEL_START_TRACE:
+ case KERNEL_STOP_TRACE:
+ /* TODO: reconnect to kernel tracer to check if
+ * it's loadded */
+ if (kernel_tracer_fd == 0) {
+ ret = LTTCOMM_KERN_NA;
+ goto error;
+ }
+ break;
+ }
+
+ /* Connect to ust apps if available pid */
+ if (cmd_ctx->lsm->pid > 0) {
+ /* Connect to app using ustctl API */
+ cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid);
+ if (cmd_ctx->ust_sock < 0) {
+ ret = LTTCOMM_NO_TRACEABLE;
+ goto error;
+ }
+ }
+
+ /* Process by command type */
+ switch (cmd_ctx->lsm->cmd_type) {
+ case KERNEL_CREATE_SESSION:
+ {
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ ret = start_kconsumerd();
+ if (ret < 0) {
+ goto error;
+ }
+
+ DBG("Creating kernel session");
+
+ ret = kernel_create_session(cmd_ctx->session, kernel_tracer_fd);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_SESS_FAIL;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
+ case KERNEL_CREATE_CHANNEL:
+ {
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ DBG("Creating kernel channel");
+
+ ret = kernel_create_channel(cmd_ctx->session->kernel_session);
+
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_CHAN_FAIL;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
+ case KERNEL_ENABLE_EVENT:
+ {
+ /* Setup lttng message with no payload */
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ DBG("Enabling kernel event %s", cmd_ctx->lsm->u.event.event_name);
+
+ ret = kernel_enable_event(cmd_ctx->session->kernel_session, cmd_ctx->lsm->u.event.event_name);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_ENABLE_FAIL;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
+ case KERNEL_OPEN_METADATA:
+ {
+ /* Setup lttng message with no payload */
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ DBG("Open kernel metadata");
+
+ ret = kernel_open_metadata(cmd_ctx->session->kernel_session);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_META_FAIL;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
+ case KERNEL_CREATE_STREAM:
+ {
+ struct ltt_kernel_channel *chan;
+ /* Setup lttng message with no payload */
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ DBG("Creating kernel stream");
+
+ ret = kernel_create_metadata_stream(cmd_ctx->session->kernel_session);
+ if (ret < 0) {
+ ERR("Kernel create metadata stream failed");
+ ret = LTTCOMM_KERN_STREAM_FAIL;
+ goto error;
+ }
+
+ /* For each channel */
+ cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
+ ret = kernel_create_channel_stream(chan);
+ if (ret < 0) {
+ ERR("Kernel create channel stream failed");
+ ret = LTTCOMM_KERN_STREAM_FAIL;
+ goto error;
+ }
+ /* Update the stream global counter */
+ cmd_ctx->session->kernel_session->stream_count_global += ret;
+ }
+
+ ret = LTTCOMM_OK;
+ break;
+ }
+ case KERNEL_START_TRACE:
+ {
+ /* Setup lttng message with no payload */
+ ret = setup_lttng_msg(cmd_ctx, 0);
+ if (ret < 0) {
+ goto setup_error;
+ }
+
+ DBG("Start kernel tracing");
+
+ ret = create_trace_dir(cmd_ctx->session->kernel_session);
+ if (ret < 0) {
+ if (ret == -EEXIST) {
+ ret = LTTCOMM_KERN_DIR_EXIST;
+ } else {
+ ret = LTTCOMM_KERN_DIR_FAIL;
+ goto error;
+ }
+ }
+
+ ret = kernel_start_session(cmd_ctx->session->kernel_session);
+ if (ret < 0) {
+ ERR("Kernel start session failed");
+ ret = LTTCOMM_KERN_START_FAIL;
+ goto error;
+ }