#include #include #include #include #include #include "fdwatcher.h" #include "logging.h" #include "network.h" static int handleNewConnection(int serverfd) { char *childProcessArgv[4] = { "haze", "childprocess", "", NULL }; struct sockaddr address = { 0 }; socklen_t addrSize = sizeof(address); char chocolate[11] = { 0 }; pid_t kwiat = 0; int sock = 0; sock = accept(serverfd, &address, &addrSize); if (errno != 0) { perror("accept(3)"); return -1; } kwiat = fork(); if (kwiat == 0) { snprintf((char *)&chocolate, 10, "%d", sock); childProcessArgv[2] = (char *)&chocolate; close(serverfd); execve("/proc/self/exe", childProcessArgv, NULL); perror("execve(3)"); abort(); } close(sock); return 0; } static int meowie(struct FDWatchHandle *handle, enum FDWatch_EventType type, int fd, void *data) { switch (type) { case FDWATCH_EVENT_INP: aura_printf("INPUT event in fd:%d\n", fd); handleNewConnection(fd); break; } return 1; } int parentProcessMain(void) { struct FDWatchHandle fdhandle = { 0 }; int sock = 0; int ret = 0; sock = net_listen4(0, 4096, 0); if (sock < 0) { aura_fprintf(stderr, "net_listen4 failed\n"); return 1; } ret = fdwatcher_initialise(&fdhandle, 1); if (ret < 0) { aura_fprintf(stderr, "fdwatcher_initialise failed\n"); return 1; } ret = fdwatcher_add(&fdhandle, sock, NULL); if (ret < 0) { aura_fprintf(stderr, "fdwatcher_add failed\n"); return 1; } aura_printf("Listening on fd:%d\n", sock); fdwatcher_watch(&fdhandle, meowie); return 0; }