InotifySource.cxx 3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 25
#include "system/fd_util.h"
#include "system/FatalError.hxx"
26
#include "Log.hxx"
27

28 29
#include <algorithm>

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

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

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

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

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

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

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

	return true;
70
}
71

72
inline
73 74
InotifySource::InotifySource(EventLoop &_loop,
			     mpd_inotify_callback_t _callback, void *_ctx,
75
			     int _fd)
76
	:SocketMonitor(_fd, _loop),
77
	 callback(_callback), callback_ctx(_ctx)
78
{
79 80
	ScheduleRead();

81
}
82

83
InotifySource *
84 85
InotifySource::Create(EventLoop &loop,
		      mpd_inotify_callback_t callback, void *callback_ctx,
86
		      Error &error)
87 88 89
{
	int fd = inotify_init_cloexec();
	if (fd < 0) {
90
		error.SetErrno("inotify_init() has failed");
91
		return nullptr;
92 93
	}

94
	return new InotifySource(loop, callback, callback_ctx, fd);
95 96 97
}

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

	return wd;
}

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

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