fcntl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Linux fcntl syscall implementation.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <fcntl.h>
  16. #include <stdarg.h>
  17. #include <errno.h>
  18. #include <sysdep-cancel.h>
  19. #ifndef __OFF_T_MATCHES_OFF64_T
  20. # ifndef FCNTL_ADJUST_CMD
  21. # define FCNTL_ADJUST_CMD(__cmd) __cmd
  22. # endif
  23. int
  24. __libc_fcntl (int fd, int cmd, ...)
  25. {
  26. va_list ap;
  27. void *arg;
  28. va_start (ap, cmd);
  29. arg = va_arg (ap, void *);
  30. va_end (ap);
  31. cmd = FCNTL_ADJUST_CMD (cmd);
  32. switch (cmd)
  33. {
  34. case F_SETLKW:
  35. case F_SETLKW64:
  36. return SYSCALL_CANCEL (fcntl64, fd, cmd, arg);
  37. case F_OFD_SETLKW:
  38. {
  39. struct flock *flk = (struct flock *) arg;
  40. struct flock64 flk64 =
  41. {
  42. .l_type = flk->l_type,
  43. .l_whence = flk->l_whence,
  44. .l_start = flk->l_start,
  45. .l_len = flk->l_len,
  46. .l_pid = flk->l_pid
  47. };
  48. return SYSCALL_CANCEL (fcntl64, fd, cmd, &flk64);
  49. }
  50. case F_OFD_GETLK:
  51. case F_OFD_SETLK:
  52. {
  53. struct flock *flk = (struct flock *) arg;
  54. struct flock64 flk64 =
  55. {
  56. .l_type = flk->l_type,
  57. .l_whence = flk->l_whence,
  58. .l_start = flk->l_start,
  59. .l_len = flk->l_len,
  60. .l_pid = flk->l_pid
  61. };
  62. int ret = INLINE_SYSCALL_CALL (fcntl64, fd, cmd, &flk64);
  63. if (ret == -1)
  64. return -1;
  65. if ((off_t) flk64.l_start != flk64.l_start
  66. || (off_t) flk64.l_len != flk64.l_len)
  67. {
  68. __set_errno (EOVERFLOW);
  69. return -1;
  70. }
  71. flk->l_type = flk64.l_type;
  72. flk->l_whence = flk64.l_whence;
  73. flk->l_start = flk64.l_start;
  74. flk->l_len = flk64.l_len;
  75. flk->l_pid = flk64.l_pid;
  76. return ret;
  77. }
  78. /* Since only F_SETLKW{64}/F_OLD_SETLK are cancellation entrypoints and
  79. only OFD locks require LFS handling, all others flags are handled
  80. unmodified by calling __NR_fcntl64. */
  81. default:
  82. return __fcntl64_nocancel_adjusted (fd, cmd, arg);
  83. }
  84. }
  85. libc_hidden_def (__libc_fcntl)
  86. weak_alias (__libc_fcntl, __fcntl)
  87. libc_hidden_weak (__fcntl)
  88. # include <shlib-compat.h>
  89. # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_28)
  90. int
  91. __old_libc_fcntl64 (int fd, int cmd, ...)
  92. {
  93. va_list ap;
  94. void *arg;
  95. va_start (ap, cmd);
  96. arg = va_arg (ap, void *);
  97. va_end (ap);
  98. /* Previous versions called __NR_fcntl64 for fcntl (which did not handle
  99. OFD locks in LFS mode). */
  100. return __libc_fcntl64 (fd, cmd, arg);
  101. }
  102. compat_symbol (libc, __old_libc_fcntl64, fcntl, GLIBC_2_0);
  103. versioned_symbol (libc, __libc_fcntl, fcntl, GLIBC_2_28);
  104. # else
  105. weak_alias (__libc_fcntl, fcntl)
  106. # endif
  107. #endif /* __OFF_T_MATCHES_OFF64_T */