/*debug.h*/
#ifndef DEBUG_H_
#define DEBUG_H_
#define MAX_EPOLL_FD (1024)
typedef struct debug_file{
int fd;
char *name;
int epoll_fd;
int inotify_fd;
}debug_file_t;
#endif
/*debug.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/queue.h>
#include <sys/inotify.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <limits.h>
#include <errno.h>
#include "debug.h"
#define INOTIFY_BUFF_LEN (1024 * (sizeof(struct inotify_event) + NAME_MAX + 1))
debug_file_t g_debug_file = {-1, "control", -1, -1};
int debug_epoll_init()
{
int ret = 0;
struct epoll_event ep_event;
g_debug_file.epoll_fd = epoll_create(MAX_EPOLL_FD);
if(g_debug_file.epoll_fd < 0)
{
printf("epoll_create() error\n");
return -1;
}
ep_event.events = EPOLL_IN;
ep_event.data.fd = g_debug_file.inotify_fd;
ret = epoll_ctl(g_debug_file.epoll_fd, EPOLL_CTL_ADD, g_debug_file.inotify_fd, &ep_event);
if(ret != 0)
{
printf("epoll_ctl error\n");
return -1;
}
return ret;
}
int debug_handle_init()
{
int ret = 0;
int wd, inotify_fd;
g_debug_file.fd = open(g_debug_file.name, O_CREAT|O_RDWR, 0777);
if(g_debug_file.fd < 0)
{
printf("can't create file %s\n", g_debug_file.name);
return -1;
}
inotify_fd = inotify_init();
if(inotify_fd == -1)
{
printf("inotify_init() error\n");
return -1;
}
g_debug_file.inotify_fd = inotify_fd;
wd = inotify_add_watch(inotify_fd, g_debug_file.name, IN_MODIFY);
if(wd == -1)
{
printf("inotify_add_watch error\n");
return -1;
}
ret = debug_epoll_init();
if(ret == -1)
{
printf("debug_epoll_init error\n");
return -1;
}
return ret;
}
void debug_callback()
{
int index;
int len, rlen;
char buf[INOTIFY_BUFF_LEN] = {0};
struct inotify_event *event;
char ctl_msg[32] = {0};
char *p;
len = read(g_debug_file.inotify_fd, buf, INOTIFY_BUFF_LEN);
if(len == 0)
{
printf("read from inotify_fd returned 0\n");
return;
}
else if(len == -1)
{
printf("read error\n");
return;
}
printf("len=%d, read success\n", len);
for(p=buf; p<buf+len; p+=(sizeof(struct inotify_event)+event->len))
{
event = (struct inotify_event*)p;
if(event->mask & IN_MODIFY)
{
rlen = read(g_debug_file.fd, ctl_msg, 32);
if(rlen<=0)
{
printf("rlen is %d, errno %d, errmsg %s\n", rlen, errno, strerror(errno));
continue;
}
printf("rlen is %d, event len = %d, read file success\n", rlen, event->len);
index = atoi(ctl_msg);
ftruncate(g_debug_file.fd, 0);
printf("%d\n", index);
}
}
return;
}
void debug_response_handle()
{
#define MAX_EPOLL_EVENT_ONE_TIMES 10
struct epoll_event events[MAX_EPOLL_EVENT_ONE_TIMES];
int event_cnt = 0;
int ret = debug_handle_init();
if(ret != 0)
{
printf("debug_hanle_init() error\n");
return;
}
while(1)
{
event_cnt = epoll_wait(g_debug_file.epoll_fd, events, MAX_EPOLL_EVENT_ONE_TIMES, -1);
if(event_cnt<=0)
{
continue;
}
int i;
for(i=0; i<event_cnt; i++)
{
if(0!=(events[i].data.fd == g_debug_file.inotify_fd))
{
printf("epoll_wait error\n");
}
else
{
if(events[i].data.fd == g_debug_file.inotify_fd)
{
debug_callback();
}
}
}
}
return;
}
int main()
{
debug_response_handle();
return 0;
}
通过内核inotify机制向内核程序传输命令
最新推荐文章于 2021-05-04 16:31:43 发布