1 |
< |
// Spectre 2 |
2 |
< |
// |
3 |
< |
// Douglas Thrift |
4 |
< |
// |
5 |
< |
// $Id$ |
1 |
> |
#!/usr/bin/env bash |
2 |
> |
# Spectre 2 |
3 |
> |
# |
4 |
> |
# Douglas Thrift |
5 |
> |
# |
6 |
> |
# $Id$ |
7 |
|
|
8 |
< |
#include <menes/platform.hpp> |
8 |
> |
command="$1" |
9 |
|
|
10 |
< |
#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> |
10 |
> |
shift |
11 |
|
|
12 |
< |
#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 |
12 |
> |
function check() |
13 |
|
{ |
14 |
< |
bool debug(false); |
14 |
> |
pid="$(< spectre.pid)" |
15 |
|
} |
16 |
|
|
17 |
< |
ext::String pid("spectre.pid"); |
32 |
< |
|
33 |
< |
inline int usage(int code = 0) |
17 |
> |
function usage() |
18 |
|
{ |
19 |
< |
api::Cerr << "Usage: " << api::GetExecutablePath().GetName() << " start|stop|restart|reload|status [...]" << ios::NewLine; |
36 |
< |
|
37 |
< |
return code; |
19 |
> |
echo "Usage: $(basename $0) start|stop|restart|reload|status [...]" |
20 |
|
} |
21 |
|
|
22 |
< |
inline pid_t process() |
22 |
> |
function start_() |
23 |
|
{ |
24 |
< |
api::FileReader fin(pid); |
25 |
< |
|
26 |
< |
return lexical_cast<pid_t>(ios::ReadLine(fin)); |
24 |
> |
echo -n "Starting Spectre daemon ... " |
25 |
> |
$(dirname $0)/Spectre2 -fork $@ |
26 |
> |
echo "started." |
27 |
|
} |
28 |
|
|
29 |
< |
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) |
29 |
> |
function stop_() |
30 |
|
{ |
31 |
< |
api::Cout << "Starting Spectre daemon ... " << ios::Flush; |
32 |
< |
|
33 |
< |
if (pid_t process = api::Posix::CheckError(::fork())) |
34 |
< |
{ |
35 |
< |
api::Cout << "started." << ios::NewLine; |
36 |
< |
} |
37 |
< |
else |
38 |
< |
{ |
39 |
< |
_L<const char*> argv; |
40 |
< |
|
41 |
< |
argv.Reserve(args.GetSize()); |
42 |
< |
|
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 |
< |
} |
31 |
> |
echo -n "Stopping Spectre daemon ... " |
32 |
> |
# try |
33 |
> |
# { |
34 |
> |
# pid_t id(process()); |
35 |
> |
# |
36 |
> |
# api::Posix::CheckError(::kill(id, SIGTERM)); |
37 |
> |
# |
38 |
> |
# // XXX: somehow wait for it to terminate |
39 |
> |
# |
40 |
> |
# api::Cout << "stopped." << ios::NewLine; |
41 |
> |
# } |
42 |
> |
# catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
43 |
|
} |
44 |
|
|
45 |
< |
void stop() |
45 |
> |
function reload_() |
46 |
|
{ |
47 |
< |
api::Cout << "Stopping Spectre daemon ... " << ios::Flush; |
48 |
< |
|
49 |
< |
try |
50 |
< |
{ |
51 |
< |
pid_t id(process()); |
52 |
< |
|
53 |
< |
api::Posix::CheckError(::kill(id, SIGTERM)); |
54 |
< |
|
55 |
< |
// XXX: somehow wait for it to terminate |
56 |
< |
|
137 |
< |
api::Cout << "stopped." << ios::NewLine; |
138 |
< |
} |
139 |
< |
catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
47 |
> |
echo -n "Reloading Spectre daemon ... " |
48 |
> |
# api::Cout << "Reloading Spectre daemon ... " << ios::Flush; |
49 |
> |
# |
50 |
> |
# try |
51 |
> |
# { |
52 |
> |
# api::Posix::CheckError(::kill(process(), SIGUSR1)); |
53 |
> |
# |
54 |
> |
# api::Cout << "reloaded." << ios::NewLine; |
55 |
> |
# } |
56 |
> |
# catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
57 |
|
} |
58 |
|
|
59 |
< |
void reload() |
59 |
> |
function status_() |
60 |
|
{ |
61 |
< |
api::Cout << "Reloading Spectre daemon ... " << ios::Flush; |
145 |
< |
|
146 |
< |
try |
147 |
< |
{ |
148 |
< |
api::Posix::CheckError(::kill(id, SIGUSR1)); |
149 |
< |
|
150 |
< |
api::Cout << "reloaded." << ios::NewLine; |
151 |
< |
} |
152 |
< |
catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
61 |
> |
echo "STUB: status_()" |
62 |
|
} |
63 |
|
|
64 |
< |
void status() |
65 |
< |
{ |
66 |
< |
} |
64 |
> |
case $command in |
65 |
> |
start) |
66 |
> |
start_ $@ |
67 |
> |
;; |
68 |
> |
stop) |
69 |
> |
stop_ |
70 |
> |
;; |
71 |
> |
restart) |
72 |
> |
restart_ $@ |
73 |
> |
;; |
74 |
> |
reload) |
75 |
> |
reload_ |
76 |
> |
;; |
77 |
> |
status) |
78 |
> |
status_ |
79 |
> |
;; |
80 |
> |
*) |
81 |
> |
usage |
82 |
> |
;; |
83 |
> |
esac |