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