ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Daemon.cpp
Revision: 431
Committed: 2005-03-26T22:35:26-08:00 (20 years, 2 months ago) by douglas
File size: 3556 byte(s)
Log Message:
Grr! Why must this thing decided to ask for all the memory in existance at a certain point?

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

Properties

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