0

I am trying to open(access) a volume of hard drive using below code in app.

  int mode=0;
  int hd_h=-1;
  const char* device  =   "/dev/rdisk1”
  mode=O_RDWR|mode_basic;
  hd_h = open(device, mode);

When I executed above code it returns error "Operation not permitted". Then I searched how it can be achieved and found one answer using kext we can achieve it. I created a sample kext(driver) to access harddrive in kernel space. can anybody assist how can I open volume in kext and return to user space. I have created a IOUserClient subclass and calling IOServiceOpen() in user space .

CFDictionaryRef     matchingDict = NULL;
    io_iterator_t       iter = 0;
    io_service_t        service = 0;
    kern_return_t       kr;
    
    // Create a matching dictionary that will find any USB device
    matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest");
    
    // Create an iterator for all IO Registry objects that match the dictionary
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
        return -1;    
  • What are you ultimately trying to do? If you want to open a block device for read-write access, you normally only need to fulfil 2 conditions: 1. root privileges (you would need these to install a kext anyway) 2. no other active clients, i.e. no volumes mounted. It would be good to know why you can't meet these 2 conditions so we can perhaps find a workaround, especially as kexts have become increasingly impractical recently, and creating a kext is perhaps not the wisest move for a beginner. – pmdj Jul 14 '22 at 09:57
  • @pmdj I am trying to open volume in my app. When I run below code. mode=O_RDONLY|mode_basic; hd_h = open(device, mode); perror("r'"); It gives error “operation not permitted.” I am running my app using root. I read from [link] (https://stackoverflow.com/questions/9215060/opening-disk-device-file-for-write-access-on-mac-os-x) to create a kext.I have created one IOService class and set the property IOUserClientClass on its start method. setProperty("IOUserClientClass","com_osxkernel_driver_IOKitTestUserClient”); now want to open and communicate.its true I am a beginner. – Sudhanshu Pareek Jul 14 '22 at 13:19

0 Answers0