/[hydra]/hydra/src/select.c
ViewVC logotype

Contents of /hydra/src/select.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Sep 21 13:53:46 2002 UTC (21 years, 7 months ago) by nmav
Branch: MAIN
Branch point for: boas
File MIME type: text/plain
Initial revision

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.1.2.2 2002/07/23 15:54:52 jnelson 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(&params->block_read_fdset);
50 FD_ZERO(&params->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 if (params->server_s[0].socket != -1) process_requests(params, params->server_s[0]);
94 if (params->server_s[1].socket != -1) process_requests(params, params->server_s[1]);
95
96 if (!params->sigterm_flag && params->total_connections < (max_connections - 10)) {
97 if (params->server_s[0].socket != -1) BOA_FD_SET(params->server_s[0].socket, &params->block_read_fdset); /* server always set */
98 if (params->server_s[1].socket != -1) BOA_FD_SET(params->server_s[1].socket, &params->block_read_fdset); /* server always set */
99 }
100
101 params->req_timeout.tv_sec = ((params->request_ready) ? 0 :
102 (ka_timeout ? ka_timeout : REQUEST_TIMEOUT));
103 params->req_timeout.tv_usec = 0l; /* reset timeout */
104
105 if (select(params->max_fd + 1, &params->block_read_fdset,
106 &params->block_write_fdset, NULL,
107 ((params->request_ready || params->request_block) ? &params->req_timeout : NULL)) == -1) {
108 /* what is the appropriate thing to do here on EBADF */
109 if (errno == EINTR)
110 continue; /* while(1) */
111 else if (errno != EBADF) {
112 DIE("select");
113 }
114 }
115
116 time(&current_time);
117 if (params->server_s[0].socket != -1 && FD_ISSET(params->server_s[0].socket, &params->block_read_fdset))
118 params->server_s[0].pending_requests = 1;
119 if (params->server_s[1].socket != -1 && FD_ISSET(params->server_s[1].socket, &params->block_read_fdset))
120 params->server_s[1].pending_requests = 1;
121 }
122
123 return NULL;
124 }
125
126 /*
127 * Name: fdset_update
128 *
129 * Description: iterate through the blocked requests, checking whether
130 * that file descriptor has been set by select. Update the fd_set to
131 * reflect current status.
132 *
133 * Here, we need to do some things:
134 * - keepalive timeouts simply close
135 * (this is special:: a keepalive timeout is a timeout where
136 keepalive is active but nothing has been read yet)
137 * - regular timeouts close + error
138 * - stuff in buffer and fd ready? write it out
139 * - fd ready for other actions? do them
140 */
141
142 static void fdset_update( server_params* params)
143 {
144 request *current, *next;
145
146 for(current = params->request_block;current;current = next) {
147 time_t time_since = current_time - current->time_last;
148 next = current->next;
149
150 /* hmm, what if we are in "the middle" of a request and not
151 * just waiting for a new one... perhaps check to see if anything
152 * has been read via header position, etc... */
153 if (current->kacount < ka_max && /* we *are* in a keepalive */
154 (time_since >= ka_timeout) && /* ka timeout */
155 !current->logline) /* haven't read anything yet */
156 current->status = DEAD; /* connection keepalive timed out */
157 else if (time_since > REQUEST_TIMEOUT) {
158 log_error_doc(current);
159 fputs("connection timed out\n", stderr);
160 current->status = DEAD;
161 }
162 if (current->buffer_end && current->status < DEAD) {
163 if (FD_ISSET(current->fd, &params->block_write_fdset))
164 ready_request( params, current);
165 else {
166 BOA_FD_SET(current->fd, &params->block_write_fdset);
167 }
168 } else {
169 switch (current->status) {
170 case WRITE:
171 case PIPE_WRITE:
172 if (FD_ISSET(current->fd, &params->block_write_fdset))
173 ready_request( params, current);
174 else {
175 BOA_FD_SET(current->fd, &params->block_write_fdset);
176 }
177 break;
178 case BODY_WRITE:
179 if (FD_ISSET(current->post_data_fd, &params->block_write_fdset))
180 ready_request( params, current);
181 else {
182 BOA_FD_SET(current->post_data_fd, &params->block_write_fdset);
183 }
184 break;
185 case PIPE_READ:
186 if (FD_ISSET(current->data_fd, &params->block_read_fdset))
187 ready_request( params, current);
188 else {
189 BOA_FD_SET(current->data_fd, &params->block_read_fdset);
190 }
191 break;
192 case DONE:
193 if (FD_ISSET(current->fd, &params->block_write_fdset))
194 ready_request( params, current);
195 else {
196 BOA_FD_SET(current->fd, &params->block_write_fdset);
197 }
198 break;
199 case DEAD:
200 ready_request( params, current);
201 break;
202 default:
203 if (FD_ISSET(current->fd, &params->block_read_fdset))
204 ready_request( params, current);
205 else {
206 BOA_FD_SET(current->fd, &params->block_read_fdset);
207 }
208 break;
209 }
210 }
211 current = next;
212 }
213 }
214

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26