]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
52f4368ca11533f739fea3bf480571a80d09af90
[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 <unistd.h>
12 #include <fcntl.h>
13
14 void *io_loop_return;
15
16 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
17                                      struct io_plan *(*init)(struct io_conn *,
18                                                              void *),
19                                      void *arg)
20 {
21         struct io_listener *l = tal(ctx, struct io_listener);
22         if (!l)
23                 return NULL;
24
25         l->fd.listener = true;
26         l->fd.fd = fd;
27         l->init = init;
28         l->arg = arg;
29         l->ctx = ctx;
30         if (!add_listener(l))
31                 return tal_free(l);
32         return l;
33 }
34
35 void io_close_listener(struct io_listener *l)
36 {
37         close(l->fd.fd);
38         del_listener(l);
39         tal_free(l);
40 }
41
42 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
43 {
44         abort();
45 }
46
47 static void next_plan(struct io_conn *conn, struct io_plan *plan)
48 {
49         struct io_plan *(*next)(struct io_conn *, void *arg);
50
51         next = plan->next;
52
53         plan->status = IO_UNSET;
54         plan->io = NULL;
55         plan->next = io_never_called;
56
57         plan = next(conn, plan->next_arg);
58
59         /* It should have set a plan inside this conn. */
60         assert(plan == &conn->plan[IO_IN]
61                || plan == &conn->plan[IO_OUT]);
62         assert(conn->plan[IO_IN].status != IO_UNSET
63                || conn->plan[IO_OUT].status != IO_UNSET);
64
65         backend_new_plan(conn);
66 }
67
68 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
69                              struct io_plan *(*init)(struct io_conn *, void *),
70                              void *arg)
71 {
72         struct io_conn *conn = tal(ctx, struct io_conn);
73
74         if (!conn)
75                 return NULL;
76
77         conn->fd.listener = false;
78         conn->fd.fd = fd;
79         conn->finish = NULL;
80         conn->finish_arg = NULL;
81         conn->list = NULL;
82
83         if (!add_conn(conn))
84                 return tal_free(conn);
85
86         /* We start with out doing nothing, and in doing our init. */
87         conn->plan[IO_OUT].status = IO_UNSET;
88
89         conn->plan[IO_IN].next = init;
90         conn->plan[IO_IN].next_arg = arg;
91         next_plan(conn, &conn->plan[IO_IN]);
92
93         return conn;
94 }
95
96 void io_set_finish_(struct io_conn *conn,
97                     void (*finish)(struct io_conn *, void *),
98                     void *arg)
99 {
100         conn->finish = finish;
101         conn->finish_arg = arg;
102 }
103
104 struct io_plan *io_get_plan(struct io_conn *conn, enum io_direction dir)
105 {
106         assert(conn->plan[dir].status == IO_UNSET);
107
108         conn->plan[dir].status = IO_POLLING;
109         return &conn->plan[dir];
110 }
111
112 static struct io_plan *set_always(struct io_conn *conn,
113                                   struct io_plan *plan,
114                                   struct io_plan *(*next)(struct io_conn *,
115                                                           void *),
116                                   void *arg)
117 {
118         plan->next = next;
119         plan->next_arg = arg;
120         plan->status = IO_ALWAYS;
121
122         backend_new_always(conn);
123         return plan;
124 }
125
126 struct io_plan *io_always_(struct io_conn *conn,
127                            enum io_direction dir,
128                            struct io_plan *(*next)(struct io_conn *, void *),
129                            void *arg)
130 {
131         struct io_plan *plan = io_get_plan(conn, dir);
132
133         assert(next);
134         set_always(conn, plan, next, arg);
135
136         return plan;
137 }
138
139 static int do_write(int fd, struct io_plan *plan)
140 {
141         ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
142         if (ret < 0)
143                 return -1;
144
145         plan->u1.cp += ret;
146         plan->u2.s -= ret;
147         return plan->u2.s == 0;
148 }
149
150 /* Queue some data to be written. */
151 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
152                           struct io_plan *(*next)(struct io_conn *, void *),
153                           void *arg)
154 {
155         struct io_plan *plan = io_get_plan(conn, IO_OUT);
156
157         assert(next);
158
159         if (len == 0)
160                 return set_always(conn, plan, next, arg);
161
162         plan->u1.const_vp = data;
163         plan->u2.s = len;
164         plan->io = do_write;
165         plan->next = next;
166         plan->next_arg = arg;
167
168         return plan;
169 }
170
171 static int do_read(int fd, struct io_plan *plan)
172 {
173         ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
174         if (ret <= 0)
175                 return -1;
176
177         plan->u1.cp += ret;
178         plan->u2.s -= ret;
179         return plan->u2.s == 0;
180 }
181
182 /* Queue a request to read into a buffer. */
183 struct io_plan *io_read_(struct io_conn *conn,
184                          void *data, size_t len,
185                          struct io_plan *(*next)(struct io_conn *, void *),
186                          void *arg)
187 {
188         struct io_plan *plan = io_get_plan(conn, IO_IN);
189
190         assert(next);
191
192         if (len == 0)
193                 return set_always(conn, plan, next, arg);
194
195         plan->u1.cp = data;
196         plan->u2.s = len;
197         plan->io = do_read;
198         plan->next = next;
199         plan->next_arg = arg;
200
201         return plan;
202 }
203
204 static int do_read_partial(int fd, struct io_plan *plan)
205 {
206         ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
207         if (ret <= 0)
208                 return -1;
209
210         *(size_t *)plan->u2.vp = ret;
211         return 1;
212 }
213
214 /* Queue a partial request to read into a buffer. */
215 struct io_plan *io_read_partial_(struct io_conn *conn,
216                                  void *data, size_t maxlen, size_t *len,
217                                  struct io_plan *(*next)(struct io_conn *,
218                                                          void *),
219                                  void *arg)
220 {
221         struct io_plan *plan = io_get_plan(conn, IO_IN);
222
223         assert(next);
224
225         if (maxlen == 0)
226                 return set_always(conn, plan, next, arg);
227
228         plan->u1.cp = data;
229         /* We store the max len in here temporarily. */
230         *len = maxlen;
231         plan->u2.vp = len;
232         plan->io = do_read_partial;
233         plan->next = next;
234         plan->next_arg = arg;
235
236         return plan;
237 }
238
239 static int do_write_partial(int fd, struct io_plan *plan)
240 {
241         ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
242         if (ret < 0)
243                 return -1;
244
245         *(size_t *)plan->u2.vp = ret;
246         return 1;
247 }
248
249 /* Queue a partial write request. */
250 struct io_plan *io_write_partial_(struct io_conn *conn,
251                                   const void *data, size_t maxlen, size_t *len,
252                                   struct io_plan *(*next)(struct io_conn *,
253                                                           void*),
254                                   void *arg)
255 {
256         struct io_plan *plan = io_get_plan(conn, IO_OUT);
257
258         assert(next);
259
260         if (maxlen == 0)
261                 return set_always(conn, plan, next, arg);
262
263         plan->u1.const_vp = data;
264         /* We store the max len in here temporarily. */
265         *len = maxlen;
266         plan->u2.vp = len;
267         plan->io = do_write_partial;
268         plan->next = next;
269         plan->next_arg = arg;
270
271         return plan;
272 }
273
274 static int do_connect(int fd, struct io_plan *plan)
275 {
276         int err, ret;
277         socklen_t len = sizeof(err);
278
279         /* Has async connect finished? */
280         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
281         if (ret < 0)
282                 return -1;
283
284         if (err == 0) {
285                 /* Restore blocking if it was initially. */
286                 fcntl(fd, F_SETFL, plan->u1.s);
287                 return 1;
288         } else if (err == EINPROGRESS)
289                 return 0;
290
291         errno = err;
292         return -1;
293 }
294
295 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
296                             struct io_plan *(*next)(struct io_conn *, void *),
297                             void *arg)
298 {
299         struct io_plan *plan = io_get_plan(conn, IO_IN);
300         int fd = io_conn_fd(conn);
301
302         assert(next);
303
304         /* Save old flags, set nonblock if not already. */
305         plan->u1.s = fcntl(fd, F_GETFL);
306         fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
307
308         /* Immediate connect can happen. */
309         if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
310                 return set_always(conn, plan, next, arg);
311
312         if (errno != EINPROGRESS)
313                 return io_close(conn);
314
315         plan->next = next;
316         plan->next_arg = arg;
317         plan->io = do_connect;
318
319         return plan;
320 }
321
322 struct io_plan *io_wait_(struct io_conn *conn,
323                          const void *wait, enum io_direction dir,
324                          struct io_plan *(*next)(struct io_conn *, void *),
325                          void *arg)
326 {
327         struct io_plan *plan = io_get_plan(conn, dir);
328
329         assert(next);
330
331         plan->next = next;
332         plan->next_arg = arg;
333         plan->u1.const_vp = wait;
334         plan->status = IO_WAITING;
335
336         return plan;
337 }
338
339 void io_wake(const void *wait)
340 {
341         backend_wake(wait);
342 }
343
344 static void do_plan(struct io_conn *conn, struct io_plan *plan)
345 {
346         /* Someone else might have called io_close() on us. */
347         if (plan->status == IO_CLOSING)
348                 return;
349
350         /* We shouldn't have polled for this event if this wasn't true! */
351         assert(plan->status == IO_POLLING);
352
353         switch (plan->io(conn->fd.fd, plan)) {
354         case -1:
355                 io_close(conn);
356                 break;
357         case 0:
358                 break;
359         case 1:
360                 next_plan(conn, plan);
361                 break;
362         default:
363                 /* IO should only return -1, 0 or 1 */
364                 abort();
365         }
366 }
367
368 void io_ready(struct io_conn *conn, int pollflags)
369 {
370         if (pollflags & POLLIN)
371                 do_plan(conn, &conn->plan[IO_IN]);
372
373         if (pollflags & POLLOUT)
374                 do_plan(conn, &conn->plan[IO_OUT]);
375 }
376
377 void io_do_always(struct io_conn *conn)
378 {
379         if (conn->plan[IO_IN].status == IO_ALWAYS)
380                 next_plan(conn, &conn->plan[IO_IN]);
381
382         if (conn->plan[IO_OUT].status == IO_ALWAYS)
383                 next_plan(conn, &conn->plan[IO_OUT]);
384 }
385
386 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
387 {
388         assert(plan->status == IO_WAITING);
389         next_plan(conn, plan);
390 }
391
392 /* Close the connection, we're done. */
393 struct io_plan *io_close(struct io_conn *conn)
394 {
395         /* Already closing?  Don't close twice. */
396         if (conn->plan[IO_IN].status == IO_CLOSING)
397                 return &conn->plan[IO_IN];
398
399         conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
400         conn->plan[IO_IN].u1.s = errno;
401         backend_new_closing(conn);
402
403         return &conn->plan[IO_IN];
404 }
405
406 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
407 {
408         return io_close(conn);
409 }
410
411 /* Exit the loop, returning this (non-NULL) arg. */
412 void io_break(const void *ret)
413 {
414         assert(ret);
415         io_loop_return = (void *)ret;
416 }
417
418 struct io_plan *io_never(struct io_conn *conn)
419 {
420         return io_always(conn, IO_IN, io_never_called, NULL);
421 }
422
423 int io_conn_fd(const struct io_conn *conn)
424 {
425         return conn->fd.fd;
426 }