Blame sysdeps/unix/sysv/linux/fcntl.c

Packit 6c4009
/* Linux fcntl syscall implementation.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sysdep-cancel.h>
Packit 6c4009
Packit 6c4009
#ifndef __OFF_T_MATCHES_OFF64_T
Packit 6c4009
Packit 6c4009
# ifndef FCNTL_ADJUST_CMD
Packit 6c4009
#  define FCNTL_ADJUST_CMD(__cmd) __cmd
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__libc_fcntl (int fd, int cmd, ...)
Packit 6c4009
{
Packit 6c4009
  va_list ap;
Packit 6c4009
  void *arg;
Packit 6c4009
Packit 6c4009
  va_start (ap, cmd);
Packit 6c4009
  arg = va_arg (ap, void *);
Packit 6c4009
  va_end (ap);
Packit 6c4009
Packit 6c4009
  cmd = FCNTL_ADJUST_CMD (cmd);
Packit 6c4009
Packit 6c4009
  switch (cmd)
Packit 6c4009
    {
Packit 6c4009
      case F_SETLKW:
Packit 6c4009
      case F_SETLKW64:
Packit 6c4009
	return SYSCALL_CANCEL (fcntl64, fd, cmd, arg);
Packit 6c4009
      case F_OFD_SETLKW:
Packit 6c4009
	{
Packit 6c4009
	  struct flock *flk = (struct flock *) arg;
Packit 6c4009
	  struct flock64 flk64 =
Packit 6c4009
	  {
Packit 6c4009
	    .l_type = flk->l_type,
Packit 6c4009
	    .l_whence = flk->l_whence,
Packit 6c4009
	    .l_start = flk->l_start,
Packit 6c4009
	    .l_len = flk->l_len,
Packit 6c4009
	    .l_pid = flk->l_pid
Packit 6c4009
	  };
Packit 6c4009
	  return SYSCALL_CANCEL (fcntl64, fd, cmd, &flk64);
Packit 6c4009
	}
Packit 6c4009
      case F_OFD_GETLK:
Packit 6c4009
      case F_OFD_SETLK:
Packit 6c4009
	{
Packit 6c4009
	  struct flock *flk = (struct flock *) arg;
Packit 6c4009
	  struct flock64 flk64 =
Packit 6c4009
	  {
Packit 6c4009
	    .l_type = flk->l_type,
Packit 6c4009
	    .l_whence = flk->l_whence,
Packit 6c4009
	    .l_start = flk->l_start,
Packit 6c4009
	    .l_len = flk->l_len,
Packit 6c4009
	    .l_pid = flk->l_pid
Packit 6c4009
	  };
Packit 6c4009
	  int ret = INLINE_SYSCALL_CALL (fcntl64, fd, cmd, &flk64);
Packit 6c4009
	  if (ret == -1)
Packit 6c4009
	    return -1;
Packit 6c4009
	  if ((off_t) flk64.l_start != flk64.l_start
Packit 6c4009
	      || (off_t) flk64.l_len != flk64.l_len)
Packit 6c4009
	    {
Packit 6c4009
	      __set_errno (EOVERFLOW);
Packit 6c4009
	      return -1;
Packit 6c4009
	    }
Packit 6c4009
	  flk->l_type = flk64.l_type;
Packit 6c4009
	  flk->l_whence = flk64.l_whence;
Packit 6c4009
	  flk->l_start = flk64.l_start;
Packit 6c4009
	  flk->l_len = flk64.l_len;
Packit 6c4009
	  flk->l_pid = flk64.l_pid;
Packit 6c4009
	  return ret;
Packit 6c4009
	}
Packit 6c4009
      /* Since only F_SETLKW{64}/F_OLD_SETLK are cancellation entrypoints and
Packit 6c4009
	 only OFD locks require LFS handling, all others flags are handled
Packit 6c4009
	 unmodified by calling __NR_fcntl64.  */
Packit 6c4009
      default:
Packit 6c4009
        return __fcntl64_nocancel_adjusted (fd, cmd, arg);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__libc_fcntl)
Packit 6c4009
Packit 6c4009
weak_alias (__libc_fcntl, __fcntl)
Packit 6c4009
libc_hidden_weak (__fcntl)
Packit 6c4009
Packit 6c4009
# include <shlib-compat.h>
Packit 6c4009
# if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_28)
Packit 6c4009
int
Packit 6c4009
__old_libc_fcntl64 (int fd, int cmd, ...)
Packit 6c4009
{
Packit 6c4009
  va_list ap;
Packit 6c4009
  void *arg;
Packit 6c4009
Packit 6c4009
  va_start (ap, cmd);
Packit 6c4009
  arg = va_arg (ap, void *);
Packit 6c4009
  va_end (ap);
Packit 6c4009
Packit 6c4009
  /* Previous versions called __NR_fcntl64 for fcntl (which did not handle
Packit 6c4009
     OFD locks in LFS mode).  */
Packit 6c4009
  return __libc_fcntl64 (fd, cmd, arg);
Packit 6c4009
}
Packit 6c4009
compat_symbol (libc, __old_libc_fcntl64, fcntl, GLIBC_2_0);
Packit 6c4009
versioned_symbol (libc, __libc_fcntl, fcntl, GLIBC_2_28);
Packit 6c4009
# else
Packit 6c4009
weak_alias (__libc_fcntl, fcntl)
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
#endif /* __OFF_T_MATCHES_OFF64_T  */