ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Daemon.cpp
Revision: 441
Committed: 2005-04-12T15:42:24-07:00 (20 years, 2 months ago) by douglas
File size: 3493 byte(s)
Log Message:
Yeah, whatever.

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

Properties

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