InotifySource.cxx 2.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "system/FileDescriptor.hxx"
24
#include "system/FatalError.hxx"
25
#include "system/Error.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) noexcept
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
	auto ifd = GetSocket().ToFileDescriptor();
42
	ssize_t nbytes = ifd.Read(buffer, sizeof(buffer));
43
	if (nbytes < 0)
44
		FatalSystemError("Failed to read from inotify");
45
	if (nbytes == 0)
46
		FatalError("end of file from inotify");
47

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

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

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

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

	return true;
69
}
70

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

78
	return fd;
79
}
80

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

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

	return wd;
}

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

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