Debian Codegraph @

analyzer/fanotify/service/service.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package service
18
19 import (
20 "bufio"
21 "encoding/binary"
22 "fmt"
23 "io"
24 "os"
25 "time"
26
27 "github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn"
28 "golang.org/x/sys/unix"
29 )
30
31 // Serve starts fanotify at target directory and notifies all accessed files.
32 // Passed io.Reader an io.Writer are used for communicating with the client.
33 func Serve(target string, r io.Reader, w io.Writer) error {
34 sConn := conn.NewService(r, w, 5*time.Second)
35
36 fd, err := unix.FanotifyInit(unix.FAN_CLASS_NOTIF, unix.O_RDONLY)
37 if err != nil {
38 return fmt.Errorf("fanotify_init: %w", err)
39 }
40
41 // This blocks until the client tells us to start monitoring the target mountpoint.
42 if err := sConn.WaitStart(); err != nil {
43 return fmt.Errorf("waiting for start inst: %w", err)
44 }
45
46 // Start monitoring the target mountpoint.
47 if err := unix.FanotifyMark(fd,
48 unix.FAN_MARK_ADD|unix.FAN_MARK_MOUNT,
49 unix.FAN_ACCESS|unix.FAN_OPEN,
50 unix.AT_FDCWD,
51 target,
52 ); err != nil {
53 return fmt.Errorf("fanotify_mark: %w", err)
54 }
55
56 // Notify "started" state to the client.
57 if err := sConn.SendStarted(); err != nil {
58 return fmt.Errorf("failed to send started message: %w", err)
59 }
60
61 nr := bufio.NewReader(os.NewFile(uintptr(fd), ""))
62 for {
63 event := &unix.FanotifyEventMetadata{}
64 if err := binary.Read(nr, binary.LittleEndian, event); err != nil {
65 if err == io.EOF {
66 break
67 }
68 return fmt.Errorf("read fanotify fd: %w", err)
69 }
70 if event.Vers != unix.FANOTIFY_METADATA_VERSION {
71 return fmt.Errorf("Fanotify version mismatch %d(got) != %d(want)",
72 event.Vers, unix.FANOTIFY_METADATA_VERSION)
73 }
74 if event.Fd < 0 {
75 // queue overflow
76 // TODO: do we need something special?
77 fmt.Fprintf(os.Stderr, "Warn: queue overflow")
78 continue
79 }
80
81 // Notify file descriptor.
82 // NOTE: There is no guarantee that we have /proc in this mount namespace
83 // (the target container's rootfs is mounted on "/") so we send file
84 // descriptor and let the client resolve the path of this file using /proc of
85 // this process.
86 if err := sConn.SendFd(int(event.Fd)); err != nil {
87 return fmt.Errorf("failed to send fd %d to client: %w", fd, err)
88 }
89 if err := unix.Close(int(event.Fd)); err != nil {
90 return fmt.Errorf("Close(fd): %w", err)
91 }
92
93 continue
94 }
95
96 return nil
97 }