]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
b3c4c760eb445f4ed6c188de08dd7bbad4cd2b22
[ccan] / ccan / io / io.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <poll.h>
12
13 void *io_loop_return;
14
15 #ifdef DEBUG
16 bool io_plan_for_other;
17 struct io_conn *current;
18 bool (*io_debug)(struct io_conn *conn);
19 bool io_debug_wakeup;
20
21 void io_plan_debug(struct io_plan *plan)
22 {
23         if (io_plan_for_other) {
24                 io_plan_for_other = false;
25                 return;
26         }
27
28         if (!io_debug || !current)
29                 return;
30
31         if (!io_debug(current) && !io_debug_wakeup)
32                 return;
33
34         io_debug_wakeup = false;
35         current->plan = *plan;
36         backend_plan_changed(current);
37
38         /* Call back into the loop immediately. */
39         io_loop_return = io_loop();
40 }
41
42 static void debug_io_wake(struct io_conn *conn)
43 {
44         /* We want linear if we wake a debugged connection, too. */
45         if (io_debug && io_debug(conn))
46                 io_debug_wakeup = true;
47 }
48 #else
49 static void debug_io_wake(struct io_conn *conn)
50 {
51 }
52 #endif
53
54 struct io_listener *io_new_listener_(int fd,
55                                      void (*init)(int fd, void *arg),
56                                      void *arg)
57 {
58         struct io_listener *l = malloc(sizeof(*l));
59
60         if (!l)
61                 return NULL;
62
63         l->fd.listener = true;
64         l->fd.fd = fd;
65         l->init = init;
66         l->arg = arg;
67         if (!add_listener(l)) {
68                 free(l);
69                 return NULL;
70         }
71         return l;
72 }
73
74 void io_close_listener(struct io_listener *l)
75 {
76         close(l->fd.fd);
77         del_listener(l);
78         free(l);
79 }
80
81 struct io_conn *io_new_conn_(int fd,
82                              struct io_plan plan,
83                              void (*finish)(struct io_conn *, void *),
84                              void *arg)
85 {
86         struct io_conn *conn = malloc(sizeof(*conn));
87
88         if (!conn)
89                 return NULL;
90
91         conn->fd.listener = false;
92         conn->fd.fd = fd;
93         conn->plan = plan;
94         conn->finish = finish;
95         conn->finish_arg = arg;
96         conn->duplex = NULL;
97         conn->timeout = NULL;
98         if (!add_conn(conn)) {
99                 free(conn);
100                 return NULL;
101         }
102         return conn;
103 }
104
105 struct io_conn *io_duplex_(struct io_conn *old,
106                            struct io_plan plan,
107                            void (*finish)(struct io_conn *, void *),
108                            void *arg)
109 {
110         struct io_conn *conn;
111
112         assert(!old->duplex);
113
114         conn = malloc(sizeof(*conn));
115         if (!conn)
116                 return NULL;
117
118         conn->fd.listener = false;
119         conn->fd.fd = old->fd.fd;
120         conn->plan = plan;
121         conn->duplex = old;
122         conn->finish = finish;
123         conn->finish_arg = arg;
124         conn->timeout = NULL;
125         if (!add_duplex(conn)) {
126                 free(conn);
127                 return NULL;
128         }
129         old->duplex = conn;
130         return conn;
131 }
132
133 bool io_timeout_(struct io_conn *conn, struct timespec ts,
134                  struct io_plan (*cb)(struct io_conn *, void *), void *arg)
135 {
136         assert(cb);
137
138         if (!conn->timeout) {
139                 conn->timeout = malloc(sizeof(*conn->timeout));
140                 if (!conn->timeout)
141                         return false;
142         } else
143                 assert(!timeout_active(conn));
144
145         conn->timeout->next = cb;
146         conn->timeout->next_arg = arg;
147         backend_add_timeout(conn, ts);
148         return true;
149 }
150
151 /* Returns true if we're finished. */
152 static bool do_write(int fd, struct io_plan *plan)
153 {
154         ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
155         if (ret < 0) {
156                 /* Override next function to close us. */
157                 plan->next = io_close;
158                 return true;
159         }
160
161         plan->u.write.buf += ret;
162         plan->u.write.len -= ret;
163         return (plan->u.write.len == 0);
164 }
165
166 /* Queue some data to be written. */
167 struct io_plan io_write_(const void *data, size_t len,
168                          struct io_plan (*cb)(struct io_conn *, void *),
169                          void *arg)
170 {
171         struct io_plan plan;
172
173         assert(cb);
174         plan.u.write.buf = data;
175         plan.u.write.len = len;
176         plan.io = do_write;
177         plan.next = cb;
178         plan.next_arg = arg;
179         plan.pollflag = POLLOUT;
180
181         io_plan_debug(&plan);
182         return plan;
183 }
184
185 static bool do_read(int fd, struct io_plan *plan)
186 {
187         ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
188         if (ret <= 0) {
189                 /* Override next function to close us. */
190                 plan->next = io_close;
191                 return true;
192         }
193
194         plan->u.read.buf += ret;
195         plan->u.read.len -= ret;
196         return (plan->u.read.len == 0);
197 }
198
199 /* Queue a request to read into a buffer. */
200 struct io_plan io_read_(void *data, size_t len,
201                         struct io_plan (*cb)(struct io_conn *, void *),
202                         void *arg)
203 {
204         struct io_plan plan;
205
206         assert(cb);
207         plan.u.read.buf = data;
208         plan.u.read.len = len;
209         plan.io = do_read;
210         plan.next = cb;
211         plan.next_arg = arg;
212         plan.pollflag = POLLIN;
213
214         io_plan_debug(&plan);
215         return plan;
216 }
217
218 static bool do_read_partial(int fd, struct io_plan *plan)
219 {
220         ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
221         if (ret <= 0) {
222                 /* Override next function to close us. */
223                 plan->next = io_close;
224                 return true;
225         }
226
227         *plan->u.readpart.lenp = ret;
228         return true;
229 }
230
231 /* Queue a partial request to read into a buffer. */
232 struct io_plan io_read_partial_(void *data, size_t *len,
233                                 struct io_plan (*cb)(struct io_conn *, void *),
234                                 void *arg)
235 {
236         struct io_plan plan;
237
238         assert(cb);
239         plan.u.readpart.buf = data;
240         plan.u.readpart.lenp = len;
241         plan.io = do_read_partial;
242         plan.next = cb;
243         plan.next_arg = arg;
244         plan.pollflag = POLLIN;
245
246         io_plan_debug(&plan);
247         return plan;
248 }
249
250 static bool do_write_partial(int fd, struct io_plan *plan)
251 {
252         ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
253         if (ret < 0) {
254                 /* Override next function to close us. */
255                 plan->next = io_close;
256                 return true;
257         }
258
259         *plan->u.writepart.lenp = ret;
260         return true;
261 }
262
263 /* Queue a partial write request. */
264 struct io_plan io_write_partial_(const void *data, size_t *len,
265                                  struct io_plan (*cb)(struct io_conn*, void *),
266                                  void *arg)
267 {
268         struct io_plan plan;
269
270         assert(cb);
271         plan.u.writepart.buf = data;
272         plan.u.writepart.lenp = len;
273         plan.io = do_write_partial;
274         plan.next = cb;
275         plan.next_arg = arg;
276         plan.pollflag = POLLOUT;
277
278         io_plan_debug(&plan);
279         return plan;
280 }
281
282 struct io_plan io_idle(void)
283 {
284         struct io_plan plan;
285
286         plan.pollflag = 0;
287         plan.io = NULL;
288         /* Never called (overridded by io_wake), but NULL means closing */
289         plan.next = io_close;
290
291         io_plan_debug(&plan);
292         return plan;
293 }
294
295 void io_wake_(struct io_conn *conn, struct io_plan plan)
296
297 {
298         /* It might be closing, but we haven't called its finish() yet. */
299         if (!conn->plan.next)
300                 return;
301         /* It was idle, right? */
302         assert(!conn->plan.io);
303         conn->plan = plan;
304         backend_plan_changed(conn);
305
306         debug_io_wake(conn);
307 }
308
309 void io_ready(struct io_conn *conn)
310 {
311         if (conn->plan.io(conn->fd.fd, &conn->plan)) {
312                 set_current(conn);
313                 if (timeout_active(conn))
314                         backend_del_timeout(conn);
315                 conn->plan = conn->plan.next(conn, conn->plan.next_arg);
316                 backend_plan_changed(conn);
317                 set_current(NULL);
318         }
319 }
320
321 /* Useful next functions. */
322 /* Close the connection, we're done. */
323 struct io_plan io_close(struct io_conn *conn, void *arg)
324 {
325         struct io_plan plan;
326
327         plan.pollflag = 0;
328         /* This means we're closing. */
329         plan.next = NULL;
330
331         io_plan_debug(&plan);
332         return plan;
333 }
334
335 /* Exit the loop, returning this (non-NULL) arg. */
336 struct io_plan io_break_(void *ret, struct io_plan plan)
337 {
338         assert(ret);
339         io_loop_return = ret;
340
341         return plan;
342 }