1 |
/* |
2 |
* Boa, an http server |
3 |
* 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 |
/* $Id: select.c,v 1.3 2002/09/23 19:28:41 nmav Exp $*/ |
26 |
|
27 |
#include "boa.h" |
28 |
#ifdef ENABLE_SMP |
29 |
# include <pthread.h> |
30 |
|
31 |
extern pthread_t father_id; |
32 |
|
33 |
#endif |
34 |
|
35 |
static void fdset_update( server_params *); |
36 |
|
37 |
/* params->server_s[0] is the plain socket, while the |
38 |
* params->server_s[1] is the ssl one. |
39 |
*/ |
40 |
void* select_loop(void* _params) |
41 |
{ |
42 |
server_params* params = _params; |
43 |
|
44 |
#ifdef ENABLE_SMP |
45 |
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL); |
46 |
pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL); |
47 |
#endif |
48 |
|
49 |
FD_ZERO(¶ms->block_read_fdset); |
50 |
FD_ZERO(¶ms->block_write_fdset); |
51 |
/* set server_s and req_timeout */ |
52 |
params->req_timeout.tv_sec = (ka_timeout ? ka_timeout : REQUEST_TIMEOUT); |
53 |
params->req_timeout.tv_usec = 0l; /* reset timeout */ |
54 |
|
55 |
/* preset max_fd */ |
56 |
params->max_fd = -1; |
57 |
|
58 |
while (1) { |
59 |
|
60 |
if (params->sigchld_flag) |
61 |
sigchld_run(); |
62 |
|
63 |
#ifdef ENABLE_SMP |
64 |
/* Only the main thread handles signals. |
65 |
*/ |
66 |
if (pthread_equal( params->tid, father_id)) { |
67 |
#endif |
68 |
/* Calculate current time. Moved here, so only one thread |
69 |
* calls this. |
70 |
*/ |
71 |
time(¤t_time); |
72 |
|
73 |
if (params->sighup_flag) |
74 |
sighup_run(); |
75 |
if (params->sigalrm_flag) |
76 |
sigalrm_run(); |
77 |
if (params->sigusr1_flag) |
78 |
sigusr1_run(); |
79 |
if (params->sigterm_flag) { |
80 |
if (params->sigterm_flag == 1) { |
81 |
sigterm_stage1_run(); |
82 |
} |
83 |
if (params->sigterm_flag == 2 && !params->request_ready && !params->request_block) { |
84 |
sigterm_stage2_run(); |
85 |
} |
86 |
} |
87 |
#ifdef ENABLE_SMP |
88 |
} |
89 |
#endif |
90 |
|
91 |
/* reset max_fd */ |
92 |
params->max_fd = -1; |
93 |
|
94 |
if (params->request_block) |
95 |
/* move selected req's from request_block to request_ready */ |
96 |
fdset_update( params); |
97 |
|
98 |
/* any blocked req's move from request_ready to request_block */ |
99 |
if (params->server_s[0].socket != -1) process_requests(params, ¶ms->server_s[0]); |
100 |
#ifdef ENABLE_SSL |
101 |
if (params->server_s[1].socket != -1) process_requests(params, ¶ms->server_s[1]); |
102 |
#endif |
103 |
|
104 |
if (!params->sigterm_flag && params->total_connections < (max_connections - 10)) { |
105 |
if (params->server_s[0].socket != -1) BOA_FD_SET(params->server_s[0].socket, ¶ms->block_read_fdset); /* server always set */ |
106 |
#ifdef ENABLE_SSL |
107 |
if (params->server_s[1].socket != -1) BOA_FD_SET(params->server_s[1].socket, ¶ms->block_read_fdset); /* server always set */ |
108 |
#endif |
109 |
} |
110 |
|
111 |
params->req_timeout.tv_sec = ((params->request_ready) ? 0 : |
112 |
(ka_timeout ? ka_timeout : REQUEST_TIMEOUT)); |
113 |
params->req_timeout.tv_usec = 0l; /* reset timeout */ |
114 |
|
115 |
if (select(params->max_fd + 1, ¶ms->block_read_fdset, |
116 |
¶ms->block_write_fdset, NULL, |
117 |
((params->request_ready || params->request_block) ? ¶ms->req_timeout : NULL)) == -1) { |
118 |
/* what is the appropriate thing to do here on EBADF */ |
119 |
if (errno == EINTR) |
120 |
continue; /* while(1) */ |
121 |
else if (errno != EBADF) { |
122 |
DIE("select"); |
123 |
} |
124 |
} |
125 |
|
126 |
if (params->server_s[0].socket != -1 && FD_ISSET(params->server_s[0].socket, ¶ms->block_read_fdset)) |
127 |
params->server_s[0].pending_requests = 1; |
128 |
#ifdef ENABLE_SSL |
129 |
if (params->server_s[1].socket != -1 && FD_ISSET(params->server_s[1].socket, ¶ms->block_read_fdset)) |
130 |
params->server_s[1].pending_requests = 1; |
131 |
#endif |
132 |
} |
133 |
|
134 |
return NULL; |
135 |
} |
136 |
|
137 |
/* |
138 |
* Name: fdset_update |
139 |
* |
140 |
* Description: iterate through the blocked requests, checking whether |
141 |
* that file descriptor has been set by select. Update the fd_set to |
142 |
* reflect current status. |
143 |
* |
144 |
* Here, we need to do some things: |
145 |
* - keepalive timeouts simply close |
146 |
* (this is special:: a keepalive timeout is a timeout where |
147 |
keepalive is active but nothing has been read yet) |
148 |
* - regular timeouts close + error |
149 |
* - stuff in buffer and fd ready? write it out |
150 |
* - fd ready for other actions? do them |
151 |
*/ |
152 |
|
153 |
static void fdset_update( server_params* params) |
154 |
{ |
155 |
request *current, *next; |
156 |
|
157 |
for(current = params->request_block;current;current = next) { |
158 |
time_t time_since = current_time - current->time_last; |
159 |
next = current->next; |
160 |
|
161 |
/* hmm, what if we are in "the middle" of a request and not |
162 |
* just waiting for a new one... perhaps check to see if anything |
163 |
* has been read via header position, etc... */ |
164 |
if (current->kacount < ka_max && /* we *are* in a keepalive */ |
165 |
(time_since >= ka_timeout) && /* ka timeout */ |
166 |
!current->logline) /* haven't read anything yet */ |
167 |
current->status = DEAD; /* connection keepalive timed out */ |
168 |
else if (time_since > REQUEST_TIMEOUT) { |
169 |
log_error_doc(current); |
170 |
fputs("connection timed out\n", stderr); |
171 |
current->status = DEAD; |
172 |
} |
173 |
if (current->buffer_end && current->status < DEAD) { |
174 |
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
175 |
ready_request( params, current); |
176 |
else { |
177 |
BOA_FD_SET(current->fd, ¶ms->block_write_fdset); |
178 |
} |
179 |
} else { |
180 |
switch (current->status) { |
181 |
case WRITE: |
182 |
case PIPE_WRITE: |
183 |
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
184 |
ready_request( params, current); |
185 |
else { |
186 |
BOA_FD_SET(current->fd, ¶ms->block_write_fdset); |
187 |
} |
188 |
break; |
189 |
case BODY_WRITE: |
190 |
if (FD_ISSET(current->post_data_fd, ¶ms->block_write_fdset)) |
191 |
ready_request( params, current); |
192 |
else { |
193 |
BOA_FD_SET(current->post_data_fd, ¶ms->block_write_fdset); |
194 |
} |
195 |
break; |
196 |
case PIPE_READ: |
197 |
if (FD_ISSET(current->data_fd, ¶ms->block_read_fdset)) |
198 |
ready_request( params, current); |
199 |
else { |
200 |
BOA_FD_SET(current->data_fd, ¶ms->block_read_fdset); |
201 |
} |
202 |
break; |
203 |
case DONE: |
204 |
if (FD_ISSET(current->fd, ¶ms->block_write_fdset)) |
205 |
ready_request( params, current); |
206 |
else { |
207 |
BOA_FD_SET(current->fd, ¶ms->block_write_fdset); |
208 |
} |
209 |
break; |
210 |
case DEAD: |
211 |
ready_request( params, current); |
212 |
break; |
213 |
default: |
214 |
if (FD_ISSET(current->fd, ¶ms->block_read_fdset)) |
215 |
ready_request( params, current); |
216 |
else { |
217 |
BOA_FD_SET(current->fd, ¶ms->block_read_fdset); |
218 |
} |
219 |
break; |
220 |
} |
221 |
} |
222 |
current = next; |
223 |
} |
224 |
} |
225 |
|