// static
void QextSerialEnumerator::scanPortsOSX(QList<QextPortInfo> & infoList)
{
    io_iterator_t serialPortIterator = 0;
    kern_return_t kernResult = KERN_FAILURE;
    CFMutableDictionaryRef matchingDictionary;

    // first try to get any serialbsd devices, then try any USBCDC devices
    if( !(matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue) ) ) {
        qWarning("IOServiceMatching returned a NULL dictionary.");
        return;
    }
    CFDictionaryAddValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));

    // then create the iterator with all the matching devices
    if( IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS ) {
        qCritical() << "IOServiceGetMatchingServices failed, returned" << kernResult;
        return;
    }
    iterateServicesOSX(serialPortIterator, infoList);
    IOObjectRelease(serialPortIterator);
    serialPortIterator = 0;

    if( !(matchingDictionary = IOServiceNameMatching("AppleUSBCDC")) ) {
        qWarning("IOServiceNameMatching returned a NULL dictionary.");
        return;
    }

    if( IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS ) {
        qCritical() << "IOServiceGetMatchingServices failed, returned" << kernResult;
        return;
    }
    iterateServicesOSX(serialPortIterator, infoList);
    IOObjectRelease(serialPortIterator);
}
Esempio n. 2
0
static kern_return_t createSerialIterator(io_iterator_t *serialIterator)
{
    kern_return_t   kernResult;
    mach_port_t     masterPort;
    CFMutableDictionaryRef  classesToMatch;

    if ((kernResult = IOMasterPort(0, &masterPort)) != KERN_SUCCESS)
    {
        printf("IOMasterPort returned %d\n", kernResult);
        return kernResult;
    }

    if ((classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue)) == NULL)
    {
        printf("IOServiceMatching returned NULL\n");
        return kernResult;
    }

    CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),CFSTR(kIOSerialBSDAllTypes));
    kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, serialIterator);
    if (kernResult != KERN_SUCCESS)
    {
        printf("IOServiceGetMatchingServices returned %d\n", kernResult);
    }

    return kernResult;
}
Esempio n. 3
0
kern_return_t SMCOpen(io_connect_t *conn, const char *serviceName)
{
    kern_return_t result;
    mach_port_t   masterPort;
    io_iterator_t iterator;
    io_object_t   device;

    IOMasterPort(MACH_PORT_NULL, &masterPort);

    CFMutableDictionaryRef matchingDictionary = IOServiceMatching(serviceName);
    result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
    if (result != kIOReturnSuccess)
    {
        //printf("Error: IOServiceGetMatchingServices() = %08x\n", result);
        return 1;
    }

    device = IOIteratorNext(iterator);
    IOObjectRelease((io_object_t)iterator);
    if (device == 0)
    {
        //printf("Error: no SMC found\n");
        return 1;
    }

    result = IOServiceOpen(device, mach_task_self(), 0, conn);
    IOObjectRelease(device);
    if (result != kIOReturnSuccess)
    {
        //printf("Error: IOServiceOpen() = %08x\n", result);
        return 1;
    }

    return kIOReturnSuccess;
}
Esempio n. 4
0
static int
getstats(void)
{
    time_t                 now;
    io_iterator_t          drivelist;
    io_registry_entry_t    drive;
    CFMutableDictionaryRef match;
    kern_return_t          status;

    now = time(NULL);	/* register current time and check wether cache can be used */
    if (cache_time + CACHE_TIMEOUT > now) {
        return 0;
    }

    /*  Retrieve a list of drives. */
    match = IOServiceMatching("IOMedia");
    CFDictionaryAddValue(match, CFSTR(kIOMediaWholeKey), kCFBooleanTrue);
    status = IOServiceGetMatchingServices(masterPort, match, &drivelist);
    if (status != KERN_SUCCESS) {
	snmp_log(LOG_ERR, "diskio: couldn't match whole IOMedia devices\n");
/*	fprintf(stderr,"Couldn't match whole IOMedia devices\n"); */
	return(1);
    }

    num_drives = 0;  /* NB: Incremented by handle_drive */
    while ((drive = IOIteratorNext(drivelist)) && (num_drives < MAXDRIVES)) {
	handle_drive(drive, &drivestat[num_drives]);
	IOObjectRelease(drive);
    }
    IOObjectRelease(drivelist);

    cache_time = now;
    return (0);
}
Esempio n. 5
0
void
serial_enumerate_device_names (serial_enumerate_device_names_callback *callback) {
    io_object_t serialPort;
    io_iterator_t serialPortIterator;

// ask for all the serial ports
    IOServiceGetMatchingServices(kIOMasterPortDefault,
       IOServiceMatching(kIOSerialBSDServiceValue),
       &serialPortIterator);

// loop through all the serial ports and add them to the array
    while ((serialPort = IOIteratorNext(serialPortIterator))) {
        CFStringRef string = IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOCalloutDeviceKey),  kCFAllocatorDefault, 0);
        CFIndex length = CFStringGetLength(string);
        CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
        char *buffer = (char *)malloc(maxSize);
        if (CFStringGetCString(string, buffer, maxSize, kCFStringEncodingUTF8)) {
            callback(buffer);
        }
        free(buffer);
        CFRelease(string);
        IOObjectRelease(serialPort);
    }

    IOObjectRelease(serialPortIterator);
}
Esempio n. 6
0
/**
 * Given a BSD device node name, guess its media type
 */
