podman
1// Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
2// Copyright (c) 2015 HPE Software Inc. All rights reserved.
3// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
4
5package watch6
7import (8"log"9"os"10"path/filepath"11"sync"12"syscall"13
14"github.com/nxadm/tail/util"15
16"github.com/fsnotify/fsnotify"17)
18
19type InotifyTracker struct {20mux sync.Mutex21watcher *fsnotify.Watcher22chans map[string]chan fsnotify.Event23done map[string]chan bool24watchNums map[string]int25watch chan *watchInfo26remove chan *watchInfo27error chan error28}
29
30type watchInfo struct {31op fsnotify.Op32fname string33}
34
35func (this *watchInfo) isCreate() bool {36return this.op == fsnotify.Create37}
38
39var (40// globally shared InotifyTracker; ensures only one fsnotify.Watcher is used41shared *InotifyTracker42
43// these are used to ensure the shared InotifyTracker is run exactly once44once = sync.Once{}45goRun = func() {46shared = &InotifyTracker{47mux: sync.Mutex{},48chans: make(map[string]chan fsnotify.Event),49done: make(map[string]chan bool),50watchNums: make(map[string]int),51watch: make(chan *watchInfo),52remove: make(chan *watchInfo),53error: make(chan error),54}55go shared.run()56}57
58logger = log.New(os.Stderr, "", log.LstdFlags)59)
60
61// Watch signals the run goroutine to begin watching the input filename
62func Watch(fname string) error {63return watch(&watchInfo{64fname: fname,65})66}
67
68// Watch create signals the run goroutine to begin watching the input filename
69// if call the WatchCreate function, don't call the Cleanup, call the RemoveWatchCreate
70func WatchCreate(fname string) error {71return watch(&watchInfo{72op: fsnotify.Create,73fname: fname,74})75}
76
77func watch(winfo *watchInfo) error {78// start running the shared InotifyTracker if not already running79once.Do(goRun)80
81winfo.fname = filepath.Clean(winfo.fname)82shared.watch <- winfo83return <-shared.error84}
85
86// RemoveWatch signals the run goroutine to remove the watch for the input filename
87func RemoveWatch(fname string) error {88return remove(&watchInfo{89fname: fname,90})91}
92
93// RemoveWatch create signals the run goroutine to remove the watch for the input filename
94func RemoveWatchCreate(fname string) error {95return remove(&watchInfo{96op: fsnotify.Create,97fname: fname,98})99}
100
101func remove(winfo *watchInfo) error {102// start running the shared InotifyTracker if not already running103once.Do(goRun)104
105winfo.fname = filepath.Clean(winfo.fname)106shared.mux.Lock()107done := shared.done[winfo.fname]108if done != nil {109delete(shared.done, winfo.fname)110close(done)111}112shared.mux.Unlock()113
114shared.remove <- winfo115return <-shared.error116}
117
118// Events returns a channel to which FileEvents corresponding to the input filename
119// will be sent. This channel will be closed when removeWatch is called on this
120// filename.
121func Events(fname string) <-chan fsnotify.Event {122shared.mux.Lock()123defer shared.mux.Unlock()124
125return shared.chans[fname]126}
127
128// Cleanup removes the watch for the input filename if necessary.
129func Cleanup(fname string) error {130return RemoveWatch(fname)131}
132
133// watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating
134// a new Watcher if the previous Watcher was closed.
135func (shared *InotifyTracker) addWatch(winfo *watchInfo) error {136shared.mux.Lock()137defer shared.mux.Unlock()138
139if shared.chans[winfo.fname] == nil {140shared.chans[winfo.fname] = make(chan fsnotify.Event)141}142if shared.done[winfo.fname] == nil {143shared.done[winfo.fname] = make(chan bool)144}145
146fname := winfo.fname147if winfo.isCreate() {148// Watch for new files to be created in the parent directory.149fname = filepath.Dir(fname)150}151
152var err error153// already in inotify watch154if shared.watchNums[fname] == 0 {155err = shared.watcher.Add(fname)156}157if err == nil {158shared.watchNums[fname]++159}160return err161}
162
163// removeWatch calls fsnotify.RemoveWatch for the input filename and closes the
164// corresponding events channel.
165func (shared *InotifyTracker) removeWatch(winfo *watchInfo) error {166shared.mux.Lock()167
168ch := shared.chans[winfo.fname]169if ch != nil {170delete(shared.chans, winfo.fname)171close(ch)172}173
174fname := winfo.fname175if winfo.isCreate() {176// Watch for new files to be created in the parent directory.177fname = filepath.Dir(fname)178}179shared.watchNums[fname]--180watchNum := shared.watchNums[fname]181if watchNum == 0 {182delete(shared.watchNums, fname)183}184shared.mux.Unlock()185
186var err error187// If we were the last ones to watch this file, unsubscribe from inotify.188// This needs to happen after releasing the lock because fsnotify waits189// synchronously for the kernel to acknowledge the removal of the watch190// for this file, which causes us to deadlock if we still held the lock.191if watchNum == 0 {192err = shared.watcher.Remove(fname)193}194
195return err196}
197
198// sendEvent sends the input event to the appropriate Tail.
199func (shared *InotifyTracker) sendEvent(event fsnotify.Event) {200name := filepath.Clean(event.Name)201
202shared.mux.Lock()203ch := shared.chans[name]204done := shared.done[name]205shared.mux.Unlock()206
207if ch != nil && done != nil {208select {209case ch <- event:210case <-done:211}212}213}
214
215// run starts the goroutine in which the shared struct reads events from its
216// Watcher's Event channel and sends the events to the appropriate Tail.
217func (shared *InotifyTracker) run() {218watcher, err := fsnotify.NewWatcher()219if err != nil {220util.Fatal("failed to create Watcher")221}222shared.watcher = watcher223
224for {225select {226case winfo := <-shared.watch:227shared.error <- shared.addWatch(winfo)228
229case winfo := <-shared.remove:230shared.error <- shared.removeWatch(winfo)231
232case event, open := <-shared.watcher.Events:233if !open {234return235}236shared.sendEvent(event)237
238case err, open := <-shared.watcher.Errors:239if !open {240return241} else if err != nil {242sysErr, ok := err.(*os.SyscallError)243if !ok || sysErr.Err != syscall.EINTR {244logger.Printf("Error in Watcher Error channel: %s", err)245}246}247}248}249}
250