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