Merge from vendor branch GROFF:
[dragonfly.git] / lib / libthread_xu / 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  * $DragonFly: src/lib/libthread_xu/thread/thr_syscalls.c,v 1.3 2005/03/29 19:26:20 joerg Exp $
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 <sys/types.h>
68 #include <sys/mman.h>
69 #include <sys/param.h>
70 #include <sys/select.h>
71 #include <sys/signalvar.h>
72 #include <sys/socket.h>
73 #include <sys/stat.h>
74 #include <sys/time.h>
75 #include <sys/uio.h>
76 #include <sys/wait.h>
77
78 #include <machine/tls.h>
79
80 #include <aio.h>
81 #include <dirent.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <poll.h>
85 #include <signal.h>
86 #include <stdarg.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <termios.h>
91 #include <unistd.h>
92 #include <pthread.h>
93
94 #include "thr_private.h"
95
96 extern int __creat(const char *, mode_t);
97 extern int __pause(void);
98 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
99                 const struct timespec *timo, const sigset_t *mask);
100 extern unsigned int __sleep(unsigned int);
101 extern int __system(const char *);
102 extern int __tcdrain(int);
103 extern pid_t __wait(int *);
104 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
105 extern pid_t __waitpid(pid_t, int *, int);
106
107 __weak_reference(__accept, accept);
108 int
109 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
110 {
111         struct pthread *curthread;
112         int oldcancel;
113         int ret;
114
115         curthread = tls_get_curthread();
116         oldcancel = _thr_cancel_enter(curthread);
117         ret = __sys_accept(s, addr, addrlen);
118         _thr_cancel_leave(curthread, oldcancel);
119
120         return (ret);
121 }
122
123 __weak_reference(_aio_suspend, aio_suspend);
124
125 int
126 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
127     timespec *timeout)
128 {
129         struct pthread *curthread = tls_get_curthread();
130         int oldcancel;
131         int ret;
132
133         oldcancel = _thr_cancel_enter(curthread);
134         ret = __sys_aio_suspend(iocbs, niocb, timeout);
135         _thr_cancel_leave(curthread, oldcancel);
136
137         return (ret);
138 }
139
140 __weak_reference(__close, close);
141
142 int
143 __close(int fd)
144 {
145         struct pthread  *curthread = tls_get_curthread();
146         int     oldcancel;
147         int     ret;
148
149         oldcancel = _thr_cancel_enter(curthread);
150         ret = __sys_close(fd);
151         _thr_cancel_leave(curthread, oldcancel);
152         
153         return (ret);
154 }
155
156 __weak_reference(__connect, connect);
157
158 int
159 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
160 {
161         struct pthread *curthread = tls_get_curthread();
162         int oldcancel;
163         int ret;
164
165         curthread = tls_get_curthread();
166         oldcancel = _thr_cancel_enter(curthread);
167         ret = __sys_connect(fd, name, namelen);
168         _thr_cancel_leave(curthread, oldcancel);
169
170         return (ret);
171 }
172
173 __weak_reference(___creat, creat);
174
175 int
176 ___creat(const char *path, mode_t mode)
177 {
178         struct pthread *curthread = tls_get_curthread();
179         int oldcancel;
180         int ret;
181
182         oldcancel = _thr_cancel_enter(curthread);
183         ret = __creat(path, mode);
184         _thr_cancel_leave(curthread, oldcancel);
185         
186         return ret;
187 }
188
189 __weak_reference(__fcntl, fcntl);
190
191 int
192 __fcntl(int fd, int cmd,...)
193 {
194         struct pthread *curthread = tls_get_curthread();
195         int     oldcancel;
196         int     ret;
197         va_list ap;
198         
199         oldcancel = _thr_cancel_enter(curthread);
200
201         va_start(ap, cmd);
202         switch (cmd) {
203         case F_DUPFD:
204                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
205                 break;
206         case F_SETFD:
207         case F_SETFL:
208                 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
209                 break;
210         case F_GETFD:
211         case F_GETFL:
212                 ret = __sys_fcntl(fd, cmd);
213                 break;
214         default:
215                 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
216         }
217         va_end(ap);
218
219         _thr_cancel_leave(curthread, oldcancel);
220
221         return (ret);
222 }
223
224 __weak_reference(__fsync, fsync);
225
226 int
227 __fsync(int fd)
228 {
229         struct pthread *curthread = tls_get_curthread();
230         int     oldcancel;
231         int     ret;
232
233         oldcancel = _thr_cancel_enter(curthread);
234         ret = __sys_fsync(fd);
235         _thr_cancel_leave(curthread, oldcancel);
236
237         return (ret);
238 }
239
240 __weak_reference(__msync, msync);
241
242 int
243 __msync(void *addr, size_t len, int flags)
244 {
245         struct pthread *curthread = tls_get_curthread();
246         int     oldcancel;
247         int     ret;
248
249         oldcancel = _thr_cancel_enter(curthread);
250         ret = __sys_msync(addr, len, flags);
251         _thr_cancel_leave(curthread, oldcancel);
252
253         return ret;
254 }
255
256 __weak_reference(__nanosleep, nanosleep);
257
258 int
259 __nanosleep(const struct timespec *time_to_sleep,
260     struct timespec *time_remaining)
261 {
262         struct pthread *curthread = tls_get_curthread();
263         int             oldcancel;
264         int             ret;
265
266         oldcancel = _thr_cancel_enter(curthread);
267         ret = __sys_nanosleep(time_to_sleep, time_remaining);
268         _thr_cancel_leave(curthread, oldcancel);
269
270         return (ret);
271 }
272
273 __weak_reference(___open, open);
274
275 int
276 ___open(const char *path, int flags,...)
277 {
278         struct pthread *curthread = tls_get_curthread();
279         int     oldcancel;
280         int     ret;
281         int     mode = 0;
282         va_list ap;
283
284         oldcancel = _thr_cancel_enter(curthread);
285         
286         /* Check if the file is being created: */
287         if (flags & O_CREAT) {
288                 /* Get the creation mode: */
289                 va_start(ap, flags);
290                 mode = va_arg(ap, int);
291                 va_end(ap);
292         }
293         
294         ret = __sys_open(path, flags, mode);
295
296         _thr_cancel_leave(curthread, oldcancel);
297
298         return ret;
299 }
300
301 __weak_reference(_pause, pause);
302
303 int
304 _pause(void)
305 {
306         struct pthread *curthread = tls_get_curthread();
307         int     oldcancel;
308         int     ret;
309
310         oldcancel = _thr_cancel_enter(curthread);
311         ret = __pause();
312         _thr_cancel_leave(curthread, oldcancel);
313         
314         return ret;
315 }
316
317 __weak_reference(__poll, poll);
318
319 int
320 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
321 {
322         struct pthread *curthread = tls_get_curthread();
323         int oldcancel;
324         int ret;
325
326         oldcancel = _thr_cancel_enter(curthread);
327         ret = __sys_poll(fds, nfds, timeout);
328         _thr_cancel_leave(curthread, oldcancel);
329
330         return ret;
331 }
332
333 #if 0
334 __weak_reference(_pselect, pselect);
335
336 int 
337 _pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 
338         const struct timespec *timo, const sigset_t *mask)
339 {
340         struct pthread *curthread = tls_get_curthread();
341         int oldcancel;
342         int ret;
343
344         oldcancel = _thr_cancel_enter(curthread);
345         ret = __pselect(count, rfds, wfds, efds, timo, mask);
346         _thr_cancel_leave(curthread, oldcancel);
347
348         return (ret);
349 }
350 #endif
351
352 __weak_reference(_raise, raise);
353
354 int
355 _raise(int sig)
356 {
357         int ret;
358
359         if (!_thr_isthreaded())
360                 ret = kill(getpid(), sig);
361         else {
362                 ret = pthread_kill(pthread_self(), sig);
363                 if (ret != 0) {
364                         errno = ret;
365                         ret = -1;
366                 }
367         }
368         return (ret);
369 }
370
371 __weak_reference(__read, read);
372
373 ssize_t
374 __read(int fd, void *buf, size_t nbytes)
375 {
376         struct pthread *curthread = tls_get_curthread();
377         int oldcancel;
378         ssize_t ret;
379
380         oldcancel = _thr_cancel_enter(curthread);
381         ret = __sys_read(fd, buf, nbytes);
382         _thr_cancel_leave(curthread, oldcancel);
383
384         return ret;
385 }
386
387 __weak_reference(__readv, readv);
388
389 ssize_t
390 __readv(int fd, const struct iovec *iov, int iovcnt)
391 {
392         struct pthread *curthread = tls_get_curthread();
393         int oldcancel;
394         ssize_t ret;
395
396         oldcancel = _thr_cancel_enter(curthread);
397         ret = __sys_readv(fd, iov, iovcnt);
398         _thr_cancel_leave(curthread, oldcancel);
399
400         return ret;
401 }
402
403 __weak_reference(__recvfrom, recvfrom);
404
405 ssize_t
406 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
407     socklen_t *fl)
408 {
409         struct pthread *curthread = tls_get_curthread();
410         int oldcancel;
411         ssize_t ret;
412
413         oldcancel = _thr_cancel_enter(curthread);
414         ret = __sys_recvfrom(s, b, l, f, from, fl);
415         _thr_cancel_leave(curthread, oldcancel);
416         return (ret);
417 }
418
419 __weak_reference(__recvmsg, recvmsg);
420
421 ssize_t
422 __recvmsg(int s, struct msghdr *m, int f)
423 {
424         struct pthread *curthread = tls_get_curthread();
425         ssize_t ret;
426         int oldcancel;
427
428         oldcancel = _thr_cancel_enter(curthread);
429         ret = __sys_recvmsg(s, m, f);
430         _thr_cancel_leave(curthread, oldcancel);
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 = tls_get_curthread();
441         int oldcancel;
442         int ret;
443
444         oldcancel = _thr_cancel_enter(curthread);
445         ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
446         _thr_cancel_leave(curthread, oldcancel);
447         return ret;
448 }
449
450 __weak_reference(__sendmsg, sendmsg);
451
452 ssize_t
453 __sendmsg(int s, const struct msghdr *m, int f)
454 {
455         struct pthread *curthread = tls_get_curthread();
456         ssize_t ret;
457         int oldcancel;
458
459         oldcancel = _thr_cancel_enter(curthread);
460         ret = __sys_sendmsg(s, m, f);
461         _thr_cancel_leave(curthread, oldcancel);
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 = tls_get_curthread();
472         ssize_t ret;
473         int oldcancel;
474
475         oldcancel = _thr_cancel_enter(curthread);
476         ret = __sys_sendto(s, m, l, f, t, tl);
477         _thr_cancel_leave(curthread, oldcancel);
478         return (ret);
479 }
480
481 unsigned int
482 _sleep(unsigned int seconds)
483 {
484         struct pthread *curthread = tls_get_curthread();
485         int             oldcancel;
486         unsigned int    ret;
487
488         oldcancel = _thr_cancel_enter(curthread);
489         ret = __sleep(seconds);
490         _thr_cancel_leave(curthread, oldcancel);
491         
492         return (ret);
493 }
494
495 __weak_reference(_system, system);
496
497 int
498 _system(const char *string)
499 {
500         struct pthread *curthread = tls_get_curthread();
501         int     oldcancel;
502         int     ret;
503
504         oldcancel = _thr_cancel_enter(curthread);
505         ret = __system(string);
506         _thr_cancel_leave(curthread, oldcancel);
507         
508         return ret;
509 }
510
511 __weak_reference(_tcdrain, tcdrain);
512
513 int
514 _tcdrain(int fd)
515 {
516         struct pthread *curthread = tls_get_curthread();
517         int     oldcancel;
518         int     ret;
519         
520         oldcancel = _thr_cancel_enter(curthread);
521         ret = __tcdrain(fd);
522         _thr_cancel_leave(curthread, oldcancel);
523
524         return (ret);
525 }
526
527 __weak_reference(_vfork, vfork);
528
529 int
530 _vfork(void)
531 {
532         return (fork());
533 }
534
535 __weak_reference(_wait, wait);
536
537 pid_t
538 _wait(int *istat)
539 {
540         struct pthread *curthread = tls_get_curthread();
541         int     oldcancel;
542         pid_t   ret;
543
544         oldcancel = _thr_cancel_enter(curthread);
545         ret = __wait(istat);
546         _thr_cancel_leave(curthread, oldcancel);
547
548         return ret;
549 }
550
551 __weak_reference(__wait4, wait4);
552
553 pid_t
554 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
555 {
556         struct pthread *curthread = tls_get_curthread();
557         int oldcancel;
558         pid_t ret;
559
560         oldcancel = _thr_cancel_enter(curthread);
561         ret = __sys_wait4(pid, istat, options, rusage);
562         _thr_cancel_leave(curthread, oldcancel);
563
564         return ret;
565 }
566
567 __weak_reference(_waitpid, waitpid);
568
569 pid_t
570 _waitpid(pid_t wpid, int *status, int options)
571 {
572         struct pthread *curthread = tls_get_curthread();
573         int     oldcancel;
574         pid_t   ret;
575
576         oldcancel = _thr_cancel_enter(curthread);
577         ret = __waitpid(wpid, status, options);
578         _thr_cancel_leave(curthread, oldcancel);
579         
580         return ret;
581 }
582
583 __weak_reference(__write, write);
584
585 ssize_t
586 __write(int fd, const void *buf, size_t nbytes)
587 {
588         struct pthread *curthread = tls_get_curthread();
589         int     oldcancel;
590         ssize_t ret;
591
592         oldcancel = _thr_cancel_enter(curthread);
593         ret = __sys_write(fd, buf, nbytes);
594         _thr_cancel_leave(curthread, oldcancel);
595
596         return ret;
597 }
598
599 __weak_reference(__writev, writev);
600
601 ssize_t
602 __writev(int fd, const struct iovec *iov, int iovcnt)
603 {
604         struct pthread *curthread = tls_get_curthread();
605         int     oldcancel;
606         ssize_t ret;
607
608         oldcancel = _thr_cancel_enter(curthread);
609         ret = __sys_writev(fd, iov, iovcnt);
610         _thr_cancel_leave(curthread, oldcancel);
611
612         return ret;
613 }