1112918Sjeff/*
2144518Sdavidxu * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3144518Sdavidxu * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
4144518Sdavidxu * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
5112918Sjeff * All rights reserved.
6112918Sjeff *
7112918Sjeff * Redistribution and use in source and binary forms, with or without
8112918Sjeff * modification, are permitted provided that the following conditions
9112918Sjeff * are met:
10112918Sjeff * 1. Redistributions of source code must retain the above copyright
11112918Sjeff *    notice(s), this list of conditions and the following disclaimer as
12112918Sjeff *    the first lines of this file unmodified other than the possible
13112918Sjeff *    addition of one or more copyright notices.
14112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15112918Sjeff *    notice(s), this list of conditions and the following disclaimer in
16112918Sjeff *    the documentation and/or other materials provided with the
17112918Sjeff *    distribution.
18112918Sjeff *
19112918Sjeff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20112918Sjeff * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22112918Sjeff * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23112918Sjeff * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24112918Sjeff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25112918Sjeff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26112918Sjeff * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27112918Sjeff * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28112918Sjeff * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29112918Sjeff * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30112918Sjeff *
31112918Sjeff * $FreeBSD$
32112918Sjeff */
33112918Sjeff
34112918Sjeff/*
35112918Sjeff * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
36112918Sjeff * All rights reserved.
37112918Sjeff *
38112918Sjeff * Redistribution and use in source and binary forms, with or without
39112918Sjeff * modification, are permitted provided that the following conditions
40112918Sjeff * are met:
41112918Sjeff * 1. Redistributions of source code must retain the above copyright
42112918Sjeff *    notice, this list of conditions and the following disclaimer.
43112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
44112918Sjeff *    notice, this list of conditions and the following disclaimer in the
45112918Sjeff *    documentation and/or other materials provided with the distribution.
46165967Simp * 3. Neither the name of the author nor the names of any co-contributors
47112918Sjeff *    may be used to endorse or promote products derived from this software
48112918Sjeff *    without specific prior written permission.
49112918Sjeff *
50112918Sjeff * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
51112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53112918Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60112918Sjeff * SUCH DAMAGE.
61112918Sjeff *
62112918Sjeff */
63112918Sjeff
64157457Sdavidxu#include "namespace.h"
65144518Sdavidxu#include <sys/types.h>
66112918Sjeff#include <sys/mman.h>
67112918Sjeff#include <sys/param.h>
68112918Sjeff#include <sys/select.h>
69144518Sdavidxu#include <sys/signalvar.h>
70135301Smtm#include <sys/socket.h>
71144518Sdavidxu#include <sys/stat.h>
72112918Sjeff#include <sys/time.h>
73112918Sjeff#include <sys/uio.h>
74112918Sjeff#include <sys/wait.h>
75112918Sjeff#include <aio.h>
76112918Sjeff#include <dirent.h>
77112918Sjeff#include <errno.h>
78112918Sjeff#include <fcntl.h>
79112918Sjeff#include <poll.h>
80112918Sjeff#include <signal.h>
81112918Sjeff#include <stdarg.h>
82112918Sjeff#include <stdio.h>
83112918Sjeff#include <stdlib.h>
84112918Sjeff#include <string.h>
85112918Sjeff#include <termios.h>
86112918Sjeff#include <unistd.h>
87144518Sdavidxu#include <pthread.h>
88157457Sdavidxu#include "un-namespace.h"
89112918Sjeff
90112918Sjeff#include "thr_private.h"
91112918Sjeff
92157457Sdavidxuextern int	__creat(const char *, mode_t);
93157457Sdavidxuextern int	__pselect(int, fd_set *, fd_set *, fd_set *,
94157457Sdavidxu			const struct timespec *, const sigset_t *);
95157457Sdavidxuextern unsigned	__sleep(unsigned int);
96157457Sdavidxuextern int	__system(const char *);
97157457Sdavidxuextern int	__tcdrain(int);
98157457Sdavidxuextern int	__usleep(useconds_t);
99157457Sdavidxuextern pid_t	__wait(int *);
100157457Sdavidxuextern pid_t	__waitpid(pid_t, int *, int);
101157457Sdavidxuextern int	__sys_aio_suspend(const struct aiocb * const[], int,
102157457Sdavidxu			const struct timespec *);
103157457Sdavidxuextern int	__sys_accept(int, struct sockaddr *, socklen_t *);
104250154Sjillesextern int	__sys_accept4(int, struct sockaddr *, socklen_t *, int);
105157457Sdavidxuextern int	__sys_connect(int, const struct sockaddr *, socklen_t);
106157457Sdavidxuextern int	__sys_fsync(int);
107157457Sdavidxuextern int	__sys_msync(void *, size_t, int);
108198508Skibextern int	__sys_pselect(int, fd_set *, fd_set *, fd_set *,
109198508Skib			const struct timespec *, const sigset_t *);
110157457Sdavidxuextern int	__sys_poll(struct pollfd *, unsigned, int);
111157457Sdavidxuextern ssize_t	__sys_recv(int, void *, size_t, int);
112157457Sdavidxuextern ssize_t	__sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
113157457Sdavidxuextern ssize_t	__sys_recvmsg(int, struct msghdr *, int);
114157457Sdavidxuextern int	__sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
115157457Sdavidxuextern int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
116157457Sdavidxu			off_t *, int);
117157457Sdavidxuextern ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
118157457Sdavidxuextern ssize_t	__sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
119157457Sdavidxuextern ssize_t	__sys_readv(int, const struct iovec *, int);
120157457Sdavidxuextern pid_t	__sys_wait4(pid_t, int *, int, struct rusage *);
121157457Sdavidxuextern ssize_t	__sys_writev(int, const struct iovec *, int);
122115260Smtm
123157457Sdavidxuint	___creat(const char *, mode_t);
124160662Sdavidxuint	___pselect(int, fd_set *, fd_set *, fd_set *,
125160662Sdavidxu		const struct timespec *, const sigset_t *);
126160662Sdavidxuunsigned	___sleep(unsigned);
127160662Sdavidxuint	___system(const char *);
128160662Sdavidxuint	___tcdrain(int);
129160662Sdavidxuint	___usleep(useconds_t useconds);
130160662Sdavidxupid_t	___wait(int *);
131160662Sdavidxupid_t	___waitpid(pid_t, int *, int);
132157457Sdavidxuint	__accept(int, struct sockaddr *, socklen_t *);
133250154Sjillesint	__accept4(int, struct sockaddr *, socklen_t *, int);
134160662Sdavidxuint	__aio_suspend(const struct aiocb * const iocbs[], int,
135160662Sdavidxu		const struct timespec *);
136157457Sdavidxuint	__close(int);
137157457Sdavidxuint	__connect(int, const struct sockaddr *, socklen_t);
138157457Sdavidxuint	__fcntl(int, int,...);
139189549Sdavidxu#ifdef SYSCALL_COMPAT
140179434Sdfrextern int __fcntl_compat(int, int,...);
141189549Sdavidxu#endif
142157457Sdavidxuint	__fsync(int);
143157457Sdavidxuint	__msync(void *, size_t, int);
144157457Sdavidxuint	__nanosleep(const struct timespec *, struct timespec *);
145157457Sdavidxuint	__open(const char *, int,...);
146197968Sjillesint	__openat(int, const char *, int,...);
147157457Sdavidxuint	__poll(struct pollfd *, unsigned int, int);
148157457Sdavidxussize_t	__read(int, void *buf, size_t);
149157457Sdavidxussize_t	__readv(int, const struct iovec *, int);
150157457Sdavidxussize_t	__recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
151157457Sdavidxussize_t	__recvmsg(int, struct msghdr *, int);
152157457Sdavidxuint	__select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
153157457Sdavidxussize_t	__sendmsg(int, const struct msghdr *, int);
154157457Sdavidxussize_t	__sendto(int, const void *, size_t, int,
155157457Sdavidxu		const struct sockaddr *, socklen_t);
156160662Sdavidxupid_t	__wait3(int *, int, struct rusage *);
157157457Sdavidxupid_t	__wait4(pid_t, int *, int, struct rusage *);
158157457Sdavidxussize_t	__write(int, const void *, size_t);
159157457Sdavidxussize_t	__writev(int, const struct iovec *, int);
160157457Sdavidxu
161144518Sdavidxu__weak_reference(__accept, accept);
162157457Sdavidxu
163211524Sdavidxu/*
164211524Sdavidxu * Cancellation behavior:
165211524Sdavidxu *   If thread is canceled, no socket is created.
166211524Sdavidxu */
167135301Smtmint
168144518Sdavidxu__accept(int s, struct sockaddr *addr, socklen_t *addrlen)
169135301Smtm{
170144518Sdavidxu	struct pthread *curthread;
171135301Smtm	int ret;
172135301Smtm
173144518Sdavidxu	curthread = _get_curthread();
174212076Sdavidxu	_thr_cancel_enter(curthread);
175135301Smtm	ret = __sys_accept(s, addr, addrlen);
176212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
177144518Sdavidxu
178144518Sdavidxu 	return (ret);
179135301Smtm}
180135301Smtm
181250154Sjilles__weak_reference(__accept4, accept4);
182250154Sjilles
183250154Sjilles/*
184250154Sjilles * Cancellation behavior:
185250154Sjilles *   If thread is canceled, no socket is created.
186250154Sjilles */
187250154Sjillesint
188250154Sjilles__accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
189250154Sjilles{
190250154Sjilles	struct pthread *curthread;
191250154Sjilles	int ret;
192250154Sjilles
193250154Sjilles	curthread = _get_curthread();
194250154Sjilles	_thr_cancel_enter(curthread);
195250154Sjilles	ret = __sys_accept4(s, addr, addrlen, flags);
196250154Sjilles	_thr_cancel_leave(curthread, ret == -1);
197250154Sjilles
198250154Sjilles 	return (ret);
199250154Sjilles}
200250154Sjilles
201160662Sdavidxu__weak_reference(__aio_suspend, aio_suspend);
202112918Sjeff
203112918Sjeffint
204160662Sdavidxu__aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
205112918Sjeff    timespec *timeout)
206112918Sjeff{
207144518Sdavidxu	struct pthread *curthread = _get_curthread();
208144518Sdavidxu	int ret;
209112918Sjeff
210164583Sdavidxu	_thr_cancel_enter(curthread);
211112918Sjeff	ret = __sys_aio_suspend(iocbs, niocb, timeout);
212212076Sdavidxu	_thr_cancel_leave(curthread, 1);
213112918Sjeff
214144518Sdavidxu	return (ret);
215112918Sjeff}
216112918Sjeff
217144518Sdavidxu__weak_reference(__close, close);
218112918Sjeff
219211524Sdavidxu/*
220211524Sdavidxu * Cancellation behavior:
221211524Sdavidxu *   According to manual of close(), the file descriptor is always deleted.
222211524Sdavidxu *   Here, thread is only canceled after the system call, so the file
223211524Sdavidxu *   descriptor is always deleted despite whether the thread is canceled
224211524Sdavidxu *   or not.
225211524Sdavidxu */
226112918Sjeffint
227144518Sdavidxu__close(int fd)
228112918Sjeff{
229144518Sdavidxu	struct pthread	*curthread = _get_curthread();
230112918Sjeff	int	ret;
231112918Sjeff
232212076Sdavidxu	_thr_cancel_enter2(curthread, 0);
233112918Sjeff	ret = __sys_close(fd);
234212076Sdavidxu	_thr_cancel_leave(curthread, 1);
235112918Sjeff
236144518Sdavidxu	return (ret);
237112918Sjeff}
238135301Smtm
239144518Sdavidxu__weak_reference(__connect, connect);
240135301Smtm
241211524Sdavidxu/*
242211524Sdavidxu * Cancellation behavior:
243211524Sdavidxu *   If the thread is canceled, connection is not made.
244211524Sdavidxu */
245135301Smtmint
246144518Sdavidxu__connect(int fd, const struct sockaddr *name, socklen_t namelen)
247135301Smtm{
248144518Sdavidxu	struct pthread *curthread = _get_curthread();
249135301Smtm	int ret;
250135301Smtm
251212076Sdavidxu	_thr_cancel_enter(curthread);
252144518Sdavidxu	ret = __sys_connect(fd, name, namelen);
253212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
254144518Sdavidxu
255144518Sdavidxu 	return (ret);
256135301Smtm}
257112918Sjeff
258144518Sdavidxu__weak_reference(___creat, creat);
259144518Sdavidxu
260211524Sdavidxu/*
261211524Sdavidxu * Cancellation behavior:
262211524Sdavidxu *   If thread is canceled, file is not created.
263211524Sdavidxu */
264112918Sjeffint
265144518Sdavidxu___creat(const char *path, mode_t mode)
266112918Sjeff{
267144518Sdavidxu	struct pthread *curthread = _get_curthread();
268144518Sdavidxu	int ret;
269112918Sjeff
270212076Sdavidxu	_thr_cancel_enter(curthread);
271112918Sjeff	ret = __creat(path, mode);
272212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
273112918Sjeff
274112918Sjeff	return ret;
275112918Sjeff}
276112918Sjeff
277144518Sdavidxu__weak_reference(__fcntl, fcntl);
278112918Sjeff
279211524Sdavidxu/*
280211524Sdavidxu * Cancellation behavior:
281211524Sdavidxu *   According to specification, only F_SETLKW is a cancellation point.
282211524Sdavidxu *   Thread is only canceled at start, or canceled if the system call
283211524Sdavidxu *   is failure, this means the function does not generate side effect
284211524Sdavidxu *   if it is canceled.
285211524Sdavidxu */
286112918Sjeffint
287144518Sdavidxu__fcntl(int fd, int cmd,...)
288112918Sjeff{
289144518Sdavidxu	struct pthread *curthread = _get_curthread();
290112918Sjeff	int	ret;
291112918Sjeff	va_list	ap;
292211524Sdavidxu
293112918Sjeff	va_start(ap, cmd);
294212076Sdavidxu	if (cmd == F_OSETLKW || cmd == F_SETLKW) {
295212076Sdavidxu		_thr_cancel_enter(curthread);
296211522Sdavidxu#ifdef SYSCALL_COMPAT
297211522Sdavidxu		ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
298211522Sdavidxu#else
299211522Sdavidxu		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
300211522Sdavidxu#endif
301212076Sdavidxu		_thr_cancel_leave(curthread, ret == -1);
302212076Sdavidxu	} else {
303189549Sdavidxu#ifdef SYSCALL_COMPAT
304179434Sdfr		ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
305189549Sdavidxu#else
306189553Sdavidxu		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
307189549Sdavidxu#endif
308112918Sjeff	}
309112918Sjeff	va_end(ap);
310112918Sjeff
311144518Sdavidxu	return (ret);
312112918Sjeff}
313112918Sjeff
314144518Sdavidxu__weak_reference(__fsync, fsync);
315131181Smtm
316211524Sdavidxu/*
317211524Sdavidxu * Cancellation behavior:
318211524Sdavidxu *   Thread may be canceled after system call.
319211524Sdavidxu */
320131181Smtmint
321144518Sdavidxu__fsync(int fd)
322131181Smtm{
323144518Sdavidxu	struct pthread *curthread = _get_curthread();
324131181Smtm	int	ret;
325131181Smtm
326212076Sdavidxu	_thr_cancel_enter2(curthread, 0);
327112918Sjeff	ret = __sys_fsync(fd);
328212076Sdavidxu	_thr_cancel_leave(curthread, 1);
329112918Sjeff
330144518Sdavidxu	return (ret);
331112918Sjeff}
332112918Sjeff
333144518Sdavidxu__weak_reference(__msync, msync);
334135301Smtm
335211524Sdavidxu/*
336211524Sdavidxu * Cancellation behavior:
337211524Sdavidxu *   Thread may be canceled after system call.
338211524Sdavidxu */
339135301Smtmint
340144518Sdavidxu__msync(void *addr, size_t len, int flags)
341135301Smtm{
342144518Sdavidxu	struct pthread *curthread = _get_curthread();
343112918Sjeff	int	ret;
344112918Sjeff
345212076Sdavidxu	_thr_cancel_enter2(curthread, 0);
346112918Sjeff	ret = __sys_msync(addr, len, flags);
347212076Sdavidxu	_thr_cancel_leave(curthread, 1);
348112918Sjeff
349112918Sjeff	return ret;
350112918Sjeff}
351112918Sjeff
352144518Sdavidxu__weak_reference(__nanosleep, nanosleep);
353112918Sjeff
354112918Sjeffint
355144518Sdavidxu__nanosleep(const struct timespec *time_to_sleep,
356144518Sdavidxu    struct timespec *time_remaining)
357112918Sjeff{
358144518Sdavidxu	struct pthread *curthread = _get_curthread();
359144518Sdavidxu	int		ret;
360112918Sjeff
361164583Sdavidxu	_thr_cancel_enter(curthread);
362112918Sjeff	ret = __sys_nanosleep(time_to_sleep, time_remaining);
363212076Sdavidxu	_thr_cancel_leave(curthread, 1);
364112918Sjeff
365144518Sdavidxu	return (ret);
366112918Sjeff}
367112918Sjeff
368144518Sdavidxu__weak_reference(__open, open);
369112918Sjeff
370211524Sdavidxu/*
371211524Sdavidxu * Cancellation behavior:
372211524Sdavidxu *   If the thread is canceled, file is not opened.
373211524Sdavidxu */
374112918Sjeffint
375144518Sdavidxu__open(const char *path, int flags,...)
376112918Sjeff{
377144518Sdavidxu	struct pthread *curthread = _get_curthread();
378112918Sjeff	int	ret;
379112918Sjeff	int	mode = 0;
380112918Sjeff	va_list	ap;
381112918Sjeff
382112918Sjeff	/* Check if the file is being created: */
383112918Sjeff	if (flags & O_CREAT) {
384112918Sjeff		/* Get the creation mode: */
385112918Sjeff		va_start(ap, flags);
386112918Sjeff		mode = va_arg(ap, int);
387112918Sjeff		va_end(ap);
388112918Sjeff	}
389112918Sjeff
390212076Sdavidxu	_thr_cancel_enter(curthread);
391112918Sjeff	ret = __sys_open(path, flags, mode);
392212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
393112918Sjeff
394112918Sjeff	return ret;
395112918Sjeff}
396112918Sjeff
397197968Sjilles__weak_reference(__openat, openat);
398197968Sjilles
399211524Sdavidxu/*
400211524Sdavidxu * Cancellation behavior:
401211524Sdavidxu *   If the thread is canceled, file is not opened.
402211524Sdavidxu */
403197968Sjillesint
404197968Sjilles__openat(int fd, const char *path, int flags, ...)
405197968Sjilles{
406197968Sjilles	struct pthread *curthread = _get_curthread();
407197968Sjilles	int	ret;
408197968Sjilles	int	mode = 0;
409197968Sjilles	va_list	ap;
410197968Sjilles
411197968Sjilles
412197968Sjilles	/* Check if the file is being created: */
413197968Sjilles	if (flags & O_CREAT) {
414197968Sjilles		/* Get the creation mode: */
415197968Sjilles		va_start(ap, flags);
416197968Sjilles		mode = va_arg(ap, int);
417197968Sjilles		va_end(ap);
418197968Sjilles	}
419197968Sjilles
420212076Sdavidxu	_thr_cancel_enter(curthread);
421197968Sjilles	ret = __sys_openat(fd, path, flags, mode);
422212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
423197968Sjilles
424197968Sjilles	return ret;
425197968Sjilles}
426197968Sjilles
427144518Sdavidxu__weak_reference(__poll, poll);
428112918Sjeff
429211524Sdavidxu/*
430211524Sdavidxu * Cancellation behavior:
431211524Sdavidxu *   Thread may be canceled at start, but if the system call returns something,
432211524Sdavidxu *   the thread is not canceled.
433211524Sdavidxu */
434112918Sjeffint
435144518Sdavidxu__poll(struct pollfd *fds, unsigned int nfds, int timeout)
436112918Sjeff{
437144518Sdavidxu	struct pthread *curthread = _get_curthread();
438112918Sjeff	int ret;
439112918Sjeff
440212076Sdavidxu	_thr_cancel_enter(curthread);
441112918Sjeff	ret = __sys_poll(fds, nfds, timeout);
442212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
443112918Sjeff
444112918Sjeff	return ret;
445112918Sjeff}
446112918Sjeff
447160662Sdavidxu__weak_reference(___pselect, pselect);
448135301Smtm
449211524Sdavidxu/*
450211524Sdavidxu * Cancellation behavior:
451211524Sdavidxu *   Thread may be canceled at start, but if the system call returns something,
452211524Sdavidxu *   the thread is not canceled.
453211524Sdavidxu */
454112918Sjeffint
455160662Sdavidxu___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
456112918Sjeff	const struct timespec *timo, const sigset_t *mask)
457112918Sjeff{
458144518Sdavidxu	struct pthread *curthread = _get_curthread();
459112918Sjeff	int ret;
460112918Sjeff
461212076Sdavidxu	_thr_cancel_enter(curthread);
462198508Skib	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
463212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
464112918Sjeff
465112918Sjeff	return (ret);
466112918Sjeff}
467112918Sjeff
468144518Sdavidxu__weak_reference(__read, read);
469112918Sjeff
470211524Sdavidxu/*
471211524Sdavidxu * Cancellation behavior:
472211524Sdavidxu *   Thread may be canceled at start, but if the system call got some data,
473211524Sdavidxu *   the thread is not canceled.
474211524Sdavidxu */
475112918Sjeffssize_t
476144518Sdavidxu__read(int fd, void *buf, size_t nbytes)
477112918Sjeff{
478144518Sdavidxu	struct pthread *curthread = _get_curthread();
479112918Sjeff	ssize_t	ret;
480112918Sjeff
481212076Sdavidxu	_thr_cancel_enter(curthread);
482112918Sjeff	ret = __sys_read(fd, buf, nbytes);
483212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
484112918Sjeff
485112918Sjeff	return ret;
486112918Sjeff}
487112918Sjeff
488144518Sdavidxu__weak_reference(__readv, readv);
489112918Sjeff
490211524Sdavidxu/*
491211524Sdavidxu * Cancellation behavior:
492211524Sdavidxu *   Thread may be canceled at start, but if the system call got some data,
493211524Sdavidxu *   the thread is not canceled.
494211524Sdavidxu */
495112918Sjeffssize_t
496144518Sdavidxu__readv(int fd, const struct iovec *iov, int iovcnt)
497112918Sjeff{
498144518Sdavidxu	struct pthread *curthread = _get_curthread();
499112918Sjeff	ssize_t ret;
500112918Sjeff
501212076Sdavidxu	_thr_cancel_enter(curthread);
502112918Sjeff	ret = __sys_readv(fd, iov, iovcnt);
503212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
504112918Sjeff	return ret;
505112918Sjeff}
506112918Sjeff
507144518Sdavidxu__weak_reference(__recvfrom, recvfrom);
508135301Smtm
509211524Sdavidxu/*
510211524Sdavidxu * Cancellation behavior:
511211524Sdavidxu *   Thread may be canceled at start, but if the system call got some data,
512211524Sdavidxu *   the thread is not canceled.
513211524Sdavidxu */
514135301Smtmssize_t
515144518Sdavidxu__recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
516135301Smtm    socklen_t *fl)
517135301Smtm{
518144518Sdavidxu	struct pthread *curthread = _get_curthread();
519135301Smtm	ssize_t ret;
520135301Smtm
521212076Sdavidxu	_thr_cancel_enter(curthread);
522135301Smtm	ret = __sys_recvfrom(s, b, l, f, from, fl);
523212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
524135301Smtm	return (ret);
525135301Smtm}
526135301Smtm
527144518Sdavidxu__weak_reference(__recvmsg, recvmsg);
528135301Smtm
529211524Sdavidxu/*
530211524Sdavidxu * Cancellation behavior:
531211524Sdavidxu *   Thread may be canceled at start, but if the system call got some data,
532211524Sdavidxu *   the thread is not canceled.
533211524Sdavidxu */
534135301Smtmssize_t
535144518Sdavidxu__recvmsg(int s, struct msghdr *m, int f)
536135301Smtm{
537144518Sdavidxu	struct pthread *curthread = _get_curthread();
538135301Smtm	ssize_t ret;
539135301Smtm
540212076Sdavidxu	_thr_cancel_enter(curthread);
541135301Smtm	ret = __sys_recvmsg(s, m, f);
542212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
543135301Smtm	return (ret);
544135301Smtm}
545135301Smtm
546144518Sdavidxu__weak_reference(__select, select);
547112918Sjeff
548211524Sdavidxu/*
549211524Sdavidxu * Cancellation behavior:
550211524Sdavidxu *   Thread may be canceled at start, but if the system call returns something,
551211524Sdavidxu *   the thread is not canceled.
552211524Sdavidxu */
553112918Sjeffint
554144518Sdavidxu__select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
555112918Sjeff	struct timeval *timeout)
556112918Sjeff{
557144518Sdavidxu	struct pthread *curthread = _get_curthread();
558112918Sjeff	int ret;
559112918Sjeff
560212076Sdavidxu	_thr_cancel_enter(curthread);
561112918Sjeff	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
562212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
563112918Sjeff	return ret;
564112918Sjeff}
565112918Sjeff
566144518Sdavidxu__weak_reference(__sendmsg, sendmsg);
567135301Smtm
568211524Sdavidxu/*
569211524Sdavidxu * Cancellation behavior:
570211524Sdavidxu *   Thread may be canceled at start, but if the system call sent
571211524Sdavidxu *   data, the thread is not canceled.
572211524Sdavidxu */
573135301Smtmssize_t
574144518Sdavidxu__sendmsg(int s, const struct msghdr *m, int f)
575135301Smtm{
576144518Sdavidxu	struct pthread *curthread = _get_curthread();
577135301Smtm	ssize_t ret;
578135301Smtm
579212076Sdavidxu	_thr_cancel_enter(curthread);
580135301Smtm	ret = __sys_sendmsg(s, m, f);
581212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
582135301Smtm	return (ret);
583135301Smtm}
584135301Smtm
585144518Sdavidxu__weak_reference(__sendto, sendto);
586135301Smtm
587211524Sdavidxu/*
588211524Sdavidxu * Cancellation behavior:
589211524Sdavidxu *   Thread may be canceled at start, but if the system call sent some
590211524Sdavidxu *   data, the thread is not canceled.
591211524Sdavidxu */
592135301Smtmssize_t
593144518Sdavidxu__sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
594135301Smtm    socklen_t tl)
595135301Smtm{
596144518Sdavidxu	struct pthread *curthread = _get_curthread();
597135301Smtm	ssize_t ret;
598135301Smtm
599212076Sdavidxu	_thr_cancel_enter(curthread);
600135301Smtm	ret = __sys_sendto(s, m, l, f, t, tl);
601212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
602135301Smtm	return (ret);
603135301Smtm}
604135301Smtm
605160662Sdavidxu__weak_reference(___sleep, sleep);
606148658Sdeischen
607112918Sjeffunsigned int
608160662Sdavidxu___sleep(unsigned int seconds)
609112918Sjeff{
610144518Sdavidxu	struct pthread *curthread = _get_curthread();
611112918Sjeff	unsigned int	ret;
612112918Sjeff
613164583Sdavidxu	_thr_cancel_enter(curthread);
614112918Sjeff	ret = __sleep(seconds);
615212076Sdavidxu	_thr_cancel_leave(curthread, 1);
616112918Sjeff
617144518Sdavidxu	return (ret);
618112918Sjeff}
619112918Sjeff
620160662Sdavidxu__weak_reference(___system, system);
621112918Sjeff
622112918Sjeffint
623160662Sdavidxu___system(const char *string)
624112918Sjeff{
625144518Sdavidxu	struct pthread *curthread = _get_curthread();
626112918Sjeff	int	ret;
627112918Sjeff
628164583Sdavidxu	_thr_cancel_enter(curthread);
629112918Sjeff	ret = __system(string);
630212076Sdavidxu	_thr_cancel_leave(curthread, 1);
631112918Sjeff
632112918Sjeff	return ret;
633112918Sjeff}
634112918Sjeff
635160662Sdavidxu__weak_reference(___tcdrain, tcdrain);
636112918Sjeff
637211524Sdavidxu/*
638211524Sdavidxu * Cancellation behavior:
639211524Sdavidxu *   If thread is canceled, the system call is not completed,
640211524Sdavidxu *   this means not all bytes were drained.
641211524Sdavidxu */
642112918Sjeffint
643160662Sdavidxu___tcdrain(int fd)
644112918Sjeff{
645144518Sdavidxu	struct pthread *curthread = _get_curthread();
646112918Sjeff	int	ret;
647112918Sjeff
648212076Sdavidxu	_thr_cancel_enter(curthread);
649112918Sjeff	ret = __tcdrain(fd);
650212076Sdavidxu	_thr_cancel_leave(curthread, ret == -1);
651144518Sdavidxu	return (ret);
652112918Sjeff}
653112918Sjeff
654160662Sdavidxu__weak_reference(___usleep, usleep);
655148658Sdeischen
656148658Sdeischenint
657160662Sdavidxu___usleep(useconds_t useconds)
658148658Sdeischen{
659148658Sdeischen	struct pthread *curthread = _get_curthread();
660148658Sdeischen	int		ret;
661148658Sdeischen
662164583Sdavidxu	_thr_cancel_enter(curthread);
663148658Sdeischen	ret = __usleep(useconds);
664212076Sdavidxu	_thr_cancel_leave(curthread, 1);
665148658Sdeischen
666148658Sdeischen	return (ret);
667148658Sdeischen}
668148658Sdeischen
669160662Sdavidxu__weak_reference(___wait, wait);
670112918Sjeff
671211524Sdavidxu/*
672211524Sdavidxu * Cancellation behavior:
673211524Sdavidxu *   Thread may be canceled at start, but if the system call returns
674211524Sdavidxu *   a child pid, the thread is not canceled.
675211524Sdavidxu */
676112918Sjeffpid_t
677160662Sdavidxu___wait(int *istat)
678112918Sjeff{
679144518Sdavidxu	struct pthread *curthread = _get_curthread();
680112918Sjeff	pid_t	ret;
681112918Sjeff
682212076Sdavidxu	_thr_cancel_enter(curthread);
683112918Sjeff	ret = __wait(istat);
684212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
685112918Sjeff
686112918Sjeff	return ret;
687112918Sjeff}
688112918Sjeff
689160662Sdavidxu__weak_reference(__wait3, wait3);
690160662Sdavidxu
691211524Sdavidxu/*
692211524Sdavidxu * Cancellation behavior:
693211524Sdavidxu *   Thread may be canceled at start, but if the system call returns
694211524Sdavidxu *   a child pid, the thread is not canceled.
695211524Sdavidxu */
696160662Sdavidxupid_t
697160662Sdavidxu__wait3(int *status, int options, struct rusage *rusage)
698160662Sdavidxu{
699160662Sdavidxu	struct pthread *curthread = _get_curthread();
700160662Sdavidxu	pid_t ret;
701160662Sdavidxu
702212076Sdavidxu	_thr_cancel_enter(curthread);
703160662Sdavidxu	ret = _wait4(WAIT_ANY, status, options, rusage);
704212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
705160662Sdavidxu
706160662Sdavidxu	return (ret);
707160662Sdavidxu}
708160662Sdavidxu
709144518Sdavidxu__weak_reference(__wait4, wait4);
710112918Sjeff
711211524Sdavidxu/*
712211524Sdavidxu * Cancellation behavior:
713211524Sdavidxu *   Thread may be canceled at start, but if the system call returns
714211524Sdavidxu *   a child pid, the thread is not canceled.
715211524Sdavidxu */
716112918Sjeffpid_t
717160662Sdavidxu__wait4(pid_t pid, int *status, int options, struct rusage *rusage)
718112918Sjeff{
719144518Sdavidxu	struct pthread *curthread = _get_curthread();
720112918Sjeff	pid_t ret;
721112918Sjeff
722212076Sdavidxu	_thr_cancel_enter(curthread);
723160662Sdavidxu	ret = __sys_wait4(pid, status, options, rusage);
724212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
725112918Sjeff
726112918Sjeff	return ret;
727112918Sjeff}
728112918Sjeff
729160662Sdavidxu__weak_reference(___waitpid, waitpid);
730112918Sjeff
731211524Sdavidxu/*
732211524Sdavidxu * Cancellation behavior:
733211524Sdavidxu *   Thread may be canceled at start, but if the system call returns
734211524Sdavidxu *   a child pid, the thread is not canceled.
735211524Sdavidxu */
736112918Sjeffpid_t
737160662Sdavidxu___waitpid(pid_t wpid, int *status, int options)
738112918Sjeff{
739144518Sdavidxu	struct pthread *curthread = _get_curthread();
740112918Sjeff	pid_t	ret;
741112918Sjeff
742212076Sdavidxu	_thr_cancel_enter(curthread);
743112918Sjeff	ret = __waitpid(wpid, status, options);
744212076Sdavidxu	_thr_cancel_leave(curthread, ret <= 0);
745112918Sjeff
746112918Sjeff	return ret;
747112918Sjeff}
748112918Sjeff
749144518Sdavidxu__weak_reference(__write, write);
750112918Sjeff
751211524Sdavidxu/*
752211524Sdavidxu * Cancellation behavior:
753211524Sdavidxu *   Thread may be canceled at start, but if the thread wrote some data,
754211524Sdavidxu *   it is not canceled.
755211524Sdavidxu */
756112918Sjeffssize_t
757144518Sdavidxu__write(int fd, const void *buf, size_t nbytes)
758112918Sjeff{
759144518Sdavidxu	struct pthread *curthread = _get_curthread();
760112918Sjeff	ssize_t	ret;
761112918Sjeff
762212076Sdavidxu	_thr_cancel_enter(curthread);
763112918Sjeff	ret = __sys_write(fd, buf, nbytes);
764212076Sdavidxu	_thr_cancel_leave(curthread, (ret <= 0));
765112918Sjeff	return ret;
766112918Sjeff}
767112918Sjeff
768144518Sdavidxu__weak_reference(__writev, writev);
769112918Sjeff
770211524Sdavidxu/*
771211524Sdavidxu * Cancellation behavior:
772211524Sdavidxu *   Thread may be canceled at start, but if the thread wrote some data,
773211524Sdavidxu *   it is not canceled.
774211524Sdavidxu */
775112918Sjeffssize_t
776144518Sdavidxu__writev(int fd, const struct iovec *iov, int iovcnt)
777112918Sjeff{
778144518Sdavidxu	struct pthread *curthread = _get_curthread();
779112918Sjeff	ssize_t ret;
780112918Sjeff
781212076Sdavidxu	_thr_cancel_enter(curthread);
782112918Sjeff	ret = __sys_writev(fd, iov, iovcnt);
783212076Sdavidxu	_thr_cancel_leave(curthread, (ret <= 0));
784112918Sjeff	return ret;
785112918Sjeff}
786