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