mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: fileMonitor : continue if inotify_init failed
This commit is contained in:
@ -39,7 +39,7 @@ Class
|
|||||||
#else
|
#else
|
||||||
# include <sys/inotify.h>
|
# include <sys/inotify.h>
|
||||||
# include <sys/ioctl.h>
|
# include <sys/ioctl.h>
|
||||||
|
# include <errno.h>
|
||||||
# define EVENT_SIZE ( sizeof (struct inotify_event) )
|
# define EVENT_SIZE ( sizeof (struct inotify_event) )
|
||||||
# define EVENT_LEN (EVENT_SIZE + 16)
|
# define EVENT_LEN (EVENT_SIZE + 16)
|
||||||
# define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
|
# define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
|
||||||
@ -144,7 +144,7 @@ namespace Foam
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
//- File descriptor for the inotify instance
|
//- File descriptor for the inotify instance
|
||||||
int fd;
|
int inotifyFd_;
|
||||||
|
|
||||||
//- Current watchIDs and corresponding directory id
|
//- Current watchIDs and corresponding directory id
|
||||||
DynamicList<label> dirWatches_;
|
DynamicList<label> dirWatches_;
|
||||||
@ -153,23 +153,39 @@ namespace Foam
|
|||||||
//- initialise inotify
|
//- initialise inotify
|
||||||
inline fileMonitorWatcher(const label sz = 20)
|
inline fileMonitorWatcher(const label sz = 20)
|
||||||
:
|
:
|
||||||
fd(inotify_init()),
|
inotifyFd_(inotify_init()),
|
||||||
dirWatches_(sz),
|
dirWatches_(sz),
|
||||||
dirFiles_(sz)
|
dirFiles_(sz)
|
||||||
{}
|
{
|
||||||
|
if (inotifyFd_ < 0)
|
||||||
|
{
|
||||||
|
WarningIn("fileMonitorWatcher(const label)")
|
||||||
|
<< "Failed allocating an inotify descriptor : "
|
||||||
|
<< string(strerror(errno)) << endl
|
||||||
|
<< " Please increase the number of allowable "
|
||||||
|
<< "inotify instances" << endl
|
||||||
|
<< " (/proc/sys/fs/inotify/max_user_instances on Linux)"
|
||||||
|
<< endl
|
||||||
|
<< " Continuing without additional file monitoring."
|
||||||
|
<< endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//- remove all watches
|
//- remove all watches
|
||||||
inline ~fileMonitorWatcher()
|
inline ~fileMonitorWatcher()
|
||||||
{
|
{
|
||||||
forAll(dirWatches_, i)
|
if (inotifyFd_ >= 0)
|
||||||
{
|
{
|
||||||
if (dirWatches_[i] >= 0)
|
forAll(dirWatches_, i)
|
||||||
{
|
{
|
||||||
if (inotify_rm_watch(fd, int(dirWatches_[i])))
|
if (dirWatches_[i] >= 0)
|
||||||
{
|
{
|
||||||
WarningIn("fileMonitor::~fileMonitor()")
|
if (inotify_rm_watch(inotifyFd_, int(dirWatches_[i])))
|
||||||
<< "Failed deleting directory watch "
|
{
|
||||||
<< dirWatches_[i] << endl;
|
WarningIn("fileMonitor::~fileMonitor()")
|
||||||
|
<< "Failed deleting directory watch "
|
||||||
|
<< dirWatches_[i] << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,10 +193,15 @@ namespace Foam
|
|||||||
|
|
||||||
inline bool addWatch(const label watchFd, const fileName& fName)
|
inline bool addWatch(const label watchFd, const fileName& fName)
|
||||||
{
|
{
|
||||||
|
if (inotifyFd_ < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Add/retrieve watch on directory containing file
|
// Add/retrieve watch on directory containing file
|
||||||
label dirWatchID = inotify_add_watch
|
label dirWatchID = inotify_add_watch
|
||||||
(
|
(
|
||||||
fd,
|
inotifyFd_,
|
||||||
fName.path().c_str(),
|
fName.path().c_str(),
|
||||||
IN_CLOSE_WRITE
|
IN_CLOSE_WRITE
|
||||||
);
|
);
|
||||||
@ -189,7 +210,8 @@ namespace Foam
|
|||||||
{
|
{
|
||||||
FatalErrorIn("addWatch(const label, const fileName&)")
|
FatalErrorIn("addWatch(const label, const fileName&)")
|
||||||
<< "Failed adding watch " << watchFd
|
<< "Failed adding watch " << watchFd
|
||||||
<< " to directory " << fName
|
<< " to directory " << fName << " due to "
|
||||||
|
<< string(strerror(errno))
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,6 +231,11 @@ namespace Foam
|
|||||||
|
|
||||||
inline bool removeWatch(const label watchFd)
|
inline bool removeWatch(const label watchFd)
|
||||||
{
|
{
|
||||||
|
if (inotifyFd_ < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
dirWatches_[watchFd] = -1;
|
dirWatches_[watchFd] = -1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -263,11 +290,11 @@ void Foam::fileMonitor::checkFiles() const
|
|||||||
fd_set fdSet;
|
fd_set fdSet;
|
||||||
// Add notify descriptor to select fd_set
|
// Add notify descriptor to select fd_set
|
||||||
FD_ZERO(&fdSet);
|
FD_ZERO(&fdSet);
|
||||||
FD_SET(watcher_->fd, &fdSet);
|
FD_SET(watcher_->inotifyFd_, &fdSet);
|
||||||
|
|
||||||
int ready = select
|
int ready = select
|
||||||
(
|
(
|
||||||
watcher_->fd+1, // num filedescriptors in fdSet
|
watcher_->inotifyFd_+1, // num filedescriptors in fdSet
|
||||||
&fdSet, // fdSet with only inotifyFd
|
&fdSet, // fdSet with only inotifyFd
|
||||||
NULL, // No writefds
|
NULL, // No writefds
|
||||||
NULL, // No errorfds
|
NULL, // No errorfds
|
||||||
@ -280,15 +307,15 @@ void Foam::fileMonitor::checkFiles() const
|
|||||||
<< "Problem in issuing select."
|
<< "Problem in issuing select."
|
||||||
<< abort(FatalError);
|
<< abort(FatalError);
|
||||||
}
|
}
|
||||||
else if (FD_ISSET(watcher_->fd, &fdSet))
|
else if (FD_ISSET(watcher_->inotifyFd_, &fdSet))
|
||||||
{
|
{
|
||||||
// Read events
|
// Read events
|
||||||
ssize_t nBytes = read(watcher_->fd, buffer, EVENT_BUF_LEN);
|
ssize_t nBytes = read(watcher_->inotifyFd_, buffer, EVENT_BUF_LEN);
|
||||||
|
|
||||||
if (nBytes < 0)
|
if (nBytes < 0)
|
||||||
{
|
{
|
||||||
FatalErrorIn("fileMonitor::updateStates(const fileName&)")
|
FatalErrorIn("fileMonitor::updateStates(const fileName&)")
|
||||||
<< "read of " << watcher_->fd
|
<< "read of " << watcher_->inotifyFd_
|
||||||
<< " failed with " << label(nBytes)
|
<< " failed with " << label(nBytes)
|
||||||
<< abort(FatalError);
|
<< abort(FatalError);
|
||||||
}
|
}
|
||||||
@ -364,6 +391,7 @@ Foam::label Foam::fileMonitor::addWatch(const fileName& fName)
|
|||||||
label watchFd;
|
label watchFd;
|
||||||
|
|
||||||
label sz = freeWatchFds_.size();
|
label sz = freeWatchFds_.size();
|
||||||
|
|
||||||
if (sz)
|
if (sz)
|
||||||
{
|
{
|
||||||
watchFd = freeWatchFds_[sz-1];
|
watchFd = freeWatchFds_[sz-1];
|
||||||
|
|||||||
Reference in New Issue
Block a user