5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "Daemon.hpp" |
8 |
+ |
#include "Mounter.hpp" |
9 |
+ |
#include "Unmounter.hpp" |
10 |
|
|
11 |
< |
Daemon::Daemon() : running(true), loaded(false), thread(new api::Thread(etl::BindAll(&Daemon::loop, this))) {} |
11 |
> |
#include <menes-api/environment.hpp> |
12 |
> |
#include <menes-api/socket.hpp> |
13 |
|
|
14 |
< |
void Daemon::load() |
14 |
> |
#include <arpa/inet.h> |
15 |
> |
|
16 |
> |
Daemon::~Daemon() |
17 |
|
{ |
18 |
< |
api::Cerr << this << ios::NewLine; |
18 |
> |
thread->Join(); |
19 |
|
|
20 |
< |
loaded = true; |
20 |
> |
#ifdef __FreeBSD__ |
21 |
> |
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
22 |
> |
#endif |
23 |
|
} |
24 |
|
|
25 |
< |
void Daemon::run() |
25 |
> |
ext::RedBlackSet<Share> shares; |
26 |
> |
|
27 |
> |
#ifdef __FreeBSD__ |
28 |
> |
ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); |
29 |
> |
#endif |
30 |
> |
|
31 |
> |
int Daemon::loop() |
32 |
|
{ |
33 |
< |
api::Cerr << this << ios::NewLine; |
33 |
> |
while (running) if (loaded) |
34 |
> |
{ |
35 |
> |
ext::ThreadSet<> threads; |
36 |
> |
|
37 |
> |
threads.Add(etl::BindAll(&Daemon::work<Mounter>, this)); |
38 |
> |
threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this)); |
39 |
> |
threads.Join(); |
40 |
> |
} |
41 |
> |
else load(); |
42 |
|
|
43 |
< |
running = false; |
43 |
> |
return 0; |
44 |
|
} |
45 |
|
|
46 |
< |
int Daemon::loop() |
46 |
> |
void Daemon::load() |
47 |
|
{ |
48 |
< |
while (running) if (loaded) run(); else load(); |
48 |
> |
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 |
> |
} |
87 |
> |
|
88 |
> |
template <typename Worker> |
89 |
> |
int Daemon::work() |
90 |
> |
{ |
91 |
> |
while (running && loaded) |
92 |
> |
{ |
93 |
> |
ext::ThreadSet<> workers; |
94 |
> |
|
95 |
> |
_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 |
|
return 0; |
102 |
|
} |