infidel-ITTI/kqueue_file_watch.cpp
2022-09-22 23:41:38 +07:00

127 lines
2.6 KiB
C++

#include <sys/event.h>
#include <cstdio>
#include <fcntl.h>
#include <stdlib.h>
#include <limits.h>
// #include <vector>
// #include <map>
#include <sys/param.h>
// #include <sys/epoll.h>
#include <sys/event.h>
//
//
typedef struct task_list_s {
siginfo_t admin;
pthread_t thread;
pthread_mutex_t queue_cond_lock;
// std::vector<MessageDef *> message_queue;
// std::map<long,timer_elm_t> timer_map;
uint64_t next_timer=UINT_MAX;
/** Replace these shits **/
struct epoll_event *events =NULL;
int nb_fd_epoll=0;
int epoll_fd=-1;
/** kqueque equivalent **/
int nb_events=0;
int sem_fd=-1;
} task_list_t;
int main(int argc, char **argv){
struct kevent event1;
struct kevent event2;
struct kevent tevent;
struct kevent tevent2;
struct epoll_event *events =NULL;
static task_list_t **tasks=NULL;
// printf("%s\n", task_list_t);
// General Pointers
int kq, fd, ret1, ret2;
// File Pointers
int fd1, fd2;
kq = kqueue();
printf("%d number of args\n", argc);
for (int i=1; i <= 2; i++){
printf("==> %s\n", argv[i]);
if (i == 1)
fd1 = open(argv[1], O_RDONLY);
if (i == 2)
fd2 = open(argv[2], O_RDONLY);
}
// fd = open(argv[1], O_RDONLY);
// for (int i=1; i <= 2; i++){
// printf("YOUR FILES \n");
// printf("==> %s\n", argv[i]);
// }
if (kq == -1){
printf("Error kq...\n");
return 0;
}
/* initialzing kevent struct */
EV_SET(
&event1,
fd1,
EVFILT_VNODE,
EV_ADD
|
EV_CLEAR,
NOTE_WRITE,
0,
NULL
);
ret1 = kevent(kq, &event1, 1, NULL, 0, NULL);
EV_SET(
&event2,
fd2,
EVFILT_VNODE,
EV_ADD
|
EV_CLEAR,
NOTE_WRITE,
0,
NULL
);
ret2 = kevent(kq, &event2, 1, NULL, 0, NULL);
if ( ret1 == -1 || ret2 == -1 ){
printf("event failure to attched");
}
for (;;) {
ret1 = kevent(kq, NULL, 0, &tevent, 1, NULL);
ret2 = kevent(kq, NULL, 0, &tevent, 1, NULL);
if (ret1 == -1 || ret2 == -1) {
printf("Waiting.......\n");
} else if ( ret1 > 0 || ret2 > 0 ){
printf("Change detected on file...\n");
printf("--> RET 1 VAL = %d\n", ret1);
printf("--> RET 2 VAL = %d\n", ret2);
}
// printf("--> RET 1 VAL = %d\n", ret1);
// printf("--> RET 2 VAL = %d\n", ret2);
}
}