From a6835bdb5572764029a3ddf46205616d3d5f5abe Mon Sep 17 00:00:00 2001 From: newHeiko <35038740+newHeiko@users.noreply.github.com> Date: Mon, 17 Nov 2025 21:03:52 +0100 Subject: [PATCH 1/3] Fix testPollPosix to work with changes from #54bd919 --- src/test/c/testPollPosix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/c/testPollPosix.c b/src/test/c/testPollPosix.c index b075591a..2d4a5916 100644 --- a/src/test/c/testPollPosix.c +++ b/src/test/c/testPollPosix.c @@ -92,7 +92,7 @@ int configPort(serialPort *port) // Apply changes if (fcntl(port->handle, F_SETFL, flags)) return 0; - if (setConfigOptions(port->handle, baudRate, &options)) + if (setCustomBaudRate(port->handle, baudRate)) return 0; return 1; } From ce34ab622dee212965bf4b561cc8f8b31b746b8a Mon Sep 17 00:00:00 2001 From: newHeiko <35038740+newHeiko@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:57:45 +0100 Subject: [PATCH 2/3] Pull symlink search into second loop... ...to ensure symlinks to Bluetooth devices are found as well. --- src/main/c/Posix/PosixHelperFunctions.c | 46 +++++++++++++++++++------ 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/c/Posix/PosixHelperFunctions.c b/src/main/c/Posix/PosixHelperFunctions.c index 090d0500..a9b917c0 100644 --- a/src/main/c/Posix/PosixHelperFunctions.c +++ b/src/main/c/Posix/PosixHelperFunctions.c @@ -739,7 +739,7 @@ void searchForComPorts(serialPortVector* comPorts) closedir(directoryIterator); } - // Search through the system DEV directory for Bluetooth and PTY devices and symlinks + // Search through the system DEV directory for Bluetooth and PTY devices directoryIterator = opendir("/dev/"); if (directoryIterator) { @@ -760,17 +760,41 @@ void searchForComPorts(serialPortVector* comPorts) pushBack(comPorts, fileName, "PTY Device", "Pseudo-Terminal Device", "0-0", "Unknown", "Unknown", "Unknown", -1, -1, 0); else if (isBluetoothDevice(fileName)) pushBack(comPorts, fileName, "Bluetooth Device", "Bluetooth Device", "0-0", "Unknown", "Unknown", "Unknown", -1, -1, 0); - else + + // Read next TTY directory entry + directoryEntry = readdir(directoryIterator); + } + closedir(directoryIterator); + } + + // Search through the system DEV directory again for symlinks + // Can't combine with loop above because otherwise symlinks to Bluetooth devices + // will only be found if they come after Bluetooth device in the iterator + directoryIterator = opendir("/dev/"); + if (directoryIterator) + { + struct dirent *directoryEntry = readdir(directoryIterator); + while (directoryEntry) + { + // Ensure that the file name buffer is large enough + if ((16 + strlen(directoryEntry->d_name)) > fileNameMaxLength) { - // Copy symlinked port details from its existing TTY enumeration - char *symlink = isSymlinkedTtyDevice(fileName); - if (symlink) - { - const serialPort *port = fetchPort(comPorts, symlink); - if (port) - pushBack(comPorts, fileName, port->friendlyName, port->portDescription, port->portLocation, port->serialNumber, port->manufacturer, port->deviceDriver, port->vendorID, port->productID, 1); - free(symlink); - } + fileNameMaxLength = 16 + strlen(directoryEntry->d_name); + fileName = (char*)realloc(fileName, fileNameMaxLength); + } + + // Build full filename + strcpy(fileName, "/dev/"); + strcat(fileName, directoryEntry->d_name); + // Find target of symbolic link if it is one + char *targetDevice = isSymlinkedTtyDevice(fileName); + if (targetDevice) + { + // check if symlink points to a serial port + const serialPort *port = fetchPort(comPorts, targetDevice); + if (port) + pushBack(comPorts, fileName, port->friendlyName, port->portDescription, port->portLocation, port->serialNumber, port->manufacturer, port->deviceDriver, port->vendorID, port->productID, 1); + free(targetDevice); } // Read next TTY directory entry From 2af891c22795e705b2e1e46db3ef5ab710eeb5ee Mon Sep 17 00:00:00 2001 From: newHeiko <35038740+newHeiko@users.noreply.github.com> Date: Sun, 23 Nov 2025 18:06:53 +0100 Subject: [PATCH 3/3] Change symlink search to only find target device. Leaves check if symlink points to serial port to searchForComPorts. Fixes #567 also for relative symlinks. --- src/main/c/Posix/PosixHelperFunctions.c | 27 +++++-------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/main/c/Posix/PosixHelperFunctions.c b/src/main/c/Posix/PosixHelperFunctions.c index a9b917c0..f5504a83 100644 --- a/src/main/c/Posix/PosixHelperFunctions.c +++ b/src/main/c/Posix/PosixHelperFunctions.c @@ -319,34 +319,17 @@ static char isPtyDevice(const char *deviceLink) return isPtyDevice; } -static char* isSymlinkedTtyDevice(const char *deviceLink) +static char* isSymlink(const char *deviceLink) { // Attempt to read the path that the potential TTY device link points to struct stat fileInfo; char *symlink = NULL; if ((lstat(deviceLink, &fileInfo) == 0) && fileInfo.st_size && ((fileInfo.st_mode & S_IFMT) == S_IFLNK)) { - ssize_t bufferSize = fileInfo.st_size + 1; - symlink = (char*)malloc(bufferSize); - if (symlink) - { - ssize_t symlinkSize = readlink(deviceLink, symlink, bufferSize); - if (symlinkSize >= 0) - { - symlink[symlinkSize] = '\0'; - if (!strstr(symlink, "dev/tty")) - { - free(symlink); - symlink = NULL; - } - } - else - { - free(symlink); - symlink = NULL; - } - } + // realpath automatically adds /dev and allocates the correct buffer size + symlink = realpath(deviceLink, NULL); } + // Note: Calling function will check if symlink points to serial port return symlink; } @@ -787,7 +770,7 @@ void searchForComPorts(serialPortVector* comPorts) strcpy(fileName, "/dev/"); strcat(fileName, directoryEntry->d_name); // Find target of symbolic link if it is one - char *targetDevice = isSymlinkedTtyDevice(fileName); + char *targetDevice = isSymlink(fileName); if (targetDevice) { // check if symlink points to a serial port