#include #include #include #include #include pthread_t notifier_thread_handle; void (**notifiers)(char*); size_t notifier_count = 0; size_t notifier_capacity = 0; char notifier_shutdown = 0; int notifier_fd; void *notifier_thread(void* param){ notifier_fd = open("/var/log/nginx/access.log", O_RDONLY); if(notifier_fd < 1){ perror("open"); return 0; } lseek(notifier_fd, 0, SEEK_END); char buffer[256]; while(1){ // Check to see if a webpage was downloaded ssize_t readlen = read(notifier_fd, buffer, 256); if(notifier_shutdown) return 0; if(readlen == 0){ sleep(1); continue; } if(readlen < 0){ perror("read"); return 0; } for(size_t i = 0; i < notifier_count; i++){ notifiers[i](buffer); } } } void notifier_clear(){ notifier_count = 0; } void notifier_init(){ notifiers = malloc(sizeof(void*) * 10); notifier_capacity = 10; pthread_create(¬ifier_thread_handle, 0, notifier_thread, 0); } void notifier_add(void (*nn)(char*)){ if(notifier_count >= notifier_capacity){ notifiers = realloc(notifiers, sizeof(void*) * (notifier_capacity + 10)); notifier_capacity += 10; } notifiers[notifier_count] = nn; notifier_count++; } void notifier_deinit(){ notifier_shutdown = 1; free(notifiers); close(notifier_fd); pthread_join(notifier_thread_handle, 0); }