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 |
douglas |
417 |
#include <menes-app/simple.hpp> |
13 |
douglas |
402 |
|
14 |
|
|
#include <sys/types.h> |
15 |
douglas |
405 |
#include <signal.h> |
16 |
douglas |
402 |
|
17 |
douglas |
417 |
int Main(const app::Options& options) |
18 |
douglas |
402 |
{ |
19 |
douglas |
418 |
api::Path config(Spectre2::prefix + "/etc/spectre.xml"); |
20 |
douglas |
417 |
bool fork(false); |
21 |
|
|
|
22 |
|
|
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
23 |
douglas |
402 |
{ |
24 |
douglas |
417 |
Matcher matcher; |
25 |
douglas |
402 |
|
26 |
douglas |
417 |
if (*arg == matcher("^-config=(.*)$")) |
27 |
|
|
config = matcher[1]; |
28 |
|
|
else if (*arg == "-fork") |
29 |
|
|
fork = true; |
30 |
|
|
else if (*arg == "-D") |
31 |
|
|
Spectre2::debug = true; |
32 |
|
|
else |
33 |
douglas |
402 |
{ |
34 |
douglas |
418 |
api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-fork] [-D]" << ios::NewLine; |
35 |
douglas |
402 |
|
36 |
douglas |
417 |
return 1; |
37 |
douglas |
402 |
} |
38 |
douglas |
417 |
} |
39 |
douglas |
402 |
|
40 |
douglas |
418 |
if (!config.Exists()) |
41 |
|
|
{ |
42 |
|
|
api::Cerr << Spectre2::program << ": configuration file " << config.GetPath() << " does not exist." << ios::NewLine; |
43 |
|
|
|
44 |
|
|
return 2; |
45 |
|
|
} |
46 |
|
|
|
47 |
douglas |
417 |
if (fork && api::Posix::CheckError(::fork())) |
48 |
douglas |
402 |
return 0; |
49 |
|
|
|
50 |
douglas |
418 |
Spectre2 spectre(config); |
51 |
douglas |
417 |
|
52 |
|
|
return 0; |
53 |
|
|
} |
54 |
|
|
|
55 |
douglas |
405 |
extern "C" |
56 |
|
|
{ |
57 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap); |
58 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap); |
59 |
|
|
} |
60 |
|
|
|
61 |
douglas |
418 |
Spectre2::Spectre2(const api::Path& config) |
62 |
douglas |
402 |
{ |
63 |
|
|
{ |
64 |
douglas |
418 |
_H<xml::Document> document(xml::Parse(config.GetPath())); |
65 |
|
|
|
66 |
|
|
pid = *document/"spectre"/"pid"; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
{ |
70 |
douglas |
402 |
api::FileWriter out(pid); |
71 |
|
|
ios::FormatWriter fout(out); |
72 |
|
|
|
73 |
|
|
fout << ::getpid() << ios::NewLine; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
Mounter mounter(config); |
77 |
|
|
Unmounter unmounter(config); |
78 |
douglas |
405 |
|
79 |
douglas |
406 |
daemons.InsertLast(dynamic_cast<Daemon*>(&mounter)); |
80 |
|
|
daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter)); |
81 |
douglas |
405 |
|
82 |
|
|
{ |
83 |
|
|
struct ::sigaction action; |
84 |
|
|
|
85 |
|
|
action.sa_sigaction = reload; |
86 |
|
|
action.sa_flags = SA_SIGINFO; |
87 |
|
|
|
88 |
|
|
api::Posix::CheckError(::sigemptyset(&action.sa_mask)); |
89 |
douglas |
415 |
api::Posix::CheckError(::sigaction(SIGUSR1, &action, NULL)); |
90 |
douglas |
405 |
|
91 |
|
|
action.sa_sigaction = stop; |
92 |
|
|
|
93 |
|
|
api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL)); |
94 |
|
|
} |
95 |
douglas |
406 |
|
96 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start(); |
97 |
|
|
_foreach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->wait(); |
98 |
douglas |
402 |
} |
99 |
|
|
|
100 |
|
|
Spectre2::~Spectre2() |
101 |
|
|
{ |
102 |
|
|
api::Posix::CheckError(::unlink(pid.NullTerminate())); |
103 |
|
|
} |
104 |
|
|
|
105 |
douglas |
414 |
ext::String Spectre2::program(api::GetExecutablePath().GetName()), Spectre2::prefix(_Spectre2_prefix_), Spectre2::root(_Spectre2_root_); |
106 |
douglas |
402 |
bool Spectre2::debug(false); |
107 |
douglas |
405 |
ext::Vector<Daemon*> Spectre2::daemons; |
108 |
|
|
|
109 |
|
|
extern "C" |
110 |
|
|
{ |
111 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap) |
112 |
|
|
{ |
113 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->reload(); |
114 |
douglas |
405 |
} |
115 |
|
|
|
116 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap) |
117 |
|
|
{ |
118 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->stop(); |
119 |
douglas |
405 |
} |
120 |
|
|
} |