ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Daemon.cpp
(Generate patch)

Comparing Spectre2/Daemon.cpp (file contents):
Revision 403 by douglas, 2004-12-29T22:57:50-08:00 vs.
Revision 439 by douglas, 2005-04-07T15:56:53-07:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Daemon.hpp"
8 + #include "Mounter.hpp"
9 + #include "Unmounter.hpp"
10  
11 < Daemon::Daemon() : running(true), loaded(false), thread(new api::Thread(etl::BindAll(&Daemon::loop, this))) {}
11 > #include <menes-api/environment.hpp>
12 > #include <menes-api/socket.hpp>
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 > //api::ThreadMutex Daemon::smbcLock;
37 > #ifdef __FreeBSD__
38 > ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc");
39 > api::ThreadMutex Daemon::secretLock;
40 > #else
41 > ext::RedBlackMap<ext::String> Daemon::secrets;
42 > api::ThreadMutex Daemon::secretsLock;
43 > #endif
44 >
45 > int Daemon::loop()
46 > {
47 >        while (running) if (loaded)
48 >        {
49 >                ext::ThreadSet<> threads;
50 >
51 >                threads.Add(etl::BindAll(&Daemon::work<Mounter>, this));
52 >                threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this));
53 >                threads.Join();
54 >        }
55 >        else load();
56 >
57 >        return 0;
58 > }
59  
60   void Daemon::load()
61   {
62 <        api::Cerr << this << ios::NewLine;
62 >        shares.Clear();
63 >        Share::passwords.Clear();
64 >
65 >        _H<xml::Document> document(xml::Parse(config.GetPath()));
66 >        _H<xml::Node> spectre(*document/"spectre");
67 >                
68 >        if (!(*spectre/"prefix").IsEmpty())
69 >                Spectre2::prefix = *spectre/"prefix";
70 >
71 >        if (!(*spectre/"root").IsEmpty())
72 >                Spectre2::root = *spectre/"root";
73 >
74 >        if (!(*spectre/"mount").IsEmpty())
75 >                Spectre2::mount = *spectre/"mount";
76 >
77 >        if (!(*spectre/"umount").IsEmpty())
78 >                Spectre2::umount = *spectre/"umount";
79 >
80 >        if (!(*spectre/"interval").IsEmpty())
81 >                interval = lexical_cast<unsigned>(ext::String(*spectre/"interval"));
82 >
83 >        CheckError(::smbc_init(authenticate, Spectre2::debug ? 2 : 0));
84 >
85 > #ifdef __FreeBSD__
86 >        api::FileWriter out(secret, O_WRONLY | O_CREAT | O_TRUNC, 0600);
87 >        ios::FormatWriter fout(out);
88 > #endif
89  
90 +        _foreach (const xml::NodeSet, host_, *spectre/"host")
91 +        {
92 +                ext::String host(**host_/"name");
93 +
94 +                _foreach (const xml::NodeSet, share, **host_/"share")
95 +                {
96 +                        ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group");
97 +
98 +                        shares.Insert(Share(host, name, owner, user, group));
99 +                }
100 +
101 + #ifdef __FreeBSD__
102 +                ::addrinfo* info;
103 +
104 +                api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info));
105 +
106 +                ::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr));
107 +                ext::Buffer buffer(128);
108 +
109 +                fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine;
110 +
111 +                ::freeaddrinfo(info);
112 + #endif
113 +        }
114 +
115 +        if (Spectre2::debug)
116 +                shares.Output(api::Cout);
117 +        
118          loaded = true;
119   }
120  
121 < void Daemon::run()
121 > template <typename Worker>
122 > int Daemon::work()
123   {
124 <        api::Cerr << this << ios::NewLine;
124 >        while (running && loaded)
125 >        {
126 >                ext::ThreadSet<> workers;
127 >
128 >                _foreach (const ext::RedBlackSet<Share>, share, shares)
129 >                        workers.Add(etl::BindAll(&Daemon::work_<Worker>, this, *share));
130 >
131 >                // XXX: not useful for solving the problem it was supposed to solve
132 >                /*_synchronized (smbcLock) if (++count % 8 == 0) try
133 >                {
134 >                        ::SMBCCTX* context(::smbc_new_context());
135 >
136 >                        context->debug = Spectre2::debug ? 2 : 0;
137 >                        context->callbacks.auth_fn = authenticate;
138 >
139 >                        CheckError(::smbc_init_context(context));
140  
141 <        running = false;
141 >                        ::SMBCCTX* old(::smbc_set_context(context));
142 >
143 >                        CheckError(::smbc_free_context(old, 0));
144 >                }
145 >                catch (const Error&) { --count; }*/
146 >
147 >                if (running && loaded)
148 >                        sleep();
149 >
150 >                workers.Join();
151 >        }
152 >
153 >        return 0;
154   }
155  
156 < int Daemon::loop()
156 > template <typename Worker>
157 > int Daemon::work_(const Share& share)
158   {
159 <        while (running) if (loaded) run(); else load();
159 >        Worker worker(share);
160 >
161 >        if (worker)
162 >                worker();
163  
164          return 0;
165   }
166 +
167 + void Daemon::sleep()
168 + {
169 +        ::timespec wait = { interval, 0 };
170 +
171 +        ::nanosleep(&wait, NULL);
172 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines