https://github.com/brltty/brltty/pull/349

commit 92ec2edbe50458fa63e261b5aabfb3357c281146
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sat Oct 9 14:48:32 2021 +0200

    Revert "Use fchmod (not chmod) to adjust the BrlAPI socket permissions. (dm)"
    
    This reverts commit 81cc72e023f6ca0fe6952d9b266ed937b38d28bc.
    
    fstat on a unix socket does not actually get the socket inode status, so
    that cannot be used for fixing the permissions.

diff --git a/Programs/brlapi_server.c b/Programs/brlapi_server.c
index 0eb346b7e..6fef00365 100644
--- a/Programs/brlapi_server.c
+++ b/Programs/brlapi_server.c
@@ -3036,27 +3036,32 @@ static int readPid(char *path)
 }
 
 static int
-adjustPermissions (
-  const void *object, const char *container,
-  int (*getStatus) (const void *object, struct stat *status),
-  int (*setPermissions) (const void *object, mode_t permissions)
-) {
-  uid_t user = geteuid();
-  int adjust = !user;
+adjustPermissions (const char *path) {
+  int adjust = !geteuid();
 
   if (!adjust) {
-    struct stat status;
+    char *directory = getPathDirectory(path);
 
-    if (stat(container, &status) == -1) {
-      logSystemError("stat");
-    } else if (status.st_uid == user) {
-      adjust = 1;
+    if (directory) {
+      struct stat status;
+
+      if (stat(directory, &status) == -1) {
+        logSystemError("stat");
+      } else if (status.st_uid == geteuid()) {
+        adjust = 1;
+      }
+
+      free(directory);
     }
   }
 
   if (adjust) {
     struct stat status;
-    if (!getStatus(object, &status)) return 0;
+
+    if (stat(path, &status) == -1) {
+      logSystemError("stat");
+      return 0;
+    }
 
     {
       mode_t oldPermissions = status.st_mode & ~S_IFMT;
@@ -3070,7 +3075,8 @@ adjustPermissions (
       #endif
 
       if (newPermissions != oldPermissions) {
-        if (!setPermissions(object, newPermissions)) {
+        if (chmod(path, newPermissions) == -1) {
+          logSystemError("chmod");
           return 0;
         }
       }
@@ -3079,60 +3085,6 @@ adjustPermissions (
 
   return 1;
 }
-
-static int
-getPathStatus (const void *object, struct stat *status) {
-  const char *path = object;
-  if (stat(path, status) != -1) return 1;
-  logSystemError("stat");
-  return 0;
-}
-
-static int
-setPathPermissions (const void *object, mode_t permissions) {
-  const char *path = object;
-  if (chmod(path, permissions) != -1) return 1;
-  logSystemError("chmod");
-  return 0;
-}
-
-static int
-adjustPathPermissions (const char *path) {
-  int ok = 0;
-  char *parent = getPathDirectory(path);
-
-  if (parent) {
-    if (adjustPermissions(path, parent, getPathStatus, setPathPermissions)) ok = 1;
-    free(parent);
-  }
-
-  return ok;
-}
-
-static int
-getFileStatus (const void *object, struct stat *status) {
-  const int *fd = object;
-  if (fstat(*fd, status) != -1) return 1;
-  logSystemError("fstat");
-  return 0;
-}
-
-static int
-setFilePermissions (const void *object, mode_t permissions) {
-  const int *fd = object;
-  if (fchmod(*fd, permissions) != -1) return 1;
-  logSystemError("fchmod");
-  return 0;
-}
-
-static int
-adjustFilePermissions (int fd, const char *directory) {
-  return adjustPermissions(
-    &fd, directory,
-    getFileStatus,
-    setFilePermissions
-  );
-}
 #endif /* __MINGW32__ */
 
 /* Function : createLocalSocket */
@@ -3218,7 +3170,7 @@ static FileDescriptor createLocalSocket(struct socketInfo *info)
     approximateDelay(1000);
   }
 
-  if (!adjustPathPermissions(BRLAPI_SOCKETPATH)) {
+  if (!adjustPermissions(BRLAPI_SOCKETPATH)) {
     goto outfd;
   }
 
@@ -3330,7 +3282,7 @@ static FileDescriptor createLocalSocket(struct socketInfo *info)
     goto outfd;
   }
 
-  if (!adjustFilePermissions(fd, BRLAPI_SOCKETPATH)) {
+  if (!adjustPermissions(sa.sun_path)) {
     goto outfd;
   }
 
