#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include static int sock_fd[2]; static int pipe_fd[2]; static void *do_file_to_pipe(void *unused) { int splice_len = 3; while (1) { char filename[80]; char command[80]; int file_fd; sprintf(filename, "splice-%d-%d-%d", getpid(), pthread_self(), splice_len); sprintf(command, "dmesg > %s", filename); system(command); file_fd = open(filename, O_RDONLY); if (file_fd == -1) exit(EXIT_FAILURE); while (1) { int n; n = splice(file_fd, NULL, pipe_fd[1], NULL, splice_len, 0); if (n == -1) exit(EXIT_FAILURE); if (n == 0) break; } close(file_fd); sprintf(command, "rm %s", filename); system(command); splice_len += rand() % 512; if (splice_len >= 16384) splice_len -= 16384; } return NULL; } static void *do_pipe_to_sock(void *unused) { int splice_len = 383; while (1) { if (splice(pipe_fd[0], NULL, sock_fd[1], NULL, splice_len, 0) == -1) { exit(EXIT_FAILURE); } splice_len += rand() % 512; if (splice_len >= 16384) splice_len -= 16384; } return NULL; } static void *do_sock_to_sock(void *unused) { while (1) { unsigned int n; char *buf; n = rand() % 16384; buf = malloc(n); if (read(sock_fd[0], buf, n) == -1) exit(EXIT_FAILURE); free(buf); } } #define N_THREADS 3 int main(int argc, char *argv[]) { if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock_fd) == -1) exit(EXIT_FAILURE); if (pipe(pipe_fd) == -1) exit(EXIT_FAILURE); pthread_t file_to_pipe_thread[N_THREADS]; pthread_t pipe_to_sock_thread[N_THREADS]; pthread_t sock_to_sock_thread[N_THREADS]; int i; for (i = 0; i < N_THREADS; ++i) { pthread_create(&file_to_pipe_thread[i], NULL, &do_file_to_pipe, NULL); pthread_create(&pipe_to_sock_thread[i], NULL, &do_pipe_to_sock, NULL); pthread_create(&sock_to_sock_thread[i], NULL, &do_sock_to_sock, NULL); } while (1) sleep(60); return EXIT_SUCCESS; }