summaryrefslogtreecommitdiff
path: root/libpthread/linuxthreads/wrapsyscall.c
blob: ca046a4425d90daceac19a24a547ecf0e4a05099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Wrapper around system calls to provide cancellation points.
   Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   see <http://www.gnu.org/licenses/>.  */

#include <fcntl.h>
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/syscall.h>


#ifndef __PIC__
/* We need a hook to force this file to be linked in when static
   libpthread is used.  */
const char __pthread_provide_wrappers = 0;
#endif

/* Using private interface to libc (__libc_foo) to implement
 * cancellable versions of some libc functions */
#define CANCELABLE_SYSCALL(res_type, name, param_list, params)			\
res_type __libc_##name param_list;						\
res_type									\
__attribute__ ((weak))								\
name param_list									\
{										\
  res_type result;								\
  int oldtype;									\
  pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);		\
  result = __libc_##name params;						\
  pthread_setcanceltype (oldtype, NULL);					\
  return result;								\
}

#define CANCELABLE_SYSCALL_VA(res_type, name, param_list, params, last_arg)	\
res_type __libc_##name param_list;						\
res_type									\
__attribute__ ((weak))								\
name param_list									\
{										\
  res_type result;								\
  int oldtype;									\
  va_list ap;									\
  pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);		\
  va_start (ap, last_arg);							\
  result = __libc_##name params;						\
  va_end (ap);									\
  pthread_setcanceltype (oldtype, NULL);					\
  return result;								\
}


/* close(2).  */
CANCELABLE_SYSCALL (int, close, (int fd), (fd))


/* fcntl(2).  */
CANCELABLE_SYSCALL_VA (int, fcntl, (int fd, int cmd, ...),
		       (fd, cmd, va_arg (ap, long int)), cmd)

#if __WORDSIZE == 32
/* fcntl64(2).  */
CANCELABLE_SYSCALL_VA (int, fcntl64, (int fd, int cmd, ...),
		       (fd, cmd, va_arg (ap, long int)), cmd)
#endif

/* fsync(2).  */
CANCELABLE_SYSCALL (int, fsync, (int fd), (fd))


/* lseek(2).  */
CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence),
		    (fd, offset, whence))

/* lseek64(2).  */
CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence),
		    (fd, offset, whence))

#if defined(__NR_msync) && defined(__ARCH_USE_MMU__)

/* msync(2).  */
CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags),
		    (addr, length, flags))
#endif


/* nanosleep(2).  */
libpthread_hidden_proto(nanosleep)
CANCELABLE_SYSCALL (int, nanosleep, (const struct timespec *requested_time,
				     struct timespec *remaining),
		    (requested_time, remaining))
libpthread_hidden_def(nanosleep)


/* open(2).  */
CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...),
		       (pathname, flags, va_arg (ap, mode_t)), flags)


/* open64(3).  */
CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...),
		       (pathname, flags, va_arg (ap, mode_t)), flags)

/* pause(2).  */
CANCELABLE_SYSCALL (int, pause, (void), ())


/* Enable this if enabling these in syscalls.c */
/* pread(3).  */
CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count,
				     off_t offset),
		    (fd, buf, count, offset))


#if defined __NR_pread64
/* pread64(3).  */
CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count,
				       off64_t offset),
		    (fd, buf, count, offset))
#endif

/* pwrite(3).  */
CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n,
				      off_t offset),
		    (fd, buf, n, offset))


#if defined __NR_pwrited64
/* pwrite64(3).  */
CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n,
					off64_t offset),
		    (fd, buf, n, offset))
#endif

/* read(2).  */
CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count),
		    (fd, buf, count))


/* system(3).  */
CANCELABLE_SYSCALL (int, system, (const char *line), (line))


/* tcdrain(2).  */
CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd))


/* wait(2).  */
CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc))


/* waitpid(2).  */
libpthread_hidden_proto(waitpid)
CANCELABLE_SYSCALL (__pid_t, waitpid, (__pid_t pid, int *stat_loc,
				       int options),
		    (pid, stat_loc, options))
libpthread_hidden_def(waitpid)


/* write(2).  */
CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n),
		    (fd, buf, n))

#if defined __UCLIBC_HAS_SOCKET__
/* The following system calls are thread cancellation points specified
   in XNS.  */

/* accept(2).  */
CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr,
				  socklen_t *addr_len),
		    (fd, addr, addr_len))

/* connect(2).  */
CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr,
				     socklen_t len),
		    (fd, addr, len))

/* recv(2).  */
CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags),
		    (fd, buf, n, flags))

/* recvfrom(2).  */
CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags,
					__SOCKADDR_ARG addr, socklen_t *addr_len),
		    (fd, buf, n, flags, addr, addr_len))

/* recvmsg(2).  */
CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags),
		    (fd, message, flags))

/* send(2).  */
CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n,
				    int flags),
		    (fd, buf, n, flags))

/* sendmsg(2).  */
CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message,
				       int flags),
		    (fd, message, flags))

/* sendto(2).  */
CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n,
				      int flags, __CONST_SOCKADDR_ARG addr,
				      socklen_t addr_len),
		    (fd, buf, n, flags, addr, addr_len))
#endif /* __UCLIBC_HAS_SOCKET__ */

#ifdef  __UCLIBC_HAS_EPOLL__
# include <sys/epoll.h>
# ifdef __NR_epoll_wait
CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout),
		    (epfd, events, maxevents, timeout))
# endif
# ifdef __NR_epoll_pwait
CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout,
				       const sigset_t *set),
		    (epfd, events, maxevents, timeout, set))
# endif
#endif