系统运行时inotify问题

发布于 2024-12-28 19:09:39 字数 3057 浏览 4 评论 0原文

我想监视一个文件夹。

每次弹出通知时,我想运行系统命令行。 使用系统命令时出现问题。每个新事件都会弹出 3 次,尽管它应该弹出一次。 编辑: 谢谢你的重播。我发现了这个错误。系统执行了受监控文件夹内的文件夹。这就是为什么每次我在受监控的文件夹中放入文件夹时,该事件都会被打印 3 次。

代码---------

    /*

    Simple example for inotify in Linux.

    */


    #include <sys/inotify.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main(){
        int fd,wd,wd1,i=0,len=0;
        char pathname[100],buf[1024];
        struct inotify_event *event;

        fd=inotify_init1(IN_NONBLOCK);
        /* watch /test directory for any activity and report it back to me */

 `wd=inotify_add_watch(fd,"/home/folder",IN_ALL_EVENTS`);

        while(1){
            //read 1024  bytes of events from fd into buf
            i=0;
            len=read(fd,buf,1024);
            while(i<len){
                event=(struct inotify_event *) &buf[i];


                /* check for changes */
                if(event->mask & IN_OPEN)
                 {  // printf("\n %s :was opened\n",event->name);
                    char*path="/home/folder/";
                    char*file=event->name;
                    int n=sizeof(path)+sizeof(file);
                    char *result=(char *)malloc(512);
                    strcpy(result,path); // copy string one into the result.
                    strcat(result,file); // append string two to the result
                    puts (result);

                    //printf("RESUULT:");

                    int pp=sizeof(result);
                    char *run="/home/test/./userr ";
                    int l=sizeof(run);

                    char *cmd=(char *)malloc(1000);
                    strcpy(cmd,run);
                    strcat(cmd,result);
                    puts (cmd);

                    system(cmd);
                    printf("\n %s :was opened\n",event->name);
                    //break;

            }
                if(event->mask & IN_MODIFY)
                      printf("%s : modified\n",event->name);

                if(event->mask & IN_ATTRIB)
                      printf("%s :meta data changed\n",event->name);

                if(event->mask & IN_ACCESS)
                      printf("%s :was read\n",event->name);

                if(event->mask & IN_CLOSE_WRITE)
                      printf("%s :file opened for writing was closed\n",event->name);

               // if(event->mask & IN_DELETE)
                //    printf("%s :deleted\n",event->name);

                /* update index to start of next event */
                i+=sizeof(struct inotify_event)+event->len;
            }

        }

    }

编辑:

我怎样才能让(1)睡一分钟? 速度(60);弹出inotify void:被打开而不是folder1:当我将foder放入受监视的文件夹

./exec

 :was opened
/home/folder/
   :file opened not for writing was closed

时打开,而(发布的代码)我有:

 1002_copy :was opened
/home/folder/1002_copy
1002_copy :file opened not for writing was closed

I want to monitor a folder.

Every time a notification pops up i want to run a system command line.
Problems when using system command. Each new event pops up 3 times though it should pop up one time.
EDIT:
Thx for you replays. I found the bug. The system executed a folder that was inside the monitored foder. this is why each time i dropped a foder in the monitored folder, the event was printed 3 times.

code-----------

    /*

    Simple example for inotify in Linux.

    */


    #include <sys/inotify.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main(){
        int fd,wd,wd1,i=0,len=0;
        char pathname[100],buf[1024];
        struct inotify_event *event;

        fd=inotify_init1(IN_NONBLOCK);
        /* watch /test directory for any activity and report it back to me */

 `wd=inotify_add_watch(fd,"/home/folder",IN_ALL_EVENTS`);

        while(1){
            //read 1024  bytes of events from fd into buf
            i=0;
            len=read(fd,buf,1024);
            while(i<len){
                event=(struct inotify_event *) &buf[i];


                /* check for changes */
                if(event->mask & IN_OPEN)
                 {  // printf("\n %s :was opened\n",event->name);
                    char*path="/home/folder/";
                    char*file=event->name;
                    int n=sizeof(path)+sizeof(file);
                    char *result=(char *)malloc(512);
                    strcpy(result,path); // copy string one into the result.
                    strcat(result,file); // append string two to the result
                    puts (result);

                    //printf("RESUULT:");

                    int pp=sizeof(result);
                    char *run="/home/test/./userr ";
                    int l=sizeof(run);

                    char *cmd=(char *)malloc(1000);
                    strcpy(cmd,run);
                    strcat(cmd,result);
                    puts (cmd);

                    system(cmd);
                    printf("\n %s :was opened\n",event->name);
                    //break;

            }
                if(event->mask & IN_MODIFY)
                      printf("%s : modified\n",event->name);

                if(event->mask & IN_ATTRIB)
                      printf("%s :meta data changed\n",event->name);

                if(event->mask & IN_ACCESS)
                      printf("%s :was read\n",event->name);

                if(event->mask & IN_CLOSE_WRITE)
                      printf("%s :file opened for writing was closed\n",event->name);

               // if(event->mask & IN_DELETE)
                //    printf("%s :deleted\n",event->name);

                /* update index to start of next event */
                i+=sizeof(struct inotify_event)+event->len;
            }

        }

    }

