1 |
// Spectre 2 |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Daemon.hpp" |
8 |
#include "Mounter.hpp" |
9 |
#include "Unmounter.hpp" |
10 |
|
11 |
#include <menes-api/environment.hpp> |
12 |
#include <menes-api/socket.hpp> |
13 |
|
14 |
#include <arpa/inet.h> |
15 |
|
16 |
extern "C" |
17 |
{ |
18 |
void authenticate(const char* server, const char* share, char* work, int workSize, char* user, int userSize, char* password, int passwordSize); |
19 |
} |
20 |
|
21 |
Daemon::~Daemon() |
22 |
{ |
23 |
thread->Join(); |
24 |
|
25 |
::smbc_free_context(::smbc_set_context(NULL), 1); |
26 |
|
27 |
#ifdef __FreeBSD__ |
28 |
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
29 |
#else |
30 |
_foreach (const ext::RedBlackMap<ext::String>, secret, secrets) |
31 |
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
32 |
#endif |
33 |
} |
34 |
|
35 |
ext::RedBlackSet<Share> Daemon::shares; |
36 |
#ifdef __FreeBSD__ |
37 |
ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); |
38 |
api::ThreadMutex Daemon::secretLock; |
39 |
#else |
40 |
ext::RedBlackMap<ext::String> Daemon::secrets; |
41 |
api::ThreadMutex Daemon::secretsLock; |
42 |
#endif |
43 |
|
44 |
int Daemon::loop() |
45 |
{ |
46 |
while (running) if (loaded) |
47 |
{ |
48 |
ext::ThreadSet<> threads; |
49 |
|
50 |
threads.Add(etl::BindAll(&Daemon::work<Mounter>, this)); |
51 |
threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this)); |
52 |
threads.Join(); |
53 |
} |
54 |
else load(); |
55 |
|
56 |
return 0; |
57 |
} |
58 |
|
59 |
void Daemon::load() |
60 |
{ |
61 |
#ifndef __FreeBSD__ |
62 |
_foreach (const ext::RedBlackMap<ext::String>, secret, secrets) |
63 |
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
64 |
|
65 |
secrets.Clear(); |
66 |
#endif |
67 |
shares.Clear(); |
68 |
Share::passwords.Clear(); |
69 |
|
70 |
_H<xml::Document> document(xml::Parse(config.GetPath())); |
71 |
_H<xml::Node> spectre(*document/"spectre"); |
72 |
|
73 |
if (!(*spectre/"prefix").IsEmpty()) |
74 |
Spectre2::prefix = *spectre/"prefix"; |
75 |
|
76 |
if (!(*spectre/"root").IsEmpty()) |
77 |
Spectre2::root = *spectre/"root"; |
78 |
|
79 |
if (!(*spectre/"mount").IsEmpty()) |
80 |
Spectre2::mount = *spectre/"mount"; |
81 |
|
82 |
if (!(*spectre/"umount").IsEmpty()) |
83 |
Spectre2::umount = *spectre/"umount"; |
84 |
|
85 |
if (!(*spectre/"interval").IsEmpty()) |
86 |
interval = lexical_cast<unsigned>(ext::String(*spectre/"interval")); |
87 |
|
88 |
CheckError(::smbc_init(authenticate, Spectre2::debug ? 2 : 0)); |
89 |
|
90 |
#ifdef __FreeBSD__ |
91 |
api::FileWriter out(secret, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
92 |
ios::FormatWriter fout(out); |
93 |
#endif |
94 |
|
95 |
_foreach (const xml::NodeSet, host_, *spectre/"host") |
96 |
{ |
97 |
ext::String host(**host_/"name"); |
98 |
|
99 |
_foreach (const xml::NodeSet, share, **host_/"share") |
100 |
{ |
101 |
ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group"); |
102 |
|
103 |
shares.Insert(Share(host, name, owner, user, group)); |
104 |
} |
105 |
|
106 |
#ifdef __FreeBSD__ |
107 |
::addrinfo* info; |
108 |
|
109 |
api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info)); |
110 |
|
111 |
::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr)); |
112 |
ext::Buffer buffer(128); |
113 |
|
114 |
fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine; |
115 |
|
116 |
::freeaddrinfo(info); |
117 |
#endif |
118 |
} |
119 |
|
120 |
if (Spectre2::debug) |
121 |
shares.Output(api::Cout); |
122 |
|
123 |
loaded = true; |
124 |
} |
125 |
|
126 |
template <typename Worker> |
127 |
int Daemon::work() |
128 |
{ |
129 |
while (running && loaded) |
130 |
{ |
131 |
ext::ThreadSet<> workers; |
132 |
|
133 |
_foreach (const ext::RedBlackSet<Share>, share, shares) |
134 |
workers.Add(etl::BindAll(&Daemon::work_<Worker>, this, *share)); |
135 |
|
136 |
if (running && loaded) |
137 |
sleep(); |
138 |
|
139 |
workers.Join(); |
140 |
} |
141 |
|
142 |
return 0; |
143 |
} |
144 |
|
145 |
template <typename Worker> |
146 |
int Daemon::work_(const Share& share) |
147 |
{ |
148 |
Worker worker(share); |
149 |
|
150 |
if (worker) |
151 |
worker(); |
152 |
|
153 |
return 0; |
154 |
} |
155 |
|
156 |
void Daemon::sleep() |
157 |
{ |
158 |
::timespec wait = { interval, 0 }; |
159 |
|
160 |
::nanosleep(&wait, NULL); |
161 |
} |