1 |
douglas |
402 |
// Spectre 2 |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Mounter.hpp" |
8 |
|
|
#include "Unmounter.hpp" |
9 |
|
|
#include "Matcher/Matcher.hpp" |
10 |
|
|
|
11 |
|
|
#include <menes-api/exename.hpp> |
12 |
|
|
#include <menes-app/application.hpp> |
13 |
|
|
|
14 |
|
|
extern "C" |
15 |
|
|
{ |
16 |
|
|
#include <sys/types.h> |
17 |
douglas |
405 |
#include <signal.h> |
18 |
douglas |
402 |
} |
19 |
|
|
|
20 |
|
|
struct Spectre2Command : public app::Application |
21 |
|
|
{ |
22 |
|
|
virtual int Run(const app::ArgumentList& args) |
23 |
|
|
{ |
24 |
|
|
ext::String config("spectre.xml"), pid("spectre.pid"); |
25 |
|
|
bool fork(false); |
26 |
|
|
|
27 |
|
|
_foreach (app::ArgumentList, arg, args) |
28 |
|
|
{ |
29 |
|
|
Matcher matcher; |
30 |
|
|
|
31 |
|
|
if (*arg == matcher("^-config=(.*)$")) config = matcher[1]; |
32 |
|
|
else if (*arg == "-fork") fork = true; |
33 |
|
|
else if (*arg == matcher("^-pid=(.*)$")) pid = matcher[1]; |
34 |
|
|
else if (*arg == "-D") Spectre2::debug = true; |
35 |
|
|
else |
36 |
|
|
{ |
37 |
|
|
api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-fork] [-pid=pid] [-D]" << ios::NewLine; |
38 |
|
|
|
39 |
|
|
return 1; |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
if (fork && api::Posix::CheckError(::fork())) return 0; |
44 |
|
|
|
45 |
|
|
Spectre2(config, pid); |
46 |
|
|
|
47 |
|
|
return 0; |
48 |
|
|
} |
49 |
|
|
} spectre; |
50 |
|
|
|
51 |
douglas |
405 |
extern "C" |
52 |
|
|
{ |
53 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap); |
54 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap); |
55 |
|
|
} |
56 |
|
|
|
57 |
douglas |
402 |
Spectre2::Spectre2(const ext::String& config, const ext::String& pid) : pid(pid) |
58 |
|
|
{ |
59 |
|
|
{ |
60 |
|
|
api::FileWriter out(pid); |
61 |
|
|
ios::FormatWriter fout(out); |
62 |
|
|
|
63 |
|
|
fout << ::getpid() << ios::NewLine; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
Mounter mounter(config); |
67 |
|
|
Unmounter unmounter(config); |
68 |
douglas |
405 |
|
69 |
douglas |
406 |
daemons.InsertLast(dynamic_cast<Daemon*>(&mounter)); |
70 |
|
|
daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter)); |
71 |
douglas |
405 |
|
72 |
|
|
{ |
73 |
|
|
struct ::sigaction action; |
74 |
|
|
|
75 |
|
|
action.sa_sigaction = reload; |
76 |
|
|
action.sa_flags = SA_SIGINFO; |
77 |
|
|
|
78 |
|
|
api::Posix::CheckError(::sigemptyset(&action.sa_mask)); |
79 |
|
|
api::Posix::CheckError(::sigaction(SIGHUP, &action, NULL)); |
80 |
|
|
|
81 |
|
|
action.sa_sigaction = stop; |
82 |
|
|
|
83 |
|
|
api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL)); |
84 |
|
|
} |
85 |
douglas |
406 |
|
86 |
|
|
_mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start(); |
87 |
douglas |
407 |
_mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->wait(); |
88 |
douglas |
402 |
} |
89 |
|
|
|
90 |
|
|
Spectre2::~Spectre2() |
91 |
|
|
{ |
92 |
|
|
api::Posix::CheckError(::unlink(pid.NullTerminate())); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
ext::String Spectre2::program(api::GetExecutablePath().GetName()); |
96 |
|
|
bool Spectre2::debug(false); |
97 |
douglas |
405 |
ext::Vector<Daemon*> Spectre2::daemons; |
98 |
|
|
|
99 |
|
|
extern "C" |
100 |
|
|
{ |
101 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap) |
102 |
|
|
{ |
103 |
|
|
_mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->reload(); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap) |
107 |
|
|
{ |
108 |
|
|
_mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->stop(); |
109 |
|
|
} |
110 |
|
|
} |