ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Daemon.cpp
Revision: 432
Committed: 2005-03-27T01:56:29-08:00 (20 years, 2 months ago) by douglas
File size: 3773 byte(s)
Log Message:
It should almost do everything it's supposed to.

File Contents

# User Rev Content
1 douglas 402 // Spectre 2
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Daemon.hpp"
8 douglas 429 #include "Mounter.hpp"
9     #include "Unmounter.hpp"
10 douglas 402
11 douglas 429 #include <menes-api/environment.hpp>
12     #include <menes-api/socket.hpp>
13    
14     #include <arpa/inet.h>
15    
16 douglas 431 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 douglas 429 Daemon::~Daemon()
22 douglas 407 {
23 douglas 429 thread->Join();
24    
25 douglas 431 ::smbc_free_context(::smbc_set_context(NULL), 1);
26    
27 douglas 429 #ifdef __FreeBSD__
28     api::Posix::CheckError(::unlink(secret.NullTerminate()));
29 douglas 432 #else
30     _foreach (const ext::RedBlackMap<ext::String>, secret, secrets)
31     api::Posix::CheckError(::unlink(secret.NullTerminate()));
32 douglas 429 #endif
33     }
34    
35 douglas 431 ext::RedBlackSet<Share> Daemon::shares;
36     //api::ThreadMutex Daemon::smbcLock;
37 douglas 429 #ifdef __FreeBSD__
38     ext::String Daemon::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc");
39 douglas 432 api::ThreadMutex Daemon::secretLock;
40     #else
41     ext::RedBlackMap<ext::String> Daemon::secrets;
42     api::ThreadMutex Daemon::secretsLock;
43 douglas 429 #endif
44    
45     int Daemon::loop()
46     {
47     while (running) if (loaded)
48 douglas 407 {
49 douglas 429 ext::ThreadSet<> threads;
50 douglas 407
51 douglas 429 threads.Add(etl::BindAll(&Daemon::work<Mounter>, this));
52     threads.Add(etl::BindAll(&Daemon::work<Unmounter>, this));
53     threads.Join();
54 douglas 407 }
55 douglas 429 else load();
56    
57     return 0;
58 douglas 407 }
59    
60 douglas 429 void Daemon::load()
61 douglas 403 {
62 douglas 429 shares.Clear();
63     Share::passwords.Clear();
64    
65     _H<xml::Document> document(xml::Parse(config.GetPath()));
66     _H<xml::Node> spectre(*document/"spectre");
67 douglas 431
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 douglas 432 if (!(*spectre/"umount").IsEmpty())
78     Spectre2::umount = *spectre/"umount";
79    
80 douglas 431 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 douglas 429 #ifdef __FreeBSD__
86     SecretFileWriter out(secret);
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 douglas 431 if (Spectre2::debug)
116     shares.Output(api::Cout);
117 douglas 429
118     loaded = true;
119 douglas 403 }
120    
121 douglas 429 template <typename Worker>
122     int Daemon::work()
123 douglas 402 {
124 douglas 429 while (running && loaded)
125     {
126     ext::ThreadSet<> workers;
127 douglas 402
128 douglas 431 _foreach (const ext::RedBlackSet<Share>, share, shares)
129     workers.Add(etl::BindAll(&Daemon::work_<Worker>, this, *share));
130 douglas 429
131 douglas 431 // 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     ::SMBCCTX* old(::smbc_set_context(context));
142    
143     CheckError(::smbc_free_context(old, 0));
144     }
145     catch (const Error&) { --count; }*/
146    
147 douglas 432 if (running && loaded)
148 douglas 431 sleep();
149    
150 douglas 429 workers.Join();
151     }
152    
153 douglas 402 return 0;
154     }
155 douglas 431
156     template <typename Worker>
157     int Daemon::work_(const Share& share)
158     {
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 douglas 432 ::nanosleep(&wait, NULL);
172 douglas 431 }

Properties

Name Value
svn:eol-style native
svn:keywords Id