1 |
douglas |
417 |
#!/usr/bin/env bash |
2 |
|
|
# Spectre 2 |
3 |
|
|
# |
4 |
|
|
# Douglas Thrift |
5 |
|
|
# |
6 |
|
|
# $Id$ |
7 |
douglas |
415 |
|
8 |
douglas |
417 |
pid="spectre.pid" |
9 |
douglas |
415 |
|
10 |
douglas |
417 |
function usage() |
11 |
douglas |
415 |
{ |
12 |
douglas |
417 |
echo "Usage: `basename $0` start|stop|restart|reload|status [...]" |
13 |
douglas |
415 |
} |
14 |
|
|
|
15 |
douglas |
417 |
#inline pid_t process() |
16 |
|
|
#{ |
17 |
|
|
# api::FileReader fin(pid); |
18 |
|
|
# |
19 |
|
|
# return lexical_cast<pid_t>(ios::ReadLine(fin)); |
20 |
|
|
#} |
21 |
douglas |
415 |
|
22 |
douglas |
417 |
#struct Spectre2Control : public app::Application |
23 |
|
|
#{ |
24 |
|
|
# virtual int Run(const app::ArgumentList& args) |
25 |
|
|
# { |
26 |
|
|
# enum Command { Start, Stop, Restart, Reload, Status } command; |
27 |
|
|
# |
28 |
|
|
# if (!args.IsEmpty()) |
29 |
|
|
# { |
30 |
|
|
# const ext::String& arg(args.First()); |
31 |
|
|
# |
32 |
|
|
# if (arg == "start") command = Start; |
33 |
|
|
# else if (arg == "stop") command = Stop; |
34 |
|
|
# else if (arg == "restart") command = Restart; |
35 |
|
|
# else if (arg == "reload") command = Reload; |
36 |
|
|
# else if (arg == "status") command = Status; |
37 |
|
|
# else return usage(1); |
38 |
|
|
# |
39 |
|
|
# _foreach (app::ArgumentList, arg, args) |
40 |
|
|
# { |
41 |
|
|
# Matcher matcher("^-pid=(.*)$"); |
42 |
|
|
# |
43 |
|
|
# if (*arg == matcher) pid = matcher[1]; |
44 |
|
|
# } |
45 |
|
|
# } |
46 |
|
|
# else return usage(); |
47 |
|
|
# |
48 |
|
|
# switch (command) |
49 |
|
|
# { |
50 |
|
|
# case Start: |
51 |
|
|
# start(args); |
52 |
|
|
# break; |
53 |
|
|
# case Stop: |
54 |
|
|
# stop(); |
55 |
|
|
# break; |
56 |
|
|
# case Restart: |
57 |
|
|
# restart(args); |
58 |
|
|
# break; |
59 |
|
|
# case Reload: |
60 |
|
|
# reload(); |
61 |
|
|
# break; |
62 |
|
|
# case Status: |
63 |
|
|
# status(); |
64 |
|
|
# } |
65 |
|
|
# |
66 |
|
|
# return 0; |
67 |
|
|
# } |
68 |
|
|
#} spectreControl; |
69 |
douglas |
415 |
|
70 |
douglas |
417 |
#void start(const app::ArgumentList& args) |
71 |
|
|
#{ |
72 |
|
|
# api::Cout << "Starting Spectre daemon ... " << ios::Flush; |
73 |
|
|
# |
74 |
|
|
# if (pid_t process = api::Posix::CheckError(::fork())) |
75 |
|
|
# { |
76 |
|
|
# api::Cout << "started." << ios::NewLine; |
77 |
|
|
# } |
78 |
|
|
# else |
79 |
|
|
# { |
80 |
|
|
# _L<const char*> argv; |
81 |
|
|
# |
82 |
|
|
# argv.Reserve(args.GetSize()); |
83 |
|
|
# |
84 |
|
|
# _foreach (app::ArgumentList, arg, args) argv.InsertLast(arg->NullTerminate()); |
85 |
|
|
# |
86 |
|
|
# ext::String command(api::GetExecutablePath().GetParent() + "Spectre2"); |
87 |
|
|
# |
88 |
|
|
# argv.First() = command.NullTerminate(); |
89 |
|
|
# |
90 |
|
|
# api::Posix::CheckError(::execv(command.NullTerminate(), const_cast<char**>(argv.NullTerminate()))); |
91 |
|
|
# } |
92 |
|
|
#} |
93 |
douglas |
415 |
|
94 |
douglas |
417 |
#void stop() |
95 |
|
|
#{ |
96 |
|
|
# api::Cout << "Stopping Spectre daemon ... " << ios::Flush; |
97 |
|
|
# |
98 |
|
|
# try |
99 |
|
|
# { |
100 |
|
|
# pid_t id(process()); |
101 |
|
|
# |
102 |
|
|
# api::Posix::CheckError(::kill(id, SIGTERM)); |
103 |
|
|
# |
104 |
|
|
# // XXX: somehow wait for it to terminate |
105 |
|
|
# |
106 |
|
|
# api::Cout << "stopped." << ios::NewLine; |
107 |
|
|
# } |
108 |
|
|
# catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
109 |
|
|
#} |
110 |
douglas |
415 |
|
111 |
douglas |
417 |
#void reload() |
112 |
|
|
#{ |
113 |
|
|
# api::Cout << "Reloading Spectre daemon ... " << ios::Flush; |
114 |
|
|
# |
115 |
|
|
# try |
116 |
|
|
# { |
117 |
|
|
# api::Posix::CheckError(::kill(process(), SIGUSR1)); |
118 |
|
|
# |
119 |
|
|
# api::Cout << "reloaded." << ios::NewLine; |
120 |
|
|
# } |
121 |
|
|
# catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; } |
122 |
|
|
#} |
123 |
douglas |
415 |
|
124 |
douglas |
417 |
#void status() |
125 |
|
|
#{ |
126 |
|
|
#} |
127 |
douglas |
415 |
|
128 |
douglas |
417 |
usage |