码迷,mamicode.com
首页 > 系统相关 > 详细

linux c 监控目录

时间:2017-11-07 16:23:12      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:inotify   led   iterator   ctr   work   nal   strcmp   tac   pre   

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>

linux c 监控目录

标签:inotify   led   iterator   ctr   work   nal   strcmp   tac   pre   

原文地址:http://www.cnblogs.com/wangjian8888/p/7799264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!