Fix inotify
We don't need the global variable here. Cleanup is handled by the
stream_descriptor. Also check error codes per the documentation, not
just on == -1
Tested:
Created log file manually with redfish-event-listener enabled
Saw log was sent to consumer.
Change-Id: I27f13c7aedfdfe642128b7129f622047dd933380
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/redfish-core/include/filesystem_log_watcher.hpp b/redfish-core/include/filesystem_log_watcher.hpp
index b89c9d2..2e82a61 100644
--- a/redfish-core/include/filesystem_log_watcher.hpp
+++ b/redfish-core/include/filesystem_log_watcher.hpp
@@ -1,7 +1,5 @@
#pragma once
-#include <sys/inotify.h>
-
#include <boost/asio/posix/stream_descriptor.hpp>
#include <optional>
@@ -16,10 +14,8 @@
private:
std::streampos redfishLogFilePosition{0};
- int inotifyFd = -1;
int dirWatchDesc = -1;
int fileWatchDesc = -1;
- boost::asio::posix::stream_descriptor inotifyConn;
void onINotify(const boost::system::error_code& ec,
std::size_t bytesTransferred);
@@ -32,6 +28,9 @@
void cacheRedfishLogFile();
std::array<char, 1024> readBuffer{};
+ // Explicit make the last item so it is canceled before the buffer goes out
+ // of scope.
+ boost::asio::posix::stream_descriptor inotifyConn;
public:
explicit FilesystemLogWatcher(boost::asio::io_context& iocIn);
diff --git a/redfish-core/src/filesystem_log_watcher.cpp b/redfish-core/src/filesystem_log_watcher.cpp
index 0bff10f..af4a241 100644
--- a/redfish-core/src/filesystem_log_watcher.cpp
+++ b/redfish-core/src/filesystem_log_watcher.cpp
@@ -153,12 +153,14 @@
"redfish event log file");
// Remove existing inotify watcher and add
// with new redfish event log file.
- inotify_rm_watch(inotifyFd, fileWatchDesc);
+ inotify_rm_watch(inotifyConn.native_handle(),
+ fileWatchDesc);
fileWatchDesc = -1;
}
- fileWatchDesc = inotify_add_watch(
- inotifyFd, redfishEventLogFile, IN_MODIFY);
+ fileWatchDesc =
+ inotify_add_watch(inotifyConn.native_handle(),
+ redfishEventLogFile, IN_MODIFY);
if (fileWatchDesc == -1)
{
BMCWEB_LOG_ERROR("inotify_add_watch failed for "
@@ -173,7 +175,8 @@
{
if (fileWatchDesc != -1)
{
- inotify_rm_watch(inotifyFd, fileWatchDesc);
+ inotify_rm_watch(inotifyConn.native_handle(),
+ fileWatchDesc);
fileWatchDesc = -1;
}
}
@@ -199,30 +202,39 @@
}
FilesystemLogWatcher::FilesystemLogWatcher(boost::asio::io_context& ioc) :
- inotifyFd(inotify_init1(IN_NONBLOCK)), inotifyConn(ioc)
+ inotifyConn(ioc)
{
BMCWEB_LOG_DEBUG("starting Event Log Monitor");
- if (inotifyFd == -1)
+ int inotifyFd = inotify_init1(IN_NONBLOCK);
+ if (inotifyFd < 0)
{
BMCWEB_LOG_ERROR("inotify_init1 failed.");
return;
}
+ boost::system::error_code ec;
+ inotifyConn.assign(inotifyFd, ec);
+ if (ec)
+ {
+ BMCWEB_LOG_ERROR("Failed to assign fd {}", ec.message());
+ return;
+ }
// Add watch on directory to handle redfish event log file
// create/delete.
- dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
- IN_CREATE | IN_MOVED_TO | IN_DELETE);
- if (dirWatchDesc == -1)
+ dirWatchDesc =
+ inotify_add_watch(inotifyConn.native_handle(), redfishEventLogDir,
+ IN_CREATE | IN_MOVED_TO | IN_DELETE);
+ if (dirWatchDesc < 0)
{
BMCWEB_LOG_ERROR("inotify_add_watch failed for event log directory.");
return;
}
// Watch redfish event log file for modifications.
- fileWatchDesc =
- inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY);
- if (fileWatchDesc == -1)
+ fileWatchDesc = inotify_add_watch(inotifyConn.native_handle(),
+ redfishEventLogFile, IN_MODIFY);
+ if (fileWatchDesc < 0)
{
BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file.");
// Don't return error if file not exist.
@@ -230,7 +242,6 @@
}
// monitor redfish event log file
- inotifyConn.assign(inotifyFd);
watchRedfishEventLogFile();
if (redfishLogFilePosition != 0)