+
+/*
+ * Open stream of channel, register it to the kernel tracer and add it
+ * to the stream list of the channel.
+ *
+ * Return the number of created stream. Else, a negative value.
+ */
+int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
+{
+ int ret;
+ struct ltt_kernel_stream *lks;
+
+ while ((ret = kernctl_create_stream(channel->fd)) > 0) {
+ lks = trace_kernel_create_stream();
+ if (lks == NULL) {
+ close(ret);
+ goto error;
+ }
+
+ lks->fd = ret;
+ /* Prevent fd duplication after execlp() */
+ ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl session fd");
+ }
+
+ ret = asprintf(&lks->pathname, "%s/%s_%d",
+ channel->pathname, channel->channel->name, channel->stream_count);
+ if (ret < 0) {
+ perror("asprintf kernel create stream");
+ goto error;
+ }
+
+ /* Add stream to channe stream list */
+ cds_list_add(&lks->list, &channel->stream_list.head);
+ channel->stream_count++;
+
+ DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
+ channel->stream_count, lks->fd, lks->state, lks->pathname);
+ }
+
+ return channel->stream_count;
+
+error:
+ return -1;
+}
+
+/*
+ * Open the metadata stream and set it to the kernel session.
+ */
+int kernel_open_metadata_stream(struct ltt_kernel_session *session)
+{
+ int ret;
+
+ ret = kernctl_create_stream(session->metadata->fd);
+ if (ret < 0) {
+ perror("kernel create metadata stream");
+ goto error;
+ }
+
+ DBG("Kernel metadata stream created (fd: %d)", ret);
+ session->metadata_stream_fd = ret;
+ /* Prevent fd duplication after execlp() */
+ ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl session fd");
+ }
+
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * Get the event list from the kernel tracer and return the number of elements.
+ */
+ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
+{
+ int fd, pos;
+ char *event;
+ size_t nbmem, count = 0;
+ ssize_t size;
+ FILE *fp;
+ struct lttng_event *elist;
+
+ fd = kernctl_tracepoint_list(tracer_fd);
+ if (fd < 0) {
+ perror("kernel tracepoint list");
+ goto error;
+ }
+
+ fp = fdopen(fd, "r");
+ if (fp == NULL) {
+ perror("kernel tracepoint list fdopen");
+ goto error_fp;
+ }
+
+ /*
+ * Init memory size counter
+ * See kernel-ctl.h for explanation of this value
+ */
+ nbmem = KERNEL_EVENT_LIST_SIZE;
+ elist = malloc(sizeof(struct lttng_event) * nbmem);
+
+ while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
+ if (count > nbmem) {
+ DBG("Reallocating event list from %zu to %zu bytes", nbmem,
+ nbmem + KERNEL_EVENT_LIST_SIZE);
+ /* Adding the default size again */
+ nbmem += KERNEL_EVENT_LIST_SIZE;
+ elist = realloc(elist, nbmem);
+ if (elist == NULL) {
+ perror("realloc list events");
+ count = -ENOMEM;
+ goto end;
+ }
+ }
+ strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
+ elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
+ count++;
+ }
+
+ *events = elist;
+ DBG("Kernel list events done (%zu events)", count);
+end:
+ fclose(fp); /* closes both fp and fd */
+ return count;
+
+error_fp:
+ close(fd);
+error:
+ return -1;
+}