+#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) {
+ 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("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;
+ }
+}
+#endif //HAS_INOTIFY