1 |
nmav |
1.1 |
/* |
2 |
nmav |
1.6 |
* Hydra, an http server |
3 |
nmav |
1.1 |
* Copyright (C) 1995 Paul Phillips <paulp@go2net.com> |
4 |
|
|
* Some changes Copyright (C) 1996 Charles F. Randall <crandall@goldsys.com> |
5 |
|
|
* Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org> |
6 |
|
|
* Some changes Copyright (C) 1996-2002 Jon Nelson <jnelson@boa.org> |
7 |
|
|
* Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
8 |
|
|
* |
9 |
|
|
* This program is free software; you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU General Public License as published by |
11 |
|
|
* the Free Software Foundation; either version 1, or (at your option) |
12 |
|
|
* any later version. |
13 |
|
|
* |
14 |
|
|
* This program is distributed in the hope that it will be useful, |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
* GNU General Public License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU General Public License |
20 |
|
|
* along with this program; if not, write to the Free Software |
21 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 |
|
|
* |
23 |
|
|
*/ |
24 |
|
|
|
25 |
nmav |
1.10 |
/* $Id: select.c,v 1.9 2002/10/21 18:46:26 nmav Exp $*/ |
26 |
nmav |
1.1 |
|
27 |
|
|
#include "boa.h" |
28 |
nmav |
1.9 |
#include "loop_signals.h" |
29 |
|
|
|
30 |
|
|
#ifndef USE_POLL |
31 |
nmav |
1.1 |
|
32 |
nmav |
1.10 |
#error HELLO |
33 |
nmav |
1.1 |
static void fdset_update( server_params *); |
34 |
|
|
|
35 |
|
|
/* params->server_s[0] is the plain socket, while the |
36 |
|
|
* params->server_s[1] is the ssl one. |
37 |
|
|
*/ |
38 |
|
|
void* select_loop(void* _params) |
39 |
|
|
{ |
40 |
|
|
server_params* params = _params; |
41 |
|
|
|
42 |
|
|
FD_ZERO(¶ms->block_read_fdset); |
43 |
|
|
FD_ZERO(¶ms->block_write_fdset); |
44 |
|
|
/* set server_s and req_timeout */ |
45 |
|
|
params->req_timeout.tv_sec = (ka_timeout ? ka_timeout : REQUEST_TIMEOUT); |
46 |
|
|
params->req_timeout.tv_usec = 0l; /* reset timeout */ |
47 |
|
|
|
48 |
|
|
/* preset max_fd */ |
49 |
|
|
|
50 |
|
|
while (1) { |
51 |
nmav |
1.3 |
|
52 |
nmav |
1.9 |
handle_signals( params); |
53 |
|
|
|
54 |
nmav |
1.1 |
/* reset max_fd */ |
55 |
|
|
params->max_fd = -1; |
56 |
nmav |
1.9 |
|
57 |
nmav |
1.1 |
if (params->request_block) |
58 |
nmav |
1.9 |
/* move selected req's from request_block to request_ready */ |
59 |
|
|
fdset_update( params); |
60 |
|
|
|
61 |
nmav |
1.1 |
/* any blocked req's move from request_ready to request_block */ |
62 |
nmav |
1.2 |
if (params->server_s[0].socket != -1) process_requests(params, ¶ms->server_s[0]); |
63 |
|
|
#ifdef ENABLE_SSL |
64 |
|
|
if (params->server_s[1].socket != -1) process_requests(params, ¶ms->server_s[1]); |
65 |
|
|
#endif |
66 |
nmav |
1.1 |
|
67 |
nmav |
1.7 |
if (!params->sigterm_flag) { |
68 |
nmav |
1.9 |
if (params->server_s[0].socket != -1) BOA_FD_SET(req, params->server_s[0].socket, ¶ms->block_read_fdset); |
69 |
nmav |
1.2 |
#ifdef ENABLE_SSL |
70 |
nmav |
1.9 |
if (params->server_s[1].socket != -1) BOA_FD_SET(req, params->server_s[1].socket, ¶ms->block_read_fdset); |
71 |
nmav |
1.2 |
#endif |
72 |
nmav |
1.1 |
} |
73 |
nmav |
1.9 |
|
74 |
nmav |
1.1 |
params->req_timeout.tv_sec = ((params->request_ready) ? 0 : |
75 |
|
|
(ka_timeout ? ka_timeout : REQUEST_TIMEOUT)); |
76 |
|
|
params->req_timeout.tv_usec = 0l; /* reset timeout */ |
77 |
|
|
|
78 |
|
|
if (select(params->max_fd + 1, ¶ms->block_read_fdset, |
79 |
|
|
¶ms->block_write_fdset, NULL, |
80 |
nmav |
1.9 |
((params->request_ready || params->request_block) ? ¶ms->req_timeout : NULL)) == -1) |
81 |
|
|
{ |
82 |
nmav |
1.1 |
/* what is the appropriate thing to do here on EBADF */ |
83 |
|
|
if (errno == EINTR) |
84 |
|
|
continue; /* while(1) */ |
85 |
|
|
else if (errno != EBADF) { |
86 |
|
|
DIE("select"); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
if (params->server_s[0].socket != -1 && FD_ISSET(params->server_s[0].socket, ¶ms->block_read_fdset)) |
91 |
|
|
params->server_s[0].pending_requests = 1; |
92 |
nmav |
1.2 |
#ifdef ENABLE_SSL |
93 |
nmav |
1.1 |
if (params->server_s[1].socket != -1 && FD_ISSET(params->server_s[1].socket, ¶ms->block_read_fdset)) |
94 |
|
|
params->server_s[1].pending_requests = 1; |
95 |
nmav |
1.2 |
#endif |
96 |
nmav |
1.1 |
} |
97 |
|
|
|
98 |
|
|
return NULL; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* Name: fdset_update |
103 |
|
|
* |
104 |
|
|
* Description: iterate through the blocked requests, checking whether |
105 |
|
|
* that file descriptor has been set by select. Update the fd_set to |
106 |
|
|
* reflect current status. |
107 |
|
|
* |
108 |
|
|
* Here, we need to do some things: |
109 |
|
|
* - keepalive timeouts simply close |
110 |
|
|
* (this is special:: a keepalive timeout is a timeout where |
111 |
|
|
keepalive is active but nothing has been read yet) |
112 |
|
|
* - regular timeouts close + error |
113 |
|
|
* - stuff in buffer and fd ready? write it out |
114 |
|
|
* - fd ready for other actions? do them |
115 |
|
|
*/ |
116 |
|
|
|
117 |
|
|
static void fdset_update( server_params* params) |
118 |
|
|
{ |
119 |
|
|
request *current, *next; |
120 |
|
|
|
121 |
|
|
for(current = params->request_block;current;current = next) { |
122 |
|
|
time_t time_since = current_time - current->time_last; |
123 |
|
|
next = current->next; |
124 |
|
|
|
125 |
|
|
/* hmm, what if we are in "the middle" of a request and not |
126 |
|
|
* just waiting for a new one... perhaps check to see if anything |
127 |
|
|
* has been read via header position, etc... */ |
128 |
|
|
if (current->kacount < ka_max && /* we *are* in a keepalive */ |
129 |
|
|
(time_since >= ka_timeout) && /* ka timeout */ |
130 |
|
|
!current->logline) /* haven't read anything yet */ |
131 |
|
|
current->status = DEAD; /* connection keepalive timed out */ |
132 |
|
|
else if (time_since > REQUEST_TIMEOUT) { |
133 |
|
|
log_error_doc(current); |
134 |
|
|
fputs("connection timed out\n", stderr); |
135 |
|
|
current->status = DEAD; |
136 |
|
|
} |
137 |
|
|
if (current->buffer_end && current->status < DEAD) { |
138 |
|
|
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
139 |
|
|
ready_request( params, current); |
140 |
|
|
else { |
141 |
nmav |
1.9 |
BOA_FD_SET( current, current->fd, ¶ms->block_write_fdset); |
142 |
nmav |
1.1 |
} |
143 |
|
|
} else { |
144 |
|
|
switch (current->status) { |
145 |
nmav |
1.9 |
case IOSHUFFLE: |
146 |
|
|
#ifndef HAVE_SENDFILE |
147 |
|
|
if (current->buffer_end - current->buffer_start == 0) { |
148 |
|
|
if (FD_ISSET(current->data_fd, &block_read_fdset)) |
149 |
|
|
ready_request(params, current); |
150 |
|
|
break; |
151 |
|
|
} |
152 |
|
|
#endif |
153 |
nmav |
1.1 |
case WRITE: |
154 |
|
|
case PIPE_WRITE: |
155 |
|
|
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
156 |
|
|
ready_request( params, current); |
157 |
|
|
else { |
158 |
nmav |
1.9 |
BOA_FD_SET( current, current->fd, ¶ms->block_write_fdset); |
159 |
nmav |
1.1 |
} |
160 |
|
|
break; |
161 |
|
|
case BODY_WRITE: |
162 |
|
|
if (FD_ISSET(current->post_data_fd, ¶ms->block_write_fdset)) |
163 |
|
|
ready_request( params, current); |
164 |
|
|
else { |
165 |
nmav |
1.9 |
BOA_FD_SET( current, current->post_data_fd, ¶ms->block_write_fdset); |
166 |
nmav |
1.1 |
} |
167 |
|
|
break; |
168 |
|
|
case PIPE_READ: |
169 |
|
|
if (FD_ISSET(current->data_fd, ¶ms->block_read_fdset)) |
170 |
|
|
ready_request( params, current); |
171 |
|
|
else { |
172 |
nmav |
1.9 |
BOA_FD_SET( current, current->data_fd, ¶ms->block_read_fdset); |
173 |
nmav |
1.1 |
} |
174 |
|
|
break; |
175 |
|
|
case DONE: |
176 |
|
|
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
177 |
|
|
ready_request( params, current); |
178 |
|
|
else { |
179 |
nmav |
1.9 |
BOA_FD_SET( current, current->fd, ¶ms->block_write_fdset); |
180 |
nmav |
1.1 |
} |
181 |
|
|
break; |
182 |
|
|
case DEAD: |
183 |
|
|
ready_request( params, current); |
184 |
|
|
break; |
185 |
|
|
default: |
186 |
|
|
if (FD_ISSET(current->fd, ¶ms->block_read_fdset)) |
187 |
|
|
ready_request( params, current); |
188 |
|
|
else { |
189 |
nmav |
1.9 |
BOA_FD_SET( current, current->fd, ¶ms->block_read_fdset); |
190 |
nmav |
1.1 |
} |
191 |
|
|
break; |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
current = next; |
195 |
|
|
} |
196 |
|
|
} |
197 |
|
|
|
198 |
nmav |
1.9 |
#endif /* USE_POLL */ |