InotifySource.cxx 2.94 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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.
 */

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

#include <sys/inotify.h>
28
#include <errno.h>
29 30
#include <stdint.h>
#include <limits.h>
31

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

39
	auto ifd = GetSocket().ToFileDescriptor();
40
	ssize_t nbytes = ifd.Read(buffer, sizeof(buffer));
41
	if (nbytes < 0)
42
		FatalSystemError("Failed to read from inotify");
43
	if (nbytes == 0)
44
		FatalError("end of file from inotify");
45

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

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

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

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

	return true;
67
}
68

69
static FileDescriptor
70
InotifyInit()
71
{
72 73 74
	FileDescriptor fd;
	if (!fd.CreateInotify())
		throw MakeErrno("inotify_init() has failed");
75

76
	return fd;
77
}
78

79 80
InotifySource::InotifySource(EventLoop &_loop,
			     mpd_inotify_callback_t _callback, void *_ctx)
81 82
	:SocketMonitor(SocketDescriptor::FromFileDescriptor(InotifyInit()),
		       _loop),
83
	 callback(_callback), callback_ctx(_ctx)
84
{
85
	ScheduleRead();
86 87 88
}

int
89
InotifySource::Add(const char *path_fs, unsigned mask)
90
{
91
	auto ifd = GetSocket().ToFileDescriptor();
92
	int wd = inotify_add_watch(ifd.Get(), path_fs, mask);
93
	if (wd < 0)
94
		throw MakeErrno("inotify_add_watch() has failed");
95 96 97 98 99

	return wd;
}

void
100
InotifySource::Remove(unsigned wd) noexcept
101
{
102
	auto ifd = GetSocket().ToFileDescriptor();
103
	int ret = inotify_rm_watch(ifd.Get(), wd);
104
	if (ret < 0 && errno != EINVAL)
105
		LogErrno(inotify_domain, "inotify_rm_watch() has failed");
106 107 108 109

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