1*0f4c859eSApple OSS Distributions #include "mach_vm_tests.h"
2*0f4c859eSApple OSS Distributions boolean_t debug = TRUE;
3*0f4c859eSApple OSS Distributions
4*0f4c859eSApple OSS Distributions int
main()5*0f4c859eSApple OSS Distributions main()
6*0f4c859eSApple OSS Distributions {
7*0f4c859eSApple OSS Distributions dispatch_source_t parentSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, (uintptr_t)getppid(), DISPATCH_PROC_EXIT, NULL);
8*0f4c859eSApple OSS Distributions dispatch_source_set_event_handler(parentSource, ^{
9*0f4c859eSApple OSS Distributions T_LOG("Event handler got invoked. Parent process died. Exiting");
10*0f4c859eSApple OSS Distributions exit(1);
11*0f4c859eSApple OSS Distributions });
12*0f4c859eSApple OSS Distributions dispatch_activate(parentSource);
13*0f4c859eSApple OSS Distributions
14*0f4c859eSApple OSS Distributions const char *serviceName = MACH_VM_TEST_SERVICE_NAME;
15*0f4c859eSApple OSS Distributions
16*0f4c859eSApple OSS Distributions kern_return_t ret;
17*0f4c859eSApple OSS Distributions mach_port_t bootstrap;
18*0f4c859eSApple OSS Distributions task_get_bootstrap_port(mach_task_self(), &bootstrap);
19*0f4c859eSApple OSS Distributions
20*0f4c859eSApple OSS Distributions mach_port_t port;
21*0f4c859eSApple OSS Distributions mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
22*0f4c859eSApple OSS Distributions
23*0f4c859eSApple OSS Distributions #pragma clang diagnostic push
24*0f4c859eSApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated"
25*0f4c859eSApple OSS Distributions ret = bootstrap_register2(bootstrap, (char *)serviceName, port, BOOTSTRAP_ALLOW_LOOKUP);
26*0f4c859eSApple OSS Distributions #pragma clang diagnostic pop
27*0f4c859eSApple OSS Distributions
28*0f4c859eSApple OSS Distributions mach_msg_size_t messageSize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
29*0f4c859eSApple OSS Distributions ipc_message_t *message = (ipc_message_t *)calloc(1, messageSize);
30*0f4c859eSApple OSS Distributions
31*0f4c859eSApple OSS Distributions message->header.msgh_bits = MACH_MSGH_BITS_ZERO;
32*0f4c859eSApple OSS Distributions message->header.msgh_size = messageSize;
33*0f4c859eSApple OSS Distributions message->header.msgh_remote_port = MACH_PORT_NULL;
34*0f4c859eSApple OSS Distributions message->header.msgh_local_port = port;
35*0f4c859eSApple OSS Distributions
36*0f4c859eSApple OSS Distributions ret = mach_msg(&message->header, MACH_RCV_MSG, 0, messageSize, port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
37*0f4c859eSApple OSS Distributions if (ret == KERN_SUCCESS) {
38*0f4c859eSApple OSS Distributions if (MACH_MSGH_BITS_REMOTE(message->header.msgh_bits) == MACH_MSG_TYPE_PORT_SEND) {
39*0f4c859eSApple OSS Distributions persistentReplyPort = message->header.msgh_remote_port;
40*0f4c859eSApple OSS Distributions mach_port_mod_refs(mach_task_self(), persistentReplyPort, MACH_PORT_RIGHT_SEND, 1);
41*0f4c859eSApple OSS Distributions }
42*0f4c859eSApple OSS Distributions }
43*0f4c859eSApple OSS Distributions
44*0f4c859eSApple OSS Distributions mach_server_make_memory_entry(port);
45*0f4c859eSApple OSS Distributions mach_server_remap(port);
46*0f4c859eSApple OSS Distributions mach_server_read(port, VM_OP_READ);
47*0f4c859eSApple OSS Distributions //mach_server_read(port, VM_OP_WRITE);
48*0f4c859eSApple OSS Distributions mach_server_read(port, VM_OP_READ_OVERWRITE);
49*0f4c859eSApple OSS Distributions
50*0f4c859eSApple OSS Distributions
51*0f4c859eSApple OSS Distributions message->header.msgh_bits = MACH_MSGH_BITS_ZERO;
52*0f4c859eSApple OSS Distributions message->header.msgh_size = messageSize;
53*0f4c859eSApple OSS Distributions message->header.msgh_remote_port = MACH_PORT_NULL;
54*0f4c859eSApple OSS Distributions message->header.msgh_local_port = port;
55*0f4c859eSApple OSS Distributions
56*0f4c859eSApple OSS Distributions mach_server_construct_header(message, port);
57*0f4c859eSApple OSS Distributions message->vm_op = VM_OP_EXIT;
58*0f4c859eSApple OSS Distributions ret = mach_msg(&message->header, MACH_SEND_MSG, message->header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
59*0f4c859eSApple OSS Distributions if (ret != KERN_SUCCESS) {
60*0f4c859eSApple OSS Distributions T_LOG("ERROR: Failed to send message to client: (%d) %s\n", ret, mach_error_string(ret));
61*0f4c859eSApple OSS Distributions return 1;
62*0f4c859eSApple OSS Distributions }
63*0f4c859eSApple OSS Distributions
64*0f4c859eSApple OSS Distributions (void)parentSource;
65*0f4c859eSApple OSS Distributions
66*0f4c859eSApple OSS Distributions return 0;
67*0f4c859eSApple OSS Distributions }
68