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