ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Spectre2.cpp
Revision: 414
Committed: 2005-01-14T22:35:17-08:00 (20 years, 5 months ago) by douglas
File size: 2500 byte(s)
Log Message:
Oh, yeah!

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

Properties

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