From 143d6f220efa6b54571a5ef1b18f5faced8543af Mon Sep 17 00:00:00 2001
From: Zebediah Figura <z.figura12@gmail.com>
Date: Sun, 10 Jun 2018 00:44:54 -0500
Subject: [PATCH 35/83] ntdll: Implement NtOpenMutant().

---
 dlls/ntdll/esync.c | 30 ++++++++++++++++++++++++++++++
 dlls/ntdll/esync.h |  2 ++
 dlls/ntdll/sync.c  |  3 +++
 3 files changed, 35 insertions(+)

diff --git a/dlls/ntdll/esync.c b/dlls/ntdll/esync.c
index e6acc9eaf..9da300853 100644
--- a/dlls/ntdll/esync.c
+++ b/dlls/ntdll/esync.c
@@ -539,6 +539,36 @@ NTSTATUS esync_create_mutex( HANDLE *handle, ACCESS_MASK access,
     return ret;
 }
 
+NTSTATUS esync_open_mutex( HANDLE *handle, ACCESS_MASK access,
+    const OBJECT_ATTRIBUTES *attr )
+{
+    enum esync_type type = ESYNC_MUTEX;
+    struct mutex *mutex;
+    NTSTATUS ret;
+    int fd;
+
+    TRACE("name %s.\n", debugstr_us(attr->ObjectName));
+
+    ret = open_esync( &type, &fd, handle, access, attr );
+    if (!ret)
+    {
+        mutex = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mutex) );
+        if (!mutex)
+            return STATUS_NO_MEMORY;
+
+        mutex->obj.type = ESYNC_MUTEX;
+        mutex->obj.fd = fd;
+
+        FIXME("Attempt to open a mutex; this will not work.\n");
+        mutex->tid = 0;
+        mutex->count = 0;
+
+        add_to_list( *handle, &mutex->obj );
+    }
+
+    return ret;
+}
+
 NTSTATUS esync_release_mutex( HANDLE *handle, LONG *prev )
 {
     struct mutex *mutex = esync_get_object( handle );
diff --git a/dlls/ntdll/esync.h b/dlls/ntdll/esync.h
index 1af343ef4..f79b9a06c 100644
--- a/dlls/ntdll/esync.h
+++ b/dlls/ntdll/esync.h
@@ -36,6 +36,8 @@ extern NTSTATUS esync_open_semaphore( HANDLE *handle, ACCESS_MASK access,
     const OBJECT_ATTRIBUTES *attr ) DECLSPEC_HIDDEN;
 extern NTSTATUS esync_open_event( HANDLE *handle, ACCESS_MASK access,
     const OBJECT_ATTRIBUTES *attr ) DECLSPEC_HIDDEN;
+extern NTSTATUS esync_open_mutex( HANDLE *handle, ACCESS_MASK access,
+    const OBJECT_ATTRIBUTES *attr ) DECLSPEC_HIDDEN;
 
 extern NTSTATUS esync_wait_objects( DWORD count, const HANDLE *handles, BOOLEAN wait_any,
                                     BOOLEAN alertable, const LARGE_INTEGER *timeout ) DECLSPEC_HIDDEN;
diff --git a/dlls/ntdll/sync.c b/dlls/ntdll/sync.c
index b39c17c64..2f8d6fe5c 100644
--- a/dlls/ntdll/sync.c
+++ b/dlls/ntdll/sync.c
@@ -575,6 +575,9 @@ NTSTATUS WINAPI NtOpenMutant( HANDLE *handle, ACCESS_MASK access, const OBJECT_A
 
     if ((status = validate_open_object_attributes( attr ))) return status;
 
+    if (do_esync())
+        return esync_open_mutex( handle, access, attr );
+
     SERVER_START_REQ( open_mutex )
     {
         req->access  = access;
-- 
2.20.1

