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
|
// Copyright © 2010-2013 sss
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//based on async_tcp_echo_server.cpp code by Christopher M. Kohlhoff (chris at kohlhoff dot com)
#include "headers.h"
extern "C" void handle_term(int i)
{
remove("/var/run/restarter_server.pid");
exit(1);
}
int main(int argc, char* argv[])
{
/* if(geteuid()) //temporary disabled for debug
{
std::cout<<"Program must be runned with root privilegies\n";
exit(EXIT_FAILURE);
} */
{
std::ifstream in_pid;
in_pid.open("/var/run/restarter_server.pid");
if(in_pid.is_open() && in_pid.good())
{
char szpid[16];
in_pid.read(szpid, 16);
if(!strcmp(szpid, "0"))
remove("/var/run/restarter_server.pid");
else
{
std::string cmd = "kill ";
for(int i = 0; i < 16 && szpid[i] != '\n' && szpid[i] != '\0'; i++)
cmd.push_back(szpid[i]);
system(cmd.c_str());
remove("/var/run/restarter_server.pid");
}
}
}
/* pid_t pid; //temporary disabled for debug
pid = fork();
if(pid < 0)
{
std::cerr<<"Failed to fork\n";
exit(EXIT_FAILURE);
}
if(pid > 0)
{
std::cerr<<"Successfuly forked\n";
std::ofstream out;
out.open("/var/run/restarter_server.pid");
char szpid[16];
snprintf(szpid, 15, "%d", pid);
out<<pid<<"\n";
out.close();
exit(EXIT_SUCCESS);
} */
signal (SIGTERM, handle_term);
load_modules();
try
{
boost::asio::io_service io_service;
server s(io_service, 1313);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
|