ofdlocks.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <fcntl.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include "kselftest.h"
  9. static int lock_set(int fd, struct flock *fl)
  10. {
  11. int ret;
  12. fl->l_pid = 0; // needed for OFD locks
  13. fl->l_whence = SEEK_SET;
  14. ret = fcntl(fd, F_OFD_SETLK, fl);
  15. if (ret)
  16. perror("fcntl()");
  17. return ret;
  18. }
  19. static int lock_get(int fd, struct flock *fl)
  20. {
  21. int ret;
  22. fl->l_pid = 0; // needed for OFD locks
  23. fl->l_whence = SEEK_SET;
  24. ret = fcntl(fd, F_OFD_GETLK, fl);
  25. if (ret)
  26. perror("fcntl()");
  27. return ret;
  28. }
  29. int main(void)
  30. {
  31. int rc;
  32. struct flock fl, fl2;
  33. int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
  34. int fd2 = open("/tmp/aa", O_RDONLY);
  35. unlink("/tmp/aa");
  36. assert(fd != -1);
  37. assert(fd2 != -1);
  38. ksft_print_msg("[INFO] opened fds %i %i\n", fd, fd2);
  39. /* Set some read lock */
  40. fl.l_type = F_RDLCK;
  41. fl.l_start = 5;
  42. fl.l_len = 3;
  43. rc = lock_set(fd, &fl);
  44. if (rc == 0) {
  45. ksft_print_msg
  46. ("[SUCCESS] set OFD read lock on first fd\n");
  47. } else {
  48. ksft_print_msg("[FAIL] to set OFD read lock on first fd\n");
  49. return -1;
  50. }
  51. /* Make sure read locks do not conflict on different fds. */
  52. fl.l_type = F_RDLCK;
  53. fl.l_start = 5;
  54. fl.l_len = 1;
  55. rc = lock_get(fd2, &fl);
  56. if (rc != 0)
  57. return -1;
  58. if (fl.l_type != F_UNLCK) {
  59. ksft_print_msg("[FAIL] read locks conflicted\n");
  60. return -1;
  61. }
  62. /* Make sure read/write locks do conflict on different fds. */
  63. fl.l_type = F_WRLCK;
  64. fl.l_start = 5;
  65. fl.l_len = 1;
  66. rc = lock_get(fd2, &fl);
  67. if (rc != 0)
  68. return -1;
  69. if (fl.l_type != F_UNLCK) {
  70. ksft_print_msg
  71. ("[SUCCESS] read and write locks conflicted\n");
  72. } else {
  73. ksft_print_msg
  74. ("[SUCCESS] read and write locks not conflicted\n");
  75. return -1;
  76. }
  77. /* Get info about the lock on first fd. */
  78. fl.l_type = F_UNLCK;
  79. fl.l_start = 5;
  80. fl.l_len = 1;
  81. rc = lock_get(fd, &fl);
  82. if (rc != 0) {
  83. ksft_print_msg
  84. ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
  85. return -1;
  86. }
  87. if (fl.l_type != F_UNLCK) {
  88. ksft_print_msg
  89. ("[SUCCESS] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
  90. fl.l_type, fl.l_pid, fl.l_len);
  91. } else {
  92. ksft_print_msg
  93. ("[FAIL] F_OFD_GETLK with F_UNLCK did not return lock info\n");
  94. return -1;
  95. }
  96. /* Try the same but by locking everything by len==0. */
  97. fl2.l_type = F_UNLCK;
  98. fl2.l_start = 0;
  99. fl2.l_len = 0;
  100. rc = lock_get(fd, &fl2);
  101. if (rc != 0) {
  102. ksft_print_msg
  103. ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
  104. return -1;
  105. }
  106. if (memcmp(&fl, &fl2, sizeof(fl))) {
  107. ksft_print_msg
  108. ("[FAIL] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
  109. fl.l_type, fl.l_pid, fl.l_len);
  110. return -1;
  111. }
  112. ksft_print_msg("[SUCCESS] F_UNLCK with len==0 returned the same\n");
  113. /* Get info about the lock on second fd - no locks on it. */
  114. fl.l_type = F_UNLCK;
  115. fl.l_start = 0;
  116. fl.l_len = 0;
  117. lock_get(fd2, &fl);
  118. if (fl.l_type != F_UNLCK) {
  119. ksft_print_msg
  120. ("[FAIL] F_OFD_GETLK with F_UNLCK return lock info from another fd\n");
  121. return -1;
  122. }
  123. return 0;
  124. }