]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/misc/file/lockf64.c
Inital import
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / misc / file / lockf64.c
1 /* Copyright (C) 1994, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <_lfs_64.h>
20
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/syscall.h>
27
28 #ifdef __NR_fcntl64
29 #define flock flock64
30 #define fcntl fcntl64
31 #undef F_GETLK
32 #define F_GETLK F_GETLK64
33 #undef F_SETLK
34 #define F_SETLK F_SETLK64
35 #else
36 #endif
37
38
39 /* lockf is a simplified interface to fcntl's locking facilities.  */
40
41 int lockf64 (int fd, int cmd, off64_t len64)
42 {
43     struct flock fl;
44     off_t len = (off_t) len64;
45
46     if (len64 != (off64_t) len)
47     {
48         /* We can't represent the length.  */
49         __set_errno(EOVERFLOW);
50         return -1;
51     }
52
53     memset((char *) &fl, '\0', sizeof (fl));
54
55     /* lockf is always relative to the current file position.  */
56     fl.l_whence = SEEK_CUR;
57     fl.l_start = 0;
58     fl.l_len = len;
59
60     switch (cmd)
61     {
62         case F_TEST:
63             /* Test the lock: return 0 if FD is unlocked or locked by this process;
64                return -1, set errno to EACCES, if another process holds the lock.  */
65             fl.l_type = F_RDLCK;
66             if (fcntl (fd, F_GETLK, &fl) < 0)
67                 return -1;
68             if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
69                 return 0;
70             __set_errno(EACCES);
71             return -1;
72
73         case F_ULOCK:
74             fl.l_type = F_UNLCK;
75             cmd = F_SETLK;
76             break;
77         case F_LOCK:
78             fl.l_type = F_WRLCK;
79             cmd = F_SETLKW;
80             break;
81         case F_TLOCK:
82             fl.l_type = F_WRLCK;
83             cmd = F_SETLK;
84             break;
85
86         default:
87             __set_errno(EINVAL);
88             return -1;
89     }
90
91     return fcntl(fd, cmd, &fl);
92 }
93 libc_hidden_def(lockf64)