stdlib: Improve fortify with clang
[glibc.git] / io / tst-fcntl-lock.c
blob3c32ca32253401e3788114c2f885527032d30b46
1 /* Test for advisory record locking.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>.
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <unistd.h>
22 /* This is essentially the POSIX lockf. */
24 static int
25 fcntl_lockf (int fd, int cmd, off_t len)
27 struct flock fl = {
28 .l_type = F_WRLCK,
29 .l_whence = SEEK_CUR,
30 .l_len = len
33 switch (cmd)
35 case F_TEST:
36 fl.l_type = F_RDLCK;
37 if (fcntl (fd, F_GETLK, &fl) < 0)
38 return -1;
39 if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
40 return 0;
41 errno = EACCES;
42 return -1;
44 case F_ULOCK:
45 fl.l_type = F_UNLCK;
46 return fcntl (fd, F_SETLK, &fl);
48 case F_LOCK:
49 return fcntl (fd, F_SETLKW, &fl);
51 case F_TLOCK:
52 return fcntl (fd, F_SETLK, &fl);
55 errno = EINVAL;
56 return -1;
59 static int
60 fcntl64_lockf (int fd, int cmd, off64_t len64)
62 struct flock64 fl64 = {
63 .l_type = F_WRLCK,
64 .l_whence = SEEK_CUR,
65 .l_len = len64
68 switch (cmd)
70 case F_TEST:
71 fl64.l_type = F_RDLCK;
72 if (fcntl64 (fd, F_GETLK64, &fl64) < 0)
73 return -1;
74 if (fl64.l_type == F_UNLCK || fl64.l_pid == getpid ())
75 return 0;
76 errno = EACCES;
77 return -1;
79 case F_ULOCK:
80 fl64.l_type = F_UNLCK;
81 return fcntl64 (fd, F_SETLK64, &fl64);
83 case F_LOCK:
84 return fcntl64 (fd, F_SETLKW64, &fl64);
86 case F_TLOCK:
87 return fcntl64 (fd, F_SETLK64, &fl64);
90 errno = EINVAL;
91 return -1;
94 #define TST_LOCKFD "tst-fcntl-lock."
95 #define LOCKF fcntl_lockf
96 #define LOCKF64 fcntl64_lockf
97 #include "tst-lockf.c"