diff mbox

hurd: Fix F_*LK* fcntl with __USE_FILE_OFFSET64

Message ID 20150207220716.GW3023@type.youpi.perso.aquilenet.fr
State New
Headers show

Commit Message

Samuel Thibault Feb. 7, 2015, 10:07 p.m. UTC
struct flock64 uses 64bit values.  This introduces other values for
F_GETLK, F_SETLK, F_SETLKW to distinguish between both.

* sysdeps/mach/hurd/bits/fcntl.h (F_GETLK64, F_SETLK64, F_SETLKW64): New
macros
[__USE_FILE_OFFSET64] (F_GETLK, F_SETLK, F_SETLKW): Define to F_GETLK64,
F_SETLK64, F_SETLKW64, respectively.
* sysdeps/mach/hurd/f_setlk.c: New file.
* sysdeps/mach/hurd/f_setlk.h: New file.
* sysdeps/mach/hurd/fcntl.c (__libc_fcntl): Include "f_setlk.h",
make F_SETLK and F_SETLKW call __f_setlk instead of __flock.
Handle F_GETLK64, F_SETLK64, F_SETLKW64 cases.

---
 sysdeps/mach/hurd/bits/fcntl.h | 15 ++++++++--
 sysdeps/mach/hurd/f_setlk.c    | 68 ++++++++++++++++++++++++++++++++++++++++++
 sysdeps/mach/hurd/f_setlk.h    | 22 ++++++++++++++
 sysdeps/mach/hurd/fcntl.c      | 57 ++++++++++++++++-------------------
 4 files changed, 127 insertions(+), 35 deletions(-)

Comments

Roland McGrath Feb. 8, 2015, 2:20 a.m. UTC | #1
> +# define	F_GETLK		7	/* Get record locking info.  */
> +# define	F_SETLK		8	/* Set record locking info (non-blocking).  */
> +# define	F_SETLKW	9	/* Set record locking info (blocking).  */
> +#endif
> +#define	F_GETLK64	10	/* Get record locking info.  */
> +#define	F_SETLK64	11	/* Set record locking info (non-blocking).  */
> +#define	F_SETLKW64	12	/* Set record locking info (blocking).  */

Do these values match up with any other system's values?
i.e., if most of our values already match Linux or some BSD subset,
then for new arbitrary values, we pick the ones that match the same thing.

