xref: /xnu-10063.121.3/tests/posix_spawn_alt_rosetta_helper.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions #include <stdio.h>
2*2c2f96dcSApple OSS Distributions #include <mach-o/dyld.h>
3*2c2f96dcSApple OSS Distributions #include <mach-o/dyld_images.h>
4*2c2f96dcSApple OSS Distributions #include <libproc.h>
5*2c2f96dcSApple OSS Distributions 
6*2c2f96dcSApple OSS Distributions /*
7*2c2f96dcSApple OSS Distributions  * Returns 1 if the standard Rosetta runtime is loaded, 2 if the alternative
8*2c2f96dcSApple OSS Distributions  * runtime is loaded, and 0 if no runtime was detected.
9*2c2f96dcSApple OSS Distributions  */
10*2c2f96dcSApple OSS Distributions int
main(int argc,const char * argv[])11*2c2f96dcSApple OSS Distributions main(int argc, const char * argv[])
12*2c2f96dcSApple OSS Distributions {
13*2c2f96dcSApple OSS Distributions 	unsigned depth = 1;
14*2c2f96dcSApple OSS Distributions 	vm_size_t size = 0;
15*2c2f96dcSApple OSS Distributions 	vm_address_t address = 0;
16*2c2f96dcSApple OSS Distributions 	vm_address_t end_address;
17*2c2f96dcSApple OSS Distributions 	kern_return_t err = KERN_SUCCESS;
18*2c2f96dcSApple OSS Distributions 	mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
19*2c2f96dcSApple OSS Distributions 	struct vm_region_submap_info_64 info;
20*2c2f96dcSApple OSS Distributions 	char path_buffer[MAXPATHLEN + 1] = {0};
21*2c2f96dcSApple OSS Distributions 
22*2c2f96dcSApple OSS Distributions 	while (1) {
23*2c2f96dcSApple OSS Distributions 		err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth, (vm_region_info_t)&info, &count);
24*2c2f96dcSApple OSS Distributions 		if (err != KERN_SUCCESS) {
25*2c2f96dcSApple OSS Distributions 			break;
26*2c2f96dcSApple OSS Distributions 		}
27*2c2f96dcSApple OSS Distributions 
28*2c2f96dcSApple OSS Distributions 		end_address = address + size;
29*2c2f96dcSApple OSS Distributions 		err = proc_regionfilename(getpid(), address, path_buffer, MAXPATHLEN);
30*2c2f96dcSApple OSS Distributions 		if (err == KERN_SUCCESS) {
31*2c2f96dcSApple OSS Distributions 			if (strcmp(path_buffer, "/usr/libexec/rosetta/runtime") == 0) {
32*2c2f96dcSApple OSS Distributions 				printf("0x%016lx-0x%016lx %s\n", address, end_address, path_buffer);
33*2c2f96dcSApple OSS Distributions 				return 1;
34*2c2f96dcSApple OSS Distributions 			} else if (strcmp(path_buffer, "/usr/local/libexec/rosetta/runtime_internal") == 0) {
35*2c2f96dcSApple OSS Distributions 				printf("0x%016lx-0x%016lx %s\n", address, end_address, path_buffer);
36*2c2f96dcSApple OSS Distributions 				return 2;
37*2c2f96dcSApple OSS Distributions 			}
38*2c2f96dcSApple OSS Distributions 		}
39*2c2f96dcSApple OSS Distributions 
40*2c2f96dcSApple OSS Distributions 		address = end_address;
41*2c2f96dcSApple OSS Distributions 	}
42*2c2f96dcSApple OSS Distributions 	return 0;
43*2c2f96dcSApple OSS Distributions }
44