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