Cygwin: file locking: always use and expect explicit lock variant
authorCorinna Vinschen <corinna@vinschen.de>
Mon, 4 Aug 2025 18:45:38 +0000 (20:45 +0200)
committerCorinna Vinschen <corinna@vinschen.de>
Mon, 4 Aug 2025 18:51:03 +0000 (20:51 +0200)
So far, not setting a lock variant (F_POSIX/F_FLOCK) defaulted to
F_POSIX locks.  Adding OFD locks in a followup patch may lead to
confusion, so make sure that the lock variant is always set in the
calling functions (fcntl, flock, lockf).  Bail out with EINVAL if
no lock variant is set.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
winsup/cygwin/fhandler/base.cc
winsup/cygwin/flock.cc

index beebd710c5d9c8b8253bf34bfffb7f69e6cfebd7..09b0a35e614ffdecae607aeea2bf25fecd0336d4 100644 (file)
@@ -1499,6 +1499,7 @@ int fhandler_base::fcntl (int cmd, intptr_t arg)
        {
          struct flock *fl = (struct flock *) arg;
          fl->l_type &= F_RDLCK | F_WRLCK | F_UNLCK;
+         fl->l_type |= F_POSIX;
          res = mandatory_locking () ? mand_lock (cmd, fl) : lock (cmd, fl);
        }
       break;
index b41cba5c70366b923f3286df3435045c2dcffa23..0eb908fd32a6884e547c1e890dff0fb4c18ddef9 100644 (file)
@@ -951,7 +951,10 @@ fhandler_base::lock (int a_op, struct flock *fl)
   short type = fl->l_type & (F_RDLCK | F_WRLCK | F_UNLCK);
 
   if (!a_flags)
-    a_flags = F_POSIX; /* default */
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
 
   /* FIXME: For BSD flock(2) we need a valid, per file table entry OS handle.
      Therefore we can't allow using flock(2) on nohandle devices. */
@@ -1859,8 +1862,7 @@ flock (int fd, int operation)
          set_errno (EINVAL);
          __leave;
        }
-      if (!cfd->mandatory_locking ())
-       fl.l_type |= F_FLOCK;
+      fl.l_type |= F_FLOCK;
       res = cfd->mandatory_locking () ? cfd->mand_lock (cmd, &fl)
                                      : cfd->lock (cmd, &fl);
       if ((res == -1) && ((get_errno () == EAGAIN) || (get_errno () == EACCES)))
@@ -1906,7 +1908,7 @@ lockf (int filedes, int function, off_t size)
          fl.l_type = F_WRLCK;
          break;
        case F_TEST:
-         fl.l_type = F_WRLCK;
+         fl.l_type = F_POSIX | F_WRLCK;
          if (cfd->lock (F_GETLK, &fl) == -1)
            __leave;
          if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
@@ -1920,6 +1922,7 @@ lockf (int filedes, int function, off_t size)
          __leave;
          /* NOTREACHED */
        }
+      fl.l_type |= F_POSIX;
       res = cfd->mandatory_locking () ? cfd->mand_lock (cmd, &fl)
                                      : cfd->lock (cmd, &fl);
     }
@@ -2022,6 +2025,8 @@ fhandler_disk_file::mand_lock (int a_op, struct flock *fl)
      the entire file, even when file grows later. */
   if (length.QuadPart == 0)
     length.QuadPart = UINT64_MAX;
+  /* Filter lock types */
+  fl->l_type &= (F_RDLCK | F_WRLCK | F_UNLCK);
   /* Action! */
   if (fl->l_type == F_UNLCK)
     {
This page took 0.038195 seconds and 5 git commands to generate.