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