| 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 fanotifyscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 21 | "os/execscip-go gomod github.com/golang/go/src go1.19 `os/exec`/" |
| 22 | "syncscip-go gomod github.com/golang/go/src go1.19 sync/" |
| 23 | "syscallscip-go gomod github.com/golang/go/src go1.19 syscall/" |
| 24 | "timescip-go gomod github.com/golang/go/src go1.19 time/" |
| 25 | |
| 26 | "github.com/containerd/stargz-snapshotter/analyzer/fanotify/connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/" |
| 27 | "github.com/hashicorp/go-multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/" |
| 28 | ) |
| 29 | |
| 30 | // Fanotifier monitors "/" mountpoint of a new mount namespace and notifies all |
| 31 | // accessed files. |
| 32 | type Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier# struct { |
| 33 | cmdscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#cmd. *execscip-go gomod github.com/golang/go/src go1.19 `os/exec`/.Cmdscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd# |
| 34 | connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#conn. *connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/.Clientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client# |
| 35 | closeOncescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#closeOnce. syncscip-go gomod github.com/golang/go/src go1.19 sync/.Oncescip-go gomod github.com/golang/go/src go1.19 sync/Once# |
| 36 | closeFuncscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#closeFunc. func() error |
| 37 | } |
| 38 | |
| 39 | func SpawnFanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/SpawnFanotifier().(fanotifierBin string) (*Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#, error) { |
| 40 | // Run fanotifier that monitor "/" in a new mount namespace |
| 41 | cmd := execscip-go gomod github.com/golang/go/src go1.19 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Command().(fanotifierBin, "fanotify", "/") |
| 42 | cmd.SysProcAttrscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#SysProcAttr. = &syscallscip-go gomod github.com/golang/go/src go1.19 syscall/.SysProcAttrscip-go gomod github.com/golang/go/src go1.19 syscall/SysProcAttr#{ |
| 43 | Cloneflagsscip-go gomod github.com/golang/go/src go1.19 syscall/SysProcAttr#Cloneflags.: syscallscip-go gomod github.com/golang/go/src go1.19 syscall/.CLONE_NEWNSscip-go gomod github.com/golang/go/src go1.19 syscall/CLONE_NEWNS., |
| 44 | } |
| 45 | notifyR, err := cmd.StdoutPipescip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#StdoutPipe().() |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | notifyW, err := cmd.StdinPipescip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#StdinPipe().() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | if err := cmd.Startscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#Start().(); err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
| 57 | return &Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#{ |
| 58 | cmdscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#cmd.: cmd, |
| 59 | |
| 60 | // Connect to the spawned fanotifier over stdio |
| 61 | connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#conn.: connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/.NewClientscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/NewClient().(notifyR, notifyW, cmd.Processscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#Process..Pidscip-go gomod github.com/golang/go/src go1.19 os/Process#Pid., 5*timescip-go gomod github.com/golang/go/src go1.19 time/.Secondscip-go gomod github.com/golang/go/src go1.19 time/Second.), |
| 62 | closeFuncscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#closeFunc.: func() (allErr error) { |
| 63 | if err := notifyR.Closescip-go gomod github.com/golang/go/src go1.19 io/Closer#Close().(); err != nil { |
| 64 | allErr = multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/.Appendscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/Append().(allErr, err) |
| 65 | } |
| 66 | if err := notifyW.Closescip-go gomod github.com/golang/go/src go1.19 io/Closer#Close().(); err != nil { |
| 67 | allErr = multierrorscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/.Appendscip-go gomod github.com/hashicorp/go-multierror v1.1.1 `github.com/hashicorp/go-multierror`/Append().(allErr, err) |
| 68 | } |
| 69 | return |
| 70 | }, |
| 71 | }, nil |
| 72 | } |
| 73 | |
| 74 | // Start starts fanotifier in the new mount namespace |
| 75 | func (n *Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#) Startscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#Start().() error { |
| 76 | return n.connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#conn..Startscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#Start().() |
| 77 | } |
| 78 | |
| 79 | // GetPath gets path notified by the fanotifier. |
| 80 | func (n *Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#) GetPathscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#GetPath().() (string, error) { |
| 81 | return n.connscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#conn..GetPathscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn`/Client#GetPath().() |
| 82 | } |
| 83 | |
| 84 | // MountNamespacePath returns the path to the mount namespace where |
| 85 | // the fanotifier is monitoring. |
| 86 | func (n *Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#) MountNamespacePathscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#MountNamespacePath().() string { |
| 87 | return 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/ns/mnt", n.cmdscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#cmd..Processscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#Process..Pidscip-go gomod github.com/golang/go/src go1.19 os/Process#Pid.) |
| 88 | } |
| 89 | |
| 90 | // Close kills fanotifier process and closes the connection. |
| 91 | func (n *Fanotifierscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#) Closescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#Close().() (err error) { |
| 92 | n.closeOncescip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#closeOnce..Doscip-go gomod github.com/golang/go/src go1.19 sync/Once#Do().(func() { |
| 93 | if err = n.cmdscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#cmd..Processscip-go gomod github.com/golang/go/src go1.19 `os/exec`/Cmd#Process..Killscip-go gomod github.com/golang/go/src go1.19 os/Process#Kill().(); err != nil { |
| 94 | return |
| 95 | } |
| 96 | err = n.closeFuncscip-go gomod github.com/containerd/stargz-snapshotter . `github.com/containerd/stargz-snapshotter/analyzer/fanotify`/Fanotifier#closeFunc.() |
| 97 | }) |
| 98 | return |
| 99 | } |