+ goto end; /* success */
+
+ /* Error handling */
+ /* munmap only the successfully mmapped indexes */
+munmap:
+ /* Munmap each FD */
+ for(j=0;j<i;j++) {
+ struct fd_pair *pair = &fd_pairs->pair[j];
+ int err_ret;
+
+ err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
+ if(err_ret != 0) {
+ perror("Error in munmap");
+ }
+ ret |= err_ret;
+ }
+
+end:
+ return ret;
+
+
+}
+
+
+int unmap_channels(struct channel_trace_fd *fd_pairs)
+{
+ int j;
+ int ret=0;
+
+ /* Munmap each FD */
+ for(j=0;j<fd_pairs->num_pairs;j++) {
+ struct fd_pair *pair = &fd_pairs->pair[j];
+ int err_ret;
+
+ err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
+ if(err_ret != 0) {
+ perror("Error in munmap");
+ }
+ ret |= err_ret;
+ err_ret = pthread_mutex_destroy(&pair->mutex);
+ if(err_ret != 0) {
+ perror("Error in mutex destroy");
+ }
+ ret |= err_ret;
+ }
+
+ return ret;
+}
+
+
+/* read_channels
+ *
+ * Thread worker.
+ *
+ * Read the relayfs channels and write them in the paired tracefiles.
+ *
+ * @fd_pairs : paired channels and trace files.
+ *
+ * returns (void*)0 on success, (void*)-1 on error.
+ *
+ * Note that the high priority polled channels are consumed first. We then poll
+ * again to see if these channels are still in priority. Only when no
+ * high priority channel is left, we start reading low priority channels.
+ *
+ * Note that a channel is considered high priority when the buffer is almost
+ * full.
+ */
+
+void * read_channels(void *arg)
+{
+ struct pollfd *pollfd;
+ int i,j;
+ int num_rdy, num_hup;
+ int high_prio;
+ int ret = 0;
+ struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg;