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