Skip to content

Instantly share code, notes, and snippets.

@isayme
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isayme/9383493 to your computer and use it in GitHub Desktop.
Save isayme/9383493 to your computer and use it in GitHub Desktop.
donot touch my files if you are not root!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/inotify.h>
#include <signal.h>
#define R_NOERR 0
#define R_ERROR -1
int fd = 0;
char *dir = "/home/wugq";
char *topdir = NULL;
void signal_handler(int signum)
{
printf("Ctrl + C Exit\n");
if (0 != fd)
{
if (close(fd) < 0)
{
printf("close(fd) Error\n");
}
}
exit(0);
}
void handle_event(struct inotify_event *event);
int main(int argc, char **argv)
{
fd_set rfds;
char buf[1024];
struct inotify_event * event;
int index, cnt;
topdir = dir;
if (signal(SIGINT, signal_handler) == SIG_IGN)
{
printf("SIGINT Error\n");
signal(SIGINT, SIG_IGN);
}
daemon(0, 0);
fd = inotify_init();
if (fd < 0)
{
perror("inotify_init() Error\n");
return -1;
}
if (inotify_add_watch(fd, topdir, IN_ISDIR|IN_ATTRIB) < 0)
{
printf("Watch %s Error\n", topdir);
return -1;
}
while (1)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) > 0)
{
if (FD_ISSET(fd, &rfds))
{
cnt = read(fd, buf, 1024);
index = 0;
while (cnt > index)
{
event = (struct inotify_event *)&buf[index];
index = index + event->len + 16;
handle_event(event);
}
fflush(NULL);
}
}
}
return 0;
}
void handle_event(struct inotify_event *event)
{
//printf("Event : %s/%x\n", event->name, event->mask);
struct stat buf;
if (event->mask & IN_ATTRIB)
{
stat(topdir, &buf);
//printf("stat : %x \n", buf.st_mode);
if (buf.st_mode & (S_IROTH|S_IWOTH|S_IXOTH|S_IXGRP|S_IWGRP|S_IRGRP))
{
//printf("Match!\n");
chmod(topdir, S_IRUSR | S_IWUSR | S_IXUSR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment