* Possible bug in inotify api
@ 2011-07-21 6:47 Morten Winkler Jørgensen
2011-07-22 23:08 ` Andrew Morton
0 siblings, 1 reply; 6+ messages in thread
From: Morten Winkler Jørgensen @ 2011-07-21 6:47 UTC (permalink / raw)
To: linux-kernel
[-- Attachment #1: Type: text/plain, Size: 1750 bytes --]
To whom it might concern
I'd like to report a possible malfunction of the inotify api and check if
this issue has been reported.
If this is not the proper place to do so, please let me know. I have
searched the archives and the web and found similar reportings but none
the same.
The observed behavior is when calling inotify_remove_watch. Now and then
it returns error 22, INVALID_ARGUMENT, even though the watchdesriptor
ought to be valid. I've seen that people complain about this behavior on
the dovecot mailing list but nobody seems to have suggested a fix.
I've attached a small program that I use to reproduce the behavior.
The program watches /tmp and whenever it's notified about a directory
created in /tmp, my program adds a watch for that directory as well.
The program is a C++ program (as I will need the inotify functionality in
a C++ program, but the behavior is the same in a pure C implementation)
and to reproduce I do the following:
* g++ main.cpp
* ./a.out
In a shell I go to /tmp and run
* for i in `seq 1 5000`; do mkdir D-$i; done; for i in `seq 1 5000`; do
rmdir D-$i; done
This makes my testprogram output the wd's created and and the wd's my
program tries to remove. I've never seen the program successfully remove
all 5000 watch descriptors and I've seen inotify_rm_watch fail after 0
removes and after approx 2000 removes and anything in between.
I'm running kernel 2.6.23-5 from the Debian repositories.
If what I've written is totally nonsense, if it is expected behavior or if
I've done anything wrong, please let me know. Also, if I can do anything
further to investigate the misbehavior also, please let me know.
Best and thanks in advance,
Morten
Attached: C++ program used to reproduce the behavior.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: main.cpp --]
[-- Type: text/x-c++src; name="main.cpp", Size: 3951 bytes --]
#include <sys/inotify.h>
#include <sys/syscall.h>
// Use this if syscalls not defined
#ifndef __NR_inotify_init
#include <sys/inotify-syscalls.h>
#endif // __NR_inotify_init
#include <stdio.h>
#include <unistd.h>
#include <map>
#include <string>
using namespace std;
typedef map<string, int> StrIntMap;
typedef pair<string, int> StrIntPair;
typedef map<int, string> IntStrMap;
typedef pair<int, string> IntStrPair;
StrIntMap pathsToWds;
IntStrMap wdsToPaths;
int fd;
int AddWatch(const string &path)
{
int wd = inotify_add_watch( fd, (char*)(path.c_str()), IN_CREATE | IN_DELETE | IN_MODIFY );
pathsToWds.insert(StrIntPair(path, wd));
wdsToPaths.insert(IntStrPair(wd, path));
printf("Adds %s as wd %d. Has %d watches\n", path.c_str(), wd, pathsToWds.size());
return wd;
}
void RemoveWatch(const string &path)
{
int wd = pathsToWds[path];
pathsToWds.erase(path);
wdsToPaths.erase(wd);
printf("Removes %s as wd %d. Has %d watches\n", path.c_str(), wd, pathsToWds.size());
if( inotify_rm_watch(fd, wd) != -1)
{
throw string("No no no...");
}
}
void RemoveWatch(int wd)
{
RemoveWatch(wdsToPaths[wd]);
}
int main( )
{
try
{
int EVENT_SIZE = sizeof (struct inotify_event) ;
int EVENT_BUF_LEN = ( 1024 * ( EVENT_SIZE + 16 ) );
int length, i = 0;
char buffer[EVENT_BUF_LEN];
/*creating the INOTIFY instance*/
fd = inotify_init();
/*checking for error*/
if ( fd < 0 ) {
// perror( "inotify_init" );
}
/*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/
string bd = "/tmp";
AddWatch(bd);
while(1) {
length = 0;
i = 0;
length = read( fd, buffer, EVENT_BUF_LEN );
/*checking for error*/
if ( length < 0 ) {
perror( "read" );
}
/*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
string parentdir;
parentdir = wdsToPaths[event->wd];
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
printf( "New directory %s created in %s.\n", event->name, parentdir.c_str());
AddWatch(parentdir+"/"+event->name);
}
else {
printf( "New file %s created in %s.\n", event->name, parentdir.c_str());
}
}
else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
printf( "Directory %s deleted in %s.\n", event->name, parentdir.c_str() );
RemoveWatch(parentdir+"/"+event->name);
}
else {
printf( "File %s deleted in %s.\n", event->name, parentdir.c_str());
}
}
else if ( event->mask & IN_MODIFY)
{
if ( event->mask & IN_ISDIR ) {
printf( "Directory %s modified in %s.\n", event->name, parentdir.c_str());
}
else {
printf( "File %s modified in %s.\n", event->name, parentdir.c_str());
}
}
}
i += EVENT_SIZE + event->len;
}
}
/*removing the “/tmp” directory from the watch list.*/
/*closing the INOTIFY instance*/
close( fd );
}
catch(string s)
{
printf("Caught: %s\n", s.c_str());
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Possible bug in inotify api
2011-07-21 6:47 Possible bug in inotify api Morten Winkler Jørgensen
@ 2011-07-22 23:08 ` Andrew Morton
2011-07-23 5:46 ` Morten Winkler Jørgensen
0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2011-07-22 23:08 UTC (permalink / raw)
To: morten; +Cc: linux-kernel, Eric Paris
On Thu, 21 Jul 2011 08:47:44 +0200
Morten Winkler J__rgensen <morten@winkler.dk> wrote:
> To whom it might concern
>
> I'd like to report a possible malfunction of the inotify api and check if
> this issue has been reported.
> If this is not the proper place to do so, please let me know. I have
> searched the archives and the web and found similar reportings but none
> the same.
>
> The observed behavior is when calling inotify_remove_watch. Now and then
> it returns error 22, INVALID_ARGUMENT, even though the watchdesriptor
> ought to be valid. I've seen that people complain about this behavior on
> the dovecot mailing list but nobody seems to have suggested a fix.
>
> I've attached a small program that I use to reproduce the behavior.
> The program watches /tmp and whenever it's notified about a directory
> created in /tmp, my program adds a watch for that directory as well.
>
> The program is a C++ program (as I will need the inotify functionality in
> a C++ program, but the behavior is the same in a pure C implementation)
> and to reproduce I do the following:
>
> * g++ main.cpp
> * ./a.out
>
> In a shell I go to /tmp and run
> * for i in `seq 1 5000`; do mkdir D-$i; done; for i in `seq 1 5000`; do
> rmdir D-$i; done
>
> This makes my testprogram output the wd's created and and the wd's my
> program tries to remove. I've never seen the program successfully remove
> all 5000 watch descriptors and I've seen inotify_rm_watch fail after 0
> removes and after approx 2000 removes and anything in between.
>
> I'm running kernel 2.6.23-5 from the Debian repositories.
>
> If what I've written is totally nonsense, if it is expected behavior or if
> I've done anything wrong, please let me know. Also, if I can do anything
> further to investigate the misbehavior also, please let me know.
>
> Best and thanks in advance,
> Morten
>
> Attached: C++ program used to reproduce the behavior.
I ran it a couple of times on my 2.6.32-based 2-CPU desktop here and it
ran to completion without any complaints.
So perhaps we fixed whatever-it-was in a later kernel. 2.6.23 is
pretty old!
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Possible bug in inotify api
2011-07-22 23:08 ` Andrew Morton
@ 2011-07-23 5:46 ` Morten Winkler Jørgensen
2011-07-25 16:21 ` Asbjørn Sannes
0 siblings, 1 reply; 6+ messages in thread
From: Morten Winkler Jørgensen @ 2011-07-23 5:46 UTC (permalink / raw)
To: Andrew Morton; +Cc: morten, linux-kernel, Eric Paris
Hi Andrew.
>> Attached: C++ program used to reproduce the behavior.
>
> I ran it a couple of times on my 2.6.32-based 2-CPU desktop here and it
> ran to completion without any complaints.
>
> So perhaps we fixed whatever-it-was in a later kernel. 2.6.23 is
> pretty old!
I see. Thank you for trying out. Once our situation allows for a kernel
update I'll do so. In the meanwhile, I'll have to think of something else.
Also, I'll try on other machines with newer kernels.
Again, thank you for your effort
Best,
Morten
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Possible bug in inotify api
2011-07-23 5:46 ` Morten Winkler Jørgensen
@ 2011-07-25 16:21 ` Asbjørn Sannes
2011-07-25 18:17 ` Morten Winkler Jørgensen
0 siblings, 1 reply; 6+ messages in thread
From: Asbjørn Sannes @ 2011-07-25 16:21 UTC (permalink / raw)
To: morten; +Cc: Andrew Morton, linux-kernel, Eric Paris
On 07/23/11 07:46, Morten Winkler Jørgensen wrote:
> Hi Andrew.
>
>>> Attached: C++ program used to reproduce the behavior.
>> I ran it a couple of times on my 2.6.32-based 2-CPU desktop here and it
>> ran to completion without any complaints.
>>
>> So perhaps we fixed whatever-it-was in a later kernel. 2.6.23 is
>> pretty old!
> I see. Thank you for trying out. Once our situation allows for a kernel
> update I'll do so. In the meanwhile, I'll have to think of something else.
> Also, I'll try on other machines with newer kernels.
>
> Again, thank you for your effort
>
Actually it failed for me on 2.6.39.2
Directory D-7 deleted in /tmp.
Removes /tmp/D-7 as wd 8. Has 4994 watches
Caught: No no no...
--
Asbjørn Sannes
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Possible bug in inotify api
2011-07-25 16:21 ` Asbjørn Sannes
@ 2011-07-25 18:17 ` Morten Winkler Jørgensen
2011-07-25 18:18 ` Eric Paris
0 siblings, 1 reply; 6+ messages in thread
From: Morten Winkler Jørgensen @ 2011-07-25 18:17 UTC (permalink / raw)
To: Asbjørn Sannes; +Cc: morten, Andrew Morton, linux-kernel, Eric Paris
>>> So perhaps we fixed whatever-it-was in a later kernel. 2.6.23 is
>>> pretty old!
>
> Actually it failed for me on 2.6.39.2
>
> Directory D-7 deleted in /tmp.
> Removes /tmp/D-7 as wd 8. Has 4994 watches
> Caught: No no no...
Ah. My colleague also saw this on an installation of the latest stable
OpenSuSE, featuring a 2.6.37.6 kernel.
I've never attended this mailinglist before. Is there anything I should to
to assist the investigation? Or just be patient?
Morten
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Possible bug in inotify api
2011-07-25 18:17 ` Morten Winkler Jørgensen
@ 2011-07-25 18:18 ` Eric Paris
0 siblings, 0 replies; 6+ messages in thread
From: Eric Paris @ 2011-07-25 18:18 UTC (permalink / raw)
To: morten; +Cc: Asbjørn Sannes, Andrew Morton, linux-kernel
On 07/25/2011 02:17 PM, Morten Winkler Jørgensen wrote:
>>>> So perhaps we fixed whatever-it-was in a later kernel. 2.6.23 is
>>>> pretty old!
>>
>> Actually it failed for me on 2.6.39.2
>>
>> Directory D-7 deleted in /tmp.
>> Removes /tmp/D-7 as wd 8. Has 4994 watches
>> Caught: No no no...
>
> Ah. My colleague also saw this on an installation of the latest stable
> OpenSuSE, featuring a 2.6.37.6 kernel.
>
> I've never attended this mailinglist before. Is there anything I should to
> to assist the investigation? Or just be patient?
Be patient. I'm running a bit behind....
-Eric
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2011-07-25 18:18 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-21 6:47 Possible bug in inotify api Morten Winkler Jørgensen
2011-07-22 23:08 ` Andrew Morton
2011-07-23 5:46 ` Morten Winkler Jørgensen
2011-07-25 16:21 ` Asbjørn Sannes
2011-07-25 18:17 ` Morten Winkler Jørgensen
2011-07-25 18:18 ` Eric Paris
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox