thr_syscalls.c revision 157457
1/*
2 * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
4 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice(s), this list of conditions and the following disclaimer as
12 *    the first lines of this file unmodified other than the possible
13 *    addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice(s), this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $FreeBSD: head/lib/libthr/thread/thr_syscalls.c 157457 2006-04-04 02:57:49Z davidxu $
32 */
33
34/*
35 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by John Birrell.
49 * 4. Neither the name of the author nor the names of any co-contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 */
66
67#include "namespace.h"
68#include <sys/types.h>
69#include <sys/mman.h>
70#include <sys/param.h>
71#include <sys/select.h>
72#include <sys/signalvar.h>
73#include <sys/socket.h>
74#include <sys/stat.h>
75#include <sys/time.h>
76#include <sys/uio.h>
77#include <sys/wait.h>
78#include <aio.h>
79#include <dirent.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <poll.h>
83#include <signal.h>
84#include <stdarg.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <termios.h>
89#include <unistd.h>
90#include <pthread.h>
91#include "un-namespace.h"
92
93#include "thr_private.h"
94
95extern int	__creat(const char *, mode_t);
96extern int	__pause(void);
97extern int	__pselect(int, fd_set *, fd_set *, fd_set *,
98			const struct timespec *, const sigset_t *);
99extern unsigned	__sleep(unsigned int);
100extern int	__system(const char *);
101extern int	__tcdrain(int);
102extern int	__usleep(useconds_t);
103extern pid_t	__wait(int *);
104extern pid_t	__waitpid(pid_t, int *, int);
105extern int	__sys_aio_suspend(const struct aiocb * const[], int,
106			const struct timespec *);
107extern int	__sys_accept(int, struct sockaddr *, socklen_t *);
108extern int	__sys_connect(int, const struct sockaddr *, socklen_t);
109extern int	__sys_fsync(int);
110extern int	__sys_msync(void *, size_t, int);
111extern int	__sys_poll(struct pollfd *, unsigned, int);
112extern ssize_t	__sys_recv(int, void *, size_t, int);
113extern ssize_t	__sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
114extern ssize_t	__sys_recvmsg(int, struct msghdr *, int);
115extern int	__sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
116extern int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
117			off_t *, int);
118extern ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
119extern ssize_t	__sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
120extern ssize_t	__sys_readv(int, const struct iovec *, int);
121extern pid_t	__sys_wait4(pid_t, int *, int, struct rusage *);
122extern ssize_t	__sys_writev(int, const struct iovec *, int);
123
124int	___creat(const char *, mode_t);
125int	__accept(int, struct sockaddr *, socklen_t *);
126int	__close(int);
127int	__connect(int, const struct sockaddr *, socklen_t);
128int	__fcntl(int, int,...);
129int	__fsync(int);
130int	__msync(void *, size_t, int);
131int	__nanosleep(const struct timespec *, struct timespec *);
132int	__open(const char *, int,...);
133int	__poll(struct pollfd *, unsigned int, int);
134ssize_t	__read(int, void *buf, size_t);
135ssize_t	__readv(int, const struct iovec *, int);
136ssize_t	__recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
137ssize_t	__recvmsg(int, struct msghdr *, int);
138int	__select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
139ssize_t	__sendmsg(int, const struct msghdr *, int);
140ssize_t	__sendto(int, const void *, size_t, int,
141		const struct sockaddr *, socklen_t);
142pid_t	__wait4(pid_t, int *, int, struct rusage *);
143ssize_t	__write(int, const void *, size_t);
144ssize_t	__writev(int, const struct iovec *, int);
145int	_aio_suspend(const struct aiocb * const iocbs[], int,
146		const struct timespec *);
147int	_pause(void);
148int	_pselect(int, fd_set *, fd_set *, fd_set *,
149		const struct timespec *, const sigset_t *);
150int	_raise(int);
151unsigned	_sleep(unsigned);
152int	_system(const char *);
153int	_tcdrain(int);
154int	_vfork(void);
155pid_t	_wait(int *);
156
157
158__weak_reference(__accept, accept);
159
160int
161__accept(int s, struct sockaddr *addr, socklen_t *addrlen)
162{
163	struct pthread *curthread;
164	int oldcancel;
165	int ret;
166
167	curthread = _get_curthread();
168	oldcancel = _thr_cancel_enter(curthread);
169	ret = __sys_accept(s, addr, addrlen);
170	_thr_cancel_leave(curthread, oldcancel);
171
172 	return (ret);
173}
174
175__weak_reference(_aio_suspend, aio_suspend);
176
177int
178_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
179    timespec *timeout)
180{
181	struct pthread *curthread = _get_curthread();
182	int oldcancel;
183	int ret;
184
185	oldcancel = _thr_cancel_enter(curthread);
186	ret = __sys_aio_suspend(iocbs, niocb, timeout);
187	_thr_cancel_leave(curthread, oldcancel);
188
189	return (ret);
190}
191
192__weak_reference(__close, close);
193
194int
195__close(int fd)
196{
197	struct pthread	*curthread = _get_curthread();
198	int	oldcancel;
199	int	ret;
200
201	oldcancel = _thr_cancel_enter(curthread);
202	ret = __sys_close(fd);
203	_thr_cancel_leave(curthread, oldcancel);
204
205	return (ret);
206}
207
208__weak_reference(__connect, connect);
209
210int
211__connect(int fd, const struct sockaddr *name, socklen_t namelen)
212{
213	struct pthread *curthread = _get_curthread();
214	int oldcancel;
215	int ret;
216
217	oldcancel = _thr_cancel_enter(curthread);
218	ret = __sys_connect(fd, name, namelen);
219	_thr_cancel_leave(curthread, oldcancel);
220
221 	return (ret);
222}
223
224__weak_reference(___creat, creat);
225
226int
227___creat(const char *path, mode_t mode)
228{
229	struct pthread *curthread = _get_curthread();
230	int oldcancel;
231	int ret;
232
233	oldcancel = _thr_cancel_enter(curthread);
234	ret = __creat(path, mode);
235	_thr_cancel_leave(curthread, oldcancel);
236
237	return ret;
238}
239
240__weak_reference(__fcntl, fcntl);
241
242int
243__fcntl(int fd, int cmd,...)
244{
245	struct pthread *curthread = _get_curthread();
246	int	oldcancel;
247	int	ret;
248	va_list	ap;
249
250	oldcancel = _thr_cancel_enter(curthread);
251
252	va_start(ap, cmd);
253	switch (cmd) {
254	case F_DUPFD:
255		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
256		break;
257	case F_SETFD:
258	case F_SETFL:
259		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
260		break;
261	case F_GETFD:
262	case F_GETFL:
263		ret = __sys_fcntl(fd, cmd);
264		break;
265	default:
266		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
267	}
268	va_end(ap);
269
270	_thr_cancel_leave(curthread, oldcancel);
271
272	return (ret);
273}
274
275__weak_reference(__fsync, fsync);
276
277int
278__fsync(int fd)
279{
280	struct pthread *curthread = _get_curthread();
281	int	oldcancel;
282	int	ret;
283
284	oldcancel = _thr_cancel_enter(curthread);
285	ret = __sys_fsync(fd);
286	_thr_cancel_leave(curthread, oldcancel);
287
288	return (ret);
289}
290
291__weak_reference(__msync, msync);
292
293int
294__msync(void *addr, size_t len, int flags)
295{
296	struct pthread *curthread = _get_curthread();
297	int	oldcancel;
298	int	ret;
299
300	oldcancel = _thr_cancel_enter(curthread);
301	ret = __sys_msync(addr, len, flags);
302	_thr_cancel_leave(curthread, oldcancel);
303
304	return ret;
305}
306
307__weak_reference(__nanosleep, nanosleep);
308
309int
310__nanosleep(const struct timespec *time_to_sleep,
311    struct timespec *time_remaining)
312{
313	struct pthread *curthread = _get_curthread();
314	int		oldcancel;
315	int		ret;
316
317	oldcancel = _thr_cancel_enter(curthread);
318	ret = __sys_nanosleep(time_to_sleep, time_remaining);
319	_thr_cancel_leave(curthread, oldcancel);
320
321	return (ret);
322}
323
324__weak_reference(__open, open);
325
326int
327__open(const char *path, int flags,...)
328{
329	struct pthread *curthread = _get_curthread();
330	int	oldcancel;
331	int	ret;
332	int	mode = 0;
333	va_list	ap;
334
335	oldcancel = _thr_cancel_enter(curthread);
336
337	/* Check if the file is being created: */
338	if (flags & O_CREAT) {
339		/* Get the creation mode: */
340		va_start(ap, flags);
341		mode = va_arg(ap, int);
342		va_end(ap);
343	}
344
345	ret = __sys_open(path, flags, mode);
346
347	_thr_cancel_leave(curthread, oldcancel);
348
349	return ret;
350}
351
352__weak_reference(_pause, pause);
353
354int
355_pause(void)
356{
357	struct pthread *curthread = _get_curthread();
358	int	oldcancel;
359	int	ret;
360
361	oldcancel = _thr_cancel_enter(curthread);
362	ret = __pause();
363	_thr_cancel_leave(curthread, oldcancel);
364
365	return ret;
366}
367
368__weak_reference(__poll, poll);
369
370int
371__poll(struct pollfd *fds, unsigned int nfds, int timeout)
372{
373	struct pthread *curthread = _get_curthread();
374	int oldcancel;
375	int ret;
376
377	oldcancel = _thr_cancel_enter(curthread);
378	ret = __sys_poll(fds, nfds, timeout);
379	_thr_cancel_leave(curthread, oldcancel);
380
381	return ret;
382}
383
384__weak_reference(_pselect, pselect);
385
386int
387_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
388	const struct timespec *timo, const sigset_t *mask)
389{
390	struct pthread *curthread = _get_curthread();
391	int oldcancel;
392	int ret;
393
394	oldcancel = _thr_cancel_enter(curthread);
395	ret = __pselect(count, rfds, wfds, efds, timo, mask);
396	_thr_cancel_leave(curthread, oldcancel);
397
398	return (ret);
399}
400
401__weak_reference(_raise, raise);
402
403int
404_raise(int sig)
405{
406	int ret;
407
408	if (!_thr_isthreaded())
409		ret = kill(getpid(), sig);
410	else
411		ret = _thr_send_sig(_get_curthread(), sig);
412	return (ret);
413}
414
415__weak_reference(__read, read);
416
417ssize_t
418__read(int fd, void *buf, size_t nbytes)
419{
420	struct pthread *curthread = _get_curthread();
421	int oldcancel;
422	ssize_t	ret;
423
424	oldcancel = _thr_cancel_enter(curthread);
425	ret = __sys_read(fd, buf, nbytes);
426	_thr_cancel_leave(curthread, oldcancel);
427
428	return ret;
429}
430
431__weak_reference(__readv, readv);
432
433ssize_t
434__readv(int fd, const struct iovec *iov, int iovcnt)
435{
436	struct pthread *curthread = _get_curthread();
437	int oldcancel;
438	ssize_t ret;
439
440	oldcancel = _thr_cancel_enter(curthread);
441	ret = __sys_readv(fd, iov, iovcnt);
442	_thr_cancel_leave(curthread, oldcancel);
443
444	return ret;
445}
446
447__weak_reference(__recvfrom, recvfrom);
448
449ssize_t
450__recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
451    socklen_t *fl)
452{
453	struct pthread *curthread = _get_curthread();
454	int oldcancel;
455	ssize_t ret;
456
457	oldcancel = _thr_cancel_enter(curthread);
458	ret = __sys_recvfrom(s, b, l, f, from, fl);
459	_thr_cancel_leave(curthread, oldcancel);
460	return (ret);
461}
462
463__weak_reference(__recvmsg, recvmsg);
464
465ssize_t
466__recvmsg(int s, struct msghdr *m, int f)
467{
468	struct pthread *curthread = _get_curthread();
469	ssize_t ret;
470	int oldcancel;
471
472	oldcancel = _thr_cancel_enter(curthread);
473	ret = __sys_recvmsg(s, m, f);
474	_thr_cancel_leave(curthread, oldcancel);
475	return (ret);
476}
477
478__weak_reference(__select, select);
479
480int
481__select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
482	struct timeval *timeout)
483{
484	struct pthread *curthread = _get_curthread();
485	int oldcancel;
486	int ret;
487
488	oldcancel = _thr_cancel_enter(curthread);
489	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
490	_thr_cancel_leave(curthread, oldcancel);
491	return ret;
492}
493
494__weak_reference(__sendmsg, sendmsg);
495
496ssize_t
497__sendmsg(int s, const struct msghdr *m, int f)
498{
499	struct pthread *curthread = _get_curthread();
500	ssize_t ret;
501	int oldcancel;
502
503	oldcancel = _thr_cancel_enter(curthread);
504	ret = __sys_sendmsg(s, m, f);
505	_thr_cancel_leave(curthread, oldcancel);
506	return (ret);
507}
508
509__weak_reference(__sendto, sendto);
510
511ssize_t
512__sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
513    socklen_t tl)
514{
515	struct pthread *curthread = _get_curthread();
516	ssize_t ret;
517	int oldcancel;
518
519	oldcancel = _thr_cancel_enter(curthread);
520	ret = __sys_sendto(s, m, l, f, t, tl);
521	_thr_cancel_leave(curthread, oldcancel);
522	return (ret);
523}
524
525__weak_reference(_sleep, sleep);
526
527unsigned int
528_sleep(unsigned int seconds)
529{
530	struct pthread *curthread = _get_curthread();
531	int		oldcancel;
532	unsigned int	ret;
533
534	oldcancel = _thr_cancel_enter(curthread);
535	ret = __sleep(seconds);
536	_thr_cancel_leave(curthread, oldcancel);
537
538	return (ret);
539}
540
541__weak_reference(_system, system);
542
543int
544_system(const char *string)
545{
546	struct pthread *curthread = _get_curthread();
547	int	oldcancel;
548	int	ret;
549
550	oldcancel = _thr_cancel_enter(curthread);
551	ret = __system(string);
552	_thr_cancel_leave(curthread, oldcancel);
553
554	return ret;
555}
556
557__weak_reference(_tcdrain, tcdrain);
558
559int
560_tcdrain(int fd)
561{
562	struct pthread *curthread = _get_curthread();
563	int	oldcancel;
564	int	ret;
565
566	oldcancel = _thr_cancel_enter(curthread);
567	ret = __tcdrain(fd);
568	_thr_cancel_leave(curthread, oldcancel);
569
570	return (ret);
571}
572
573__weak_reference(_usleep, usleep);
574
575int
576_usleep(useconds_t useconds)
577{
578	struct pthread *curthread = _get_curthread();
579	int		oldcancel;
580	int		ret;
581
582	oldcancel = _thr_cancel_enter(curthread);
583	ret = __usleep(useconds);
584	_thr_cancel_leave(curthread, oldcancel);
585
586	return (ret);
587}
588
589__weak_reference(_vfork, vfork);
590
591int
592_vfork(void)
593{
594	return (fork());
595}
596
597__weak_reference(_wait, wait);
598
599pid_t
600_wait(int *istat)
601{
602	struct pthread *curthread = _get_curthread();
603	int	oldcancel;
604	pid_t	ret;
605
606	oldcancel = _thr_cancel_enter(curthread);
607	ret = __wait(istat);
608	_thr_cancel_leave(curthread, oldcancel);
609
610	return ret;
611}
612
613__weak_reference(__wait4, wait4);
614
615pid_t
616__wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
617{
618	struct pthread *curthread = _get_curthread();
619	int oldcancel;
620	pid_t ret;
621
622	oldcancel = _thr_cancel_enter(curthread);
623	ret = __sys_wait4(pid, istat, options, rusage);
624	_thr_cancel_leave(curthread, oldcancel);
625
626	return ret;
627}
628
629__weak_reference(_waitpid, waitpid);
630
631pid_t
632_waitpid(pid_t wpid, int *status, int options)
633{
634	struct pthread *curthread = _get_curthread();
635	int	oldcancel;
636	pid_t	ret;
637
638	oldcancel = _thr_cancel_enter(curthread);
639	ret = __waitpid(wpid, status, options);
640	_thr_cancel_leave(curthread, oldcancel);
641
642	return ret;
643}
644
645__weak_reference(__write, write);
646
647ssize_t
648__write(int fd, const void *buf, size_t nbytes)
649{
650	struct pthread *curthread = _get_curthread();
651	int	oldcancel;
652	ssize_t	ret;
653
654	oldcancel = _thr_cancel_enter(curthread);
655	ret = __sys_write(fd, buf, nbytes);
656	_thr_cancel_leave(curthread, oldcancel);
657
658	return ret;
659}
660
661__weak_reference(__writev, writev);
662
663ssize_t
664__writev(int fd, const struct iovec *iov, int iovcnt)
665{
666	struct pthread *curthread = _get_curthread();
667	int	oldcancel;
668	ssize_t ret;
669
670	oldcancel = _thr_cancel_enter(curthread);
671	ret = __sys_writev(fd, iov, iovcnt);
672	_thr_cancel_leave(curthread, oldcancel);
673
674	return ret;
675}
676