EDIT:

HOW CAN I PUT TO SLEEP THE WHILE(1) FOR 1 MINUTE?
SPEEP(60); pops the inotify void:was opened instead of folder1:was opened when i dropp a foder in the monitored folder

./exec

 :was opened
/home/folder/
   :file opened not for writing was closed

Without sleep inside while (the code posted) i have:

 1002_copy :was opened
/home/folder/1002_copy
1002_copy :file opened not for writing was closed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2025-01-04 19:09:39

您正在 if 条件中运行 system() ,每次看到打开的文件时都会调用该条件。

因此,每当您打开文件时,它都会无限运行。你必须找到一种方法来摆脱循环。

当执行到换行符时,它会跳出内部 while,但是外部 while(1) 又如何呢?

按要求(工作代码):

#include <sys/inotify.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    int fd,wd,wd1,i=0,len=0;
    char pathname[100],buf[1024];
    struct inotify_event *event;

    fd=inotify_init1(IN_NONBLOCK);
    wd=inotify_add_watch(fd,"/tmp/temp",IN_ALL_EVENTS);
    while(1){
        //read 1024  bytes of events from fd into buf
        i=0;
        len=read(fd,buf,1024);
        while(i<len){
            event=(struct inotify_event *) &buf[i];
            /* check for changes */
            if(event->mask & IN_CREATE){
                system("/bin/date");
            }
            i+=sizeof(struct inotify_event)+event->len;
        }  
    }
}

每次将文件添加到 /tmp/temp 时,上述代码都会执行 $date 命令。修改它以满足您的需要。

You are running system() in the if condition which is called every time a open file is seen.

So, it will run infinitely whenever you open a file. You have to find a way to get out of the loop.

When execution reaches the line break, it breaks out of the inner while but what about the outer while(1)?

AS REQUESTED (working code):

#include <sys/inotify.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    int fd,wd,wd1,i=0,len=0;
    char pathname[100],buf[1024];
    struct inotify_event *event;

    fd=inotify_init1(IN_NONBLOCK);
    wd=inotify_add_watch(fd,"/tmp/temp",IN_ALL_EVENTS);
    while(1){
        //read 1024  bytes of events from fd into buf
        i=0;
        len=read(fd,buf,1024);
        while(i<len){
            event=(struct inotify_event *) &buf[i];
            /* check for changes */
            if(event->mask & IN_CREATE){
                system("/bin/date");
            }
            i+=sizeof(struct inotify_event)+event->len;
        }  
    }
}

The above code executes $date command each time a file is added to /tmp/temp. Modify it to suit your needs.

谁对谁错谁最难过 2025-01-04 19:09:39

你的系统命令在做什么?它正在打开您正在更改的文件吗?如果是这样,是否会导致您的代码再次运行并使您陷入无限循环(这显然就是您所看到的)?

编辑 - Linux Journal 中有一篇文章它所做的事情与你非常相似。我注意到他们使用了更大的缓冲区,并且注释提到了有关结构对齐的内容,因此您是否有可能获得错误的事件长度,从而导致缓冲区溢出并迫使您的代码循环时间超过预期。

以某种方式,我会检查(使用一些 printf 调试逻辑)您是否按预期循环。

What is your system command doing? Is it opening the file you're changing? If so, wouldn't that cause your code to run again and put you into an infinite loop (which is apparently what you're seeing)?

Edit - There's an article in Linux Journal which is doing something very similar to you. I noticed they're using a larger buffer and the comments mention something about structure alignment so is it possible you're getting a bad event length which is causing a buffer overrun and forcing your code to loop for longer than expected.

Ether way, I'd check (with a bit of printf debug logic) that you're looping as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文