Search code examples
inotifyfile-sharing

inotify failed to start the watcher


ALL,

I'm trying to write a file watching service using inotify. Here is my code:

m_fd = inotify_init1( IN_NONBLOCK );
if( m_fd == -1 )
{
    syslog( LOG_ERR, "Failed to initialize inotify" );
    m_isOK = false;
}
else
    syslog( LOG_DEBUG, "Inotify initialize successfully" );
m_wd = inotify_add_watch( m_fd, log.c_str(), IN_CLOSE );
if( m_wd == -1 )
{
    std::string error = "Failed to add log watching to inotify, error is " + std::to_string( errno );
    syslog( LOG_ERR, error.c_str() );
    m_isOK = false;
}
else
    syslog( LOG_DEBUG, "Watching system intrusion file log added" );

When I tried to execute it it did run once successfully. However, every time I try to run it after that, I am getting in the log:

Failed to add log watching to inotify, error is 2.

This happens even after rebooting the system.

Since error 2 is File sharing error, I guess there is some lock_file present on the system that I need to remove in order for inotify_add_watch() to succeed.

Could someone please help? Or maybe I'm completely wrong and there is something else?

TIA!

[EDIT]

I'm trying to see when the file will be created on disk so that I can start processing it. Am I using the wrong mask?

Also modifying the line:

m_wd = inotify_add_watch( m_fd, log.c_str(), IN_CLOSE );

to read:

m_wd = inotify_add_watch( m_fd, log.c_str(), IN_CREATE | IN_CLOSE );

doesn't change the outcome. I still get a failure with the errno as 2.

[/EDIT]


Solution

  • So apparently, I can't watch the file for the file creation.

    I have to watch the appropriate directory and then check inotify_event for the file name being created.

    Thank you for reading and sorry for the traffic.