ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Spectre2.cpp
Revision: 407
Committed: 2004-12-30T22:32:13-08:00 (20 years, 5 months ago) by douglas
File size: 2476 byte(s)
Log Message:
Worky!

File Contents

# User Rev Content
1 douglas 402 // Spectre 2
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Mounter.hpp"
8     #include "Unmounter.hpp"
9     #include "Matcher/Matcher.hpp"
10    
11     #include <menes-api/exename.hpp>
12     #include <menes-api/files.hpp>
13     #include <menes-app/application.hpp>
14    
15     extern "C"
16     {
17     #include <sys/types.h>
18 douglas 405 #include <signal.h>
19 douglas 402 }
20    
21     struct Spectre2Command : public app::Application
22     {
23     virtual int Run(const app::ArgumentList& args)
24     {
25     ext::String config("spectre.xml"), pid("spectre.pid");
26     bool fork(false);
27    
28     _foreach (app::ArgumentList, arg, args)
29     {
30     Matcher matcher;
31    
32     if (*arg == matcher("^-config=(.*)$")) config = matcher[1];
33     else if (*arg == "-fork") fork = true;
34     else if (*arg == matcher("^-pid=(.*)$")) pid = matcher[1];
35     else if (*arg == "-D") Spectre2::debug = true;
36     else
37     {
38     api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-fork] [-pid=pid] [-D]" << ios::NewLine;
39    
40     return 1;
41     }
42     }
43    
44     if (fork && api::Posix::CheckError(::fork())) return 0;
45    
46     Spectre2(config, pid);
47    
48     return 0;
49     }
50     } spectre;
51    
52 douglas 405 extern "C"
53     {
54     void reload(int signal, ::siginfo_t* info, void* uap);
55     void stop(int signal, ::siginfo_t* info, void* uap);
56     }
57    
58 douglas 402 Spectre2::Spectre2(const ext::String& config, const ext::String& pid) : pid(pid)
59     {
60     {
61     api::FileWriter out(pid);
62     ios::FormatWriter fout(out);
63    
64     fout << ::getpid() << ios::NewLine;
65     }
66    
67     Mounter mounter(config);
68     Unmounter unmounter(config);
69 douglas 405
70 douglas 406 daemons.InsertLast(dynamic_cast<Daemon*>(&mounter));
71     daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter));
72 douglas 405
73     {
74     struct ::sigaction action;
75    
76     action.sa_sigaction = reload;
77     action.sa_flags = SA_SIGINFO;
78    
79     api::Posix::CheckError(::sigemptyset(&action.sa_mask));
80     api::Posix::CheckError(::sigaction(SIGHUP, &action, NULL));
81    
82     action.sa_sigaction = stop;
83    
84     api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL));
85     }
86 douglas 406
87     _mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start();
88 douglas 407 _mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->wait();
89 douglas 402 }
90    
91     Spectre2::~Spectre2()
92     {
93     api::Posix::CheckError(::unlink(pid.NullTerminate()));
94     }
95    
96     ext::String Spectre2::program(api::GetExecutablePath().GetName());
97     bool Spectre2::debug(false);
98 douglas 405 ext::Vector<Daemon*> Spectre2::daemons;
99    
100     extern "C"
101     {
102     void reload(int signal, ::siginfo_t* info, void* uap)
103     {
104     _mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->reload();
105     }
106    
107     void stop(int signal, ::siginfo_t* info, void* uap)
108     {
109     _mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->stop();
110     }
111     }

Properties

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