版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、学号:3011216010 姓名:罗壹文 学院:计算机科学与技术操作系统实验2(1) 实验目的完成一个监视指定的若干目录变动情况的(服务)程序。(2) 实验内容利用inotify机制进行文件或文件夹的监控。(3) 实验结果fileMonitor为程序名称,源码如下:#include <sys/inotify.h>#include <errno.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include &l
2、t;sys/stat.h>#include <signal.h>#include <syslog.h>#include <fcntl.h>#include <sys/resource.h>#include <errno.h>/#define _DEBUG#define _DAEMON#define EVENT_NUM 16#define BUF_SIZE 10240#ifdef _DAEMONchar * fileConf = "/etc/fileMonitor.conf"char * fileScan =
3、"/etc/fileMonitor.scan"char * fileLog = "/etc/fileMonitor.log"#elsechar * fileConf = "fileMonitor.conf"char * fileScan = "fileMonitor.scan"char * fileLog = "fileMonitor.log"#endifstruct wd_nameint wd;char * name;int wd_NUM = 0;struct wd_name * wd_arr
4、ay;/struct stat buf;/ for detimine the type of file.const int filenameLen = 500;const int filelistLen = 10000;char files10000500;char bufferBUF_SIZE;char logmsg500;FILE * fplog;int fd, wd;char *offset = NULL;struct inotify_event *event;int len, tmp_len;char strbuf16;int i = 0;int m_argc;char *m_argv
5、;int sleeptime=1;char * event_array="File was accessed","File was modified","File attributes were changed","writtable file closed","Unwrittable file closed","File was opened","File was moved from X","File was moved to Y"
6、,"Subfile was created","Subfile was deleted","Self was deleted","Self was moved","","Backing fs was unmounted","Event queued overflowed","File was ignored"int init(int argc, char *argv);int readConfig(char * filename);in
7、t resetConfig(char * filename);int readScan(char * filename, int cur);int parseCmd(char * filename,int argc, char *argv);int execCmd(char* dirs, char* f_files, char* filetypes, int dsize,int fsize, int tsize, char * filename, int flag);/ no recursivelyint execCmdN(char* dirs,int dsize, char * filena
8、me,int cur);/ recursivelyint execCmdR(char* dirs, int dsize, char * filename,int cur);/ with file typeint execCmdT(char* dirs, char* filetypes, int dsize, int tsize, char * filename,int cur);/ only filesint execCmdF(char* f_files,int fsize,int cur);char * getTime()time_t now; struct tm *timenow; cha
9、r strtemp500; time(&now); timenow = (struct tm *)localtime(&now); return (char*)asctime(timenow); /printf("recent time is : %s n", asctime(timenow); /for selectint isready(int fd) int rc; fd_set fds; struct timeval tv; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = tv.tv_usec
10、 = 0; rc = select(fd+1, &fds, NULL, NULL, &tv); if( rc<0 ) /error exit(-1);return FD_ISSET(fd, &fds) ? 1: 0;/for daemon#define LOCKFILE "/var/run/fileMonitor.pid" #define LOCKMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) int daemonize(const char *cmd) int i,fd0,fd1,fd2; pid_t
11、 pid; struct rlimit r1; struct sigaction sa; umask(0); /Clear file creation mask if(getrlimit(RLIMIT_NOFILE,&r1) < 0) printf("getrlimit error.n"); exit(1); if(pid = fork() < 0 ) printf("fork1 error.n"); exit(2); else if(pid > 0)/Parent exit(0); setsid(); sa.sa_handle
12、r = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0;if(sigaction(SIGHUP,&sa,NULL)<0) printf("sigaction error.n"); exit(3); if(pid=fork() < 0) printf("fork2 error.n"); exit(2); else if(pid > 0) exit(0); if(chdir("/") < 0) printf("chdir error.n
13、"); exit(4); /close(0); /* close stdin */ / for file.scan and file.conf/close(1); /* close stdout */ / for file.scan and file.conf/close(2); /* close stderr */ openlog(cmd,LOG_PID, LOG_USER); syslog(LOG_INFO,"fileMonitor Startn");int lockfile(int fd) struct flock f1; f1.l_type = F_WRL
14、CK; f1.l_start = 0; f1.l_whence = SEEK_SET; f1.l_len = 0; return(fcntl(fd,F_SETLK,&f1); int already_running(void) int fd; char buf16; fd = open(LOCKFILE,O_RDWR|O_CREAT,LOCKMODE); if(fd < 0) printf("Can't Open %s:%sn",LOCKFILE,strerror(errno); syslog(LOG_INFO,"Can't Open
15、 %s:%s",LOCKFILE,strerror(errno); exit(1); if(lockfile(fd) < 0) if(errno = EACCES | errno = EAGAIN) close(fd); return 1; printf("Can't lock %s:%sn",LOCKFILE,strerror(errno); syslog(LOG_INFO,"Can't lock %s:%s",LOCKFILE,strerror(errno); exit(1); ftruncate(fd,0); spr
16、intf(buf,"%ld",(long)getpid(); write(fd,buf,strlen(buf); return 0; void sigterm(int signo) syslog(LOG_INFO,"got SIGTERM,EXIT"); fclose(fplog); closelog(); exit(0); void reread(void) syslog(LOG_INFO,"Re-read configuration file ok"); init(m_argc, m_argv); void sighup(int
17、signo) syslog(LOG_INFO,"Re-Reading configuration file"); reread(); int main(int argc, char *argv)#ifdef _DAEMONstruct sigaction sa; daemonize(argv0); if(already_running() printf("fileMonitor already runningn"); syslog(LOG_INFO,"fileMonitor already runningn"); exit(2); s
18、a.sa_handler = sigterm; sigemptyset(&sa.sa_mask); sigaddset(&sa.sa_mask,SIGTERM); sa.sa_flags = 0; if(sigaction(SIGTERM,&sa,NULL) < 0) syslog(LOG_INFO,"Can't Catch SIGTERM:%s",strerror(errno); exit(1); sa.sa_handler = sighup; sigemptyset(&sa.sa_mask); sigaddset(&
19、sa.sa_mask,SIGHUP); sa.sa_flags = 0; if(sigaction(SIGHUP,&sa,NULL) < 0) syslog(LOG_INFO,"Can't catch SIGHUP:%s",strerror(errno); exit(1); #endifm_argc = argc;m_argv = argv;fd = inotify_init();if (fd < 0) #ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"Fail to initiali
20、ze inotify.n");syslog(LOG_INFO,logmsg);#elseprintf("Fail to initialize inotify.n");#endifexit(-1);fplog = fopen(fileLog,"wb");init(m_argc, m_argv);memset(logmsg,0,500);while(1)if (isready(fd) if(len = read(fd, buffer, BUF_SIZE) offset = buffer;#ifdef _DAEMONmemset(logmsg,0,5
21、00);sprintf(logmsg,"%stSome event happens, len = %d.n",getTime(), len);syslog(LOG_INFO,logmsg);#elsememset(logmsg,0,500);sprintf(logmsg,"%stSome event happens, len = %d.n",getTime(), len);fwrite(logmsg, 1 , sizeof(logmsg),fplog);#endif#ifdef _DEBUGprintf("%stSome event happe
22、ns, len = %d.n",getTime(), len);#endifevent = (struct inotify_event *)buffer;while (char *)event - buffer) < len)if (event->mask & IN_ISDIR)strcpy(strbuf, "Direcotory");elsestrcpy(strbuf, "File");#ifdef _DEBUG#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"
23、tObject type: %sn", strbuf);syslog(LOG_INFO,logmsg);#elsememset(logmsg,0,500);sprintf(logmsg,"tObject type: %sn", strbuf);fwrite(logmsg, 1 , sizeof(logmsg),fplog);printf("tObject type: %sn", strbuf);#endif#endiffor (i=0; i<wd_NUM; i+)if (event->wd != wd_arrayi.wd) cont
24、inue;#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"tObject name: %sn", wd_);syslog(LOG_INFO,logmsg);#elsememset(logmsg,0,500);sprintf(logmsg,"tObject name: %sn", wd_);fwrite(logmsg, 1 , sizeof(logmsg),fplog);#ifdef _DEBUGprintf("tObject name: %sn&q
25、uot;, wd_);#endif#endifbreak;#ifdef _DEBUG#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"tEvent mask: %08Xn", event->mask);syslog(LOG_INFO,logmsg);#elsememset(logmsg,0,500);sprintf(logmsg,"tEvent mask: %08Xn", event->mask);fwrite(logmsg, 1 , sizeof(logmsg),fp
26、log);printf("tEvent mask: %08Xn", event->mask);#endif#endiffor (i=0; i<EVENT_NUM; i+)if (event_arrayi0 = '0') continue;if (event->mask & (1<<i)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"tEvent: %sn", event_arrayi);syslog(LOG_INFO,logmsg);#elseme
27、mset(logmsg,0,500);sprintf(logmsg,"tEvent: %sn", event_arrayi);fwrite(logmsg, 1 , sizeof(logmsg),fplog);#ifdef _DEBUGprintf("tEvent: %sn", event_arrayi);#endif#endiftmp_len = sizeof(struct inotify_event) + event->len;event = (struct inotify_event *)(offset + tmp_len); offset +
28、= tmp_len;fflush(fplog);#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"tEsleep time %dr",sleeptime);syslog(LOG_INFO,logmsg);#elseprintf("sleep time %dr", sleeptime);fflush(stdout);#endiffflush(stdout);init(m_argc, m_argv);read(fd, buffer, BUF_SIZE);sleep(sleeptime);return 0;/
29、 read the configure from file.confint readConfig(char * filename)FILE * fp;fp = fopen(filename,"r");if(fp = NULL)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"%s created.n", filename);syslog(LOG_INFO,logmsg);#elseprintf("No %s exists.n", filename);printf("%s c
30、reated.n", filename);#endifresetConfig(filename);fp = fopen(filename,"r");if(fp = NULL)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"%s can not read.n", filename);syslog(LOG_INFO,logmsg);#elseprintf("%s can not read.n", filename);#endifexit(-1);int cur_file =
31、0;int cur_dir = 0;int cur_type = 0;char *f_dirs500;/ for record the files /so the max dirs cmd list len is 500char *fileTypes500;/ for record the file type / so the max fileType cmd list len is 500char *f_files500;/ for record the files / so the max fileType cmd list len is 500char * p;int i;int rfl
32、ag = 0;int type = 0;int ret = 0;while(!feof(fp) && type < 4)fgets(buffer,filenameLen, fp);if(buffer0 = '#')#ifdef _DEBUG #ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"%d %s",strlen(buffer),buffer);syslog(LOG_INFO,logmsg);#elseprintf("%d %s",strlen(buffer),
33、buffer);fflush(stdout);#endif#endifcontinue;if(0 = strcmp(buffer, "DIRECTORYn")type+;fgets(buffer,filenameLen, fp);if(strlen(buffer) > 1)while(!feof(fp) && buffer0 != '#' && 0!= strcmp(buffer,"FILEn")#ifdef _DEBUG #ifdef _DAEMONmemset(logmsg,0,500);spri
34、ntf(logmsg,"%d %s",strlen(buffer),buffer);syslog(LOG_INFO,logmsg);#elseprintf("%d %s",strlen(buffer),buffer);fflush(stdout);#endif#endifif(strlen(buffer) > 1)int len = strlen(buffer);bufferlen-1='0'f_dirscur_dir = (char *) malloc(sizeof(char)*500);strcpy(f_dirscur_dir,
35、 buffer); / save typecur_dir+;fgets(buffer,filenameLen, fp);else if(0 = strcmp(buffer, "FILEn")type+;fgets(buffer,filenameLen, fp);if(strlen(buffer) > 1)while(!feof(fp) && buffer0 != '#' && 0!= strcmp(buffer,"FILE_TYPEn")#ifdef _DEBUG #ifdef _DAEMONmems
36、et(logmsg,0,500);sprintf(logmsg,"%d %s",strlen(buffer),buffer);syslog(LOG_INFO,logmsg);#elseprintf("%d %s",strlen(buffer),buffer);fflush(stdout);#endif#endifif(strlen(buffer) > 1)int len = strlen(buffer);bufferlen-1='0'f_filescur_file = (char *) malloc(sizeof(char)*500
37、);strcpy(f_filescur_file, buffer); / save typecur_file+;fgets(buffer,filenameLen, fp);else if(0 = strcmp(buffer, "FILE_TYPEn")type+;fgets(buffer,filenameLen, fp);if(strlen(buffer) > 1)while(!feof(fp) && buffer0 != '#' && 0!= strcmp(buffer,"Timen")#ifdef
38、 _DEBUG #ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"%d %s",strlen(buffer),buffer);syslog(LOG_INFO,logmsg);#elseprintf("%d %s",strlen(buffer),buffer);fflush(stdout);#endif#endifif(strlen(buffer) > 1)int len = strlen(buffer);bufferlen-1='0'fileTypescur_type = (cha
39、r*)malloc(sizeof(char) * 500);strcpy(fileTypescur_type,buffer);/ save itcur_type+;fgets(buffer,filenameLen, fp);else if(0 = strcmp(buffer, "Timen")type+;fgets(buffer,filenameLen, fp);if(strlen(buffer) > 1)while(!feof(fp) && buffer0 != '#')#ifdef _DEBUG #ifdef _DAEMONmems
40、et(logmsg,0,500);sprintf(logmsg,"%d %s",strlen(buffer),buffer);syslog(LOG_INFO,logmsg);#elseprintf("%d %s",strlen(buffer),buffer);fflush(stdout);#endif#endifif(strlen(buffer) > 1)int len = strlen(buffer);/bufferlen-1='0'sleeptime = atoi(buffer);fgets(buffer,filenameLen
41、, fp);if(type != 4)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"%s is broken. Reset the configure.n", filename);syslog(LOG_INFO,logmsg);resetConfig(filename);memset(logmsg,0,500);sprintf(logmsg,"%s is read again.n", filename);syslog(LOG_INFO,logmsg);return readConfig(filena
42、me);#elseprintf("%s is broken. Reset the configure.n", filename);resetConfig(filename);printf("%s is read again.n", filename);return readConfig(filename);#endifif(cur_dir + cur_file = 0 | (cur_dir =0 && cur_type != 0)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"
43、;%s is not configured correctly. Set the configure please.n", filename);syslog(LOG_INFO,logmsg);#elseprintf("%s is not configured correctly. Set the configure please.n", filename);#endif/resetConfig(filename);exit(-1);#ifdef _DEBUG#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,&quo
44、t;%d lines read %d types.n",cur_dir +cur_type + cur_file, cur_type);syslog(LOG_INFO,logmsg);#elseprintf("%d lines read %d types.n",cur_dir +cur_type + cur_file, cur_type);#endif#endiffclose(fp);#ifdef _DEBUG#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"Here is readConfig fun
45、ction. n");syslog(LOG_INFO,logmsg);#elseprintf("Here is readConfig function. n");fflush(stdout);#endif#endif/ default is with recursivelyret = execCmd(f_dirs, f_files, fileTypes, cur_dir, cur_file, cur_type, fileScan, 1);#ifdef _DEBUG#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,&
46、quot;Here is readConfig function. ret %dn",ret);syslog(LOG_INFO,logmsg);#elseprintf("Here is readConfig function. ret %dn",ret);fflush(stdout);#endif#endiffor(i = 0; i < cur_dir; i+)free(f_dirsi);for(i = 0; i < cur_type; i+)free(fileTypesi);return ret;/ reset the file.conf if er
47、rorint resetConfig(char * filename)FILE * fp;fp = fopen(filename,"w");if(fp = NULL)#ifdef _DAEMONmemset(logmsg,0,500);sprintf(logmsg,"Can not open %sn", filename);syslog(LOG_INFO,logmsg);#elseprintf("Can not open %sn", filename);#endifexit(-1);fputs("#This is the configure file for fileMonitor.n", fp);fputs("#DO NOT CHANGE the structure of this file.n", fp);fputs("#In this place you can add the directories to be monitored.n", fp);fputs("#Be aware that there is no space in front of each line.n", fp);fputs(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《预应力钢筒混凝土管无损检测(远场涡流电磁法)技术要求gbt+41055-2021》详细解读
- 7.1 浓浓亲情相伴一生(课件)-【中职专用】中职思想政治《心理健康与职业生涯》(高教版2023·基础模块)
- 第7课 珍视亲情 学会感恩(课件+视频)-【中职专用】高一思想政治《心理健康与职业生涯》(高教版2023基础模块)
- 藏象-五脏-中医科培训课件
- 今年教师节上的精彩讲话稿(11篇)
- 2024年度医疗科研合作协议模板范文大全 1
- 2022年幼儿园校本研修活动总结范文(6篇)
- 关于安全管理的建议书5
- 汽车专业:汽车发动机总体构造和工作原理试题预测五
- 汽车专业:点火系统考试题库(题库版)
- 做新闻:现实的社会建构
- AutoCAD建筑制图实用教程期末试卷二含答案
- 【word电子板报】我的家乡
- DB37-T 4649.3-20233智能制造++第3部分:数字化车间建设指南-地方标准
- 10以内的加减法练习 自动生成器
- 语音短信业务整体推广方案
- 变革的基因读后感
- 邮票的张数名师课件公开课一等奖课件省赛课获奖课件
- 办公用品、耗材采购服务投标方案
- 二年级下册科学《春夏秋冬》 大象版 公开课一等奖课件省课获奖课件
- 冠能魅影dm630豪华版说明书
评论
0/150
提交评论