wrapsyscall.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Wrapper arpund 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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <fcntl.h>
  18. #include <sys/mman.h>
  19. #include <pthread.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. #include <termios.h>
  25. #include <sys/epoll.h>
  26. #include <sys/resource.h>
  27. #include <sys/wait.h>
  28. #include <sys/socket.h>
  29. #include <sys/syscall.h>
  30. #ifndef __PIC__
  31. /* We need a hook to force this file to be linked in when static
  32. libpthread is used. */
  33. const char __pthread_provide_wrappers = 0;
  34. #endif
  35. /* Using private interface to libc (__libc_foo) to implement
  36. * cancellable versions of some libc functions */
  37. #define CANCELABLE_SYSCALL(res_type, name, param_list, params) \
  38. res_type __libc_##name param_list; \
  39. res_type \
  40. __attribute__ ((weak)) \
  41. name param_list \
  42. { \
  43. res_type result; \
  44. int oldtype; \
  45. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \
  46. result = __libc_##name params; \
  47. pthread_setcanceltype (oldtype, NULL); \
  48. return result; \
  49. }
  50. #define CANCELABLE_SYSCALL_VA(res_type, name, param_list, params, last_arg) \
  51. res_type __libc_##name param_list; \
  52. res_type \
  53. __attribute__ ((weak)) \
  54. name param_list \
  55. { \
  56. res_type result; \
  57. int oldtype; \
  58. va_list ap; \
  59. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \
  60. va_start (ap, last_arg); \
  61. result = __libc_##name params; \
  62. va_end (ap); \
  63. pthread_setcanceltype (oldtype, NULL); \
  64. return result; \
  65. }
  66. /* close(2). */
  67. CANCELABLE_SYSCALL (int, close, (int fd), (fd))
  68. /* fcntl(2). */
  69. CANCELABLE_SYSCALL_VA (int, fcntl, (int fd, int cmd, ...),
  70. (fd, cmd, va_arg (ap, long int)), cmd)
  71. #ifdef __UCLIBC_HAS_LFS__
  72. /* fcntl64(2). */
  73. CANCELABLE_SYSCALL_VA (int, fcntl64, (int fd, int cmd, ...),
  74. (fd, cmd, va_arg (ap, long int)), cmd)
  75. #endif
  76. /* fsync(2). */
  77. CANCELABLE_SYSCALL (int, fsync, (int fd), (fd))
  78. /* lseek(2). */
  79. CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence),
  80. (fd, offset, whence))
  81. #ifdef __UCLIBC_HAS_LFS__
  82. /* lseek64(2). */
  83. CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence),
  84. (fd, offset, whence))
  85. #endif
  86. #if defined(__NR_msync) && defined(__ARCH_USE_MMU__)
  87. /* msync(2). */
  88. CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags),
  89. (addr, length, flags))
  90. #endif
  91. /* nanosleep(2). */
  92. libpthread_hidden_proto(nanosleep)
  93. CANCELABLE_SYSCALL (int, nanosleep, (const struct timespec *requested_time,
  94. struct timespec *remaining),
  95. (requested_time, remaining))
  96. libpthread_hidden_def(nanosleep)
  97. /* open(2). */
  98. CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...),
  99. (pathname, flags, va_arg (ap, mode_t)), flags)
  100. #ifdef __UCLIBC_HAS_LFS__
  101. /* open64(3). */
  102. CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...),
  103. (pathname, flags, va_arg (ap, mode_t)), flags)
  104. #endif
  105. /* pause(2). */
  106. CANCELABLE_SYSCALL (int, pause, (void), ())
  107. /* Enable this if enabling these in syscalls.c */
  108. /* pread(3). */
  109. CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count,
  110. off_t offset),
  111. (fd, buf, count, offset))
  112. #if defined __UCLIBC_HAS_LFS__ && defined __NR_pread64
  113. /* pread64(3). */
  114. CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count,
  115. off64_t offset),
  116. (fd, buf, count, offset))
  117. #endif
  118. /* pwrite(3). */
  119. CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n,
  120. off_t offset),
  121. (fd, buf, n, offset))
  122. #if defined __UCLIBC_HAS_LFS__ && defined __NR_pwrited64
  123. /* pwrite64(3). */
  124. CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n,
  125. off64_t offset),
  126. (fd, buf, n, offset))
  127. #endif
  128. /* read(2). */
  129. CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count),
  130. (fd, buf, count))
  131. /* system(3). */
  132. CANCELABLE_SYSCALL (int, system, (const char *line), (line))
  133. /* tcdrain(2). */
  134. CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd))
  135. /* wait(2). */
  136. CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc))
  137. /* waitpid(2). */
  138. libpthread_hidden_proto(waitpid)
  139. CANCELABLE_SYSCALL (__pid_t, waitpid, (__pid_t pid, int *stat_loc,
  140. int options),
  141. (pid, stat_loc, options))
  142. libpthread_hidden_def(waitpid)
  143. /* write(2). */
  144. CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n),
  145. (fd, buf, n))
  146. #if defined __UCLIBC_HAS_SOCKET__
  147. /* The following system calls are thread cancellation points specified
  148. in XNS. */
  149. /* accept(2). */
  150. CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr,
  151. socklen_t *addr_len),
  152. (fd, addr, addr_len))
  153. /* connect(2). */
  154. CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr,
  155. socklen_t len),
  156. (fd, addr, len))
  157. /* recv(2). */
  158. CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags),
  159. (fd, buf, n, flags))
  160. /* recvfrom(2). */
  161. CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags,
  162. __SOCKADDR_ARG addr, socklen_t *addr_len),
  163. (fd, buf, n, flags, addr, addr_len))
  164. /* recvmsg(2). */
  165. CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags),
  166. (fd, message, flags))
  167. /* send(2). */
  168. CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n,
  169. int flags),
  170. (fd, buf, n, flags))
  171. /* sendmsg(2). */
  172. CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message,
  173. int flags),
  174. (fd, message, flags))
  175. /* sendto(2). */
  176. CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n,
  177. int flags, __CONST_SOCKADDR_ARG addr,
  178. socklen_t addr_len),
  179. (fd, buf, n, flags, addr, addr_len))
  180. #endif /* __UCLIBC_HAS_SOCKET__ */
  181. #ifdef __UCLIBC_HAS_EPOLL__
  182. # include <sys/epoll.h>
  183. # ifdef __NR_epoll_wait
  184. CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout),
  185. (epfd, events, maxevents, timeout))
  186. # endif
  187. # ifdef __NR_epoll_pwait
  188. CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout,
  189. const sigset_t *set),
  190. (epfd, events, maxevents, timeout, set))
  191. # endif
  192. #endif