From 614b78470c5ed3c701abdd32bc65412f363a6fc2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 4 Sep 2012 12:01:53 +0300 Subject: added restarter project (code wcreated to restart critical services on server without root access) --- server/main.cpp | 305 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 server/main.cpp (limited to 'server/main.cpp') diff --git a/server/main.cpp b/server/main.cpp new file mode 100644 index 0000000..240ee22 --- /dev/null +++ b/server/main.cpp @@ -0,0 +1,305 @@ +// +// async_tcp_echo_server.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +using boost::asio::ip::tcp; +typedef boost::asio::ssl::stream ssl_socket; + +class session +{ +public: + session(boost::asio::io_service& io_service, + boost::asio::ssl::context& context) + : socket_(io_service, context) { + } + + ssl_socket::lowest_layer_type& socket() + { + return socket_.lowest_layer(); + } + + void handle_handshake(const boost::system::error_code& error) + { + if (!error) + { + socket_.async_read_some(boost::asio::buffer(data_, max_length), + boost::bind(&session::handle_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + } + else + { + delete this; + } + } + + void start() + { + socket_.async_handshake(boost::asio::ssl::stream_base::server, + boost::bind(&session::handle_handshake, this, + boost::asio::placeholders::error)); + } + +private: + void handle_read(const boost::system::error_code& error, + size_t bytes_transferred) + { + if (!error) + { +// std::cout<<"recieved: "<socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + std::string get_password() const + { + return ""; + } + + void handle_accept(session* new_session, + const boost::system::error_code& error) + { + if (!error) + { + new_session->start(); + } + else + { + delete new_session; + } + + start_accept(); + } + + boost::asio::io_service& io_service_; + boost::asio::ip::tcp::acceptor acceptor_; + boost::asio::ssl::context context_; +}; + +extern "C" void handle_term(int i) +{ + remove("/var/run/restarter_server.pid"); + exit(1); +} + +int main(int argc, char* argv[]) +{ + if(geteuid()) + { + 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; + 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<