xref: /xnu-11215/tests/vm_spawn_tool.c (revision bb611c8f)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <spawn.h>
4 #include <spawn_private.h>
5 #include <sys/sysctl.h>
6 
7 extern char **environ;
8 
9 #ifndef _POSIX_SPAWN_FORCE_4K_PAGES
10 #define _POSIX_SPAWN_FORCE_4K_PAGES 0x1000
11 #endif /* _POSIX_SPAWN_FORCE_4K_PAGES */
12 
13 int
main(int argc,char * argv[])14 main(int argc, char *argv[])
15 {
16 	if (argc < 2) {
17 		fprintf(stderr, "Usage: %s </path/to/command> [<arg> ..]\n", argv[0]);
18 		return 1;
19 	}
20 
21 	char * prog_path = argv[1];
22 	if (0 != access(prog_path, X_OK)) {
23 		fprintf(stderr, "%s is not an executable\n", prog_path);
24 		return 1;
25 	}
26 
27 	pid_t newpid = 0;
28 	posix_spawn_file_actions_t fileactions;
29 	posix_spawnattr_t spawnattrs;
30 	if (posix_spawnattr_init(&spawnattrs)) {
31 		perror("posix_spawnattr_init");
32 		return 1;
33 	}
34 	if (posix_spawn_file_actions_init(&fileactions)) {
35 		perror("posix_spawn_file_actions_init");
36 		return 1;
37 	}
38 	short sp_flags = POSIX_SPAWN_SETEXEC;
39 
40 	/* Need to set special flags */
41 	int supported = 0;
42 	size_t supported_size = sizeof(supported);
43 
44 	int r = sysctlbyname("debug.vm_mixed_pagesize_supported", &supported, &supported_size, NULL, 0);
45 	if (r == 0 && supported) {
46 		sp_flags |= _POSIX_SPAWN_FORCE_4K_PAGES;
47 	} else {
48 		/*
49 		 * We didnt find debug.vm.mixed_page.supported OR its set to 0.
50 		 * Skip the test.
51 		 */
52 		printf("Hardware doesn't support 4K pages, skipping test...");
53 		return 0;
54 	}
55 
56 	posix_spawnattr_setflags(&spawnattrs, sp_flags);
57 	posix_spawn(&newpid, prog_path, &fileactions, &spawnattrs, &argv[1], environ);
58 
59 	/* Should not have reached here */
60 	fprintf(stderr, "should not have reached here");
61 	return 1;
62 }
63