tst-ofdlocks.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Check non representable OFD locks regions in non-LFS mode (BZ #20251)
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <stdint.h>
  17. #include <errno.h>
  18. #include <support/temp_file.h>
  19. #include <support/check.h>
  20. static char *temp_filename;
  21. static int temp_fd;
  22. static void
  23. do_prepare (int argc, char **argv)
  24. {
  25. temp_fd = create_temp_file ("tst-ofdlocks.", &temp_filename);
  26. TEST_VERIFY_EXIT (temp_fd != -1);
  27. }
  28. #define PREPARE do_prepare
  29. static int
  30. do_test (void)
  31. {
  32. /* It first allocates a open file description lock range which can not
  33. be represented in a 32 bit struct flock. */
  34. struct flock64 lck64 = {
  35. .l_type = F_WRLCK,
  36. .l_whence = SEEK_SET,
  37. .l_start = (off64_t)INT32_MAX + 1024,
  38. .l_len = 1024,
  39. };
  40. int ret = fcntl64 (temp_fd, F_OFD_SETLKW, &lck64);
  41. if (ret == -1 && errno == EINVAL)
  42. /* OFD locks are only available on Linux 3.15. */
  43. FAIL_UNSUPPORTED ("fcntl (F_OFD_SETLKW) not supported");
  44. TEST_VERIFY_EXIT (ret == 0);
  45. /* Open file description locks placed through the same open file description
  46. (either by same file descriptor or a duplicated one created by fork,
  47. dup, fcntl F_DUPFD, etc.) overwrites then old lock. To force a
  48. conflicting lock combination, it creates a new file descriptor. */
  49. int fd = open64 (temp_filename, O_RDWR, 0666);
  50. TEST_VERIFY_EXIT (fd != -1);
  51. /* It tries then to allocate another open file descriptior with a valid
  52. non-LFS bits struct flock but which will result in a conflicted region
  53. which can not be represented in a non-LFS struct flock. */
  54. struct flock lck = {
  55. .l_type = F_WRLCK,
  56. .l_whence = SEEK_SET,
  57. .l_start = INT32_MAX - 1024,
  58. .l_len = 4 * 1024,
  59. };
  60. int r = fcntl (fd, F_OFD_GETLK, &lck);
  61. if (sizeof (off_t) != sizeof (off64_t))
  62. TEST_VERIFY (r == -1 && errno == EOVERFLOW);
  63. else
  64. TEST_VERIFY (r == 0);
  65. return 0;
  66. }
  67. #include <support/test-driver.c>