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() |
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 |
< |
SecretFileWriter out(secret); |
91 |
> |
api::FileWriter out(secret, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
92 |
|
ios::FormatWriter fout(out); |
93 |
|
#endif |
94 |
|
|
117 |
|
#endif |
118 |
|
} |
119 |
|
|
120 |
< |
shares.Output(api::Cout); |
120 |
> |
if (Spectre2::debug) |
121 |
> |
shares.Output(api::Cout); |
122 |
|
|
123 |
|
loaded = true; |
124 |
|
} |
130 |
|
{ |
131 |
|
ext::ThreadSet<> workers; |
132 |
|
|
133 |
< |
_foreach (const ext::RedBlackSet<Share>, share, shares) if (Worker::workable(*share)) |
134 |
< |
workers.Add(etl::BindAll(&Worker::work, *share)); |
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 |
+ |
} |