From c27588bc3f2e5afd8c71f727023bbd167d1195e9 Mon Sep 17 00:00:00 2001
From: Alex Henrie <alexhenrie24@gmail.com>
Date: Tue, 29 Dec 2015 00:48:02 -0700
Subject: [PATCH] mountmgr.sys: Do a device check before returning a default
 serial port name.

Fixes https://bugs.winehq.org/show_bug.cgi?id=39793
---
 dlls/mountmgr.sys/device.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/dlls/mountmgr.sys/device.c b/dlls/mountmgr.sys/device.c
index 7c34f8b3db7..dd23b2baa21 100644
--- a/dlls/mountmgr.sys/device.c
+++ b/dlls/mountmgr.sys/device.c
@@ -26,6 +26,9 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <fcntl.h>
+#ifdef HAVE_TERMIOS_H
+# include <termios.h>
+#endif
 #include <sys/time.h>
 #ifdef HAVE_SYS_IOCTL_H
 # include <sys/ioctl.h>
@@ -1253,6 +1256,27 @@ static BOOL create_port_device( DRIVER_OBJECT *driver, int n, const char *unix_p
 
     sprintfW( dos_name, dos_name_format, n );
 
+#ifdef linux
+    /* Serial port device files almost always exist on Linux even if the corresponding serial
+     * ports don't exist. Do a basic functionality check before advertising a serial port. */
+    if (driver == serial_driver)
+    {
+        struct termios tios;
+        int fd;
+
+        if ((fd = open( unix_path, O_RDONLY )) == -1)
+            return FALSE;
+
+        if (tcgetattr( fd, &tios ) == -1)
+        {
+            close( fd );
+            return FALSE;
+        }
+
+        close( fd );
+    }
+#endif
+
     /* create DOS device */
     unlink( dosdevices_path );
     if (symlink( unix_path, dosdevices_path ) != 0)
-- 
2.17.1

