1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build linux
6
7package fsnotify
8
9import (
10	"errors"
11	"fmt"
12	"io"
13	"os"
14	"path/filepath"
15	"strings"
16	"sync"
17	"unsafe"
18
19	"golang.org/x/sys/unix"
20)
21
22// Watcher watches a set of files, delivering events to a channel.
23type Watcher struct {
24	Events   chan Event
25	Errors   chan error
26	mu       sync.Mutex // Map access
27	cv       *sync.Cond // sync removing on rm_watch with IN_IGNORE
28	fd       int
29	poller   *fdPoller
30	watches  map[string]*watch // Map of inotify watches (key: path)
31	paths    map[int]string    // Map of watched paths (key: watch descriptor)
32	done     chan struct{}     // Channel for sending a "quit message" to the reader goroutine
33	doneResp chan struct{}     // Channel to respond to Close
34}
35
36// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
37func NewWatcher() (*Watcher, error) {
38	// Create inotify fd
39	fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
40	if fd == -1 {
41		return nil, errno
42	}
43	// Create epoll
44	poller, err := newFdPoller(fd)
45	if err != nil {
46		unix.Close(fd)
47		return nil, err
48	}
49	w := &Watcher{
50		fd:       fd,
51		poller:   poller,
52		watches:  make(map[string]*watch),
53		paths:    make(map[int]string),
54		Events:   make(chan Event),
55		Errors:   make(chan error),
56		done:     make(chan struct{}),
57		doneResp: make(chan struct{}),
58	}
59	w.cv = sync.NewCond(&w.mu)
60
61	go w.readEvents()
62	return w, nil
63}
64
65func (w *Watcher) isClosed() bool {
66	select {
67	case <-w.done:
68		return true
69	default:
70		return false
71	}
72}
73
74// Close removes all watches and closes the events channel.
75func (w *Watcher) Close() error {
76	if w.isClosed() {
77		return nil
78	}
79
80	// Send 'close' signal to goroutine, and set the Watcher to closed.
81	close(w.done)
82
83	// Wake up goroutine
84	w.poller.wake()
85
86	// Wait for goroutine to close
87	<-w.doneResp
88
89	return nil
90}
91
92// Add starts watching the named file or directory (non-recursively).
93func (w *Watcher) Add(name string) error {
94	name = filepath.Clean(name)
95	if w.isClosed() {
96		return errors.New("inotify instance already closed")
97	}
98
99	const agnosticEvents = unix.IN_MOVED_TO | unix.IN_MOVED_FROM |
100		unix.IN_CREATE | unix.IN_ATTRIB | unix.IN_MODIFY |
101		unix.IN_MOVE_SELF | unix.IN_DELETE | unix.IN_DELETE_SELF
102
103	var flags uint32 = agnosticEvents
104
105	w.mu.Lock()
106	watchEntry, found := w.watches[name]
107	w.mu.Unlock()
108	if found {
109		watchEntry.flags |= flags
110		flags |= unix.IN_MASK_ADD
111	}
112	wd, errno := unix.InotifyAddWatch(w.fd, name, flags)
113	if wd == -1 {
114		return errno
115	}
116
117	w.mu.Lock()
118	w.watches[name] = &watch{wd: uint32(wd), flags: flags}
119	w.paths[wd] = name
120	w.mu.Unlock()
121
122	return nil
123}
124
125// Remove stops watching the named file or directory (non-recursively).
126func (w *Watcher) Remove(name string) error {
127	name = filepath.Clean(name)
128
129	// Fetch the watch.
130	w.mu.Lock()
131	defer w.mu.Unlock()
132	watch, ok := w.watches[name]
133
134	// Remove it from inotify.
135	if !ok {
136		return fmt.Errorf("can't remove non-existent inotify watch for: %s", name)
137	}
138	// inotify_rm_watch will return EINVAL if the file has been deleted;
139	// the inotify will already have been removed.
140	// watches and pathes are deleted in ignoreLinux() implicitly and asynchronously
141	// by calling inotify_rm_watch() below. e.g. readEvents() goroutine receives IN_IGNORE
142	// so that EINVAL means that the wd is being rm_watch()ed or its file removed
143	// by another thread and we have not received IN_IGNORE event.
144	success, errno := unix.InotifyRmWatch(w.fd, watch.wd)
145	if success == -1 {
146		// TODO: Perhaps it's not helpful to return an error here in every case.
147		// the only two possible errors are:
148		// EBADF, which happens when w.fd is not a valid file descriptor of any kind.
149		// EINVAL, which is when fd is not an inotify descriptor or wd is not a valid watch descriptor.
150		// Watch descriptors are invalidated when they are removed explicitly or implicitly;
151		// explicitly by inotify_rm_watch, implicitly when the file they are watching is deleted.
152		return errno
153	}
154
155	// wait until ignoreLinux() deleting maps
156	exists := true
157	for exists {
158		w.cv.Wait()
159		_, exists = w.watches[name]
160	}
161
162	return nil
163}
164
165type watch struct {
166	wd    uint32 // Watch descriptor (as returned by the inotify_add_watch() syscall)
167	flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)
168}
169
170// readEvents reads from the inotify file descriptor, converts the
171// received events into Event objects and sends them via the Events channel
172func (w *Watcher) readEvents() {
173	var (
174		buf   [unix.SizeofInotifyEvent * 4096]byte // Buffer for a maximum of 4096 raw events
175		n     int                                  // Number of bytes read with read()
176		errno error                                // Syscall errno
177		ok    bool                                 // For poller.wait
178	)
179
180	defer close(w.doneResp)
181	defer close(w.Errors)
182	defer close(w.Events)
183	defer unix.Close(w.fd)
184	defer w.poller.close()
185
186	for {
187		// See if we have been closed.
188		if w.isClosed() {
189			return
190		}
191
192		ok, errno = w.poller.wait()
193		if errno != nil {
194			select {
195			case w.Errors <- errno:
196			case <-w.done:
197				return
198			}
199			continue
200		}
201
202		if !ok {
203			continue
204		}
205
206		n, errno = unix.Read(w.fd, buf[:])
207		// If a signal interrupted execution, see if we've been asked to close, and try again.
208		// http://man7.org/linux/man-pages/man7/signal.7.html :
209		// "Before Linux 3.8, reads from an inotify(7) file descriptor were not restartable"
210		if errno == unix.EINTR {
211			continue
212		}
213
214		// unix.Read might have been woken up by Close. If so, we're done.
215		if w.isClosed() {
216			return
217		}
218
219		if n < unix.SizeofInotifyEvent {
220			var err error
221			if n == 0 {
222				// If EOF is received. This should really never happen.
223				err = io.EOF
224			} else if n < 0 {
225				// If an error occurred while reading.
226				err = errno
227			} else {
228				// Read was too short.
229				err = errors.New("notify: short read in readEvents()")
230			}
231			select {
232			case w.Errors <- err:
233			case <-w.done:
234				return
235			}
236			continue
237		}
238
239		var offset uint32
240		// We don't know how many events we just read into the buffer
241		// While the offset points to at least one whole event...
242		for offset <= uint32(n-unix.SizeofInotifyEvent) {
243			// Point "raw" to the event in the buffer
244			raw := (*unix.InotifyEvent)(unsafe.Pointer(&buf[offset]))
245
246			mask := uint32(raw.Mask)
247			nameLen := uint32(raw.Len)
248			// If the event happened to the watched directory or the watched file, the kernel
249			// doesn't append the filename to the event, but we would like to always fill the
250			// the "Name" field with a valid filename. We retrieve the path of the watch from
251			// the "paths" map.
252			w.mu.Lock()
253			name := w.paths[int(raw.Wd)]
254			w.mu.Unlock()
255			if nameLen > 0 {
256				// Point "bytes" at the first byte of the filename
257				bytes := (*[unix.PathMax]byte)(unsafe.Pointer(&buf[offset+unix.SizeofInotifyEvent]))
258				// The filename is padded with NULL bytes. TrimRight() gets rid of those.
259				name += "/" + strings.TrimRight(string(bytes[0:nameLen]), "\000")
260			}
261
262			event := newEvent(name, mask)
263
264			// Send the events that are not ignored on the events channel
265			if !event.ignoreLinux(w, raw.Wd, mask) {
266				select {
267				case w.Events <- event:
268				case <-w.done:
269					return
270				}
271			}
272
273			// Move to the next event in the buffer
274			offset += unix.SizeofInotifyEvent + nameLen
275		}
276	}
277}
278
279// Certain types of events can be "ignored" and not sent over the Events
280// channel. Such as events marked ignore by the kernel, or MODIFY events
281// against files that do not exist.
282func (e *Event) ignoreLinux(w *Watcher, wd int32, mask uint32) bool {
283	// Ignore anything the inotify API says to ignore
284	if mask&unix.IN_IGNORED == unix.IN_IGNORED {
285		w.mu.Lock()
286		defer w.mu.Unlock()
287		name := w.paths[int(wd)]
288		delete(w.paths, int(wd))
289		delete(w.watches, name)
290		w.cv.Broadcast()
291		return true
292	}
293
294	// If the event is not a DELETE or RENAME, the file must exist.
295	// Otherwise the event is ignored.
296	// *Note*: this was put in place because it was seen that a MODIFY
297	// event was sent after the DELETE. This ignores that MODIFY and
298	// assumes a DELETE will come or has come if the file doesn't exist.
299	if !(e.Op&Remove == Remove || e.Op&Rename == Rename) {
300		_, statErr := os.Lstat(e.Name)
301		return os.IsNotExist(statErr)
302	}
303	return false
304}
305
306// newEvent returns an platform-independent Event based on an inotify mask.
307func newEvent(name string, mask uint32) Event {
308	e := Event{Name: name}
309	if mask&unix.IN_CREATE == unix.IN_CREATE || mask&unix.IN_MOVED_TO == unix.IN_MOVED_TO {
310		e.Op |= Create
311	}
312	if mask&unix.IN_DELETE_SELF == unix.IN_DELETE_SELF || mask&unix.IN_DELETE == unix.IN_DELETE {
313		e.Op |= Remove
314	}
315	if mask&unix.IN_MODIFY == unix.IN_MODIFY {
316		e.Op |= Write
317	}
318	if mask&unix.IN_MOVE_SELF == unix.IN_MOVE_SELF || mask&unix.IN_MOVED_FROM == unix.IN_MOVED_FROM {
319		e.Op |= Rename
320	}
321	if mask&unix.IN_ATTRIB == unix.IN_ATTRIB {
322		e.Op |= Chmod
323	}
324	return e
325}
326