• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <aio.h>
2 #include <errno.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include "pthread_impl.h"
6 #include <unsupported_api.h>
7 
8 struct lio_state {
9 	struct sigevent *sev;
10 	int cnt;
11 	struct aiocb *cbs[];
12 };
13 
lio_wait(struct lio_state * st)14 static int lio_wait(struct lio_state *st)
15 {
16 	int i, err, got_err = 0;
17 	int cnt = st->cnt;
18 	struct aiocb **cbs = st->cbs;
19 
20 	for (;;) {
21 		for (i=0; i<cnt; i++) {
22 			if (!cbs[i]) continue;
23 			err = aio_error(cbs[i]);
24 			if (err==EINPROGRESS)
25 				break;
26 			if (err) got_err=1;
27 			cbs[i] = 0;
28 		}
29 		if (i==cnt) {
30 			if (got_err) {
31 				errno = EIO;
32 				return -1;
33 			}
34 			return 0;
35 		}
36 		if (aio_suspend((void *)cbs, cnt, 0))
37 			return -1;
38 	}
39 }
40 
notify_signal(struct sigevent * sev)41 static void notify_signal(struct sigevent *sev)
42 {
43 	siginfo_t si = {
44 		.si_signo = sev->sigev_signo,
45 		.si_value = sev->sigev_value,
46 		.si_code = SI_ASYNCIO,
47 		.si_pid = getpid(),
48 		.si_uid = getuid()
49 	};
50 	__syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si);
51 }
52 
wait_thread(void * p)53 static void *wait_thread(void *p)
54 {
55 	struct lio_state *st = p;
56 	struct sigevent *sev = st->sev;
57 	lio_wait(st);
58 	free(st);
59 	switch (sev->sigev_notify) {
60 	case SIGEV_SIGNAL:
61 		notify_signal(sev);
62 		break;
63 	case SIGEV_THREAD:
64 		sev->sigev_notify_function(sev->sigev_value);
65 		break;
66 	}
67 	return 0;
68 }
69 
lio_listio(int mode,struct aiocb * restrict const * restrict cbs,int cnt,struct sigevent * restrict sev)70 int lio_listio(int mode, struct aiocb *restrict const *restrict cbs, int cnt, struct sigevent *restrict sev)
71 {
72 	UNSUPPORTED_API_VOID(LITEOS_A);
73 	int i, ret;
74 	struct lio_state *st=0;
75 
76 	if (cnt < 0) {
77 		errno = EINVAL;
78 		return -1;
79 	}
80 
81 	if (mode == LIO_WAIT || (sev && sev->sigev_notify != SIGEV_NONE)) {
82 		if (!(st = malloc(sizeof *st + cnt*sizeof *cbs))) {
83 			errno = EAGAIN;
84 			return -1;
85 		}
86 		st->cnt = cnt;
87 		st->sev = sev;
88 		memcpy(st->cbs, (void*) cbs, cnt*sizeof *cbs);
89 	}
90 
91 	for (i=0; i<cnt; i++) {
92 		if (!cbs[i]) continue;
93 		switch (cbs[i]->aio_lio_opcode) {
94 		case LIO_READ:
95 			ret = aio_read(cbs[i]);
96 			break;
97 		case LIO_WRITE:
98 			ret = aio_write(cbs[i]);
99 			break;
100 		default:
101 			continue;
102 		}
103 		if (ret) {
104 			free(st);
105 			errno = EAGAIN;
106 			return -1;
107 		}
108 	}
109 
110 	if (mode == LIO_WAIT) {
111 		ret = lio_wait(st);
112 		free(st);
113 		return ret;
114 	}
115 
116 	if (st) {
117 		pthread_attr_t a;
118 		sigset_t set, set_old;
119 		pthread_t td;
120 
121 		if (sev->sigev_notify == SIGEV_THREAD) {
122 			if (sev->sigev_notify_attributes)
123 				a = *sev->sigev_notify_attributes;
124 			else
125 				pthread_attr_init(&a);
126 		} else {
127 			pthread_attr_init(&a);
128 			pthread_attr_setstacksize(&a, PAGE_SIZE);
129 			pthread_attr_setguardsize(&a, 0);
130 		}
131 		pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
132 		sigfillset(&set);
133 		pthread_sigmask(SIG_BLOCK, &set, &set_old);
134 		if (pthread_create(&td, &a, wait_thread, st)) {
135 			free(st);
136 			errno = EAGAIN;
137 			return -1;
138 		}
139 		pthread_sigmask(SIG_SETMASK, &set_old, 0);
140 	}
141 
142 	return 0;
143 }
144 
145 weak_alias(lio_listio, lio_listio64);
146