__private_extern__ void SystemLoad_prime(void) { systemLoadKey = SCDynamicStoreKeyCreate( kCFAllocatorDefault, CFSTR("%@%@"), kSCDynamicStoreDomainState, CFSTR("/IOKit/PowerManagement/SystemLoad")); systemLoadDetailedKey = SCDynamicStoreKeyCreate( kCFAllocatorDefault, CFSTR("%@%@"), kSCDynamicStoreDomainState, CFSTR("/IOKit/PowerManagement/SystemLoad/Detailed")); notify_register_check(kIOSystemLoadAdvisoryNotifyName, &gNotifyToken); // If this is a desktop, then we won't get any battery notifications. // Let's prime the battery pump right here with an initial coll. SystemLoadBatteriesHaveChanged(_batteries()); SystemLoadPrefsHaveChanged(); #if !TARGET_OS_EMBEDDED SystemLoadUserStateHasChanged(); #endif SystemLoadCPUPowerHasChanged(NULL); }
//----------------------------------------------------------------------------- // publish_state //----------------------------------------------------------------------------- int publish_state(struct vpn_params* params) { CFMutableDictionaryRef dict; CFStringRef key; int val; CFNumberRef num; if (params->storeRef == 0) return 0; /* Interface information */ key = SCDynamicStoreKeyCreate(0, CFSTR("%@/%@/%@/%@/%s"), kSCDynamicStoreDomainState, kSCCompNetwork, kRASRemoteAccessServer, params->serverIDRef, "Interface"); if (key) { dict = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (dict) { switch (params->server_type) { case SERVER_TYPE_PPP: CFDictionarySetValue(dict, kRASPropInterfaceType, kRASValInterfaceTypePPP); CFDictionarySetValue(dict, kRASPropInterfaceSubType, params->serverSubTypeRef); break; case SERVER_TYPE_IPSEC: CFDictionarySetValue(dict, kRASPropInterfaceType, kRASValInterfaceTypeIPSec); break; } SCDynamicStoreAddTemporaryValue(params->storeRef, key, dict); CFRelease(dict); } CFRelease(key); } /* Server information */ key = SCDynamicStoreKeyCreate(0, CFSTR("%@/%@/%@/%@/%s"), kSCDynamicStoreDomainState, kSCCompNetwork, kRASRemoteAccessServer, params->serverIDRef, "Server"); if (key) { dict = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (dict) { val = getpid(); num = CFNumberCreate(0, kCFNumberIntType, &val); if (num) { CFDictionarySetValue(dict, CFSTR("pid"), num); SCDynamicStoreAddTemporaryValue(params->storeRef, key, dict); CFRelease(num); } CFRelease(dict); } CFRelease(key); } return 1; }
/* IOCopyLoadAdvisoryLevelDetailed * In case of error, or inability to find system load advisory level, * returns NULL. */ CFDictionaryRef IOCopySystemLoadAdvisoryDetailed(void) { CFDictionaryRef gtDetailed = NULL; SCDynamicStoreRef storage = NULL; CFStringRef gtDetailedKey = SCDynamicStoreKeyCreate( kCFAllocatorDefault, CFSTR("%@%@"), _io_kSCDynamicStoreDomainState, kSLADetailedPath); storage = SCDynamicStoreCreate( kCFAllocatorDefault, CFSTR("IOKit IOGetSystemLoadAdvisoryDetailed"), NULL, NULL); if (!storage || !gtDetailedKey) { goto exit; } gtDetailed = SCDynamicStoreCopyValue(storage, gtDetailedKey); if(gtDetailed) { if(!isA_CFDictionary(gtDetailed)) { CFRelease(gtDetailed); gtDetailed = NULL; } } exit: if (gtDetailedKey) CFRelease(gtDetailedKey); if (storage) CFRelease(storage); return gtDetailed; }
/* _weManageUPSPower * * Determines whether X Power Management should do the emergency shutdown when low on UPS power. * OS X should NOT manage low power situations if another third party application has already claimed * that emergency shutdown responsibility. * * Return value: * true == OS X should manage emergency shutdowns * false == another installed & running application is managing shutdowns */ static bool _weManageUPSPower(void) { static CFStringRef ups_claimed = NULL; SCDynamicStoreRef ds_ref = NULL; CFTypeRef temp; bool ret_val = true; if(!ups_claimed) { ups_claimed = SCDynamicStoreKeyCreate(kCFAllocatorDefault, CFSTR("%@%@"), kSCDynamicStoreDomainState, CFSTR(kIOPSUPSManagementClaimed)); } // Check for existence of "UPS Management claimed" key in SCDynamicStore if( ups_claimed && ds_ref && (temp = SCDynamicStoreCopyValue(_getSharedPMDynamicStore(), ups_claimed)) ) { if(kCFBooleanTrue == temp) ret_val = false; CFRelease(temp); } return ret_val; }
static void watchQuietEnable() { CFArrayRef keys; Boolean ok; initKey = SCDynamicStoreKeyCreate(NULL, CFSTR("%@" "InterfaceNamer"), kSCDynamicStoreDomainPlugin); initRls = SCDynamicStoreCreateRunLoopSource(NULL, store, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), initRls, kCFRunLoopDefaultMode); keys = CFArrayCreate(NULL, (const void **)&initKey, 1, &kCFTypeArrayCallBacks); ok = SCDynamicStoreSetNotificationKeys(store, keys, NULL); CFRelease(keys); if (!ok) { SCLog(TRUE, LOG_ERR, CFSTR("SCDynamicStoreSetNotificationKeys() failed: %s\n"), SCErrorString(SCError())); watchQuietDisable(); } return; }
IOReturn CreatePowerManagerUPSEntry(UPSDataRef upsDataRef, CFDictionaryRef properties, CFSetRef capabilities) { CFMutableDictionaryRef upsStoreDict = NULL; CFStringRef upsName = NULL; CFStringRef transport = NULL; CFStringRef upsStoreKey = NULL; CFNumberRef number = NULL; SCDynamicStoreRef upsStore = NULL; IOReturn status = kIOReturnSuccess; int elementValue = 0; char upsLabelString[kInternalUPSLabelLength]; if ( !upsDataRef || !properties || !capabilities) return kIOReturnError; upsStoreDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); // Set some Store values if ( upsStoreDict ) { // We need to save a name for this device. First, try to see if we have a USB Product Name. If // that fails then use the manufacturer and if that fails, then use a generic name. Couldn't we use // a serial # here? // upsName = (CFStringRef) CFDictionaryGetValue( properties, CFSTR( kIOPSNameKey ) ); if ( !upsName ) upsName = CFSTR(kDefaultUPSName); transport = (CFStringRef) CFDictionaryGetValue( properties, CFSTR( kIOPSTransportTypeKey ) ); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSNameKey), upsName); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSTransportTypeKey), transport); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSIsPresentKey), kCFBooleanTrue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSIsChargingKey), kCFBooleanTrue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSPowerSourceStateKey), CFSTR(kIOPSACPowerValue)); number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &upsDataRef->upsID); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSPowerSourceIDKey), number); CFRelease(number); elementValue = 100; number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &elementValue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSMaxCapacityKey), number); CFRelease(number); if (CFSetContainsValue(capabilities, CFSTR(kIOPSCurrentCapacityKey))) { // Initialize kIOPSCurrentCapacityKey // // For Power Manager, we will be sharing capacity with Power Book battery capacities, so // we want a consistent measure. For now we have settled on percentage of full capacity. // elementValue = 100; number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &elementValue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSCurrentCapacityKey), number); CFRelease(number); } if (CFSetContainsValue(capabilities, CFSTR(kIOPSTimeToEmptyKey))) { // Initialize kIOPSTimeToEmptyKey (OS 9 PowerClass.c assumed 100 milliwatt-hours) // elementValue = 100; number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &elementValue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSTimeToEmptyKey), number); CFRelease(number); } if (CFSetContainsValue(capabilities, CFSTR(kIOPSVoltageKey))) { // Initialize kIOPSVoltageKey (OS 9 PowerClass.c assumed millivolts. // (Shouldn't that be 130,000 millivolts for AC?)) // Actually, Power Devices Usage Tables say units will be in Volts. // However we have to check what exponent is used because that may // make the value we get in centiVolts (exp = -2). So it looks like // OS 9 sources said millivolts, but used centivolts. Our final // answer should device by proper exponent to get back to Volts. // elementValue = 13 * 1000 / 100; number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &elementValue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSVoltageKey), number); CFRelease(number); } if (CFSetContainsValue(capabilities, CFSTR(kIOPSCurrentKey))) { // Initialize kIOPSCurrentKey (What would be a good amperage to // initialize to?) Same discussion as for Volts, where the unit // for current is Amps. But with typical exponents (-2), we get // centiAmps. Hmm... typical current for USB may be 500 milliAmps, // which would be .5 A. Since that is not an integer, that may be // why our displays get larger numbers // elementValue = 1; // Just a guess! number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &elementValue); CFDictionarySetValue(upsStoreDict, CFSTR(kIOPSCurrentKey), number); CFRelease(number); } } upsStore = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("UPS Power Manager"), NULL, NULL); // Uniquely name each Sys Config key // snprintf(upsLabelString, kInternalUPSLabelLength, "/UPS%d", upsDataRef->upsID); #if 0 SCLog(TRUE, LOG_NOTICE, CFSTR("What does CreatePowerManagerUPSEntry think our key name is?")); SCLog(TRUE, LOG_NOTICE, CFSTR(" %@%@%@"), kSCDynamicStoreDomainState, CFSTR(kIOPSDynamicStorePath), CFStringCreateWithCStringNoCopy(NULL, upsLabelString, kCFStringEncodingMacRoman, kCFAllocatorNull)); #endif CFStringRef upsLabelCF = NULL; upsLabelCF = CFStringCreateWithCStringNoCopy(NULL, upsLabelString, kCFStringEncodingMacRoman, kCFAllocatorNull); if (upsLabelCF) { upsStoreKey = SCDynamicStoreKeyCreate(kCFAllocatorDefault, CFSTR("%@%@%@"), kSCDynamicStoreDomainState, CFSTR(kIOPSDynamicStorePath), upsLabelCF); CFRelease(upsLabelCF); } if(!upsStoreKey || !SCDynamicStoreSetValue(upsStore, upsStoreKey, upsStoreDict)) { status = SCError(); #if UPS_DEBUG SCLog(TRUE, LOG_NOTICE, CFSTR("UPSSupport: Encountered SCDynamicStoreSetValue error 0x%x"), status); #endif } if (kIOReturnSuccess == status) { // Store our SystemConfiguration variables in our private data // upsDataRef->upsStoreDict = upsStoreDict; upsDataRef->upsStore = upsStore; upsDataRef->upsStoreKey = upsStoreKey; } else { if (upsStoreDict) CFRelease(upsStoreDict); if (upsStore) CFRelease(upsStore); if (upsStoreKey) CFRelease(upsStoreKey); } return status; }
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); }
static CFDictionaryRef entity_one(SCDynamicStoreRef store, CFStringRef key) { CFDictionaryRef ent_dict = NULL; CFDictionaryRef if_dict = NULL; CFStringRef if_key = NULL; CFStringRef if_port; CFMutableDictionaryRef new_dict = NULL; static CFStringRef pre = NULL; CFStringRef serviceID = NULL; CFStringRef serviceType; if (!pre) { pre = SCDynamicStoreKeyCreate(NULL, CFSTR("%@/%@/%@/"), kSCDynamicStoreDomainSetup, kSCCompNetwork, kSCCompService); } /* * get entity dictionary for service */ ent_dict = cache_SCDynamicStoreCopyValue(store, key); if (!isA_CFDictionary(ent_dict)) { goto done; } /* * get interface dictionary for service */ serviceID = parse_component(key, pre); if (!serviceID) { goto done; } if_key = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, kSCDynamicStoreDomainSetup, serviceID, kSCEntNetInterface); if_dict = cache_SCDynamicStoreCopyValue(store, if_key); CFRelease(if_key); if (!isA_CFDictionary(if_dict)) { goto done; } /* check the interface type */ serviceType = CFDictionaryGetValue(if_dict, kSCPropNetInterfaceType); if (!isA_CFString(serviceType) || !CFEqual(serviceType, kSCValNetInterfaceTypeEthernet)) { /* sorry, no AT networking on this interface */ goto done; } /* * get port name (from interface dictionary). */ if_port = CFDictionaryGetValue(if_dict, kSCPropNetInterfaceDeviceName); if (!isA_CFString(if_port)) { goto done; } /* * add ServiceID and interface port name to entity dictionary. */ new_dict = CFDictionaryCreateMutableCopy(NULL, 0, ent_dict); CFDictionarySetValue(new_dict, CFSTR("ServiceID"), serviceID); CFDictionarySetValue(new_dict, kSCPropNetInterfaceDeviceName, if_port); done: if (ent_dict) CFRelease(ent_dict); if (if_dict) CFRelease(if_dict); if (serviceID) CFRelease(serviceID); return (CFDictionaryRef)new_dict; }