int FsDropInService::processDropInWatcher()

in src/oomd/dropin/FsDropInService.cpp [258:298]


int FsDropInService::processDropInWatcher(int fd) {
  const struct inotify_event* event;
  alignas(struct inotify_event) std::array<char, 4096> buf;

  while (true) {
    int len = ::read(fd, buf.data(), sizeof(buf));
    if (len < 0 && errno != EAGAIN) {
      OLOG << "read: " << Util::strerror_r();
      return 1;
    }

    if (len <= 0) {
      break;
    }

    for (char* ptr = buf.data(); ptr < (buf.data() + len);
         ptr += sizeof(struct inotify_event) + event->len) {
      event = reinterpret_cast<const struct inotify_event*>(ptr);

      if (event->mask & (IN_MOVED_TO | IN_MODIFY)) {
        // Remove and re-add drop in if a file has been added to the
        // watched directory
        processDropInAdd(event->name);
      } else if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
        // Remove drop in if file has been moved from or removed from
        // the watched directory
        processDropInRemove(event->name);
      } else if (event->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) {
        // Remove stale watch descriptor for drop in if watched file or
        // directory itself is moved or deleted
        if (deregisterDropInWatcherFromEventLoop()) {
          return 1;
        }
        drop_in_dir_deleted_ = true;
        return 0;
      }
    }
  }

  return 0;
}