| 1 |
/* COVERAGE: flock */ |
| 2 |
#include <stdio.h> |
| 3 |
#include <sys/file.h> |
| 4 |
#include <sys/types.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <fcntl.h> |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
int fd; |
| 10 |
char filename[100]; |
| 11 |
|
| 12 |
|
| 13 |
int main() { |
| 14 |
sprintf(filename, "flock-tst.%d", getpid()); |
| 15 |
fd = creat(filename, 0644); |
| 16 |
if (fd < 0) { |
| 17 |
printf("ERROR"); |
| 18 |
return 1; |
| 19 |
} |
| 20 |
|
| 21 |
// --- test normal operation --- |
| 22 |
|
| 23 |
flock(fd, LOCK_SH); |
| 24 |
//staptest// flock (NNNN, LOCK_SH) = 0 |
| 25 |
|
| 26 |
flock(fd, LOCK_UN); |
| 27 |
//staptest// flock (NNNN, LOCK_UN) = 0 |
| 28 |
|
| 29 |
flock(fd, LOCK_EX); |
| 30 |
//staptest// flock (NNNN, LOCK_EX) = 0 |
| 31 |
|
| 32 |
flock(fd, LOCK_UN); |
| 33 |
//staptest// flock (NNNN, LOCK_UN) = 0 |
| 34 |
|
| 35 |
// --- now try nasty things --- |
| 36 |
|
| 37 |
flock(-1, LOCK_SH); |
| 38 |
//staptest// flock (-1, LOCK_SH) = NNNN (EBADF) |
| 39 |
|
| 40 |
flock(fd, -1); |
| 41 |
//staptest// flock (NNNN, LOCK_[^ ]+|XXXX) = NNNN |
| 42 |
|
| 43 |
// The above should do LOCK_SH in fact. |
| 44 |
// So let's go ahead and call LOCK_UN at the end: |
| 45 |
flock(fd, LOCK_UN); |
| 46 |
|
| 47 |
close(fd); |
| 48 |
unlink(filename); |
| 49 |
} |