5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "Daemon.hpp" |
8 |
+ |
#include "Mounter.hpp" |
9 |
+ |
#include "Unmounter.hpp" |
10 |
|
|
11 |
< |
void Daemon::wait() |
11 |
> |
#include <menes-api/environment.hpp> |
12 |
> |
#include <menes-api/socket.hpp> |
13 |
> |
|
14 |
> |
#include <arpa/inet.h> |
15 |
> |
|
16 |
> |
Daemon::~Daemon() |
17 |
> |
{ |
18 |
> |
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 |
< |
if (!thread.IsEmpty()) |
31 |
> |
while (running) if (loaded) |
32 |
|
{ |
33 |
< |
thread->Join(); |
33 |
> |
ext::ThreadSet<> threads; |
34 |
|
|
35 |
< |
thread.Clear(); |
35 |
> |
threads.Add(etl::BindAll(&Daemon::work<Mounter>, this)); |
36 |
> |
threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this)); |
37 |
> |
threads.Join(); |
38 |
|
} |
39 |
+ |
else load(); |
40 |
+ |
|
41 |
+ |
return 0; |
42 |
|
} |
43 |
|
|
44 |
< |
void Daemon::start() |
44 |
> |
void Daemon::load() |
45 |
|
{ |
46 |
< |
running = true; |
47 |
< |
thread = new api::Thread(etl::BindAll(&Daemon::loop, this)); |
46 |
> |
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 |
|
} |
85 |
|
|
86 |
< |
int Daemon::loop() |
86 |
> |
template <typename Worker> |
87 |
> |
int Daemon::work() |
88 |
|
{ |
89 |
< |
while (running) if (loaded) run(); else load(); |
89 |
> |
while (running && loaded) |
90 |
> |
{ |
91 |
> |
ext::ThreadSet<> workers; |
92 |
> |
|
93 |
> |
_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 |
|
return 0; |
100 |
|
} |