From 4863e6b323f7ecc1fceaff42a573cd0b9a07f3d5 Mon Sep 17 00:00:00 2001
From: Zebediah Figura <z.figura12@gmail.com>
Date: Sat, 4 Aug 2018 15:15:12 -0500
Subject: [PATCH] ntdll, server: Abort if esync is enabled for the server but
 not the client, and vice versa.

---
 dlls/ntdll/esync.c  | 28 +++++++++++++++++++++++++++-
 dlls/ntdll/loader.c |  3 +--
 server/esync.c      | 12 ++++++++++++
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/dlls/ntdll/esync.c b/dlls/ntdll/esync.c
index 0625de9fff0..522704f757f 100644
--- a/dlls/ntdll/esync.c
+++ b/dlls/ntdll/esync.c
@@ -115,10 +115,29 @@ static void **shm_addrs;
 static int shm_addrs_size;  /* length of the allocated shm_addrs array */
 static long pagesize;
 
+static NTSTATUS create_esync( enum esync_type type, HANDLE *handle,
+    ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr, int initval, int flags );
+
 void esync_init(void)
 {
     struct stat st;
 
+    if (!do_esync())
+    {
+        /* make sure the server isn't running with WINEESYNC */
+        HANDLE handle;
+        NTSTATUS ret;
+
+        ret = create_esync( 0, &handle, 0, NULL, 0, 0 );
+        if (ret != STATUS_NOT_IMPLEMENTED)
+        {
+            ERR("Server is running with WINEESYNC but this process is not, please enable WINEESYNC or restart wineserver.\n");
+            exit(1);
+        }
+
+        return;
+    }
+
     if (stat( config_dir, &st ) == -1)
         ERR("Cannot stat %s\n", config_dir);
 
@@ -128,7 +147,14 @@ void esync_init(void)
         sprintf( shm_name, "/wine-%lx-esync", (unsigned long)st.st_ino );
 
     if ((shm_fd = shm_open( shm_name, O_RDWR, 0644 )) == -1)
-        ERR("Failed to initialize shared memory: %s\n", strerror( errno ));
+    {
+        /* probably the server isn't running with WINEESYNC, tell the user and bail */
+        if (errno == ENOENT)
+            ERR("Failed to open esync shared memory file; make sure no stale wineserver instances are running without WINEESYNC.\n");
+        else
+            ERR("Failed to initialize shared memory: %s\n", strerror( errno ));
+        exit(1);
+    }
 
     pagesize = sysconf( _SC_PAGESIZE );
 
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
index 752a0b3b622..36c5092a4b1 100644
--- a/dlls/ntdll/loader.c
+++ b/dlls/ntdll/loader.c
@@ -4648,8 +4648,7 @@ void __wine_process_init(void)
     peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL );
     peb->LoaderLock = &loader_section;
 
-    if (do_esync())
-        esync_init();
+    esync_init();
 
     init_unix_codepage();
     init_directories();
diff --git a/server/esync.c b/server/esync.c
index 11e5e482024..bc2ce61c9a2 100644
--- a/server/esync.c
+++ b/server/esync.c
@@ -394,6 +394,18 @@ DECL_HANDLER(create_esync)
     const struct security_descriptor *sd;
     const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
 
+    if (!do_esync())
+    {
+        set_error( STATUS_NOT_IMPLEMENTED );
+        return;
+    }
+
+    if (!req->type)
+    {
+        set_error( STATUS_INVALID_PARAMETER_4 );
+        return;
+    }
+
     if (!objattr) return;
 
     if ((esync = create_esync( root, &name, objattr->attributes, req->initval, req->flags, req->type, sd )))
-- 
2.26.2

