summaryrefslogtreecommitdiffabout
path: root/src/sitecing-fastcgi.cc
authorMichael Krelin <hacker@klever.net>2005-01-29 21:21:05 (UTC)
committer Michael Krelin <hacker@klever.net>2005-01-29 21:21:05 (UTC)
commitce1f37aae46ea95020d7b865f7a80e8abdfad0d8 (patch) (unidiff)
tree4964383ab8cd7e6d8ea821f1a615d1bbcf98dad8 /src/sitecing-fastcgi.cc
parent3c75c860fc1ad5b3f5185e23ec6f438dd2528958 (diff)
downloadsitecing-ce1f37aae46ea95020d7b865f7a80e8abdfad0d8.zip
sitecing-ce1f37aae46ea95020d7b865f7a80e8abdfad0d8.tar.gz
sitecing-ce1f37aae46ea95020d7b865f7a80e8abdfad0d8.tar.bz2
initial commit into repository0.0
Diffstat (limited to 'src/sitecing-fastcgi.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/sitecing-fastcgi.cc325
1 files changed, 325 insertions, 0 deletions
diff --git a/src/sitecing-fastcgi.cc b/src/sitecing-fastcgi.cc
new file mode 100644
index 0000000..963c257
--- a/dev/null
+++ b/src/sitecing-fastcgi.cc
@@ -0,0 +1,325 @@
1#include <sys/types.h>
2#include <unistd.h>
3#include <signal.h>
4#include <getopt.h>
5#include <pwd.h>
6#include <grp.h>
7#include <errno.h>
8#include <syslog.h>
9#include <iostream>
10#include <memory>
11#include <typeinfo>
12using namespace std;
13#include "kingate/fastcgi.h"
14#include "kingate/cgi_gateway.h"
15using namespace kingate;
16#include "sitecing/sitecing_util.h"
17#include "sitecing/util.h"
18#include "sitecing/sitespace.h"
19#include "sitecing/sitecing_interface_cgi.h"
20#include "sitecing/cgi_component.h"
21#include "sitecing/configuration.h"
22#include "sitecing/magic.h"
23#include "sitecing/sitecing_exception.h"
24#include "sitecing/exception.h"
25#include "sitecing/process_manager.h"
26using namespace sitecing;
27
28#include "config.h"
29#define PHEADER PACKAGE " Version " VERSION
30#define PCOPY "Copyright (c) 2004 Klever Group"
31
32class cdummyClass : public acomponent {
33 public:
34 void main(int _magic,va_list _args) {}
35 void *__the_most_derived_this() { return NULL; }
36} cdummyInstance;
37class adummyClass : public cgi_component {
38 public:
39 void main(int _magic,va_list _args) {}
40 void *__the_most_derived_this() { return NULL; }
41} adummyInstance;
42
43class sitecing_fastcgi_pm : public process_manager {
44 public:
45 configuration config;
46 fcgi_socket *fss;
47 semaphore sem;
48 bool multi;
49 uid_t uid;
50 gid_t gid;
51 pid_file pidfile;
52
53 sitecing_fastcgi_pm(const string& config_file);
54 ~sitecing_fastcgi_pm();
55
56 void process(int slot);
57
58 void run();
59 void give_up_privs();
60};
61
62sitecing_fastcgi_pm::sitecing_fastcgi_pm(const string& config_file)
63 : config(config_file), multi(false) {
64 if(( (config.flags&configuration::flag_user) || (config.flags&configuration::flag_group) || (config.flags&configuration::flag_chroot) ) && geteuid() )
65 throw konforka::exception(CODEPOINT,"can't use User, Group or Chroot when started without root privileges");
66 if(config.flags&configuration::flag_user) {
67 struct passwd *ptmp = getpwnam(config.user.c_str());
68 if(ptmp) {
69 uid = ptmp->pw_uid;
70 }else{
71 errno=0;
72 uid = strtol(config.user.c_str(),NULL,0);
73 if(errno)
74 throw konforka::exception(CODEPOINT,"failed to resolve User value to uid");
75 }
76 }
77 if(config.flags&configuration::flag_group) {
78 struct group *gtmp = getgrnam(config.group.c_str());
79 if(gtmp) {
80 gid = gtmp->gr_gid;
81 }else{
82 errno=0;
83 gid = strtol(config.group.c_str(),NULL,0);
84 if(errno)
85 throw konforka::exception(CODEPOINT,"failed to resolve Group value to gid");
86 }
87 }
88 if(!(config.flags&configuration::flag_root_source))
89 throw konforka::exception(CODEPOINT,"unspecified root for sources");
90 if(!(config.flags&configuration::flag_root_intermediate))
91 throw konforka::exception(CODEPOINT,"unspecified root for intermediate files");
92 if(!(config.flags&configuration::flag_root_so))
93 throw konforka::exception(CODEPOINT,"unspecified root for shared objects");
94 if(config.flags&configuration::flag_min_children)
95 min_children = config.min_children;
96 if(config.flags&configuration::flag_max_children)
97 max_children = config.max_children;
98 if(config.flags&configuration::flag_min_spare_children)
99 min_spare_children = config.min_spare_children;
100 if(config.flags&configuration::flag_max_spare_children)
101 max_spare_children = config.max_spare_children;
102 if(max_children<min_spare_children)
103 throw konforka::exception(CODEPOINT,"inconsistent numbers of MaxChildren and MinSpareChildren");
104 if(min_children>max_spare_children && max_spare_children>=0)
105 throw konforka::exception(CODEPOINT,"inconsistent numbers of MinChildren and MaxSpareChildren");
106 if(config.flags&configuration::flag_multi_process) {
107 multi = config.multi_process;
108 }else{
109 if(config.flags&configuration::flag_listen_socket)
110 multi = true;
111 else
112 multi = false;
113 }
114 fss = (config.flags&configuration::flag_listen_socket)?new fcgi_socket(config.listen_socket.c_str(),5):new fcgi_socket(0);
115 if(!fss)
116 throw konforka::exception(CODEPOINT,"failed to establish listening socket");
117 if(config.flags&configuration::flag_daemonize && config.daemonize) {
118 pid_t pf = fork();
119 if(pf<0)
120 throw konforka::exception(CODEPOINT,"failed to fork()");
121 if(pf) {
122 die_humbly=true;
123 _exit(0);
124 }
125 }
126 if(config.flags&configuration::flag_pid_file) {
127 pidfile.set(config.pid_file);
128 }
129 if(multi)
130 sem.init();
131 }
132sitecing_fastcgi_pm::~sitecing_fastcgi_pm() {
133 if(fss)
134 delete fss;
135}
136
137void sitecing_fastcgi_pm::process(int slot) {
138 signal(SIGINT,SIG_DFL);
139 signal(SIGABRT,SIG_DFL);
140 signal(SIGTERM,SIG_DFL);
141 give_up_privs();
142 scoreboard_slot *sslot = sboard.get_slot(slot);
143 try {
144 sitespace ss(config);
145 fcgi_socket& fs = *fss;
146 sitecing_interface_cgi scif(&ss);
147 string component_path;
148 string action;
149 config_options::action_handler_t *action_handler;
150 int rpc = 0;
151 if(config.flags&configuration::flag_requests_per_child)
152 rpc = config.requests_per_child;
153 for(int req=0;(rpc<=0) || (req<rpc);rpc++) {
154 semaphore_lock sl;
155 if(multi) {
156 sslot->state = scoreboard_slot::state_idle;
157 sl.sem = &sem;
158 sl.lock();
159 }
160 sslot->state = scoreboard_slot::state_accept;
161 fcgi_interface fi(fs);
162 sslot->state = scoreboard_slot::state_processing;
163 if(multi)
164 sl.unlock();
165 cgi_gateway gw(fi);
166 scif.prepare(&gw);
167 try {
168 component_path = normalize_path(gw.get_meta("PATH_INFO"),strip_leading_slash|strip_trailing_slash);
169 string full_component_path;
170 while(true) {
171 full_component_path = config.root_source+'/'+component_path;
172 if(!access(full_component_path.c_str(),F_OK))
173 break;
174 string::size_type sl = component_path.rfind('/');
175 if(sl==string::npos)
176 throw konforka::exception(CODEPOINT,"can't find the target component");
177 component_path.erase(sl);
178 }
179 action = component_path;
180 action_handler = config.lookup_action_handler(component_path);
181 if(action_handler) {
182 action = action_handler->action;
183 }
184 string pwd = dir_name(full_component_path);
185 if(chdir(pwd.c_str()))
186 throw konforka::exception(CODEPOINT,"failed to chdir() into document's directory");
187 so_component soc = ss.fetch(action,&scif);
188 if(action_handler) {
189 soc.ac->run(__magic_action,
190 config.root_source.c_str(), config.root_intermediate.c_str(), config.root_so.c_str(),
191 &(action_handler->args)
192 );
193 }else{
194 soc.ac->main(0,NULL);
195 }
196 }catch(http_status& hs) {
197 scif.headers["Status"] = hs.status+" "+hs.message;
198 string hshp = config.lookup_http_status_handler(component_path,hs.status);
199 if(!hshp.empty()) {
200 so_component hsh = ss.fetch(hshp,&scif); // TODO: handle error trying to handle status
201 hsh.ac->run(__magic_http_status,config.root_source.c_str(),config.root_intermediate.c_str(),
202 config.root_so.c_str(),action.c_str(),&hs);
203 }
204 }catch(compile_error& ce) {
205 config_options *co_exception_handler = config.lookup_config(component_path,config_options::flag_exception_handler);
206 if(co_exception_handler) {
207 so_component eh = ss.fetch(co_exception_handler->exception_handler,&scif); // TODO: handle error trying to handle error.
208 eh.ac->run(__magic_compile_error,ce.what(),config.root_source.c_str(),config.root_intermediate.c_str(),config.root_so.c_str(),ce.component_path.c_str());
209 }else{
210 ce.see(CODEPOINT);
211 throw;
212 }
213 }catch(preprocessor_error& pe) {
214 config_options *co_exception_handler = config.lookup_config(component_path,config_options::flag_exception_handler);
215 if(co_exception_handler) {
216 so_component eh = ss.fetch(co_exception_handler->exception_handler,&scif); // TODO: handle error trying to handle error.
217 eh.ac->run(__magic_preprocess_error,pe.what(),config.root_source.c_str(),config.root_intermediate.c_str(),config.root_so.c_str(),pe.component_name.c_str(),pe.line_number);
218 }else{
219 pe.see(CODEPOINT);
220 throw;
221 }
222 }catch(exception& e) {
223 config_options *co_exception_handler = config.lookup_config(component_path,config_options::flag_exception_handler);
224 if(co_exception_handler) {
225 so_component eh = ss.fetch(co_exception_handler->exception_handler,&scif); // TODO: handle error trying to handle error.
226 eh.ac->run(__magic_generic_exception,e.what(),config.root_source.c_str(),config.root_intermediate.c_str(),config.root_so.c_str(),component_path.c_str(),&e);
227 }
228 }
229 scif.flush();
230 }
231 }catch(exception& e) {
232 cerr << "->Oops: " << e.what() << endl;
233 }
234}
235
236void sitecing_fastcgi_pm::run() {
237 if(multi)
238 manage();
239 else
240 process(0);
241}
242
243void sitecing_fastcgi_pm::give_up_privs() {
244 if(config.flags&configuration::flag_chroot) {
245 if(chroot(config.chroot.c_str()))
246 throw konforka::exception(CODEPOINT,"failed to chroot()");
247 }
248 if(config.flags&configuration::flag_group) {
249 if((getgid()!=gid) && setgid(gid))
250 throw konforka::exception(CODEPOINT,"failed to setgid()");
251 }
252 if(config.flags&configuration::flag_user) {
253 if((getuid()!=uid) && setuid(uid))
254 throw konforka::exception(CODEPOINT,"failed to setuid()");
255 }
256}
257
258static sitecing_fastcgi_pm* _process_manager = NULL;
259
260static void lethal_signal_handler(int signum) {
261 _process_manager->finishing=true;
262}
263
264int main(int argc,char **argv) {
265 const char* id = *argv;
266 const char* t;
267 while(t = index(id,'/')) {
268 id=t; id++;
269 }
270 openlog(id,LOG_PERROR|LOG_PID,LOG_USER);
271 try {
272 string config_file = "sitecing.conf";
273 while(true) {
274 static struct option opts[] = {
275 { "help", no_argument, 0, 'h' },
276 { "usage", no_argument, 0, 'h' },
277 { "version", no_argument, 0, 'V' },
278 { "license", no_argument, 0, 'L' },
279 { "config", required_argument, 0, 'f' },
280 { NULL, 0, 0, 0 }
281 };
282 int c = getopt_long(argc,argv,"f:hVL",opts,NULL);
283 if(c==-1)
284 break;
285 switch(c) {
286 case 'h':
287 cerr << PHEADER << endl
288 << PCOPY << endl << endl
289 << " -h, --help" << endl
290 << " --usage display this text" << endl
291 << " -V, --version display version number" << endl
292 << " -L, --license show license" << endl
293 << " -f filename, --config=filename" << endl
294 << " specify configuration file to use" << endl;
295 exit(0);
296 break;
297 case 'V':
298 cerr << VERSION << endl;
299 exit(0);
300 break;
301 case 'L':
302 extern const char *COPYING;
303 cerr << COPYING << endl;
304 exit(0);
305 break;
306 case 'f':
307 config_file = optarg;
308 break;
309 default:
310 cerr << "Huh??" << endl;
311 break;
312 }
313 }
314 sitecing_fastcgi_pm sfpm(config_file);
315 _process_manager = &sfpm;
316 signal(SIGINT,lethal_signal_handler);
317 signal(SIGABRT,lethal_signal_handler);
318 signal(SIGTERM,lethal_signal_handler);
319 sfpm.run();
320 }catch(exception& e) {
321 /* cerr << "Oops: " << e.what() << endl; */
322 syslog(LOG_ERR,"uncaught exception: %s",e.what());
323 }
324 closelog();
325}