xref: /xnu-12377.81.4/tests/port_exhaustion.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 #include <darwintest.h>
2 #include <mach/mach.h>
3 #include <stdlib.h>
4 #include <sys/sysctl.h>
5 #include <unistd.h>
6 #include <darwintest_multiprocess.h>
7 #include <spawn.h>
8 #include <spawn_private.h>
9 #include <libproc_internal.h>
10 #include <signal.h>
11 
12 T_GLOBAL_META(
13 	T_META_NAMESPACE("xnu.ipc"),
14 	T_META_RUN_CONCURRENTLY(TRUE),
15 	T_META_RADAR_COMPONENT_NAME("xnu"),
16 	T_META_RADAR_COMPONENT_VERSION("IPC"),
17 	T_META_TAG_VM_PREFERRED);
18 
19 #define MAX_ARGV 2
20 
21 extern char **environ;
22 
23 T_DECL(port_exhaustion_test_max_ports, "Allocate maximum ipc ports possible", T_META_IGNORECRASHES(".*port_exhaustion_client.*"), T_META_CHECK_LEAKS(false))
24 {
25 	char *test_prog_name = "./port_exhaustion_client";
26 	char *child_args[MAX_ARGV];
27 	int child_pid;
28 	posix_spawnattr_t       attrs;
29 	int err;
30 
31 	/* Initialize posix_spawn attributes */
32 	posix_spawnattr_init(&attrs);
33 
34 	child_args[0] = test_prog_name;
35 	child_args[1] = NULL;
36 
37 	err = posix_spawn(&child_pid, child_args[0], NULL, &attrs, &child_args[0], environ);
38 	T_EXPECT_POSIX_SUCCESS(err, "posix_spawn port_exhaustion_client");
39 
40 	int child_status;
41 	/* Wait for child and check for exception */
42 	if (-1 == waitpid(child_pid, &child_status, 0)) {
43 		T_FAIL("waitpid: child mia");
44 	}
45 
46 	T_ASSERT_EQ(WIFEXITED(child_status), 0, "Child did not exit normally");
47 
48 	if (WIFSIGNALED(child_status)) {
49 		T_ASSERT_EQ(child_status, 9, "Child exited with status = %x", child_status);
50 	}
51 }
52