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 |
nmav |
1.5 |
#include <signal.h> /* signal */ |
23 |
nmav |
1.1 |
|
24 |
nmav |
1.2 |
/* Hydra Internally handled CGIs (HIC HIC) |
25 |
|
|
* |
26 |
|
|
* How does it work? |
27 |
|
|
* |
28 |
|
|
* A special thread (maybe more than one), is used for CGIs. This tread |
29 |
|
|
* uses one pipe to communicate with the others via hic_send_command(). |
30 |
|
|
* |
31 |
|
|
* If a request for a HIC file arrives, the server goes as a normal CGI |
32 |
nmav |
1.5 |
* creates a pipe, but does not fork. It enqueues a hic_request |
33 |
|
|
* for this (HIC) thread, and sends a SIGUSR2 signal. The HIC thread is then |
34 |
|
|
* waken up, and handles all pending requests. |
35 |
nmav |
1.2 |
* |
36 |
nmav |
1.5 |
* The HIC thread, when receives the message, parses the CGIs, |
37 |
|
|
* one CGI at the time, and sends the output to fd. |
38 |
nmav |
1.2 |
* |
39 |
|
|
* The benefit is that in no case the server is blocked. We may have |
40 |
nmav |
1.5 |
* a performance problem, if the server only uses parsed CGIs. |
41 |
|
|
* |
42 |
nmav |
1.2 |
*/ |
43 |
|
|
|
44 |
nmav |
1.1 |
#ifdef ENABLE_HIC |
45 |
|
|
|
46 |
|
|
#include "hic.h" |
47 |
|
|
|
48 |
nmav |
1.5 |
#ifdef ENABLE_SMP |
49 |
|
|
pthread_mutex_t hic_lock = PTHREAD_MUTEX_INITIALIZER; |
50 |
|
|
#endif |
51 |
|
|
|
52 |
|
|
typedef struct _hic_request { |
53 |
|
|
struct _hic_request *next; |
54 |
|
|
struct _hic_request *prev; |
55 |
|
|
hic_stuff command; |
56 |
|
|
} hic_request; |
57 |
|
|
|
58 |
|
|
hic_request * hic_request_head = NULL; |
59 |
|
|
|
60 |
|
|
static int process_hic_request(hic_request * cmd); |
61 |
|
|
void hic_dequeue(hic_request ** head, hic_request * req); |
62 |
|
|
void hic_enqueue(hic_request ** head, hic_request * req); |
63 |
|
|
int hic_sigchld_flag = 0; /* if we received a SIGCHLD signal, then this flag |
64 |
|
|
* is non zero */ |
65 |
nmav |
1.1 |
|
66 |
nmav |
1.5 |
void *hic_main_loop(void *cml) |
67 |
nmav |
1.1 |
{ |
68 |
nmav |
1.5 |
hic_request* current, *next; |
69 |
nmav |
1.1 |
|
70 |
|
|
while (1) { |
71 |
nmav |
1.5 |
for (current = hic_request_head;current;current=next) { |
72 |
nmav |
1.1 |
|
73 |
nmav |
1.5 |
#ifdef ENABLE_SMP |
74 |
|
|
pthread_mutex_lock(&hic_lock); |
75 |
|
|
#endif |
76 |
|
|
next = current->next; |
77 |
|
|
#ifdef ENABLE_SMP |
78 |
|
|
pthread_mutex_unlock(&hic_lock); |
79 |
|
|
#endif |
80 |
nmav |
1.1 |
|
81 |
nmav |
1.5 |
if (process_hic_request( current) == -1) { |
82 |
nmav |
1.1 |
log_error_time(); |
83 |
|
|
fprintf(stderr, "hic: Error while processing command.\n"); |
84 |
|
|
exit(1); |
85 |
|
|
} |
86 |
|
|
} |
87 |
nmav |
1.5 |
|
88 |
|
|
/* If somebody sends a command, then he should |
89 |
|
|
* send a sigusr2 as well. |
90 |
|
|
*/ |
91 |
|
|
if (!hic_request_head) pause(); |
92 |
|
|
|
93 |
|
|
if (hic_sigchld_flag) |
94 |
|
|
sigchld_run(); |
95 |
|
|
|
96 |
nmav |
1.1 |
|
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
} |
100 |
|
|
|
101 |
nmav |
1.5 |
static int process_hic_request(hic_request * req) |
102 |
nmav |
1.1 |
{ |
103 |
nmav |
1.5 |
hic_module_st *hst; |
104 |
|
|
hic_stuff* cmd = &req->command; |
105 |
|
|
|
106 |
|
|
#ifdef ENABLE_SMP |
107 |
|
|
pthread_mutex_lock(&hic_lock); |
108 |
|
|
#endif |
109 |
|
|
hic_dequeue( &hic_request_head, req); |
110 |
|
|
#ifdef ENABLE_SMP |
111 |
|
|
pthread_mutex_unlock(&hic_lock); |
112 |
|
|
#endif |
113 |
nmav |
1.3 |
|
114 |
nmav |
1.5 |
hst = find_hic_appr_module(get_mime_type(cmd->path_translated), 0); |
115 |
nmav |
1.3 |
if (hst == NULL) { |
116 |
|
|
log_error_time(); |
117 |
nmav |
1.5 |
fprintf(stderr, "Could not find HIC handler for '%s'\n", |
118 |
|
|
cmd->path_translated); |
119 |
|
|
close(cmd->out_fd); |
120 |
|
|
free( req); |
121 |
nmav |
1.3 |
return 0; |
122 |
|
|
} |
123 |
|
|
|
124 |
nmav |
1.5 |
hst->request(cmd); |
125 |
|
|
close(cmd->out_fd); |
126 |
|
|
free( req); |
127 |
nmav |
1.1 |
|
128 |
nmav |
1.3 |
/* |
129 |
nmav |
1.1 |
log_error_time(); |
130 |
|
|
fprintf(stderr, "HIC status: %d wrote: %d\n", cmd->status, cmd->bytes_sent); |
131 |
nmav |
1.3 |
*/ |
132 |
nmav |
1.1 |
|
133 |
|
|
return 0; |
134 |
|
|
} |
135 |
|
|
|
136 |
nmav |
1.5 |
/* Sends a command to a HIC thread. |
137 |
|
|
*/ |
138 |
|
|
int hic_send_command(request * req, int out_fd) |
139 |
nmav |
1.1 |
{ |
140 |
nmav |
1.5 |
hic_request *x; |
141 |
nmav |
1.1 |
|
142 |
nmav |
1.5 |
x = malloc( sizeof( hic_request)); |
143 |
|
|
if (x==NULL) |
144 |
|
|
return -1; |
145 |
|
|
|
146 |
|
|
x->next = x->prev = NULL; |
147 |
|
|
x->command.cgi_env = req->cgi_env; |
148 |
|
|
x->command.cgi_env_max = req->cgi_env_index; |
149 |
|
|
x->command.one_one = 0; |
150 |
|
|
x->command.post_data_fd = req->post_data_fd; |
151 |
|
|
x->command.out_fd = out_fd; |
152 |
|
|
x->command.request_uri = req->request_uri; |
153 |
|
|
x->command.path_translated = req->path_translated; |
154 |
nmav |
1.1 |
|
155 |
|
|
#ifdef ENABLE_SMP |
156 |
nmav |
1.5 |
pthread_mutex_lock(&hic_lock); |
157 |
nmav |
1.1 |
#endif |
158 |
nmav |
1.5 |
|
159 |
|
|
hic_enqueue( &hic_request_head, x); |
160 |
nmav |
1.1 |
#ifdef ENABLE_SMP |
161 |
nmav |
1.5 |
/* Notify the main loop */ |
162 |
|
|
pthread_kill( hic_tid, SIGUSR2); |
163 |
nmav |
1.1 |
#endif |
164 |
|
|
|
165 |
|
|
#ifdef ENABLE_SMP |
166 |
nmav |
1.5 |
pthread_mutex_unlock(&hic_lock); |
167 |
nmav |
1.1 |
#endif |
168 |
|
|
|
169 |
nmav |
1.5 |
return 1; |
170 |
nmav |
1.1 |
|
171 |
|
|
} |
172 |
|
|
|
173 |
nmav |
1.5 |
/* Implement a basic queue for HIC stuff. This is created after |
174 |
|
|
* queue.c. |
175 |
|
|
*/ |
176 |
nmav |
1.1 |
|
177 |
nmav |
1.5 |
#include "queue.h" |
178 |
nmav |
1.1 |
|
179 |
nmav |
1.5 |
ENQUEUE_FUNCTION( hic_enqueue, hic_request) |
180 |
|
|
DEQUEUE_FUNCTION( hic_dequeue, hic_request) |
181 |
nmav |
1.1 |
|
182 |
nmav |
1.5 |
#endif /* ENABLE_HIC */ |