void launchd_runtime_init(void) { #ifdef notyet pid_t p = getpid(); #endif (void)posix_assert_zero((mainkq = kqueue())); os_assert_zero(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &demand_port_set)); os_assert_zero(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &ipc_port_set)); posix_assert_zero(kevent_mod(demand_port_set, EVFILT_MACHPORT, EV_ADD, 0, 0, &kqmportset_callback)); os_assert_zero(launchd_mport_create_recv(&launchd_internal_port)); os_assert_zero(launchd_mport_make_send(launchd_internal_port)); max_msg_size = sizeof(union vproc_mig_max_sz); #ifdef notyet if (sizeof(union xpc_domain_max_sz) > max_msg_size) { max_msg_size = sizeof(union xpc_domain_max_sz); } #endif os_assert_zero(runtime_add_mport(launchd_internal_port, launchd_internal_demux)); os_assert_zero(pthread_create(&kqueue_demand_thread, NULL, kqueue_demand_loop, NULL)); os_assert_zero(pthread_detach(kqueue_demand_thread)); os_assert_zero(pthread_create(&waitpid_thread, NULL, waitpid_loop, NULL)); os_assert_zero(pthread_detach(waitpid_thread)); #ifdef notyet (void)posix_assumes_zero(sysctlbyname("vfs.generic.noremotehang", NULL, NULL, &p, sizeof(p))); #endif }
static mach_port_t register_service(const char *service_name) { mach_port_t port = MACH_PORT_NULL; kern_return_t kr; if (KERN_SUCCESS == (kr = bootstrap_check_in(bootstrap_port, (char *)service_name, &port))) { if (KERN_SUCCESS != (kr = mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND))) helplog(ASL_LEVEL_ERR, "mach_port_insert_right: %s", mach_error_string(kr)); else return port; } if (KERN_SUCCESS != (kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port))) { helplog(ASL_LEVEL_ERR, "mach_port_allocate: %s", mach_error_string(kr)); goto error; } if (KERN_SUCCESS != (kr = mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND))) { helplog(ASL_LEVEL_ERR, "mach_port_insert_right: %s", mach_error_string(kr)); goto error; } // XXX bootstrap_register does not modify its second argument, but the prototype does not include const. if (KERN_SUCCESS != (kr = bootstrap_register(bootstrap_port, (char *)service_name, port))) { helplog(ASL_LEVEL_ERR, "bootstrap_register failed: %s", mach_error_string(kr)); goto error; } return port; error: if (MACH_PORT_NULL != port) mach_port_deallocate(mach_task_self(), port); return MACH_PORT_NULL; }
pthread_t setup_mach_exception_handling_thread() { kern_return_t ret; pthread_t mach_exception_handling_thread = NULL; pthread_attr_t attr; current_mach_task = mach_task_self(); /* allocate a mach_port for this process */ ret = mach_port_allocate(current_mach_task, MACH_PORT_RIGHT_PORT_SET, &mach_exception_handler_port_set); /* create the thread that will receive the mach exceptions */ FSHOW((stderr, "Creating mach_exception_handler thread!\n")); pthread_attr_init(&attr); pthread_create(&mach_exception_handling_thread, &attr, mach_exception_handler, (void*) mach_exception_handler_port_set); pthread_attr_destroy(&attr); return mach_exception_handling_thread; }
bool MemoryProtectionExceptionHandler::install() { MOZ_ASSERT(!sExceptionHandlerInstalled); // If the exception handler is disabled, report success anyway. if (MemoryProtectionExceptionHandler::isDisabled()) return true; kern_return_t ret; mach_port_t task = mach_task_self(); // Allocate a new exception port with receive rights. sMachExceptionState.current = {}; MachExceptionParameters& current = sMachExceptionState.current; ret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, ¤t.port); if (ret != KERN_SUCCESS) return false; // Give the new port send rights as well. ret = mach_port_insert_right(task, current.port, current.port, MACH_MSG_TYPE_MAKE_SEND); if (ret != KERN_SUCCESS) { mach_port_deallocate(task, current.port); current = {}; return false; } // Start the thread that will receive the messages from our exception port. if (!sMachExceptionState.handlerThread.init(MachExceptionHandler)) { mach_port_deallocate(task, current.port); current = {}; return false; } // Set the other properties of our new exception handler. current.mask = EXC_MASK_BAD_ACCESS; current.behavior = exception_behavior_t(EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES); current.flavor = THREAD_STATE_NONE; // Tell the task to use our exception handler, and save the previous one. sMachExceptionState.previous = {}; MachExceptionParameters& previous = sMachExceptionState.previous; mach_msg_type_number_t previousCount = 1; ret = task_swap_exception_ports(task, current.mask, current.port, current.behavior, current.flavor, &previous.mask, &previousCount, &previous.port, &previous.behavior, &previous.flavor); if (ret != KERN_SUCCESS) { TerminateMachExceptionHandlerThread(); mach_port_deallocate(task, current.port); previous = {}; current = {}; return false; } // We should have info on the previous exception handler, even if it's null. MOZ_ASSERT(previousCount == 1); sExceptionHandlerInstalled = true; return sExceptionHandlerInstalled; }
bool Connection::open() { if (m_isServer) { ASSERT(m_receivePort); ASSERT(!m_sendPort); } else { ASSERT(!m_receivePort); ASSERT(m_sendPort); // Create the receive port. mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &m_receivePort); m_isConnected = true; // Send the initialize message, which contains a send right for the server to use. send(CoreIPCMessage::InitializeConnection, 0, MachPort(m_receivePort, MACH_MSG_TYPE_MAKE_SEND)); // Set the dead name handler for our send port. initializeDeadNameSource(); } // Register the data available handler. m_connectionQueue.registerMachPortEventHandler(m_receivePort, WorkQueue::MachPortDataAvailable, WorkItem::create(this, &Connection::receiveSourceEventHandler)); return true; }
int main() { mach_port_t port; kern_return_t err; printf("THE MACH_TASK_SELF IS %d.\n", mach_task_self()); err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (err != KERN_SUCCESS) { perror("ERROR:COULD NOT ALLOCATE THE PORT.\n"); return ; } printf("THE REGISTER PORT IS %d.\n", port); mach_ports_register(mach_task_self(), &port, 1); send(port, 9999); mach_port_t *recvport; int recv_num; task_get_kernel_port(mach_task_self(),&port); printf("SPECIAL KERNAL PORT IS %d.\n",port); mach_ports_lookup(mach_task_self(), &recvport, &recv_num); int i; for (i = 0; i < recv_num; i++) { printf("THE RECV PORT IS %d.AND THE NUM IS %d.\n", *(recvport + i), recv_num); } return 0; }
void darwin_arm_init_mach_exception_handler() { pthread_mutex_init(&mach_exception_handler_port_set_mu, NULL); // Called once per process to initialize a mach port server, listening // for EXC_BAD_ACCESS thread exceptions. int ret; pthread_t thr = NULL; pthread_attr_t attr; ret = mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &mach_exception_handler_port_set); if (ret) { fprintf(stderr, "runtime/cgo: mach_port_allocate failed for port_set: %d\n", ret); abort(); } // Start a thread to handle exceptions. uintptr_t port_set = (uintptr_t)mach_exception_handler_port_set; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); ret = pthread_create(&thr, &attr, mach_exception_handler, (void*)port_set); if (ret) { fprintf(stderr, "runtime/cgo: pthread_create failed: %d\n", ret); abort(); } pthread_attr_destroy(&attr); }
static void allocate_segv_handler() { #ifdef JULIA_ENABLE_THREADING arraylist_new(&suspended_threads, jl_n_threads); #endif pthread_t thread; pthread_attr_t attr; kern_return_t ret; mach_port_t self = mach_task_self(); ret = mach_port_allocate(self, MACH_PORT_RIGHT_RECEIVE, &segv_port); HANDLE_MACH_ERROR("mach_port_allocate",ret); ret = mach_port_insert_right(self, segv_port, segv_port, MACH_MSG_TYPE_MAKE_SEND); HANDLE_MACH_ERROR("mach_port_insert_right",ret); // Alright, create a thread to serve as the listener for exceptions if (pthread_attr_init(&attr) != 0) { jl_error("pthread_attr_init failed"); } pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&thread, &attr, mach_segv_listener, NULL) != 0) { jl_error("pthread_create failed"); } pthread_attr_destroy(&attr); for (int16_t tid = 0;tid < jl_n_threads;tid++) { attach_exception_port(pthread_mach_thread_np(jl_all_task_states[tid].system_id)); } }
int main( void ) { int i, n_queue; int s, r; mach_port_t destination; kern_return_t err; printf("in qlimit\n"); err = mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_RECEIVE,&destination ); if( err != KERN_SUCCESS ) { perror( "Error : could not allocate any port\n" ); } if (fork() == 0) send_integer( destination, 9999); else { receive_integer( destination, &r ); printf("The received integer is : %d\n", r ); } mach_port_deallocate( mach_task_self(), destination ); return(r); }
EXPORT int start(mach_port_t task, pid_t infoPid) { kern_return_t kret; pthread_t tid[2]; interface* face = find_interface(task); kret = mach_port_allocate(current_task(), MACH_PORT_RIGHT_RECEIVE, &face->server_port); RETURN_ON_MACH_ERROR("[-start] mach_port_allocate failed", kret); kret = mach_port_insert_right(current_task(), face->server_port, face->server_port, MACH_MSG_TYPE_MAKE_SEND); RETURN_ON_MACH_ERROR("[-start] mach_port_insert_right failed", kret); kret = task_set_exception_ports(task, EXC_MASK_ALL, face->server_port, EXCEPTION_DEFAULT|MACH_EXCEPTION_CODES, THREAD_STATE_NONE); RETURN_ON_MACH_ERROR("[-start] task_set_exception_ports failed", kret); int err = pthread_create(&tid[0], NULL, (void *(*)(void*))kqueue_loop, (void *)(unsigned long long)infoPid); if (err != 0) DEBUG_PRINT("\n[-start] can't create thread :[%s]", strerror(err)); else DEBUG_PRINT("\n[-start] Thread created successfully %d\n", 0); err = pthread_create(&tid[1], NULL, (void *(*)(void*))exception_server, (void *(*)(void*))(unsigned long long)face->server_port); if (err != 0) DEBUG_PRINT("\n[-start] can't create thread :[%s]", strerror(err)); else DEBUG_PRINT("\n[-start] Thread created successfully %d\n", 0); return 1; }
DLLEXPORT int jl_profile_start_timer(void) { kern_return_t ret; if (!profile_started) { mach_port_t self = mach_task_self(); ret = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, (clock_serv_t *)&clk); HANDLE_MACH_ERROR("host_get_clock_service", ret); ret = mach_port_allocate(self, MACH_PORT_RIGHT_RECEIVE, &profile_port); HANDLE_MACH_ERROR("mach_port_allocate", ret); // Alright, create a thread to serve as the listener for exceptions pthread_attr_t attr; if (pthread_attr_init(&attr) != 0) { jl_error("pthread_attr_init failed"); } pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (pthread_create(&profiler_thread, &attr, mach_profile_listener, NULL) != 0) { jl_error("pthread_create failed"); } pthread_attr_destroy(&attr); profile_started = 1; } timerprof.tv_sec = nsecprof/GIGA; timerprof.tv_nsec = nsecprof%GIGA; running = 1; ret = clock_alarm(clk, TIME_RELATIVE, timerprof, profile_port); HANDLE_MACH_ERROR("clock_alarm", ret); return 0; }
static mach_port_t checkin_or_register(char *bname) { kern_return_t kr; mach_port_t mp; /* If we're started by launchd or the old mach_init */ kr = bootstrap_check_in(bootstrap_port, bname, &mp); if (kr == KERN_SUCCESS) return mp; /* We probably were not started by launchd or the old mach_init */ kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp); if (kr != KERN_SUCCESS) { fprintf(stderr, "mach_port_allocate(): %s\n", mach_error_string(kr)); exit(EXIT_FAILURE); } kr = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND); if (kr != KERN_SUCCESS) { fprintf(stderr, "mach_port_insert_right(): %s\n", mach_error_string(kr)); exit(EXIT_FAILURE); } kr = bootstrap_register(bootstrap_port, bname, mp); if (kr != KERN_SUCCESS) { fprintf(stderr, "bootstrap_register(): %s\n", mach_error_string(kr)); exit(EXIT_FAILURE); } return mp; }
static int mach_lookup(const char *name, xpc_port_t *local, xpc_port_t *remote) { mach_port_t mp, rmp; kern_return_t kr; kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp); if (kr != KERN_SUCCESS) { errno = EPERM; return (-1); } kr = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND); if (kr != KERN_SUCCESS) { errno = EPERM; return (-1); } kr = bootstrap_look_up(bootstrap_port, name, &rmp); if (kr != KERN_SUCCESS) { debugf("bootstrap_look_up failed kr=%d", kr); errno = EBUSY; return (-1); } *local = (xpc_port_t)mp; *remote = (xpc_port_t)rmp; return (0); }
void RegisterExceptionHandler(void) { mach_port_t task = mach_task_self(); mach_port_t handler_port; kern_return_t rc; pthread_t tid; int err; /* Use the same mask as Breakpad. */ exception_mask_t exception_mask = EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC | EXC_MASK_BREAKPOINT; rc = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &handler_port); CHECK(rc == KERN_SUCCESS); rc = mach_port_insert_right(task, handler_port, handler_port, MACH_MSG_TYPE_MAKE_SEND); CHECK(rc == KERN_SUCCESS); err = pthread_create(&tid, NULL, ExceptionHandlerThread, (void *) (uintptr_t) handler_port); CHECK(err == 0); err = pthread_detach(tid); CHECK(err == 0); rc = task_set_exception_ports(mach_task_self(), exception_mask, handler_port, EXCEPTION_DEFAULT, THREAD_STATE_NONE); CHECK(rc == KERN_SUCCESS); }
void NetworkProcess::createNetworkConnectionToWebProcess() { #if USE(UNIX_DOMAIN_SOCKETS) IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection(); RefPtr<NetworkConnectionToWebProcess> connection = NetworkConnectionToWebProcess::create(socketPair.server); m_webProcessConnections.append(connection.release()); IPC::Attachment clientSocket(socketPair.client); parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientSocket), 0); #elif OS(DARWIN) // Create the listening port. mach_port_t listeningPort; mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort); // Create a listening connection. RefPtr<NetworkConnectionToWebProcess> connection = NetworkConnectionToWebProcess::create(IPC::Connection::Identifier(listeningPort)); m_webProcessConnections.append(connection.release()); IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND); parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientPort), 0); #else notImplemented(); #endif }
mach_port_t IODataQueueAllocateNotificationPort() { mach_port_t port = MACH_PORT_NULL; mach_port_limits_t limits; mach_msg_type_number_t info_cnt; mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); info_cnt = MACH_PORT_LIMITS_INFO_COUNT; mach_port_get_attributes(mach_task_self(), port, MACH_PORT_LIMITS_INFO, (mach_port_info_t)&limits, &info_cnt); limits.mpl_qlimit = 1; // Set queue to only 1 message mach_port_set_attributes(mach_task_self(), port, MACH_PORT_LIMITS_INFO, (mach_port_info_t)&limits, MACH_PORT_LIMITS_INFO_COUNT); return port; }
void setup_shared_port() { kern_return_t err; // get a send right to the port we're going to overwrite so that we can both // restore it for ourselves and send it to our child err = task_get_special_port(mach_task_self(), STOLEN_SPECIAL_PORT, &saved_special_port); MACH_ERR("saving original special port value", err); // allocate the shared port we want our child to have a send right to err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &shared_port_parent); MACH_ERR("allocating shared port", err); // insert the send right err = mach_port_insert_right(mach_task_self(), shared_port_parent, shared_port_parent, MACH_MSG_TYPE_MAKE_SEND); MACH_ERR("inserting MAKE_SEND into shared port", err); // stash the port in the STOLEN_SPECIAL_PORT slot such that the send right survives the fork err = task_set_special_port(mach_task_self(), STOLEN_SPECIAL_PORT, shared_port_parent); MACH_ERR("setting special port", err); }
void CEeExecutor::AddExceptionHandler() { assert(g_eeExecutor == nullptr); g_eeExecutor = this; #if defined(_WIN32) m_handler = AddVectoredExceptionHandler(TRUE, &CEeExecutor::HandleException); assert(m_handler != NULL); #elif defined(__ANDROID__) struct sigaction sigAction; sigAction.sa_handler = nullptr; sigAction.sa_sigaction = &HandleException; sigAction.sa_flags = SA_SIGINFO; sigemptyset(&sigAction.sa_mask); int result = sigaction(SIGSEGV, &sigAction, nullptr); assert(result >= 0); #elif defined(__APPLE__) kern_return_t result = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &m_port); assert(result == KERN_SUCCESS); m_handlerThread = std::thread([this] () { HandlerThreadProc(); }); result = mach_port_insert_right(mach_task_self(), m_port, m_port, MACH_MSG_TYPE_MAKE_SEND); assert(result == KERN_SUCCESS); result = thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, m_port, EXCEPTION_STATE | MACH_EXCEPTION_CODES, STATE_FLAVOR); assert(result == KERN_SUCCESS); result = mach_port_mod_refs(mach_task_self(), m_port, MACH_PORT_RIGHT_SEND, -1); assert(result == KERN_SUCCESS); #endif }
mach_port_t mach_reply_port(void) { mach_port_t p; mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &p); return p; }
CFUserNotificationRef CFUserNotificationCreate(CFAllocatorRef allocator, CFTimeInterval timeout, CFOptionFlags flags, SInt32 *error, CFDictionaryRef dictionary) { CHECK_FOR_FORK(); CFUserNotificationRef userNotification = NULL; SInt32 retval = ERR_SUCCESS; static uint16_t tokenCounter = 0; SInt32 token = ((getpid() << 16) | (tokenCounter++)); CFStringRef sessionID = (dictionary ? CFDictionaryGetValue(dictionary, kCFUserNotificationSessionIDKey) : NULL); mach_port_t replyPort = MACH_PORT_NULL; if (!allocator) allocator = __CFGetDefaultAllocator(); retval = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort); if (ERR_SUCCESS == retval && MACH_PORT_NULL != replyPort) retval = _CFUserNotificationSendRequest(allocator, sessionID, replyPort, token, timeout, flags, dictionary); if (ERR_SUCCESS == retval) { userNotification = (CFUserNotificationRef)_CFRuntimeCreateInstance(allocator, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification) - sizeof(CFRuntimeBase), NULL); if (userNotification) { userNotification->_replyPort = replyPort; userNotification->_token = token; userNotification->_timeout = timeout; userNotification->_requestFlags = flags; userNotification->_responseFlags = 0; userNotification->_sessionID = NULL; userNotification->_responseDictionary = NULL; userNotification->_machPort = NULL; userNotification->_callout = NULL; if (sessionID) userNotification->_sessionID = CFStringCreateCopy(allocator, sessionID); } else { retval = unix_err(ENOMEM); } } else { if (dictionary) CFUserNotificationLog(CFDictionaryGetValue(dictionary, kCFUserNotificationAlertHeaderKey), CFDictionaryGetValue(dictionary, kCFUserNotificationAlertMessageKey)); } if (ERR_SUCCESS != retval && MACH_PORT_NULL != replyPort) mach_port_destroy(mach_task_self(), replyPort); if (error) *error = retval; return userNotification; }
void do_child(mach_port_t shared_port) { kern_return_t err; // create a reply port to receive an ack that we should exec the target mach_port_t reply_port; err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &reply_port); MACH_ERR("child allocating reply port", err); // send our task port to our parent over the shared port port_msg_send_t msg = {0}; msg.header.msgh_size = sizeof(msg); msg.header.msgh_local_port = reply_port; msg.header.msgh_remote_port = shared_port; msg.header.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) | MACH_MSGH_BITS_COMPLEX; msg.body.msgh_descriptor_count = 1; msg.port.name = mach_task_self(); msg.port.disposition = MACH_MSG_TYPE_COPY_SEND; msg.port.type = MACH_MSG_PORT_DESCRIPTOR; err = mach_msg_send(&msg.header); MACH_ERR("child sending task port message", err); LOG("child sent task port back to parent"); // spin and let our parent kill us while(1){;} }
void guard_mach_port() { mach_port_t port; mach_port_options_t options; mach_port_context_t gval = CONTEXT_VALUE1; int kret; printf("Testing guarding a non-guarded mach port: "); 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, 0); if (kret == KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; printf("Testing guarding a guarded mach port: "); kret = mach_port_guard(mach_task_self(), port, CONTEXT_VALUE2, 0); if (kret != KERN_SUCCESS) printf("[PASSED]\n"); else goto failed; return; failed: printf("[FAILED]\n"); exit(1); }
static OSStatus AllocatePortFromPool(PerThreadStorage **storagePtr) // Grab a Mach port from sPool; if sPool is empty, create one. { OSStatus err; OSStatus junk; PerThreadStorage * storage; assert( storagePtr != NULL); assert(*storagePtr == NULL); storage = NULL; // First try to get an entry from pool. We try to grab the last one because // that minimises the amount of copying that CFArrayRemoveValueAtIndex has to // do. err = (OSStatus) pthread_mutex_lock(&sPoolMutex); if (err == noErr) { CFIndex poolCount; poolCount = CFArrayGetCount(sPool); if (poolCount > 0) { storage = (PerThreadStorage *) CFArrayGetValueAtIndex(sPool, poolCount - 1); CFArrayRemoveValueAtIndex(sPool, poolCount - 1); } junk = (OSStatus) pthread_mutex_unlock(&sPoolMutex); assert(junk == noErr); } // If we failed to find an entry in the pool, create a new one. if ( (err == noErr) && (storage == NULL) ) { storage = (PerThreadStorage *) malloc(sizeof(*storage)); if (storage == NULL) { err = memFullErr; } else { storage->magic = kPerThreadStorageMagic; storage->port = MACH_PORT_NULL; err = (OSStatus) mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &storage->port); if (err != noErr) { assert(storage->port == MACH_PORT_NULL); free(storage); storage = NULL; } } } if (err == noErr) { *storagePtr = storage; } assert( (err == noErr) == (*storagePtr != NULL) ); assert( (*storagePtr == NULL) || ((*storagePtr)->magic == kPerThreadStorageMagic) ); assert( (*storagePtr == NULL) || ((*storagePtr)->port != MACH_PORT_NULL) ); return err; }
void Connection::setShouldCloseConnectionOnMachExceptions() { ASSERT(m_exceptionPort == MACH_PORT_NULL); if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &m_exceptionPort) != KERN_SUCCESS) ASSERT_NOT_REACHED(); if (mach_port_insert_right(mach_task_self(), m_exceptionPort, m_exceptionPort, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) ASSERT_NOT_REACHED(); }
void netfs_init () { netfs_protid_class = ports_create_class (netfs_release_protid, 0); netfs_control_class = ports_create_class (0, 0); netfs_port_bucket = ports_create_bucket (); netfs_auth_server_port = getauth (); mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &netfs_fsys_identity); }
int main (int argc, char **argv) { mach_port_t bootstrap; mach_port_t control; error_t err; /* Parse our options... */ argp_parse (&argp, argc, argv, 0, 0, 0); task_get_bootstrap_port (mach_task_self (), &bootstrap); if (bootstrap == MACH_PORT_NULL) error (1, 0, "Must be started as a translator"); linktarget = argv[1]; /* Reply to our parent */ mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &control); mach_port_insert_right (mach_task_self (), control, control, MACH_MSG_TYPE_MAKE_SEND); err = fsys_startup (bootstrap, 0, control, MACH_MSG_TYPE_COPY_SEND, &realnode); mach_port_deallocate (mach_task_self (), control); mach_port_deallocate (mach_task_self (), bootstrap); if (err) error (1, err, "Starting up translator"); io_restrict_auth (realnode, &realnodenoauth, 0, 0, 0, 0); mach_port_deallocate (mach_task_self (), realnode); /* Mark us as important. */ mach_port_t proc = getproc (); if (proc == MACH_PORT_NULL) error (2, err, "cannot get a handle to our process"); err = proc_mark_important (proc); /* This might fail due to permissions or because the old proc server is still running, ignore any such errors. */ if (err && err != EPERM && err != EMIG_BAD_ID) error (2, err, "Cannot mark us as important"); mach_port_deallocate (mach_task_self (), proc); /* Launch */ while (1) { /* The timeout here is 10 minutes */ err = mach_msg_server_timeout (fsys_server, 0, control, MACH_RCV_TIMEOUT, 1000 * 60 * 10); if (err == MACH_RCV_TIMED_OUT) exit (0); } }
void darwin_arm_init_thread_exception_port() { // Called by each new OS thread to bind its EXC_BAD_ACCESS exception // to mach_exception_handler_port_set. int ret; mach_port_t port = MACH_PORT_NULL; ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port); if (ret) { fprintf(stderr, "runtime/cgo: mach_port_allocate failed: %d\n", ret); abort(); } ret = mach_port_insert_right( mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND); if (ret) { fprintf(stderr, "runtime/cgo: mach_port_insert_right failed: %d\n", ret); abort(); } ret = thread_set_exception_ports( mach_thread_self(), EXC_MASK_BAD_ACCESS, port, EXCEPTION_DEFAULT, THREAD_STATE_NONE); if (ret) { fprintf(stderr, "runtime/cgo: thread_set_exception_ports failed: %d\n", ret); abort(); } ret = pthread_mutex_lock(&mach_exception_handler_port_set_mu); if (ret) { fprintf(stderr, "runtime/cgo: pthread_mutex_lock failed: %d\n", ret); abort(); } ret = mach_port_move_member( mach_task_self(), port, mach_exception_handler_port_set); if (ret) { fprintf(stderr, "runtime/cgo: mach_port_move_member failed: %d\n", ret); abort(); } ret = pthread_mutex_unlock(&mach_exception_handler_port_set_mu); if (ret) { fprintf(stderr, "runtime/cgo: pthread_mutex_unlock failed: %d\n", ret); abort(); } }
int main(int argc, char **argv) { kern_return_t rc; mach_port_t security_port; mach_port_t root_ledger_wired; mach_port_t root_ledger_paged; if(argc > 1) { if(argv[1][0] == '-') if(argv[1][1] == 't') { tgdb_debug_flags = -1; debug_flag = 1; } } if (bootstrap_ports(bootstrap_port, &tgdb_host_priv, &device_server_port, &root_ledger_wired, &root_ledger_paged, &security_port)) panic("bootstrap_ports"); threads_init(); printf_init(device_server_port); panic_init(tgdb_host_priv); tgdb_session_init(); if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &tgdb_wait_port_set) != KERN_SUCCESS) { printf("tgdb: can't allocate port set\n"); return 1; } exc_thread = tgdb_thread_create((vm_offset_t) tgdb_thread); if(exc_thread == 0) printf("tgdb_thread_create failed\n"); if((rc = thread_resume(exc_thread)) != KERN_SUCCESS) { printf("tgdb: thread_resume failed %x\n", rc); } /* * Change our scheduling policy to round-robin, and boost our priority. */ tgdb_set_policy(tgdb_host_priv); tgdb_connect(); return 1; /* Only reached on error. */ }
/* make new set of port rights, same size as inCnt, set inout to contain MACH_PORT_NULLs (same size) */ kern_return_t s_outline_poly_arrays(mach_port_t target, mach_port_array_t inRight, mach_msg_type_number_t inRightCnt, mach_port_array_t *inoutRight, mach_msg_type_number_t *inoutRightCnt, mach_port_array_t *outRight, mach_msg_type_number_t *outRightCnt) { int i; vm_allocate(mach_task_self(), (vm_address_t *)outRight, inRightCnt*sizeof(mach_port_t), 1); for (i=0; i < inRightCnt; i++) mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &((*outRight)[i])); *outRightCnt = inRightCnt; for (i=0; i < *inoutRightCnt; i++) { mach_port_deallocate(mach_task_self(), (*inoutRight)[i]); (*inoutRight)[i] = MACH_PORT_NULL; } return KERN_SUCCESS; }
int main(void) { ERR_FILE = fopen("/tmp/FortunateSonErrorFile.txt", "a"); fprintf(ERR_FILE, "Started with pid %d\n", getpid()); mach_port_t parent_recv_port = get_hex_fiend_receive_port(); // Get notified when the parent receive port dies mach_port_t my_task = mach_task_self(); mach_port_t notificationPort = MACH_PORT_NULL; CHECK_MACH_ERROR(mach_port_allocate(my_task, MACH_PORT_RIGHT_RECEIVE, ¬ificationPort)); mach_port_t old; CHECK_MACH_ERROR(mach_port_request_notification(my_task, parent_recv_port, MACH_NOTIFY_NO_SENDERS, 0/*sync*/, notificationPort, MACH_MSG_TYPE_MAKE_SEND_ONCE, &old)); /* Make a port set */ mach_port_t portSet = MACH_PORT_NULL; CHECK_MACH_ERROR(mach_port_allocate(my_task, MACH_PORT_RIGHT_PORT_SET, &portSet)); CHECK_MACH_ERROR(mach_port_insert_member(my_task, parent_recv_port, portSet)); CHECK_MACH_ERROR(mach_port_insert_member(my_task, notificationPort, portSet)); run_server(portSet, notificationPort); /* Once run_server returns, we're done, so exit */ return 0; }