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