1 |
douglas |
402 |
// Spectre 2 |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Mounter.hpp" |
8 |
|
|
#include "Unmounter.hpp" |
9 |
|
|
|
10 |
|
|
#include <menes-api/exename.hpp> |
11 |
douglas |
417 |
#include <menes-app/simple.hpp> |
12 |
douglas |
402 |
|
13 |
|
|
#include <sys/types.h> |
14 |
douglas |
405 |
#include <signal.h> |
15 |
douglas |
402 |
|
16 |
douglas |
417 |
int Main(const app::Options& options) |
17 |
douglas |
402 |
{ |
18 |
douglas |
418 |
api::Path config(Spectre2::prefix + "/etc/spectre.xml"); |
19 |
douglas |
428 |
enum Mode { none, pid = 1, fork } mode(none); |
20 |
douglas |
417 |
|
21 |
|
|
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
22 |
douglas |
402 |
{ |
23 |
douglas |
417 |
Matcher matcher; |
24 |
douglas |
402 |
|
25 |
douglas |
417 |
if (*arg == matcher("^-config=(.*)$")) |
26 |
|
|
config = matcher[1]; |
27 |
douglas |
428 |
else if (*arg == "-pid") |
28 |
|
|
mode = pid; |
29 |
douglas |
417 |
else if (*arg == "-fork") |
30 |
douglas |
428 |
mode = fork; |
31 |
douglas |
417 |
else if (*arg == "-D") |
32 |
|
|
Spectre2::debug = true; |
33 |
|
|
else |
34 |
douglas |
402 |
{ |
35 |
douglas |
428 |
api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-pid|-fork] [-D]" << ios::NewLine; |
36 |
douglas |
402 |
|
37 |
douglas |
417 |
return 1; |
38 |
douglas |
402 |
} |
39 |
douglas |
417 |
} |
40 |
douglas |
402 |
|
41 |
douglas |
418 |
if (!config.Exists()) |
42 |
|
|
{ |
43 |
|
|
api::Cerr << Spectre2::program << ": configuration file " << config.GetPath() << " does not exist." << ios::NewLine; |
44 |
|
|
|
45 |
|
|
return 2; |
46 |
|
|
} |
47 |
|
|
|
48 |
douglas |
428 |
switch (mode) |
49 |
|
|
{ |
50 |
|
|
case pid: |
51 |
|
|
try |
52 |
|
|
{ |
53 |
|
|
_H<xml::Document> document(xml::Parse(config.GetPath())); |
54 |
|
|
api::FileReader in(*document/"spectre"/"pid"); |
55 |
douglas |
402 |
|
56 |
douglas |
428 |
ios::ReadToWrite(in, api::Cout); |
57 |
|
|
} |
58 |
|
|
catch (const api::Error&) |
59 |
|
|
{ |
60 |
|
|
return 1; |
61 |
|
|
} |
62 |
|
|
break; |
63 |
|
|
case fork: |
64 |
|
|
if (api::Posix::CheckError(::fork())) |
65 |
|
|
return 0; |
66 |
|
|
default: |
67 |
|
|
{ |
68 |
|
|
Spectre2 spectre(config); |
69 |
|
|
} |
70 |
|
|
} |
71 |
douglas |
417 |
|
72 |
|
|
return 0; |
73 |
|
|
} |
74 |
|
|
|
75 |
douglas |
405 |
extern "C" |
76 |
|
|
{ |
77 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap); |
78 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap); |
79 |
|
|
} |
80 |
|
|
|
81 |
douglas |
418 |
Spectre2::Spectre2(const api::Path& config) |
82 |
douglas |
402 |
{ |
83 |
|
|
{ |
84 |
douglas |
418 |
_H<xml::Document> document(xml::Parse(config.GetPath())); |
85 |
douglas |
428 |
_H<xml::Node> spectre(*document/"spectre"); |
86 |
douglas |
418 |
|
87 |
douglas |
428 |
pid = *spectre/"pid"; |
88 |
|
|
|
89 |
|
|
if (!(*spectre/"prefix").IsEmpty()) |
90 |
|
|
prefix = *spectre/"prefix"; |
91 |
|
|
|
92 |
|
|
if (!(*spectre/"root").IsEmpty()) |
93 |
|
|
root = *spectre/"root"; |
94 |
|
|
|
95 |
|
|
if (!(*spectre/"mount").IsEmpty()) |
96 |
|
|
mount = *spectre/"mount"; |
97 |
douglas |
418 |
} |
98 |
|
|
|
99 |
|
|
{ |
100 |
douglas |
402 |
api::FileWriter out(pid); |
101 |
|
|
ios::FormatWriter fout(out); |
102 |
|
|
|
103 |
|
|
fout << ::getpid() << ios::NewLine; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
Mounter mounter(config); |
107 |
|
|
Unmounter unmounter(config); |
108 |
douglas |
405 |
|
109 |
douglas |
406 |
daemons.InsertLast(dynamic_cast<Daemon*>(&mounter)); |
110 |
|
|
daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter)); |
111 |
douglas |
405 |
|
112 |
|
|
{ |
113 |
|
|
struct ::sigaction action; |
114 |
|
|
|
115 |
|
|
action.sa_sigaction = reload; |
116 |
|
|
action.sa_flags = SA_SIGINFO; |
117 |
|
|
|
118 |
|
|
api::Posix::CheckError(::sigemptyset(&action.sa_mask)); |
119 |
douglas |
415 |
api::Posix::CheckError(::sigaction(SIGUSR1, &action, NULL)); |
120 |
douglas |
405 |
|
121 |
|
|
action.sa_sigaction = stop; |
122 |
|
|
|
123 |
douglas |
428 |
api::Posix::CheckError(::sigaction(SIGINT, &action, NULL)); |
124 |
douglas |
405 |
api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL)); |
125 |
|
|
} |
126 |
douglas |
406 |
|
127 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start(); |
128 |
|
|
_foreach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->wait(); |
129 |
douglas |
402 |
} |
130 |
|
|
|
131 |
|
|
Spectre2::~Spectre2() |
132 |
|
|
{ |
133 |
|
|
api::Posix::CheckError(::unlink(pid.NullTerminate())); |
134 |
|
|
} |
135 |
|
|
|
136 |
douglas |
428 |
ext::String Spectre2::program(api::GetExecutablePath().GetName()), Spectre2::prefix(_Spectre2_prefix_), Spectre2::root(_Spectre2_root_), Spectre2::mount(_Spectre2_mount_); |
137 |
douglas |
402 |
bool Spectre2::debug(false); |
138 |
douglas |
405 |
ext::Vector<Daemon*> Spectre2::daemons; |
139 |
douglas |
428 |
ext::RedBlackSet<Share> Spectre2::shares; |
140 |
|
|
api::ThreadMutex Spectre2::sharesLock; |
141 |
douglas |
405 |
|
142 |
|
|
extern "C" |
143 |
|
|
{ |
144 |
|
|
void reload(int signal, ::siginfo_t* info, void* uap) |
145 |
|
|
{ |
146 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->reload(); |
147 |
douglas |
405 |
} |
148 |
|
|
|
149 |
|
|
void stop(int signal, ::siginfo_t* info, void* uap) |
150 |
|
|
{ |
151 |
douglas |
417 |
_foreach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->stop(); |
152 |
douglas |
405 |
} |
153 |
|
|
} |