--- Spectre2/Daemon.cpp 2004/12/30 06:38:37 402 +++ Spectre2/Daemon.cpp 2005/04/07 22:56:53 439 @@ -5,12 +5,168 @@ // $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())); +#else + _foreach (const ext::RedBlackMap, secret, secrets) + 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"); +api::ThreadMutex Daemon::secretLock; +#else +ext::RedBlackMap Daemon::secrets; +api::ThreadMutex Daemon::secretsLock; +#endif int Daemon::loop() { - while (running) if (loaded) run(); else load(); + 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() +{ + 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/"umount").IsEmpty()) + Spectre2::umount = *spectre/"umount"; + + if (!(*spectre/"interval").IsEmpty()) + interval = lexical_cast(ext::String(*spectre/"interval")); + + CheckError(::smbc_init(authenticate, Spectre2::debug ? 2 : 0)); + +#ifdef __FreeBSD__ + api::FileWriter out(secret, O_WRONLY | O_CREAT | O_TRUNC, 0600); + 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; +} + +template +int Daemon::work() +{ + 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) + sleep(); + + workers.Join(); + } + + return 0; +} + +template +int Daemon::work_(const Share& share) +{ + Worker worker(share); + + if (worker) + worker(); + + return 0; +} + +void Daemon::sleep() +{ + ::timespec wait = { interval, 0 }; + + ::nanosleep(&wait, NULL); +}