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