Test in_delete_self in inotify.

Source: Internet
Author: User

// An error occurred while understanding in_delete_self in this article.

Some time ago I wrote inotify-related programs and found that when the time is in_delete_self or in_move_self, the name attribute in the event structure cannot be printf.

I don't think so. I wrote a code test today.

#include <stdio.h>#include <sys/inotify.h>#include <unistd.h>#include <stdlib.h>#define BUF_LEN 1024#define EVENT_NUM 12static char *event_str[EVENT_NUM] = {"IN_ACCESS","IN_MODIFY","IN_ATTRIB","IN_CLOSE_WRITE","IN_CLOSE_NOWRITE","IN_OPEN","IN_MOVED_FROM","IN_MOVED_TO","IN_CREATE","IN_DELETE","IN_DELETE_SELF","IN_MOVE_SELF"};int main(int argc, char *argv[]){pid_t pid;if(argc < 2){printf("%s dir\n", argv[0]);return -1;}pid = fork();if(pid == 0){sleep(20);remove(argv[0]);exit(-1);}else if(pid < 0){printf("fork failed\n");return -1;}set_watch(argv[1]);return 0;}int set_watch(char *dir){int fd, wd;int i;char buf[BUF_LEN];int len, nread;struct inotify_event *event;if((fd = inotify_init()) == -1){printf("inotify_init failed\n");return -1;}wd = inotify_add_watch(fd, dir, IN_ALL_EVENTS);if(wd == -1){printf("inotify_add_watch %s failed\n", dir);return -1;}while((len = read(fd, buf, BUF_LEN)) > 0){buf[BUF_LEN-1] = '\0';nread = 0;while(len){event = (struct inotify_event *)&buf[nread];for(i=0; i<EVENT_NUM; i++){if((event->mask >> i) & 1){printf("%s --- %s\n", event->name, event_str[i]);break;}}nread = nread + sizeof(struct inotify_event) + event->len;len = len - (sizeof(struct inotify_event) + event->len);}}inotify_rm_watch(fd, wd);close(fd);return 0;}

Run the program:

Ts_mv_del_self --- in_delete

Normal. It seems that my previous code is faulty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.