]> git.ozlabs.org Git - ccan/blob - ccan/io/_info
ccan/io: remove conn arg from io_plan constructors.
[ccan] / ccan / io / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * io - simple library for stateful io handling.
7  *
8  * io provides a simple mechanism to write I/O servers with multiple
9  * connections.  Handling of connections is multiplexed, and function
10  * indicate what they want written or read, and what follow-on
11  * function to call on success (or failure).
12  *
13  * Example:
14  * // Given tr A-Z a-z outputs tr a-z a-z
15  * #include <ccan/io/io.h>
16  * #include <ccan/err/err.h>
17  * #include <assert.h>
18  * #include <stdlib.h>
19  * #include <signal.h>
20  * #include <sys/types.h>
21  * #include <sys/wait.h>
22  *
23  * struct buffer {
24  *      size_t max, off, rlen;
25  *      char *buf;
26  * };
27  *
28  * struct stdin_buffer {
29  *      struct io_conn *reader, *writer;
30  *      size_t len;
31  *      char inbuf[4096];
32  * };
33  *
34  * // This reads from stdin.
35  * static struct io_plan wake_writer(struct io_conn *, struct stdin_buffer *);
36  * // This writes the stdin buffer to the child.
37  * static struct io_plan write_to_child(struct io_conn *c,
38  *                                      struct stdin_buffer *b);
39  * static struct io_plan read_stdin(struct io_conn *c, struct stdin_buffer *b)
40  * {
41  *      assert(c == b->reader);
42  *      b->len = sizeof(b->inbuf);
43  *      return io_read_partial(b->inbuf, &b->len, wake_writer, b);
44  * }
45  *
46  * static struct io_plan wake_writer(struct io_conn *c, struct stdin_buffer *b)
47  * {
48  *      assert(c == b->reader);
49  *      io_wake(b->writer, write_to_child, b);
50  *      return io_idle();
51  * }
52  *
53  * static void reader_exit(struct io_conn *c, struct stdin_buffer *b)
54  * {
55  *      assert(c == b->reader);
56  *      io_wake(b->writer, write_to_child, b);
57  *      b->reader = NULL;
58  * }
59  *
60  * static struct io_plan wake_reader(struct io_conn *c, struct stdin_buffer *b)
61  * {
62  *      assert(c == b->writer);
63  *      io_wake(b->reader, read_stdin, b);
64  *      return io_idle();
65  * }
66  *
67  * static struct io_plan write_to_child(struct io_conn *conn,
68  *                                      struct stdin_buffer *b)
69  * {
70  *      assert(conn == b->writer);
71  *      if (!b->reader)
72  *              return io_close(conn, NULL);
73  *      return io_write(b->inbuf, b->len, wake_reader, b);
74  * }
75  *
76  * static struct io_plan start_writer(struct io_conn *conn,
77  *                                    struct stdin_buffer *b)
78  * {
79  *      assert(conn == b->writer);
80  *      return io_idle();
81  * }
82  *
83  * static void fail_child_write(struct io_conn *conn, struct stdin_buffer *b)
84  * {
85  *      if (b->reader)
86  *              err(1, "Failed writing to child.");
87  * }
88  *
89  * // This reads from the child and saves it into buffer.
90  * static struct io_plan read_from_child(struct io_conn *conn,
91  *                                       struct buffer *b)
92  * {
93  *      b->off += b->rlen;
94  *
95  *      if (b->off == b->max) {
96  *              if (b->max == 0)
97  *                      b->max = 128;
98  *              else if (b->max >= 1024*1024)
99  *                      b->max += 1024*1024;
100  *              else
101  *                      b->max *= 2;
102  *              b->buf = realloc(b->buf, b->max);
103  *      }
104  *
105  *      b->rlen = b->max - b->off;
106  *      return io_read_partial(b->buf + b->off, &b->rlen, read_from_child, b);
107  * }
108  *
109  * // Feed a program our stdin, gather its stdout, print that at end.
110  * int main(int argc, char *argv[])
111  * {
112  *      int tochild[2], fromchild[2];
113  *      struct buffer out = { 0, 0, 0, NULL };
114  *      struct stdin_buffer sbuf;
115  *      int status;
116  *      size_t off;
117  *      ssize_t ret;
118  *
119  *      if (argc == 1)
120  *              errx(1, "Usage: runner <cmdline>...");
121  *
122  *      if (pipe(tochild) != 0 || pipe(fromchild) != 0)
123  *              err(1, "Creating pipes");
124  *
125  *      if (!fork()) {
126  *              // Child runs command.
127  *              close(tochild[1]);
128  *              close(fromchild[0]);
129  *
130  *              dup2(tochild[0], STDIN_FILENO);
131  *              dup2(fromchild[1], STDOUT_FILENO);
132  *              execvp(argv[1], argv + 1);
133  *              exit(127);
134  *      }
135  *
136  *      close(tochild[0]);
137  *      close(fromchild[1]);
138  *      signal(SIGPIPE, SIG_IGN);
139  *
140  *      sbuf.reader = io_new_conn(STDIN_FILENO, read_stdin, reader_exit, &sbuf);
141  *      sbuf.writer = io_new_conn(tochild[1], start_writer, fail_child_write,
142  *                                &sbuf);
143  *      if (!sbuf.reader || !sbuf.writer
144  *          || !io_new_conn(fromchild[0], read_from_child, NULL, &out))
145  *              err(1, "Allocating connections");
146  *
147  *      io_loop();
148  *      wait(&status);
149  *
150  *      for (off = 0; off < out.off; off += ret) {
151  *              ret = write(STDOUT_FILENO, out.buf+off, out.off-off);
152  *              if (ret < 0)
153  *                      err(1, "Writing stdout");
154  *      }
155  *      free(out.buf);
156  *
157  *      return WIFEXITED(status) ? WEXITSTATUS(status) : 2;
158  * }
159  *
160  * License: LGPL (v2.1 or any later version)
161  * Author: Rusty Russell <rusty@rustcorp.com.au>
162  */
163 int main(int argc, char *argv[])
164 {
165         if (argc != 2)
166                 return 1;
167
168         if (strcmp(argv[1], "depends") == 0) {
169                 printf("ccan/time\n");
170                 printf("ccan/timer\n");
171                 return 0;
172         }
173
174         return 1;
175 }