adb5688a7b0e39f087fcbace55a32de1aa2e32d2
[musl] / src / aio / aio_readwrite.c
1 #include <aio.h>
2 #include <fcntl.h>
3 #include "pthread_impl.h"
4
5 static void dummy(void)
6 {
7 }
8
9 weak_alias(dummy, __aio_wake);
10
11 static void notify_signal(struct sigevent *sev)
12 {
13         siginfo_t si = {
14                 .si_signo = sev->sigev_signo,
15                 .si_value = sev->sigev_value,
16                 .si_code = SI_ASYNCIO,
17                 .si_pid = __pthread_self()->pid,
18                 .si_uid = getuid()
19         };
20         __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si);
21 }
22
23 static void *io_thread(void *p)
24 {
25         struct aiocb *cb = p;
26         int fd = cb->aio_filedes;
27         void *buf = (void *)cb->aio_buf;
28         size_t len = cb->aio_nbytes;
29         off_t off = cb->aio_offset;
30         int op = cb->aio_lio_opcode;
31         struct sigevent sev = cb->aio_sigevent;
32         ssize_t ret;
33
34         if (op == LIO_WRITE) {
35                 if (  (fcntl(fd, F_GETFL) & O_APPEND)
36                     ||((ret = pwrite(fd, buf, len, off))<0 && errno==ESPIPE) )
37                         ret = write(fd, buf, len);
38         } else if (op == LIO_READ) {
39                 if ( (ret = pread(fd, buf, len, off))<0 && errno==ESPIPE )
40                         ret = read(fd, buf, len);
41         } else {
42                 ret = 0;
43         }
44         cb->__ret = ret;
45
46         if (ret < 0) a_store(&cb->__err, errno);
47         else a_store(&cb->__err, 0);
48
49         __aio_wake();
50
51         switch (cb->aio_sigevent.sigev_notify) {
52         case SIGEV_SIGNAL:
53                 notify_signal(&sev);
54                 break;
55         case SIGEV_THREAD:
56                 sev.sigev_notify_function(sev.sigev_value);
57                 break;
58         }
59
60         return 0;
61 }
62
63 static int new_req(struct aiocb *cb)
64 {
65         int ret = 0;
66         pthread_attr_t a;
67         sigset_t set;
68         pthread_t td;
69
70         if (cb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
71                 if (cb->aio_sigevent.sigev_notify_attributes)
72                         a = *cb->aio_sigevent.sigev_notify_attributes;
73                 else
74                         pthread_attr_init(&a);
75         } else {
76                 pthread_attr_init(&a);
77                 pthread_attr_setstacksize(&a, PAGE_SIZE);
78                 pthread_attr_setguardsize(&a, 0);
79         }
80         pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
81         sigfillset(&set);
82         pthread_sigmask(SIG_BLOCK, &set, &set);
83         cb->__err = EINPROGRESS;
84         if (pthread_create(&td, &a, io_thread, cb)) {
85                 errno = EAGAIN;
86                 ret = -1;
87         }
88         pthread_sigmask(SIG_SETMASK, &set, 0);
89         cb->__td = td;
90
91         return ret;
92 }
93
94 int aio_read(struct aiocb *cb)
95 {
96         cb->aio_lio_opcode = LIO_READ;
97         return new_req(cb);
98 }
99
100 int aio_write(struct aiocb *cb)
101 {
102         cb->aio_lio_opcode = LIO_WRITE;
103         return new_req(cb);
104 }