| 1 |
|
|---|
| 2 | #include <sys/types.h>
|
|---|
| 3 | #include <sys/stat.h>
|
|---|
| 4 | #include <fcntl.h>
|
|---|
| 5 | #include <unistd.h>
|
|---|
| 6 | #include <stdio.h>
|
|---|
| 7 |
|
|---|
| 8 | int main(int argc, char *argv[])
|
|---|
| 9 | {
|
|---|
| 10 | unlink("test.lock");
|
|---|
| 11 |
|
|---|
| 12 | printf("Open file. Result: ");
|
|---|
| 13 | int fd = open("test.lock", O_WRONLY|O_CREAT, 0666);
|
|---|
| 14 | printf("%i\n", fd);
|
|---|
| 15 |
|
|---|
| 16 | printf("Locking file. Result: ");
|
|---|
| 17 | struct flock lock_data;
|
|---|
| 18 | lock_data.l_type = F_WRLCK;
|
|---|
| 19 | lock_data.l_whence = SEEK_SET;
|
|---|
| 20 | lock_data.l_start = 0;
|
|---|
| 21 | lock_data.l_len = 1;
|
|---|
| 22 | int result = fcntl(fd, F_SETLK, &lock_data);
|
|---|
| 23 | printf("%i\n", result);
|
|---|
| 24 |
|
|---|
| 25 | printf("Unlink file. Result: ");
|
|---|
| 26 | // following call fails, when executing in a shared folder:
|
|---|
| 27 | result = unlink("test.lock");
|
|---|
| 28 | printf("%i\n", result);
|
|---|
| 29 |
|
|---|
| 30 | if (result == -1)
|
|---|
| 31 | {
|
|---|
| 32 | printf("Test failed\n");
|
|---|
| 33 | close(fd);
|
|---|
| 34 | unlink("test.lock");
|
|---|
| 35 | return -1;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | printf("Close file. Result: ");
|
|---|
| 39 | result = close(fd);
|
|---|
| 40 | printf("%i\n", result);
|
|---|
| 41 |
|
|---|
| 42 | printf("Test passed.\n");
|
|---|
| 43 |
|
|---|
| 44 | return 0;
|
|---|
| 45 | }
|
|---|