wrapsyscall.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Wrapper around system calls to provide cancellation points.
  2. Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. see <http://www.gnu.org/licenses/>. */
  16. #include <fcntl.h>
  17. #include <sys/mman.h>
  18. #include <pthread.h>
  19. #include <unistd.h>
  20. #include <stdarg.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <termios.h>
  24. #include <sys/epoll.h>
  25. #include <sys/resource.h>
  26. #include <sys/wait.h>
  27. #include <sys/socket.h>
  28. #include <sys/syscall.h>
  29. #ifndef __PIC__
  30. /* We need a hook to force this file to be linked in when static
  31. libpthread is used. */
  32. const char __pthread_provide_wrappers = 0;
  33. #endif
  34. /* Using private interface to libc (__libc_foo) to implement
  35. * cancellable versions of some libc functions */
  36. #define CANCELABLE_SYSCALL(res_type, name, param_list, params) \
  37. res_type __libc_##name param_list; \
  38. res_type \
  39. __attribute__ ((weak)) \
  40. name param_list \
  41. { \
  42. res_type result; \
  43. int oldtype; \
  44. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \
  45. result = __libc_##name params; \
  46. pthread_setcanceltype (oldtype, NULL); \
  47. return result; \
  48. }
  49. /* Like CANCELABLE_SYSCALL but the wrapper is a strong definition. Used for
  50. * the functions that also export a __GI_ alias (libpthread_hidden_def): a weak
  51. * wrapper makes that __GI_ symbol weak too, and on MIPS a function derives its
  52. * PIC gp via %gp_rel(__GI_name) -- with two weak __GI_name (this wrapper and
  53. * the libc fallback) the linker may pick the fallback, at a different address,
  54. * so the wrapper computes a wrong gp and SIGSEGVs in its prologue. A strong
  55. * wrapper makes its __GI_ win the link, co-located with the code. */
  56. #define CANCELABLE_SYSCALL_STRONG(res_type, name, param_list, params) \
  57. res_type __libc_##name param_list; \
  58. res_type \
  59. name param_list \
  60. { \
  61. res_type result; \
  62. int oldtype; \
  63. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \
  64. result = __libc_##name params; \
  65. pthread_setcanceltype (oldtype, NULL); \
  66. return result; \
  67. }
  68. #define CANCELABLE_SYSCALL_VA(res_type, name, param_list, params, last_arg) \
  69. res_type __libc_##name param_list; \
  70. res_type \
  71. __attribute__ ((weak)) \
  72. name param_list \
  73. { \
  74. res_type result; \
  75. int oldtype; \
  76. va_list ap; \
  77. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \
  78. va_start (ap, last_arg); \
  79. result = __libc_##name params; \
  80. va_end (ap); \
  81. pthread_setcanceltype (oldtype, NULL); \
  82. return result; \
  83. }
  84. /* close(2). */
  85. CANCELABLE_SYSCALL (int, close, (int fd), (fd))
  86. /* fcntl(2). */
  87. CANCELABLE_SYSCALL_VA (int, fcntl, (int fd, int cmd, ...),
  88. (fd, cmd, va_arg (ap, long int)), cmd)
  89. #if __WORDSIZE == 32
  90. /* fcntl64(2). */
  91. CANCELABLE_SYSCALL_VA (int, fcntl64, (int fd, int cmd, ...),
  92. (fd, cmd, va_arg (ap, long int)), cmd)
  93. #endif
  94. /* fsync(2). */
  95. CANCELABLE_SYSCALL (int, fsync, (int fd), (fd))
  96. /* lseek(2). */
  97. CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence),
  98. (fd, offset, whence))
  99. /* lseek64(2). */
  100. CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence),
  101. (fd, offset, whence))
  102. #if defined(__NR_msync) && defined(__ARCH_USE_MMU__)
  103. /* msync(2). */
  104. CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags),
  105. (addr, length, flags))
  106. #endif
  107. /* nanosleep(2). */
  108. libpthread_hidden_proto(nanosleep)
  109. CANCELABLE_SYSCALL_STRONG (int, nanosleep, (const struct timespec *requested_time,
  110. struct timespec *remaining),
  111. (requested_time, remaining))
  112. libpthread_hidden_def(nanosleep)
  113. /* open(2). */
  114. CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...),
  115. (pathname, flags, va_arg (ap, mode_t)), flags)
  116. /* open64(3). */
  117. CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...),
  118. (pathname, flags, va_arg (ap, mode_t)), flags)
  119. /* pause(2). */
  120. CANCELABLE_SYSCALL (int, pause, (void), ())
  121. /* Enable this if enabling these in syscalls.c */
  122. /* pread(3). */
  123. CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count,
  124. off_t offset),
  125. (fd, buf, count, offset))
  126. #if defined __NR_pread64
  127. /* pread64(3). */
  128. CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count,
  129. off64_t offset),
  130. (fd, buf, count, offset))
  131. #endif
  132. /* pwrite(3). */
  133. CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n,
  134. off_t offset),
  135. (fd, buf, n, offset))
  136. #if defined __NR_pwrited64
  137. /* pwrite64(3). */
  138. CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n,
  139. off64_t offset),
  140. (fd, buf, n, offset))
  141. #endif
  142. /* read(2). */
  143. CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count),
  144. (fd, buf, count))
  145. /* system(3). */
  146. CANCELABLE_SYSCALL (int, system, (const char *line), (line))
  147. /* tcdrain(2). */
  148. CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd))
  149. /* wait(2). */
  150. CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc))
  151. /* waitpid(2). */
  152. libpthread_hidden_proto(waitpid)
  153. CANCELABLE_SYSCALL_STRONG (__pid_t, waitpid, (__pid_t pid, int *stat_loc,
  154. int options),
  155. (pid, stat_loc, options))
  156. libpthread_hidden_def(waitpid)
  157. /* write(2). */
  158. CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n),
  159. (fd, buf, n))
  160. #if defined __UCLIBC_HAS_SOCKET__
  161. /* The following system calls are thread cancellation points specified
  162. in XNS. */
  163. /* accept(2). */
  164. CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr,
  165. socklen_t *addr_len),
  166. (fd, addr, addr_len))
  167. #if defined __UCLIBC_LINUX_SPECIFIC__
  168. /* accept4(2). */
  169. CANCELABLE_SYSCALL (int, accept4, (int fd, __SOCKADDR_ARG addr,
  170. socklen_t *addr_len, int flags),
  171. (fd, addr, addr_len, flags))
  172. #endif
  173. /* connect(2). */
  174. CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr,
  175. socklen_t len),
  176. (fd, addr, len))
  177. /* recv(2). */
  178. CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags),
  179. (fd, buf, n, flags))
  180. /* recvfrom(2). */
  181. CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags,
  182. __SOCKADDR_ARG addr, socklen_t *addr_len),
  183. (fd, buf, n, flags, addr, addr_len))
  184. /* recvmsg(2). */
  185. CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags),
  186. (fd, message, flags))
  187. /* send(2). */
  188. CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n,
  189. int flags),
  190. (fd, buf, n, flags))
  191. /* sendmsg(2). */
  192. CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message,
  193. int flags),
  194. (fd, message, flags))
  195. /* sendto(2). */
  196. CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n,
  197. int flags, __CONST_SOCKADDR_ARG addr,
  198. socklen_t addr_len),
  199. (fd, buf, n, flags, addr, addr_len))
  200. #endif /* __UCLIBC_HAS_SOCKET__ */
  201. #ifdef __UCLIBC_HAS_EPOLL__
  202. # include <sys/epoll.h>
  203. # ifdef __NR_epoll_wait
  204. CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout),
  205. (epfd, events, maxevents, timeout))
  206. # endif
  207. # ifdef __NR_epoll_pwait
  208. CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout,
  209. const sigset_t *set),
  210. (epfd, events, maxevents, timeout, set))
  211. # endif
  212. #endif