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