1 #include <sys/sysctl.h> 2 #include <IOKit/IOKitLib.h> 3 #include <errno.h> 4 #include <stdio.h> 5 6 #include "service_helpers.h" 7 8 #define MAX_RETRIES 10 9 10 /* 11 * Helper method to find IOServices needed for testing. Use with T_ASSERT_POSIX_SUCCESS(...) 12 */ 13 int IOTestServiceFindService(const char * name,io_service_t * serviceOut)14IOTestServiceFindService(const char * name, io_service_t * serviceOut) 15 { 16 int err = 0; 17 int retries = 0; 18 io_service_t service = IO_OBJECT_NULL; 19 20 #pragma clang diagnostic push 21 #pragma clang diagnostic ignored "-Wcast-qual" 22 err = sysctlbyname("kern.iokit_test_service_setup", NULL, 0, (void *)name, strlen(name)); 23 #pragma clang diagnostic pop 24 if (err) { 25 goto finish; 26 } 27 28 do { 29 service = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching(name)); 30 if (service == IO_OBJECT_NULL) { 31 sleep(1); 32 retries += 1; 33 } 34 } while (service == IO_OBJECT_NULL && retries <= MAX_RETRIES); 35 36 if (service == IO_OBJECT_NULL) { 37 err = ENOENT; 38 goto finish; 39 } 40 41 err = 0; 42 43 finish: 44 if (serviceOut && service != IO_OBJECT_NULL) { 45 *serviceOut = service; 46 } else if (service != IO_OBJECT_NULL) { 47 IOObjectRelease(service); 48 } 49 50 return err; 51 } 52