void get_receive_port_context(task_t taskp, mach_port_name_t portname, mach_port_context_t *context) { if (context == NULL) { return; } kern_return_t ret; ret = mach_port_get_context(taskp, portname, context); if (ret != KERN_SUCCESS) { fprintf(stderr, "mach_port_get_context(0x%08x) failed: %s\n", portname, mach_error_string(ret)); *context = (mach_port_context_t)0; } return; }
void get_context_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; mach_port_context_t g; int kret; options.flags = (MPO_CONTEXT_AS_GUARD); printf("Testing get_context() for non-strict guarded port: "); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != gval) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for strict guarded port: "); options.flags = (MPO_CONTEXT_AS_GUARD|MPO_STRICT); kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for strict guard port (guarded using mach_port_guard): "); kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_guard(mach_task_self(), port, gval, 1); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); printf("Testing get_context() for non-guarded port with context: "); kret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_set_context(mach_task_self(), port, gval); if (kret != KERN_SUCCESS) exit(1); kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != gval) goto failed; else printf("[PASSED]\n"); return; failed: printf("[FAILED]\n"); exit(1); }
void construct_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; mach_port_context_t g; int kret; printf("Testing All mach_port_construct() options...\n"); printf("No options specified: "); options.flags = 0; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_GUARD: "); options.flags = MPO_CONTEXT_AS_GUARD; kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_GUARD|MPO_STRICT: "); options.flags = MPO_CONTEXT_AS_GUARD|MPO_STRICT; kret = mach_port_construct(mach_task_self(), &options, gval, &port); if (kret == KERN_SUCCESS) { kret = mach_port_get_context(mach_task_self(), port, &g); if (kret != KERN_SUCCESS || g != 0) goto failed; else printf("[PASSED]\n"); } else goto failed; printf("Options MPO_QLIMIT: "); options.flags = MPO_QLIMIT; mach_port_limits_t limits = { MACH_PORT_QLIMIT_SMALL }; options.mpl = limits; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_TEMPOWNER: "); options.flags = MPO_TEMPOWNER; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_IMPORTANCE_RECEIVER: "); options.flags = MPO_IMPORTANCE_RECEIVER; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Options MPO_INSERT_SEND_RIGHT: "); options.flags = MPO_INSERT_SEND_RIGHT; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("No options specified (Construct Port-Set): "); options.flags = 0; kret = mach_port_construct(mach_task_self(), &options, 0, &port); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("...Complete\n"); return; failed: printf("[FAILED %d]\n", kret); exit(1); }