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