]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libthr/thread/thr_syscalls.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libthr / thread / thr_syscalls.c
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$
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. Neither the name of the author nor the names of any co-contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  */
63
64 #include "namespace.h"
65 #include <sys/types.h>
66 #include <sys/mman.h>
67 #include <sys/param.h>
68 #include <sys/select.h>
69 #include <sys/signalvar.h>
70 #include <sys/socket.h>
71 #include <sys/stat.h>
72 #include <sys/time.h>
73 #include <sys/uio.h>
74 #include <sys/wait.h>
75 #include <aio.h>
76 #include <dirent.h>
77 #include <errno.h>
78 #include <fcntl.h>
79 #include <poll.h>
80 #include <signal.h>
81 #include <stdarg.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <termios.h>
86 #include <unistd.h>
87 #include <pthread.h>
88 #include "un-namespace.h"
89
90 #include "thr_private.h"
91
92 extern int      __creat(const char *, mode_t);
93 extern int      __pselect(int, fd_set *, fd_set *, fd_set *,
94                         const struct timespec *, const sigset_t *);
95 extern unsigned __sleep(unsigned int);
96 extern int      __system(const char *);
97 extern int      __tcdrain(int);
98 extern int      __usleep(useconds_t);
99 extern pid_t    __wait(int *);
100 extern pid_t    __waitpid(pid_t, int *, int);
101 extern int      __sys_aio_suspend(const struct aiocb * const[], int,
102                         const struct timespec *);
103 extern int      __sys_accept(int, struct sockaddr *, socklen_t *);
104 extern int      __sys_connect(int, const struct sockaddr *, socklen_t);
105 extern int      __sys_fsync(int);
106 extern int      __sys_msync(void *, size_t, int);
107 extern int      __sys_pselect(int, fd_set *, fd_set *, fd_set *,
108                         const struct timespec *, const sigset_t *);
109 extern int      __sys_poll(struct pollfd *, unsigned, int);
110 extern ssize_t  __sys_recv(int, void *, size_t, int);
111 extern ssize_t  __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
112 extern ssize_t  __sys_recvmsg(int, struct msghdr *, int);
113 extern int      __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
114 extern int      __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
115                         off_t *, int);
116 extern ssize_t  __sys_sendmsg(int, const struct msghdr *, int);
117 extern ssize_t  __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
118 extern ssize_t  __sys_readv(int, const struct iovec *, int);
119 extern pid_t    __sys_wait4(pid_t, int *, int, struct rusage *);
120 extern ssize_t  __sys_writev(int, const struct iovec *, int);
121
122 int     ___creat(const char *, mode_t);
123 int     ___pselect(int, fd_set *, fd_set *, fd_set *, 
124                 const struct timespec *, const sigset_t *);
125 unsigned        ___sleep(unsigned);
126 int     ___system(const char *);
127 int     ___tcdrain(int);
128 int     ___usleep(useconds_t useconds);
129 pid_t   ___wait(int *);
130 pid_t   ___waitpid(pid_t, int *, int);
131 int     __accept(int, struct sockaddr *, socklen_t *);
132 int     __aio_suspend(const struct aiocb * const iocbs[], int,
133                 const struct timespec *);
134 int     __close(int);
135 int     __connect(int, const struct sockaddr *, socklen_t);
136 int     __fcntl(int, int,...);
137 #ifdef SYSCALL_COMPAT
138 extern int __fcntl_compat(int, int,...);
139 #endif
140 int     __fsync(int);
141 int     __msync(void *, size_t, int);
142 int     __nanosleep(const struct timespec *, struct timespec *);
143 int     __open(const char *, int,...);
144 int     __poll(struct pollfd *, unsigned int, int);
145 ssize_t __read(int, void *buf, size_t);
146 ssize_t __readv(int, const struct iovec *, int);
147 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
148 ssize_t __recvmsg(int, struct msghdr *, int);
149 int     __select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
150 ssize_t __sendmsg(int, const struct msghdr *, int);
151 ssize_t __sendto(int, const void *, size_t, int,
152                 const struct sockaddr *, socklen_t);
153 pid_t   __wait3(int *, int, struct rusage *);
154 pid_t   __wait4(pid_t, int *, int, struct rusage *);
155 ssize_t __write(int, const void *, size_t);
156 ssize_t __writev(int, const struct iovec *, int);
157
158 __weak_reference(__accept, accept);
159
160 int
161 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
162 {
163         struct pthread *curthread;
164         int ret;
165
166         curthread = _get_curthread();
167         _thr_cancel_enter(curthread);
168         ret = __sys_accept(s, addr, addrlen);
169         _thr_cancel_leave(curthread);
170
171         return (ret);
172 }
173
174 __weak_reference(__aio_suspend, aio_suspend);
175
176 int
177 __aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
178     timespec *timeout)
179 {
180         struct pthread *curthread = _get_curthread();
181         int ret;
182
183         _thr_cancel_enter(curthread);
184         ret = __sys_aio_suspend(iocbs, niocb, timeout);
185         _thr_cancel_leave(curthread);
186
187         return (ret);
188 }
189
190 __weak_reference(__close, close);
191
192 int
193 __close(int fd)
194 {
195         struct pthread  *curthread = _get_curthread();
196         int     ret;
197
198         _thr_cancel_enter(curthread);
199         ret = __sys_close(fd);
200         _thr_cancel_leave(curthread);
201         
202         return (ret);
203 }
204
205 __weak_reference(__connect, connect);
206
207 int
208 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
209 {
210         struct pthread *curthread = _get_curthread();
211         int ret;
212
213         _thr_cancel_enter(curthread);
214         ret = __sys_connect(fd, name, namelen);
215         _thr_cancel_leave(curthread);
216
217         return (ret);
218 }
219
220 __weak_reference(___creat, creat);
221
222 int
223 ___creat(const char *path, mode_t mode)
224 {
225         struct pthread *curthread = _get_curthread();
226         int ret;
227
228         _thr_cancel_enter(curthread);
229         ret = __creat(path, mode);
230         _thr_cancel_leave(curthread);
231         
232         return ret;
233 }
234
235 __weak_reference(__fcntl, fcntl);
236
237 int
238 __fcntl(int fd, int cmd,...)
239 {
240         struct pthread *curthread = _get_curthread();
241         int     ret;
242         va_list ap;
243         
244         _thr_cancel_enter(curthread);
245
246         va_start(ap, cmd);
247         switch (cmd) {
248         case F_DUPFD:
249                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
250                 break;
251         case F_SETFD:
252         case F_SETFL:
253                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
254                 break;
255         case F_GETFD:
256         case F_GETFL:
257                 ret = __sys_fcntl(fd, cmd);
258                 break;
259         default:
260 #ifdef SYSCALL_COMPAT
261                 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
262 #else
263                 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
264 #endif
265         }
266         va_end(ap);
267
268         _thr_cancel_leave(curthread);
269
270         return (ret);
271 }
272
273 __weak_reference(__fsync, fsync);
274
275 int
276 __fsync(int fd)
277 {
278         struct pthread *curthread = _get_curthread();
279         int     ret;
280
281         _thr_cancel_enter(curthread);
282         ret = __sys_fsync(fd);
283         _thr_cancel_leave(curthread);
284
285         return (ret);
286 }
287
288 __weak_reference(__msync, msync);
289
290 int
291 __msync(void *addr, size_t len, int flags)
292 {
293         struct pthread *curthread = _get_curthread();
294         int     ret;
295
296         _thr_cancel_enter(curthread);
297         ret = __sys_msync(addr, len, flags);
298         _thr_cancel_leave(curthread);
299
300         return ret;
301 }
302
303 __weak_reference(__nanosleep, nanosleep);
304
305 int
306 __nanosleep(const struct timespec *time_to_sleep,
307     struct timespec *time_remaining)
308 {
309         struct pthread *curthread = _get_curthread();
310         int             ret;
311
312         _thr_cancel_enter(curthread);
313         ret = __sys_nanosleep(time_to_sleep, time_remaining);
314         _thr_cancel_leave(curthread);
315
316         return (ret);
317 }
318
319 __weak_reference(__open, open);
320
321 int
322 __open(const char *path, int flags,...)
323 {
324         struct pthread *curthread = _get_curthread();
325         int     ret;
326         int     mode = 0;
327         va_list ap;
328
329         _thr_cancel_enter(curthread);
330         
331         /* Check if the file is being created: */
332         if (flags & O_CREAT) {
333                 /* Get the creation mode: */
334                 va_start(ap, flags);
335                 mode = va_arg(ap, int);
336                 va_end(ap);
337         }
338         
339         ret = __sys_open(path, flags, mode);
340
341         _thr_cancel_leave(curthread);
342
343         return ret;
344 }
345
346 __weak_reference(__poll, poll);
347
348 int
349 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
350 {
351         struct pthread *curthread = _get_curthread();
352         int ret;
353
354         _thr_cancel_enter(curthread);
355         ret = __sys_poll(fds, nfds, timeout);
356         _thr_cancel_leave(curthread);
357
358         return ret;
359 }
360
361 __weak_reference(___pselect, pselect);
362
363 int 
364 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 
365         const struct timespec *timo, const sigset_t *mask)
366 {
367         struct pthread *curthread = _get_curthread();
368         int ret;
369
370         _thr_cancel_enter(curthread);
371         ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
372         _thr_cancel_leave(curthread);
373
374         return (ret);
375 }
376
377 __weak_reference(__read, read);
378
379 ssize_t
380 __read(int fd, void *buf, size_t nbytes)
381 {
382         struct pthread *curthread = _get_curthread();
383         ssize_t ret;
384
385         _thr_cancel_enter(curthread);
386         ret = __sys_read(fd, buf, nbytes);
387         _thr_cancel_leave(curthread);
388
389         return ret;
390 }
391
392 __weak_reference(__readv, readv);
393
394 ssize_t
395 __readv(int fd, const struct iovec *iov, int iovcnt)
396 {
397         struct pthread *curthread = _get_curthread();
398         ssize_t ret;
399
400         _thr_cancel_enter(curthread);
401         ret = __sys_readv(fd, iov, iovcnt);
402         _thr_cancel_leave(curthread);
403
404         return ret;
405 }
406
407 __weak_reference(__recvfrom, recvfrom);
408
409 ssize_t
410 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
411     socklen_t *fl)
412 {
413         struct pthread *curthread = _get_curthread();
414         ssize_t ret;
415
416         _thr_cancel_enter(curthread);
417         ret = __sys_recvfrom(s, b, l, f, from, fl);
418         _thr_cancel_leave(curthread);
419         return (ret);
420 }
421
422 __weak_reference(__recvmsg, recvmsg);
423
424 ssize_t
425 __recvmsg(int s, struct msghdr *m, int f)
426 {
427         struct pthread *curthread = _get_curthread();
428         ssize_t ret;
429
430         _thr_cancel_enter(curthread);
431         ret = __sys_recvmsg(s, m, f);
432         _thr_cancel_leave(curthread);
433         return (ret);
434 }
435
436 __weak_reference(__select, select);
437
438 int 
439 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
440         struct timeval *timeout)
441 {
442         struct pthread *curthread = _get_curthread();
443         int ret;
444
445         _thr_cancel_enter(curthread);
446         ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
447         _thr_cancel_leave(curthread);
448         return ret;
449 }
450
451 __weak_reference(__sendmsg, sendmsg);
452
453 ssize_t
454 __sendmsg(int s, const struct msghdr *m, int f)
455 {
456         struct pthread *curthread = _get_curthread();
457         ssize_t ret;
458
459         _thr_cancel_enter(curthread);
460         ret = __sys_sendmsg(s, m, f);
461         _thr_cancel_leave(curthread);
462         return (ret);
463 }
464
465 __weak_reference(__sendto, sendto);
466
467 ssize_t
468 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
469     socklen_t tl)
470 {
471         struct pthread *curthread = _get_curthread();
472         ssize_t ret;
473
474         _thr_cancel_enter(curthread);
475         ret = __sys_sendto(s, m, l, f, t, tl);
476         _thr_cancel_leave(curthread);
477         return (ret);
478 }
479
480 __weak_reference(___sleep, sleep);
481
482 unsigned int
483 ___sleep(unsigned int seconds)
484 {
485         struct pthread *curthread = _get_curthread();
486         unsigned int    ret;
487
488         _thr_cancel_enter(curthread);
489         ret = __sleep(seconds);
490         _thr_cancel_leave(curthread);
491         
492         return (ret);
493 }
494
495 __weak_reference(___system, system);
496
497 int
498 ___system(const char *string)
499 {
500         struct pthread *curthread = _get_curthread();
501         int     ret;
502
503         _thr_cancel_enter(curthread);
504         ret = __system(string);
505         _thr_cancel_leave(curthread);
506         
507         return ret;
508 }
509
510 __weak_reference(___tcdrain, tcdrain);
511
512 int
513 ___tcdrain(int fd)
514 {
515         struct pthread *curthread = _get_curthread();
516         int     ret;
517         
518         _thr_cancel_enter(curthread);
519         ret = __tcdrain(fd);
520         _thr_cancel_leave(curthread);
521
522         return (ret);
523 }
524
525 __weak_reference(___usleep, usleep);
526
527 int
528 ___usleep(useconds_t useconds)
529 {
530         struct pthread *curthread = _get_curthread();
531         int             ret;
532
533         _thr_cancel_enter(curthread);
534         ret = __usleep(useconds);
535         _thr_cancel_leave(curthread);
536         
537         return (ret);
538 }
539
540 __weak_reference(___wait, wait);
541
542 pid_t
543 ___wait(int *istat)
544 {
545         struct pthread *curthread = _get_curthread();
546         pid_t   ret;
547
548         _thr_cancel_enter(curthread);
549         ret = __wait(istat);
550         _thr_cancel_leave(curthread);
551
552         return ret;
553 }
554
555 __weak_reference(__wait3, wait3);
556
557 pid_t
558 __wait3(int *status, int options, struct rusage *rusage)
559 {
560         struct pthread *curthread = _get_curthread();
561         pid_t ret;
562
563         _thr_cancel_enter(curthread);
564         ret = _wait4(WAIT_ANY, status, options, rusage);
565         _thr_cancel_leave(curthread);
566
567         return (ret);
568 }
569
570 __weak_reference(__wait4, wait4);
571
572 pid_t
573 __wait4(pid_t pid, int *status, int options, struct rusage *rusage)
574 {
575         struct pthread *curthread = _get_curthread();
576         pid_t ret;
577
578         _thr_cancel_enter(curthread);
579         ret = __sys_wait4(pid, status, options, rusage);
580         _thr_cancel_leave(curthread);
581
582         return ret;
583 }
584
585 __weak_reference(___waitpid, waitpid);
586
587 pid_t
588 ___waitpid(pid_t wpid, int *status, int options)
589 {
590         struct pthread *curthread = _get_curthread();
591         pid_t   ret;
592
593         _thr_cancel_enter(curthread);
594         ret = __waitpid(wpid, status, options);
595         _thr_cancel_leave(curthread);
596         
597         return ret;
598 }
599
600 __weak_reference(__write, write);
601
602 ssize_t
603 __write(int fd, const void *buf, size_t nbytes)
604 {
605         struct pthread *curthread = _get_curthread();
606         ssize_t ret;
607
608         _thr_cancel_enter(curthread);
609         ret = __sys_write(fd, buf, nbytes);
610         _thr_cancel_leave(curthread);
611
612         return ret;
613 }
614
615 __weak_reference(__writev, writev);
616
617 ssize_t
618 __writev(int fd, const struct iovec *iov, int iovcnt)
619 {
620         struct pthread *curthread = _get_curthread();
621         ssize_t ret;
622
623         _thr_cancel_enter(curthread);
624         ret = __sys_writev(fd, iov, iovcnt);
625         _thr_cancel_leave(curthread);
626
627         return ret;
628 }