| 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 connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/ |
| 18 | |
| 19 | import ( |
| 20 | "bufioscip-go gomod github.com/golang/go/src go1.19 bufio/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.19 io/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.19 os/" |
| 24 | "strconvscip-go gomod github.com/golang/go/src go1.19 strconv/" |
| 25 | "stringsscip-go gomod github.com/golang/go/src go1.19 strings/" |
| 26 | "timescip-go gomod github.com/golang/go/src go1.19 time/" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | mesStartscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStart. = "start" |
| 31 | mesStartedscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStarted. = "started" |
| 32 | mesAckscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesAck. = "ack" |
| 33 | mesFdPrefixscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesFdPrefix. = "fd:" |
| 34 | ) |
| 35 | |
| 36 | // Client is the client to talk to the fanotifier. |
| 37 | type Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client# struct { |
| 38 | rscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#r. ioscip-go gomod github.com/golang/go/src go1.19 io/.Readerscip-go gomod github.com/golang/go/src go1.19 io/Reader# |
| 39 | wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#w. ioscip-go gomod github.com/golang/go/src go1.19 io/.Writerscip-go gomod github.com/golang/go/src go1.19 io/Writer# |
| 40 | notificationscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#notification. *bufioscip-go gomod github.com/golang/go/src go1.19 bufio/.Scannerscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner# |
| 41 | servicePidscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#servicePid. int |
| 42 | timeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#timeout. timescip-go gomod github.com/golang/go/src go1.19 time/.Durationscip-go gomod github.com/golang/go/src go1.19 time/Duration# |
| 43 | } |
| 44 | |
| 45 | // NewClient returns the client to talk to the fanotifier. |
| 46 | func NewClientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/NewClient().(r ioscip-go gomod github.com/golang/go/src go1.19 io/.Readerscip-go gomod github.com/golang/go/src go1.19 io/Reader#, w ioscip-go gomod github.com/golang/go/src go1.19 io/.Writerscip-go gomod github.com/golang/go/src go1.19 io/Writer#, servicePid int, timeout timescip-go gomod github.com/golang/go/src go1.19 time/.Durationscip-go gomod github.com/golang/go/src go1.19 time/Duration#) *Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client# { |
| 47 | return &Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#{r, w, bufioscip-go gomod github.com/golang/go/src go1.19 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.19 bufio/NewScanner().(r), servicePid, timeout} |
| 48 | } |
| 49 | |
| 50 | // Start lets the notirier start to monitor the filesystem. |
| 51 | func (nc *Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#) Startscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#Start().() error { |
| 52 | if err := writeMessagescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/writeMessage().(nc.wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#w., mesStartscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStart.); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if mes, err := scanWithTimeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/scanWithTimeout().(nc.notificationscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#notification., nc.timeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#timeout.); err != nil { |
| 56 | return err |
| 57 | } else if mes != mesStartedscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStarted. { |
| 58 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("non-started message got from fanotifier service: %q", mes) |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | // GetPath returns notified path from the fanotifier. This blocks until new path is notified |
| 64 | // from the service |
| 65 | func (nc *Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#) GetPathscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#GetPath().() (string, error) { |
| 66 | if !nc.notificationscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#notification..Scanscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Scan().() { // NOTE: no timeout |
| 67 | return "", ioscip-go gomod github.com/golang/go/src go1.19 io/.EOFscip-go gomod github.com/golang/go/src go1.19 io/EOF. |
| 68 | } |
| 69 | mes := nc.notificationscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#notification..Textscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Text().() |
| 70 | if !stringsscip-go gomod github.com/golang/go/src go1.19 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.19 strings/HasPrefix().(mes, mesFdPrefixscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesFdPrefix.) { |
| 71 | return "", fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("unexpected prefix for message %q", mes) |
| 72 | } |
| 73 | fd, err := strconvscip-go gomod github.com/golang/go/src go1.19 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.19 strconv/ParseInt().(mes[len(mesFdPrefixscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesFdPrefix.):], 10, 32) |
| 74 | if err != nil { |
| 75 | return "", fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("invalid fd %q: %w", mes, err) |
| 76 | } |
| 77 | path, err := osscip-go gomod github.com/golang/go/src go1.19 os/.Readlinkscip-go gomod github.com/golang/go/src go1.19 os/Readlink().(fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("/proc/%d/fd/%d", nc.servicePidscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#servicePid., fd)) |
| 78 | if err != nil { |
| 79 | return "", fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("failed to get link from fd %q: %w", mes, err) |
| 80 | } |
| 81 | return path, writeMessagescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/writeMessage().(nc.wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#w., mesAckscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesAck.) |
| 82 | } |
| 83 | |
| 84 | // Service is the service end of the fanotifier. |
| 85 | type Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service# struct { |
| 86 | rscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#r. ioscip-go gomod github.com/golang/go/src go1.19 io/.Readerscip-go gomod github.com/golang/go/src go1.19 io/Reader# |
| 87 | wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#w. ioscip-go gomod github.com/golang/go/src go1.19 io/.Writerscip-go gomod github.com/golang/go/src go1.19 io/Writer# |
| 88 | clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#client. *bufioscip-go gomod github.com/golang/go/src go1.19 bufio/.Scannerscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner# |
| 89 | timeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#timeout. timescip-go gomod github.com/golang/go/src go1.19 time/.Durationscip-go gomod github.com/golang/go/src go1.19 time/Duration# |
| 90 | } |
| 91 | |
| 92 | // NewService is the service end of the fanotifier. |
| 93 | func NewServicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/NewService().(r ioscip-go gomod github.com/golang/go/src go1.19 io/.Readerscip-go gomod github.com/golang/go/src go1.19 io/Reader#, w ioscip-go gomod github.com/golang/go/src go1.19 io/.Writerscip-go gomod github.com/golang/go/src go1.19 io/Writer#, timeout timescip-go gomod github.com/golang/go/src go1.19 time/.Durationscip-go gomod github.com/golang/go/src go1.19 time/Duration#) *Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service# { |
| 94 | return &Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#{r, w, bufioscip-go gomod github.com/golang/go/src go1.19 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.19 bufio/NewScanner().(r), timeout} |
| 95 | } |
| 96 | |
| 97 | // WaitStart waits "start" message from the client. |
| 98 | func (ns *Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#) WaitStartscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#WaitStart().() error { |
| 99 | if !ns.clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#client..Scanscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Scan().() { // NOTE: no timeout |
| 100 | return ioscip-go gomod github.com/golang/go/src go1.19 io/.EOFscip-go gomod github.com/golang/go/src go1.19 io/EOF. |
| 101 | } |
| 102 | if mes := ns.clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#client..Textscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Text().(); mes != mesStartscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStart. { |
| 103 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("non-start message got from fanotifier client: %q", mes) |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // SendStarted tells client that the service has been started. |
| 109 | func (ns *Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#) SendStartedscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#SendStarted().() error { |
| 110 | return writeMessagescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/writeMessage().(ns.wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#w., mesStartedscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesStarted.) |
| 111 | } |
| 112 | |
| 113 | // SendFd send file descriptor to the client. |
| 114 | func (ns *Servicescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#) SendFdscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#SendFd().(fd int) error { |
| 115 | if err := writeMessagescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/writeMessage().(ns.wscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#w., fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("%s%d", mesFdPrefixscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesFdPrefix., fd)); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if mes, err := scanWithTimeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/scanWithTimeout().(ns.clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#client., ns.timeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Service#timeout.); err != nil { |
| 119 | return err |
| 120 | } else if mes != mesAckscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/mesAck. { |
| 121 | return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("non-ack message got from fanotifier client: %q", mes) |
| 122 | } |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func scanWithTimeoutscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/scanWithTimeout().(sc *bufioscip-go gomod github.com/golang/go/src go1.19 bufio/.Scannerscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#, timeout timescip-go gomod github.com/golang/go/src go1.19 time/.Durationscip-go gomod github.com/golang/go/src go1.19 time/Duration#) (string, error) { |
| 127 | notifyCh := make(chan string) |
| 128 | errCh := make(chan error) |
| 129 | go func() { |
| 130 | if !sc.Scanscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Scan().() { |
| 131 | errCh <- ioscip-go gomod github.com/golang/go/src go1.19 io/.EOFscip-go gomod github.com/golang/go/src go1.19 io/EOF. |
| 132 | } |
| 133 | notifyCh <- sc.Textscip-go gomod github.com/golang/go/src go1.19 bufio/Scanner#Text().() |
| 134 | }() |
| 135 | select { |
| 136 | case mes := <-notifyCh: |
| 137 | return mes, nil |
| 138 | case err := <-errCh: |
| 139 | return "", err |
| 140 | case <-timescip-go gomod github.com/golang/go/src go1.19 time/.Afterscip-go gomod github.com/golang/go/src go1.19 time/After().(timeout): |
| 141 | return "", fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("timeout") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func writeMessagescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/writeMessage().(w ioscip-go gomod github.com/golang/go/src go1.19 io/.Writerscip-go gomod github.com/golang/go/src go1.19 io/Writer#, mes string) error { |
| 146 | _, err := ioscip-go gomod github.com/golang/go/src go1.19 io/.WriteStringscip-go gomod github.com/golang/go/src go1.19 io/WriteString().(w, mes+"\n") |
| 147 | return err |
| 148 | } |