Commit | Line | Data |
---|---|---|
83723896 FT |
1 | /* |
2 | ashd - A Sane HTTP Daemon | |
3 | Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation, either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
945d02f5 FT |
19 | #ifdef HAVE_CONFIG_H |
20 | #include <config.h> | |
21 | #endif | |
83723896 | 22 | #include <stdlib.h> |
e1cdf02e | 23 | #include <stdio.h> |
0b7964e9 | 24 | #include <unistd.h> |
e1cdf02e | 25 | #include <fcntl.h> |
83723896 | 26 | #include <string.h> |
83723896 | 27 | #include <errno.h> |
e1cdf02e | 28 | #include <sys/socket.h> |
83723896 | 29 | |
83723896 FT |
30 | #include <log.h> |
31 | #include <utils.h> | |
32 | #include <mt.h> | |
33 | #include <mtio.h> | |
34 | ||
e1cdf02e FT |
35 | struct stdiofd { |
36 | int fd; | |
37 | int sock; | |
38 | int timeout; | |
39 | }; | |
40 | ||
2b8eb6df | 41 | static ssize_t mtread(void *cookie, void *buf, size_t len) |
e1cdf02e FT |
42 | { |
43 | struct stdiofd *d = cookie; | |
44 | int ev; | |
45 | ssize_t ret; | |
46 | ||
47 | while(1) { | |
48 | ret = read(d->fd, buf, len); | |
49 | if((ret < 0) && (errno == EAGAIN)) { | |
50 | ev = block(d->fd, EV_READ, d->timeout); | |
51 | if(ev < 0) { | |
52 | /* If we just go on, we should get the real error. */ | |
53 | continue; | |
54 | } else if(ev == 0) { | |
55 | errno = ETIMEDOUT; | |
56 | return(-1); | |
57 | } else { | |
58 | continue; | |
59 | } | |
60 | } else { | |
61 | return(ret); | |
62 | } | |
63 | } | |
64 | } | |
65 | ||
2b8eb6df | 66 | static ssize_t mtwrite(void *cookie, const void *buf, size_t len) |
e1cdf02e FT |
67 | { |
68 | struct stdiofd *d = cookie; | |
69 | int ev; | |
9dc25d3d | 70 | size_t off; |
e1cdf02e FT |
71 | ssize_t ret; |
72 | ||
9dc25d3d FT |
73 | off = 0; |
74 | while(off < len) { | |
e1cdf02e | 75 | if(d->sock) |
9dc25d3d | 76 | ret = send(d->fd, buf + off, len - off, MSG_DONTWAIT | MSG_NOSIGNAL); |
e1cdf02e | 77 | else |
9dc25d3d FT |
78 | ret = write(d->fd, buf + off, len - off); |
79 | if(ret < 0) { | |
80 | if(errno == EAGAIN) { | |
81 | ev = block(d->fd, EV_WRITE, d->timeout); | |
82 | if(ev < 0) { | |
83 | /* If we just go on, we should get the real error. */ | |
84 | continue; | |
85 | } else if(ev == 0) { | |
86 | errno = ETIMEDOUT; | |
87 | return(off); | |
88 | } else { | |
89 | continue; | |
90 | } | |
e1cdf02e | 91 | } else { |
9dc25d3d | 92 | return(off); |
e1cdf02e FT |
93 | } |
94 | } else { | |
9dc25d3d | 95 | off += ret; |
e1cdf02e FT |
96 | } |
97 | } | |
9dc25d3d | 98 | return(off); |
e1cdf02e FT |
99 | } |
100 | ||
101 | static int mtclose(void *cookie) | |
102 | { | |
103 | struct stdiofd *d = cookie; | |
104 | ||
105 | close(d->fd); | |
106 | free(d); | |
107 | return(0); | |
108 | } | |
109 | ||
2a619a21 FT |
110 | FILE *mtstdopen(int fd, int issock, int timeout, char *mode) |
111 | { | |
112 | struct stdiofd *d; | |
113 | FILE *ret; | |
114 | int r, w; | |
115 | ||
116 | if(!strcmp(mode, "r")) { | |
117 | r = 1; w = 0; | |
118 | } else if(!strcmp(mode, "w")) { | |
119 | r = 0; w = 1; | |
120 | } else if(!strcmp(mode, "r+")) { | |
121 | r = w = 1; | |
122 | } else { | |
123 | return(NULL); | |
124 | } | |
125 | omalloc(d); | |
126 | d->fd = fd; | |
127 | d->sock = issock; | |
128 | d->timeout = timeout; | |
2b8eb6df | 129 | ret = funstdio(d, r?mtread:NULL, w?mtwrite:NULL, NULL, mtclose); |
2a619a21 FT |
130 | if(!ret) |
131 | free(d); | |
132 | else | |
133 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); | |
134 | return(ret); | |
135 | } | |
d8aea4cf FT |
136 | |
137 | struct pipe { | |
138 | struct charbuf data; | |
139 | size_t bufmax; | |
140 | int closed; | |
141 | struct muth *r, *w; | |
142 | }; | |
143 | ||
144 | static void freepipe(struct pipe *p) | |
145 | { | |
146 | buffree(p->data); | |
147 | free(p); | |
148 | } | |
149 | ||
150 | static ssize_t piperead(void *pdata, void *buf, size_t len) | |
151 | { | |
152 | struct pipe *p = pdata; | |
153 | ssize_t ret; | |
154 | ||
155 | while(p->data.d == 0) { | |
156 | if(p->closed & 2) | |
157 | return(0); | |
158 | if(p->r) { | |
159 | errno = EBUSY; | |
160 | return(-1); | |
161 | } | |
162 | p->r = current; | |
163 | yield(); | |
164 | p->r = NULL; | |
165 | } | |
166 | ret = min(len, p->data.d); | |
167 | memcpy(buf, p->data.b, ret); | |
168 | memmove(p->data.b, p->data.b + ret, p->data.d -= ret); | |
169 | if(p->w) | |
170 | resume(p->w, 0); | |
171 | return(ret); | |
172 | } | |
173 | ||
174 | static int piperclose(void *pdata) | |
175 | { | |
176 | struct pipe *p = pdata; | |
177 | ||
178 | if(p->closed & 2) { | |
179 | freepipe(p); | |
180 | } else { | |
181 | p->closed |= 1; | |
182 | if(p->w) | |
183 | resume(p->w, 0); | |
184 | } | |
185 | return(0); | |
186 | } | |
187 | ||
188 | static ssize_t pipewrite(void *pdata, const void *buf, size_t len) | |
189 | { | |
190 | struct pipe *p = pdata; | |
191 | size_t off, part; | |
192 | ||
193 | if(p->closed & 1) { | |
194 | errno = EPIPE; | |
195 | return(-1); | |
196 | } | |
197 | off = 0; | |
198 | while(off < len) { | |
199 | while(p->data.d >= p->bufmax) { | |
200 | if(p->w) { | |
201 | errno = EBUSY; | |
202 | return(-1); | |
203 | } | |
204 | if(p->closed & 1) { | |
205 | if(off == 0) { | |
206 | errno = EPIPE; | |
207 | return(-1); | |
208 | } | |
209 | return(off); | |
210 | } | |
211 | p->w = current; | |
212 | yield(); | |
213 | p->w = NULL; | |
214 | } | |
215 | part = min(len - off, p->bufmax - p->data.d); | |
216 | sizebuf(p->data, p->data.d + part); | |
217 | memcpy(p->data.b + p->data.d, buf + off, part); | |
218 | off += part; | |
219 | p->data.d += part; | |
220 | if(p->r) | |
221 | resume(p->r, 0); | |
222 | } | |
223 | return(off); | |
224 | } | |
225 | ||
226 | static int pipewclose(void *pdata) | |
227 | { | |
228 | struct pipe *p = pdata; | |
229 | ||
230 | if(p->closed & 1) { | |
231 | freepipe(p); | |
232 | } else { | |
233 | p->closed |= 2; | |
234 | if(p->r) | |
235 | resume(p->r, 0); | |
236 | } | |
237 | return(0); | |
238 | } | |
239 | ||
240 | void mtiopipe(FILE **read, FILE **write) | |
241 | { | |
242 | struct pipe *p; | |
243 | ||
244 | omalloc(p); | |
245 | p->bufmax = 4096; | |
246 | *read = funstdio(p, piperead, NULL, NULL, piperclose); | |
247 | *write = funstdio(p, NULL, pipewrite, NULL, pipewclose); | |
248 | } |