LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2008, 01:47 PM   #1
quixote08
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
inotify problems


Greetings,

I have been playing with a piece of code based mostly on Robert Love's article in the Linux journal and the code (which follows) is simply not doing what it is supposed to be doing. Basically, every time select unblocks after an event, the event mask is the same, even though the events are all different. I am quite certain I have missed something very basic but for the life of me I cannot see it. Here is the code:

#include <stdio.h>
#include <sys/inotify.h>
#include <sys/select.h>

void print_mask(int );

int main (void)
{
int fd, wd, ret;
struct timeval time;
struct inotify_event *event;
fd_set rfds;


// time out after 10 seconds
time.tv_sec = 5;
time.tv_usec = 0;

fd = inotify_init();
if (fd < 0)
perror ("inotify_init");

wd = inotify_add_watch (fd, "/data/temp", IN_MODIFY|IN_CREATE|IN_DELETE);
if (wd < 0)
perror ("inotify_add_watch");

FD_ZERO (&rfds);
FD_SET (fd, &rfds);

ret = select (fd + 1, &rfds, NULL, NULL, NULL);
if (ret < 0)
perror ("select");
else if (!ret)
printf ("timed out\n");
else if (FD_ISSET (fd, &rfds))
{
if (event->mask & IN_CREATE)
{
printf ("File Created!\n");
print_mask (event->mask);
}
else if (event->mask & IN_DELETE)
{
printf ("File Deleted!\n");
printf("0x%08x\n", event->mask);
}
}

ret = inotify_rm_watch (fd, wd);
if (ret)
perror ("inotify_rm_watch");
if (close(fd))
perror ("close");
return 0;
}

void print_mask(int mask)
{
if (mask & IN_ACCESS)
printf("ACCESS ");
if (mask & IN_MODIFY)
printf("MODIFY ");
if (mask & IN_ATTRIB)
printf("ATTRIB ");
if (mask & IN_CLOSE)
printf("CLOSE ");
if (mask & IN_OPEN)
printf("OPEN ");
if (mask & IN_MOVED_FROM)
printf("MOVE_FROM ");
if (mask & IN_MOVED_TO)
printf("MOVE_TO ");
if (mask & IN_DELETE)
printf("DELETE ");
if (mask & IN_CREATE)
printf("CREATE ");
if (mask & IN_DELETE_SELF)
printf("DELETE_SELF ");
if (mask & IN_UNMOUNT)
printf("UNMOUNT ");
if (mask & IN_Q_OVERFLOW)
printf("Q_OVERFLOW ");
if (mask & IN_IGNORED)
printf("IGNORED " );

if (mask & IN_ISDIR)
printf("(dir) ");
else
printf("(file) ");

printf("0x%08x\n", mask);
}


Any help will be most appreciated. Thanks.
 
Old 01-26-2008, 05:17 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,450

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

Please use code tags when you post code - it's much easier to read.

You are thinking of http://www.linuxjournal.com/article/8478 right? In it, there is a section where the event is read - which you seem to miss in your code. Select is used to check if there is data available to read, since the read function will wait till there is some. When you have found out there is data to be read you need to read it. Reading events is a bit complicated as the event structure doesn't have a fixed length. But there is a section in the article explaining how you do it.

Also, in your code, you specify IN_MODIFY|IN_CREATE|IN_DELETE meaning that's the only event you will recieve. If you want all, you can use IN_ALL_EVENTS instead.
 
Old 01-26-2008, 09:53 AM   #3
quixote08
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Hi Guttorm,

Yes I read that part. I think I misunderstood because I thought what he meant was we could use select and that way not get blocked on a read. Later on I did realized that even was not initialized, which is pretty basic error. In any case I sorted it all out now.

I thought I had inserted the code tags but perhaps not; I rarely post on th eforums so the little details escape me. But I will keep it in mind for next time.

Thanks for your suggestions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in programming with inotify shifter Programming 12 06-21-2007 06:14 PM
udev and inotify aeruzcar Linux - General 3 03-24-2007 12:42 PM
Mounting Inotify? trscookie Linux - Software 1 12-19-2005 01:37 PM
inotify user instance limit??? brad182 Linux - Software 0 12-15-2005 05:41 PM
Is inotify worth it? aneel.d Linux - General 0 10-14-2005 10:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration