+/*
+ * Start a kernel session by opening all necessary streams.
+ */
+static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
+{
+ int ret;
+ struct ltt_kernel_channel *kchan;
+
+ /* Open kernel metadata */
+ if (ksess->metadata == NULL) {
+ ret = kernel_open_metadata(ksess);
+ if (ret < 0) {
+ ret = LTTNG_ERR_KERN_META_FAIL;
+ goto error;
+ }
+ }
+
+ /* Open kernel metadata stream */
+ if (ksess->metadata_stream_fd < 0) {
+ ret = kernel_open_metadata_stream(ksess);
+ if (ret < 0) {
+ ERR("Kernel create metadata stream failed");
+ ret = LTTNG_ERR_KERN_STREAM_FAIL;
+ goto error;
+ }
+ }
+
+ /* For each channel */
+ cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
+ if (kchan->stream_count == 0) {
+ ret = kernel_open_channel_stream(kchan);
+ if (ret < 0) {
+ ret = LTTNG_ERR_KERN_STREAM_FAIL;
+ goto error;
+ }
+ /* Update the stream global counter */
+ ksess->stream_count_global += ret;
+ }
+ }
+
+ /* Setup kernel consumer socket and send fds to it */
+ ret = init_kernel_tracing(ksess);
+ if (ret < 0) {
+ ret = LTTNG_ERR_KERN_START_FAIL;
+ goto error;
+ }
+
+ /* This start the kernel tracing */
+ ret = kernel_start_session(ksess);
+ if (ret < 0) {
+ ret = LTTNG_ERR_KERN_START_FAIL;
+ goto error;
+ }
+
+ /* Quiescent wait after starting trace */
+ kernel_wait_quiescent(wpipe);
+
+ ksess->started = 1;
+
+ ret = LTTNG_OK;
+
+error:
+ return ret;
+}
+