1 |
douglas |
415 |
// Spectre 2 |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include <menes/platform.hpp> |
8 |
|
|
|
9 |
|
|
#include <menes-api/console.hpp> |
10 |
|
|
#include <menes-api/exename.hpp> |
11 |
|
|
#include <menes-api/files.hpp> |
12 |
|
|
#include <menes-app/application.hpp> |
13 |
|
|
#include <menes-ext/string.hpp> |
14 |
|
|
#include <menes-ios/helpers.hpp> |
15 |
|
|
|
16 |
|
|
#include <sys/resource.h> |
17 |
|
|
#include <sys/time.h> |
18 |
|
|
#include <sys/types.h> |
19 |
|
|
#include <sys/wait.h> |
20 |
|
|
#include <signal.h> |
21 |
|
|
#include <unistd.h> |
22 |
|
|
|
23 |
|
|
#include "Matcher/Matcher.hpp" |
24 |
|
|
|
25 |
|
|
// XXX: Menes' linker is linking too much |
26 |
|
|
namespace Spectre2 |
27 |
|
|
{ |
28 |
|
|
bool debug(false); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
ext::String pid("spectre.pid"); |
32 |
|
|
|
33 |
|
|
inline int usage(int code = 0) |
34 |
|
|
{ |
35 |
|
|
api::Cerr << "Usage: " << api::GetExecutablePath().GetName() << " start|stop|restart|reload|status [...]" << ios::NewLine; |
36 |
|
|
|
37 |
|
|
return code; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
inline pid_t process() |
41 |
|
|
{ |
42 |
|
|
api::FileReader fin(pid); |
43 |
|
|
|
44 |
|
|
return lexical_cast<pid_t>(ios::ReadLine(fin)); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void start(const app::ArgumentList& args); |
48 |
|
|
void stop(); |
49 |
|
|
inline void restart(const app::ArgumentList& args) { stop(); start(args); } |
50 |
|
|
void reload(); |
51 |
|
|
void status(); |
52 |
|
|
|
53 |
|
|
struct Spectre2Control : public app::Application |
54 |
|
|
{ |
55 |
|
|
virtual int Run(const app::ArgumentList& args) |
56 |
|
|
{ |
57 |
|
|
enum Command { Start, Stop, Restart, Reload, Status } command; |
58 |
|
|
|
59 |
|
|
if (!args.IsEmpty()) |
60 |
|
|
{ |
61 |
|
|
const ext::String& arg(args.First()); |
62 |
|
|
|
63 |
|
|
if (arg == "start") command = Start; |
64 |
|
|
else if (arg == "stop") command = Stop; |
65 |
|
|
else if (arg == "restart") command = Restart; |
66 |
|
|
else if (arg == "reload") command = Reload; |
67 |
|
|
else if (arg == "status") command = Status; |
68 |
|
|
else return usage(1); |
69 |
|
|
|
70 |
|
|
_foreach (app::ArgumentList, arg, args) |
71 |
|
|
{ |
72 |
|
|
Matcher matcher("^-pid=(.*)$"); |
73 |
|
|
|
74 |
|
|
if (*arg == matcher) pid = matcher[1]; |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
else return usage(); |
78 |
|
|
|
79 |
|
|
switch (command) |
80 |
|
|
{ |
81 |
|
|
case Start: |
82 |
|
|
start(args); |
83 |
|
|
break; |
84 |
|
|
case Stop: |
85 |
|
|
stop(); |
86 |
|
|
break; |
87 |
|
|
case Restart: |
88 |
|
|
restart(args); |
89 |
|
|
break; |
90 |
|
|
case Reload: |
91 |
|
|
reload(); |
92 |
|
|
break; |
93 |
|
|
case Status: |
94 |
|
|
status(); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
return 0; |
98 |
|
|
} |
99 |
|
|
} spectreControl; |
100 |
|
|
|
101 |
|
|
void start(const app::ArgumentList& args) |
102 |
|
|
{ |
103 |
|
|
api::Cout << "Starting Spectre daemon ... " << ios::Flush; |
104 |
|
|
|
105 |
|
|
if (pid_t process = api::Posix::CheckError(::fork())) |
106 |
|
|
{ |
107 |
|
|
api::Cout << "started." << ios::NewLine; |
108 |
|
|
} |
109 |
|
|
else |
110 |
|
|
{ |
111 |
|
|
_L<const char*> argv; |
112 |
|
|
|
113 |
|
|
argv.Reserve(args.GetSize()); |
114 |
|
|
|
115 |
|
|
_foreach (app::ArgumentList, arg, args) argv.InsertLast(arg->NullTerminate()); |
116 |
|
|
|
117 |
|
|
ext::String command(api::GetExecutablePath().GetParent() + "Spectre2"); |
118 |
|
|
|
119 |
|
|
argv.First() = command.NullTerminate(); |
120 |
|
|
|
121 |
|
|
api::Posix::CheckError(::execv(command.NullTerminate(), const_cast<char**>(argv.NullTerminate()))); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
void stop() |
126 |
|
|
{ |
127 |
|
|
api::Cout << "Stopping Spectre daemon ... " << ios::Flush; |
128 |
|
|
|
129 |
|
|
try |
130 |
|
|
{ |
131 |
|
|
pid_t id(process()); |
132 |
|
|
|
133 |
|
|
api::Posix::CheckError(::kill(id, SIGTERM)); |
134 |
|
|
|
135 |
|
|
// XXX: somehow wait for it to terminate |
136 |
|
|
|
137 |
|
|
api::Cout << "stopped." << ios::NewLine; |
138 |
|
|
} |
139 |
|
|
catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
void reload() |
143 |
|
|
{ |
144 |
|
|
api::Cout << "Reloading Spectre daemon ... " << ios::Flush; |
145 |
|
|
|
146 |
|
|
try |
147 |
|
|
{ |
148 |
douglas |
416 |
api::Posix::CheckError(::kill(process(), SIGUSR1)); |
149 |
douglas |
415 |
|
150 |
|
|
api::Cout << "reloaded." << ios::NewLine; |
151 |
|
|
} |
152 |
|
|
catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
void status() |
156 |
|
|
{ |
157 |
|
|
} |