lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Library General Public License as |
| 8 | published by the Free Software Foundation; either version 2 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Library General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Library General Public |
| 17 | License along with the GNU C Library; see the file COPYING.LIB. If not, |
| 18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #define __FORCE_GLIBC |
| 22 | #include <features.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <pthread.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <termios.h> |
| 31 | #include <sys/epoll.h> |
| 32 | #include <sys/resource.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/syscall.h> |
| 36 | |
| 37 | |
| 38 | #ifndef __PIC__ |
| 39 | /* We need a hook to force this file to be linked in when static |
| 40 | libpthread is used. */ |
| 41 | const char __pthread_provide_wrappers = 0; |
| 42 | #endif |
| 43 | |
| 44 | /* Using private interface to libc (__libc_foo) to implement |
| 45 | * cancellable versions of some libc functions */ |
| 46 | #define CANCELABLE_SYSCALL(res_type, name, param_list, params) \ |
| 47 | res_type __libc_##name param_list; \ |
| 48 | res_type \ |
| 49 | __attribute__ ((weak)) \ |
| 50 | name param_list \ |
| 51 | { \ |
| 52 | res_type result; \ |
| 53 | int oldtype; \ |
| 54 | pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \ |
| 55 | result = __libc_##name params; \ |
| 56 | pthread_setcanceltype (oldtype, NULL); \ |
| 57 | return result; \ |
| 58 | } |
| 59 | |
| 60 | #define CANCELABLE_SYSCALL_VA(res_type, name, param_list, params, last_arg) \ |
| 61 | res_type __libc_##name param_list; \ |
| 62 | res_type \ |
| 63 | __attribute__ ((weak)) \ |
| 64 | name param_list \ |
| 65 | { \ |
| 66 | res_type result; \ |
| 67 | int oldtype; \ |
| 68 | va_list ap; \ |
| 69 | pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); \ |
| 70 | va_start (ap, last_arg); \ |
| 71 | result = __libc_##name params; \ |
| 72 | va_end (ap); \ |
| 73 | pthread_setcanceltype (oldtype, NULL); \ |
| 74 | return result; \ |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* close(2). */ |
| 79 | CANCELABLE_SYSCALL (int, close, (int fd), (fd)) |
| 80 | |
| 81 | |
| 82 | /* fcntl(2). */ |
| 83 | CANCELABLE_SYSCALL_VA (int, fcntl, (int fd, int cmd, ...), |
| 84 | (fd, cmd, va_arg (ap, long int)), cmd) |
| 85 | |
| 86 | |
| 87 | /* fsync(2). */ |
| 88 | CANCELABLE_SYSCALL (int, fsync, (int fd), (fd)) |
| 89 | |
| 90 | |
| 91 | /* lseek(2). */ |
| 92 | CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence), |
| 93 | (fd, offset, whence)) |
| 94 | |
| 95 | #ifdef __UCLIBC_HAS_LFS__ |
| 96 | /* lseek64(2). */ |
| 97 | CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence), |
| 98 | (fd, offset, whence)) |
| 99 | #endif |
| 100 | |
| 101 | #if defined(__NR_msync) && defined(__ARCH_USE_MMU__) |
| 102 | |
| 103 | /* msync(2). */ |
| 104 | CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags), |
| 105 | (addr, length, flags)) |
| 106 | #endif |
| 107 | |
| 108 | |
| 109 | /* nanosleep(2). */ |
| 110 | libpthread_hidden_proto(nanosleep) |
| 111 | CANCELABLE_SYSCALL (int, nanosleep, (const struct timespec *requested_time, |
| 112 | struct timespec *remaining), |
| 113 | (requested_time, remaining)) |
| 114 | libpthread_hidden_def(nanosleep) |
| 115 | |
| 116 | |
| 117 | /* open(2). */ |
| 118 | CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...), |
| 119 | (pathname, flags, va_arg (ap, mode_t)), flags) |
| 120 | |
| 121 | |
| 122 | #ifdef __UCLIBC_HAS_LFS__ |
| 123 | /* open64(3). */ |
| 124 | CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...), |
| 125 | (pathname, flags, va_arg (ap, mode_t)), flags) |
| 126 | #endif |
| 127 | |
| 128 | /* pause(2). */ |
| 129 | CANCELABLE_SYSCALL (int, pause, (void), ()) |
| 130 | |
| 131 | |
| 132 | /* Enable this if enabling these in syscalls.c */ |
| 133 | /* pread(3). */ |
| 134 | CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count, |
| 135 | off_t offset), |
| 136 | (fd, buf, count, offset)) |
| 137 | |
| 138 | |
| 139 | #if defined __UCLIBC_HAS_LFS__ && defined __NR_pread64 |
| 140 | /* pread64(3). */ |
| 141 | CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count, |
| 142 | off64_t offset), |
| 143 | (fd, buf, count, offset)) |
| 144 | #endif |
| 145 | |
| 146 | /* pwrite(3). */ |
| 147 | CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n, |
| 148 | off_t offset), |
| 149 | (fd, buf, n, offset)) |
| 150 | |
| 151 | |
| 152 | #if defined __UCLIBC_HAS_LFS__ && defined __NR_pwrited64 |
| 153 | /* pwrite64(3). */ |
| 154 | CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n, |
| 155 | off64_t offset), |
| 156 | (fd, buf, n, offset)) |
| 157 | #endif |
| 158 | |
| 159 | /* read(2). */ |
| 160 | CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count), |
| 161 | (fd, buf, count)) |
| 162 | |
| 163 | |
| 164 | /* system(3). */ |
| 165 | CANCELABLE_SYSCALL (int, system, (const char *line), (line)) |
| 166 | |
| 167 | |
| 168 | /* tcdrain(2). */ |
| 169 | CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd)) |
| 170 | |
| 171 | |
| 172 | /* wait(2). */ |
| 173 | CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc)) |
| 174 | |
| 175 | |
| 176 | /* waitpid(2). */ |
| 177 | libpthread_hidden_proto(waitpid) |
| 178 | CANCELABLE_SYSCALL (__pid_t, waitpid, (__pid_t pid, int *stat_loc, |
| 179 | int options), |
| 180 | (pid, stat_loc, options)) |
| 181 | libpthread_hidden_def(waitpid) |
| 182 | |
| 183 | |
| 184 | /* write(2). */ |
| 185 | CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n), |
| 186 | (fd, buf, n)) |
| 187 | |
| 188 | #if defined __UCLIBC_HAS_SOCKET__ |
| 189 | /* The following system calls are thread cancellation points specified |
| 190 | in XNS. */ |
| 191 | |
| 192 | /* accept(2). */ |
| 193 | CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr, |
| 194 | socklen_t *addr_len), |
| 195 | (fd, addr, addr_len)) |
| 196 | |
| 197 | /* connect(2). */ |
| 198 | CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr, |
| 199 | socklen_t len), |
| 200 | (fd, addr, len)) |
| 201 | |
| 202 | /* recv(2). */ |
| 203 | CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags), |
| 204 | (fd, buf, n, flags)) |
| 205 | |
| 206 | /* recvfrom(2). */ |
| 207 | CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags, |
| 208 | __SOCKADDR_ARG addr, socklen_t *addr_len), |
| 209 | (fd, buf, n, flags, addr, addr_len)) |
| 210 | |
| 211 | /* recvmsg(2). */ |
| 212 | CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags), |
| 213 | (fd, message, flags)) |
| 214 | |
| 215 | /* send(2). */ |
| 216 | CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n, |
| 217 | int flags), |
| 218 | (fd, buf, n, flags)) |
| 219 | |
| 220 | /* sendmsg(2). */ |
| 221 | CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message, |
| 222 | int flags), |
| 223 | (fd, message, flags)) |
| 224 | |
| 225 | /* sendto(2). */ |
| 226 | CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n, |
| 227 | int flags, __CONST_SOCKADDR_ARG addr, |
| 228 | socklen_t addr_len), |
| 229 | (fd, buf, n, flags, addr, addr_len)) |
| 230 | #endif /* __UCLIBC_HAS_SOCKET__ */ |
| 231 | |
| 232 | #ifdef __UCLIBC_HAS_EPOLL__ |
| 233 | # ifdef __NR_epoll_wait |
| 234 | CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout), |
| 235 | (epfd, events, maxevents, timeout)) |
| 236 | # endif |
| 237 | # ifdef __NR_epoll_pwait |
| 238 | # include <signal.h> |
| 239 | CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout, |
| 240 | const sigset_t *set), |
| 241 | (epfd, events, maxevents, timeout, set)) |
| 242 | # endif |
| 243 | #endif |