xref: /illumos-gate/usr/src/lib/libc/port/threads/scalls.c (revision 4763305e3243687c189d755d737d52205b2614ed)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright (c) 2015, Joyent, Inc.  All rights reserved.
26  */
27 
28 /* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
29 
30 #include "lint.h"
31 #include "thr_uberdata.h"
32 #include "libc.h"
33 #include <stdarg.h>
34 #include <poll.h>
35 #include <stropts.h>
36 #include <dlfcn.h>
37 #include <wait.h>
38 #include <sys/socket.h>
39 #include <sys/uio.h>
40 #include <sys/file.h>
41 #include <sys/door.h>
42 
43 /*
44  * These leading-underbar symbols exist because mistakes were made
45  * in the past that put them into non-SUNWprivate versions of
46  * the libc mapfiles.  They should be eliminated, but oh well...
47  */
48 #pragma weak _fork = fork
49 #pragma weak _read = read
50 #pragma weak _write = write
51 #pragma weak _getmsg = getmsg
52 #pragma weak _getpmsg = getpmsg
53 #pragma weak _putmsg = putmsg
54 #pragma weak _putpmsg = putpmsg
55 #pragma weak _sleep = sleep
56 #pragma weak _close = close
57 #pragma weak _creat = creat
58 #pragma weak _fcntl = fcntl
59 #pragma weak _fsync = fsync
60 #pragma weak _lockf = lockf
61 #pragma weak _msgrcv = msgrcv
62 #pragma weak _msgsnd = msgsnd
63 #pragma weak _msync = msync
64 #pragma weak _open = open
65 #pragma weak _openat = openat
66 #pragma weak _pause = pause
67 #pragma weak _readv = readv
68 #pragma weak _sigpause = sigpause
69 #pragma weak _sigsuspend = sigsuspend
70 #pragma weak _tcdrain = tcdrain
71 #pragma weak _waitid = waitid
72 #pragma weak _writev = writev
73 
74 #if !defined(_LP64)
75 #pragma weak _creat64 = creat64
76 #pragma weak _lockf64 = lockf64
77 #pragma weak _open64 = open64
78 #pragma weak _openat64 = openat64
79 #pragma weak _pread64 = pread64
80 #pragma weak _pwrite64 = pwrite64
81 #endif
82 
83 /*
84  * These are SUNWprivate, but they are being used by Sun Studio libcollector.
85  */
86 #pragma weak _fork1 = fork1
87 #pragma weak _forkall = forkall
88 
89 /*
90  * atfork_lock protects the pthread_atfork() data structures.
91  *
92  * fork_lock does double-duty.  Not only does it (and atfork_lock)
93  * serialize calls to fork() and forkall(), but it also serializes calls
94  * to thr_suspend() and thr_continue() (because fork() and forkall() also
95  * suspend and continue other threads and they want no competition).
96  *
97  * Functions called in dlopen()ed L10N objects can do anything, including
98  * call malloc() and free().  Such calls are not fork-safe when protected
99  * by an ordinary mutex that is acquired in libc's prefork processing
100  * because, with an interposed malloc library present, there would be a
101  * lock ordering violation due to the pthread_atfork() prefork function
102  * in the interposition library acquiring its malloc lock(s) before the
103  * ordinary mutex in libc being acquired by libc's prefork functions.
104  *
105  * Within libc, calls to malloc() and free() are fork-safe if the calls
106  * are made while holding no other libc locks.  This covers almost all
107  * of libc's malloc() and free() calls.  For those libc code paths, such
108  * as the above-mentioned L10N calls, that require serialization and that
109  * may call malloc() or free(), libc uses callout_lock_enter() to perform
110  * the serialization.  This works because callout_lock is not acquired as
111  * part of running the pthread_atfork() prefork handlers (to avoid the
112  * lock ordering violation described above).  Rather, it is simply
113  * reinitialized in postfork1_child() to cover the case that some
114  * now-defunct thread might have been suspended while holding it.
115  */
116 
117 void
fork_lock_enter(void)118 fork_lock_enter(void)
119 {
120 	ASSERT(curthread->ul_critical == 0);
121 	(void) mutex_lock(&curthread->ul_uberdata->fork_lock);
122 }
123 
124 void
fork_lock_exit(void)125 fork_lock_exit(void)
126 {
127 	ASSERT(curthread->ul_critical == 0);
128 	(void) mutex_unlock(&curthread->ul_uberdata->fork_lock);
129 }
130 
131 /*
132  * Use cancel_safe_mutex_lock() to protect against being cancelled while
133  * holding callout_lock and calling outside of libc (via L10N plugins).
134  * We will honor a pending cancellation request when callout_lock_exit()
135  * is called, by calling cancel_safe_mutex_unlock().
136  */
137 void
callout_lock_enter(void)138 callout_lock_enter(void)
139 {
140 	ASSERT(curthread->ul_critical == 0);
141 	cancel_safe_mutex_lock(&curthread->ul_uberdata->callout_lock);
142 }
143 
144 void
callout_lock_exit(void)145 callout_lock_exit(void)
146 {
147 	ASSERT(curthread->ul_critical == 0);
148 	cancel_safe_mutex_unlock(&curthread->ul_uberdata->callout_lock);
149 }
150 
151 pid_t
forkx(int flags)152 forkx(int flags)
153 {
154 	ulwp_t *self = curthread;
155 	uberdata_t *udp = self->ul_uberdata;
156 	pid_t pid;
157 
158 	if (self->ul_vfork) {
159 		/*
160 		 * We are a child of vfork(); omit all of the fork
161 		 * logic and go straight to the system call trap.
162 		 * A vfork() child of a multithreaded parent
163 		 * must never call fork().
164 		 */
165 		if (udp->uberflags.uf_mt) {
166 			errno = ENOTSUP;
167 			return (-1);
168 		}
169 		pid = __forkx(flags);
170 		if (pid == 0) {		/* child */
171 			udp->pid = getpid();
172 			self->ul_vfork = 0;
173 		}
174 		return (pid);
175 	}
176 
177 	sigoff(self);
178 	if (self->ul_fork) {
179 		/*
180 		 * Cannot call fork() from a fork handler.
181 		 */
182 		sigon(self);
183 		errno = EDEADLK;
184 		return (-1);
185 	}
186 	self->ul_fork = 1;
187 
188 	/*
189 	 * The functions registered by pthread_atfork() are defined by
190 	 * the application and its libraries and we must not hold any
191 	 * internal lmutex_lock()-acquired locks while invoking them.
192 	 * We hold only udp->atfork_lock to protect the atfork linkages.
193 	 * If one of these pthread_atfork() functions attempts to fork
194 	 * or to call pthread_atfork(), libc will detect the error and
195 	 * fail the call with EDEADLK.  Otherwise, the pthread_atfork()
196 	 * functions are free to do anything they please (except they
197 	 * will not receive any signals).
198 	 */
199 	(void) mutex_lock(&udp->atfork_lock);
200 
201 	/*
202 	 * Posix (SUSv3) requires fork() to be async-signal-safe.
203 	 * This cannot be made to happen with fork handlers in place
204 	 * (they grab locks).  To be in nominal compliance, don't run
205 	 * any fork handlers if we are called within a signal context.
206 	 * This leaves the child process in a questionable state with
207 	 * respect to its locks, but at least the parent process does
208 	 * not become deadlocked due to the calling thread attempting
209 	 * to acquire a lock that it already owns.
210 	 */
211 	if (self->ul_siglink == NULL)
212 		_prefork_handler();
213 
214 	/*
215 	 * Block every other thread attempting thr_suspend() or thr_continue().
216 	 */
217 	(void) mutex_lock(&udp->fork_lock);
218 
219 	/*
220 	 * Block all signals.
221 	 * Just deferring them via sigoff() is not enough.
222 	 * We have to avoid taking a deferred signal in the child
223 	 * that was actually sent to the parent before __forkx().
224 	 */
225 	block_all_signals(self);
226 
227 	/*
228 	 * This suspends all threads but this one, leaving them
229 	 * suspended outside of any critical regions in the library.
230 	 * Thus, we are assured that no lmutex_lock()-acquired library
231 	 * locks are held while we invoke fork() from the current thread.
232 	 */
233 	suspend_fork();
234 
235 	pid = __forkx(flags);
236 
237 	if (pid == 0) {		/* child */
238 		/*
239 		 * Clear our schedctl pointer.
240 		 * Discard any deferred signal that was sent to the parent.
241 		 * Because we blocked all signals before __forkx(), a
242 		 * deferred signal cannot have been taken by the child.
243 		 */
244 		self->ul_schedctl_called = NULL;
245 		self->ul_schedctl = NULL;
246 		self->ul_cursig = 0;
247 		self->ul_siginfo.si_signo = 0;
248 		udp->pid = getpid();
249 		/* reset the library's data structures to reflect one thread */
250 		unregister_locks();
251 		postfork1_child();
252 		restore_signals(self);
253 		(void) mutex_unlock(&udp->fork_lock);
254 		if (self->ul_siglink == NULL)
255 			_postfork_child_handler();
256 	} else {
257 		/* restart all threads that were suspended for fork() */
258 		continue_fork(0);
259 		restore_signals(self);
260 		(void) mutex_unlock(&udp->fork_lock);
261 		if (self->ul_siglink == NULL)
262 			_postfork_parent_handler();
263 	}
264 
265 	(void) mutex_unlock(&udp->atfork_lock);
266 	self->ul_fork = 0;
267 	sigon(self);
268 
269 	return (pid);
270 }
271 
272 /*
273  * fork() is fork1() for both Posix threads and Solaris threads.
274  * The forkall() interface exists for applications that require
275  * the semantics of replicating all threads.
276  */
277 #pragma weak fork1 = fork
278 pid_t
fork(void)279 fork(void)
280 {
281 	return (forkx(0));
282 }
283 
284 /*
285  * Much of the logic here is the same as in forkx().
286  * See the comments in forkx(), above.
287  */
288 pid_t
forkallx(int flags)289 forkallx(int flags)
290 {
291 	ulwp_t *self = curthread;
292 	uberdata_t *udp = self->ul_uberdata;
293 	pid_t pid;
294 
295 	if (self->ul_vfork) {
296 		if (udp->uberflags.uf_mt) {
297 			errno = ENOTSUP;
298 			return (-1);
299 		}
300 		pid = __forkallx(flags);
301 		if (pid == 0) {		/* child */
302 			udp->pid = getpid();
303 			self->ul_vfork = 0;
304 		}
305 		return (pid);
306 	}
307 
308 	sigoff(self);
309 	if (self->ul_fork) {
310 		sigon(self);
311 		errno = EDEADLK;
312 		return (-1);
313 	}
314 	self->ul_fork = 1;
315 	(void) mutex_lock(&udp->atfork_lock);
316 	(void) mutex_lock(&udp->fork_lock);
317 	block_all_signals(self);
318 	suspend_fork();
319 
320 	pid = __forkallx(flags);
321 
322 	if (pid == 0) {
323 		self->ul_schedctl_called = NULL;
324 		self->ul_schedctl = NULL;
325 		self->ul_cursig = 0;
326 		self->ul_siginfo.si_signo = 0;
327 		udp->pid = getpid();
328 		unregister_locks();
329 		continue_fork(1);
330 	} else {
331 		continue_fork(0);
332 	}
333 	restore_signals(self);
334 	(void) mutex_unlock(&udp->fork_lock);
335 	(void) mutex_unlock(&udp->atfork_lock);
336 	self->ul_fork = 0;
337 	sigon(self);
338 
339 	return (pid);
340 }
341 
342 pid_t
forkall(void)343 forkall(void)
344 {
345 	return (forkallx(0));
346 }
347 
348 /*
349  * For the implementation of cancellation at cancellation points.
350  */
351 #define	PROLOGUE							\
352 {									\
353 	ulwp_t *self = curthread;					\
354 	int nocancel =							\
355 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |	\
356 	    self->ul_critical | self->ul_sigdefer);			\
357 	int abort = 0;							\
358 	if (nocancel == 0) {						\
359 		self->ul_save_async = self->ul_cancel_async;		\
360 		if (!self->ul_cancel_disabled) {			\
361 			self->ul_cancel_async = 1;			\
362 			if (self->ul_cancel_pending)			\
363 				pthread_exit(PTHREAD_CANCELED);		\
364 		}							\
365 		self->ul_sp = stkptr();					\
366 	} else if (self->ul_cancel_pending &&				\
367 	    !self->ul_cancel_disabled) {				\
368 		set_cancel_eintr_flag(self);				\
369 		abort = 1;						\
370 	}
371 
372 #define	EPILOGUE							\
373 	if (nocancel == 0) {						\
374 		self->ul_sp = 0;					\
375 		self->ul_cancel_async = self->ul_save_async;		\
376 	}								\
377 }
378 
379 /*
380  * Perform the body of the action required by most of the cancelable
381  * function calls.  The return(function_call) part is to allow the
382  * compiler to make the call be executed with tail recursion, which
383  * saves a register window on sparc and slightly (not much) improves
384  * the code for x86/x64 compilations.
385  */
386 #define	PERFORM(function_call)						\
387 	PROLOGUE							\
388 	if (abort) {							\
389 		*self->ul_errnop = EINTR;				\
390 		return (-1);						\
391 	}								\
392 	if (nocancel)							\
393 		return (function_call);					\
394 	rv = function_call;						\
395 	EPILOGUE							\
396 	return (rv);
397 
398 /*
399  * Specialized prologue for sigsuspend() and pollsys().
400  * These system calls pass a signal mask to the kernel.
401  * The kernel replaces the thread's signal mask with the
402  * temporary mask before the thread goes to sleep.  If
403  * a signal is received, the signal handler will execute
404  * with the temporary mask, as modified by the sigaction
405  * for the particular signal.
406  *
407  * We block all signals until we reach the kernel with the
408  * temporary mask.  This eliminates race conditions with
409  * setting the signal mask while signals are being posted.
410  */
411 #define	PROLOGUE_MASK(sigmask)						\
412 {									\
413 	ulwp_t *self = curthread;					\
414 	int nocancel =							\
415 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |	\
416 	    self->ul_critical | self->ul_sigdefer);			\
417 	if (!self->ul_vfork) {						\
418 		if (sigmask) {						\
419 			block_all_signals(self);			\
420 			self->ul_tmpmask = *sigmask;			\
421 			delete_reserved_signals(&self->ul_tmpmask);	\
422 			self->ul_sigsuspend = 1;			\
423 		}							\
424 		if (nocancel == 0) {					\
425 			self->ul_save_async = self->ul_cancel_async;	\
426 			if (!self->ul_cancel_disabled) {		\
427 				self->ul_cancel_async = 1;		\
428 				if (self->ul_cancel_pending) {		\
429 					if (self->ul_sigsuspend) {	\
430 						self->ul_sigsuspend = 0;\
431 						restore_signals(self);	\
432 					}				\
433 					pthread_exit(PTHREAD_CANCELED);	\
434 				}					\
435 			}						\
436 			self->ul_sp = stkptr();				\
437 		}							\
438 	}
439 
440 /*
441  * If a signal is taken, we return from the system call wrapper with
442  * our original signal mask restored (see code in call_user_handler()).
443  * If not (self->ul_sigsuspend is still non-zero), we must restore our
444  * original signal mask ourself.
445  */
446 #define	EPILOGUE_MASK							\
447 	if (nocancel == 0) {						\
448 		self->ul_sp = 0;					\
449 		self->ul_cancel_async = self->ul_save_async;		\
450 	}								\
451 	if (self->ul_sigsuspend) {					\
452 		self->ul_sigsuspend = 0;				\
453 		restore_signals(self);					\
454 	}								\
455 }
456 
457 /*
458  * Cancellation prologue and epilogue functions,
459  * for cancellation points too complex to include here.
460  */
461 void
_cancel_prologue(void)462 _cancel_prologue(void)
463 {
464 	ulwp_t *self = curthread;
465 
466 	self->ul_cancel_prologue =
467 	    (self->ul_vfork | self->ul_nocancel | self->ul_libc_locks |
468 	    self->ul_critical | self->ul_sigdefer) != 0;
469 	if (self->ul_cancel_prologue == 0) {
470 		self->ul_save_async = self->ul_cancel_async;
471 		if (!self->ul_cancel_disabled) {
472 			self->ul_cancel_async = 1;
473 			if (self->ul_cancel_pending)
474 				pthread_exit(PTHREAD_CANCELED);
475 		}
476 		self->ul_sp = stkptr();
477 	} else if (self->ul_cancel_pending &&
478 	    !self->ul_cancel_disabled) {
479 		set_cancel_eintr_flag(self);
480 	}
481 }
482 
483 void
_cancel_epilogue(void)484 _cancel_epilogue(void)
485 {
486 	ulwp_t *self = curthread;
487 
488 	if (self->ul_cancel_prologue == 0) {
489 		self->ul_sp = 0;
490 		self->ul_cancel_async = self->ul_save_async;
491 	}
492 }
493 
494 /*
495  * Called from _thrp_join() (thr_join() is a cancellation point)
496  */
497 int
lwp_wait(thread_t tid,thread_t * found)498 lwp_wait(thread_t tid, thread_t *found)
499 {
500 	int error;
501 
502 	PROLOGUE
503 	if (abort)
504 		return (EINTR);
505 	while ((error = __lwp_wait(tid, found)) == EINTR && !cancel_active())
506 		continue;
507 	EPILOGUE
508 	return (error);
509 }
510 
511 ssize_t
read(int fd,void * buf,size_t size)512 read(int fd, void *buf, size_t size)
513 {
514 	extern ssize_t __read(int, void *, size_t);
515 	ssize_t rv;
516 
517 	PERFORM(__read(fd, buf, size))
518 }
519 
520 ssize_t
write(int fd,const void * buf,size_t size)521 write(int fd, const void *buf, size_t size)
522 {
523 	extern ssize_t __write(int, const void *, size_t);
524 	ssize_t rv;
525 
526 	PERFORM(__write(fd, buf, size))
527 }
528 
529 int
getmsg(int fd,struct strbuf * ctlptr,struct strbuf * dataptr,int * flagsp)530 getmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr,
531     int *flagsp)
532 {
533 	extern int __getmsg(int, struct strbuf *, struct strbuf *, int *);
534 	int rv;
535 
536 	PERFORM(__getmsg(fd, ctlptr, dataptr, flagsp))
537 }
538 
539 int
getpmsg(int fd,struct strbuf * ctlptr,struct strbuf * dataptr,int * bandp,int * flagsp)540 getpmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr,
541     int *bandp, int *flagsp)
542 {
543 	extern int __getpmsg(int, struct strbuf *, struct strbuf *,
544 	    int *, int *);
545 	int rv;
546 
547 	PERFORM(__getpmsg(fd, ctlptr, dataptr, bandp, flagsp))
548 }
549 
550 int
putmsg(int fd,const struct strbuf * ctlptr,const struct strbuf * dataptr,int flags)551 putmsg(int fd, const struct strbuf *ctlptr,
552     const struct strbuf *dataptr, int flags)
553 {
554 	extern int __putmsg(int, const struct strbuf *,
555 	    const struct strbuf *, int);
556 	int rv;
557 
558 	PERFORM(__putmsg(fd, ctlptr, dataptr, flags))
559 }
560 
561 int
__xpg4_putmsg(int fd,const struct strbuf * ctlptr,const struct strbuf * dataptr,int flags)562 __xpg4_putmsg(int fd, const struct strbuf *ctlptr,
563     const struct strbuf *dataptr, int flags)
564 {
565 	extern int __putmsg(int, const struct strbuf *,
566 	    const struct strbuf *, int);
567 	int rv;
568 
569 	PERFORM(__putmsg(fd, ctlptr, dataptr, flags|MSG_XPG4))
570 }
571 
572 int
putpmsg(int fd,const struct strbuf * ctlptr,const struct strbuf * dataptr,int band,int flags)573 putpmsg(int fd, const struct strbuf *ctlptr,
574     const struct strbuf *dataptr, int band, int flags)
575 {
576 	extern int __putpmsg(int, const struct strbuf *,
577 	    const struct strbuf *, int, int);
578 	int rv;
579 
580 	PERFORM(__putpmsg(fd, ctlptr, dataptr, band, flags))
581 }
582 
583 int
__xpg4_putpmsg(int fd,const struct strbuf * ctlptr,const struct strbuf * dataptr,int band,int flags)584 __xpg4_putpmsg(int fd, const struct strbuf *ctlptr,
585     const struct strbuf *dataptr, int band, int flags)
586 {
587 	extern int __putpmsg(int, const struct strbuf *,
588 	    const struct strbuf *, int, int);
589 	int rv;
590 
591 	PERFORM(__putpmsg(fd, ctlptr, dataptr, band, flags|MSG_XPG4))
592 }
593 
594 int
nanosleep(const timespec_t * rqtp,timespec_t * rmtp)595 nanosleep(const timespec_t *rqtp, timespec_t *rmtp)
596 {
597 	int error;
598 
599 	PROLOGUE
600 	error = abort? EINTR : __nanosleep(rqtp, rmtp);
601 	EPILOGUE
602 	if (error) {
603 		errno = error;
604 		return (-1);
605 	}
606 	return (0);
607 }
608 
609 int
clock_nanosleep(clockid_t clock_id,int flags,const timespec_t * rqtp,timespec_t * rmtp)610 clock_nanosleep(clockid_t clock_id, int flags,
611     const timespec_t *rqtp, timespec_t *rmtp)
612 {
613 	timespec_t reltime;
614 	hrtime_t start;
615 	hrtime_t rqlapse;
616 	hrtime_t lapse;
617 	int error;
618 
619 	switch (clock_id) {
620 	case CLOCK_VIRTUAL:
621 	case CLOCK_PROCESS_CPUTIME_ID:
622 	case CLOCK_THREAD_CPUTIME_ID:
623 		return (ENOTSUP);
624 	case CLOCK_REALTIME:
625 	case CLOCK_HIGHRES:
626 		break;
627 	default:
628 		return (EINVAL);
629 	}
630 	if (flags & TIMER_ABSTIME) {
631 		abstime_to_reltime(clock_id, rqtp, &reltime);
632 		rmtp = NULL;
633 	} else {
634 		reltime = *rqtp;
635 		if (clock_id == CLOCK_HIGHRES)
636 			start = gethrtime();
637 	}
638 restart:
639 	PROLOGUE
640 	error = abort? EINTR : __nanosleep(&reltime, rmtp);
641 	EPILOGUE
642 	if (error == 0 && clock_id == CLOCK_HIGHRES) {
643 		/*
644 		 * Don't return yet if we didn't really get a timeout.
645 		 * This can happen if we return because someone resets
646 		 * the system clock.
647 		 */
648 		if (flags & TIMER_ABSTIME) {
649 			if ((hrtime_t)(uint32_t)rqtp->tv_sec * NANOSEC +
650 			    rqtp->tv_nsec > gethrtime()) {
651 				abstime_to_reltime(clock_id, rqtp, &reltime);
652 				goto restart;
653 			}
654 		} else {
655 			rqlapse = (hrtime_t)(uint32_t)rqtp->tv_sec * NANOSEC +
656 			    rqtp->tv_nsec;
657 			lapse = gethrtime() - start;
658 			if (rqlapse > lapse) {
659 				hrt2ts(rqlapse - lapse, &reltime);
660 				goto restart;
661 			}
662 		}
663 	}
664 	if (error == 0 && clock_id == CLOCK_REALTIME &&
665 	    (flags & TIMER_ABSTIME)) {
666 		/*
667 		 * Don't return yet just because someone reset the
668 		 * system clock.  Recompute the new relative time
669 		 * and reissue the nanosleep() call if necessary.
670 		 *
671 		 * Resetting the system clock causes all sorts of
672 		 * problems and the SUSV3 standards body should
673 		 * have made the behavior of clock_nanosleep() be
674 		 * implementation-defined in such a case rather than
675 		 * being specific about honoring the new system time.
676 		 * Standards bodies are filled with fools and idiots.
677 		 */
678 		abstime_to_reltime(clock_id, rqtp, &reltime);
679 		if (reltime.tv_sec != 0 || reltime.tv_nsec != 0)
680 			goto restart;
681 	}
682 	return (error);
683 }
684 
685 unsigned int
sleep(unsigned int sec)686 sleep(unsigned int sec)
687 {
688 	unsigned int rem = 0;
689 	timespec_t ts;
690 	timespec_t tsr;
691 
692 	ts.tv_sec = (time_t)sec;
693 	ts.tv_nsec = 0;
694 	if (nanosleep(&ts, &tsr) == -1 && errno == EINTR) {
695 		rem = (unsigned int)tsr.tv_sec;
696 		if (tsr.tv_nsec >= NANOSEC / 2)
697 			rem++;
698 	}
699 	return (rem);
700 }
701 
702 int
usleep(useconds_t usec)703 usleep(useconds_t usec)
704 {
705 	timespec_t ts;
706 
707 	ts.tv_sec = usec / MICROSEC;
708 	ts.tv_nsec = (long)(usec % MICROSEC) * 1000;
709 	(void) nanosleep(&ts, NULL);
710 	return (0);
711 }
712 
713 int
close(int fildes)714 close(int fildes)
715 {
716 	extern void _aio_close(int);
717 	extern int __close(int);
718 	int rv;
719 
720 	/*
721 	 * If we call _aio_close() while in a critical region,
722 	 * we will draw an ASSERT() failure, so don't do it.
723 	 * No calls to close() from within libc need _aio_close();
724 	 * only the application's calls to close() need this,
725 	 * and such calls are never from a libc critical region.
726 	 */
727 	if (curthread->ul_critical == 0)
728 		_aio_close(fildes);
729 	PERFORM(__close(fildes))
730 }
731 
732 int
door_call(int d,door_arg_t * params)733 door_call(int d, door_arg_t *params)
734 {
735 	extern int __door_call(int, door_arg_t *);
736 	int rv;
737 
738 	PERFORM(__door_call(d, params))
739 }
740 
741 int
fcntl(int fildes,int cmd,...)742 fcntl(int fildes, int cmd, ...)
743 {
744 	extern int __fcntl(int, int, ...);
745 	intptr_t arg, arg1 = 0;
746 	int rv;
747 	va_list ap;
748 
749 	va_start(ap, cmd);
750 	switch (cmd) {
751 	case F_DUP3FD:
752 		arg = va_arg(ap, int);
753 		arg1 = va_arg(ap, int);
754 		break;
755 	default:
756 		arg = va_arg(ap, intptr_t);
757 		break;
758 	}
759 	va_end(ap);
760 	if (cmd != F_SETLKW)
761 		return (__fcntl(fildes, cmd, arg, arg1));
762 	PERFORM(__fcntl(fildes, cmd, arg))
763 }
764 
765 int
fdatasync(int fildes)766 fdatasync(int fildes)
767 {
768 	int rv;
769 
770 	PERFORM(__fdsync(fildes, FDSYNC_DATA))
771 }
772 
773 int
fsync(int fildes)774 fsync(int fildes)
775 {
776 	int rv;
777 
778 	PERFORM(__fdsync(fildes, FDSYNC_FILE))
779 }
780 
781 int
syncfs(int fildes)782 syncfs(int fildes)
783 {
784 	int rv;
785 
786 	PERFORM(__fdsync(fildes, FDSYNC_FS))
787 }
788 
789 int
lockf(int fildes,int function,off_t size)790 lockf(int fildes, int function, off_t size)
791 {
792 	extern int __lockf(int, int, off_t);
793 	int rv;
794 
795 	PERFORM(__lockf(fildes, function, size))
796 }
797 
798 #if !defined(_LP64)
799 int
lockf64(int fildes,int function,off64_t size)800 lockf64(int fildes, int function, off64_t size)
801 {
802 	extern int __lockf64(int, int, off64_t);
803 	int rv;
804 
805 	PERFORM(__lockf64(fildes, function, size))
806 }
807 #endif	/* !_LP64 */
808 
809 ssize_t
msgrcv(int msqid,void * msgp,size_t msgsz,long msgtyp,int msgflg)810 msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
811 {
812 	extern ssize_t __msgrcv(int, void *, size_t, long, int);
813 	ssize_t rv;
814 
815 	PERFORM(__msgrcv(msqid, msgp, msgsz, msgtyp, msgflg))
816 }
817 
818 int
msgsnd(int msqid,const void * msgp,size_t msgsz,int msgflg)819 msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
820 {
821 	extern int __msgsnd(int, const void *, size_t, int);
822 	int rv;
823 
824 	PERFORM(__msgsnd(msqid, msgp, msgsz, msgflg))
825 }
826 
827 int
msync(void * addr,size_t len,int flags)828 msync(void *addr, size_t len, int flags)
829 {
830 	extern int __msync(caddr_t, size_t, int);
831 	int rv;
832 
833 	PERFORM(__msync(addr, len, flags))
834 }
835 
836 int
openat(int fd,const char * path,int oflag,...)837 openat(int fd, const char *path, int oflag, ...)
838 {
839 	mode_t mode;
840 	int rv;
841 	va_list ap;
842 
843 	va_start(ap, oflag);
844 	mode = va_arg(ap, mode_t);
845 	va_end(ap);
846 	PERFORM(__openat(fd, path, oflag, mode))
847 }
848 
849 int
open(const char * path,int oflag,...)850 open(const char *path, int oflag, ...)
851 {
852 	mode_t mode;
853 	int rv;
854 	va_list ap;
855 
856 	va_start(ap, oflag);
857 	mode = va_arg(ap, mode_t);
858 	va_end(ap);
859 	PERFORM(__open(path, oflag, mode))
860 }
861 
862 int
creat(const char * path,mode_t mode)863 creat(const char *path, mode_t mode)
864 {
865 	return (open(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
866 }
867 
868 #if !defined(_LP64)
869 int
openat64(int fd,const char * path,int oflag,...)870 openat64(int fd, const char *path, int oflag, ...)
871 {
872 	mode_t mode;
873 	int rv;
874 	va_list ap;
875 
876 	va_start(ap, oflag);
877 	mode = va_arg(ap, mode_t);
878 	va_end(ap);
879 	PERFORM(__openat64(fd, path, oflag, mode))
880 }
881 
882 int
open64(const char * path,int oflag,...)883 open64(const char *path, int oflag, ...)
884 {
885 	mode_t mode;
886 	int rv;
887 	va_list ap;
888 
889 	va_start(ap, oflag);
890 	mode = va_arg(ap, mode_t);
891 	va_end(ap);
892 	PERFORM(__open64(path, oflag, mode))
893 }
894 
895 int
creat64(const char * path,mode_t mode)896 creat64(const char *path, mode_t mode)
897 {
898 	return (open64(path, O_WRONLY | O_CREAT | O_TRUNC, mode));
899 }
900 #endif	/* !_LP64 */
901 
902 int
pause(void)903 pause(void)
904 {
905 	extern int __pause(void);
906 	int rv;
907 
908 	PERFORM(__pause())
909 }
910 
911 ssize_t
pread(int fildes,void * buf,size_t nbyte,off_t offset)912 pread(int fildes, void *buf, size_t nbyte, off_t offset)
913 {
914 	extern ssize_t __pread(int, void *, size_t, off_t);
915 	ssize_t rv;
916 
917 	PERFORM(__pread(fildes, buf, nbyte, offset))
918 }
919 
920 #if !defined(_LP64)
921 ssize_t
pread64(int fildes,void * buf,size_t nbyte,off64_t offset)922 pread64(int fildes, void *buf, size_t nbyte, off64_t offset)
923 {
924 	extern ssize_t __pread64(int, void *, size_t, off64_t);
925 	ssize_t rv;
926 
927 	PERFORM(__pread64(fildes, buf, nbyte, offset))
928 }
929 
930 ssize_t
preadv64(int fildes,const struct iovec * iov,int iovcnt,off64_t offset)931 preadv64(int fildes, const struct iovec *iov, int iovcnt, off64_t offset)
932 {
933 
934 	extern ssize_t __preadv64(int, const struct iovec *, int, off_t, off_t);
935 	ssize_t rv;
936 
937 	PERFORM(__preadv64(fildes, iov, iovcnt, offset & 0xffffffffULL,
938 	    offset>>32))
939 }
940 #endif	/* !_LP64 */
941 
942 ssize_t
preadv(int fildes,const struct iovec * iov,int iovcnt,off_t offset)943 preadv(int fildes, const struct iovec *iov, int iovcnt, off_t offset)
944 {
945 
946 	extern ssize_t __preadv(int, const struct iovec *, int, off_t, off_t);
947 	ssize_t rv;
948 
949 	PERFORM(__preadv(fildes, iov, iovcnt, offset, 0))
950 }
951 ssize_t
pwrite(int fildes,const void * buf,size_t nbyte,off_t offset)952 pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
953 {
954 	extern ssize_t __pwrite(int, const void *, size_t, off_t);
955 	ssize_t rv;
956 
957 	PERFORM(__pwrite(fildes, buf, nbyte, offset))
958 }
959 
960 #if !defined(_LP64)
961 ssize_t
pwrite64(int fildes,const void * buf,size_t nbyte,off64_t offset)962 pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset)
963 {
964 	extern ssize_t __pwrite64(int, const void *, size_t, off64_t);
965 	ssize_t rv;
966 
967 	PERFORM(__pwrite64(fildes, buf, nbyte, offset))
968 }
969 
970 ssize_t
pwritev64(int fildes,const struct iovec * iov,int iovcnt,off64_t offset)971 pwritev64(int fildes, const struct iovec *iov, int iovcnt, off64_t offset)
972 {
973 
974 	extern ssize_t __pwritev64(int,
975 	    const struct iovec *, int, off_t, off_t);
976 	ssize_t rv;
977 
978 	PERFORM(__pwritev64(fildes, iov, iovcnt, offset &
979 	    0xffffffffULL, offset>>32))
980 }
981 
982 #endif	/* !_LP64 */
983 
984 ssize_t
pwritev(int fildes,const struct iovec * iov,int iovcnt,off_t offset)985 pwritev(int fildes, const struct iovec *iov, int iovcnt, off_t offset)
986 {
987 	extern ssize_t __pwritev(int, const struct iovec *, int, off_t, off_t);
988 	ssize_t rv;
989 
990 	PERFORM(__pwritev(fildes, iov, iovcnt, offset, 0))
991 }
992 
993 ssize_t
readv(int fildes,const struct iovec * iov,int iovcnt)994 readv(int fildes, const struct iovec *iov, int iovcnt)
995 {
996 	extern ssize_t __readv(int, const struct iovec *, int);
997 	ssize_t rv;
998 
999 	PERFORM(__readv(fildes, iov, iovcnt))
1000 }
1001 
1002 int
sigpause(int sig)1003 sigpause(int sig)
1004 {
1005 	extern int __sigpause(int);
1006 	int rv;
1007 
1008 	PERFORM(__sigpause(sig))
1009 }
1010 
1011 int
sigsuspend(const sigset_t * set)1012 sigsuspend(const sigset_t *set)
1013 {
1014 	extern int __sigsuspend(const sigset_t *);
1015 	int rv;
1016 
1017 	PROLOGUE_MASK(set)
1018 	rv = __sigsuspend(set);
1019 	EPILOGUE_MASK
1020 	return (rv);
1021 }
1022 
1023 int
_pollsys(struct pollfd * fds,nfds_t nfd,const timespec_t * timeout,const sigset_t * sigmask)1024 _pollsys(struct pollfd *fds, nfds_t nfd, const timespec_t *timeout,
1025     const sigset_t *sigmask)
1026 {
1027 	extern int __pollsys(struct pollfd *, nfds_t, const timespec_t *,
1028 	    const sigset_t *);
1029 	int rv;
1030 
1031 	PROLOGUE_MASK(sigmask)
1032 	rv = __pollsys(fds, nfd, timeout, sigmask);
1033 	EPILOGUE_MASK
1034 	return (rv);
1035 }
1036 
1037 int
sigtimedwait(const sigset_t * set,siginfo_t * infop,const timespec_t * timeout)1038 sigtimedwait(const sigset_t *set, siginfo_t *infop, const timespec_t *timeout)
1039 {
1040 	extern int __sigtimedwait(const sigset_t *, siginfo_t *,
1041 	    const timespec_t *);
1042 	siginfo_t info;
1043 	int sig;
1044 
1045 	PROLOGUE
1046 	if (abort) {
1047 		*self->ul_errnop = EINTR;
1048 		sig = -1;
1049 	} else {
1050 		sig = __sigtimedwait(set, &info, timeout);
1051 		if (sig == SIGCANCEL &&
1052 		    (SI_FROMKERNEL(&info) || info.si_code == SI_LWP)) {
1053 			do_sigcancel();
1054 			*self->ul_errnop = EINTR;
1055 			sig = -1;
1056 		}
1057 	}
1058 	EPILOGUE
1059 	if (sig != -1 && infop)
1060 		(void) memcpy(infop, &info, sizeof (*infop));
1061 	return (sig);
1062 }
1063 
1064 int
sigwait(sigset_t * set)1065 sigwait(sigset_t *set)
1066 {
1067 	return (sigtimedwait(set, NULL, NULL));
1068 }
1069 
1070 int
sigwaitinfo(const sigset_t * set,siginfo_t * info)1071 sigwaitinfo(const sigset_t *set, siginfo_t *info)
1072 {
1073 	return (sigtimedwait(set, info, NULL));
1074 }
1075 
1076 int
sigqueue(pid_t pid,int signo,const union sigval value)1077 sigqueue(pid_t pid, int signo, const union sigval value)
1078 {
1079 	extern int __sigqueue(pid_t pid, int signo,
1080 	    /* const union sigval */ void *value, int si_code, int block);
1081 	return (__sigqueue(pid, signo, value.sival_ptr, SI_QUEUE, 0));
1082 }
1083 
1084 int
_so_accept(int sock,struct sockaddr * addr,uint_t * addrlen,int version,int flags)1085 _so_accept(int sock, struct sockaddr *addr, uint_t *addrlen, int version,
1086     int flags)
1087 {
1088 	extern int __so_accept(int, struct sockaddr *, uint_t *, int, int);
1089 	int rv;
1090 
1091 	PERFORM(__so_accept(sock, addr, addrlen, version, flags))
1092 }
1093 
1094 int
_so_connect(int sock,struct sockaddr * addr,uint_t addrlen,int version)1095 _so_connect(int sock, struct sockaddr *addr, uint_t addrlen, int version)
1096 {
1097 	extern int __so_connect(int, struct sockaddr *, uint_t, int);
1098 	int rv;
1099 
1100 	PERFORM(__so_connect(sock, addr, addrlen, version))
1101 }
1102 
1103 int
_so_recv(int sock,void * buf,size_t len,int flags)1104 _so_recv(int sock, void *buf, size_t len, int flags)
1105 {
1106 	extern int __so_recv(int, void *, size_t, int);
1107 	int rv;
1108 
1109 	PERFORM(__so_recv(sock, buf, len, flags))
1110 }
1111 
1112 int
_so_recvfrom(int sock,void * buf,size_t len,int flags,struct sockaddr * addr,int * addrlen)1113 _so_recvfrom(int sock, void *buf, size_t len, int flags,
1114     struct sockaddr *addr, int *addrlen)
1115 {
1116 	extern int __so_recvfrom(int, void *, size_t, int,
1117 	    struct sockaddr *, int *);
1118 	int rv;
1119 
1120 	PERFORM(__so_recvfrom(sock, buf, len, flags, addr, addrlen))
1121 }
1122 
1123 int
_so_recvmsg(int sock,struct msghdr * msg,int flags)1124 _so_recvmsg(int sock, struct msghdr *msg, int flags)
1125 {
1126 	extern int __so_recvmsg(int, struct msghdr *, int);
1127 	int rv;
1128 
1129 	PERFORM(__so_recvmsg(sock, msg, flags))
1130 }
1131 
1132 int
_so_send(int sock,const void * buf,size_t len,int flags)1133 _so_send(int sock, const void *buf, size_t len, int flags)
1134 {
1135 	extern int __so_send(int, const void *, size_t, int);
1136 	int rv;
1137 
1138 	PERFORM(__so_send(sock, buf, len, flags))
1139 }
1140 
1141 int
_so_sendmsg(int sock,const struct msghdr * msg,int flags)1142 _so_sendmsg(int sock, const struct msghdr *msg, int flags)
1143 {
1144 	extern int __so_sendmsg(int, const struct msghdr *, int);
1145 	int rv;
1146 
1147 	PERFORM(__so_sendmsg(sock, msg, flags))
1148 }
1149 
1150 int
_so_sendto(int sock,const void * buf,size_t len,int flags,const struct sockaddr * addr,int * addrlen)1151 _so_sendto(int sock, const void *buf, size_t len, int flags,
1152     const struct sockaddr *addr, int *addrlen)
1153 {
1154 	extern int __so_sendto(int, const void *, size_t, int,
1155 	    const struct sockaddr *, int *);
1156 	int rv;
1157 
1158 	PERFORM(__so_sendto(sock, buf, len, flags, addr, addrlen))
1159 }
1160 
1161 int
tcdrain(int fildes)1162 tcdrain(int fildes)
1163 {
1164 	extern int __tcdrain(int);
1165 	int rv;
1166 
1167 	PERFORM(__tcdrain(fildes))
1168 }
1169 
1170 int
waitid(idtype_t idtype,id_t id,siginfo_t * infop,int options)1171 waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options)
1172 {
1173 	extern int __waitid(idtype_t, id_t, siginfo_t *, int);
1174 	int rv;
1175 
1176 	if (options & WNOHANG)
1177 		return (__waitid(idtype, id, infop, options));
1178 	PERFORM(__waitid(idtype, id, infop, options))
1179 }
1180 
1181 ssize_t
writev(int fildes,const struct iovec * iov,int iovcnt)1182 writev(int fildes, const struct iovec *iov, int iovcnt)
1183 {
1184 	extern ssize_t __writev(int, const struct iovec *, int);
1185 	ssize_t rv;
1186 
1187 	PERFORM(__writev(fildes, iov, iovcnt))
1188 }
1189