Back to index

glibc  2.9
lockf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <sys/types.h>
00020 #include <unistd.h>
00021 #include <fcntl.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 
00025 /* lockf is a simplified interface to fcntl's locking facilities.  */
00026 
00027 int
00028 lockf (int fd, int cmd, off_t len)
00029 {
00030   struct flock fl;
00031 
00032   memset ((char *) &fl, '\0', sizeof (fl));
00033 
00034   /* lockf is always relative to the current file position.  */
00035   fl.l_whence = SEEK_CUR;
00036   fl.l_start = 0;
00037   fl.l_len = len;
00038 
00039   switch (cmd)
00040     {
00041     case F_TEST:
00042       /* Test the lock: return 0 if FD is unlocked or locked by this process;
00043         return -1, set errno to EACCES, if another process holds the lock.  */
00044       fl.l_type = F_RDLCK;
00045       if (__fcntl (fd, F_GETLK, &fl) < 0)
00046        return -1;
00047       if (fl.l_type == F_UNLCK || fl.l_pid == __getpid ())
00048        return 0;
00049       __set_errno (EACCES);
00050       return -1;
00051 
00052     case F_ULOCK:
00053       fl.l_type = F_UNLCK;
00054       cmd = F_SETLK;
00055       break;
00056     case F_LOCK:
00057       fl.l_type = F_WRLCK;
00058       cmd = F_SETLKW;
00059       break;
00060     case F_TLOCK:
00061       fl.l_type = F_WRLCK;
00062       cmd = F_SETLK;
00063       break;
00064 
00065     default:
00066       __set_errno (EINVAL);
00067       return -1;
00068     }
00069 
00070   /* lockf() is a cancellation point but so is fcntl() if F_SETLKW is
00071      used.  Therefore we don't have to care about cancellation here,
00072      the fcntl() function will take care of it.  */
00073   return __fcntl (fd, cmd, &fl);
00074 }