]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
ccan/io: save errno on io_close, for finish functions.
[ccan] / ccan / io / io.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_IO_H
3 #define CCAN_IO_H
4 #include <ccan/typesafe_cb/typesafe_cb.h>
5 #include <ccan/time/time.h>
6 #include <stdbool.h>
7 #include <unistd.h>
8
9 struct io_conn;
10
11 /**
12  * struct io_plan - returned from a setup function.
13  *
14  * A plan of what IO to do, when.
15  */
16 struct io_plan {
17         int pollflag;
18         /* Only NULL if idle. */
19         int (*io)(int fd, struct io_plan *plan);
20         /* Only NULL if closing. */
21         struct io_plan (*next)(struct io_conn *, void *arg);
22         void *next_arg;
23
24         union {
25                 struct {
26                         char *buf;
27                         size_t len;
28                 } read;
29                 struct {
30                         const char *buf;
31                         size_t len;
32                 } write;
33                 struct {
34                         char *buf;
35                         size_t *lenp;
36                 } readpart;
37                 struct {
38                         const char *buf;
39                         size_t *lenp;
40                 } writepart;
41                 struct {
42                         int saved_errno;
43                 } close;
44                 struct {
45                         void *p;
46                         size_t len;
47                 } ptr_len;
48                 struct {
49                         void *p1;
50                         void *p2;
51                 } ptr_ptr;
52                 struct {
53                         size_t len1;
54                         size_t len2;
55                 } len_len;
56         } u;
57 };
58
59 #ifdef DEBUG
60 extern bool io_plan_for_other;
61 extern bool (*io_debug)(struct io_conn *conn);
62 #define io_plan_other() ((io_plan_for_other = true))
63 void io_plan_debug(struct io_plan *plan);
64 #else
65 #define io_plan_other() (void)0
66 static inline void io_plan_debug(struct io_plan *plan) { }
67 #endif
68
69 /**
70  * io_new_conn - create a new connection.
71  * @fd: the file descriptor.
72  * @plan: the first I/O function.
73  *
74  * This creates a connection which owns @fd.  @plan will be called on the
75  * next io_loop().
76  *
77  * Returns NULL on error (and sets errno).
78  */
79 #define io_new_conn(fd, plan)                           \
80         (io_plan_other(), io_new_conn_((fd), (plan)))
81 struct io_conn *io_new_conn_(int fd, struct io_plan plan);
82
83 /**
84  * io_set_finish - set finish function on a connection.
85  * @conn: the connection.
86  * @finish: the function to call when it's closed or fails.
87  * @arg: the argument to @finish.
88  *
89  * @finish will be called when an I/O operation fails, or you call
90  * io_close() on the connection.  errno will be set to the value
91  * after the failed I/O, or at the call to io_close().
92  */
93 #define io_set_finish(conn, finish, arg)                                \
94         io_set_finish_((conn),                                          \
95                        typesafe_cb_preargs(void, void *,                \
96                                            (finish), (arg),             \
97                                            struct io_conn *),           \
98                        (arg))
99 void io_set_finish_(struct io_conn *conn,
100                     void (*finish)(struct io_conn *, void *),
101                     void *arg);
102
103 /**
104  * io_new_listener - create a new accepting listener.
105  * @fd: the file descriptor.
106  * @init: the function to call for a new connection
107  * @arg: the argument to @init.
108  *
109  * When @fd becomes readable, we accept() and pass that fd to init().
110  *
111  * Returns NULL on error (and sets errno).
112  */
113 #define io_new_listener(fd, init, arg)                                  \
114         io_new_listener_((fd),                                          \
115                          typesafe_cb_preargs(void, void *,              \
116                                              (init), (arg),             \
117                                              int fd),                   \
118                          (arg))
119 struct io_listener *io_new_listener_(int fd,
120                                      void (*init)(int fd, void *arg),
121                                      void *arg);
122
123 /**
124  * io_close_listener - delete a listener.
125  * @listener: the listener returned from io_new_listener.
126  *
127  * This closes the fd and frees @listener.
128  */
129 void io_close_listener(struct io_listener *listener);
130
131 /**
132  * io_write - queue data to be written.
133  * @data: the data buffer.
134  * @len: the length to write.
135  * @cb: function to call once it's done.
136  * @arg: @cb argument
137  *
138  * This will queue the data buffer for writing.  Once it's all
139  * written, the @cb function will be called: on an error, the finish
140  * function is called instead.
141  *
142  * Note that the I/O may actually be done immediately.
143  */
144 #define io_write(data, len, cb, arg)                                    \
145         io_write_((data), (len),                                        \
146                   typesafe_cb_preargs(struct io_plan, void *,           \
147                                       (cb), (arg), struct io_conn *),   \
148                   (arg))
149 struct io_plan io_write_(const void *data, size_t len,
150                          struct io_plan (*cb)(struct io_conn *, void *),
151                          void *arg);
152
153 /**
154  * io_read - queue buffer to be read.
155  * @data: the data buffer.
156  * @len: the length to read.
157  * @cb: function to call once it's done.
158  * @arg: @cb argument
159  *
160  * This will queue the data buffer for reading.  Once it's all read,
161  * the @cb function will be called: on an error, the finish function
162  * is called instead.
163  *
164  * Note that the I/O may actually be done immediately.
165  */
166 #define io_read(data, len, cb, arg)                                     \
167         io_read_((data), (len),                                         \
168                  typesafe_cb_preargs(struct io_plan, void *,            \
169                                      (cb), (arg), struct io_conn *),    \
170                  (arg))
171 struct io_plan io_read_(void *data, size_t len,
172                         struct io_plan (*cb)(struct io_conn *, void *),
173                         void *arg);
174
175
176 /**
177  * io_read_partial - queue buffer to be read (partial OK).
178  * @data: the data buffer.
179  * @len: the maximum length to read, set to the length actually read.
180  * @cb: function to call once it's done.
181  * @arg: @cb argument
182  *
183  * This will queue the data buffer for reading.  Once any data is
184  * read, @len is updated and the @cb function will be called: on an
185  * error, the finish function is called instead.
186  *
187  * Note that the I/O may actually be done immediately.
188  */
189 #define io_read_partial(data, len, cb, arg)                             \
190         io_read_partial_((data), (len),                                 \
191                          typesafe_cb_preargs(struct io_plan, void *,    \
192                                              (cb), (arg), struct io_conn *), \
193                          (arg))
194 struct io_plan io_read_partial_(void *data, size_t *len,
195                                 struct io_plan (*cb)(struct io_conn *, void *),
196                                 void *arg);
197
198 /**
199  * io_write_partial - queue data to be written (partial OK).
200  * @data: the data buffer.
201  * @len: the maximum length to write, set to the length actually written.
202  * @cb: function to call once it's done.
203  * @arg: @cb argument
204  *
205  * This will queue the data buffer for writing.  Once any data is
206  * written, @len is updated and the @cb function will be called: on an
207  * error, the finish function is called instead.
208  *
209  * Note that the I/O may actually be done immediately.
210  */
211 #define io_write_partial(data, len, cb, arg)                            \
212         io_write_partial_((data), (len),                                \
213                           typesafe_cb_preargs(struct io_plan, void *,   \
214                                               (cb), (arg), struct io_conn *), \
215                           (arg))
216 struct io_plan io_write_partial_(const void *data, size_t *len,
217                                  struct io_plan (*cb)(struct io_conn *, void*),
218                                  void *arg);
219
220
221 /**
222  * io_idle - explicitly note that this connection will do nothing.
223  *
224  * This indicates the connection is idle: some other function will
225  * later call io_read/io_write etc. (or io_close) on it, in which case
226  * it will do that.
227  */
228 struct io_plan io_idle(void);
229
230 /**
231  * io_timeout - set timeout function if the callback doesn't fire.
232  * @conn: the current connection.
233  * @ts: how long until the timeout should be called.
234  * @cb to call.
235  * @arg: argument to @cb.
236  *
237  * If the usual next callback is not called for this connection before @ts,
238  * this function will be called.  If next callback is called, the timeout
239  * is automatically removed.
240  *
241  * Returns false on allocation failure.  A connection can only have one
242  * timeout.
243  */
244 #define io_timeout(conn, ts, fn, arg)                                   \
245         io_timeout_((conn), (ts),                                       \
246                     typesafe_cb_preargs(struct io_plan, void *,         \
247                                         (fn), (arg),                    \
248                                         struct io_conn *),              \
249                     (arg))
250 bool io_timeout_(struct io_conn *conn, struct timespec ts,
251                  struct io_plan (*fn)(struct io_conn *, void *), void *arg);
252
253 /**
254  * io_duplex - split an fd into two connections.
255  * @conn: a connection.
256  * @plan: the first I/O function to call.
257  *
258  * Sometimes you want to be able to simultaneously read and write on a
259  * single fd, but io forces a linear call sequence.  The solition is
260  * to have two connections for the same fd, and use one for read
261  * operations and one for write.
262  *
263  * You must io_close() both of them to close the fd.
264  */
265 #define io_duplex(conn, plan)                           \
266         (io_plan_other(), io_duplex_((conn), (plan)))
267
268 struct io_conn *io_duplex_(struct io_conn *conn, struct io_plan plan);
269
270 /**
271  * io_wake - wake up an idle connection.
272  * @conn: an idle connection.
273  * @plan: the next I/O function for @conn.
274  *
275  * This makes @conn do I/O the next time around the io_loop().
276  */
277 #define io_wake(conn, plan) (io_plan_other(), io_wake_((conn), (plan)))
278 void io_wake_(struct io_conn *conn, struct io_plan plan);
279
280 /**
281  * io_break - return from io_loop()
282  * @ret: non-NULL value to return from io_loop().
283  * @plan: I/O to perform on return (if any)
284  *
285  * This breaks out of the io_loop.  As soon as the current @next
286  * function returns, any io_closed()'d connections will have their
287  * finish callbacks called, then io_loop() with return with @ret.
288  *
289  * If io_loop() is called again, then @plan will be carried out.
290  */
291 #define io_break(ret, plan) (io_plan_other(), io_break_((ret), (plan)))
292 struct io_plan io_break_(void *ret, struct io_plan plan);
293
294 /* FIXME: io_recvfrom/io_sendto */
295
296 /**
297  * io_close - plan to close a connection.
298  *
299  * On return to io_loop, the connection will be closed.
300  */
301 struct io_plan io_close(void);
302
303 /**
304  * io_close_cb - helper callback to close a connection.
305  * @conn: the connection.
306  *
307  * This schedules a connection to be closed; designed to be used as
308  * a callback function.
309  */
310 struct io_plan io_close_cb(struct io_conn *, void *unused);
311
312 /**
313  * io_loop - process fds until all closed on io_break.
314  *
315  * This is the core loop; it exits with the io_break() arg, or NULL if
316  * all connections and listeners are closed.
317  */
318 void *io_loop(void);
319 #endif /* CCAN_IO_H */