Thread: inotify woes

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    inotify woes

    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:

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So, what happened to the code indentation?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    Does it really matter?

    Obviously the cut and paste did not quite work.
    Other than checking the formatting (which I assure you is not the problem) would you happen to have a more useful response?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I take it you are referring to this:
    http://www.linuxjournal.com/node/8478/print

    In that case, I'd say you are probably missing a great deal of this:
    Code:
    len = read (fd, buf, BUF_LEN);
    if (len < 0) {
            if (errno == EINTR)
                    /* need to reissue system call */
            else
                    perror ("read");
    } else if (!len)
            /* BUF_LEN too small? */
    
    while (i < len) {
            struct inotify_event *event;
    
            event = (struct inotify_event *) &buf[i];
    
            printf ("wd=%d mask=%u cookie=%u len=%u\n",
                    event->wd, event->mask,
                    event->cookie, event->len);
    
            if (event->len)
                    printf ("name=%s\n", event->name);
    
            i += EVENT_SIZE + event->len;
    }
    Your "event" isn't even initialized, so assuming your code actually runs, it probably won't if you add
    Code:
    event = NULL;
    at the top of your code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Thank you Mats,

    Yes, that is the article I am referring to. I actually have the code snippet you have shown. Quite honestly I did not think I needed that for single events. He does mention in his article that select can be used instead of read but it is entirely possible that I misunderstood that part. I am going to read the article again. I suppose I am fixated on the mask too much at this point.

    Incidentally, I also tried Robert Love's more complete example provided in: inotify-utils-0.25, and curiously enough his code hangs after the first event. In other words, select unblocks for the first event, it is processed, and then the code hangs forever on the second block on select. It will not report any more events.

    Anyway I will keep tinkering. Thanks again.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    I ought to give my head a shake.

    Mats,

    I re-read that portion and I have it all working now. Yes...obviously not initialized. What was I thinking!?

    Thanks for setting me straight.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > would you happen to have a more useful response?
    For sure.
    I see no reason to help people who can't even get the basics of formatting right?

    I mean, if youwroteasentenceoutlikethiswithallthespacesremove d, would you even bother to help someone, if their pitiful excuse was "oh, I couldn't get the formatting to work".

    Unindented code is crap to look at, and shows you really couldn't give a rats ass.
    http://www.catb.org/~esr/faqs/smart-questions.html

    You're asking for free help. In competition with everyone else, if yours is the only one with such poor presentation, guess which one is most likely to be answered last (or not at all). Bzzzzzt - that's correct, yours.

    > Other than checking the formatting (which I assure you is not the problem)
    I tend to assume that lack of formatting is just the tip of the iceberg of many other problems.
    And yes, people have used the same "assurance" before, and been wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inotify problem
    By MisterIO in forum Linux Programming
    Replies: 17
    Last Post: 11-14-2008, 11:02 AM
  2. MD2 woes
    By psychopath in forum Game Programming
    Replies: 9
    Last Post: 07-02-2005, 07:46 PM
  3. asctime woes
    By magis in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2005, 04:01 PM
  4. Ugh, ComboBox Woes
    By Zeusbwr in forum Windows Programming
    Replies: 11
    Last Post: 04-11-2005, 11:14 PM
  5. Doc/view woes fixed
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 04-08-2005, 09:40 AM