--- Spectre2/Daemon.cpp 2004/12/31 06:32:13 407 +++ Spectre2/Daemon.cpp 2005/03/26 02:47:25 429 @@ -5,26 +5,96 @@ // $Id$ #include "Daemon.hpp" +#include "Mounter.hpp" +#include "Unmounter.hpp" -void Daemon::wait() +#include +#include + +#include + +Daemon::~Daemon() +{ + thread->Join(); + +#ifdef __FreeBSD__ + api::Posix::CheckError(::unlink(secret.NullTerminate())); +#endif +} + +#ifdef __FreeBSD__ +ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); +#endif + +int Daemon::loop() { - if (!thread.IsEmpty()) + while (running) if (loaded) { - thread->Join(); + ext::ThreadSet<> threads; - thread.Clear(); + threads.Add(etl::BindAll(&Daemon::work, this)); + threads.Add(etl::BindAll(&Daemon::work, this)); + threads.Join(); } + else load(); + + return 0; } -void Daemon::start() +void Daemon::load() { - running = true; - thread = new api::Thread(etl::BindAll(&Daemon::loop, this)); + shares.Clear(); + Share::passwords.Clear(); + + _H document(xml::Parse(config.GetPath())); + _H spectre(*document/"spectre"); +#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 + } + + shares.Output(api::Cout); + + loaded = true; } -int Daemon::loop() +template +int Daemon::work() { - while (running) if (loaded) run(); else load(); + while (running && loaded) + { + ext::ThreadSet<> workers; + + _foreach (const ext::RedBlackSet, share, shares) if (Worker::workable(*share)) + workers.Add(etl::BindAll(&Worker::work, *share)); + + workers.Join(); + } return 0; }