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