2 /* Test program to demonstrate valgrind breaking fcntl locks during
3 * mmap. Feed it a r/w file, such as its own source code. */
12 #include <sys/types.h>
16 int main(int argc
, char *argv
[])
19 const char *file
= /* argv[1]; */
25 errx(1, "Usage: %s <normal-file>", argv
[0]);
27 fd
= open(file
, O_RDWR
);
29 err(1, "Opening %s", file
);
31 // reproduce bug 297991: mmap interferes with fd position
32 initial
= lseek(fd
, 123, SEEK_SET
);
34 err(1, "initial off_t differs from 123 (TEST FAILED)");
35 if (lseek(fd
, 0, SEEK_CUR
) != 123)
36 err(1, "zero offset from initial differs from 123 (TEST FAILED)");
39 fl
.l_whence
= SEEK_SET
;
43 /* I'm assuming no one else tries to lock this! */
44 if (fcntl(fd
, F_SETLK
, &fl
) != 0)
45 err(1, "Locking %s", file
);
47 /* If under valgrind, mmap re-opens and closes file, screwing us */
48 if (mmap(NULL
, getpagesize(), PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0) == MAP_FAILED
)
49 err(1, "mmap of %s", file
);
50 if (lseek(fd
, 0, SEEK_CUR
) != 123)
51 errx(1, "zero offset from initial after mmap differs from 123 (TEST FAILED)");
55 /* Child. Lock should fail. */
56 if (fcntl(fd
, F_SETLK
, &fl
) == 0)
60 err(1, "Fork failed");
63 if (wait(&status
) == -1)
64 err(1, "Child vanished?");
66 if (!WIFEXITED(status
))
67 errx(1, "Child died with signal %i", WTERMSIG(status
));
69 switch (WEXITSTATUS(status
)) {
71 errx(1, "Child got lock, we must have dropped it (TEST FAILED)");
73 fprintf(stderr
, "Child exited with zero (TEST PASSED).\n");
76 errx(1, "Child weird exit status %i", WEXITSTATUS(status
));