- name_len = data_len + 1;
-#endif
- if (!(name = malloc(name_len))) {
- SET_ERROR(ret, SP_ERR_MEM, "registry port name malloc failed");
- goto out;
- }
-#ifdef UNICODE
- WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
-#else
- strcpy(name, data);
-#endif
- if (type == REG_SZ) {
- DEBUG("Found port %s", name);
- if (!(list = list_append(list, name))) {
- SET_ERROR(ret, SP_ERR_MEM, "list append failed");
- goto out;
- }
- }
- index++;
- }
-out:
- free(data);
-out_free_value:
- free(value);
-out_close:
- RegCloseKey(key);
-out_done:
-#endif
-#ifdef __APPLE__
- mach_port_t master;
- CFMutableDictionaryRef classes;
- io_iterator_t iter;
- char *path;
- io_object_t port;
- CFTypeRef cf_path;
- Boolean result;
-
- ret = SP_OK;
-
- DEBUG("Getting IOKit master port");
- if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
- SET_FAIL(ret, "IOMasterPort() failed");
- goto out_done;
- }
-
- DEBUG("Creating matching dictionary");
- if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
- SET_FAIL(ret, "IOServiceMatching() failed");
- goto out_done;
- }
-
- CFDictionarySetValue(classes,
- CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
-
- DEBUG("Getting matching services");
- if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
- SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
- goto out_done;
- }
-
- if (!(path = malloc(PATH_MAX))) {
- SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
- goto out_release;
- }
-
- DEBUG("Iterating over results");
- while ((port = IOIteratorNext(iter))) {
- cf_path = IORegistryEntryCreateCFProperty(port,
- CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
- if (cf_path) {
- result = CFStringGetCString(cf_path,
- path, PATH_MAX, 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:
- free(path);
-out_release:
- IOObjectRelease(iter);
-out_done:
-#endif
-#if defined(__linux__) && defined(HAVE_LIBUDEV)
- struct udev *ud;
- struct udev_enumerate *ud_enumerate;
- struct udev_list_entry *ud_list;
- struct udev_list_entry *ud_entry;
- const char *path;
- struct udev_device *ud_dev, *ud_parent;
- const char *name;
- const char *driver;
- int fd, ioctl_result;
- struct serial_struct serial_info;
-
- ret = SP_OK;
-
- DEBUG("Enumerating tty devices");
- ud = udev_new();
- ud_enumerate = udev_enumerate_new(ud);
- udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
- udev_enumerate_scan_devices(ud_enumerate);
- ud_list = udev_enumerate_get_list_entry(ud_enumerate);
- DEBUG("Iterating over results");
- udev_list_entry_foreach(ud_entry, ud_list) {
- path = udev_list_entry_get_name(ud_entry);
- DEBUG("Found device %s", path);
- ud_dev = udev_device_new_from_syspath(ud, path);
- /* If there is no parent device, this is a virtual tty. */
- ud_parent = udev_device_get_parent(ud_dev);
- if (ud_parent == NULL) {
- DEBUG("No parent device, assuming virtual tty");
- udev_device_unref(ud_dev);
- continue;
- }
- name = udev_device_get_devnode(ud_dev);
- /* 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. */
- driver = udev_device_get_driver(ud_parent);
- if (driver && !strcmp(driver, "serial8250")) {
- DEBUG("serial8250 device, attempting to open");
- if ((fd = open(name, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
- DEBUG("open failed, skipping");
- goto skip;
- }
- ioctl_result = ioctl(fd, TIOCGSERIAL, &serial_info);
- close(fd);
- if (ioctl_result != 0) {
- DEBUG("ioctl failed, skipping");
- goto skip;
- }
- if (serial_info.type == PORT_UNKNOWN) {
- DEBUG("port type is unknown, skipping");
- goto skip;
- }
- }
- DEBUG("Found port %s", name);
- list = list_append(list, name);
-skip:
- udev_device_unref(ud_dev);
- if (!list) {
- SET_ERROR(ret, SP_ERR_MEM, "list append failed");
- goto out;
- }
- }
-out:
- udev_enumerate_unref(ud_enumerate);
- udev_unref(ud);