]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: keep more io_plan details internal.
[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 struct io_plan *io_always_(struct io_conn *conn,
129                            struct io_plan *(*next)(struct io_conn *, void *),
130                            void *arg)
131 {
132         /* If we're duplex, we want this on the current plan.  Otherwise,
133          * doesn't matter. */
134         if (conn->plan[IO_IN].status == IO_UNSET)
135                 return set_always(conn, IO_IN, next, arg);
136         else
137                 return set_always(conn, IO_OUT, next, arg);
138 }
139
140 static int do_write(int fd, struct io_plan_arg *arg)
141 {
142         ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
143         if (ret < 0)
144                 return -1;
145
146         arg->u1.cp += ret;
147         arg->u2.s -= ret;
148         return arg->u2.s == 0;
149 }
150
151 /* Queue some data to be written. */
152 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
153                           struct io_plan *(*next)(struct io_conn *, void *),
154                           void *next_arg)
155 {
156         struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
157
158         if (len == 0)
159                 return set_always(conn, IO_OUT, next, next_arg);
160
161         arg->u1.const_vp = data;
162         arg->u2.s = len;
163
164         return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
165 }
166
167 static int do_read(int fd, struct io_plan_arg *arg)
168 {
169         ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
170         if (ret <= 0)
171                 return -1;
172
173         arg->u1.cp += ret;
174         arg->u2.s -= ret;
175         return arg->u2.s == 0;
176 }
177
178 /* Queue a request to read into a buffer. */
179 struct io_plan *io_read_(struct io_conn *conn,
180                          void *data, size_t len,
181                          struct io_plan *(*next)(struct io_conn *, void *),
182                          void *next_arg)
183 {
184         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
185
186         if (len == 0)
187                 return set_always(conn, IO_IN, next, next_arg);
188
189         arg->u1.cp = data;
190         arg->u2.s = len;
191
192         return io_set_plan(conn, IO_IN, do_read, next, next_arg);
193 }
194
195 static int do_read_partial(int fd, struct io_plan_arg *arg)
196 {
197         ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
198         if (ret <= 0)
199                 return -1;
200
201         *(size_t *)arg->u2.vp = ret;
202         return 1;
203 }
204
205 /* Queue a partial request to read into a buffer. */
206 struct io_plan *io_read_partial_(struct io_conn *conn,
207                                  void *data, size_t maxlen, size_t *len,
208                                  struct io_plan *(*next)(struct io_conn *,
209                                                          void *),
210                                  void *next_arg)
211 {
212         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
213
214         if (maxlen == 0)
215                 return set_always(conn, IO_IN, next, next_arg);
216
217         arg->u1.cp = data;
218         /* We store the max len in here temporarily. */
219         *len = maxlen;
220         arg->u2.vp = len;
221
222         return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
223 }
224
225 static int do_write_partial(int fd, struct io_plan_arg *arg)
226 {
227         ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
228         if (ret < 0)
229                 return -1;
230
231         *(size_t *)arg->u2.vp = ret;
232         return 1;
233 }
234
235 /* Queue a partial write request. */
236 struct io_plan *io_write_partial_(struct io_conn *conn,
237                                   const void *data, size_t maxlen, size_t *len,
238                                   struct io_plan *(*next)(struct io_conn *,
239                                                           void*),
240                                   void *next_arg)
241 {
242         struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
243
244         if (maxlen == 0)
245                 return set_always(conn, IO_OUT, next, next_arg);
246
247         arg->u1.const_vp = data;
248         /* We store the max len in here temporarily. */
249         *len = maxlen;
250         arg->u2.vp = len;
251
252         return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
253 }
254
255 static int do_connect(int fd, struct io_plan_arg *arg)
256 {
257         int err, ret;
258         socklen_t len = sizeof(err);
259
260         /* Has async connect finished? */
261         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
262         if (ret < 0)
263                 return -1;
264
265         if (err == 0) {
266                 /* Restore blocking if it was initially. */
267                 fcntl(fd, F_SETFL, arg->u1.s);
268                 return 1;
269         } else if (err == EINPROGRESS)
270                 return 0;
271
272         errno = err;
273         return -1;
274 }
275
276 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
277                             struct io_plan *(*next)(struct io_conn *, void *),
278                             void *next_arg)
279 {
280         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
281         int fd = io_conn_fd(conn);
282
283         /* Save old flags, set nonblock if not already. */
284         arg->u1.s = fcntl(fd, F_GETFL);
285         fcntl(fd, F_SETFL, arg->u1.s | O_NONBLOCK);
286
287         /* Immediate connect can happen. */
288         if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
289                 return set_always(conn, IO_IN, next, next_arg);
290
291         if (errno != EINPROGRESS)
292                 return io_close(conn);
293
294         return io_set_plan(conn, IO_IN, do_connect, next, next_arg);
295 }
296
297 struct io_plan *io_wait_(struct io_conn *conn,
298                          const void *wait,
299                          struct io_plan *(*next)(struct io_conn *, void *),
300                          void *next_arg)
301 {
302         enum io_direction dir;
303         struct io_plan_arg *arg;
304
305         /* If we're duplex, we want this on the current plan.  Otherwise,
306          * doesn't matter. */
307         if (conn->plan[IO_IN].status == IO_UNSET)
308                 dir = IO_IN;
309         else
310                 dir = IO_OUT;
311
312         arg = io_plan_arg(conn, dir);
313         arg->u1.const_vp = wait;
314
315         conn->plan[dir].status = IO_WAITING;
316
317         return io_set_plan(conn, dir, NULL, next, next_arg);
318 }
319
320 void io_wake(const void *wait)
321 {
322         backend_wake(wait);
323 }
324
325 static int do_plan(struct io_conn *conn, struct io_plan *plan)
326 {
327         /* Someone else might have called io_close() on us. */
328         if (plan->status == IO_CLOSING)
329                 return -1;
330
331         /* We shouldn't have polled for this event if this wasn't true! */
332         assert(plan->status == IO_POLLING);
333
334         switch (plan->io(conn->fd.fd, &plan->arg)) {
335         case -1:
336                 io_close(conn);
337                 return -1;
338         case 0:
339                 return 0;
340         case 1:
341                 next_plan(conn, plan);
342                 return 1;
343         default:
344                 /* IO should only return -1, 0 or 1 */
345                 abort();
346         }
347 }
348
349 void io_ready(struct io_conn *conn, int pollflags)
350 {
351         if (pollflags & POLLIN)
352                 do_plan(conn, &conn->plan[IO_IN]);
353
354         if (pollflags & POLLOUT)
355                 do_plan(conn, &conn->plan[IO_OUT]);
356 }
357
358 void io_do_always(struct io_conn *conn)
359 {
360         if (conn->plan[IO_IN].status == IO_ALWAYS)
361                 next_plan(conn, &conn->plan[IO_IN]);
362
363         if (conn->plan[IO_OUT].status == IO_ALWAYS)
364                 next_plan(conn, &conn->plan[IO_OUT]);
365 }
366
367 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
368 {
369         assert(plan->status == IO_WAITING);
370         next_plan(conn, plan);
371 }
372
373 /* Close the connection, we're done. */
374 struct io_plan *io_close(struct io_conn *conn)
375 {
376         /* Already closing?  Don't close twice. */
377         if (conn->plan[IO_IN].status == IO_CLOSING)
378                 return &conn->plan[IO_IN];
379
380         conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
381         conn->plan[IO_IN].arg.u1.s = errno;
382         backend_new_closing(conn);
383
384         return io_set_plan(conn, IO_IN, NULL, NULL, NULL);
385 }
386
387 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
388 {
389         return io_close(conn);
390 }
391
392 /* Exit the loop, returning this (non-NULL) arg. */
393 void io_break(const void *ret)
394 {
395         assert(ret);
396         io_loop_return = (void *)ret;
397 }
398
399 struct io_plan *io_never(struct io_conn *conn, void *unused)
400 {
401         return io_always(conn, io_never_called, NULL);
402 }
403
404 int io_conn_fd(const struct io_conn *conn)
405 {
406         return conn->fd.fd;
407 }
408
409 void io_duplex_prepare(struct io_conn *conn)
410 {
411         assert(conn->plan[IO_IN].status == IO_UNSET);
412         assert(conn->plan[IO_OUT].status == IO_UNSET);
413
414         /* We can't sync debug until we've set both: io_wait() and io_always
415          * can't handle it. */
416         conn->debug_saved = conn->debug;
417         conn->debug = false;
418 }
419
420 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
421 {
422         struct io_conn *conn;
423
424         /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
425         assert(out_plan == in_plan + 1);
426
427         /* Restore debug. */
428         conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
429         conn->debug = conn->debug_saved;
430
431         /* Now set the plans again, to invoke sync debug. */
432         io_set_plan(conn, IO_OUT,
433                     out_plan->io, out_plan->next, out_plan->next_arg);
434         io_set_plan(conn, IO_IN,
435                     in_plan->io, in_plan->next, in_plan->next_arg);
436
437         return out_plan + 1;
438 }
439
440 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
441                             int (*io)(int fd, struct io_plan_arg *arg),
442                             struct io_plan *(*next)(struct io_conn *, void *),
443                             void *next_arg)
444 {
445         struct io_plan *plan = &conn->plan[dir];
446
447         plan->io = io;
448         plan->next = next;
449         plan->next_arg = next_arg;
450         assert(plan->status == IO_CLOSING || next != NULL);
451
452         if (!conn->debug)
453                 return plan;
454
455         if (io_loop_return) {
456                 io_debug_complete(conn);
457                 return plan;
458         }
459
460         switch (plan->status) {
461         case IO_POLLING:
462                 while (do_plan(conn, plan) == 0);
463                 break;
464         /* Shouldn't happen, since you said you did plan! */
465         case IO_UNSET:
466                 abort();
467         case IO_ALWAYS:
468                 /* If other one is ALWAYS, leave in list! */
469                 if (conn->plan[!dir].status != IO_ALWAYS)
470                         remove_from_always(conn);
471                 next_plan(conn, plan);
472                 break;
473         case IO_WAITING:
474         case IO_CLOSING:
475                 io_debug_complete(conn);
476         }
477
478         return plan;
479 }
480
481 void io_set_debug(struct io_conn *conn, bool debug)
482 {
483         conn->debug = debug;
484 }
485
486 void io_debug_complete(struct io_conn *conn)
487 {
488 }