avast病毒库更新程序的改进
avast病毒库更新程序为avast-update ,这个东东实际上是调用了wget 来下载病毒库的,但是运行以后没有任何提示,病毒库到底有没有在下载呢?不清楚。于是写了个东东来以便可以直观的看到是否在下载以及下载速度的快慢(根据旋转速度)。
/opt/avast4workstation-1.3.0/bin/avast-update 是 avast-update 的路径。
这个小程序主要用到了LINUX内核的 inotify 来监控文件变化。
TODO:
增加超时检测(超时自动退出)
增加更新完成后自动退出(这一点应该简单)
/*
* =====================================================================================
*
* Filename: avast-watch.c
*
* Description: 监视avast病毒库更新
*
* Version: 1.0
* Created: 2010年10月13日 13时35分28秒
* Revision: none
* Compiler: gcc
*
* Author: 荒野无灯 (HuangYeWuDeng), admin@ihacklog.com
* Company: hhtc edu
*
* =====================================================================================
*/
#include
#include
#include
#include
#define MAX_BUF_SIZE 4096
/*
struct inotify_event {
int wd; // watch descriptor
uint32_t mask; // mask of events
uint32_t cookie; // unique cookie associating related events ( form rename(2) )
uint32_t len; // size of name field
char name[]; // optional null-terminated name
}
*/
int main()
{
// fd : file discriptor
// wp watch discriptor
int fd, wd;
int len, index;
char buffer[4096];
struct inotify_event *event;
char *path = "/home/hacklog/.avast";
char *prog="/opt/avast4workstation-1.3.0/bin/avast-update";
int progress = 0;
char indicator[]="-\\|/";
FILE *fp=popen(prog,"r");
fd = inotify_init();
if (fd < 0)
{
printf("Failed to initialize inotify.\n");
return 1;
}
wd = inotify_add_watch(fd, path, IN_CLOSE_WRITE | IN_CREATE | IN_MODIFY| IN_DELETE );
if (wd < 0)
{
printf("Can't add watch for %s", path);
return 1;
}
// 当没有事件发生时,fd 处于阻塞状态,read阻塞直到有一个事件发生.
time_t start_time;
time_t end_time;
while (len = read(fd, buffer, MAX_BUF_SIZE))
{
index = 0;
while (index < len)
{
event = (struct inotify_event *) (buffer + index);
// 如果不是我们添加的对.avast目录的监视,就继续 (因为我们可能添加了多个监视的,每个监视都有一个 wd (watch discriptor )
if (event->wd != wd)
continue;
// if (event->mask & IN_OPEN)
// printf("file %s is opened.\n", event->name);
if (event->mask & IN_CREATE)
{
if( strcmp(event->name,"400.vps.new.md5") == 0 )
{
time(&start_time);
}
printf("file %s is created.\n", event->name);
}
if (event->mask & IN_MODIFY)
{
// printf("file %s is modified.\n", event->name);
printf(" %c",indicator[progress]);
fflush(stdout);
printf("\b\b");
progress=( progress + 1 ) % 4;
}
if (event->mask & IN_CLOSE_WRITE)
{
printf("file %s is closed for write.\n", event->name);
}
if (event->mask & IN_DELETE)
printf("file %s is deleted.\n", event->name);
// sizeof(struct inotify_event)+len 为每个inotify_event 的长度
index += sizeof(struct inotify_event) + event->len;
}
}
return 0;
}
参考文档:
inotify — Linux 2.6 内核中的文件系统变化通知机制
linux开发 — 文件监控 inotify
扩展阅读:
linux inotify+rsync+ssh数据时时同步
All Comments (0)