MythMediaType MediaTypeForBSDName(const char *bsdName)
{
    CFMutableDictionaryRef  matchingDict;
    kern_return_t           kernResult;
    io_iterator_t           iter;
    io_service_t            service;
    QString                 msg = QString("MediaTypeForBSDName(%1)")
                                  .arg(bsdName);
    MythMediaType           mediaType;


    if (!bsdName || !*bsdName)
    {
        LOG(VB_GENERAL, LOG_ALERT, msg + " - No name supplied?");
        return MEDIATYPE_UNKNOWN;
    }

    matchingDict = IOBSDNameMatching(sMasterPort, 0, bsdName);
    if (!matchingDict)
    {
        LOG(VB_GENERAL, LOG_ALERT, 
                 msg + " - IOBSDNameMatching() returned a NULL dictionary.");
        return MEDIATYPE_UNKNOWN;
    }

    // Return an iterator across all objects with the matching
    // BSD node name. Note that there should only be one match!
    kernResult = IOServiceGetMatchingServices(sMasterPort, matchingDict, &iter);

    if (KERN_SUCCESS != kernResult)
    {
        LOG(VB_GENERAL, LOG_ALERT,
                 QString(msg + " - IOServiceGetMatchingServices() returned %2")
                         .arg(kernResult));
        return MEDIATYPE_UNKNOWN;
    }
    if (!iter)
    {
        LOG(VB_GENERAL, LOG_ALERT,
                 msg + " - IOServiceGetMatchingServices() returned a NULL "
                       "iterator");
        return MEDIATYPE_UNKNOWN;
    }

    service = IOIteratorNext(iter);

    // Release this now because we only expect
    // the iterator to contain a single io_service_t.
    IOObjectRelease(iter);

    if (!service)
    {
        LOG(VB_GENERAL, LOG_ALERT, 
                 msg + " - IOIteratorNext() returned a NULL iterator");
        return MEDIATYPE_UNKNOWN;
    }
    mediaType = FindMediaType(service);
    IOObjectRelease(service);
    return mediaType;
}
Esempio n. 7
0
static int pyidle_init(PyIdle *self,
                       PyObject *args,
                       PyObject *kwargs)
{
     if (!PyArg_ParseTuple(args, ":__init__"))
        return -1;

     if (self)
     {
         io_iterator_t iterator;
         int ret;

         if ((ret = IOMasterPort(MACH_PORT_NULL, &self->machPort)) != kIOReturnSuccess)
         {
             PyErr_Format(PyExc_RuntimeError, "IOMasterPort failed: %d", ret);
             return -1;
         }

         if ((ret = IOServiceGetMatchingServices(self->machPort, IOServiceMatching("IOHIDSystem"), &iterator)) != kIOReturnSuccess)
         {
             PyErr_Format(PyExc_RuntimeError, "IOServiceGetMatchingServices failed: %d", ret);
             return -1;
         }

         if (!(self->regEntry = IOIteratorNext(iterator)))
         {
             PyErr_SetString(PyExc_RuntimeError, "Empty IO iterator");
             return -1;
         }

         IOObjectRelease(iterator);
     }

     return 0;
}
Esempio n. 8
0
bool isComputerIdle(int idle_secs)
{
    int64_t os_idle_secs = 0;
    io_iterator_t iter = 0;
    if (IOServiceGetMatchingServices(kIOMasterPortDefault, 
                                     IOServiceMatching("IOHIDSystem"), 
                                     &iter) == KERN_SUCCESS)
    {
        io_registry_entry_t entry = IOIteratorNext(iter);
        if (entry)
        {
            CFMutableDictionaryRef dict = NULL;
            if (IORegistryEntryCreateCFProperties(entry, &dict, 
                                                  kCFAllocatorDefault, 
                                                  0) == KERN_SUCCESS)
            {
                CFNumberRef obj = static_cast<CFNumberRef>
                (CFDictionaryGetValue(dict, 
                                      CFSTR("HIDIdleTime")));
                if (obj)
                {
                    int64_t nanoseconds = 0;
                    if (CFNumberGetValue(obj, kCFNumberSInt64Type, 
                                         &nanoseconds))
                    {
// Divide by 10^9 to convert from nanoseconds to seconds.
                        os_idle_secs = (nanoseconds >> 30); 
                    }
                }
            }
CFArrayRef getSerialModemList() {
    uint32_t serialModemCount = 0;
    CFMutableArrayRef devicePaths = CFArrayCreateMutable(kCFAllocatorDefault, serialModemCount, &kCFTypeArrayCallBacks);
    
    kern_return_t kernalReturnValue;
    
    CFMutableDictionaryRef deviceMatchingDictionary = NULL;
    deviceMatchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue);
    io_iterator_t deviceIterator;
    kernalReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, deviceMatchingDictionary,
                                                     &deviceIterator);
    if (kernalReturnValue != KERN_SUCCESS) {
        return NULL;
    }
    
    io_service_t device;
    while ((device = IOIteratorNext(deviceIterator)))
    {
        CFMutableDictionaryRef deviceProperties = NULL;
        kernalReturnValue = IORegistryEntryCreateCFProperties(device, &deviceProperties, kCFAllocatorDefault, kNilOptions);
        if (kernalReturnValue != KERN_SUCCESS) {
            return NULL;
        }
        
        CFStringRef devicePathKey = CFStringCreateWithCString(kCFAllocatorDefault, kIOCalloutDeviceKey, kCFStringEncodingASCII);
        CFStringRef devicePath = CFDictionaryGetValue(deviceProperties, devicePathKey);
        IOObjectRelease(device);
        CFArrayAppendValue(devicePaths, devicePath);
    }
    
    IOObjectRelease(deviceIterator);
    return CFArrayCreateCopy(kCFAllocatorDefault, devicePaths);
}
Esempio n. 10
0
// This function is much inspired by record_all_devices() from
// http://opensource.apple.com/source/system_cmds/system_cmds-230/iostat.tproj/iostat.c
static void measureIoLoad(meter_sysload_t *load) {
  dynamic_accumulator_t *ioLoad = (dynamic_accumulator_t*)load->user;
  dynamic_accumulator_startReporting(ioLoad);
  
  // For all devices..
  io_iterator_t drivelist;
  io_registry_entry_t drive;
  CFMutableDictionaryRef match;
  kern_return_t status;
  
  // Get an iterator for IOMedia objects.
  match = IOServiceMatching("IOMedia");
  CFDictionaryAddValue(match, CFSTR(kIOMediaWholeKey), kCFBooleanTrue);
  status = IOServiceGetMatchingServices(masterPort, match, &drivelist);
  assert(status == KERN_SUCCESS);
  
  // Scan all of the IOMedia objects, report each object that has a parent
  // IOBlockStorageDriver
  while ((drive = IOIteratorNext(drivelist))) {
    reportDrive(ioLoad, drive);
    
    IOObjectRelease(drive);
  }
  IOObjectRelease(drivelist);
  
  load->ioLoad = dynamic_accumulator_getLoadPercentage(ioLoad);
}
Esempio n. 11
0
int
main(void)
{
    kern_return_t          kr;
    io_iterator_t          io_hw_sensors;
    io_service_t           io_hw_sensor;
    CFMutableDictionaryRef sensor_properties;
    CFStringEncoding       systemEncoding = CFStringGetSystemEncoding();
   
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
             IOServiceNameMatching("IOHWSensor"), &io_hw_sensors);
   
    while ((io_hw_sensor = IOIteratorNext(io_hw_sensors))) {
        kr = IORegistryEntryCreateCFProperties(io_hw_sensor, &sensor_properties,
                 kCFAllocatorDefault, kNilOptions);
        if (kr == KERN_SUCCESS)
            printTemperatureSensor(sensor_properties, systemEncoding);
   
        CFRelease(sensor_properties);
        IOObjectRelease(io_hw_sensor);
    }
   
    IOObjectRelease(io_hw_sensors);
   
    exit(kr);
}
Esempio n. 12
0
io_connect_t open_and_connect_user_client(char* service_name, int type) {
    kern_return_t err;
    io_connect_t conn = MACH_PORT_NULL;
    
    CFMutableDictionaryRef matching = IOServiceMatching("IOGraphicsAccelerator2");
    if(!matching){
        printf("unable to create service matching dictionary\n");
        return conn;
    }
    
    io_iterator_t iterator;
    err = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &iterator);
    if (err != KERN_SUCCESS){
        printf("no matches\n");
        return conn;
    }
    
    io_service_t service = IOIteratorNext(iterator);
    
    if (service == IO_OBJECT_NULL){
        printf("unable to find service\n");
        return conn;
    }
    printf("got service: %x\n", service);
    
    
    err = IOServiceOpen(service, mach_task_self(), type, &conn);
    if (err != KERN_SUCCESS){
        printf("unable to get user client connection\n");
        return MACH_PORT_NULL;
    }
    //getchar();
    printf("got userclient connection: %x\n", conn);
    return conn;
}
Esempio n. 13
0
IOReturn darwin_get_service_from_location_id ( unsigned int location_id, io_service_t *service )
{
	io_iterator_t deviceIterator;
	unsigned int found_location_id;

	CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);

	if (!matchingDict)
		return kIOReturnError;

	IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &deviceIterator);

	while ((*service = IOIteratorNext (deviceIterator))) {
		/* get the location from the i/o registry */

		found_location_id = GetLocationID(*service);

		if (location_id == found_location_id) {
		  found_location_id = GetLocationID(*service);
		  IOObjectRelease(deviceIterator);
		  return kIOReturnSuccess;
		}
	}
	/* not found, shouldn't happen */
	IOObjectRelease(deviceIterator);
	return kIOReturnError;
}
Esempio n. 14
0
// Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
// releasing the iterator after the caller is done with it.
static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices, char* interfaceName)
{
    kern_return_t    kernResult;
    CFMutableDictionaryRef  matchingDict;
    //CFMutableDictionaryRef  propertyMatchDict;

    // Ethernet interfaces are instances of class kIOEthernetInterfaceClass.
    // IOServiceMatching is a convenience function to create a dictionary with the key kIOProviderClassKey and
    // the specified value.
  // Note by mike: we're not using this method and then filtering by kIOPropertyMatchKey anymore,
  // because that way we were getting authorization errors for some people for whom, for whatever reason,
  // kIOPropertyMatchKey was not TRUE for their en0 interface
    //matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);

    matchingDict = IOBSDNameMatching(kIOMasterPortDefault, 0, interfaceName);

    if (NULL == matchingDict) {
      printf("IOBSDNameMatching returned a NULL dictionary.\n");
    }

    // IOServiceGetMatchingServices retains the returned iterator, so release the iterator when we're done with it.
    // IOServiceGetMatchingServices also consumes a reference on the matching dictionary so we don't need to release
    // the dictionary explicitly.
    kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, matchingServices);
    if (KERN_SUCCESS != kernResult) {
        printf("IOServiceGetMatchingServices returned 0x%08x\n", kernResult);
    }

    return kernResult;
}
Esempio n. 15
0
 kern_return_t 
 CPUTemp::SMCOpen(void)
 {
     kern_return_t result;
     io_iterator_t iterator;
     io_object_t   device;
 
     CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
     result = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iterator);
     if (result != kIOReturnSuccess)
     {
         printf("Error: IOServiceGetMatchingServices() = %08x\n", result);
         return 1;
     }
 
     device = IOIteratorNext(iterator);
     IOObjectRelease(iterator);
     if (device == 0)
     {
         printf("Error: no SMC found\n");
         return 1;
     }
 
     result = IOServiceOpen(device, mach_task_self(), 0, &conn);
     IOObjectRelease(device);
     if (result != kIOReturnSuccess)
     {
         printf("Error: IOServiceOpen() = %08x\n", result);
         return 1;
     }
 
     return kIOReturnSuccess;
 }
