]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libthr/thread/thr_syscalls.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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_poll(struct pollfd *, unsigned, int);
108 extern ssize_t  __sys_recv(int, void *, size_t, int);
109 extern ssize_t  __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
110 extern ssize_t  __sys_recvmsg(int, struct msghdr *, int);
111 extern int      __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
112 extern int      __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
113                         off_t *, int);
114 extern ssize_t  __sys_sendmsg(int, const struct msghdr *, int);
115 extern ssize_t  __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
116 extern ssize_t  __sys_readv(int, const struct iovec *, int);
117 extern pid_t    __sys_wait4(pid_t, int *, int, struct rusage *);
118 extern ssize_t  __sys_writev(int, const struct iovec *, int);
119
120 int     ___creat(const char *, mode_t);
121 int     ___pselect(int, fd_set *, fd_set *, fd_set *, 
122                 const struct timespec *, const sigset_t *);
123 unsigned        ___sleep(unsigned);
124 int     ___system(const char *);
125 int     ___tcdrain(int);
126 int     ___usleep(useconds_t useconds);
127 pid_t   ___wait(int *);
128 pid_t   ___waitpid(pid_t, int *, int);
129 int     __accept(int, struct sockaddr *, socklen_t *);
130 int     __aio_suspend(const struct aiocb * const iocbs[], int,
131                 const struct timespec *);
132 int     __close(int);
133 int     __connect(int, const struct sockaddr *, socklen_t);
134 int     __fcntl(int, int,...);
135 #ifdef SYSCALL_COMPAT
136 extern int __fcntl_compat(int, int,...);
137 #endif
138 int     __fsync(int);
139 int     __msync(void *, size_t, int);
140 int     __nanosleep(const struct timespec *, struct timespec *);
141 int     __open(const char *, int,...);
142 int     __poll(struct pollfd *, unsigned int, int);
143 ssize_t __read(int, void *buf, size_t);
144 ssize_t __readv(int, const struct iovec *, int);
145 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
146 ssize_t __recvmsg(int, struct msghdr *, int);
147 int     __select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
148 ssize_t __sendmsg(int, const struct msghdr *, int);
149 ssize_t __sendto(int, const void *, size_t, int,
150                 const struct sockaddr *, socklen_t);
151 pid_t   __wait3(int *, int, struct rusage *);
152 pid_t   __wait4(pid_t, int *, int, struct rusage *);
153 ssize_t __write(int, const void *, size_t);
154 ssize_t __writev(int, const struct iovec *, int);
155
156 __weak_reference(__accept, accept);
157
158 int
159 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
160 {
161         struct pthread *curthread;
162         int ret;
163
164         curthread = _get_curthread();
165         _thr_cancel_enter(curthread);
166         ret = __sys_accept(s, addr, addrlen);
167         _thr_cancel_leave(curthread);
168
169         return (ret);
170 }
171
172 __weak_reference(__aio_suspend, aio_suspend);
173
174 int
175 __aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
176     timespec *timeout)
177 {
178         struct pthread *curthread = _get_curthread();
179         int ret;
180
181         _thr_cancel_enter(curthread);
182         ret = __sys_aio_suspend(iocbs, niocb, timeout);
183         _thr_cancel_leave(curthread);
184
185         return (ret);
186 }
187
188 __weak_reference(__close, close);
189
190 int
191 __close(int fd)
192 {
193         struct pthread  *curthread = _get_curthread();
194         int     ret;
195
196         _thr_cancel_enter(curthread);
197         ret = __sys_close(fd);
198         _thr_cancel_leave(curthread);
199         
200         return (ret);
201 }
202
203 __weak_reference(__connect, connect);
204
205 int
206 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
207 {
208         struct pthread *curthread = _get_curthread();
209         int ret;
210
211         _thr_cancel_enter(curthread);
212         ret = __sys_connect(fd, name, namelen);
213         _thr_cancel_leave(curthread);
214
215         return (ret);
216 }
217
218 __weak_reference(___creat, creat);
219
220 int
221 ___creat(const char *path, mode_t mode)
222 {
223         struct pthread *curthread = _get_curthread();
224         int ret;
225
226         _thr_cancel_enter(curthread);
227         ret = __creat(path, mode);
228         _thr_cancel_leave(curthread);
229         
230         return ret;
231 }
232
233 __weak_reference(__fcntl, fcntl);
234
235 int
236 __fcntl(int fd, int cmd,...)
237 {
238         struct pthread *curthread = _get_curthread();
239         int     ret;
240         va_list ap;
241         
242         _thr_cancel_enter(curthread);
243
244         va_start(ap, cmd);
245         switch (cmd) {
246         case F_DUPFD:
247                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
248                 break;
249         case F_SETFD:
250         case F_SETFL:
251                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
252                 break;
253         case F_GETFD:
254         case F_GETFL:
255                 ret = __sys_fcntl(fd, cmd);
256                 break;
257         default:
258 #ifdef SYSCALL_COMPAT
259                 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
260 #else
261                 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
262 #endif
263         }
264         va_end(ap);
265
266         _thr_cancel_leave(curthread);
267
268         return (ret);
269 }
270
271 __weak_reference(__fsync, fsync);
272
273 int
274 __fsync(int fd)
275 {
276         struct pthread *curthread = _get_curthread();
277         int     ret;
278
279         _thr_cancel_enter(curthread);
280         ret = __sys_fsync(fd);
281         _thr_cancel_leave(curthread);
282
283         return (ret);
284 }
285
286 __weak_reference(__msync, msync);
287
288 int
289 __msync(void *addr, size_t len, int flags)
290 {
291         struct pthread *curthread = _get_curthread();
292         int     ret;
293
294         _thr_cancel_enter(curthread);
295         ret = __sys_msync(addr, len, flags);
296         _thr_cancel_leave(curthread);
297
298         return ret;
299 }
300
301 __weak_reference(__nanosleep, nanosleep);
302
303 int
304 __nanosleep(const struct timespec *time_to_sleep,
305     struct timespec *time_remaining)
306 {
307         struct pthread *curthread = _get_curthread();
308         int             ret;
309
310         _thr_cancel_enter(curthread);
311         ret = __sys_nanosleep(time_to_sleep, time_remaining);
312         _thr_cancel_leave(curthread);
313
314         return (ret);
315 }
316
317 __weak_reference(__open, open);
318
319 int
320 __open(const char *path, int flags,...)
321 {
322         struct pthread *curthread = _get_curthread();
323         int     ret;
324         int     mode = 0;
325         va_list ap;
326
327         _thr_cancel_enter(curthread);
328         
329         /* Check if the file is being created: */
330         if (flags & O_CREAT) {
331                 /* Get the creation mode: */
332                 va_start(ap, flags);
333                 mode = va_arg(ap, int);
334                 va_end(ap);
335         }
336         
337         ret = __sys_open(path, flags, mode);
338
339         _thr_cancel_leave(curthread);
340
341         return ret;
342 }
343
344 __weak_reference(__poll, poll);
345
346 int
347 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
348 {
349         struct pthread *curthread = _get_curthread();
350         int ret;
351
352         _thr_cancel_enter(curthread);
353         ret = __sys_poll(fds, nfds, timeout);
354         _thr_cancel_leave(curthread);
355
356         return ret;
357 }
358
359 __weak_reference(___pselect, pselect);
360
361 int 
362 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 
363         const struct timespec *timo, const sigset_t *mask)
364 {
365         struct pthread *curthread = _get_curthread();
366         int ret;
367
368         _thr_cancel_enter(curthread);
369         ret = __pselect(count, rfds, wfds, efds, timo, mask);
370         _thr_cancel_leave(curthread);
371
372         return (ret);
373 }
374
375 __weak_reference(__read, read);
376
377 ssize_t
378 __read(int fd, void *buf, size_t nbytes)
379 {
380         struct pthread *curthread = _get_curthread();
381         ssize_t ret;
382
383         _thr_cancel_enter(curthread);
384         ret = __sys_read(fd, buf, nbytes);
385         _thr_cancel_leave(curthread);
386
387         return ret;
388 }
389
390 __weak_reference(__readv, readv);
391
392 ssize_t
393 __readv(int fd, const struct iovec *iov, int iovcnt)
394 {
395         struct pthread *curthread = _get_curthread();
396         ssize_t ret;
397
398         _thr_cancel_enter(curthread);
399         ret = __sys_readv(fd, iov, iovcnt);
400         _thr_cancel_leave(curthread);
401
402         return ret;
403 }
404
405 __weak_reference(__recvfrom, recvfrom);
406
407 ssize_t
408 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
409     socklen_t *fl)
410 {
411         struct pthread *curthread = _get_curthread();
412         ssize_t ret;
413
414         _thr_cancel_enter(curthread);
415         ret = __sys_recvfrom(s, b, l, f, from, fl);
416         _thr_cancel_leave(curthread);
417         return (ret);
418 }
419
420 __weak_reference(__recvmsg, recvmsg);
421
422 ssize_t
423 __recvmsg(int s, struct msghdr *m, int f)
424 {
425         struct pthread *curthread = _get_curthread();
426         ssize_t ret;
427
428         _thr_cancel_enter(curthread);
429         ret = __sys_recvmsg(s, m, f);
430         _thr_cancel_leave(curthread);
431         return (ret);
432 }
433
434 __weak_reference(__select, select);
435
436 int 
437 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
438         struct timeval *timeout)
439 {
440         struct pthread *curthread = _get_curthread();
441         int ret;
442
443         _thr_cancel_enter(curthread);
444         ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
445         _thr_cancel_leave(curthread);
446         return ret;
447 }
448
449 __weak_reference(__sendmsg, sendmsg);
450
451 ssize_t
452 __sendmsg(int s, const struct msghdr *m, int f)
453 {
454         struct pthread *curthread = _get_curthread();
455         ssize_t ret;
456
457         _thr_cancel_enter(curthread);
458         ret = __sys_sendmsg(s, m, f);
459         _thr_cancel_leave(curthread);
460         return (ret);
461 }
462
463 __weak_reference(__sendto, sendto);
464
465 ssize_t
466 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
467     socklen_t tl)
468 {
469         struct pthread *curthread = _get_curthread();
470         ssize_t ret;
471
472         _thr_cancel_enter(curthread);
473         ret = __sys_sendto(s, m, l, f, t, tl);
474         _thr_cancel_leave(curthread);
475         return (ret);
476 }
477
478 __weak_reference(___sleep, sleep);
479
480 unsigned int
481 ___sleep(unsigned int seconds)
482 {
483         struct pthread *curthread = _get_curthread();
484         unsigned int    ret;
485
486         _thr_cancel_enter(curthread);
487         ret = __sleep(seconds);
488         _thr_cancel_leave(curthread);
489         
490         return (ret);
491 }
492
493 __weak_reference(___system, system);
494
495 int
496 ___system(const char *string)
497 {
498         struct pthread *curthread = _get_curthread();
499         int     ret;
500
501         _thr_cancel_enter(curthread);
502         ret = __system(string);
503         _thr_cancel_leave(curthread);
504         
505         return ret;
506 }
507
508 __weak_reference(___tcdrain, tcdrain);
509
510 int
511 ___tcdrain(int fd)
512 {
513         struct pthread *curthread = _get_curthread();
514         int     ret;
515         
516         _thr_cancel_enter(curthread);
517         ret = __tcdrain(fd);
518         _thr_cancel_leave(curthread);
519
520         return (ret);
521 }
522
523 __weak_reference(___usleep, usleep);
524
525 int
526 ___usleep(useconds_t useconds)
527 {
528         struct pthread *curthread = _get_curthread();
529         int             ret;
530
531         _thr_cancel_enter(curthread);
532         ret = __usleep(useconds);
533         _thr_cancel_leave(curthread);
534         
535         return (ret);
536 }
537
538 __weak_reference(___wait, wait);
539
540 pid_t
541 ___wait(int *istat)
542 {
543         struct pthread *curthread = _get_curthread();
544         pid_t   ret;
545
546         _thr_cancel_enter(curthread);
547         ret = __wait(istat);
548         _thr_cancel_leave(curthread);
549
550         return ret;
551 }
552
553 __weak_reference(__wait3, wait3);
554
555 pid_t
556 __wait3(int *status, int options, struct rusage *rusage)
557 {
558         struct pthread *curthread = _get_curthread();
559         pid_t ret;
560
561         _thr_cancel_enter(curthread);
562         ret = _wait4(WAIT_ANY, status, options, rusage);
563         _thr_cancel_leave(curthread);
564
565         return (ret);
566 }
567
568 __weak_reference(__wait4, wait4);
569
570 pid_t
571 __wait4(pid_t pid, int *status, int options, struct rusage *rusage)
572 {
573         struct pthread *curthread = _get_curthread();
574         pid_t ret;
575
576         _thr_cancel_enter(curthread);
577         ret = __sys_wait4(pid, status, options, rusage);
578         _thr_cancel_leave(curthread);
579
580         return ret;
581 }
582
583 __weak_reference(___waitpid, waitpid);
584
585 pid_t
586 ___waitpid(pid_t wpid, int *status, int options)
587 {
588         struct pthread *curthread = _get_curthread();
589         pid_t   ret;
590
591         _thr_cancel_enter(curthread);
592         ret = __waitpid(wpid, status, options);
593         _thr_cancel_leave(curthread);
594         
595         return ret;
596 }
597
598 __weak_reference(__write, write);
599
600 ssize_t
601 __write(int fd, const void *buf, size_t nbytes)
602 {
603         struct pthread *curthread = _get_curthread();
604         ssize_t ret;
605
606         _thr_cancel_enter(curthread);
607         ret = __sys_write(fd, buf, nbytes);
608         _thr_cancel_leave(curthread);
609
610         return ret;
611 }
612
613 __weak_reference(__writev, writev);
614
615 ssize_t
616 __writev(int fd, const struct iovec *iov, int iovcnt)
617 {
618         struct pthread *curthread = _get_curthread();
619         ssize_t ret;
620
621         _thr_cancel_enter(curthread);
622         ret = __sys_writev(fd, iov, iovcnt);
623         _thr_cancel_leave(curthread);
624
625         return ret;
626 }