Detect create or delete files report: Signal processing mechanism

Source: Internet
Author: User

#include <stdio.h> #define  _GNU_SOURC#include<fcntl.h> #include <stdlib.h> #include < signal.h> #include <unistd.h>static void handler_d (int sig,siginfo_t *s,void*  Data) {int even_fd,even_pid;even_fd=s->si_fd;even_pid=s->si_pid;printf ("process:%d,has deleted  something from fd:%d\n ", EVEN_PID,EVEN_FD);} Static void handler_m (int sig,siginfo_t *s,void* data) {Int even_fd,even_pid;even _fd=s->si_fd;even_pid=s->si_pid;printf ("process:%d,has motified something from fd:%d \ n ", even_pid,even_fd);} Int main () {struct sigaction act_d,act_m;int fd1,fd2;pid_t pid;pid=fork (); if (pid<0) {printf ("error\n"); exit (1);} Else if (pid==0) {//childfd1=open ("/home/xiaozhi", o_rdonly), if (fd1==-1) {printf ("Open /home/xiaozhi  fail\n ");} Fd2=open ("/home/rz", o_rdonly), if (fd2==-1) {printf ("open /home/rz fail\n");} Act_m.sa_sigaction=handler_m;sIgemptyset (&act_m.sa_mask); act_m.sa_flags=sa_siginfo;sigaction (sigrtmin+2,&act_m,null); Fcntl (fd1,F_ setsig,sigrtmin+2); Fcntl (fd1,f_notify,dn_modify| Dn_multishot); Fcntl (fd2,f_setsig,sigrtmin+2); Fcntl (fd2,f_notify,dn_modify| Dn_multishot); while (1) {pause ();}} Else{//fatherfd1=open ("/home/xiaozhi", o_rdonly), if (fd1==-1) {printf ("open /home/xiaozhi fail\n");} Fd2=open ("/home/rz", o_rdonly), if (fd2==-1)     {printf ("open /home/rz fail\n");} Act_d.sa_sigaction=handler_d;sigemptyset (&act_d.sa_mask); Act_d.sa_flags=sa_siginfo;sigaction (SIGRTMIN+1, &act_d,null); Fcntl (fd1,f_setsig,sigrtmin+1); Fcntl (fd1,f_notify,dn_modify| Dn_multishot); Fcntl (fd2,f_setsig,sigrtmin+1); Fcntl (fd2,f_notify,dn_modify| Dn_multishot); while (1) {pause ();}}}


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1793269

Detect create or delete files report: Signal processing mechanism

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.