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