ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Spectre2.cpp
(Generate patch)

Comparing Spectre2/Spectre2.cpp (file contents):
Revision 406 by douglas, 2004-12-30T22:08:35-08:00 vs.
Revision 431 by douglas, 2005-03-26T22:35:26-08:00

# Line 6 | Line 6
6  
7   #include "Mounter.hpp"
8   #include "Unmounter.hpp"
9 #include "Matcher/Matcher.hpp"
9  
10   #include <menes-api/exename.hpp>
11 < #include <menes-api/files.hpp>
13 < #include <menes-app/application.hpp>
11 > #include <menes-app/simple.hpp>
12  
15 extern "C"
16 {
13   #include <sys/types.h>
18 #include <unistd.h>
14   #include <signal.h>
20 }
15  
16 < struct Spectre2Command : public app::Application
16 > int Main(const app::Options& options)
17   {
18 <        virtual int Run(const app::ArgumentList& args)
18 >        api::Path config(Spectre2::prefix + "/etc/spectre.xml");
19 >        enum Mode { none, pid = 1, fork } mode(none);
20 >
21 >        _foreach (const app::ArgumentList, arg, app::GetArguments())
22          {
23 <                ext::String config("spectre.xml"), pid("spectre.pid");
27 <                bool fork(false);
23 >                Matcher matcher;
24  
25 <                _foreach (app::ArgumentList, arg, args)
25 >                if (*arg == matcher("^-config=(.*)$"))
26 >                        config = matcher[1];
27 >                else if (*arg == "-pid")
28 >                        mode = pid;
29 >                else if (*arg == "-fork")
30 >                        mode = fork;
31 >                else if (*arg == "-D")
32 >                        Spectre2::debug = true;
33 >                else
34                  {
35 <                        Matcher matcher;
35 >                        api::Cerr << "Usage: " << Spectre2::program << " [-config=config] [-pid|-fork] [-D]" << ios::NewLine;
36  
37 <                        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 <                        }
37 >                        return 1;
38                  }
39 +        }
40  
41 <                if (fork && api::Posix::CheckError(::fork())) return 0;
41 >        if (!config.Exists())
42 >        {
43 >                api::Cerr << Spectre2::program << ": configuration file " << config.GetPath() << " does not exist." << ios::NewLine;
44  
45 <                Spectre2(config, pid);
45 >                return 2;
46 >        }
47 >
48 >        switch (mode)
49 >        {
50 >        case pid:
51 >                try
52 >                {
53 >                        _H<xml::Document> document(xml::Parse(config.GetPath()));
54 >                        api::FileReader in(*document/"spectre"/"pid");
55  
56 <                return 0;
56 >                        ios::ReadToWrite(in, api::Cout);
57 >                }
58 >                catch (const api::Error&)
59 >                {
60 >                        return 1;
61 >                }
62 >                break;
63 >        case fork:
64 >                if (api::Posix::CheckError(::fork()))
65 >                        return 0;
66 >        default:
67 >                {
68 >                        Spectre2 spectre(config);
69 >                }
70          }
71 < } spectre;
71 >
72 >        return 0;
73 > }
74  
75   extern "C"
76   {
# Line 56 | Line 78 | extern "C"
78          void stop(int signal, ::siginfo_t* info, void* uap);
79   }
80  
81 < Spectre2::Spectre2(const ext::String& config, const ext::String& pid) : pid(pid)
81 > Spectre2::Spectre2(const api::Path& config)
82   {
83          {
84 +                _H<xml::Document> document(xml::Parse(config.GetPath()));
85 +                _H<xml::Node> spectre(*document/"spectre");
86 +
87 +                pid = *spectre/"pid";
88 +        }
89 +
90 +        {
91                  api::FileWriter out(pid);
92                  ios::FormatWriter fout(out);
93  
94                  fout << ::getpid() << ios::NewLine;
95          }
96  
97 <        Mounter mounter(config);
69 <        Unmounter unmounter(config);
97 >        Daemon daemon_(config);
98  
99 <        daemons.InsertLast(dynamic_cast<Daemon*>(&mounter));
72 <        daemons.InsertLast(dynamic_cast<Daemon*>(&unmounter));
99 >        daemon = &daemon_;
100  
101          {
102                  struct ::sigaction action;
# Line 78 | Line 105 | Spectre2::Spectre2(const ext::String& co
105                  action.sa_flags = SA_SIGINFO;
106  
107                  api::Posix::CheckError(::sigemptyset(&action.sa_mask));
108 <                api::Posix::CheckError(::sigaction(SIGHUP, &action, NULL));
108 >                api::Posix::CheckError(::sigaction(SIGUSR1, &action, NULL));
109  
110                  action.sa_sigaction = stop;
111  
112 +                api::Posix::CheckError(::sigaction(SIGINT, &action, NULL));
113                  api::Posix::CheckError(::sigaction(SIGTERM, &action, NULL));
114          }
87
88        _mforeach (ext::Vector<Daemon*>, daemon, daemons) (*daemon)->start();
115   }
116  
117   Spectre2::~Spectre2()
# Line 93 | Line 119 | Spectre2::~Spectre2()
119          api::Posix::CheckError(::unlink(pid.NullTerminate()));
120   }
121  
122 < ext::String Spectre2::program(api::GetExecutablePath().GetName());
122 > ext::String Spectre2::program(api::GetExecutablePath().GetName()), Spectre2::prefix(_Spectre2_prefix_), Spectre2::root(_Spectre2_root_), Spectre2::mount(_Spectre2_mount_);
123   bool Spectre2::debug(false);
124 < ext::Vector<Daemon*> Spectre2::daemons;
124 > Daemon* Spectre2::daemon;
125  
126   extern "C"
127   {
128          void reload(int signal, ::siginfo_t* info, void* uap)
129          {
130 <                _mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->reload();
130 >                if (Spectre2::debug)
131 >                        api::Cout << "Reload" << ios::NewLine;
132 >
133 >                Spectre2::daemon->reload();
134          }
135  
136          void stop(int signal, ::siginfo_t* info, void* uap)
137          {
138 <                _mforeach (ext::Vector<Daemon*>, daemon, Spectre2::daemons) (*daemon)->stop();
138 >                if (Spectre2::debug)
139 >                        api::Cout << "Stop" << ios::NewLine;
140 >
141 >                Spectre2::daemon->stop();
142          }
143   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines