--- Spectre2/Daemon.cpp 2005/03/26 03:01:08 430 +++ Spectre2/Daemon.cpp 2005/04/07 22:56:53 439 @@ -13,19 +13,33 @@ #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 shares; - +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() @@ -50,8 +64,26 @@ void Daemon::load() _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__ - SecretFileWriter out(secret); + api::FileWriter out(secret, O_WRONLY | O_CREAT | O_TRUNC, 0600); ios::FormatWriter fout(out); #endif @@ -80,7 +112,8 @@ void Daemon::load() #endif } - shares.Output(api::Cout); + if (Spectre2::debug) + shares.Output(api::Cout); loaded = true; } @@ -92,11 +125,48 @@ int Daemon::work() { ext::ThreadSet<> workers; - _foreach (const ext::RedBlackSet, share, shares) if (Worker::workable(*share)) - workers.Add(etl::BindAll(&Worker::work, *share)); + _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); +}