- quit_program = 1;
-}
-
-
-int open_buffer_file(char *filename, char *path_channel, char *path_trace,
- struct channel_trace_fd *fd_pairs)
-{
- int open_ret = 0;
- int ret = 0;
- struct stat stat_buf;
-
- if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) {
- if(dump_flight_only) {
- printf_verbose("Skipping normal channel %s\n",
- path_channel);
- return 0;
- }
- } else {
- if(dump_normal_only) {
- printf_verbose("Skipping flight channel %s\n",
- path_channel);
- return 0;
- }
- }
- printf_verbose("Opening file.\n");
-
- fd_pairs->pair = realloc(fd_pairs->pair,
- ++fd_pairs->num_pairs * sizeof(struct fd_pair));
-
- /* Open the channel in read mode */
- fd_pairs->pair[fd_pairs->num_pairs-1].channel =
- open(path_channel, O_RDONLY | O_NONBLOCK);
- if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
- perror(path_channel);
- fd_pairs->num_pairs--;
- return 0; /* continue */
- }
- /* Open the trace in write mode, only append if append_mode */
- ret = stat(path_trace, &stat_buf);
- if(ret == 0) {
- if(append_mode) {
- printf_verbose("Appending to file %s as requested\n",
- path_trace);
-
- fd_pairs->pair[fd_pairs->num_pairs-1].trace =
- open(path_trace, O_WRONLY,
- S_IRWXU|S_IRWXG|S_IRWXO);
- if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
- perror(path_trace);
- open_ret = -1;
- close(fd_pairs->pair[fd_pairs->num_pairs-1].channel);
- fd_pairs->num_pairs--;
- goto end;
- }
- ret = lseek(fd_pairs->pair[fd_pairs->num_pairs-1].trace,
- 0, SEEK_END);
- if (ret < 0) {
- perror(path_trace);
- open_ret = -1;
- close(fd_pairs->pair[fd_pairs->num_pairs-1].channel);
- close(fd_pairs->pair[fd_pairs->num_pairs-1].trace);
- fd_pairs->num_pairs--;
- goto end;
- }
- } else {
- printf("File %s exists, cannot open. Try append mode.\n", path_trace);
- open_ret = -1;
- close(fd_pairs->pair[fd_pairs->num_pairs-1].channel);
- fd_pairs->num_pairs--;
- goto end;
- }
- } else {
- if(errno == ENOENT) {
- fd_pairs->pair[fd_pairs->num_pairs-1].trace =
- open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
- S_IRWXU|S_IRWXG|S_IRWXO);
- if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
- perror(path_trace);
- open_ret = -1;
- close(fd_pairs->pair[fd_pairs->num_pairs-1].channel);
- fd_pairs->num_pairs--;
- goto end;
- }
- }
- }
-end:
- return open_ret;
-}
-
-int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name,
- struct channel_trace_fd *fd_pairs, int *inotify_fd,
- struct inotify_watch_array *iwatch_array)
-{
- DIR *channel_dir = opendir(subchannel_name);
- struct dirent *entry;
- struct stat stat_buf;
- int ret;
- char path_channel[PATH_MAX];
- int path_channel_len;
- char *path_channel_ptr;
- char path_trace[PATH_MAX];
- int path_trace_len;
- char *path_trace_ptr;
- int open_ret = 0;
-
- if(channel_dir == NULL) {
- perror(subchannel_name);
- open_ret = ENOENT;
- goto end;
- }
-
- printf_verbose("Creating trace subdirectory %s\n", subtrace_name);
- ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
- if(ret == -1) {
- if(errno != EEXIST) {
- perror(subtrace_name);
- open_ret = -1;
- goto end;
- }
- }
-
- strncpy(path_channel, subchannel_name, PATH_MAX-1);
- path_channel_len = strlen(path_channel);
- path_channel[path_channel_len] = '/';
- path_channel_len++;
- path_channel_ptr = path_channel + path_channel_len;
-
- strncpy(path_trace, subtrace_name, PATH_MAX-1);
- path_trace_len = strlen(path_trace);
- path_trace[path_trace_len] = '/';
- path_trace_len++;
- path_trace_ptr = path_trace + path_trace_len;
-
-#ifdef HAS_INOTIFY
- iwatch_array->elem = realloc(iwatch_array->elem,
- ++iwatch_array->num * sizeof(struct inotify_watch));
-
- printf_verbose("Adding inotify for channel %s\n", path_channel);
- iwatch_array->elem[iwatch_array->num-1].wd = inotify_add_watch(*inotify_fd, path_channel, IN_CREATE);
- strcpy(iwatch_array->elem[iwatch_array->num-1].path_channel, path_channel);
- strcpy(iwatch_array->elem[iwatch_array->num-1].path_trace, path_trace);
- printf_verbose("Added inotify for channel %s, wd %u\n",
- iwatch_array->elem[iwatch_array->num-1].path_channel,
- iwatch_array->elem[iwatch_array->num-1].wd);
-#endif
-
- while((entry = readdir(channel_dir)) != NULL) {
-
- if(entry->d_name[0] == '.') continue;
-
- strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
- strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
-
- ret = stat(path_channel, &stat_buf);
- if(ret == -1) {
- perror(path_channel);
- continue;
- }
-
- printf_verbose("Channel file : %s\n", path_channel);
-
- if(S_ISDIR(stat_buf.st_mode)) {
-
- printf_verbose("Entering channel subdirectory...\n");
- ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs,
- inotify_fd, iwatch_array);
- if(ret < 0) continue;
- } else if(S_ISREG(stat_buf.st_mode)) {
- open_ret = open_buffer_file(entry->d_name, path_channel, path_trace,
- fd_pairs);
- if(open_ret)
- goto end;
- }
- }
-
-end:
- closedir(channel_dir);
-
- return open_ret;
-}
-
-
-int read_subbuffer(struct fd_pair *pair)
-{
- unsigned int consumed_old, len;
- int err;
- long ret;
- off_t offset;
-
-
- err = ioctl(pair->channel, RELAY_GET_SB, &consumed_old);
- printf_verbose("cookie : %u\n", consumed_old);
- if(err != 0) {
- ret = errno;
- perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
- goto get_error;
- }
-#if 0
- err = TEMP_FAILURE_RETRY(write(pair->trace,
- pair->mmap
- + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
- pair->subbuf_size));
-
- if(err < 0) {
- ret = errno;
- perror("Error in writing to file");
- goto write_error;
- }
-#endif //0
- err = ioctl(pair->channel, RELAY_GET_SB_SIZE, &len);
- if(err != 0) {
- ret = errno;
- perror("Getting sub-buffer len failed.");
- goto get_error;
- }
-
- offset = 0;
- while (len > 0) {
- printf_verbose("splice chan to pipe offset %lu\n",
- (unsigned long)offset);
- ret = splice(pair->channel, &offset, thread_pipe[1], NULL,
- len, SPLICE_F_MOVE | SPLICE_F_MORE);
- printf_verbose("splice chan to pipe ret %ld\n", ret);
- if (ret < 0) {
- perror("Error in relay splice");
- goto write_error;
- }
- ret = splice(thread_pipe[0], NULL, pair->trace, NULL,
- ret, SPLICE_F_MOVE | SPLICE_F_MORE);
- printf_verbose("splice pipe to file %ld\n", ret);
- if (ret < 0) {
- perror("Error in file splice");
- goto write_error;
- }
- len -= ret;
- }
-
-#if 0
- err = fsync(pair->trace);
- if(err < 0) {
- ret = errno;
- perror("Error in writing to file");
- goto write_error;
- }
-#endif //0
-write_error:
- ret = 0;
- err = ioctl(pair->channel, RELAY_PUT_SB, &consumed_old);
- if(err != 0) {
- ret = errno;
- if(errno == EFAULT) {
- perror("Error in unreserving sub buffer\n");
- } else if(errno == EIO) {
- /* Should never happen with newer LTTng versions */
- perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
- }
- goto get_error;
- }
-
-get_error:
- return ret;
-}
-
-
-int map_channels(struct channel_trace_fd *fd_pairs,
- int idx_begin, int idx_end)
-{
- int i,j;
- int ret=0;
-
- if(fd_pairs->num_pairs <= 0) {
- printf("No channel to read\n");
- goto end;
- }
-
- /* Get the subbuf sizes and number */
-
- for(i=idx_begin;i<idx_end;i++) {
- struct fd_pair *pair = &fd_pairs->pair[i];
-
- ret = ioctl(pair->channel, RELAY_GET_N_SB, &pair->n_sb);
- if(ret != 0) {
- perror("Error in getting the number of sub-buffers");
- goto end;
- }
- ret = ioctl(pair->channel, RELAY_GET_MAX_SB_SIZE,
- &pair->max_sb_size);
- if(ret != 0) {
- perror("Error in getting the max sub-buffer size");
- goto end;
- }
- ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */
- if(ret != 0) {
- perror("Error in mutex init");
- goto end;
- }
- }
-
-#if 0
- /* Mmap each FD */
- for(i=idx_begin;i<idx_end;i++) {
- struct fd_pair *pair = &fd_pairs->pair[i];
-
- pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ,
- MAP_SHARED, pair->channel, 0);
- if(pair->mmap == MAP_FAILED) {
- perror("Mmap error");
- goto munmap;
- }
- }
-
- goto end; /* success */
-
- /* Error handling */
- /* munmap only the successfully mmapped indexes */
-munmap:
- /* Munmap each FD */
- for(j=idx_begin;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;
- }
-
-#endif //0
-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;
-
-#if 0
- err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
- if(err_ret != 0) {
- perror("Error in munmap");
- }
- ret |= err_ret;
-#endif //0
- err_ret = pthread_mutex_destroy(&pair->mutex);
- if(err_ret != 0) {
- perror("Error in mutex destroy");
- }
- ret |= err_ret;
- }
-
- return ret;
-}
-
-#ifdef HAS_INOTIFY
-/* Inotify event arrived.
- *
- * Only support add file for now.
- */
-
-int read_inotify(int inotify_fd,
- struct channel_trace_fd *fd_pairs,
- struct inotify_watch_array *iwatch_array)
-{
- char buf[sizeof(struct inotify_event) + PATH_MAX];
- char path_channel[PATH_MAX];
- char path_trace[PATH_MAX];
- ssize_t len;
- struct inotify_event *ievent;
- size_t offset;
- unsigned int i;
- int ret;
- int old_num;
-
- offset = 0;
- len = read(inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX);
- if(len < 0) {
-
- if(errno == EAGAIN)
- return 0; /* another thread got the data before us */
-
- printf("Error in read from inotify FD %s.\n", strerror(len));
- return -1;
- }
- while(offset < len) {
- ievent = (struct inotify_event *)&(buf[offset]);
- for(i=0; i<iwatch_array->num; i++) {
- if(iwatch_array->elem[i].wd == ievent->wd &&
- ievent->mask == IN_CREATE) {
- printf_verbose(
- "inotify wd %u event mask : %u for %s%s\n",
- ievent->wd, ievent->mask,
- iwatch_array->elem[i].path_channel,
- ievent->name);
- old_num = fd_pairs->num_pairs;
- strcpy(path_channel, iwatch_array->elem[i].path_channel);
- strcat(path_channel, ievent->name);
- strcpy(path_trace, iwatch_array->elem[i].path_trace);
- strcat(path_trace, ievent->name);
- if(ret = open_buffer_file(ievent->name, path_channel,
- path_trace, fd_pairs)) {
- printf("Error opening buffer file\n");
- return -1;
- }
- if(ret = map_channels(fd_pairs, old_num, fd_pairs->num_pairs)) {
- printf("Error mapping channel\n");
- return -1;
- }
-
- }
- }
- offset += sizeof(*ievent) + ievent->len;
- }