From ebc82ef6a886955f8e2abff6b1b126c8b41b6515 Mon Sep 17 00:00:00 2001
From: Alex Henrie <alexhenrie24@gmail.com>
Date: Tue, 29 Dec 2015 00:48:02 -0700
Subject: 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 ee2599984a6..994e3676176 100644
--- a/dlls/mountmgr.sys/device.c
+++ b/dlls/mountmgr.sys/device.c
@@ -25,6 +25,9 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
+#ifdef HAVE_TERMIOS_H
+# include <termios.h>
+#endif
 #include <sys/time.h>
 
 #define NONAMELESSUNION
@@ -1038,6 +1041,27 @@ static BOOL create_port_device( DRIVER_OBJECT *driver, int n, const char *unix_p
     if (!unix_path)
         return FALSE;
 
+#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 */
     sprintf( p, "%u", n );
     if (symlink( unix_path, dosdevices_path ) != 0)
-- 
2.12.2

