使用daemon和inotify在C中创建一个单文件监视器
我正在使用守护进程和inotify来监视文件。我不知道我的代码怎么了。如果我使用inotify_add_watch来监视目录,它会工作。但它不适用于单个文件。如果你有任何想法,请帮助我。我认为问题出在read()部分。系统似乎无法将文件读入缓冲区。但我不太确定。蒂亚使用daemon和inotify在C中创建一个单文件监视器,c,linux,events,daemon,inotify,C,Linux,Events,Daemon,Inotify,我正在使用守护进程和inotify来监视文件。我不知道我的代码怎么了。如果我使用inotify_add_watch来监视目录,它会工作。但它不适用于单个文件。如果你有任何想法,请帮助我。我认为问题出在read()部分。系统似乎无法将文件读入缓冲区。但我不太确定。蒂亚 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <errno.h>
#include <linux/inotify.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义事件大小(sizeof(结构索引事件))
#定义事件大小(1024*(事件大小+16))
静态void骨架_守护进程()
{
pid\u t pid;
/*离开父进程*/
pid=fork();
/*发生了一个错误*/
if(pid<0){
//printf(“pid0母公司终止”);
退出(退出成功);
}
/*成功时:子进程成为会话负责人*/
if(setsid()<0){
//printf(“设置ID 0”);
退出(退出成功);
}
/*设置新文件权限*/
乌马斯克(0);
/*将工作目录更改为根目录*/
/*或另一个适当的目录*/
chdir(“/”);
/*关闭所有打开的文件描述符*/
int x;
对于(x=sysconf(\u SC\u OPEN\u MAX);x>0;x--)
{
关闭(x);
}
/*打开日志文件*/
openlog(“firstdaemon”,LOG\u-PID,LOG\u-DAEMON);
}
int main(int argc,char*argv[])
{
int-length,i=0;
int-fd;
int-wd;
字符缓冲区[EVENT_BUF_LEN];
骨架_守护进程();
//printf(“守护进程后的argv[]是%s”,argv[1]);
syslog(LOG_注意,“------------第一个测试守护进程已启动。-----------”);
/*创建INOTIFY实例*/
fd=inotify_init();
/*检查错误*/
如果(fd<0){
perror(“inotify_init”);
}
/*将目录添加到监视列表中*/
wd=inotify_add_watch(fd,/home/temp/test),IN_ALL_EVENTS/*IN_IGNORED | IN|u OPEN | IN|u CLOSE | IN|u ATTRIB | IN|u CREATE | IN| IN|u CLOSE | IN|修改| IN| IN|u ACCESS*/);
如果(wdlen){
如果(忽略事件->屏蔽和输入){
inotify_rm_手表(fd、wd);
i在所有事件中添加手表(fd,“/home/test”);
}
如果(事件->掩码和属性中){
if(事件->掩码和IN_ISDIR){
//printf(“目录%s属性。\n”,事件->名称);
syslog(日志通知,“目录:chaningattrib.”);
syslog(日志通知,“目录:%s ATTRIB.”,事件->名称);
}
否则{
syslog(日志通知,“目录:chaningattrib.”);
系统日志(日志通知,“文件:%s ATTRIB.”,事件->名称);
}
}
如果(事件->屏蔽和修改){
if(事件->掩码和IN_ISDIR){
//printf(“目录%s已修改。\n”,事件->名称);
系统日志(日志通知,“目录:更改!!!!!!!”);
系统日志(日志通知,“文件:%s修改”,事件->名称);
}
否则{
系统日志(日志通知,“文件修改:更改!!!”);
系统日志(日志通知,“文件:%s修改”,事件->名称);
}
}
如果(事件->屏蔽和写入关闭){
if(事件->掩码和IN_ISDIR){
//printf(“目录%s已修改。\n”,事件->名称);
系统日志(日志通知,“CLSOE写入!!!!!!!”);
//系统日志(日志通知,“文件:%s修改”,事件->名称);
}
否则{
//系统日志(日志通知,“文件修改:更改!!!”);
系统日志(日志通知,“CLSOE写入!!!!!!!”);
//系统日志(日志通知,“文件:%s修改”,事件->名称);
}
}
如果(事件->屏蔽和打开){
if(事件->掩码和IN_ISDIR){
//printf(“新目录%s已打开。\n”,事件->名称);
系统日志(日志通知,“目录:%s打开”,事件->名称);
}
否则{
系统日志(日志通知,“文件:打开”);
系统日志(日志通知,“文件:%s打开”,事件->名称);
//printf(“新文件%s打开。\n”,事件->名称);
}
}
如果(事件->掩码和创建中){
if(事件->掩码和IN_ISDIR){
//printf(“新目录%s已创建。\n”,事件->名称);
系统日志(日志通知,“新目录:%s已创建”,事件->名称);
}
否则{
系统日志(日志通知,“新文件:%s已创建”,事件->名称);
//printf(“新文件%s已创建。\n”,事件->名称);
}
}
else if(事件->屏蔽和删除中){
if(事件->掩码和IN_ISDIR){
系统日志(日志通知,“目录:%s已删除”,事件->名称);
//printf(“目录%s已删除。\n”,事件->名称);
}
否则{
系统日志(日志通知,“文件:%s已删除”,事件->名称);
//printf(“文件%s已删除。\n”,事件->名称);
}
}
else if(事件->掩码和IN_访问){
if(事件->掩码和IN_ISDIR){
系统日志(日志通知,“目录:%s已被访问。”,事件->名称);
//printf(“已访问目录%s。\n”,事件->名称);
}
否则{
系统日志(日志通知,“文件:%s已被访问。”,事件->名称);
//printf(“已访问文件%s。\n”,事件->名称);
}
}
如果(事件->屏蔽和关闭){
if(事件->掩码和IN_ISDIR){
//printf(“新目录%s已创建。\n”,事件->名称);
系统日志(日志通知,“目录:%s已关闭”,事件->名称);
}
否则{
系统日志(日志通知,“文件:%s已关闭”,事件->名称);
//printf(“新文件%s已创建。\n”,事件->名称);
}
}
}
i+=事件i
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0){
//printf("pid<0 error");
exit(EXIT_FAILURE);
}
/* Success: Let the parent terminate */
if (pid > 0){
//printf("pid>0 parent terminate");
exit(EXIT_SUCCESS);
}
/* On success: The child process becomes session leader */
if (setsid() < 0){
//printf("setsid <0");
exit(EXIT_FAILURE);
}
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0){
//printf("pid<0");
exit(EXIT_FAILURE);
}
/* Success: Let the parent terminate */
if (pid > 0){
//printf("pid > 0 ");
exit(EXIT_SUCCESS);
}
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");
/* Close all open file descriptors */
int x;
for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
{
close (x);
}
/* Open the log file */
openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
int length, i = 0;
int fd;
int wd;
char buffer[EVENT_BUF_LEN];
skeleton_daemon();
//printf("the argv[] after daemon is %s",argv[1]);
syslog (LOG_NOTICE, "------------ First testing daemon started.----------");
/*creating the INOTIFY instance*/
fd = inotify_init();
/*checking for error*/
if ( fd < 0 ) {
perror( "inotify_init" );
}
/*adding the directory into watch list. */
wd = inotify_add_watch( fd, "/home/temp/test" , IN_ALL_EVENTS /* IN_IGNORED | IN_OPEN | IN_CLOSE_WRITE | IN_ATTRIB | IN_CREATE | IN_DELETE |IN_CLOSE | IN_MODIFY | IN_ACCESS */ );
if(wd<0){
syslog(LOG_NOTICE, "wd < 0");
perror("inotify_add_watch");
}
//syslog(LOG_NOTICE, "THE FILE WE ARE MONITORING IS: %s",argv[1]);
while (1)
{
//TODO: Insert daemon code here.
syslog (LOG_NOTICE, "~~~~~~~ One new testing daemon started.~~~~~~~~~");
/*read to determine the event change happens on directory. Actually this read blocks until the change event occurs*/
syslog (LOG_NOTICE, "BEFORE READ IN BUFFER");
length = read( fd, buffer, EVENT_BUF_LEN );
syslog (LOG_NOTICE, "AFTER READ IN BUFFER.");
/*checking for error*/
if ( length < 0 ) {
perror( "read" );
}else if(length == 0){
syslog(LOG_NOTICE," length =0 " );
continue;
}
/*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if (event->mask & IN_IGNORED) {
inotify_rm_watch(fd,wd);
inotify_add_watch(fd,"/home/test", IN_ALL_EVENTS );
}
if ( event->mask & IN_ATTRIB ) {
if ( event->mask & IN_ISDIR ) {
//printf( " directory %s ATTRIB .\n", event->name );
syslog (LOG_NOTICE, " directory:chaning ATTRIB .");
syslog (LOG_NOTICE, " directory:%s ATTRIB .", event->name);
}
else {
syslog (LOG_NOTICE, " directory:chaning ATTRIB .");
syslog (LOG_NOTICE, " file:%s ATTRIB .", event->name);
}
}
if ( event->mask & IN_MODIFY ) {
if ( event->mask & IN_ISDIR ) {
//printf( " directory %s modified .\n", event->name );
syslog (LOG_NOTICE, " dirtory:chaning !!!!!!! .");
syslog (LOG_NOTICE, " file:%s modify.", event->name);
}
else {
syslog (LOG_NOTICE, " file modify:chaning!!! .");
syslog (LOG_NOTICE, " file:%s modify.", event->name);
}
}
if ( event->mask & IN_CLOSE_WRITE ) {
if ( event->mask & IN_ISDIR ) {
//printf( " directory %s modified .\n", event->name );
syslog (LOG_NOTICE, " CLSOE WRITE !!!!!!! .");
//syslog (LOG_NOTICE, " file:%s modify.", event->name);
}
else {
//syslog (LOG_NOTICE, " file modify:changing!!! .");
syslog (LOG_NOTICE, " CLSOE WRITE !!!!!!! .");
//syslog (LOG_NOTICE, " file:%s modify.", event->name);
}
}
if ( event->mask & IN_OPEN ) {
if ( event->mask & IN_ISDIR ) {
//printf( "New directory %s opened.\n", event->name );
syslog (LOG_NOTICE, " directory:%s open.", event->name);
}
else {
syslog (LOG_NOTICE, " file:opening.");
syslog (LOG_NOTICE, " file:%s open.", event->name);
//printf( "New file %s open.\n", event->name );
}
}
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
//printf( "New directory %s created.\n", event->name );
syslog (LOG_NOTICE, "new directory:%s created.", event->name);
}
else {
syslog (LOG_NOTICE, "new file:%s created.", event->name);
//printf( "New file %s created.\n", event->name );
}
}
else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
syslog (LOG_NOTICE, "dir:%s is deleted.", event->name);
//printf( "Directory %s deleted.\n", event->name );
}
else {
syslog (LOG_NOTICE, "file:%s is deleted.", event->name);
//printf( "File %s deleted.\n", event->name );
}
}
else if ( event->mask & IN_ACCESS ) {
if ( event->mask & IN_ISDIR ) {
syslog(LOG_NOTICE, "dir:%s is accessed.", event->name);
//printf( "The directory %s was accessed.\n", event->name );
}
else {
syslog(LOG_NOTICE, "file:%s is accessed.", event->name);
//printf( "The file %s was accessed.\n", event->name );
}
}
if ( event->mask & IN_CLOSE ) {
if ( event->mask & IN_ISDIR ) {
//printf( "New directory %s created.\n", event->name );
syslog (LOG_NOTICE, " directory:%s closed.", event->name);
}
else {
syslog (LOG_NOTICE, " file:%s closed.", event->name);
//printf( "New file %s created.\n", event->name );
}
}
}
i += EVENT_SIZE + event->len;
}
i=0;
syslog (LOG_NOTICE, "~~~~~~one testing daemon ended.~~~~~~~~~~");
sleep (20);
}
/*removing the directory from the watch list.*/
inotify_rm_watch( fd, wd );
/*closing the INOTIFY instance*/
close( fd );
syslog (LOG_NOTICE, "--------------- first testing daemon terminated.----------------");
closelog();
return EXIT_SUCCESS;
if ( event->len )