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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/slobrok/server/sbenv.h>
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/fastos/app.h>
#include <csignal>
#include <vespa/log/log.h>
LOG_SETUP("vespa-slobrok");
/**
* @brief namespace for the actual slobrok application.
**/
namespace slobrok {
class App : public FastOS_Application
{
public:
int Main() override;
};
static std::unique_ptr<SBEnv> mainobj;
extern "C" {
static void sigtermhandler(int signo);
};
static void
sigtermhandler(int signo)
{
(void) signo;
if (mainobj) {
mainobj->shutdown();
}
}
static void
hook_sigterm(void)
{
struct sigaction act;
act.sa_handler = sigtermhandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGTERM, &act, NULL);
}
int
App::Main()
{
uint32_t portnum = 2773;
vespalib::string cfgId;
int argi = 1;
const char* optArg;
int c;
while ((c = GetOpt("c:s:p:N", optArg, argi)) != -1) {
switch (c) {
case 'c':
cfgId = std::string(optArg);
break;
case 'p':
portnum = atoi(optArg);
break;
case 'N':
// ignored
break;
default:
LOG(error, "unknown option letter '%c'", c);
return 1;
}
}
int res = 1;
try {
if (cfgId.empty()) {
LOG(debug, "no config id specified");
ConfigShim shim(portnum);
mainobj = std::make_unique<SBEnv>(shim);
} else {
ConfigShim shim(portnum, cfgId);
shim.enableStateServer(true);
mainobj = std::make_unique<SBEnv>(shim);
}
hook_sigterm();
res = mainobj->MainLoop();
} catch (const config::ConfigTimeoutException &e) {
LOG(error, "config timeout during construction : %s", e.what());
EV_STOPPING("slobrok", "config timeout during construction");
res = 1;
} catch (const vespalib::PortListenException &e) {
LOG(error, "Failed listening to network port(%d) with protocol(%s): '%s'",
e.get_port(), e.get_protocol().c_str(), e.what());
EV_STOPPING("slobrok", "could not listen to our network port");
res = 1;
} catch (const std::exception & e) {
LOG(error, "unknown exception during construction : %s", e.what());
EV_STOPPING("slobrok", "unknown exception during construction");
res = 2;
}
if (mainobj && !mainobj->isShuttingDown()) {
mainobj->shutdown();
}
mainobj.reset();
return res;
}
} // namespace slobrok
int
main(int argc, char **argv)
{
slobrok::App slobrok;
return slobrok.Entry(argc, argv);
}
|