1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/wait.h>
4 
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 
8 #include <err.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 #define CHECKFD_CMD	"checkfd"
15 #define CHECKFD_PATH	"/usr/local/bin/" CHECKFD_CMD
16 
17 int
18 main(void)
19 {
20 	pid_t pid;
21 	int s, status, ecode;
22 
23 	s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
24 	if (s < 0)
25 		err(1, "socket failed");
26 
27 	pid = fork();
28 	if (pid < 0) {
29 		err(1, "fork failed");
30 	} else if (pid == 0) {
31 		char fd[8];
32 
33 		snprintf(fd, sizeof(fd), "%d", s);
34 		if (execl(CHECKFD_PATH, CHECKFD_CMD, fd, NULL) < 0)
35 			err(3, "execl failed");
36 	}
37 
38 	if (waitpid(pid, &status, 0) < 0)
39 		err(1, "waitpid failed");
40 
41 	if (!WIFEXITED(status))
42 		errx(1, CHECKFD_CMD " did not exit");
43 
44 	ecode = WEXITSTATUS(status);
45 	if (ecode != 0) {
46 		warnx("exit code %d", ecode);
47 		abort();
48 	}
49 
50 	fprintf(stderr, "passed\n");
51 	exit(0);
52 }
53