/* Cleanup of the open ports */ void PortsCleanup() { CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes); /* Remove the notification port from the runloop */ IODeregisterForSystemPower(¬ifierObject); /* Deregister from power notifications */ IOServiceClose(root_power_port); /* Close the Root Power Domain IOService port */ IONotificationPortDestroy(notifyPortRef); /* Destroy the notification port */ }
void CCocoaPowerSyscall::DeleteOSPowerCallBacks(void) { #if !defined(TARGET_DARWIN_IOS) CCocoaAutoPool autopool; // we no longer want sleep/wake notifications // remove the sleep notification port from the application runloop CFRunLoopRemoveSource( CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(m_notify_port), kCFRunLoopDefaultMode ); // deregister for system sleep notifications IODeregisterForSystemPower(&m_notifier_object); // IORegisterForSystemPower implicitly opens the Root Power Domain IOService // so we close it here IOServiceClose(m_root_port); // destroy the notification port allocated by IORegisterForSystemPower IONotificationPortDestroy(m_notify_port); // we no longer want power source change notifications if (m_HasBattery) { if (m_power_source) { CFRunLoopRemoveSource( CFRunLoopGetCurrent(), m_power_source, kCFRunLoopDefaultMode ); CFRelease(m_power_source); } } #endif }
void mac_sleep_stop() { if (root_port) { // remove the sleep notification port from the application runloop CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes); // deregister for system sleep notifications IODeregisterForSystemPower(¬ifierObject); // IORegisterForSystemPower implicitly opens the Root Power Domain IOService // so we close it here IOServiceClose(root_port); // destroy the notification port allocated by IORegisterForSystemPower IONotificationPortDestroy(notifyPortRef); // reset object members root_port = 0; notifyPortRef = NULL; notifierObject = 0; } }
static void pypowerobserver_dealloc(PyPowerObserver *self) { if (self->rootPort) { IODeregisterForSystemPower(&self->rootPort); } self->ob_type->tp_free((PyObject*)self); }
/*! Deregisters the daemon with the kernel to no longer receive power events. */ void iSCSIDDeregisterForPowerEvents() { CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(powerNotifyPortRef), kCFRunLoopDefaultMode); IODeregisterForSystemPower(&powerNotifier); IOServiceClose(powerPlaneRoot); IONotificationPortDestroy(powerNotifyPortRef); }
void COSXScreen::watchSystemPowerThread(void*) { io_object_t notifier; IONotificationPortRef notificationPortRef; CFRunLoopSourceRef runloopSourceRef = 0; m_pmRunloop = CFRunLoopGetCurrent(); // install system power change callback m_pmRootPort = IORegisterForSystemPower(this, ¬ificationPortRef, powerChangeCallback, ¬ifier); if (m_pmRootPort == 0) { LOG((CLOG_WARN "IORegisterForSystemPower failed")); } else { runloopSourceRef = IONotificationPortGetRunLoopSource(notificationPortRef); CFRunLoopAddSource(m_pmRunloop, runloopSourceRef, kCFRunLoopCommonModes); } // thread is ready { CLock lock(m_pmMutex); *m_pmThreadReady = true; m_pmThreadReady->signal(); } // if we were unable to initialize then exit. we must do this after // setting m_pmThreadReady to true otherwise the parent thread will // block waiting for it. if (m_pmRootPort == 0) { return; } // start the run loop LOG((CLOG_DEBUG "started watchSystemPowerThread")); CFRunLoopRun(); // cleanup if (notificationPortRef) { CFRunLoopRemoveSource(m_pmRunloop, runloopSourceRef, kCFRunLoopDefaultMode); CFRunLoopSourceInvalidate(runloopSourceRef); CFRelease(runloopSourceRef); } CLock lock(m_pmMutex); IODeregisterForSystemPower(¬ifier); m_pmRootPort = 0; LOG((CLOG_DEBUG "stopped watchSystemPowerThread")); }
void SystemEventsManager::stopLoop(bool forceStop) { if (systemEventLoopRunning && (forceStop || allEventsDisabled())) { CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes); IODeregisterForSystemPower(¬ifierObject); IOServiceClose(rootPort); IONotificationPortDestroy(notifyPortRef); CFRunLoopStop(CFRunLoopGetCurrent()); systemEventLoopRunning = false; } }
HostPowerServiceDarwin::~HostPowerServiceDarwin() { /* Jump out of the run loop. */ CFRunLoopStop(mRunLoop); /* Remove the sleep notification port from the application runloop. */ CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(mNotifyPort), kCFRunLoopCommonModes); /* Deregister for system sleep notifications. */ IODeregisterForSystemPower(&mNotifierObject); /* IORegisterForSystemPower implicitly opens the Root Power Domain * IOService so we close it here. */ IOServiceClose(mRootPort); /* Destroy the notification port allocated by IORegisterForSystemPower */ IONotificationPortDestroy(mNotifyPort); }
static void iokit_poll_notifications(void *opaque) { int phase = 0; Semaphore *handshake = opaque; IONotificationPortRef notification_port; io_object_t notifier; io_connect_t root_port; CFRunLoopSourceRef notification_run_loop_source; log_debug("Started notification poll thread"); // need to register this PThread with the Objective-C garbage collector, // because CoreFoundation uses Objective-C #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 objc_registerThreadWithCollector(); #endif // register for system sleep/wake notifications root_port = IORegisterForSystemPower(&root_port, ¬ification_port, iokit_handle_notifications, ¬ifier); if (root_port == MACH_PORT_NULL) { log_error("Could not register for root power domain"); goto cleanup; } phase = 1; // get notification run loop source notification_run_loop_source = IONotificationPortGetRunLoopSource(notification_port); if (notification_run_loop_source == NULL) { log_error("Could not get notification run loop source"); goto cleanup; } CFRunLoopAddSource(CFRunLoopGetCurrent(), notification_run_loop_source, kCFRunLoopDefaultMode); phase = 2; // start loop _run_loop = (CFRunLoopRef)CFRetain(CFRunLoopGetCurrent()); _running = true; semaphore_release(handshake); CFRunLoopRun(); log_debug("Stopped notification poll thread"); cleanup: if (!_running) { // need to release the handshake in all cases, otherwise iokit_init // will block forever in semaphore_acquire semaphore_release(handshake); } switch (phase) { // no breaks, all cases fall through intentionally case 2: CFRunLoopRemoveSource(CFRunLoopGetCurrent(), notification_run_loop_source, kCFRunLoopDefaultMode); case 1: IODeregisterForSystemPower(¬ifier); IOServiceClose(root_port); IONotificationPortDestroy(notification_port); default: break; } _running = false; }
static void * /* O - Return status/value */ sysEventThreadEntry(void) { io_object_t powerNotifierObj; /* Power notifier object */ IONotificationPortRef powerNotifierPort; /* Power notifier port */ SCDynamicStoreRef store = NULL;/* System Config dynamic store */ CFRunLoopSourceRef powerRLS = NULL,/* Power runloop source */ storeRLS = NULL;/* System Config runloop source */ CFStringRef key[6], /* System Config keys */ pattern[2]; /* System Config patterns */ CFArrayRef keys = NULL, /* System Config key array*/ patterns = NULL;/* System Config pattern array */ SCDynamicStoreContext storeContext; /* Dynamic store context */ CFRunLoopTimerContext timerContext; /* Timer context */ cupsd_thread_data_t threadData; /* Thread context data for the * * runloop notifiers */ /* * Register for power state change notifications */ bzero(&threadData, sizeof(threadData)); threadData.sysevent.powerKernelPort = IORegisterForSystemPower(&threadData, &powerNotifierPort, sysEventPowerNotifier, &powerNotifierObj); if (threadData.sysevent.powerKernelPort) { powerRLS = IONotificationPortGetRunLoopSource(powerNotifierPort); CFRunLoopAddSource(CFRunLoopGetCurrent(), powerRLS, kCFRunLoopDefaultMode); } else DEBUG_puts("sysEventThreadEntry: error registering for system power " "notifications"); /* * Register for system configuration change notifications */ bzero(&storeContext, sizeof(storeContext)); storeContext.info = &threadData; store = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("cupsd"), sysEventConfigurationNotifier, &storeContext); if (!ComputerNameKey) ComputerNameKey = SCDynamicStoreKeyCreateComputerName(kCFAllocatorDefault); if (!BTMMKey) BTMMKey = SCDynamicStoreKeyCreate(kCFAllocatorDefault, CFSTR("Setup:/Network/BackToMyMac")); if (!NetworkGlobalKeyIPv4) NetworkGlobalKeyIPv4 = SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault, kSCDynamicStoreDomainState, kSCEntNetIPv4); if (!NetworkGlobalKeyIPv6) NetworkGlobalKeyIPv6 = SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault, kSCDynamicStoreDomainState, kSCEntNetIPv6); if (!NetworkGlobalKeyDNS) NetworkGlobalKeyDNS = SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault, kSCDynamicStoreDomainState, kSCEntNetDNS); if (!HostNamesKey) HostNamesKey = SCDynamicStoreKeyCreateHostNames(kCFAllocatorDefault); if (!NetworkInterfaceKeyIPv4) NetworkInterfaceKeyIPv4 = SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault, kSCDynamicStoreDomainState, kSCCompAnyRegex, kSCEntNetIPv4); if (!NetworkInterfaceKeyIPv6) NetworkInterfaceKeyIPv6 = SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault, kSCDynamicStoreDomainState, kSCCompAnyRegex, kSCEntNetIPv6); if (store && ComputerNameKey && HostNamesKey && NetworkGlobalKeyIPv4 && NetworkGlobalKeyIPv6 && NetworkGlobalKeyDNS && NetworkInterfaceKeyIPv4 && NetworkInterfaceKeyIPv6) { key[0] = ComputerNameKey; key[1] = BTMMKey; key[2] = NetworkGlobalKeyIPv4; key[3] = NetworkGlobalKeyIPv6; key[4] = NetworkGlobalKeyDNS; key[5] = HostNamesKey; pattern[0] = NetworkInterfaceKeyIPv4; pattern[1] = NetworkInterfaceKeyIPv6; keys = CFArrayCreate(kCFAllocatorDefault, (const void **)key, sizeof(key) / sizeof(key[0]), &kCFTypeArrayCallBacks); patterns = CFArrayCreate(kCFAllocatorDefault, (const void **)pattern, sizeof(pattern) / sizeof(pattern[0]), &kCFTypeArrayCallBacks); if (keys && patterns && SCDynamicStoreSetNotificationKeys(store, keys, patterns)) { if ((storeRLS = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, store, 0)) != NULL) { CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLS, kCFRunLoopDefaultMode); } else DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreateRunLoopSource " "failed: %s\n", SCErrorString(SCError()))); } else DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreSetNotificationKeys " "failed: %s\n", SCErrorString(SCError()))); } else DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreate failed: %s\n", SCErrorString(SCError()))); if (keys) CFRelease(keys); if (patterns) CFRelease(patterns); /* * Set up a timer to delay the wake change notifications. * * The initial time is set a decade or so into the future, we'll adjust * this later. */ bzero(&timerContext, sizeof(timerContext)); timerContext.info = &threadData; threadData.timerRef = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L), 86400L * 365L * 10L, 0, 0, sysEventTimerNotifier, &timerContext); CFRunLoopAddTimer(CFRunLoopGetCurrent(), threadData.timerRef, kCFRunLoopDefaultMode); /* * Store our runloop in a global so the main thread can use it to stop us. */ pthread_mutex_lock(&SysEventThreadMutex); SysEventRunloop = CFRunLoopGetCurrent(); pthread_cond_signal(&SysEventThreadCond); pthread_mutex_unlock(&SysEventThreadMutex); /* * Disappear into the runloop until it's stopped by the main thread. */ CFRunLoopRun(); /* * Clean up before exiting. */ if (threadData.timerRef) { CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), threadData.timerRef, kCFRunLoopDefaultMode); CFRelease(threadData.timerRef); } if (threadData.sysevent.powerKernelPort) { CFRunLoopRemoveSource(CFRunLoopGetCurrent(), powerRLS, kCFRunLoopDefaultMode); IODeregisterForSystemPower(&powerNotifierObj); IOServiceClose(threadData.sysevent.powerKernelPort); IONotificationPortDestroy(powerNotifierPort); } if (storeRLS) { CFRunLoopRemoveSource(CFRunLoopGetCurrent(), storeRLS, kCFRunLoopDefaultMode); CFRunLoopSourceInvalidate(storeRLS); CFRelease(storeRLS); } if (store) CFRelease(store); pthread_exit(NULL); }