Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1879343
  • 博文数量: 333
  • 博客积分: 10791
  • 博客等级: 上将
  • 技术积分: 4314
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 07:39
文章分类

全部博文(333)

文章存档

2015年(1)

2011年(116)

2010年(187)

2009年(25)

2008年(3)

2007年(1)

分类: C/C++

2008-05-22 13:06:55

//主要描述:假设一个目录中有些重要文件,用C语言实现保证时刻检测该目录文件的安全性。

//具体代码如下:不过这仅仅是个框架,大家有什么好的建议,我们一起share!

 

 

//Created by Reesun Huang

//5 22 2008

 

#define _GNU_SOURCE
#include
#include
#include
#include

 

char quit;


//To deal with the event that the file was deleted by process
static void handler_d(int sig,siginfo_t *si,void *data)
{
  int event_fd,event_pid;

  event_fd = si->si_fd;
  event_pid = si->si_pid;

  printf("Process:%d,has deleted something from fd:%d!\n",event_pid,event_fd);
}//end handler_d


//To deal with the event that the file was motified by process
static void handler_m(int sig,siginfo_t *si,void *data)
{
  int event_fd,event_pid;

  event_fd = si->si_fd;
  event_pid = si->si_pid;

  printf("Process:%d,has motified something of fd:%d!\n",event_pid,event_fd);
}//end handler_m


int main(void)
{
  struct sigaction act_d,act_m;
  int fd1,fd2;
  pid_t pid;

  if((pid = fork()) == 0)
  {
    printf("Fork error!\n");
    return (1);
  }
  else if(pid == 0)
  {
    //This is a child process
    //To open the directory by the following :/root/hello1  /root/hello2
    //In the parent process
  
    if((fd1 = open("/root/hello1",O_RDONLY)) == -1)
    {
      printf("To open /root/hello1 error!\n");
    }
    if((fd2 = open("/root/hello2",O_RDONLY)) == -1)
    {
      printf("To open /root/hello2 error!\n");
    }

    //To config the operation of signal
    act_m.sa_sigaction = handler_m;
    sigemptyset(&act_m.sa_mask);
    act_m.sa_flags = SA_SIGINFO;
   
    //To point the function which can operate the signal
    sigaction(SIGRTMIN + 2,&act_m,NULL);
   

    //To connect the fd1 with the signal
    fcntl(fd1,F_SETSIG,SIGRTMIN + 2);
    //To define the event as a notice
    fcntl(fd1,F_NOTIFY,DN_MODIFY|DN_MULTISHOT);

    //Do as the same operation on fd2
    fcntl(fd2,F_SETSIG,SIGRTMIN + 2);
    fcntl(fd2,F_NOTIFY,DN_MODIFY|DN_MULTISHOT);

    //daemon
    while(1)
    {
      pause();
    }//end while
  }
  else
  {
    //The parent process
    //To open the directory by the following:/root/hello1  /root/hello2
    
    if((fd1 = open("/root/hello1",O_RDONLY)) == -1)
    {
      printf("To open /root/hello1 error!\n");
    }
 
    if((fd2 = open("/root/hello2",O_RDONLY)) == -1)
    {
      printf("To open /root/hello2 error!\n");
    }

    //Output /root/hello1 and /root/hello2
    printf("/root/hello1,fd = %d\n",fd1);
    printf("/root/hello2,fd = %d\n",fd2);

    // To config the operation of signal
    act_d.sa_sigaction = handler_d;
    sigemptyset(&act_d.sa_mask);
    act_d.sa_flags = SA_SIGINFO;

    //To set the function of operating signal
    sigaction(SIGRTMIN +1 ,&act_d,NULL);

    //To connect fd1 with signal
    fcntl(fd1,F_SETSIG,SIGRTMIN +1);
    //To defind the event as a notice
    fcntl(fd1,F_NOTIFY,DN_DELETE|DN_MULTISHOT);
  
    //daemon
    while(1)
    {
      pause();
    }
  }
}//end main

阅读(2188) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~