Esempio n. 16
0
int inputopen(usbdevice* kb){
    // Open master port (if not done yet)
    static mach_port_t master = 0;
    kern_return_t res;
    if(!master&& (res = IOMasterPort(bootstrap_port, &master)) != KERN_SUCCESS){
        master = 0;
        printf("Unable to open master port: 0x%08x\n", res);
        return 0;
    }
    // Open an HID service
    io_iterator_t iter;
    if((res = IOServiceGetMatchingServices(master, IOServiceMatching(kIOHIDSystemClass), &iter)) != KERN_SUCCESS){
        printf("Unable to get input service iterator: 0x%08x\n", res);
        return 0;
    }
    if((res = IOServiceOpen(IOIteratorNext(iter), mach_task_self(), kIOHIDParamConnectType, &kb->event)) != KERN_SUCCESS){
        IOObjectRelease(iter);
        printf("Unable to open IO service: 0x%08x\n", res);
        kb->event = 0;
        return 0;
    }
    IOObjectRelease(iter);
    clearkeys(kb);
    return 1;
}
Esempio n. 17
0
io_connect_t get_event_driver(void){
	static  mach_port_t sEventDrvrRef = 0;
	mach_port_t masterPort, service, iter;
	kern_return_t    kr;
	
	if (!sEventDrvrRef)
	{
		// Get master device port
		kr = IOMasterPort( bootstrap_port, &masterPort );
		check( KERN_SUCCESS == kr);
		
		kr = IOServiceGetMatchingServices( masterPort, IOServiceMatching(
																		 kIOHIDSystemClass ), &iter );
		check( KERN_SUCCESS == kr);
		
		service = IOIteratorNext( iter );
		check( service );
		
		kr = IOServiceOpen( service, mach_task_self(),
							kIOHIDParamConnectType, &sEventDrvrRef );
		check( KERN_SUCCESS == kr );
		
		IOObjectRelease( service );
		IOObjectRelease( iter );
	}
	return sEventDrvrRef;
}
Esempio n. 18
0
QueryData genBlockDevs(QueryContext& context) {
  QueryData results;

  auto matching = IOServiceMatching(kIOMediaClassName_);
  if (matching == nullptr) {
    // No devices matched IOMedia.
    return {};
  }

  io_iterator_t it;
  auto kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matching, &it);
  if (kr != KERN_SUCCESS) {
    return {};
  }

  std::vector<std::string> whole_devices;

  io_service_t device;
  while ((device = IOIteratorNext(it))) {
    genIOMediaDevice(device, whole_devices, results);
    IOObjectRelease(device);
  }

  IOObjectRelease(it);
  return results;
}
Esempio n. 19
0
static io_object_t _findAppleRemoteDevice(const char *devName)
{
    CFMutableDictionaryRef hidMatchDictionary = 0;
    io_iterator_t          hidObjectIterator = 0;
    io_object_t            hidDevice = 0;
    IOReturn               ioReturnValue;


    hidMatchDictionary = IOServiceMatching(devName);

    // check for matching devices
    ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                                 hidMatchDictionary,
                                                 &hidObjectIterator);

    if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0))
        hidDevice = IOIteratorNext(hidObjectIterator);
    else
        LOG(VB_GENERAL, LOG_ERR, LOC +
            QString("_findAppleRemoteDevice(%1) failed").arg(devName));

    // IOServiceGetMatchingServices consumes a reference to the dictionary,
    // so we don't need to release the dictionary ref.
    hidMatchDictionary = 0;
    return hidDevice;
}
Esempio n. 20
0
// Opens HID service. Returns kIOReturnSuccess on success.
static int open_iohid(io_connect_t* connection){
    io_iterator_t iter;
    io_service_t service;
    // Open master port (if not done yet)
    static mach_port_t master = 0;
    kern_return_t res;
    if(!master && (res = IOMasterPort(bootstrap_port, &master)) != kIOReturnSuccess){
        master = 0;
        ckb_err("Unable to open master port: 0x%08x\n", res);
        goto failure;
    }
    // Open the HID service
    if((res = IOServiceGetMatchingServices(master, IOServiceMatching(kIOHIDSystemClass), &iter)) != kIOReturnSuccess)
        goto failure;
    service = IOIteratorNext(iter);
    if(!service){
        res = kIOReturnNotOpen;
        goto failure_release_iter;
    }
    if((res = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, connection)) != kIOReturnSuccess){
        *connection = 0;
        goto failure_release_iter;
    }
    // Finished; release objects and return success
    IOObjectRelease(service);
    failure_release_iter:
    IOObjectRelease(iter);
    failure:
    return res;
}
Esempio n. 21
0
SCM usb_device(SCM name)
{
	io_iterator_t iterator = 0;
	
	CFDictionaryRef matchDict = IOServiceMatching(kIOUSBDeviceClassName);
	IOServiceGetMatchingServices(kIOMasterPortDefault, matchDict, &iterator);

	io_service_t device;
	int cnt = 0;
	
	int found_device = false;

	while(device = IOIteratorNext(iterator))
	{
		io_name_t dev_name;

		if(IORegistryEntryGetName(device, dev_name) == KERN_SUCCESS)
			if(!strncmp(dev_name,scm_to_locale_string(name),strlen(scm_to_locale_string(name))))
				found_device = true;
		
		IOObjectRelease(device);

		++cnt;
	}
	
	IOObjectRelease(iterator);
	
	return scm_from_int(found_device);
}
Esempio n. 22
0
int PCIDriver_setupDriver()
{
	kern_return_t   kern_result;
    io_iterator_t   iterator;
    bool            driverFound = false;
    io_service_t    local_driver_service;
    
	// get services
    kern_result = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(kPcmMsrDriverClassName), &iterator);
	if (kern_result != KERN_SUCCESS) {
		fprintf(stderr, "[error] IOServiceGetMatchingServices returned 0x%08x\n", kern_result);
        return kern_result;
    }
	
	// find service
	while ((local_driver_service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
        driverFound = true;
        break;
    }
	if (driverFound == false) {  
        fprintf(stderr, "[error] No matching drivers found \"%s\".\n", kPcmMsrDriverClassName);
        return KERN_FAILURE;
    }
	IOObjectRelease(iterator);

	// connect to service
    kern_result = IOServiceOpen(local_driver_service, mach_task_self(), 0, &PCIDriver_connect);
    if (kern_result != KERN_SUCCESS) {
        fprintf(stderr, "[error] IOServiceOpen returned 0x%08x\n", kern_result);
		return kern_result;
    }
	
	return KERN_SUCCESS;
}
Esempio n. 23
0
int uvccGetCamList(uvccCam ***list)
{
	CFDictionaryRef	mRef;
	io_iterator_t devIter;
	kern_return_t kr;
	int nCams;
	/* make sure we've been initialized */
	if(!uvcc_port)
	{
		fprintf(stderr, UVCC_INIT_ERROR_MSG);
		return -1;
	}
	/* get the matching dic, get_usb_service_dic dumps error if needed */
	if(!(mRef = get_usb_service_dic())) return -1;
	/* get ALL the cams! */
	kr = IOServiceGetMatchingServices(uvcc_port, mRef, &devIter);
	if(kr != kIOReturnSuccess || !IOIteratorIsValid(devIter))
	{
		uvcc_err("uvccGetCamList: IOServiceGetMatchingServices", kr);
		return -1;
	}
	/* normally one ref of mdRef should be consumed by IOServiceGet... but a
	   bug in os x < Tiger can cause that to be missed. unfortunately you
	   can't get the retain count on a completely released obj in a safe manor
	   since they may or may not be free'd.
	if(CFGetRetainCount(mRef) > 0) CFRelease(mRef); */
	nCams = get_cam_list(devIter, list);
	IOObjectRelease(devIter);
	return nCams;
}
Esempio n. 24
0
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
{
    kern_return_t       kernResult;
    mach_port_t     masterPort;
    CFMutableDictionaryRef  classesToMatch;

    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
    if ( KERN_SUCCESS != kernResult ) {
        printf( "IOMasterPort returned %d\n", kernResult );
    }

    classesToMatch = IOServiceMatching( kIOCDMediaClass );
    if ( classesToMatch == NULL ) {
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
    } else {
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
    }
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
    if ( KERN_SUCCESS != kernResult )
    {
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
    }

    return kernResult;
}
Esempio n. 25
0
static kern_return_t
FindModems(io_iterator_t *matchingServices)
{
    kern_return_t           kernResult;
    mach_port_t             masterPort;
    CFMutableDictionaryRef  classesToMatch;

    kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
    if (KERN_SUCCESS != kernResult)
    {
        /* printf("IOMasterPort returned %d\n", kernResult); */
        goto exit;
    }

    classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
    if (classesToMatch != NULL)
    {
        CFDictionarySetValue(classesToMatch,
                             CFSTR(kIOSerialBSDTypeKey),
                             CFSTR(kIOSerialBSDModemType));
    }

    kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, matchingServices);
    if (KERN_SUCCESS != kernResult)
    {
        /* printf("IOServiceGetMatchingServices returned %d\n", kernResult); */
        goto exit;
    }

