- DEBUG("Getting matching services");
- if (IOServiceGetMatchingServices(kIOMasterPortDefault, classes,
- &iter) != KERN_SUCCESS) {
- SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
- goto out_done;
- }
-
- DEBUG("Iterating over results");
- while ((port = IOIteratorNext(iter))) {
- cf_path = IORegistryEntryCreateCFProperty(port,
- CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
- if (cf_path) {
- result = CFStringGetCString(cf_path, path, sizeof(path),
- kCFStringEncodingASCII);
- CFRelease(cf_path);
- if (result) {
- DEBUG("Found port %s", path);
- if (!(list = list_append(list, path))) {
- SET_ERROR(ret, SP_ERR_MEM, "list append failed");
- IOObjectRelease(port);
- goto out;
- }
- }
- }
- IOObjectRelease(port);
- }
-out:
- IOObjectRelease(iter);
-out_done:
-#endif
-#ifdef __linux__
- char name[PATH_MAX], target[PATH_MAX];
- struct dirent entry, *result;
- struct serial_struct serial_info;
- int len, fd, ioctl_result;
- DIR *dir;
-
- ret = SP_OK;
-
- DEBUG("Enumerating tty devices");
- if (!(dir = opendir("/sys/class/tty")))
- RETURN_FAIL("could not open /sys/class/tty");
-
- DEBUG("Iterating over results");
- while (!readdir_r(dir, &entry, &result) && result) {
- len = readlinkat(dirfd(dir), entry.d_name, target, sizeof(target));
- if (len <= 0 || len >= (int) sizeof(target)-1)
- continue;
- target[len] = 0;
- if (strstr(target, "virtual"))
- continue;
- snprintf(name, sizeof(name), "/dev/%s", entry.d_name);
- DEBUG("Found device %s", name);
- if (strstr(target, "serial8250")) {
- /* The serial8250 driver has a hardcoded number of ports.
- * The only way to tell which actually exist on a given system
- * is to try to open them and make an ioctl call. */
- DEBUG("serial8250 device, attempting to open");
- if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
- DEBUG("open failed, skipping");
- continue;
- }
- ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
- close(fd);
- if (ioctl_result != 0) {
- DEBUG("ioctl failed, skipping");
- continue;
- }
- if (serial_info.type == PORT_UNKNOWN) {
- DEBUG("port type is unknown, skipping");
- continue;
- }
- }
- DEBUG("Found port %s", name);
- list = list_append(list, name);
- if (!list) {
- SET_ERROR(ret, SP_ERR_MEM, "list append failed");
- break;
- }
- }
- closedir(dir);
-#endif
-
- switch (ret) {
- case SP_OK: