author | Tero Marttila <terom@fixme.fi> |
Tue, 28 Apr 2009 23:11:06 +0300 | |
branch | new-transport |
changeset 161 | d229e4668476 |
parent 157 | 1e5674d0eec4 |
child 176 | 6750d50ee8cd |
permissions | -rw-r--r-- |
157 | 1 |
|
2 |
#include "fifo.h" |
|
3 |
#include "transport_fd.h" |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
#include <sys/types.h> |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
#include <sys/stat.h> |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
#include <fcntl.h> |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
#include <string.h> |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
|
157 | 11 |
/** |
12 |
* Our transport_type |
|
13 |
*/ |
|
14 |
extern const struct transport_type fifo_type; |
|
15 |
||
16 |
/** |
|
17 |
* The fifo state |
|
18 |
*/ |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
struct fifo { |
157 | 20 |
/** The FD-based state */ |
21 |
struct transport_fd base_fd; |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
|
157 | 23 |
/** Path to the fifo */ |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
char *path; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
}; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
* Get a sock_fd pointer from a sock_fifo pointer |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
*/ |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
#define FIFO_FD(sock_ptr) (&(sock_ptr)->base_fd) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
* Get a sock_base pointer from a sock_fifo pointer |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
*/ |
157 | 35 |
#define FIFO_TRANSPORT(sock_ptr) TRANSPORT_FD_BASE(FIFO_FD(sock_ptr)) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
* (re)open the fifo, closing it if already open, and keeping any event callbacks registered. |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
*/ |
157 | 41 |
static err_t fifo_open (struct fifo *fifo, error_t *err) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
{ |
157 | 43 |
int _fd; |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
|
157 | 45 |
// open(2) the path in non-blocking read-only mode |
46 |
if ((_fd = open(fifo->path, O_RDONLY | O_NONBLOCK)) < 0) |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
RETURN_SET_ERROR_ERRNO(err, ERR_OPEN); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
// set the new fd |
157 | 50 |
if ((ERROR_CODE(err) = transport_fd_set(FIFO_FD(fifo), _fd))) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
return ERROR_CODE(err); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
|
157 | 53 |
// use default transport event-based behaviour |
54 |
if ((ERROR_CODE(err) = transport_fd_defaults(FIFO_FD(fifo)))) |
|
55 |
return ERROR_CODE(err); |
|
56 |
||
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
// ok |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
return SUCCESS; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
* Destroy the fifo, releasing all resources |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
*/ |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
static void fifo_destroy (struct fifo *fifo) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
{ |
157 | 66 |
// destroy base |
67 |
transport_fd_destroy(FIFO_FD(fifo)); |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
// release the path |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
free(fifo->path); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
* sock_stream_methods::read implementation. |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
* |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
* Try and do a normal sock_fd_read call, but re-open with EAGAIN on EOF |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
*/ |
157 | 78 |
err_t fifo_read (transport_t *transport, void *buf, size_t *len, struct error_info *err) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
{ |
157 | 80 |
struct fifo *fifo = transport_check(transport, &fifo_type); |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
|
157 | 82 |
// trap READ_EOF |
83 |
if (transport_fd_methods_read(transport, buf, len, err) != ERR_EOF) |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
return ERROR_CODE(err); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
// re-open it |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
// XXX: re-add events? |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
if (fifo_open(fifo, err)) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
goto error; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
// ok, act as if it was EAGAIN |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
*len = 0; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
return SUCCESS; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
error: |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
return ERROR_CODE(err); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
* sock_stream_methods::release implementation |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
*/ |
157 | 103 |
static void _fifo_destroy (transport_t *transport) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
{ |
157 | 105 |
struct fifo *fifo = transport_check(transport, &fifo_type); |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
fifo_destroy(fifo); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
/* |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
* Our sock_stream_type |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
*/ |
157 | 113 |
const struct transport_type fifo_type = { |
161
d229e4668476
amend d3e253d7281a, also note that it fixed other bugs with transport_fd
Tero Marttila <terom@fixme.fi>
parents:
157
diff
changeset
|
114 |
.parent = &transport_fd_type, |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
.methods = { |
157 | 116 |
.read = fifo_read, |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
.write = NULL, |
157 | 118 |
.events = transport_fd_methods_events, |
119 |
.destroy = _fifo_destroy, |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
}, |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
}; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
|
157 | 123 |
err_t fifo_open_read (struct transport_info *transport_info, transport_t **transport_ptr, struct event_base *ev_base, |
124 |
const char *path, error_t *err) |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
125 |
{ |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
struct fifo *fifo; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
128 |
// alloc |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
129 |
if ((fifo = calloc(1, sizeof(*fifo))) == NULL) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
return SET_ERROR(err, ERR_CALLOC); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
132 |
// copy the path |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
133 |
if ((fifo->path = strdup(path)) == NULL) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
134 |
return SET_ERROR(err, ERR_STRDUP); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
// init |
157 | 137 |
transport_init(FIFO_TRANSPORT(fifo), &fifo_type, transport_info); |
138 |
transport_fd_init(FIFO_FD(fifo), ev_base, TRANSPORT_FD_INVALID); |
|
139 |
||
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
140 |
// open the fifo |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
141 |
if (fifo_open(fifo, err)) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
142 |
goto error; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
144 |
// ok |
157 | 145 |
*transport_ptr = FIFO_TRANSPORT(fifo); |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
146 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
147 |
return SUCCESS; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
148 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
149 |
error: |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
150 |
// cleanup |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
151 |
fifo_destroy(fifo); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
152 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
153 |
return ERROR_CODE(err); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
154 |
} |