-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecop.cpp
More file actions
113 lines (86 loc) · 2.27 KB
/
Copy pathsecop.cpp
File metadata and controls
113 lines (86 loc) · 2.27 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
#include <iostream>
#include <fstream>
#include <functional>
#include <libutils/Socket.h>
#include <libutils/NetServer.h>
#include <libutils/Logger.h>
#include <libutils/FileUtils.h>
#include <libutils/ArgParser.h>
#include <libutils/Application.h>
#include <nlohmann/json.hpp>
#include <unistd.h>
#include <syslog.h>
#include "SecopServer.h"
#include "Crypto.h"
#include "CryptoStorage.h"
using namespace std;
using namespace CryptoPP;
using namespace Utils;
using namespace Utils::Net;
using namespace std::placeholders;
using json = nlohmann::json;
class SecopApp: public DaemonApplication
{
public:
/* Should perhaps change user/group to secop */
SecopApp():DaemonApplication("secop","/run", "root", "root")
{
}
void Startup() override
{
// Divert logger to syslog
openlog( "secop", LOG_PERROR, LOG_DAEMON);
logg.SetOutputter( [](const string& msg){ syslog(LOG_INFO, "%s",msg.c_str());});
logg.SetLogName("");
logg << Logger::Debug << "Starting up"<<lend;
Utils::SigHandler::Instance().AddHandler(SIGTERM, std::bind(&SecopApp::SigTerm, this, _1) );
Utils::SigHandler::Instance().AddHandler(SIGINT, std::bind(&SecopApp::SigTerm, this, _1) );
Utils::SigHandler::Instance().AddHandler(SIGHUP, std::bind(&SecopApp::SigHup, this, _1) );
unlink(SOCKPATH);
this->options.AddOption( Option('D', "debug", Option::ArgNone,"0","Debug logging") );
}
void Main() override
{
if( this->options["debug"] == "1" )
{
logg << Logger::Info << "Increase logging to debug level "<<lend;
logg.SetLevel(Logger::Debug);
}
this->secop = SecopServerPtr( new SecopServer( SOCKPATH, DBPATH) );
chmod( SOCKPATH, 0666);
this->secop->Run();
}
void ShutDown() override
{
unlink(SOCKPATH);
logg << Logger::Info << "Shutting down"<<lend;
}
void SigTerm(int signo)
{
(void) signo;
logg << Logger::Info << "Got sigterm initiate shutdown"<<lend;
this->secop->ShutDown();
}
void SigHup(int signo)
{
(void) signo;
logg << Logger::Debug << "Got sighup" << lend;
}
private:
SecopServerPtr secop;
};
int main(int argc, char** argv)
{
logg.SetLevel(Logger::Info);
int ret=0;
try
{
SecopApp app;
ret = app.Start( argc, argv);
}
catch(std::runtime_error& err)
{
logg << Logger::Error << "Caught runtime exception " << err.what() << lend;
}
return ret;
}