2009-09-25 18:32:00 +02:00
|
|
|
/*
|
2013-01-02 19:22:15 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-09-25 18:32:00 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 19:22:15 +01:00
|
|
|
#include "InotifySource.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "InotifyDomain.hxx"
|
2013-01-15 01:00:59 +01:00
|
|
|
#include "util/fifo_buffer.h"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
|
|
|
#include "system/FatalError.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2012-08-02 18:20:46 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2009-09-25 18:32:00 +02:00
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-01-30 10:53:32 +01:00
|
|
|
bool
|
2013-01-15 18:18:02 +01:00
|
|
|
InotifySource::OnSocketReady(gcc_unused unsigned flags)
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
|
|
|
void *dest;
|
|
|
|
size_t length;
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
dest = fifo_buffer_write(buffer, &length);
|
2009-09-25 18:32:00 +02:00
|
|
|
if (dest == NULL)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError("buffer full");
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-15 18:18:02 +01:00
|
|
|
nbytes = read(Get(), dest, length);
|
2009-09-25 18:32:00 +02:00
|
|
|
if (nbytes < 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalSystemError("Failed to read from inotify");
|
2009-09-25 18:32:00 +02:00
|
|
|
if (nbytes == 0)
|
2013-08-07 09:35:30 +02:00
|
|
|
FatalError("end of file from inotify");
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
fifo_buffer_append(buffer, nbytes);
|
2009-09-25 18:32:00 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const char *name;
|
|
|
|
|
2013-01-02 19:22:15 +01:00
|
|
|
const struct inotify_event *event =
|
|
|
|
(const struct inotify_event *)
|
2013-01-14 09:52:35 +01:00
|
|
|
fifo_buffer_read(buffer, &length);
|
2009-09-25 18:32:00 +02:00
|
|
|
if (event == NULL || length < sizeof(*event) ||
|
|
|
|
length < sizeof(*event) + event->len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (event->len > 0 && event->name[event->len - 1] == 0)
|
|
|
|
name = event->name;
|
|
|
|
else
|
|
|
|
name = NULL;
|
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
callback(event->wd, event->mask, name, callback_ctx);
|
|
|
|
fifo_buffer_consume(buffer, sizeof(*event) + event->len);
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
2013-01-30 10:53:32 +01:00
|
|
|
|
|
|
|
return true;
|
2013-01-14 09:52:35 +01:00
|
|
|
}
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
inline
|
2013-01-15 18:18:02 +01:00
|
|
|
InotifySource::InotifySource(EventLoop &_loop,
|
|
|
|
mpd_inotify_callback_t _callback, void *_ctx,
|
2013-01-14 09:52:35 +01:00
|
|
|
int _fd)
|
2013-01-15 18:18:02 +01:00
|
|
|
:SocketMonitor(_fd, _loop),
|
|
|
|
callback(_callback), callback_ctx(_ctx),
|
2013-01-14 09:52:35 +01:00
|
|
|
buffer(fifo_buffer_new(4096))
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2013-01-15 18:18:02 +01:00
|
|
|
ScheduleRead();
|
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
}
|
2009-09-25 18:32:00 +02:00
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
InotifySource *
|
2013-01-15 18:18:02 +01:00
|
|
|
InotifySource::Create(EventLoop &loop,
|
|
|
|
mpd_inotify_callback_t callback, void *callback_ctx,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2013-01-14 09:52:35 +01:00
|
|
|
{
|
|
|
|
int fd = inotify_init_cloexec();
|
|
|
|
if (fd < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("inotify_init() has failed");
|
2009-09-25 18:32:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-15 18:18:02 +01:00
|
|
|
return new InotifySource(loop, callback, callback_ctx, fd);
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
|
|
|
|
2013-01-14 09:52:35 +01:00
|
|
|
InotifySource::~InotifySource()
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2013-01-14 09:52:35 +01:00
|
|
|
fifo_buffer_free(buffer);
|
2009-09-25 18:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-08-10 18:02:44 +02:00
|
|
|
InotifySource::Add(const char *path_fs, unsigned mask, Error &error)
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2013-01-15 18:18:02 +01:00
|
|
|
int wd = inotify_add_watch(Get(), path_fs, mask);
|
2009-09-25 18:32:00 +02:00
|
|
|
if (wd < 0)
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("inotify_add_watch() has failed");
|
2009-09-25 18:32:00 +02:00
|
|
|
|
|
|
|
return wd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-14 09:52:35 +01:00
|
|
|
InotifySource::Remove(unsigned wd)
|
2009-09-25 18:32:00 +02:00
|
|
|
{
|
2013-01-15 18:18:02 +01:00
|
|
|
int ret = inotify_rm_watch(Get(), wd);
|
2009-09-25 18:32:00 +02:00
|
|
|
if (ret < 0 && errno != EINVAL)
|
2013-09-27 22:31:24 +02:00
|
|
|
LogErrno(inotify_domain, "inotify_rm_watch() has failed");
|
2009-09-25 18:32:00 +02:00
|
|
|
|
|
|
|
/* EINVAL may happen here when the file has been deleted; the
|
|
|
|
kernel seems to auto-unregister deleted files */
|
|
|
|
}
|