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

File Contents

# Content
1 // 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 #include <unistd.h>
19 #include <signal.h>
20 }
21
22 struct Spectre2Command : public app::Application
23 {
24 virtual int Run(const app::ArgumentList& args)
25 {
26 ext::String config("spectre.xml"), pid("spectre.pid");
27 bool fork(false);
28
29 _foreach (app::ArgumentList, arg, args)
30 {
31 Matcher matcher;
32
33 if (*arg == matcher("^-config=(.*)$")) config = matcher[1];
34 else if (*arg == "-fork") fork = true;
35 else if (*arg == matcher("^-pid=(.*)$")) pid = matcher[1];
36 else if (*arg == "-D") Spectre2::debug = true;
37 else
38 {
39 api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-fork] [-pid=pid] [-D]" << ios::NewLine;
40
41 return 1;
42 }
43 }
44
45 if (fork && api::Posix::CheckError(::fork())) return 0;
46
47 Spectre2(config, pid);
48
49 return 0;
50 }
51 } spectre;
52
53 extern "C"
54 {
55 void reload(int signal, ::siginfo_t* info, void* uap);
56 void stop(int signal, ::siginfo_t* info, void* uap);
57 }
58
59 Spectre2::Spectre2(const ext::String& config, const ext::String& pid) : pid(pid)
60 {
61 {
62 api::FileWriter out(pid);
63 ios::FormatWriter fout(out);
64
65 fout << ::getpid() << ios::NewLine;
66 }
67
68 Mounter mounter(config);
69 Unmounter unmounter(config);
70
71 daemons.InsertLast(dynamic_cast<Daemon*>(&mounter));
72 daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter));
73
74 {
75 struct ::sigaction action;
76
77 action.sa_sigaction = reload;
78 action.sa_flags = SA_SIGINFO;
79
80 api::Posix::CheckError(::sigemptyset(&action.sa_mask));
81 api::Posix::CheckError(::sigaction(SIGHUP, &action, NULL));
82
83 action.sa_sigaction = stop;
84
85 api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL));
86 }
87
88 _mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start();
89 }
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 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