1 |
nmav |
1.1 |
/* |
2 |
|
|
* Copyright (C) 2002 Nikos Mavroyanopoulos |
3 |
|
|
* |
4 |
|
|
* This file is part of BOA webserver. |
5 |
|
|
* |
6 |
|
|
* Boa is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* Boa is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "boa.h" |
22 |
|
|
|
23 |
|
|
#ifdef ENABLE_HIC |
24 |
|
|
|
25 |
|
|
#include "hic.h" |
26 |
|
|
|
27 |
|
|
static ssize_t full_read(int fd, void *buf, size_t count); |
28 |
|
|
static ssize_t full_write(int fd, const void *buf, size_t count); |
29 |
|
|
static int process_command(hic_stuff * cmd); |
30 |
|
|
|
31 |
|
|
void* hic_main_loop( void* cml) |
32 |
|
|
{ |
33 |
|
|
int max_fd, ret; |
34 |
|
|
struct timeval req_timeout; |
35 |
|
|
fd_set read_fdset; |
36 |
|
|
hic_stuff cmd; |
37 |
|
|
int *command_line = cml; /* this is an int[2] */ |
38 |
|
|
|
39 |
|
|
_php_hic_init(); |
40 |
|
|
|
41 |
|
|
FD_ZERO(&read_fdset); |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
while (1) { |
45 |
|
|
|
46 |
|
|
time(¤t_time); |
47 |
|
|
|
48 |
|
|
req_timeout.tv_sec = REQUEST_TIMEOUT; |
49 |
|
|
req_timeout.tv_usec = 0l; /* reset timeout */ |
50 |
|
|
|
51 |
|
|
max_fd = -1; |
52 |
|
|
|
53 |
|
|
FD_SET(command_line[0], &read_fdset); |
54 |
|
|
max_fd = command_line[0]; |
55 |
|
|
|
56 |
|
|
if (select(max_fd + 1, &read_fdset, NULL, NULL, &req_timeout) == -1) { |
57 |
|
|
if (errno == EINTR) |
58 |
|
|
continue; /* while(1) */ |
59 |
|
|
else if (errno != EBADF) { |
60 |
|
|
log_error_time(); |
61 |
|
|
fprintf(stderr, "hic: Error while processing command. %s.\n", strerror(errno)); |
62 |
|
|
exit(1); |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
if (FD_ISSET(command_line[0], &read_fdset)) { |
67 |
|
|
/* read a command */ |
68 |
|
|
if ((ret = |
69 |
|
|
full_read(command_line[0], &cmd, |
70 |
|
|
sizeof(cmd))) < sizeof(cmd)) { |
71 |
|
|
log_error_time(); |
72 |
|
|
if (ret == 0) { /* normal shutdown */ |
73 |
|
|
log_error_time(); |
74 |
|
|
fprintf(stderr, "Shutting down..."); |
75 |
|
|
close(command_line[0]); |
76 |
|
|
return NULL; |
77 |
|
|
} |
78 |
|
|
fprintf(stderr, "hic: Error while receiving command. %s.\n", |
79 |
|
|
strerror(errno)); |
80 |
|
|
exit(1); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
if (process_command(&cmd) == -1) { |
84 |
|
|
log_error_time(); |
85 |
|
|
fprintf(stderr, "hic: Error while processing command.\n"); |
86 |
|
|
exit(1); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
static int process_command(hic_stuff * cmd) |
95 |
|
|
{ |
96 |
|
|
_php_hic_request( cmd); |
97 |
|
|
close( cmd->out_fd); |
98 |
|
|
|
99 |
|
|
log_error_time(); |
100 |
|
|
fprintf(stderr, "HIC status: %d wrote: %d\n", cmd->status, cmd->bytes_sent); |
101 |
|
|
|
102 |
|
|
return 0; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
static ssize_t full_read(int fd, void *buf, size_t count) |
106 |
|
|
{ |
107 |
|
|
size_t nleft; |
108 |
|
|
ssize_t nread; |
109 |
|
|
char *ptr; |
110 |
|
|
|
111 |
|
|
ptr = buf; |
112 |
|
|
nleft = count; |
113 |
|
|
while (nleft > 0) { |
114 |
|
|
if ((nread = read(fd, ptr, nleft)) == -1) { |
115 |
|
|
if (errno == EINTR) { |
116 |
|
|
nread = 0; |
117 |
|
|
} else |
118 |
|
|
return -1; |
119 |
|
|
} else if (nread == 0) |
120 |
|
|
break; |
121 |
|
|
|
122 |
|
|
nleft -= nread; |
123 |
|
|
ptr += nread; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
return count - nleft; |
127 |
|
|
|
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
static ssize_t full_write(int fd, const void *buf, size_t count) |
131 |
|
|
{ |
132 |
|
|
size_t nleft; |
133 |
|
|
ssize_t nwritten; |
134 |
|
|
const char *ptr; |
135 |
|
|
|
136 |
|
|
ptr = buf; |
137 |
|
|
nleft = count; |
138 |
|
|
while (nleft > 0) { |
139 |
|
|
if ((nwritten = write(fd, ptr, nleft)) == -1) { |
140 |
|
|
if (errno == EINTR) { |
141 |
|
|
nwritten = 0; |
142 |
|
|
} else |
143 |
|
|
return -1; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
nleft -= nwritten; |
147 |
|
|
ptr += nwritten; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
return count; |
151 |
|
|
|
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
#ifdef ENABLE_SMP |
155 |
|
|
pthread_mutex_t hic_lock = PTHREAD_MUTEX_INITIALIZER; |
156 |
|
|
#endif |
157 |
|
|
|
158 |
|
|
extern int hic_write_fd; |
159 |
|
|
|
160 |
|
|
/* Sends a command to a HIC thread. |
161 |
|
|
*/ |
162 |
|
|
int hic_send_command( request *req, int out_fd) |
163 |
|
|
{ |
164 |
|
|
hic_stuff x; |
165 |
|
|
int ret; |
166 |
|
|
|
167 |
|
|
x.cgi_env = req->cgi_env; |
168 |
|
|
x.cgi_env_max = req->cgi_env_index; |
169 |
|
|
x.one_one = 0; |
170 |
|
|
x.post_data_fd = req->post_data_fd; |
171 |
|
|
x.out_fd = out_fd; |
172 |
|
|
x.request_uri = req->request_uri; |
173 |
|
|
x.path_translated = req->path_translated; |
174 |
|
|
|
175 |
|
|
#ifdef ENABLE_SMP |
176 |
|
|
pthread_mutex_lock( &hic_lock); |
177 |
|
|
#endif |
178 |
|
|
|
179 |
|
|
ret = full_write( hic_write_fd, &x, sizeof(x)); |
180 |
|
|
|
181 |
|
|
#ifdef ENABLE_SMP |
182 |
|
|
pthread_mutex_unlock( &hic_lock); |
183 |
|
|
#endif |
184 |
|
|
|
185 |
|
|
if (ret==sizeof(x)) return 1; |
186 |
|
|
|
187 |
|
|
log_error_time(); |
188 |
|
|
fprintf(stderr, "Error sending to HIC thread.\n"); |
189 |
|
|
/* this should be a fatal error */ |
190 |
|
|
|
191 |
|
|
return -1; |
192 |
|
|
|
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
#endif /* ENABLE_HIC */ |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
#ifdef TEST |
200 |
|
|
|
201 |
|
|
int main() |
202 |
|
|
{ |
203 |
|
|
int fd[2]; |
204 |
|
|
hic_stuff x; |
205 |
|
|
pthread_t tid; |
206 |
|
|
|
207 |
|
|
_php_hic_init(); |
208 |
|
|
|
209 |
|
|
memset( &x, 0, sizeof(x)); |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
x.remote_address="127.0.0.1"; |
213 |
|
|
x.server_port = 8080; |
214 |
|
|
x.out_fd = STDOUT_FILENO; |
215 |
|
|
x.script_name = "/test.php"; |
216 |
|
|
x.content_type = "text/html; charset=ISO-8859-7"; |
217 |
|
|
x.query_string = "STRING"; |
218 |
|
|
x.path_translated = "/tmp/test.php"; |
219 |
|
|
x.request_uri = "/test.php"; |
220 |
|
|
x.path_info = "/tmp/test.php"; |
221 |
|
|
x.str_method = "GET"; |
222 |
|
|
x.server_name = "Hydra.localhost"; |
223 |
|
|
x.server_port = 80; |
224 |
|
|
x.http_version = "HTTP/1.1"; |
225 |
|
|
x.post_data_fd = -1; |
226 |
|
|
|
227 |
|
|
if (pipe( fd) < 0) { |
228 |
|
|
fprintf(stderr, "ERROR IN PIPE\n"); |
229 |
|
|
exit(1); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
pthread_create( &tid, NULL, &hic_main_loop, (void*)fd); |
233 |
|
|
|
234 |
|
|
fprintf(stderr, "Spawned %u\n", tid); |
235 |
|
|
sleep(2); |
236 |
|
|
|
237 |
|
|
fprintf(stderr, "Written command\n"); |
238 |
|
|
write( fd[1], &x, sizeof(x)); |
239 |
|
|
|
240 |
|
|
sleep(5); |
241 |
|
|
|
242 |
|
|
close(fd[1]); |
243 |
|
|
|
244 |
|
|
pause(); |
245 |
|
|
|
246 |
|
|
return 0; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
#endif |
250 |
|
|
|