Skip to content

Commit

Permalink
Merge pull request #265 from bobrik/ivan/inode-first
Browse files Browse the repository at this point in the history
Resolve cgroup inode in fanotify as soon as possible
  • Loading branch information
bobrik authored Sep 13, 2023
2 parents 1fe0f1a + 1cdace4 commit f9a4fdd
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions cgroup/fanotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ func (m *fanotifyMonitor) handleFanotify(_ *unix.FanotifyEventMetadata, buf []by
return fmt.Errorf("error reading file_handle: %v", err)
}

dir, err := m.resolveDir(handle)
fd, err := unix.OpenByHandleAt(int(m.mount.Fd()), handle, 0)
if err != nil {
return fmt.Errorf("error reading fanotify event path: %v", err)
return fmt.Errorf("error opening event fd: %v", err)
}

defer syscall.Close(fd)

name, err := reader.ReadString('\x00')
if err != nil {
return fmt.Errorf("error reading name: %v", err)
Expand All @@ -123,34 +125,22 @@ func (m *fanotifyMonitor) handleFanotify(_ *unix.FanotifyEventMetadata, buf []by
// Truncate \x00 at the end
name = name[:len(name)-1]

path := fmt.Sprintf("%s/%s", dir, name)

inode, err := inode(path)
stat := unix.Stat_t{}
err = unix.Fstatat(fd, name, &stat, 0)
if err != nil {
// Sometimes we can't get the inode in type and it shouldn't be a fatal error
log.Printf("error resolving inode for %q: %v", path, err)
log.Printf("error calling fstatat for %q: %v", name, err)
return nil
}

m.mapping[inode] = path

return nil
}

func (m *fanotifyMonitor) resolveDir(handle unix.FileHandle) (string, error) {
fd, err := unix.OpenByHandleAt(int(m.mount.Fd()), handle, 0)
if err != nil {
return "", fmt.Errorf("error opening event fd: %v", err)
}

defer syscall.Close(fd)

dir, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", fd))
if err != nil {
return "", fmt.Errorf("error resolving event fd symlink: %v", err)
return fmt.Errorf("error resolving event fd symlink: %v", err)
}

return dir, nil
m.mapping[int(stat.Ino)] = fmt.Sprintf("%s/%s", dir, name)

return nil
}

// Reading struct file_handle piece by piece:
Expand Down

0 comments on commit f9a4fdd

Please sign in to comment.