#include #include #include #include #include #include #include #include #include #include #include #include #include #include /* From libnv. */ extern int fd_send(int sock, const int *fds, size_t nfds); extern int fd_recv(int sock, int *fds, size_t nfds); /* * This thread triggers the unp garbage collector. Domain socket loops are * created that can only be cleaned up by the garbage collector. */ static void * trigger_gc(void *dummy_arg) { int sock[2]; (void)dummy_arg; for (;;) { if (socketpair(PF_UNIX, SOCK_STREAM, 0, sock) < 0) { err(1, "socketpair"); } if (fd_send(sock[1], &sock[0], 1) != 0) { errx(1, "fd_send failed"); } close(sock[0]); close(sock[1]); } } static unsigned long counter = 0; static void print_info() { int unp_recycled; size_t unp_recycled_size = sizeof(unp_recycled); char const recycled_sysctl[] = "net.local.recycled"; if (sysctlbyname(recycled_sysctl, &unp_recycled, &unp_recycled_size, NULL, 0) < 0 || unp_recycled_size != sizeof(unp_recycled)) { err(1, "sysctlbyname"); } fprintf(stdout, "%9lu, %s: %9d\n", /**/ counter, recycled_sysctl, unp_recycled); } static int comm_sock[2] = { -1, -1 }; static void * create_sock(void *dummy_arg) { uint8_t dummy = 0; int sock[2] = { -1, -1 }; (void)dummy_arg; for (;;) { if (socketpair(PF_UNIX, SOCK_STREAM, 0, sock) < 0) { err(1, "socketpair"); } ++counter; if (fd_send(comm_sock[1], &sock[0], 1) != 0) { errx(1, "fd_send failed"); } close(sock[0]); if (write(sock[1], &dummy, 1) != 1) { errx(1, "write error"); } if (read(sock[1], &dummy, 1) != 1) { errx(1, "read error"); } close(sock[1]); if (!(counter % 10000)) { print_info(); } } return (NULL); } int main() { pthread_t socket_create_thread; pthread_t gc_trigger_thread; int sock_client; uint8_t dummy; ssize_t rc; atexit(print_info); print_info(); if (pthread_create(&gc_trigger_thread, NULL, trigger_gc, NULL)) { err(1, "pthread_create"); } if (socketpair(PF_UNIX, SOCK_STREAM, 0, comm_sock) < 0) { err(1, "socketpair"); } if (pthread_create(&socket_create_thread, NULL, create_sock, NULL)) { err(1, "pthread_create"); } for (;;) { if (fd_recv(comm_sock[0], &sock_client, 1) != 0) { errx(1, "fd_recv failed"); } if ((rc = read(sock_client, &dummy, 1)) != 1) { if (rc < 0) { err(1, "read"); } else { errx(1, "read: EOF"); } } if (write(sock_client, &dummy, 1) < 0) { err(1, "write error"); } close(sock_client); } }