Menu

[r751]: / framework / branches / refactoring / service.cpp  Maximize  Restore  History

Download this file

289 lines (242 with data), 5.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "service.h"
#include "service_impl.h"
#include "applications_pool.h"
#include "thread_pool.h"
#include "global_config.h"
#include "cppcms_error.h"
#include "cgi_acceptor.h"
#include "cgi_api.h"
#include "scgi_api.h"
#include "asio_config.h"
#if !defined(_WIN32) && !defined(__CYGWIN__)
#include <sys/wait.h>
#endif
namespace cppcms {
service::service(int argc,char *argv[]) :
impl_(new impl::service())
{
impl_->settings_.reset(new cppcms_config());
impl_->settings_->load(argc,argv);
int apps=settings().integer("service.applications_pool_size",threads_no()*2);
impl_->applications_pool_.reset(new cppcms::applications_pool(*this,apps));
}
service::~service()
{
}
int service::threads_no()
{
return settings().integer("service.worker_threads",5);
}
namespace {
#if defined(__CYGWIN__) || defined(_WIN32)
void make_socket_pair(boost::asio::ip::tcp::socket &s1,boost::asio::ip::tcp::socket &s2)
{
boost::asio::ip::tcp::acceptor acceptor(s1.get_io_service(),
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
server_endpoint.address(boost::asio::ip::address_v4::loopback());
s1.lowest_layer().connect(server_endpoint);
acceptor.accept(s2.lowest_layer());
}
SOCKET notification_socket;
void handler(int nothing)
{
char c='A';
if(send(notification_socket,&c,1,0) <= 0) {
perror("notification failed");
exit(1);
}
}
#else
void make_socket_pair(boost::asio::local::stream_protocol::socket &s1,boost::asio::local::stream_protocol::socket &s2)
{
boost::asio::local::connect_pair(s1,s2);
}
int notification_socket;
void handler(int nothing)
{
char c='A';
for(;;){
int res=::write(notification_socket,&c,1);
if(res<0 && errno == EINTR)
continue;
if(res<=0) {
perror("shudown notification failed");
exit(1);
}
return;
}
}
#endif
} // anon
#ifdef _WIN32
void service::setup_exit_handling()
{
throw cppcms_error("TODO Setup exit handling");
}
#else
void service::setup_exit_handling()
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set,SIGINT);
sigaddset(&set,SIGTERM);
sigaddset(&set,SIGUSR1);
pthread_sigmask(SIG_BLOCK,&set,0);
make_socket_pair(impl_->sig_,impl_->breaker_);
static char c;
impl_->breaker_.async_read_some(boost::asio::buffer(&c,1),
boost::bind(&service::stop,this));
notification_socket=impl_->sig_.native();
struct sigaction sa;
memset(&sa,0,sizeof(sa));
sa.sa_handler=handler;
sigaction(SIGINT,&sa,0);
sigaction(SIGTERM,&sa,0);
sigaction(SIGUSR1,&sa,0);
}
#endif
void service::run()
{
start_acceptor();
if(prefork()) {
return;
}
thread_pool(); // make sure we start it
impl_->acceptor_->async_accept();
setup_exit_handling();
impl_->get_io_service().run();
}
int service::procs_no()
{
int procs=settings().integer("service.procs",0);
if(procs < 0)
procs = 0;
#if defined(_WIN32) || defined(__CYGWIN)
if(procs > 0)
throw cppcms_error("Prefork is not supported under Windows");
#endif
return procs;
}
#if defined(_WIN32) || defined(__CYGWIN__)
bool service::prefork()
{
procs_no();
return false;
}
#else // UNIX
bool service::prefork()
{
int procs=settings().integer("service.procs",0);
if(procs<=0)
return false;
std::vector<int> pids(procs,0);
for(int i=0;i<procs;i++) {
int pid=::fork();
if(pid < 0) {
int err=errno;
for(int j=0;j<i;j++) {
::kill(pids[j],SIGTERM);
int stat;
::waitpid(pids[j],&stat,0);
}
throw cppcms_error(err,"fork failed");
}
else if(pid==0) {
return false; // chaild
}
else {
pids[i]=pid;
}
}
sigset_t set;
sigemptyset(&set);
sigaddset(&set,SIGTERM);
sigaddset(&set,SIGINT);
sigaddset(&set,SIGQUIT);
sigprocmask(SIG_BLOCK,&set,NULL);
int sig;
do {
sig=0;
sigwait(&set,&sig);
}while(sig!=SIGINT && sig!=SIGTERM && sig!=SIGQUIT);
sigprocmask(SIG_UNBLOCK,&set,NULL);
for(int i=0;i<procs;i++) {
::kill(pids[i],SIGTERM);
int stat;
::waitpid(pids[i],&stat,0);
}
return true;
}
#endif
void service::start_acceptor()
{
using namespace impl::cgi;
std::string api=settings().str("service.api");
std::string ip=settings().str("service.ip","127.0.0.1");
int port=0;
std::string socket=settings().str("service.socket","");
int backlog=settings().integer("service.backlog",threads_no() * 2);
bool tcp=socket.empty();
if(tcp && port==0) {
port=settings().integer("service.port");
}
if(tcp) {
if(api=="scgi")
impl_->acceptor_.reset(
new tcp_socket_acceptor<scgi<boost::asio::ip::tcp> >(*this,ip,port,backlog));
else
throw cppcms_error("Unknown service.api: " + api);
}
else {
#ifdef _WIN32
throw cppcms_error("Unix domain sockets are not supported under Windows... (isn't it obvious?)");
#elif defined(__CYGWIN__)
throw cppcms_error("CppCMS uses native Win32 sockets under cygwin, so Unix sockets are not supported");
#else
if(api=="scgi")
impl_->acceptor_.reset(
new unix_socket_acceptor<scgi<boost::asio::local::stream_protocol> >(*this,socket,backlog));
else
throw cppcms_error("Unknown service.api: " + api);
#endif
}
}
cppcms::applications_pool &service::applications_pool()
{
return *impl_->applications_pool_;
}
cppcms::thread_pool &service::thread_pool()
{
if(!impl_->thread_pool_.get()) {
impl_->thread_pool_.reset(new cppcms::thread_pool(threads_no()));
}
return *impl_->thread_pool_;
}
cppcms::cppcms_config const &service::settings()
{
return *impl_->settings_;
}
cppcms::impl::service &service::impl()
{
return *impl_;
}
void service::stop()
{
if(impl_->acceptor_.get())
impl_->acceptor_->stop();
thread_pool().stop();
impl_->get_io_service().stop();
}
namespace impl {
service::service() :
io_service_(),
sig_(io_service_),
breaker_(io_service_)
{
}
service::~service()
{
}
} // impl
} // cppcms
MongoDB Logo MongoDB