InotifySource.cxx 3 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "InotifySource.hxx"
22
#include "InotifyDomain.hxx"
23
#include "util/Error.hxx"
24
#include "system/FileDescriptor.hxx"
25
#include "system/FatalError.hxx"
26
#include "Log.hxx"
27 28 29

#include <sys/inotify.h>
#include <unistd.h>
30
#include <errno.h>
31 32
#include <stdint.h>
#include <limits.h>
33

34
bool
35
InotifySource::OnSocketReady(gcc_unused unsigned flags)
36
{
37 38 39
	uint8_t buffer[4096];
	static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
		      "inotify buffer too small");
40

41
	ssize_t nbytes = read(Get(), buffer, sizeof(buffer));
42
	if (nbytes < 0)
43
		FatalSystemError("Failed to read from inotify");
44
	if (nbytes == 0)
45
		FatalError("end of file from inotify");
46

47
	const uint8_t *p = buffer, *const end = p + nbytes;
48 49

	while (true) {
50
		const size_t remaining = end - p;
Max Kellermann's avatar
Max Kellermann committed
51
		const struct inotify_event *event =
52 53 54
			(const struct inotify_event *)p;
		if (remaining < sizeof(*event) ||
		    remaining < sizeof(*event) + event->len)
55 56
			break;

57
		const char *name;
58 59 60
		if (event->len > 0 && event->name[event->len - 1] == 0)
			name = event->name;
		else
61
			name = nullptr;
62

63
		callback(event->wd, event->mask, name, callback_ctx);
64
		p += sizeof(*event) + event->len;
65
	}
66 67

	return true;
68
}
69

70
inline
71 72
InotifySource::InotifySource(EventLoop &_loop,
			     mpd_inotify_callback_t _callback, void *_ctx,
73 74
			     FileDescriptor _fd)
	:SocketMonitor(_fd.Get(), _loop),
75
	 callback(_callback), callback_ctx(_ctx)
76
{
77 78
	ScheduleRead();

79
}
80

81
InotifySource *
82 83
InotifySource::Create(EventLoop &loop,
		      mpd_inotify_callback_t callback, void *callback_ctx,
84
		      Error &error)
85
{
86 87
	FileDescriptor fd;
	if (!fd.CreateInotify()) {
88
		error.SetErrno("inotify_init() has failed");
89
		return nullptr;
90 91
	}

92
	return new InotifySource(loop, callback, callback_ctx, fd);
93 94 95
}

int
96
InotifySource::Add(const char *path_fs, unsigned mask, Error &error)
97
{
98
	int wd = inotify_add_watch(Get(), path_fs, mask);
99
	if (wd < 0)
100
		error.SetErrno("inotify_add_watch() has failed");
101 102 103 104 105

	return wd;
}

void
106
InotifySource::Remove(unsigned wd)
107
{
108
	int ret = inotify_rm_watch(Get(), wd);
109
	if (ret < 0 && errno != EINVAL)
110
		LogErrno(inotify_domain, "inotify_rm_watch() has failed");
111 112 113 114

	/* EINVAL may happen here when the file has been deleted; the
	   kernel seems to auto-unregister deleted files */
}