diff options
Diffstat (limited to 'src/testzzuf/socat.c')
-rw-r--r-- | src/testzzuf/socat.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/testzzuf/socat.c b/src/testzzuf/socat.c new file mode 100644 index 00000000..b8948575 --- /dev/null +++ b/src/testzzuf/socat.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2008 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file socat.c | ||
23 | * @brief Code to fork-exec zzuf and start the socat process | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include <errno.h> | ||
28 | #include <sys/types.h> | ||
29 | #include <sys/wait.h> | ||
30 | #include <signal.h> | ||
31 | |||
32 | |||
33 | static pid_t zzuf_pid; | ||
34 | |||
35 | static void | ||
36 | zzuf_socat_start() { | ||
37 | int status; | ||
38 | char * const args[] = { | ||
39 | "zzuf", | ||
40 | "--ratio=0.0:0.75", | ||
41 | "-n", | ||
42 | "-A", | ||
43 | "socat", | ||
44 | "TCP4-LISTEN:11081,reuseaddr,fork", | ||
45 | "TCP4:127.0.0.1:11080", | ||
46 | NULL, | ||
47 | }; | ||
48 | zzuf_pid = fork(); | ||
49 | if (zzuf_pid == -1) { | ||
50 | fprintf(stderr, | ||
51 | "fork failed: %s\n", | ||
52 | strerror(errno)); | ||
53 | exit(1); | ||
54 | } | ||
55 | if (zzuf_pid != 0) | ||
56 | { | ||
57 | sleep(1); /* allow zzuf and socat to start */ | ||
58 | status = 0; | ||
59 | if (0 < waitpid(zzuf_pid, &status, WNOHANG)) | ||
60 | { | ||
61 | if (WIFEXITED(status)) | ||
62 | fprintf(stderr, | ||
63 | "zzuf died with status code %d!\n", | ||
64 | WEXITSTATUS(status)); | ||
65 | if (WIFSIGNALED(status)) | ||
66 | fprintf(stderr, | ||
67 | "zzuf died from signal %d!\n", | ||
68 | WTERMSIG(status)); | ||
69 | exit(1); | ||
70 | } | ||
71 | return; | ||
72 | } | ||
73 | setpgrp(); | ||
74 | execvp("zzuf", | ||
75 | args); | ||
76 | fprintf(stderr, | ||
77 | "execution of `zzuf' failed: %s\n", | ||
78 | strerror(errno)); | ||
79 | zzuf_pid = 0; /* fork failed */ | ||
80 | exit(1); | ||
81 | } | ||
82 | |||
83 | |||
84 | static void | ||
85 | zzuf_socat_stop() { | ||
86 | int status; | ||
87 | if (zzuf_pid != 0) | ||
88 | { | ||
89 | if (0 != killpg(zzuf_pid, SIGINT)) | ||
90 | fprintf(stderr, | ||
91 | "Failed to killpg: %s\n", | ||
92 | strerror(errno)); | ||
93 | kill(zzuf_pid, SIGINT); | ||
94 | waitpid(zzuf_pid, &status, 0); | ||
95 | sleep(1); /* allow socat to also die in peace */ | ||
96 | } | ||
97 | } | ||
98 | |||
99 | /* end of socat.c */ | ||