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