--- Spectre2/Daemon.cpp 2004/12/30 06:57:50 403 +++ Spectre2/Daemon.cpp 2005/03/27 06:35:26 431 @@ -5,26 +5,164 @@ // $Id$ #include "Daemon.hpp" +#include "Mounter.hpp" +#include "Unmounter.hpp" -Daemon::Daemon() : running(true), loaded(false), thread(new api::Thread(etl::BindAll(&Daemon::loop, this))) {} +#include +#include + +#include + +extern "C" +{ + void authenticate(const char* server, const char* share, char* work, int workSize, char* user, int userSize, char* password, int passwordSize); +} + +Daemon::~Daemon() +{ + thread->Join(); + + ::smbc_free_context(::smbc_set_context(NULL), 1); + +#ifdef __FreeBSD__ + api::Posix::CheckError(::unlink(secret.NullTerminate())); +#endif +} + +ext::RedBlackSet Daemon::shares; +//api::ThreadMutex Daemon::smbcLock; +#ifdef __FreeBSD__ +ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); +#endif + +int Daemon::loop() +{ + while (running) if (loaded) + { + ext::ThreadSet<> threads; + + threads.Add(etl::BindAll(&Daemon::work, this)); + threads.Add(etl::BindAll(&Daemon::work, this)); + threads.Join(); + } + else load(); + + return 0; +} void Daemon::load() { - api::Cerr << this << ios::NewLine; + shares.Clear(); + Share::passwords.Clear(); + + _H document(xml::Parse(config.GetPath())); + _H spectre(*document/"spectre"); + + if (!(*spectre/"prefix").IsEmpty()) + Spectre2::prefix = *spectre/"prefix"; + + if (!(*spectre/"root").IsEmpty()) + Spectre2::root = *spectre/"root"; + + if (!(*spectre/"mount").IsEmpty()) + Spectre2::mount = *spectre/"mount"; + if (!(*spectre/"interval").IsEmpty()) + interval = lexical_cast(ext::String(*spectre/"interval")); + + CheckError(::smbc_init(authenticate, Spectre2::debug ? 2 : 0)); + +#ifdef __FreeBSD__ + SecretFileWriter out(secret); + ios::FormatWriter fout(out); +#endif + + _foreach (const xml::NodeSet, host_, *spectre/"host") + { + ext::String host(**host_/"name"); + + _foreach (const xml::NodeSet, share, **host_/"share") + { + ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group"); + + shares.Insert(Share(host, name, owner, user, group)); + } + +#ifdef __FreeBSD__ + ::addrinfo* info; + + api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info)); + + ::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr)); + ext::Buffer buffer(128); + + fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine; + + ::freeaddrinfo(info); +#endif + } + + if (Spectre2::debug) + shares.Output(api::Cout); + loaded = true; } -void Daemon::run() +template +int Daemon::work() { - api::Cerr << this << ios::NewLine; + while (running && loaded) + { + ext::ThreadSet<> workers; + + _foreach (const ext::RedBlackSet, share, shares) + workers.Add(etl::BindAll(&Daemon::work_, this, *share)); + + // XXX: not useful for solving the problem it was supposed to solve + /*_synchronized (smbcLock) if (++count % 8 == 0) try + { + ::SMBCCTX* context(::smbc_new_context()); + + context->debug = Spectre2::debug ? 2 : 0; + context->callbacks.auth_fn = authenticate; + + CheckError(::smbc_init_context(context)); + + ::SMBCCTX* old(::smbc_set_context(context)); + + CheckError(::smbc_free_context(old, 0)); + } + catch (const Error&) { --count; }*/ + + if (running && loaded) try + { + sleep(); + } + catch (const api::Error&) + { + api::Cout << "Can't sleep!" << ios::NewLine; + } + + workers.Join(); + } - running = false; + return 0; } -int Daemon::loop() +template +int Daemon::work_(const Share& share) { - while (running) if (loaded) run(); else load(); + Worker worker(share); + + if (worker) + worker(); return 0; } + +void Daemon::sleep() +{ + ::timespec wait = { interval, 0 }; + + api::Posix::CheckError(::nanosleep(&wait, NULL)); +}