#include #include #include #include #include #include #include "fdwatcher.h" #include "logging.h" struct ConnectionDataBuffer { size_t capacity; size_t length; char buf[]; }; struct ConnectionData { struct ConnectionDataBuffer *buffer; }; static int meowiero(struct FDWatchHandle *handle, enum FDWatch_EventType type, int fd, void *data) { struct ConnectionData *connectiondata = (struct ConnectionData *)data; ssize_t readSize = 0; char c = 0; switch (type) { case FDWATCH_EVENT_INP: aura_printf("INPUT event in fd:%d\n", fd); readSize = read(fd, &connectiondata->buffer->buf[connectiondata->buffer->length], connectiondata->buffer->capacity - connectiondata->buffer->length); connectiondata->buffer->length = connectiondata->buffer->length + readSize; printf("%ld\n", readSize); if (errno != 0) { perror("read(2)"); abort(); } if (connectiondata->buffer->length == connectiondata->buffer->capacity) { aura_printf("Too much unread data!\n"); abort(); } aura_printf("%c\n", c); break; case FDWATCH_EVENT_HUP: aura_printf("HANGUP event in fd:%d, exiting\n", fd); fdwatcher_remove(handle, fd); close(fd); return 0; } return 1; } /* init */ int childProcessMain(int argc, char **argv) { struct ConnectionData *connectiondata = { 0 }; struct FDWatchHandle fdhandle = { 0 }; int sockie = 0; int ret = 0; connectiondata = (struct ConnectionData *)malloc(sizeof(struct ConnectionData)); if (errno != 0) { perror("malloc(3)"); return 1; } connectiondata->buffer = malloc(sizeof(struct ConnectionDataBuffer) + 65536); if (errno != 0) { perror("malloc(3)"); return 1; } connectiondata->buffer->capacity = 65536; /* socket fd int in argv[2]! ^w^ */ sscanf(argv[2], "%d", &sockie); aura_printf("received fd:%d\n", sockie); ret = fdwatcher_initialise(&fdhandle, 1); if (ret < 0) { aura_fprintf(stderr, "fdwatcher_initialise failed\n"); return 1; } ret = fdwatcher_add(&fdhandle, sockie, (void *)connectiondata); if (ret < 0) { aura_fprintf(stderr, "fdwatcher_add failed\n"); return 1; } fdwatcher_watch(&fdhandle, meowiero); aura_printf("finished\n"); return 0; }