8 #include <sys/socket.h>
11 #define FNAME "locktest.dat"
13 #ifdef GLIBC_HACK_FCNTL64
14 /* this is a gross hack. 64 bit locking is completely screwed up on
15 i386 Linux in glibc 2.1.95 (which ships with RedHat 7.0). This hack
16 "fixes" the problem with the current 2.4.0test kernels
25 #include <asm/unistd.h>
26 int fcntl64(int fd, int cmd, struct flock * lock)
28 return syscall(__NR_fcntl64, fd, cmd, lock);
30 #endif /* HACK_FCNTL64 */
33 static pid_t child_pid;
35 typedef unsigned long long ull;
37 static int brlock(int fd, off_t offset, size_t len, int set, int rw_type, int lck_type)
41 fl.l_type = set?rw_type:F_UNLCK;
42 fl.l_whence = SEEK_SET;
47 if (fcntl(fd, lck_type, &fl) != 0) {
53 static void child_main(int pfd)
62 while ((fd = open(FNAME,O_RDWR)) == -1) sleep(1);
65 read(pfd, &ofs, sizeof(ofs));
66 read(pfd, &len, sizeof(len));
68 /* check that we can't lock that range */
69 if (brlock(fd, ofs, len, 1, F_WRLCK, F_SETLK) == 0) {
70 printf("ERROR: able to lock range %lld:%lld\n", (ull)ofs, (ull)len);
74 /* but we should be able to lock just before and after it */
75 if (brlock(fd, ofs+len, 1, 1, F_WRLCK, F_SETLK) == -1) {
76 printf("ERROR: unable to lock range %lld:%lld\n", (ull)ofs+len, (ull)1);
79 if (brlock(fd, ofs-1, 1, 1, F_WRLCK, F_SETLK) == -1) {
80 printf("ERROR: unable to lock range %lld:%lld\n", (ull)ofs-1, (ull)1);
84 /* and remove them again */
85 if (brlock(fd, ofs+len, 1, 0, F_WRLCK, F_SETLK) == -1) {
86 printf("ERROR: unable to unlock range %lld:%lld\n", (ull)ofs+len, (ull)1);
89 if (brlock(fd, ofs-1, 1, 0, F_WRLCK, F_SETLK) == -1) {
90 printf("ERROR: unable to unlock range %lld:%lld\n", (ull)ofs-1, (ull)1);
96 write(pfd, &ret, sizeof(ret));
102 snprintf(line, sizeof(line), "egrep POSIX.*%u /proc/locks", (int)st.st_ino);
106 write(pfd, &ret, sizeof(ret));
111 static void parent_main(int pfd)
113 int fd = open(FNAME,O_RDWR|O_CREAT|O_TRUNC, 0600);
123 #if (_FILE_OFFSET_BITS == 64)
136 for (i=0;i<sizeof(tests)/sizeof(tests[0]); i++) {
140 if (brlock(fd, ofs, len, 1, F_WRLCK, F_SETLK) == -1) {
141 printf("lock 1 failed\n");
145 write(pfd, &ofs, sizeof(ofs));
146 write(pfd, &len, sizeof(len));
148 read(pfd, &ret, sizeof(ret));
150 printf("child reported failure\n");
154 printf("test %d OK\n", i);
156 printf("all tests OK\n");
157 kill(child_pid, SIGKILL);
158 waitpid(child_pid, NULL, 0);
162 kill(child_pid, SIGKILL);
163 waitpid(child_pid, NULL, 0);
168 int main(int argc, char *argv[])
173 socketpair(AF_UNIX, SOCK_STREAM, 0, pfd);
175 printf("sizeof(off_t) : %d\n", sizeof(off_t));
176 printf("sizeof(size_t) : %d\n", sizeof(size_t));
177 printf("sizeof(l_start) : %d\n", sizeof(fl.l_start));
179 if ((child_pid=fork()) == 0) {