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