exit:
    return kernResult;
}
Esempio n. 26
0
bool fetch_raw_smbios(unsigned char** data, unsigned int* length)
{
  CFDataRef               value = NULL;
  io_object_t             device;
  io_iterator_t           objectIterator;
  CFMutableDictionaryRef  properties = NULL;
  mach_port_t             master_port;
  bool                    result = false;
  
  if (kIOReturnSuccess == IOMasterPort(MACH_PORT_NULL, &master_port)) {
    if (kIOReturnSuccess == IOServiceGetMatchingServices(master_port, IOServiceMatching("AppleSMBIOS"), &objectIterator)) {
      while ((device = IOIteratorNext(objectIterator))) {
        if (kIOReturnSuccess == IORegistryEntryCreateCFProperties(device, &properties, kCFAllocatorDefault, kNilOptions)) {
          if (CFDictionaryGetValueIfPresent(properties, CFSTR("SMBIOS"), (const void **)&value)) {
            *length = CFDataGetLength(value);
            *data = new uint8[*length];
            memcpy(*data, (uint8 *)CFDataGetBytePtr(value), *length);
            result = true;
            break;
          }
          CFRelease(properties);
        }
        IOObjectRelease(device);
      }
      IOObjectRelease(objectIterator);
    }
    mach_port_deallocate(mach_task_self(), master_port);
  }
  return result;
}
void enkript_prologue(void)
{
  if (IOConnectCallMethod == NULL) die("IOConnectCallMethod unavailable, require version >= 10.5");
  /* get iterator to browse drivers of the chosen class
   */
  io_iterator_t iterator;
  if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("com_enkript_driver_Service"), &iterator) != KERN_SUCCESS) die("IOServiceGetMatchingServices failed");
  /* browse drivers
   */
  for (io_service_t service = IOIteratorNext(iterator); service != IO_OBJECT_NULL; service = IOIteratorNext(iterator))
  {
    debug("com_enkript_driver_Service instance found!");
    /* open service
     */
    io_connect_t connect = IO_OBJECT_NULL;
    if (IOServiceOpen(service, mach_task_self(), 0, &connect) != KERN_SUCCESS) die("IOServiceOpen failed");
    /* call driver's open method
     */
    if (IOConnectCallMethod(connect, 0 /* open, TOFIX */, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) != KERN_SUCCESS) die("IOConnectCallMethod failed");
    /* call driver's hello method
     */
    uint64_t buf = getpid();
    if (IOConnectCallMethod(connect, 2 /* hello, TOFIX */, &buf, 1, NULL, 0, NULL, NULL, NULL, NULL) != KERN_SUCCESS) die("IOConnectCallMethod failed");
    /* call driver's close method
     */
    if (IOConnectCallMethod(connect, 1 /* close, TOFIX */, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL) != KERN_SUCCESS) die("IOConnectCallMethod failed");
    /* close service
     */
    if (IOServiceClose(connect) != KERN_SUCCESS) die("IOServiceClose failed");
	}
  IOObjectRelease(iterator);
}
Esempio n. 28
0
/*
 * Try to record the named device as interesting.  It
 * must be an IOMedia device.
 */
static int
record_one_device(char *name)
{
    io_iterator_t drivelist;
    io_registry_entry_t drive;
    kern_return_t status;

    /*
     * Find the device.
     */
    status = IOServiceGetMatchingServices(masterPort,
                                          IOBSDNameMatching(masterPort, kNilOptions, name),
                                          &drivelist);
    if (status != KERN_SUCCESS)
        errx(1, "couldn't match '%s'", name);

    /*
     * Get the first match (should only be one)
     */
    if ((drive = IOIteratorNext(drivelist)) == NULL)
        errx(1, "'%s' not found", name);
    if (!IOObjectConformsTo(drive, "IOMedia"))
        errx(1, "'%s' is not a storage device", name);

    /*
     * Record the device.
     */
    if (record_device(drive))
        errx(1, "could not record '%s' for monitoring", name);

    IOObjectRelease(drive);
    IOObjectRelease(drivelist);

    return(0);
}
Esempio n. 29
0
boolean_t FindDevicesUsingOldMethod(SInt32 peripheralDeviceType, mach_port_t masterPort, io_iterator_t *iterator)
{
    CFMutableDictionaryRef	matchingDict = NULL;
    boolean_t				result = false;
    
    // Set up a matching dictionary to search the I/O Registry for SCSI devices
    // we are interested in.
    CreateMatchingDictionaryForOldMethod(peripheralDeviceType, &matchingDict);
    
    if (matchingDict == NULL) {
        fprintf(stderr, "Couldn't create a matching dictionary.\n");
    }
    else {
		kern_return_t	kr;
        
        // Now search I/O Registry for matching devices.
        kr = IOServiceGetMatchingServices(masterPort, matchingDict, iterator);

        if (*iterator && kr == kIOReturnSuccess) {
            result = true;
        }
    }
    
    // IOServiceGetMatchingServices consumes a reference to the
    // matching dictionary, so we don't need to release the dictionary ref.

    return result;
}
Esempio n. 30
0
// makes a list of ATA or SCSI devices for the DEVICESCAN directive of
// smartd.  Returns number N of devices, or -1 if out of
// memory. Allocates N+1 arrays: one of N pointers (devlist); the
// other N arrays each contain null-terminated character strings.  In
// the case N==0, no arrays are allocated because the array of 0
// pointers has zero length, equivalent to calling malloc(0).
int make_device_names (char*** devlist, const char* name) {
  IOReturn err;
  io_iterator_t i;
  io_object_t device = MACH_PORT_NULL;
  int result;
  int index;

  // We treat all devices as ATA so long as they support SMARTLib.
  if (strcmp (name, "ATA") != 0)
    return 0;

  err = IOServiceGetMatchingServices 
    (kIOMasterPortDefault, IOServiceMatching (kIOBlockStorageDeviceClass), &i);
  if (err != kIOReturnSuccess)
    return -1;

  // Count the devices.
  result = 0;
  while ((device = IOIteratorNext (i)) != MACH_PORT_NULL) {
    if (is_smart_capable (device))
      result++;
    IOObjectRelease (device);
  }

  // Create an array of service names.
  IOIteratorReset (i);
  *devlist = (char**)Calloc (result, sizeof (char *)); 
  if (! *devlist)
    goto error;
  index = 0;
  while ((device = IOIteratorNext (i)) != MACH_PORT_NULL) {
    if (is_smart_capable (device))
      {
	io_string_t devName;
	IORegistryEntryGetPath(device, kIOServicePlane, devName);
	(*devlist)[index] = CustomStrDup (devName, true, __LINE__, __FILE__);
	if (! (*devlist)[index])
	  goto error;
	index++;
      }
    IOObjectRelease (device);
  }

  IOObjectRelease (i);
  return result;

 error:
  if (device != MACH_PORT_NULL)
    IOObjectRelease (device);
  IOObjectRelease (i);
  if (*devlist)
    {
      for (index = 0; index < result; index++)
	if ((*devlist)[index])
	  FreeNonZero ((*devlist)[index], 0, __LINE__, __FILE__);
      FreeNonZero (*devlist, result * sizeof (char *), __LINE__, __FILE__);
    }
  return -1;
}