event/InotifyEvent: new class wrapping inotify
Replaces class InotifySource.
This commit is contained in:
129
src/event/InotifyEvent.cxx
Normal file
129
src/event/InotifyEvent.cxx
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2022 CM4all GmbH
|
||||
* All rights reserved.
|
||||
*
|
||||
* author: Max Kellermann <mk@cm4all.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "InotifyEvent.hxx"
|
||||
#include "system/Error.hxx"
|
||||
#include "io/UniqueFileDescriptor.hxx"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <limits.h>
|
||||
#include <sys/inotify.h>
|
||||
|
||||
static UniqueFileDescriptor
|
||||
CreateInotify()
|
||||
{
|
||||
int fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||
if (fd < 0)
|
||||
throw MakeErrno("inotify_init1() failed");
|
||||
|
||||
return UniqueFileDescriptor(fd);
|
||||
}
|
||||
|
||||
InotifyEvent::InotifyEvent(EventLoop &event_loop, InotifyHandler &_handler)
|
||||
:event(event_loop, BIND_THIS_METHOD(OnInotifyReady),
|
||||
CreateInotify().Release()),
|
||||
handler(_handler)
|
||||
{
|
||||
Enable();
|
||||
}
|
||||
|
||||
InotifyEvent::~InotifyEvent() noexcept
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
int
|
||||
InotifyEvent::AddWatch(const char *pathname, uint32_t mask)
|
||||
{
|
||||
int wd = inotify_add_watch(event.GetFileDescriptor().Get(),
|
||||
pathname, mask);
|
||||
if (wd < 0)
|
||||
throw FormatErrno("inotify_add_watch('%s') failed", pathname);
|
||||
|
||||
return wd;
|
||||
}
|
||||
|
||||
int
|
||||
InotifyEvent::AddModifyWatch(const char *pathname)
|
||||
{
|
||||
return AddWatch(pathname, IN_MODIFY);
|
||||
}
|
||||
|
||||
void
|
||||
InotifyEvent::RemoveWatch(int wd) noexcept
|
||||
{
|
||||
inotify_rm_watch(event.GetFileDescriptor().Get(), wd);
|
||||
}
|
||||
|
||||
inline void
|
||||
InotifyEvent::OnInotifyReady(unsigned) noexcept
|
||||
try {
|
||||
std::array<std::byte, 4096> buffer;
|
||||
static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
|
||||
"inotify buffer too small");
|
||||
|
||||
ssize_t nbytes = event.GetFileDescriptor().Read(buffer.data(),
|
||||
buffer.size());
|
||||
if (nbytes <= 0) [[unlikely]] {
|
||||
if (nbytes == 0)
|
||||
throw std::runtime_error{"EOF from inotify"};
|
||||
|
||||
const int e = errno;
|
||||
if (e == EAGAIN)
|
||||
return;
|
||||
|
||||
throw MakeErrno(e, "Reading inotify failed");
|
||||
}
|
||||
|
||||
const std::byte *p = buffer.data(), *const end = p + nbytes;
|
||||
|
||||
while (true) {
|
||||
const size_t remaining = end - p;
|
||||
const auto &ie = *(const struct inotify_event *)(const void *)p;
|
||||
if (remaining < sizeof(ie) ||
|
||||
remaining < sizeof(ie) + ie.len)
|
||||
break;
|
||||
|
||||
const char *name;
|
||||
if (ie.len > 0 && ie.name[ie.len - 1] == 0)
|
||||
name = ie.name;
|
||||
else
|
||||
name = nullptr;
|
||||
|
||||
handler.OnInotify(ie.wd, ie.mask, name);
|
||||
p += sizeof(ie) + ie.len;
|
||||
}
|
||||
} catch (...) {
|
||||
Close();
|
||||
handler.OnInotifyError(std::current_exception());
|
||||
}
|
||||
129
src/event/InotifyEvent.hxx
Normal file
129
src/event/InotifyEvent.hxx
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2022 CM4all GmbH
|
||||
* All rights reserved.
|
||||
*
|
||||
* author: Max Kellermann <mk@cm4all.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "PipeEvent.hxx"
|
||||
|
||||
#include <exception>
|
||||
|
||||
/**
|
||||
* Handler for #InotifyEvent.
|
||||
*/
|
||||
class InotifyHandler {
|
||||
public:
|
||||
/**
|
||||
* An inotify event was received.
|
||||
*
|
||||
* @param wd the watch descriptor returned by
|
||||
* InotifyEvent::AddWatch().
|
||||
*/
|
||||
virtual void OnInotify(int wd, unsigned mask, const char *name) = 0;
|
||||
|
||||
/**
|
||||
* An (permanent) inotify error has occurred, and the
|
||||
* #InotifyEvent has been closed.
|
||||
*/
|
||||
virtual void OnInotifyError(std::exception_ptr error) noexcept = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* #EventLoop integration for Linux inotify.
|
||||
*/
|
||||
class InotifyEvent final {
|
||||
PipeEvent event;
|
||||
|
||||
InotifyHandler &handler;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create an inotify file descriptor add register it in the
|
||||
* #EventLoop.
|
||||
*
|
||||
* Throws on error.
|
||||
*/
|
||||
InotifyEvent(EventLoop &event_loop, InotifyHandler &_handler);
|
||||
|
||||
~InotifyEvent() noexcept;
|
||||
|
||||
EventLoop &GetEventLoop() const noexcept {
|
||||
return event.GetEventLoop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enable polling the inotify file descriptor after it was
|
||||
* disabled by Disable().
|
||||
*/
|
||||
void Enable() noexcept {
|
||||
event.ScheduleRead();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable polling the inotify file descriptor. Can be
|
||||
* re-enabled by Enable().
|
||||
*/
|
||||
void Disable() noexcept {
|
||||
event.Cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permanently close the inotify file descriptor. Further
|
||||
* method calls not allowed after that.
|
||||
*/
|
||||
void Close() noexcept {
|
||||
event.Close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new path to be watched.
|
||||
*
|
||||
* Throws on error.
|
||||
*
|
||||
* @return a watch descriptor
|
||||
*/
|
||||
int AddWatch(const char *pathname, uint32_t mask);
|
||||
|
||||
/**
|
||||
* Wrapper for AddWatch(pathname, IN_MODIFY).
|
||||
*/
|
||||
int AddModifyWatch(const char *pathname);
|
||||
|
||||
/**
|
||||
* Stop watching the given watch descriptor.
|
||||
*
|
||||
* @param wd a watch descriptor returned by AddWatch()
|
||||
*/
|
||||
void RemoveWatch(int wd) noexcept;
|
||||
|
||||
private:
|
||||
void OnInotifyReady(unsigned) noexcept;
|
||||
};
|
||||
@@ -19,6 +19,10 @@ else
|
||||
event_sources += 'PollBackend.cxx'
|
||||
endif
|
||||
|
||||
if enable_inotify
|
||||
event_sources += 'InotifyEvent.cxx'
|
||||
endif
|
||||
|
||||
event = static_library(
|
||||
'event',
|
||||
'SignalMonitor.cxx',
|
||||
|
||||
Reference in New Issue
Block a user