-
-// 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;
-}
-
-// Information that we keep about each device.
-
-static struct {
- io_object_t ioob;
- IOCFPlugInInterface **plugin;
- IOATASMARTInterface **smartIf;
-} devices[20];
-
-// Like open(). Return non-negative integer handle, only used by the
-// functions below. type=="ATA" or "SCSI". The return value is
-// an index into the devices[] array. If the device can't be opened,
-// sets errno and returns -1.
-// Acceptable device names are:
-// /dev/disk*
-// /dev/rdisk*
-// disk*
-// IOService:*
-// IODeviceTree:*
-int deviceopen(const char *pathname, char *type){