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

Comparing:
Spectre2/Spectre2Control.cpp (file contents), Revision 416 by douglas, 2005-01-15T18:32:41-08:00 vs.
Spectre2/Spectre2Control (file contents), Revision 417 by douglas, 2005-03-02T14:49:53-08:00

# Line 1 | Line 1
1 < // Spectre 2
2 < //
3 < // Douglas Thrift
4 < //
5 < // $Id$
6 <
7 < #include <menes/platform.hpp>
8 <
9 < #include <menes-api/console.hpp>
10 < #include <menes-api/exename.hpp>
11 < #include <menes-api/files.hpp>
12 < #include <menes-app/application.hpp>
13 < #include <menes-ext/string.hpp>
14 < #include <menes-ios/helpers.hpp>
15 <
16 < #include <sys/resource.h>
17 < #include <sys/time.h>
18 < #include <sys/types.h>
19 < #include <sys/wait.h>
20 < #include <signal.h>
21 < #include <unistd.h>
1 > #!/usr/bin/env bash
2 > # Spectre 2
3 > #
4 > # Douglas Thrift
5 > #
6 > # $Id$
7 >
8 > pid="spectre.pid"
9 >
10 > function usage()
11 > {
12 >        echo "Usage: `basename $0` start|stop|restart|reload|status [...]"
13 > }
14 >
15 > #inline pid_t process()
16 > #{
17 > #       api::FileReader fin(pid);
18 > #
19 > #       return lexical_cast<pid_t>(ios::ReadLine(fin));
20 > #}
21 >
22 > #struct Spectre2Control : public app::Application
23 > #{
24 > #       virtual int Run(const app::ArgumentList& args)
25 > #       {
26 > #               enum Command { Start, Stop, Restart, Reload, Status } command;
27 > #
28 > #               if (!args.IsEmpty())
29 > #               {
30 > #                       const ext::String& arg(args.First());
31 > #
32 > #                       if (arg == "start") command = Start;
33 > #                       else if (arg == "stop") command = Stop;
34 > #                       else if (arg == "restart") command = Restart;
35 > #                       else if (arg == "reload") command = Reload;
36 > #                       else if (arg == "status") command = Status;
37 > #                       else return usage(1);
38 > #
39 > #                       _foreach (app::ArgumentList, arg, args)
40 > #                       {
41 > #                               Matcher matcher("^-pid=(.*)$");
42 > #
43 > #                               if (*arg == matcher) pid = matcher[1];
44 > #                       }
45 > #               }
46 > #               else return usage();
47 > #
48 > #               switch (command)
49 > #               {
50 > #               case Start:
51 > #                       start(args);
52 > #                       break;
53 > #               case Stop:
54 > #                       stop();
55 > #                       break;
56 > #               case Restart:
57 > #                       restart(args);
58 > #                       break;
59 > #               case Reload:
60 > #                       reload();
61 > #                       break;
62 > #               case Status:
63 > #                       status();
64 > #               }
65 > #
66 > #               return 0;
67 > #       }
68 > #} spectreControl;
69 >
70 > #void start(const app::ArgumentList& args)
71 > #{
72 > #       api::Cout << "Starting Spectre daemon ... " << ios::Flush;
73 > #
74 > #       if (pid_t process = api::Posix::CheckError(::fork()))
75 > #       {
76 > #               api::Cout << "started." << ios::NewLine;
77 > #       }
78 > #       else
79 > #       {
80 > #               _L<const char*> argv;
81 > #
82 > #               argv.Reserve(args.GetSize());
83 > #
84 > #               _foreach (app::ArgumentList, arg, args) argv.InsertLast(arg->NullTerminate());
85 > #
86 > #               ext::String command(api::GetExecutablePath().GetParent() + "Spectre2");
87 > #
88 > #               argv.First() = command.NullTerminate();
89 > #
90 > #               api::Posix::CheckError(::execv(command.NullTerminate(), const_cast<char**>(argv.NullTerminate())));
91 > #       }
92 > #}
93 >
94 > #void stop()
95 > #{
96 > #       api::Cout << "Stopping Spectre daemon ... " << ios::Flush;
97 > #
98 > #       try
99 > #       {
100 > #               pid_t id(process());
101 > #
102 > #               api::Posix::CheckError(::kill(id, SIGTERM));
103 > #
104 > #               // XXX: somehow wait for it to terminate
105 > #
106 > #               api::Cout << "stopped." << ios::NewLine;
107 > #       }
108 > #       catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; }
109 > #}
110 >
111 > #void reload()
112 > #{
113 > #       api::Cout << "Reloading Spectre daemon ... " << ios::Flush;
114 > #
115 > #       try
116 > #       {
117 > #               api::Posix::CheckError(::kill(process(), SIGUSR1));
118 > #
119 > #               api::Cout << "reloaded." << ios::NewLine;
120 > #       }
121 > #       catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; }
122 > #}
123 >
124 > #void status()
125 > #{
126 > #}
127  
128 < #include "Matcher/Matcher.hpp"
24 <
25 < // XXX: Menes' linker is linking too much
26 < namespace Spectre2
27 < {
28 <        bool debug(false);
29 < }
30 <
31 < ext::String pid("spectre.pid");
32 <
33 < inline int usage(int code = 0)
34 < {
35 <        api::Cerr << "Usage: " << api::GetExecutablePath().GetName() << " start|stop|restart|reload|status [...]" << ios::NewLine;
36 <
37 <        return code;
38 < }
39 <
40 < inline pid_t process()
41 < {
42 <        api::FileReader fin(pid);
43 <
44 <        return lexical_cast<pid_t>(ios::ReadLine(fin));
45 < }
46 <
47 < void start(const app::ArgumentList& args);
48 < void stop();
49 < inline void restart(const app::ArgumentList& args) { stop(); start(args); }
50 < void reload();
51 < void status();
52 <
53 < struct Spectre2Control : public app::Application
54 < {
55 <        virtual int Run(const app::ArgumentList& args)
56 <        {
57 <                enum Command { Start, Stop, Restart, Reload, Status } command;
58 <
59 <                if (!args.IsEmpty())
60 <                {
61 <                        const ext::String& arg(args.First());
62 <
63 <                        if (arg == "start") command = Start;
64 <                        else if (arg == "stop") command = Stop;
65 <                        else if (arg == "restart") command = Restart;
66 <                        else if (arg == "reload") command = Reload;
67 <                        else if (arg == "status") command = Status;
68 <                        else return usage(1);
69 <
70 <                        _foreach (app::ArgumentList, arg, args)
71 <                        {
72 <                                Matcher matcher("^-pid=(.*)$");
73 <
74 <                                if (*arg == matcher) pid = matcher[1];
75 <                        }
76 <                }
77 <                else return usage();
78 <
79 <                switch (command)
80 <                {
81 <                case Start:
82 <                        start(args);
83 <                        break;
84 <                case Stop:
85 <                        stop();
86 <                        break;
87 <                case Restart:
88 <                        restart(args);
89 <                        break;
90 <                case Reload:
91 <                        reload();
92 <                        break;
93 <                case Status:
94 <                        status();
95 <                }
96 <
97 <                return 0;
98 <        }
99 < } spectreControl;
100 <
101 < void start(const app::ArgumentList& args)
102 < {
103 <        api::Cout << "Starting Spectre daemon ... " << ios::Flush;
104 <
105 <        if (pid_t process = api::Posix::CheckError(::fork()))
106 <        {
107 <                api::Cout << "started." << ios::NewLine;
108 <        }
109 <        else
110 <        {
111 <                _L<const char*> argv;
112 <
113 <                argv.Reserve(args.GetSize());
114 <
115 <                _foreach (app::ArgumentList, arg, args) argv.InsertLast(arg->NullTerminate());
116 <
117 <                ext::String command(api::GetExecutablePath().GetParent() + "Spectre2");
118 <
119 <                argv.First() = command.NullTerminate();
120 <
121 <                api::Posix::CheckError(::execv(command.NullTerminate(), const_cast<char**>(argv.NullTerminate())));
122 <        }
123 < }
124 <
125 < void stop()
126 < {
127 <        api::Cout << "Stopping Spectre daemon ... " << ios::Flush;
128 <
129 <        try
130 <        {
131 <                pid_t id(process());
132 <
133 <                api::Posix::CheckError(::kill(id, SIGTERM));
134 <
135 <                // XXX: somehow wait for it to terminate
136 <
137 <                api::Cout << "stopped." << ios::NewLine;
138 <        }
139 <        catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; }
140 < }
141 <
142 < void reload()
143 < {
144 <        api::Cout << "Reloading Spectre daemon ... " << ios::Flush;
145 <
146 <        try
147 <        {
148 <                api::Posix::CheckError(::kill(process(), SIGUSR1));
149 <
150 <                api::Cout << "reloaded." << ios::NewLine;
151 <        }
152 <        catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; }
153 < }
154 <
155 < void status()
156 < {
157 < }
128 > usage

Comparing:
Spectre2/Spectre2Control.cpp (property svn:executable), Revision 416 by douglas, 2005-01-15T18:32:41-08:00 vs.
Spectre2/Spectre2Control (property svn:executable), Revision 417 by douglas, 2005-03-02T14:49:53-08:00

# Line 0 | Line 1
1 + *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines