linux c 监控目录

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>

转载于:https://www.cnblogs.com/wangjian8888/p/7799264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值