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