xref: /onnv-gate/usr/src/lib/libc/port/threads/scalls.c (revision 6933:1663308aa184)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51885Sraf  * Common Development and Distribution License (the "License").
61885Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211111Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <poll.h>
330Sstevel@tonic-gate #include <stropts.h>
340Sstevel@tonic-gate #include <dlfcn.h>
355891Sraf #include <wait.h>
365891Sraf #include <sys/socket.h>
370Sstevel@tonic-gate #include <sys/uio.h>
385891Sraf #include <sys/file.h>
395891Sraf #include <sys/door.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
426812Sraf  * These leading-underbar symbols exist because mistakes were made
436812Sraf  * in the past that put them into non-SUNWprivate versions of
446812Sraf  * the libc mapfiles.  They should be eliminated, but oh well...
456812Sraf  */
466812Sraf #pragma weak _fork = fork
476812Sraf #pragma weak _read = read
486812Sraf #pragma weak _write = write
496812Sraf #pragma weak _getmsg = getmsg
506812Sraf #pragma weak _getpmsg = getpmsg
516812Sraf #pragma weak _putmsg = putmsg
526812Sraf #pragma weak _putpmsg = putpmsg
536812Sraf #pragma weak _sleep = sleep
546812Sraf #pragma weak _close = close
556812Sraf #pragma weak _creat = creat
566812Sraf #pragma weak _fcntl = fcntl
576812Sraf #pragma weak _fsync = fsync
586812Sraf #pragma weak _lockf = lockf
596812Sraf #pragma weak _msgrcv = msgrcv
606812Sraf #pragma weak _msgsnd = msgsnd
616812Sraf #pragma weak _msync = msync
626812Sraf #pragma weak _open = open
636812Sraf #pragma weak _openat = openat
646812Sraf #pragma weak _pause = pause
656812Sraf #pragma weak _readv = readv
666812Sraf #pragma weak _sigpause = sigpause
676812Sraf #pragma weak _sigsuspend = sigsuspend
686812Sraf #pragma weak _tcdrain = tcdrain
696812Sraf #pragma weak _waitid = waitid
706812Sraf #pragma weak _writev = writev
716812Sraf 
726812Sraf #if !defined(_LP64)
736812Sraf #pragma weak _creat64 = creat64
746812Sraf #pragma weak _lockf64 = lockf64
756812Sraf #pragma weak _open64 = open64
766812Sraf #pragma weak _openat64 = openat64
776812Sraf #pragma weak _pread64 = pread64
786812Sraf #pragma weak _pwrite64 = pwrite64
796812Sraf #endif
806812Sraf 
816812Sraf /*
82*6933Sraf  * These are SUNWprivate, but they are being used by Sun Studio libcollector.
83*6933Sraf  */
84*6933Sraf #pragma weak _fork1 = fork1
85*6933Sraf #pragma weak _forkall = forkall
86*6933Sraf 
87*6933Sraf /*
885002Sraf  * atfork_lock protects the pthread_atfork() data structures.
895002Sraf  *
904914Sraf  * fork_lock does double-duty.  Not only does it (and atfork_lock)
914914Sraf  * serialize calls to fork() and forkall(), but it also serializes calls
924914Sraf  * to thr_suspend() and thr_continue() (because fork() and forkall() also
934914Sraf  * suspend and continue other threads and they want no competition).
944914Sraf  *
955002Sraf  * Functions called in dlopen()ed L10N objects can do anything, including
965002Sraf  * call malloc() and free().  Such calls are not fork-safe when protected
975002Sraf  * by an ordinary mutex that is acquired in libc's prefork processing
985002Sraf  * because, with an interposed malloc library present, there would be a
995002Sraf  * lock ordering violation due to the pthread_atfork() prefork function
1005002Sraf  * in the interposition library acquiring its malloc lock(s) before the
1014914Sraf  * ordinary mutex in libc being acquired by libc's prefork functions.
1024914Sraf  *
1035002Sraf  * Within libc, calls to malloc() and free() are fork-safe if the calls
1045002Sraf  * are made while holding no other libc locks.  This covers almost all
1055002Sraf  * of libc's malloc() and free() calls.  For those libc code paths, such
1065002Sraf  * as the above-mentioned L10N calls, that require serialization and that
1075002Sraf  * may call malloc() or free(), libc uses callout_lock_enter() to perform
1085002Sraf  * the serialization.  This works because callout_lock is not acquired as
1095002Sraf  * part of running the pthread_atfork() prefork handlers (to avoid the
1105002Sraf  * lock ordering violation described above).  Rather, it is simply
1115002Sraf  * reinitialized in postfork1_child() to cover the case that some
1125002Sraf  * now-defunct thread might have been suspended while holding it.
1130Sstevel@tonic-gate  */
1144914Sraf 
1154843Sraf void
1164843Sraf fork_lock_enter(void)
1170Sstevel@tonic-gate {
1184914Sraf 	ASSERT(curthread->ul_critical == 0);
1196515Sraf 	(void) mutex_lock(&curthread->ul_uberdata->fork_lock);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate void
1230Sstevel@tonic-gate fork_lock_exit(void)
1240Sstevel@tonic-gate {
1254914Sraf 	ASSERT(curthread->ul_critical == 0);
1266515Sraf 	(void) mutex_unlock(&curthread->ul_uberdata->fork_lock);
1274914Sraf }
1280Sstevel@tonic-gate 
1295891Sraf /*
1305891Sraf  * Use cancel_safe_mutex_lock() to protect against being cancelled while
1315891Sraf  * holding callout_lock and calling outside of libc (via L10N plugins).
1325891Sraf  * We will honor a pending cancellation request when callout_lock_exit()
1335891Sraf  * is called, by calling cancel_safe_mutex_unlock().
1345891Sraf  */
1354914Sraf void
1365002Sraf callout_lock_enter(void)
1374914Sraf {
1384914Sraf 	ASSERT(curthread->ul_critical == 0);
1395891Sraf 	cancel_safe_mutex_lock(&curthread->ul_uberdata->callout_lock);
1404914Sraf }
1414914Sraf 
1424914Sraf void
1435002Sraf callout_lock_exit(void)
1444914Sraf {
1454914Sraf 	ASSERT(curthread->ul_critical == 0);
1465891Sraf 	cancel_safe_mutex_unlock(&curthread->ul_uberdata->callout_lock);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1494292Sab196087 pid_t
1506812Sraf forkx(int flags)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	ulwp_t *self = curthread;
1530Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
1540Sstevel@tonic-gate 	pid_t pid;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (self->ul_vfork) {
1570Sstevel@tonic-gate 		/*
1580Sstevel@tonic-gate 		 * We are a child of vfork(); omit all of the fork
1590Sstevel@tonic-gate 		 * logic and go straight to the system call trap.
1600Sstevel@tonic-gate 		 * A vfork() child of a multithreaded parent
1610Sstevel@tonic-gate 		 * must never call fork().
1620Sstevel@tonic-gate 		 */
1630Sstevel@tonic-gate 		if (udp->uberflags.uf_mt) {
1640Sstevel@tonic-gate 			errno = ENOTSUP;
1650Sstevel@tonic-gate 			return (-1);
1660Sstevel@tonic-gate 		}
1673235Sraf 		pid = __forkx(flags);
1680Sstevel@tonic-gate 		if (pid == 0) {		/* child */
1696515Sraf 			udp->pid = getpid();
1700Sstevel@tonic-gate 			self->ul_vfork = 0;
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 		return (pid);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1754843Sraf 	sigoff(self);
1764843Sraf 	if (self->ul_fork) {
1770Sstevel@tonic-gate 		/*
1780Sstevel@tonic-gate 		 * Cannot call fork() from a fork handler.
1790Sstevel@tonic-gate 		 */
1804843Sraf 		sigon(self);
1814843Sraf 		errno = EDEADLK;
1820Sstevel@tonic-gate 		return (-1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	self->ul_fork = 1;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * The functions registered by pthread_atfork() are defined by
1880Sstevel@tonic-gate 	 * the application and its libraries and we must not hold any
1894843Sraf 	 * internal lmutex_lock()-acquired locks while invoking them.
1904843Sraf 	 * We hold only udp->atfork_lock to protect the atfork linkages.
1914843Sraf 	 * If one of these pthread_atfork() functions attempts to fork
1924914Sraf 	 * or to call pthread_atfork(), libc will detect the error and
1934914Sraf 	 * fail the call with EDEADLK.  Otherwise, the pthread_atfork()
1944914Sraf 	 * functions are free to do anything they please (except they
1954914Sraf 	 * will not receive any signals).
1960Sstevel@tonic-gate 	 */
1976515Sraf 	(void) mutex_lock(&udp->atfork_lock);
1980Sstevel@tonic-gate 	_prefork_handler();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2014843Sraf 	 * Block every other thread attempting thr_suspend() or thr_continue().
2024843Sraf 	 */
2036515Sraf 	(void) mutex_lock(&udp->fork_lock);
2044843Sraf 
2054843Sraf 	/*
2060Sstevel@tonic-gate 	 * Block all signals.
2074843Sraf 	 * Just deferring them via sigoff() is not enough.
2080Sstevel@tonic-gate 	 * We have to avoid taking a deferred signal in the child
2093235Sraf 	 * that was actually sent to the parent before __forkx().
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	block_all_signals(self);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * This suspends all threads but this one, leaving them
2150Sstevel@tonic-gate 	 * suspended outside of any critical regions in the library.
2164914Sraf 	 * Thus, we are assured that no lmutex_lock()-acquired library
2174914Sraf 	 * locks are held while we invoke fork() from the current thread.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	suspend_fork();
2200Sstevel@tonic-gate 
2213235Sraf 	pid = __forkx(flags);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (pid == 0) {		/* child */
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * Clear our schedctl pointer.
2260Sstevel@tonic-gate 		 * Discard any deferred signal that was sent to the parent.
2273235Sraf 		 * Because we blocked all signals before __forkx(), a
2280Sstevel@tonic-gate 		 * deferred signal cannot have been taken by the child.
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 		self->ul_schedctl_called = NULL;
2310Sstevel@tonic-gate 		self->ul_schedctl = NULL;
2320Sstevel@tonic-gate 		self->ul_cursig = 0;
2330Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
2346515Sraf 		udp->pid = getpid();
2350Sstevel@tonic-gate 		/* reset the library's data structures to reflect one thread */
2364574Sraf 		unregister_locks();
2372248Sraf 		postfork1_child();
2380Sstevel@tonic-gate 		restore_signals(self);
2396515Sraf 		(void) mutex_unlock(&udp->fork_lock);
2400Sstevel@tonic-gate 		_postfork_child_handler();
2410Sstevel@tonic-gate 	} else {
2423235Sraf 		/* restart all threads that were suspended for fork() */
2430Sstevel@tonic-gate 		continue_fork(0);
2440Sstevel@tonic-gate 		restore_signals(self);
2456515Sraf 		(void) mutex_unlock(&udp->fork_lock);
2460Sstevel@tonic-gate 		_postfork_parent_handler();
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2496515Sraf 	(void) mutex_unlock(&udp->atfork_lock);
2500Sstevel@tonic-gate 	self->ul_fork = 0;
2514843Sraf 	sigon(self);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	return (pid);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2573235Sraf  * fork() is fork1() for both Posix threads and Solaris threads.
2583235Sraf  * The forkall() interface exists for applications that require
2593235Sraf  * the semantics of replicating all threads.
2600Sstevel@tonic-gate  */
2616812Sraf #pragma weak fork1 = fork
2620Sstevel@tonic-gate pid_t
2636812Sraf fork(void)
2643235Sraf {
2656812Sraf 	return (forkx(0));
2663235Sraf }
2673235Sraf 
2683235Sraf /*
2693235Sraf  * Much of the logic here is the same as in forkx().
2703235Sraf  * See the comments in forkx(), above.
2713235Sraf  */
2724292Sab196087 pid_t
2736812Sraf forkallx(int flags)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	ulwp_t *self = curthread;
2760Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
2770Sstevel@tonic-gate 	pid_t pid;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (self->ul_vfork) {
2800Sstevel@tonic-gate 		if (udp->uberflags.uf_mt) {
2810Sstevel@tonic-gate 			errno = ENOTSUP;
2820Sstevel@tonic-gate 			return (-1);
2830Sstevel@tonic-gate 		}
2843235Sraf 		pid = __forkallx(flags);
2850Sstevel@tonic-gate 		if (pid == 0) {		/* child */
2866515Sraf 			udp->pid = getpid();
2870Sstevel@tonic-gate 			self->ul_vfork = 0;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		return (pid);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2924843Sraf 	sigoff(self);
2934843Sraf 	if (self->ul_fork) {
2944843Sraf 		sigon(self);
2954843Sraf 		errno = EDEADLK;
2960Sstevel@tonic-gate 		return (-1);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 	self->ul_fork = 1;
2996515Sraf 	(void) mutex_lock(&udp->atfork_lock);
3006515Sraf 	(void) mutex_lock(&udp->fork_lock);
3010Sstevel@tonic-gate 	block_all_signals(self);
3020Sstevel@tonic-gate 	suspend_fork();
3030Sstevel@tonic-gate 
3043235Sraf 	pid = __forkallx(flags);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (pid == 0) {
3070Sstevel@tonic-gate 		self->ul_schedctl_called = NULL;
3080Sstevel@tonic-gate 		self->ul_schedctl = NULL;
3090Sstevel@tonic-gate 		self->ul_cursig = 0;
3100Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
3116515Sraf 		udp->pid = getpid();
3124574Sraf 		unregister_locks();
3130Sstevel@tonic-gate 		continue_fork(1);
3140Sstevel@tonic-gate 	} else {
3150Sstevel@tonic-gate 		continue_fork(0);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	restore_signals(self);
3186515Sraf 	(void) mutex_unlock(&udp->fork_lock);
3196515Sraf 	(void) mutex_unlock(&udp->atfork_lock);
3200Sstevel@tonic-gate 	self->ul_fork = 0;
3214843Sraf 	sigon(self);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return (pid);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3263235Sraf pid_t
3276812Sraf forkall(void)
3283235Sraf {
3296812Sraf 	return (forkallx(0));
3303235Sraf }
3313235Sraf 
3320Sstevel@tonic-gate /*
3335891Sraf  * For the implementation of cancellation at cancellation points.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate #define	PROLOGUE							\
3360Sstevel@tonic-gate {									\
3370Sstevel@tonic-gate 	ulwp_t *self = curthread;					\
3385891Sraf 	int nocancel =							\
3395891Sraf 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |	\
3405891Sraf 	    self->ul_critical | self->ul_sigdefer);			\
3415891Sraf 	int abort = 0;							\
3420Sstevel@tonic-gate 	if (nocancel == 0) {						\
3430Sstevel@tonic-gate 		self->ul_save_async = self->ul_cancel_async;		\
3440Sstevel@tonic-gate 		if (!self->ul_cancel_disabled) {			\
3450Sstevel@tonic-gate 			self->ul_cancel_async = 1;			\
3460Sstevel@tonic-gate 			if (self->ul_cancel_pending)			\
3476812Sraf 				pthread_exit(PTHREAD_CANCELED);		\
3480Sstevel@tonic-gate 		}							\
3490Sstevel@tonic-gate 		self->ul_sp = stkptr();					\
3505891Sraf 	} else if (self->ul_cancel_pending &&				\
3515891Sraf 	    !self->ul_cancel_disabled) {				\
3525891Sraf 		set_cancel_eintr_flag(self);				\
3535891Sraf 		abort = 1;						\
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate #define	EPILOGUE							\
3570Sstevel@tonic-gate 	if (nocancel == 0) {						\
3580Sstevel@tonic-gate 		self->ul_sp = 0;					\
3590Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;		\
3600Sstevel@tonic-gate 	}								\
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * Perform the body of the action required by most of the cancelable
3650Sstevel@tonic-gate  * function calls.  The return(function_call) part is to allow the
3660Sstevel@tonic-gate  * compiler to make the call be executed with tail recursion, which
3670Sstevel@tonic-gate  * saves a register window on sparc and slightly (not much) improves
3680Sstevel@tonic-gate  * the code for x86/x64 compilations.
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate #define	PERFORM(function_call)						\
3710Sstevel@tonic-gate 	PROLOGUE							\
3725891Sraf 	if (abort) {							\
3735891Sraf 		*self->ul_errnop = EINTR;				\
3745891Sraf 		return (-1);						\
3755891Sraf 	}								\
3760Sstevel@tonic-gate 	if (nocancel)							\
3770Sstevel@tonic-gate 		return (function_call);					\
3780Sstevel@tonic-gate 	rv = function_call;						\
3790Sstevel@tonic-gate 	EPILOGUE							\
3800Sstevel@tonic-gate 	return (rv);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * Specialized prologue for sigsuspend() and pollsys().
3840Sstevel@tonic-gate  * These system calls pass a signal mask to the kernel.
3850Sstevel@tonic-gate  * The kernel replaces the thread's signal mask with the
3860Sstevel@tonic-gate  * temporary mask before the thread goes to sleep.  If
3870Sstevel@tonic-gate  * a signal is received, the signal handler will execute
3880Sstevel@tonic-gate  * with the temporary mask, as modified by the sigaction
3890Sstevel@tonic-gate  * for the particular signal.
3900Sstevel@tonic-gate  *
3910Sstevel@tonic-gate  * We block all signals until we reach the kernel with the
3920Sstevel@tonic-gate  * temporary mask.  This eliminates race conditions with
3930Sstevel@tonic-gate  * setting the signal mask while signals are being posted.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate #define	PROLOGUE_MASK(sigmask)						\
3960Sstevel@tonic-gate {									\
3970Sstevel@tonic-gate 	ulwp_t *self = curthread;					\
3985891Sraf 	int nocancel =							\
3995891Sraf 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |	\
4005891Sraf 	    self->ul_critical | self->ul_sigdefer);			\
4010Sstevel@tonic-gate 	if (!self->ul_vfork) {						\
4020Sstevel@tonic-gate 		if (sigmask) {						\
4030Sstevel@tonic-gate 			block_all_signals(self);			\
4041111Sraf 			self->ul_tmpmask.__sigbits[0] = sigmask->__sigbits[0]; \
4051111Sraf 			self->ul_tmpmask.__sigbits[1] = sigmask->__sigbits[1]; \
4060Sstevel@tonic-gate 			delete_reserved_signals(&self->ul_tmpmask);	\
4070Sstevel@tonic-gate 			self->ul_sigsuspend = 1;			\
4080Sstevel@tonic-gate 		}							\
4090Sstevel@tonic-gate 		if (nocancel == 0) {					\
4100Sstevel@tonic-gate 			self->ul_save_async = self->ul_cancel_async;	\
4110Sstevel@tonic-gate 			if (!self->ul_cancel_disabled) {		\
4120Sstevel@tonic-gate 				self->ul_cancel_async = 1;		\
4130Sstevel@tonic-gate 				if (self->ul_cancel_pending) {		\
4140Sstevel@tonic-gate 					if (self->ul_sigsuspend) {	\
4150Sstevel@tonic-gate 						self->ul_sigsuspend = 0;\
4160Sstevel@tonic-gate 						restore_signals(self);	\
4170Sstevel@tonic-gate 					}				\
4186812Sraf 					pthread_exit(PTHREAD_CANCELED);	\
4190Sstevel@tonic-gate 				}					\
4200Sstevel@tonic-gate 			}						\
4210Sstevel@tonic-gate 			self->ul_sp = stkptr();				\
4220Sstevel@tonic-gate 		}							\
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * If a signal is taken, we return from the system call wrapper with
4270Sstevel@tonic-gate  * our original signal mask restored (see code in call_user_handler()).
4280Sstevel@tonic-gate  * If not (self->ul_sigsuspend is still non-zero), we must restore our
4290Sstevel@tonic-gate  * original signal mask ourself.
4300Sstevel@tonic-gate  */
4310Sstevel@tonic-gate #define	EPILOGUE_MASK							\
4320Sstevel@tonic-gate 	if (nocancel == 0) {						\
4330Sstevel@tonic-gate 		self->ul_sp = 0;					\
4340Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;		\
4350Sstevel@tonic-gate 	}								\
4360Sstevel@tonic-gate 	if (self->ul_sigsuspend) {					\
4370Sstevel@tonic-gate 		self->ul_sigsuspend = 0;				\
4380Sstevel@tonic-gate 		restore_signals(self);					\
4390Sstevel@tonic-gate 	}								\
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4432248Sraf  * Cancellation prologue and epilogue functions,
4442248Sraf  * for cancellation points too complex to include here.
4451885Sraf  */
4461885Sraf void
4471885Sraf _cancel_prologue(void)
4481885Sraf {
4491885Sraf 	ulwp_t *self = curthread;
4501885Sraf 
4515891Sraf 	self->ul_cancel_prologue =
4525891Sraf 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |
4535891Sraf 	    self->ul_critical | self->ul_sigdefer) != 0;
4541885Sraf 	if (self->ul_cancel_prologue == 0) {
4551885Sraf 		self->ul_save_async = self->ul_cancel_async;
4561885Sraf 		if (!self->ul_cancel_disabled) {
4571885Sraf 			self->ul_cancel_async = 1;
4581885Sraf 			if (self->ul_cancel_pending)
4596812Sraf 				pthread_exit(PTHREAD_CANCELED);
4601885Sraf 		}
4611885Sraf 		self->ul_sp = stkptr();
4625891Sraf 	} else if (self->ul_cancel_pending &&
4635891Sraf 	    !self->ul_cancel_disabled) {
4645891Sraf 		set_cancel_eintr_flag(self);
4651885Sraf 	}
4661885Sraf }
4671885Sraf 
4681885Sraf void
4691885Sraf _cancel_epilogue(void)
4701885Sraf {
4711885Sraf 	ulwp_t *self = curthread;
4721885Sraf 
4731885Sraf 	if (self->ul_cancel_prologue == 0) {
4741885Sraf 		self->ul_sp = 0;
4751885Sraf 		self->ul_cancel_async = self->ul_save_async;
4761885Sraf 	}
4771885Sraf }
4781885Sraf 
4791885Sraf /*
4800Sstevel@tonic-gate  * Called from _thrp_join() (thr_join() is a cancellation point)
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate int
4830Sstevel@tonic-gate lwp_wait(thread_t tid, thread_t *found)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	int error;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	PROLOGUE
4885891Sraf 	if (abort)
4895891Sraf 		return (EINTR);
4905891Sraf 	while ((error = __lwp_wait(tid, found)) == EINTR && !cancel_active())
4915891Sraf 		continue;
4920Sstevel@tonic-gate 	EPILOGUE
4930Sstevel@tonic-gate 	return (error);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate ssize_t
4976812Sraf read(int fd, void *buf, size_t size)
4980Sstevel@tonic-gate {
4995891Sraf 	extern ssize_t __read(int, void *, size_t);
5000Sstevel@tonic-gate 	ssize_t rv;
5010Sstevel@tonic-gate 
5025891Sraf 	PERFORM(__read(fd, buf, size))
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate ssize_t
5066812Sraf write(int fd, const void *buf, size_t size)
5070Sstevel@tonic-gate {
5085891Sraf 	extern ssize_t __write(int, const void *, size_t);
5090Sstevel@tonic-gate 	ssize_t rv;
5100Sstevel@tonic-gate 
5115891Sraf 	PERFORM(__write(fd, buf, size))
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate int
5156812Sraf getmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr,
5160Sstevel@tonic-gate 	int *flagsp)
5170Sstevel@tonic-gate {
5185891Sraf 	extern int __getmsg(int, struct strbuf *, struct strbuf *, int *);
5190Sstevel@tonic-gate 	int rv;
5200Sstevel@tonic-gate 
5215891Sraf 	PERFORM(__getmsg(fd, ctlptr, dataptr, flagsp))
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate int
5256812Sraf getpmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr,
5260Sstevel@tonic-gate 	int *bandp, int *flagsp)
5270Sstevel@tonic-gate {
5285891Sraf 	extern int __getpmsg(int, struct strbuf *, struct strbuf *,
5294843Sraf 	    int *, int *);
5300Sstevel@tonic-gate 	int rv;
5310Sstevel@tonic-gate 
5325891Sraf 	PERFORM(__getpmsg(fd, ctlptr, dataptr, bandp, flagsp))
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate int
5366812Sraf putmsg(int fd, const struct strbuf *ctlptr,
5370Sstevel@tonic-gate 	const struct strbuf *dataptr, int flags)
5380Sstevel@tonic-gate {
5395891Sraf 	extern int __putmsg(int, const struct strbuf *,
5404843Sraf 	    const struct strbuf *, int);
5410Sstevel@tonic-gate 	int rv;
5420Sstevel@tonic-gate 
5435891Sraf 	PERFORM(__putmsg(fd, ctlptr, dataptr, flags))
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate int
5470Sstevel@tonic-gate __xpg4_putmsg(int fd, const struct strbuf *ctlptr,
5480Sstevel@tonic-gate 	const struct strbuf *dataptr, int flags)
5490Sstevel@tonic-gate {
5505891Sraf 	extern int __putmsg(int, const struct strbuf *,
5514843Sraf 	    const struct strbuf *, int);
5520Sstevel@tonic-gate 	int rv;
5530Sstevel@tonic-gate 
5545891Sraf 	PERFORM(__putmsg(fd, ctlptr, dataptr, flags|MSG_XPG4))
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate int
5586812Sraf putpmsg(int fd, const struct strbuf *ctlptr,
5590Sstevel@tonic-gate 	const struct strbuf *dataptr, int band, int flags)
5600Sstevel@tonic-gate {
5615891Sraf 	extern int __putpmsg(int, const struct strbuf *,
5624843Sraf 	    const struct strbuf *, int, int);
5630Sstevel@tonic-gate 	int rv;
5640Sstevel@tonic-gate 
5655891Sraf 	PERFORM(__putpmsg(fd, ctlptr, dataptr, band, flags))
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate int
5690Sstevel@tonic-gate __xpg4_putpmsg(int fd, const struct strbuf *ctlptr,
5700Sstevel@tonic-gate 	const struct strbuf *dataptr, int band, int flags)
5710Sstevel@tonic-gate {
5725891Sraf 	extern int __putpmsg(int, const struct strbuf *,
5734843Sraf 	    const struct strbuf *, int, int);
5740Sstevel@tonic-gate 	int rv;
5750Sstevel@tonic-gate 
5765891Sraf 	PERFORM(__putpmsg(fd, ctlptr, dataptr, band, flags|MSG_XPG4))
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate int
5806812Sraf nanosleep(const timespec_t *rqtp, timespec_t *rmtp)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	int error;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	PROLOGUE
5855891Sraf 	error = abort? EINTR : __nanosleep(rqtp, rmtp);
5860Sstevel@tonic-gate 	EPILOGUE
5870Sstevel@tonic-gate 	if (error) {
5880Sstevel@tonic-gate 		errno = error;
5890Sstevel@tonic-gate 		return (-1);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	return (0);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate int
5956812Sraf clock_nanosleep(clockid_t clock_id, int flags,
5960Sstevel@tonic-gate 	const timespec_t *rqtp, timespec_t *rmtp)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	timespec_t reltime;
5990Sstevel@tonic-gate 	hrtime_t start;
6000Sstevel@tonic-gate 	hrtime_t rqlapse;
6010Sstevel@tonic-gate 	hrtime_t lapse;
6020Sstevel@tonic-gate 	int error;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	switch (clock_id) {
6050Sstevel@tonic-gate 	case CLOCK_VIRTUAL:
6060Sstevel@tonic-gate 	case CLOCK_PROCESS_CPUTIME_ID:
6070Sstevel@tonic-gate 	case CLOCK_THREAD_CPUTIME_ID:
6080Sstevel@tonic-gate 		return (ENOTSUP);
6090Sstevel@tonic-gate 	case CLOCK_REALTIME:
6100Sstevel@tonic-gate 	case CLOCK_HIGHRES:
6110Sstevel@tonic-gate 		break;
6120Sstevel@tonic-gate 	default:
6130Sstevel@tonic-gate 		return (EINVAL);
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 	if (flags & TIMER_ABSTIME) {
6160Sstevel@tonic-gate 		abstime_to_reltime(clock_id, rqtp, &reltime);
6170Sstevel@tonic-gate 		rmtp = NULL;
6180Sstevel@tonic-gate 	} else {
6190Sstevel@tonic-gate 		reltime = *rqtp;
6200Sstevel@tonic-gate 		if (clock_id == CLOCK_HIGHRES)
6210Sstevel@tonic-gate 			start = gethrtime();
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate restart:
6240Sstevel@tonic-gate 	PROLOGUE
6255891Sraf 	error = abort? EINTR : __nanosleep(&reltime, rmtp);
6260Sstevel@tonic-gate 	EPILOGUE
6270Sstevel@tonic-gate 	if (error == 0 && clock_id == CLOCK_HIGHRES) {
6280Sstevel@tonic-gate 		/*
6290Sstevel@tonic-gate 		 * Don't return yet if we didn't really get a timeout.
6300Sstevel@tonic-gate 		 * This can happen if we return because someone resets
6310Sstevel@tonic-gate 		 * the system clock.
6320Sstevel@tonic-gate 		 */
6330Sstevel@tonic-gate 		if (flags & TIMER_ABSTIME) {
6340Sstevel@tonic-gate 			if ((hrtime_t)(uint32_t)rqtp->tv_sec * NANOSEC +
6350Sstevel@tonic-gate 			    rqtp->tv_nsec > gethrtime()) {
6360Sstevel@tonic-gate 				abstime_to_reltime(clock_id, rqtp, &reltime);
6370Sstevel@tonic-gate 				goto restart;
6380Sstevel@tonic-gate 			}
6390Sstevel@tonic-gate 		} else {
6400Sstevel@tonic-gate 			rqlapse = (hrtime_t)(uint32_t)rqtp->tv_sec * NANOSEC +
6414843Sraf 			    rqtp->tv_nsec;
6420Sstevel@tonic-gate 			lapse = gethrtime() - start;
6430Sstevel@tonic-gate 			if (rqlapse > lapse) {
6440Sstevel@tonic-gate 				hrt2ts(rqlapse - lapse, &reltime);
6450Sstevel@tonic-gate 				goto restart;
6460Sstevel@tonic-gate 			}
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 	if (error == 0 && clock_id == CLOCK_REALTIME &&
6500Sstevel@tonic-gate 	    (flags & TIMER_ABSTIME)) {
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * Don't return yet just because someone reset the
6530Sstevel@tonic-gate 		 * system clock.  Recompute the new relative time
6540Sstevel@tonic-gate 		 * and reissue the nanosleep() call if necessary.
6550Sstevel@tonic-gate 		 *
6560Sstevel@tonic-gate 		 * Resetting the system clock causes all sorts of
6570Sstevel@tonic-gate 		 * problems and the SUSV3 standards body should
6580Sstevel@tonic-gate 		 * have made the behavior of clock_nanosleep() be
6590Sstevel@tonic-gate 		 * implementation-defined in such a case rather than
6600Sstevel@tonic-gate 		 * being specific about honoring the new system time.
6610Sstevel@tonic-gate 		 * Standards bodies are filled with fools and idiots.
6620Sstevel@tonic-gate 		 */
6630Sstevel@tonic-gate 		abstime_to_reltime(clock_id, rqtp, &reltime);
6640Sstevel@tonic-gate 		if (reltime.tv_sec != 0 || reltime.tv_nsec != 0)
6650Sstevel@tonic-gate 			goto restart;
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	return (error);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate unsigned int
6716812Sraf sleep(unsigned int sec)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	unsigned int rem = 0;
6740Sstevel@tonic-gate 	timespec_t ts;
6750Sstevel@tonic-gate 	timespec_t tsr;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	ts.tv_sec = (time_t)sec;
6780Sstevel@tonic-gate 	ts.tv_nsec = 0;
6796812Sraf 	if (nanosleep(&ts, &tsr) == -1 && errno == EINTR) {
6800Sstevel@tonic-gate 		rem = (unsigned int)tsr.tv_sec;
6810Sstevel@tonic-gate 		if (tsr.tv_nsec >= NANOSEC / 2)
6820Sstevel@tonic-gate 			rem++;
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 	return (rem);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate int
6886812Sraf usleep(useconds_t usec)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	timespec_t ts;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	ts.tv_sec = usec / MICROSEC;
6930Sstevel@tonic-gate 	ts.tv_nsec = (long)(usec % MICROSEC) * 1000;
6946812Sraf 	(void) nanosleep(&ts, NULL);
6950Sstevel@tonic-gate 	return (0);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate int
6996812Sraf close(int fildes)
7000Sstevel@tonic-gate {
7012248Sraf 	extern void _aio_close(int);
7025891Sraf 	extern int __close(int);
7030Sstevel@tonic-gate 	int rv;
7040Sstevel@tonic-gate 
7056755Sraf 	/*
7066755Sraf 	 * If we call _aio_close() while in a critical region,
7076755Sraf 	 * we will draw an ASSERT() failure, so don't do it.
7086755Sraf 	 * No calls to close() from within libc need _aio_close();
7096755Sraf 	 * only the application's calls to close() need this,
7106755Sraf 	 * and such calls are never from a libc critical region.
7116755Sraf 	 */
7126755Sraf 	if (curthread->ul_critical == 0)
7136755Sraf 		_aio_close(fildes);
7145891Sraf 	PERFORM(__close(fildes))
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate int
7186812Sraf creat(const char *path, mode_t mode)
7190Sstevel@tonic-gate {
7205891Sraf 	extern int __creat(const char *, mode_t);
7210Sstevel@tonic-gate 	int rv;
7220Sstevel@tonic-gate 
7235891Sraf 	PERFORM(__creat(path, mode))
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate #if !defined(_LP64)
7270Sstevel@tonic-gate int
7286812Sraf creat64(const char *path, mode_t mode)
7290Sstevel@tonic-gate {
7305891Sraf 	extern int __creat64(const char *, mode_t);
7310Sstevel@tonic-gate 	int rv;
7320Sstevel@tonic-gate 
7335891Sraf 	PERFORM(__creat64(path, mode))
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate #endif	/* !_LP64 */
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate int
7386812Sraf door_call(int d, door_arg_t *params)
7390Sstevel@tonic-gate {
7405891Sraf 	extern int __door_call(int, door_arg_t *);
7415891Sraf 	int rv;
7425891Sraf 
7435891Sraf 	PERFORM(__door_call(d, params))
7445891Sraf }
7455891Sraf 
7465891Sraf int
7476812Sraf fcntl(int fildes, int cmd, ...)
7485891Sraf {
7495891Sraf 	extern int __fcntl(int, int, ...);
7500Sstevel@tonic-gate 	intptr_t arg;
7510Sstevel@tonic-gate 	int rv;
7520Sstevel@tonic-gate 	va_list ap;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	va_start(ap, cmd);
7550Sstevel@tonic-gate 	arg = va_arg(ap, intptr_t);
7560Sstevel@tonic-gate 	va_end(ap);
7570Sstevel@tonic-gate 	if (cmd != F_SETLKW)
7585891Sraf 		return (__fcntl(fildes, cmd, arg));
7595891Sraf 	PERFORM(__fcntl(fildes, cmd, arg))
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate int
7636812Sraf fdatasync(int fildes)
7644722Sraf {
7655891Sraf 	extern int __fdsync(int, int);
7664722Sraf 	int rv;
7674722Sraf 
7685891Sraf 	PERFORM(__fdsync(fildes, FDSYNC))
7694722Sraf }
7704722Sraf 
7714722Sraf int
7726812Sraf fsync(int fildes)
7730Sstevel@tonic-gate {
7745891Sraf 	extern int __fdsync(int, int);
7750Sstevel@tonic-gate 	int rv;
7760Sstevel@tonic-gate 
7775891Sraf 	PERFORM(__fdsync(fildes, FSYNC))
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate int
7816812Sraf lockf(int fildes, int function, off_t size)
7820Sstevel@tonic-gate {
7835891Sraf 	extern int __lockf(int, int, off_t);
7840Sstevel@tonic-gate 	int rv;
7850Sstevel@tonic-gate 
7865891Sraf 	PERFORM(__lockf(fildes, function, size))
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate #if !defined(_LP64)
7900Sstevel@tonic-gate int
7916812Sraf lockf64(int fildes, int function, off64_t size)
7920Sstevel@tonic-gate {
7935891Sraf 	extern int __lockf64(int, int, off64_t);
7940Sstevel@tonic-gate 	int rv;
7950Sstevel@tonic-gate 
7965891Sraf 	PERFORM(__lockf64(fildes, function, size))
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate #endif	/* !_LP64 */
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate ssize_t
8016812Sraf msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
8020Sstevel@tonic-gate {
8035891Sraf 	extern ssize_t __msgrcv(int, void *, size_t, long, int);
8040Sstevel@tonic-gate 	ssize_t rv;
8050Sstevel@tonic-gate 
8065891Sraf 	PERFORM(__msgrcv(msqid, msgp, msgsz, msgtyp, msgflg))
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate int
8106812Sraf msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
8110Sstevel@tonic-gate {
8125891Sraf 	extern int __msgsnd(int, const void *, size_t, int);
8130Sstevel@tonic-gate 	int rv;
8140Sstevel@tonic-gate 
8155891Sraf 	PERFORM(__msgsnd(msqid, msgp, msgsz, msgflg))
8165891Sraf }
8175891Sraf 
8185891Sraf int
8196812Sraf msync(caddr_t addr, size_t len, int flags)
8205891Sraf {
8215891Sraf 	extern int __msync(caddr_t, size_t, int);
8225891Sraf 	int rv;
8235891Sraf 
8245891Sraf 	PERFORM(__msync(addr, len, flags))
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate int
8286812Sraf open(const char *path, int oflag, ...)
8290Sstevel@tonic-gate {
8305891Sraf 	extern int __open(const char *, int, ...);
8315891Sraf 	mode_t mode;
8320Sstevel@tonic-gate 	int rv;
8335891Sraf 	va_list ap;
8340Sstevel@tonic-gate 
8355891Sraf 	va_start(ap, oflag);
8365891Sraf 	mode = va_arg(ap, mode_t);
8375891Sraf 	va_end(ap);
8385891Sraf 	PERFORM(__open(path, oflag, mode))
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate int
8426812Sraf openat(int fd, const char *path, int oflag, ...)
8430Sstevel@tonic-gate {
8445891Sraf 	extern int __openat(int, const char *, int, ...);
8450Sstevel@tonic-gate 	mode_t mode;
8460Sstevel@tonic-gate 	int rv;
8470Sstevel@tonic-gate 	va_list ap;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	va_start(ap, oflag);
8500Sstevel@tonic-gate 	mode = va_arg(ap, mode_t);
8510Sstevel@tonic-gate 	va_end(ap);
8525891Sraf 	PERFORM(__openat(fd, path, oflag, mode))
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate #if !defined(_LP64)
8560Sstevel@tonic-gate int
8576812Sraf open64(const char *path, int oflag, ...)
8580Sstevel@tonic-gate {
8595891Sraf 	extern int __open64(const char *, int, ...);
8605891Sraf 	mode_t mode;
8615891Sraf 	int rv;
8625891Sraf 	va_list ap;
8635891Sraf 
8645891Sraf 	va_start(ap, oflag);
8655891Sraf 	mode = va_arg(ap, mode_t);
8665891Sraf 	va_end(ap);
8675891Sraf 	PERFORM(__open64(path, oflag, mode))
8685891Sraf }
8695891Sraf 
8705891Sraf int
8716812Sraf openat64(int fd, const char *path, int oflag, ...)
8725891Sraf {
8735891Sraf 	extern int __openat64(int, const char *, int, ...);
8740Sstevel@tonic-gate 	mode_t mode;
8750Sstevel@tonic-gate 	int rv;
8760Sstevel@tonic-gate 	va_list ap;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	va_start(ap, oflag);
8790Sstevel@tonic-gate 	mode = va_arg(ap, mode_t);
8800Sstevel@tonic-gate 	va_end(ap);
8815891Sraf 	PERFORM(__openat64(fd, path, oflag, mode))
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate #endif	/* !_LP64 */
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate int
8866812Sraf pause(void)
8870Sstevel@tonic-gate {
8885891Sraf 	extern int __pause(void);
8890Sstevel@tonic-gate 	int rv;
8900Sstevel@tonic-gate 
8915891Sraf 	PERFORM(__pause())
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate ssize_t
8956812Sraf pread(int fildes, void *buf, size_t nbyte, off_t offset)
8960Sstevel@tonic-gate {
8975891Sraf 	extern ssize_t __pread(int, void *, size_t, off_t);
8980Sstevel@tonic-gate 	ssize_t rv;
8990Sstevel@tonic-gate 
9005891Sraf 	PERFORM(__pread(fildes, buf, nbyte, offset))
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate #if !defined(_LP64)
9040Sstevel@tonic-gate ssize_t
9056812Sraf pread64(int fildes, void *buf, size_t nbyte, off64_t offset)
9060Sstevel@tonic-gate {
9075891Sraf 	extern ssize_t __pread64(int, void *, size_t, off64_t);
9080Sstevel@tonic-gate 	ssize_t rv;
9090Sstevel@tonic-gate 
9105891Sraf 	PERFORM(__pread64(fildes, buf, nbyte, offset))
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate #endif	/* !_LP64 */
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate ssize_t
9156812Sraf pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
9160Sstevel@tonic-gate {
9175891Sraf 	extern ssize_t __pwrite(int, const void *, size_t, off_t);
9180Sstevel@tonic-gate 	ssize_t rv;
9190Sstevel@tonic-gate 
9205891Sraf 	PERFORM(__pwrite(fildes, buf, nbyte, offset))
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate #if !defined(_LP64)
9240Sstevel@tonic-gate ssize_t
9256812Sraf pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset)
9260Sstevel@tonic-gate {
9275891Sraf 	extern ssize_t __pwrite64(int, const void *, size_t, off64_t);
9280Sstevel@tonic-gate 	ssize_t rv;
9290Sstevel@tonic-gate 
9305891Sraf 	PERFORM(__pwrite64(fildes, buf, nbyte, offset))
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate #endif	/* !_LP64 */
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate ssize_t
9356812Sraf readv(int fildes, const struct iovec *iov, int iovcnt)
9360Sstevel@tonic-gate {
9375891Sraf 	extern ssize_t __readv(int, const struct iovec *, int);
9380Sstevel@tonic-gate 	ssize_t rv;
9390Sstevel@tonic-gate 
9405891Sraf 	PERFORM(__readv(fildes, iov, iovcnt))
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate int
9446812Sraf sigpause(int sig)
9450Sstevel@tonic-gate {
9465891Sraf 	extern int __sigpause(int);
9470Sstevel@tonic-gate 	int rv;
9480Sstevel@tonic-gate 
9495891Sraf 	PERFORM(__sigpause(sig))
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate int
9536812Sraf sigsuspend(const sigset_t *set)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	extern int __sigsuspend(const sigset_t *);
9560Sstevel@tonic-gate 	int rv;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	PROLOGUE_MASK(set)
9590Sstevel@tonic-gate 	rv = __sigsuspend(set);
9600Sstevel@tonic-gate 	EPILOGUE_MASK
9610Sstevel@tonic-gate 	return (rv);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate int
9650Sstevel@tonic-gate _pollsys(struct pollfd *fds, nfds_t nfd, const timespec_t *timeout,
9660Sstevel@tonic-gate 	const sigset_t *sigmask)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	extern int __pollsys(struct pollfd *, nfds_t, const timespec_t *,
9694843Sraf 	    const sigset_t *);
9700Sstevel@tonic-gate 	int rv;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	PROLOGUE_MASK(sigmask)
9730Sstevel@tonic-gate 	rv = __pollsys(fds, nfd, timeout, sigmask);
9740Sstevel@tonic-gate 	EPILOGUE_MASK
9750Sstevel@tonic-gate 	return (rv);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate int
9796812Sraf sigtimedwait(const sigset_t *set, siginfo_t *infop, const timespec_t *timeout)
9800Sstevel@tonic-gate {
9812248Sraf 	extern int __sigtimedwait(const sigset_t *, siginfo_t *,
9824843Sraf 	    const timespec_t *);
9830Sstevel@tonic-gate 	siginfo_t info;
9840Sstevel@tonic-gate 	int sig;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	PROLOGUE
9875891Sraf 	if (abort) {
9885891Sraf 		*self->ul_errnop = EINTR;
9890Sstevel@tonic-gate 		sig = -1;
9905891Sraf 	} else {
9915891Sraf 		sig = __sigtimedwait(set, &info, timeout);
9925891Sraf 		if (sig == SIGCANCEL &&
9935891Sraf 		    (SI_FROMKERNEL(&info) || info.si_code == SI_LWP)) {
9945891Sraf 			do_sigcancel();
9955891Sraf 			*self->ul_errnop = EINTR;
9965891Sraf 			sig = -1;
9975891Sraf 		}
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	EPILOGUE
10000Sstevel@tonic-gate 	if (sig != -1 && infop)
10016515Sraf 		(void) memcpy(infop, &info, sizeof (*infop));
10020Sstevel@tonic-gate 	return (sig);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate int
10066812Sraf sigwait(sigset_t *set)
10070Sstevel@tonic-gate {
10086812Sraf 	return (sigtimedwait(set, NULL, NULL));
10092248Sraf }
10102248Sraf 
10112248Sraf int
10126812Sraf sigwaitinfo(const sigset_t *set, siginfo_t *info)
10132248Sraf {
10146812Sraf 	return (sigtimedwait(set, info, NULL));
10152248Sraf }
10162248Sraf 
10172248Sraf int
10186812Sraf sigqueue(pid_t pid, int signo, const union sigval value)
10192248Sraf {
10202248Sraf 	extern int __sigqueue(pid_t pid, int signo,
10214843Sraf 	    /* const union sigval */ void *value, int si_code, int block);
10222248Sraf 	return (__sigqueue(pid, signo, value.sival_ptr, SI_QUEUE, 0));
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate int
10265891Sraf _so_accept(int sock, struct sockaddr *addr, uint_t *addrlen, int version)
10270Sstevel@tonic-gate {
10285891Sraf 	extern int __so_accept(int, struct sockaddr *, uint_t *, int);
10290Sstevel@tonic-gate 	int rv;
10300Sstevel@tonic-gate 
10315891Sraf 	PERFORM(__so_accept(sock, addr, addrlen, version))
10325891Sraf }
10335891Sraf 
10345891Sraf int
10355891Sraf _so_connect(int sock, struct sockaddr *addr, uint_t addrlen, int version)
10365891Sraf {
10375891Sraf 	extern int __so_connect(int, struct sockaddr *, uint_t, int);
10385891Sraf 	int rv;
10395891Sraf 
10405891Sraf 	PERFORM(__so_connect(sock, addr, addrlen, version))
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate 
10435891Sraf int
10445891Sraf _so_recv(int sock, void *buf, size_t len, int flags)
10450Sstevel@tonic-gate {
10465891Sraf 	extern int __so_recv(int, void *, size_t, int);
10475891Sraf 	int rv;
10480Sstevel@tonic-gate 
10495891Sraf 	PERFORM(__so_recv(sock, buf, len, flags))
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate 
10525891Sraf int
10535891Sraf _so_recvfrom(int sock, void *buf, size_t len, int flags,
10545891Sraf     struct sockaddr *addr, int *addrlen)
10550Sstevel@tonic-gate {
10565891Sraf 	extern int __so_recvfrom(int, void *, size_t, int,
10575891Sraf 	    struct sockaddr *, int *);
10585891Sraf 	int rv;
10595891Sraf 
10605891Sraf 	PERFORM(__so_recvfrom(sock, buf, len, flags, addr, addrlen))
10615891Sraf }
10620Sstevel@tonic-gate 
10635891Sraf int
10645891Sraf _so_recvmsg(int sock, struct msghdr *msg, int flags)
10655891Sraf {
10665891Sraf 	extern int __so_recvmsg(int, struct msghdr *, int);
10675891Sraf 	int rv;
10685891Sraf 
10695891Sraf 	PERFORM(__so_recvmsg(sock, msg, flags))
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate int
10735891Sraf _so_send(int sock, const void *buf, size_t len, int flags)
10740Sstevel@tonic-gate {
10755891Sraf 	extern int __so_send(int, const void *, size_t, int);
10760Sstevel@tonic-gate 	int rv;
10770Sstevel@tonic-gate 
10785891Sraf 	PERFORM(__so_send(sock, buf, len, flags))
10795891Sraf }
10805891Sraf 
10815891Sraf int
10825891Sraf _so_sendmsg(int sock, const struct msghdr *msg, int flags)
10835891Sraf {
10845891Sraf 	extern int __so_sendmsg(int, const struct msghdr *, int);
10855891Sraf 	int rv;
10865891Sraf 
10875891Sraf 	PERFORM(__so_sendmsg(sock, msg, flags))
10885891Sraf }
10895891Sraf 
10905891Sraf int
10915891Sraf _so_sendto(int sock, const void *buf, size_t len, int flags,
10925891Sraf     const struct sockaddr *addr, int *addrlen)
10935891Sraf {
10945891Sraf 	extern int __so_sendto(int, const void *, size_t, int,
10955891Sraf 	    const struct sockaddr *, int *);
10965891Sraf 	int rv;
10975891Sraf 
10985891Sraf 	PERFORM(__so_sendto(sock, buf, len, flags, addr, addrlen))
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate 
11015891Sraf int
11026812Sraf tcdrain(int fildes)
11030Sstevel@tonic-gate {
11045891Sraf 	extern int __tcdrain(int);
11055891Sraf 	int rv;
11060Sstevel@tonic-gate 
11075891Sraf 	PERFORM(__tcdrain(fildes))
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
11105891Sraf int
11116812Sraf waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options)
11125891Sraf {
11135891Sraf 	extern int __waitid(idtype_t, id_t, siginfo_t *, int);
11145891Sraf 	int rv;
11155891Sraf 
11165891Sraf 	if (options & WNOHANG)
11175891Sraf 		return (__waitid(idtype, id, infop, options));
11185891Sraf 	PERFORM(__waitid(idtype, id, infop, options))
11195891Sraf }
11205891Sraf 
11210Sstevel@tonic-gate ssize_t
11226812Sraf writev(int fildes, const struct iovec *iov, int iovcnt)
11230Sstevel@tonic-gate {
11245891Sraf 	extern ssize_t __writev(int, const struct iovec *, int);
11250Sstevel@tonic-gate 	ssize_t rv;
11260Sstevel@tonic-gate 
11275891Sraf 	PERFORM(__writev(fildes, iov, iovcnt))
11280Sstevel@tonic-gate }
1129