Debian Codegraph @

deps/libneon/test/common/child.c

raw
1 /*
2 Framework for testing with a server process
3 Copyright (C) 2001-2008, Joe Orton <[email protected]>
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 2 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, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24
25 #include <sys/wait.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29
30 #include <sys/stat.h>
31
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41
42 #include <fcntl.h>
43 #include <netdb.h>
44 #include <signal.h>
45
46 #include "ne_socket.h"
47 #include "ne_utils.h"
48 #include "ne_string.h"
49
50 #include "tests.h"
51 #include "child.h"
52
53 static pid_t child = 0;
54
55 int clength;
56
57 static struct in_addr lh_addr = {0}, hn_addr = {0};
58
59 const char *want_header = NULL;
60 got_header_fn got_header = NULL;
61 char *local_hostname = NULL;
62
63 /* If we have pipe(), then use a pipe between the parent and child to
64 * know when the child is ready to accept incoming connections.
65 * Otherwise use boring sleep()s trying to avoid the race condition
66 * between listen() and connect() in the two processes. */
67 #ifdef HAVE_PIPE
68 #define USE_PIPE 1
69 #endif
70
71 int lookup_localhost(void)
72 {
73 /* this will break if a system is set up so that `localhost' does
74 * not resolve to 127.0.0.1, but... */
75 lh_addr.s_addr = inet_addr("127.0.0.1");
76 return OK;
77 }
78
79 int lookup_hostname(void)
80 {
81 char buf[BUFSIZ];
82 struct hostent *ent;
83
84 local_hostname = NULL;
85 ONV(gethostname(buf, BUFSIZ) < 0,
86 ("gethostname failed: %s", strerror(errno)));
87
88 ent = gethostbyname(buf);
89 #ifdef HAVE_HSTRERROR
90 ONV(ent == NULL,
91 ("could not resolve `%s': %s", buf, hstrerror(h_errno)));
92 #else
93 ONV(ent == NULL, ("could not resolve `%s'", buf));
94 #endif
95
96 local_hostname = ne_strdup(ent->h_name);
97
98 return OK;
99 }
100
101 static int do_listen(struct in_addr addr, int port)
102 {
103 int ls = socket(AF_INET, SOCK_STREAM, 0);
104 struct sockaddr_in saddr = {0};
105 int val = 1;
106
107 setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(int));
108
109 saddr.sin_addr = addr;
110 saddr.sin_port = htons(port);
111 saddr.sin_family = AF_INET;
112
113 if (bind(ls, (struct sockaddr *)&saddr, sizeof(saddr))) {
114 printf("bind failed: %s\n", strerror(errno));
115 return -1;
116 }
117 if (listen(ls, 5)) {
118 printf("listen failed: %s\n", strerror(errno));
119 return -1;
120 }
121
122 return ls;
123 }
124
125 void minisleep(void)
126 {
127 #ifdef HAVE_USLEEP
128 usleep(500);
129 #else
130 sleep(1);
131 #endif
132 }
133
134 int reset_socket(ne_socket *sock)
135 {
136 #ifdef SO_LINGER
137 /* Stevens' magic trick to send an RST on close(). */
138 struct linger l = {1, 0};
139 return setsockopt(ne_sock_fd(sock), SOL_SOCKET, SO_LINGER, &l, sizeof l);
140 #else
141 return 1;
142 #endif
143 }
144
145 /* close 'sock', performing lingering close to avoid premature RST. */
146 static int close_socket(ne_socket *sock)
147 {
148 #ifdef HAVE_SHUTDOWN
149 char buf[20];
150 int fd = ne_sock_fd(sock);
151
152 shutdown(fd, 0);
153 while (ne_sock_read(sock, buf, sizeof buf) > 0);
154 #endif
155 return ne_sock_close(sock);
156 }
157
158 /* This runs as the child process. */
159 static int server_child(int readyfd, struct in_addr addr, int port,
160 server_fn callback, void *userdata)
161 {
162 ne_socket *s = ne_sock_create();
163 int ret, listener;
164
165 in_child();
166
167 listener = do_listen(addr, port);
168 if (listener < 0)
169 return FAIL;
170
171 #ifdef USE_PIPE
172 /* Tell the parent we're ready for the request. */
173 if (write(readyfd, "a", 1) != 1) abort();
174 #endif
175
176 ONN("accept failed", ne_sock_accept(s, listener));
177
178 ret = callback(s, userdata);
179
180 close_socket(s);
181
182 return ret;
183 }
184
185 int spawn_server(int port, server_fn fn, void *ud)
186 {
187 return spawn_server_addr(1, port, fn, ud);
188 }
189
190 int spawn_server_addr(int bind_local, int port, server_fn fn, void *ud)
191 {
192 int fds[2];
193 struct in_addr addr;
194
195 addr = bind_local?lh_addr:hn_addr;
196
197 #ifdef USE_PIPE
198 if (pipe(fds)) {
199 perror("spawn_server: pipe");
200 return FAIL;
201 }
202 #else
203 /* avoid using uninitialized variable. */
204 fds[0] = fds[1] = 0;
205 #endif
206
207 child = fork();
208
209 ONN("fork server", child == -1);
210
211 if (child == 0) {
212 /* this is the child. */
213 int ret;
214
215 ret = server_child(fds[1], addr, port, fn, ud);
216
217 #ifdef USE_PIPE
218 close(fds[0]);
219 close(fds[1]);
220 #endif
221
222 /* print the error out otherwise it gets lost. */
223 if (ret) {
224 printf("server child failed: %s\n", test_context);
225 }
226
227 /* and quit the child. */
228 NE_DEBUG(NE_DBG_HTTP, "child exiting with %d\n", ret);
229 exit(ret);
230 } else {
231 char ch;
232
233 #ifdef USE_PIPE
234 if (read(fds[0], &ch, 1) < 0)
235 perror("parent read");
236
237 close(fds[0]);
238 close(fds[1]);
239 #else
240 minisleep();
241 #endif
242
243 return OK;
244 }
245 }
246
247 int spawn_server_repeat(int port, server_fn fn, void *userdata, int n)
248 {
249 int fds[2];
250
251 #ifdef USE_PIPE
252 if (pipe(fds)) {
253 perror("spawn_server: pipe");
254 return FAIL;
255 }
256 #else
257 /* avoid using uninitialized variable. */
258 fds[0] = fds[1] = 0;
259 #endif
260
261 child = fork();
262
263 if (child == 0) {
264 /* this is the child. */
265 int listener, count = 0;
266
267 in_child();
268
269 listener = do_listen(lh_addr, port);
270
271 #ifdef USE_PIPE
272 if (write(fds[1], "Z", 1) != 1) abort();
273 #endif
274
275 close(fds[1]);
276 close(fds[0]);
277
278 /* Loop serving requests. */
279 while (++count < n) {
280 ne_socket *sock = ne_sock_create();
281 int ret;
282
283 NE_DEBUG(NE_DBG_HTTP, "child awaiting connection #%d.\n", count);
284 ONN("accept failed", ne_sock_accept(sock, listener));
285 ret = fn(sock, userdata);
286 close_socket(sock);
287 NE_DEBUG(NE_DBG_HTTP, "child served request, %d.\n", ret);
288 if (ret) {
289 printf("server child failed: %s\n", test_context);
290 exit(-1);
291 }
292 /* don't send back notification to parent more than
293 * once. */
294 }
295
296 NE_DEBUG(NE_DBG_HTTP, "child aborted.\n");
297 close(listener);
298
299 exit(-1);
300
301 } else {
302 char ch;
303 /* this is the parent. wait for the child to get ready */
304 #ifdef USE_PIPE
305 if (read(fds[0], &ch, 1) < 0)
306 perror("parent read");
307
308 close(fds[0]);
309 close(fds[1]);
310 #else
311 minisleep();
312 #endif
313 }
314
315 return OK;
316 }
317
318 int dead_server(void)
319 {
320 int status;
321
322 if (waitpid(child, &status, WNOHANG)) {
323 /* child quit already! */
324 return FAIL;
325 }
326
327 NE_DEBUG(NE_DBG_HTTP, "child has not quit.\n");
328
329 return OK;
330 }
331
332
333 int await_server(void)
334 {
335 int status, code;
336
337 (void) wait(&status);
338
339 /* so that we aren't reaped by mistake. */
340 child = 0;
341
342 if (WIFEXITED(status)) {
343 code = WEXITSTATUS(status);
344
345 ONV(code,
346 ("server process terminated abnormally: %s (%d)",
347 code == FAIL ? "FAIL" : "error", code));
348 }
349 else {
350 ONV(WIFSIGNALED(status),
351 ("server process terminated by signal %d", WTERMSIG(status)));
352 }
353
354 return OK;
355 }
356
357 int reap_server(void)
358 {
359 int status;
360
361 if (child != 0) {
362 (void) kill(child, SIGTERM);
363 minisleep();
364 (void) wait(&status);
365 child = 0;
366 }
367
368 return OK;
369 }
370
371 ssize_t server_send(ne_socket *sock, const char *str, size_t len)
372 {
373 NE_DEBUG(NE_DBG_HTTP, "Sending: %.*s\n", (int)len, str);
374 return ne_sock_fullwrite(sock, str, len);
375 }
376
377 int discard_request(ne_socket *sock)
378 {
379 char buffer[1024];
380 size_t offset = want_header?strlen(want_header):0;
381
382 clength = 0;
383
384 NE_DEBUG(NE_DBG_HTTP, "Discarding request...\n");
385 do {
386 ONV(ne_sock_readline(sock, buffer, 1024) < 0,
387 ("error reading line: %s", ne_sock_error(sock)));
388 NE_DEBUG(NE_DBG_HTTP, "[req] %s", buffer);
389 if (strncasecmp(buffer, "content-length:", 15) == 0) {
390 clength = atoi(buffer + 16);
391 }
392 if (got_header != NULL && want_header != NULL &&
393 strncasecmp(buffer, want_header, offset) == 0 &&
394 buffer[offset] == ':') {
395 char *value = buffer + offset + 1;
396 if (*value == ' ') value++;
397 got_header(ne_shave(value, "\r\n"));
398 }
399 } while (strcmp(buffer, "\r\n") != 0);
400
401 return OK;
402 }
403
404 int discard_body(ne_socket *sock)
405 {
406 while (clength > 0) {
407 char buf[BUFSIZ];
408 size_t bytes = clength;
409 ssize_t ret;
410 if (bytes > sizeof(buf)) bytes = sizeof(buf);
411 NE_DEBUG(NE_DBG_HTTP, "Discarding %" NE_FMT_SIZE_T " bytes.\n",
412 bytes);
413 ret = ne_sock_read(sock, buf, bytes);
414 ONV(ret < 0, ("socket read failed (%" NE_FMT_SSIZE_T "): %s",
415 ret, ne_sock_error(sock)));
416 clength -= ret;
417 NE_DEBUG(NE_DBG_HTTP, "Got %" NE_FMT_SSIZE_T " bytes.\n", ret);
418 }
419 NE_DEBUG(NE_DBG_HTTP, "Discard successful.\n");
420 return OK;
421 }
422
423 int serve_file(ne_socket *sock, void *ud)
424 {
425 char buffer[BUFSIZ];
426 struct stat st;
427 struct serve_file_args *args = ud;
428 ssize_t ret;
429 int fd;
430
431 CALL(discard_request(sock));
432
433 ne_sock_fullread(sock, buffer, clength);
434
435 fd = open(args->fname, O_RDONLY);
436 if (fd < 0) {
437 SEND_STRING(sock,
438 "HTTP/1.0 404 File Not Found\r\n"
439 "Content-Length: 0\r\n\r\n");
440 return 0;
441 }
442
443 ONN("fstat fd", fstat(fd, &st));
444
445 SEND_STRING(sock, "HTTP/1.0 200 OK\r\n");
446 if (args->chunks) {
447 sprintf(buffer, "Transfer-Encoding: chunked\r\n");
448 } else {
449 sprintf(buffer, "Content-Length: %" NE_FMT_OFF_T "\r\n",
450 st.st_size);
451 }
452
453 if (args->headers) {
454 strcat(buffer, args->headers);
455 }
456
457 strcat(buffer, "\r\n");
458
459 SEND_STRING(sock, buffer);
460
461 NE_DEBUG(NE_DBG_HTTP, "Serving %s (%" NE_FMT_OFF_T " bytes).\n",
462 args->fname, st.st_size);
463
464 if (args->chunks) {
465 char buf[1024];
466
467 while ((ret = read(fd, &buf, args->chunks)) > 0) {
468 /* this is a small integer, cast it explicitly to avoid
469 * warnings with printing an ssize_t. */
470 sprintf(buffer, "%x\r\n", (unsigned int)ret);
471 SEND_STRING(sock, buffer);
472 ONN("writing body", ne_sock_fullwrite(sock, buf, ret));
473 SEND_STRING(sock, "\r\n");
474 }
475
476 SEND_STRING(sock, "0\r\n\r\n");
477
478 } else {
479 while ((ret = read(fd, buffer, BUFSIZ)) > 0) {
480 ONN("writing body", ne_sock_fullwrite(sock, buffer, ret));
481 }
482 }
483
484 ONN("error reading from file", ret < 0);
485
486 (void) close(fd);
487
488 return OK;
489 }