commit a31ec12308374625527c2426cd65fe18ef6d8aa5
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:44:46 2021 +0100

    brlapi_server: make api_claimDriver not unlock apiSuspendMutex
    
    It is api_releaseDriver which is supposed to be doing it.

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index 95bec5308..c2127c657 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -4279,7 +4279,6 @@ int api_claimDriver (BrailleDisplay *brl)
   int ret;
   lockMutex(&apiSuspendMutex);
   ret = driverConstructed;
-  unlockMutex(&apiSuspendMutex);
   return ret;
 }
 
commit b7c5e3b67c31e65d24b46dc51ab0c3b4adca42ab
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:39:26 2021 +0100

    brlapi_client: Fix uninitialized value
    
    In case select would return without an actual fd set.
    That's not supposed to happen, but better be safe.

diff --git a/Programs/brlapi_client.c b/Programs/brlapi_client.c
index 5876b8ecc..18be4fb31 100644
--- a/Programs/brlapi_client.c
+++ b/Programs/brlapi_client.c
@@ -377,7 +377,7 @@ static ssize_t brlapi__doWaitForPacket(brlapi_handle_t *handle, brlapi_packetTyp
 
   uint32_t size;
   brlapi_packetType_t type;
-  int ret;
+  int ret = 0;
 
   struct timeval now;
   int delay = 0;
commit 43040e4afac6afa12a5b6a8c5dff837ccef9af10
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:41:19 2021 +0100

    brlapi_server: Do not look at parameters when freeing a tty
    
    Ttys do not have parameter subscriptions, and locking apiParamMutex
    would bring an unwanted apiConnectionsMutex -> apiParamMutex lock
    ordering.

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index c2127c657..84c54bcff 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -683,18 +683,18 @@ static void freeConnection(Connection *c)
 {
   struct Subscription *s, *next;
 
-  lockMutex(&apiParamMutex);
-  for (s=c->subscriptions.next; s!=&c->subscriptions; s=next) {
-    if (s->flags & BRLAPI_PARAMF_GLOBAL)
-      paramState[s->parameter].global_subscriptions--;
-    else
-      paramState[s->parameter].local_subscriptions--;
-    next = s->next;
-    free(s);
-  }
-  unlockMutex(&apiParamMutex);
-
   if (c->fd != INVALID_FILE_DESCRIPTOR) {
+    lockMutex(&apiParamMutex);
+    for (s=c->subscriptions.next; s!=&c->subscriptions; s=next) {
+      if (s->flags & BRLAPI_PARAMF_GLOBAL)
+	paramState[s->parameter].global_subscriptions--;
+      else
+	paramState[s->parameter].local_subscriptions--;
+      next = s->next;
+      free(s);
+    }
+    unlockMutex(&apiParamMutex);
+
     if (c->auth != 1) unauthConnections--;
     closeFileDescriptor(c->fd);
   }
commit 8bf30b906f531784b91e4427a0dc1bb4e3ad2e84
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:43:25 2021 +0100

    brlapi_server: Do not set disp inside brlResize but in the caller
    
    This allows to call brlResize with apiDriverMutex not held, thus
    avoiding the apiDriverMutex -> apiParamMutex lock ordering.

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index 84c54bcff..2c55dbdbe 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -395,15 +395,18 @@ static int resumeBrailleDriver(BrailleDisplay *brl) {
   driverConstructing = 1;
   lockMutex(&apiSuspendMutex);
   driverConstructed = constructBrailleDriver();
+  if (driverConstructed) {
+    disp = brl;
+  }
+  unlockMutex(&apiSuspendMutex);
   if (driverConstructed) {
     logMessage(LOG_CATEGORY(SERVER_EVENTS), "driver resumed");
     handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DRIVER_NAME, 0, BRLAPI_PARAMF_GLOBAL, braille->definition.name, strlen(braille->definition.name));
     handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DRIVER_CODE, 0, BRLAPI_PARAMF_GLOBAL, braille->definition.code, strlen(braille->definition.code));
     handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DRIVER_VERSION, 0, BRLAPI_PARAMF_GLOBAL, braille->definition.version, strlen(braille->definition.version));
-    if (disp) handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DEVICE_MODEL, 0, BRLAPI_PARAMF_GLOBAL, disp->keyBindings, strlen(disp->keyBindings));
+    handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DEVICE_MODEL, 0, BRLAPI_PARAMF_GLOBAL, disp->keyBindings, strlen(disp->keyBindings));
     brlResize(brl);
   }
-  unlockMutex(&apiSuspendMutex);
   driverConstructing = 0;
   return driverConstructed;
 }
@@ -4301,7 +4304,6 @@ static void brlResize(BrailleDisplay *brl)
   coreWindowText = realloc(coreWindowText, displaySize * sizeof(*coreWindowText));
   coreWindowDots = realloc(coreWindowDots, displaySize * sizeof(*coreWindowDots));
   coreWindowCursor = 0;
-  disp = brl;
   handleParamUpdate(NULL, NULL, BRLAPI_PARAM_DISPLAY_SIZE, 0, BRLAPI_PARAMF_GLOBAL, displayDimensions, sizeof(displayDimensions));
 }
 
@@ -4329,9 +4331,10 @@ void api_linkServer(BrailleDisplay *brl)
   ApiBraille.writePacket = NULL;
   braille=&ApiBraille;
   lockMutex(&apiDriverMutex);
-  brlResize(brl);
+  disp = brl;
   driverConstructed=1;
   unlockMutex(&apiDriverMutex);
+  brlResize(brl);
   lockMutex(&apiConnectionsMutex);
   broadcastKey(&ttys, BRLAPI_KEY_TYPE_CMD|BRLAPI_KEY_CMD_NOOP, BRL_COMMANDS);
   unlockMutex(&apiConnectionsMutex);
commit 11c87e4e1dc073c8d13ccdce54aefbd0bcfd7f33
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:45:51 2021 +0100

    brlapi_server: Make api_flushOutput lock apiParamMutex
    
    This allows to respect the apiParamMutex -> apiConnectionsMutex lock
    oredering.

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index 2c55dbdbe..0bfd6467d 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -4188,6 +4188,7 @@ int api_flushOutput(BrailleDisplay *brl) {
   int drain = 0;
   int update = 0;
 
+  lockMutex(&apiParamMutex);
   lockMutex(&apiConnectionsMutex);
   lockMutex(&apiRawMutex);
   if (suspendConnection) {
@@ -4261,6 +4262,7 @@ int api_flushOutput(BrailleDisplay *brl) {
   unlockMutex(&apiRawMutex);
 out:
   unlockMutex(&apiConnectionsMutex);
+  unlockMutex(&apiParamMutex);
   return ok;
 }
 
commit fdab5afbcf2ea0566c43e3fe7bdb91699a31a377
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sun Jan 3 01:46:40 2021 +0100

    brlapi_server: add apiParamMutex in lock ordering documentation

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index 0bfd6467d..6c77fd475 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -260,10 +260,11 @@ pthread_mutex_t apiParamMutex;
 static Connection *paramUpdateConnection;
 
 /* mutex lock order is as follows:
- * 1. apiConnectionsMutex
- * 2. apiRawMutex
- * 3. acceptedKeysMutex or brailleWindowMutex
- * 4. apiDriverMutex
+ * 1. apiParamMutex
+ * 2. apiConnectionsMutex
+ * 3. apiRawMutex
+ * 4. acceptedKeysMutex or brailleWindowMutex
+ * 5. apiDriverMutex
 */
 
 static Tty notty;
