1 |
douglas |
402 |
// Spectre 2 |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Daemon.hpp" |
8 |
douglas |
429 |
#include "Mounter.hpp" |
9 |
|
|
#include "Unmounter.hpp" |
10 |
douglas |
402 |
|
11 |
douglas |
429 |
#include <menes-api/environment.hpp> |
12 |
|
|
#include <menes-api/socket.hpp> |
13 |
|
|
|
14 |
|
|
#include <arpa/inet.h> |
15 |
|
|
|
16 |
|
|
Daemon::~Daemon() |
17 |
douglas |
407 |
{ |
18 |
douglas |
429 |
thread->Join(); |
19 |
|
|
|
20 |
|
|
#ifdef __FreeBSD__ |
21 |
|
|
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
22 |
|
|
#endif |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
#ifdef __FreeBSD__ |
26 |
|
|
ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); |
27 |
|
|
#endif |
28 |
|
|
|
29 |
|
|
int Daemon::loop() |
30 |
|
|
{ |
31 |
|
|
while (running) if (loaded) |
32 |
douglas |
407 |
{ |
33 |
douglas |
429 |
ext::ThreadSet<> threads; |
34 |
douglas |
407 |
|
35 |
douglas |
429 |
threads.Add(etl::BindAll(&Daemon::work<Mounter>, this)); |
36 |
|
|
threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this)); |
37 |
|
|
threads.Join(); |
38 |
douglas |
407 |
} |
39 |
douglas |
429 |
else load(); |
40 |
|
|
|
41 |
|
|
return 0; |
42 |
douglas |
407 |
} |
43 |
|
|
|
44 |
douglas |
429 |
void Daemon::load() |
45 |
douglas |
403 |
{ |
46 |
douglas |
429 |
shares.Clear(); |
47 |
|
|
Share::passwords.Clear(); |
48 |
|
|
|
49 |
|
|
_H<xml::Document> document(xml::Parse(config.GetPath())); |
50 |
|
|
_H<xml::Node> spectre(*document/"spectre"); |
51 |
|
|
#ifdef __FreeBSD__ |
52 |
|
|
SecretFileWriter out(secret); |
53 |
|
|
ios::FormatWriter fout(out); |
54 |
|
|
#endif |
55 |
|
|
|
56 |
|
|
_foreach (const xml::NodeSet, host_, *spectre/"host") |
57 |
|
|
{ |
58 |
|
|
ext::String host(**host_/"name"); |
59 |
|
|
|
60 |
|
|
_foreach (const xml::NodeSet, share, **host_/"share") |
61 |
|
|
{ |
62 |
|
|
ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group"); |
63 |
|
|
|
64 |
|
|
shares.Insert(Share(host, name, owner, user, group)); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
#ifdef __FreeBSD__ |
68 |
|
|
::addrinfo* info; |
69 |
|
|
|
70 |
|
|
api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info)); |
71 |
|
|
|
72 |
|
|
::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr)); |
73 |
|
|
ext::Buffer buffer(128); |
74 |
|
|
|
75 |
|
|
fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine; |
76 |
|
|
|
77 |
|
|
::freeaddrinfo(info); |
78 |
|
|
#endif |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
shares.Output(api::Cout); |
82 |
|
|
|
83 |
|
|
loaded = true; |
84 |
douglas |
403 |
} |
85 |
|
|
|
86 |
douglas |
429 |
template <typename Worker> |
87 |
|
|
int Daemon::work() |
88 |
douglas |
402 |
{ |
89 |
douglas |
429 |
while (running && loaded) |
90 |
|
|
{ |
91 |
|
|
ext::ThreadSet<> workers; |
92 |
douglas |
402 |
|
93 |
douglas |
429 |
_foreach (const ext::RedBlackSet<Share>, share, shares) if (Worker::workable(*share)) |
94 |
|
|
workers.Add(etl::BindAll(&Worker::work, *share)); |
95 |
|
|
|
96 |
|
|
workers.Join(); |
97 |
|
|
} |
98 |
|
|
|
99 |
douglas |
402 |
return 0; |
100 |
|
|
} |