static void* thread_monitor(void* args)
{
pthread_detach(pthread_self());
int fd;
int wd;
int len;
int nread;
char buf[BUFSIZ];
char cmd[256] = {0};
struct inotify_event *event;
fd = inotify_init();
wd = inotify_add_watch(fd,cups_backend, IN_DELETE|IN_MOVED_TO|IN_MOVED_FROM|IN_CREATE);
if(wd < 0)
{
//LOG_ERR("inotify_add_watch failed\n");
close(fd);
return NULL;
}
while((len = read(fd,buf,sizeof(buf) - 1 )) > 0)
{
nread = 0;
while(len > 0)
{
event = (struct inotify_event *)&buf[nread];
if((event->mask & IN_DELETE) && strcmp("hook_backend",event->name) ==0)
{
memset(cmd,0,sizeof(cmd));
snprintf(cmd,sizeof(cmd),"cp /opt/CEMS/bin/hook_backend %s",cups_backend);
system(cmd);
//chmod(full_hook_backend,0500);
}
std::set<std::string>::iterator iter;
iter = device_list.find(event->name);
std::set<std::string>::const_iterator it;
for(it=device_list.begin();it!=device_list.end();it++)
{
//printAudit_log_run_info((*it).c_str());
//printAudit_log_run_info(event->name);
}
if(((event->mask & IN_DELETE)||(event->mask & IN_MOVED_FROM)||(event->mask & IN_CREATE) )&& iter!=device_list.end())
{
memset(cmd,0,sizeof(cmd));
snprintf(cmd,sizeof(cmd),"%s%s",cups_backend,event->name);
symlink(full_hook_backend,cmd);
}
if((event->mask & IN_MOVED_TO) || ((event->mask & IN_CREATE)))
{
//get_backend_info();
std::set<std::string>::iterator it;
deal_backend();
// it = device_list.find(event->name);
// if(it!= device_list.end())
// {
// deal_backend();
// }
}
nread = nread + sizeof(struct inotify_event) + event->len;
len = len - sizeof(struct inotify_event) - event->len;
}
}
inotify_rm_watch(fd,wd);
close(fd);
return NULL;
}
创建线程
bool printer_ctrl_worker()
{
printer_ctrl_init();
if(pid_monitor == 0)
{
if(pthread_create(&pid_monitor,NULL,thread_monitor,NULL))
{
pid_monitor = 0;
}
}
if(!check_hook_backend())
{
//chmod(full_hook_backend,0500);
}
return true;
}
#include <stdlib.h>
#include <vector>
#include <string>
#include <set>
#include <functional>
#include <sys/inotify.h>
#include <string.h>
#include <dirent.h>
#include <list>
#include <sys/stat.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>