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 |
|
#endif |
30 |
|
} |
31 |
|
|
32 |
< |
ext::RedBlackSet<Share> shares; |
33 |
< |
|
32 |
> |
ext::RedBlackSet<Share> Daemon::shares; |
33 |
> |
//api::ThreadMutex Daemon::smbcLock; |
34 |
|
#ifdef __FreeBSD__ |
35 |
|
ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); |
36 |
|
#endif |
57 |
|
|
58 |
|
_H<xml::Document> document(xml::Parse(config.GetPath())); |
59 |
|
_H<xml::Node> spectre(*document/"spectre"); |
60 |
+ |
|
61 |
+ |
if (!(*spectre/"prefix").IsEmpty()) |
62 |
+ |
Spectre2::prefix = *spectre/"prefix"; |
63 |
+ |
|
64 |
+ |
if (!(*spectre/"root").IsEmpty()) |
65 |
+ |
Spectre2::root = *spectre/"root"; |
66 |
+ |
|
67 |
+ |
if (!(*spectre/"mount").IsEmpty()) |
68 |
+ |
Spectre2::mount = *spectre/"mount"; |
69 |
+ |
|
70 |
+ |
if (!(*spectre/"interval").IsEmpty()) |
71 |
+ |
interval = lexical_cast<unsigned>(ext::String(*spectre/"interval")); |
72 |
+ |
|
73 |
+ |
CheckError(::smbc_init(authenticate, Spectre2::debug ? 2 : 0)); |
74 |
+ |
|
75 |
|
#ifdef __FreeBSD__ |
76 |
|
SecretFileWriter out(secret); |
77 |
|
ios::FormatWriter fout(out); |
102 |
|
#endif |
103 |
|
} |
104 |
|
|
105 |
< |
shares.Output(api::Cout); |
105 |
> |
if (Spectre2::debug) |
106 |
> |
shares.Output(api::Cout); |
107 |
|
|
108 |
|
loaded = true; |
109 |
|
} |
115 |
|
{ |
116 |
|
ext::ThreadSet<> workers; |
117 |
|
|
118 |
< |
_foreach (const ext::RedBlackSet<Share>, share, shares) if (Worker::workable(*share)) |
119 |
< |
workers.Add(etl::BindAll(&Worker::work, *share)); |
118 |
> |
_foreach (const ext::RedBlackSet<Share>, share, shares) |
119 |
> |
workers.Add(etl::BindAll(&Daemon::work_<Worker>, this, *share)); |
120 |
> |
|
121 |
> |
// XXX: not useful for solving the problem it was supposed to solve |
122 |
> |
/*_synchronized (smbcLock) if (++count % 8 == 0) try |
123 |
> |
{ |
124 |
> |
::SMBCCTX* context(::smbc_new_context()); |
125 |
> |
|
126 |
> |
context->debug = Spectre2::debug ? 2 : 0; |
127 |
> |
context->callbacks.auth_fn = authenticate; |
128 |
> |
|
129 |
> |
CheckError(::smbc_init_context(context)); |
130 |
> |
|
131 |
> |
::SMBCCTX* old(::smbc_set_context(context)); |
132 |
> |
|
133 |
> |
CheckError(::smbc_free_context(old, 0)); |
134 |
> |
} |
135 |
> |
catch (const Error&) { --count; }*/ |
136 |
> |
|
137 |
> |
if (running && loaded) try |
138 |
> |
{ |
139 |
> |
sleep(); |
140 |
> |
} |
141 |
> |
catch (const api::Error&) |
142 |
> |
{ |
143 |
> |
api::Cout << "Can't sleep!" << ios::NewLine; |
144 |
> |
} |
145 |
|
|
146 |
|
workers.Join(); |
147 |
|
} |
148 |
|
|
149 |
|
return 0; |
150 |
|
} |
151 |
+ |
|
152 |
+ |
template <typename Worker> |
153 |
+ |
int Daemon::work_(const Share& share) |
154 |
+ |
{ |
155 |
+ |
Worker worker(share); |
156 |
+ |
|
157 |
+ |
if (worker) |
158 |
+ |
worker(); |
159 |
+ |
|
160 |
+ |
return 0; |
161 |
+ |
} |
162 |
+ |
|
163 |
+ |
void Daemon::sleep() |
164 |
+ |
{ |
165 |
+ |
::timespec wait = { interval, 0 }; |
166 |
+ |
|
167 |
+ |
api::Posix::CheckError(::nanosleep(&wait, NULL)); |
168 |
+ |
} |