Blame io/lockf.c

Packit Service 82fcde
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* We need to avoid the header declaration of lockf64, because
Packit Service 82fcde
   the types don't match lockf and then the compiler will
Packit Service 82fcde
   complain about the mismatch when we do the alias below.  */
Packit Service 82fcde
#define lockf64	__renamed_lockf64
Packit Service 82fcde
Packit Service 82fcde
#include <fcntl.h>
Packit Service 82fcde
Packit Service 82fcde
#undef	lockf64
Packit Service 82fcde
Packit Service 82fcde
#include <sys/types.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
Packit Service 82fcde
/* lockf is a simplified interface to fcntl's locking facilities.  */
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
lockf (int fd, int cmd, off_t len)
Packit Service 82fcde
{
Packit Service 82fcde
  struct flock fl;
Packit Service 82fcde
Packit Service 82fcde
  memset ((char *) &fl, '\0', sizeof (fl));
Packit Service 82fcde
Packit Service 82fcde
  /* lockf is always relative to the current file position.  */
Packit Service 82fcde
  fl.l_whence = SEEK_CUR;
Packit Service 82fcde
  fl.l_start = 0;
Packit Service 82fcde
  fl.l_len = len;
Packit Service 82fcde
Packit Service 82fcde
  switch (cmd)
Packit Service 82fcde
    {
Packit Service 82fcde
    case F_TEST:
Packit Service 82fcde
      /* Test the lock: return 0 if FD is unlocked or locked by this process;
Packit Service 82fcde
	 return -1, set errno to EACCES, if another process holds the lock.  */
Packit Service 82fcde
      fl.l_type = F_RDLCK;
Packit Service 82fcde
      if (__fcntl (fd, F_GETLK, &fl) < 0)
Packit Service 82fcde
	return -1;
Packit Service 82fcde
      if (fl.l_type == F_UNLCK || fl.l_pid == __getpid ())
Packit Service 82fcde
	return 0;
Packit Service 82fcde
      __set_errno (EACCES);
Packit Service 82fcde
      return -1;
Packit Service 82fcde
Packit Service 82fcde
    case F_ULOCK:
Packit Service 82fcde
      fl.l_type = F_UNLCK;
Packit Service 82fcde
      cmd = F_SETLK;
Packit Service 82fcde
      break;
Packit Service 82fcde
    case F_LOCK:
Packit Service 82fcde
      fl.l_type = F_WRLCK;
Packit Service 82fcde
      cmd = F_SETLKW;
Packit Service 82fcde
      break;
Packit Service 82fcde
    case F_TLOCK:
Packit Service 82fcde
      fl.l_type = F_WRLCK;
Packit Service 82fcde
      cmd = F_SETLK;
Packit Service 82fcde
      break;
Packit Service 82fcde
Packit Service 82fcde
    default:
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return -1;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* lockf() is a cancellation point but so is fcntl() if F_SETLKW is
Packit Service 82fcde
     used.  Therefore we don't have to care about cancellation here,
Packit Service 82fcde
     the fcntl() function will take care of it.  */
Packit Service 82fcde
  return __fcntl (fd, cmd, &fl);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#ifdef __OFF_T_MATCHES_OFF64_T
Packit Service 82fcde
weak_alias (lockf, lockf64)
Packit Service 82fcde
#endif