infidel-ITTI/kqueue_socket.c

87 lines
1.6 KiB
C
Raw Normal View History

2022-09-25 09:15:32 -04:00
#include <sys/event.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
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);
}