diff --git a/README.md b/README.md index c7e0347..8753c68 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,33 @@ -# What the fuck is this +# KQUEUE Utilization This is your lame attempt to understand how those Network Virtual Functions sofware works as well to sharpen your understanding on C++ and C programming language. ## Hints -- Compile using `g++` + +- Compile using `g++` for the file watcher + +To trace the function calls on FreeBSD based system use `truss()` instead of `strace`. + +The `kqueue_socket.c` is my attempt on create a socket watcher timer program using `kevent`. + +- ![KQUEUE NETBSD GUIDE](https://wiki.netbsd.org/tutorials/kqueue_tutorial/) + +``` + + struct kevent my_event; + struct kevent my_change; + + EV_SET(&my_change, ...); // Initialize the kevent + + for(;;){ + + ...; + + } + + +``` ## Goal - Understand the difference between c++ and C diff --git a/bin/ktimer b/bin/ktimer new file mode 100755 index 0000000..ccdbd5c Binary files /dev/null and b/bin/ktimer differ diff --git a/bin/test.txt b/bin/test.txt index 7050359..88aae09 100644 --- a/bin/test.txt +++ b/bin/test.txt @@ -4,3 +4,11 @@ aaaaa aaaaa aaaaa aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa +aaaaa diff --git a/kqueue_file_watch.cpp b/kqueue_file_watch.cpp index defb023..a2bbab8 100644 --- a/kqueue_file_watch.cpp +++ b/kqueue_file_watch.cpp @@ -40,7 +40,6 @@ int main(int argc, char **argv){ struct epoll_event *events =NULL; - static task_list_t **tasks=NULL; // printf("%s\n", task_list_t); @@ -64,7 +63,6 @@ int main(int argc, char **argv){ } // fd = open(argv[1], O_RDONLY); - // for (int i=1; i <= 2; i++){ // printf("YOUR FILES \n"); // printf("==> %s\n", argv[i]); diff --git a/kqueue_socket.c b/kqueue_socket.c new file mode 100644 index 0000000..4fc18c6 --- /dev/null +++ b/kqueue_socket.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +struct inf_kevent { + uintptr_t ident; + short filter; + u_short flags; + u_int fflags; + int64_t data; + void *udata; + u_int64_t ext[4]; +}; + +struct addrinfo *add; +struct addrinfo hints; + +void inf_info(const char *s); + +int main(void) { + struct kevent event; + struct kevent change; + pid_t pid; + int kq, nev; + + printf("\n"); + printf("---------------------------------------\n"); + printf("Stupid struct\n"); + printf("---------------------------------------\n"); + printf("\n"); + + + if ((kq = kqueue()) == -1){ + inf_info("kqueue()"); + } + + EV_SET(&change, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 2000, 0); + + for (;;) { + + nev = kevent(kq, &change, 1, &event, 1, NULL); + + printf("--> kq %d\n", kq); + printf("--> pid %d\n", pid); + + if (nev < 0){ + inf_info("kevent()"); + } + + else if (nev > 0) { + + if (event.flags & EV_ERROR) { + fprintf(stderr, "EV_ERROR..."); + exit(EXIT_FAILURE); + } + + if ((pid = fork()) < 0 ) { + fprintf(stderr, "EV_ERROR..."); + exit(EXIT_FAILURE); + } + + else if (pid == 0) { + if (execlp("date", "date", (char *)0) < 0) + inf_info("execplp()"); + } + + } + } + close(kq); +} + +void inf_info(const char *s) +{ + perror(s); + exit(EXIT_FAILURE); +} +