> +{
> +  int cmd;
> +
> +  switch (type)
> +    {
> +    case F_RDLCK: cmd |= LOCK_SH; break;
> +    case F_WRLCK: cmd |= LOCK_EX; break;

Uses CMD uninitialized.  The equivalent to the original code
in __fcntl is to initialize CMD to zero here.

I would have thought the compiler would catch that.  Have you tested this
patch on current trunk libc?  Are you using --disable-werror for some reason?
If you are, then let's work on resolving those outstanding warnings ASAP.


Thanks,
Roland
Samuel Thibault Feb. 8, 2015, 2:27 a.m. UTC | #2
Roland McGrath, le Sat 07 Feb 2015 18:20:31 -0800, a écrit :
> > +# define	F_GETLK		7	/* Get record locking info.  */
> > +# define	F_SETLK		8	/* Set record locking info (non-blocking).  */
> > +# define	F_SETLKW	9	/* Set record locking info (blocking).  */
> > +#endif
> > +#define	F_GETLK64	10	/* Get record locking info.  */
> > +#define	F_SETLK64	11	/* Set record locking info (non-blocking).  */
> > +#define	F_SETLKW64	12	/* Set record locking info (blocking).  */
> 
> Do these values match up with any other system's values?

No: BSD has only 64 variants, and Linux values don't match ours.

Samuel
Samuel Thibault Feb. 8, 2015, 4:24 a.m. UTC | #3
Roland McGrath, le Sat 07 Feb 2015 18:20:31 -0800, a écrit :
> Are you using --disable-werror for some reason?

Yes: we have a lot of warnings related with mig:

/usr/src/glibc/build/mach/RPC_default_pager_objects.c: In function '__default_pager_objects':
/usr/src/glibc/build/mach/RPC_default_pager_objects.c:188:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
      *objects = *((default_pager_object_t **)OutP->objects);
      ^
/usr/src/glibc/build/mach/RPC_default_pager_objects.c:215:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
      *ports = *((mach_port_t **)OutP->ports);
      ^

devstream.c:44:46: warning: passing argument 4 of 'device_write_inband' discards 'const' qualifier from pointer target type
    if (err = device_write_inband (dev, 0, 0, p, thiswrite, &wrote))

In file included from devstream.c:22:0:
/usr/src/glibc/build/mach/device/device.h:223:15: note: expected 'char *' but argument is of type 'const char *'
 kern_return_t device_write_inband
               ^

Samuel
diff mbox

Patch

diff --git a/sysdeps/mach/hurd/bits/fcntl.h b/sysdeps/mach/hurd/bits/fcntl.h
index 135aba3..9460be7 100644
--- a/sysdeps/mach/hurd/bits/fcntl.h
+++ b/sysdeps/mach/hurd/bits/fcntl.h
@@ -163,9 +163,18 @@ 
 # define F_GETOWN	5	/* Get owner (receiver of SIGIO).  */
 # define F_SETOWN	6	/* Set owner (receiver of SIGIO).  */
 #endif
-#define	F_GETLK		7	/* Get record locking info.  */
-#define	F_SETLK		8	/* Set record locking info (non-blocking).  */
-#define	F_SETLKW	9	/* Set record locking info (blocking).  */
+#ifdef __USE_FILE_OFFSET64
+# define	F_GETLK		F_GETLK64
+# define	F_SETLK		F_SETLK64
+# define	F_SETLKW	F_SETLKW64
+#else
+# define	F_GETLK		7	/* Get record locking info.  */
+# define	F_SETLK		8	/* Set record locking info (non-blocking).  */
+# define	F_SETLKW	9	/* Set record locking info (blocking).  */
+#endif
+#define	F_GETLK64	10	/* Get record locking info.  */
+#define	F_SETLK64	11	/* Set record locking info (non-blocking).  */
+#define	F_SETLKW64	12	/* Set record locking info (blocking).  */
 
 #ifdef __USE_XOPEN2K8
 # define F_DUPFD_CLOEXEC 1030	/* Duplicate, set FD_CLOEXEC on new one.  */
diff --git a/sysdeps/mach/hurd/f_setlk.c b/sysdeps/mach/hurd/f_setlk.c
new file mode 100644
index 0000000..9c898b1
--- /dev/null
+++ b/sysdeps/mach/hurd/f_setlk.c
@@ -0,0 +1,68 @@ 
+/* Copyright (C) 2014-2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <fcntl.h>
+#include <errno.h>
+
+/* XXX
+   We need new RPCs to support POSIX.1 fcntl file locking!!
+   For the time being we support the whole-file case only,
+   with all kinds of WRONG WRONG WRONG semantics,
+   by using flock.  This is definitely the Wrong Thing,
+   but it might be better than nothing (?).  */
+int
+__f_setlk (int fd, int type, int whence, __off64_t start, __off64_t len, int wait)
+{
+  int cmd;
+
+  switch (type)
+    {
+    case F_RDLCK: cmd |= LOCK_SH; break;
+    case F_WRLCK: cmd |= LOCK_EX; break;
+    case F_UNLCK: cmd = LOCK_UN; break;
+    default:
+      errno = EINVAL;
+      return -1;
+    }
+
+  if (wait == 0)
+    cmd |= LOCK_NB;
+
+  switch (whence)
+    {
+    case SEEK_SET:
+      if (start == 0 && len == 0) /* Whole file request.  */
+	break;
+      /* It seems to be common for applications to lock the first
+	 byte of the file when they are really doing whole-file locking.
+	 So, since it's so wrong already, might as well do that too.  */
+      if (start == 0 && len == 1)
+	break;
+      /* FALLTHROUGH */
+    case SEEK_CUR:
+    case SEEK_END:
+      errno = ENOTSUP;
+      return -1;
+    default:
+      errno = EINVAL;
+      return -1;
+    }
+
+  return __flock (fd, cmd);
+}
diff --git a/sysdeps/mach/hurd/f_setlk.h b/sysdeps/mach/hurd/f_setlk.h
new file mode 100644
index 0000000..a9d09fd
--- /dev/null
+++ b/sysdeps/mach/hurd/f_setlk.h
@@ -0,0 +1,22 @@ 
+/* Copyright (C) 2014-2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _F_SETLK_H
+#define _F_SETLK_H 1
+
+extern int __f_setlk (int fd, int type, int whence, __off64_t start, __off64_t len, int wait);
+#endif /* f_setlk.h */
diff --git a/sysdeps/mach/hurd/fcntl.c b/sysdeps/mach/hurd/fcntl.c
index 8f4bf3f..a7ef21b 100644
--- a/sysdeps/mach/hurd/fcntl.c
+++ b/sysdeps/mach/hurd/fcntl.c
@@ -21,6 +21,7 @@ 
 #include <hurd/fd.h>
 #include <stdarg.h>
 #include <sys/file.h>		/* XXX for LOCK_* */
+#include "f_setlk.h"
 
 /* Perform file control operations on FD.  */
 int
@@ -128,56 +129,48 @@  __libc_fcntl (int fd, int cmd, ...)
     case F_SETLK:
     case F_SETLKW:
       {
-	/* XXX
-	   We need new RPCs to support POSIX.1 fcntl file locking!!
-	   For the time being we support the whole-file case only,
-	   with all kinds of WRONG WRONG WRONG semantics,
-	   by using flock.  This is definitely the Wrong Thing,
-	   but it might be better than nothing (?).  */
 	struct flock *fl = va_arg (ap, struct flock *);
+	int wait = 0;
 	va_end (ap);
 	switch (cmd)
 	  {
 	  case F_GETLK:
 	    errno = ENOSYS;
 	    return -1;
+	  case F_SETLKW:
+	    wait = 1;
+	    /* FALLTHROUGH */
 	  case F_SETLK:
-	    cmd = LOCK_NB;
-	    break;
-	  default:
-	    cmd = 0;
-	    break;
-	  }
-	switch (fl->l_type)
-	  {
-	  case F_RDLCK: cmd |= LOCK_SH; break;
-	  case F_WRLCK: cmd |= LOCK_EX; break;
-	  case F_UNLCK: cmd |= LOCK_UN; break;
+	    return __f_setlk (fd, fl->l_type, fl->l_whence,
+			      fl->l_start, fl->l_end, wait);
 	  default:
 	    errno = EINVAL;
 	    return -1;
 	  }
-	switch (fl->l_whence)
+      }
+
+    case F_GETLK64:
+    case F_SETLK64:
+    case F_SETLKW64:
+      {
+	struct flock64 *fl64 = va_arg (ap, struct flock64 *);
+	int wait = 0;
+	va_end (ap);
+	switch (cmd)
 	  {
-	  case SEEK_SET:
-	    if (fl->l_start == 0 && fl->l_len == 0) /* Whole file request.  */
-	      break;
-	    /* It seems to be common for applications to lock the first
-	       byte of the file when they are really doing whole-file locking.
-	       So, since it's so wrong already, might as well do that too.  */
-	    if (fl->l_start == 0 && fl->l_len == 1)
-	      break;
-	    /* FALLTHROUGH */
-	  case SEEK_CUR:
-	  case SEEK_END:
-	    errno = ENOTSUP;
+	  case F_GETLK64:
+	    errno = ENOSYS;
 	    return -1;
+	  case F_SETLK64:
+	    wait = 1;
+	    /* FALLTHROUGH */
+	  case F_SETLKW64:
+	    return __f_setlk (fd, fl->l_type, fl->l_whence,
+			      fl->l_start, fl->l_end, wait);
 	  default:
 	    errno = EINVAL;
 	    return -1;
 	  }
-
-	return __flock (fd, cmd);
       }
 
     case F_GETFL:		/* Get per-open flags.  */