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 415 by douglas, 2005-01-15T18:26:13-08:00 vs.
Spectre2/Spectre2Control (file contents), Revision 428 by douglas, 2005-03-24T17:38:00-08:00

# Line 1 | Line 1
1 < // Spectre 2
2 < //
3 < // Douglas Thrift
4 < //
5 < // $Id$
1 > #!/usr/bin/env bash
2 > # Spectre 2
3 > #
4 > # Douglas Thrift
5 > #
6 > # $Id$
7  
8 < #include <menes/platform.hpp>
8 > command="$1"
9  
10 < #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>
10 > shift
11  
12 < #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>
22 <
23 < #include "Matcher/Matcher.hpp"
24 <
25 < // XXX: Menes' linker is linking too much
26 < namespace Spectre2
12 > function check()
13   {
14 <        bool debug(false);
14 >        pid="$($(dirname $0)/Spectre2 -pid $@)"
15 >        if [ "$pid" = "" ]; then
16 >                echo "not running."
17 >                exit
18 >        fi
19   }
20  
21 < ext::String pid("spectre.pid");
32 <
33 < inline int usage(int code = 0)
21 > function usage()
22   {
23 <        api::Cerr << "Usage: " << api::GetExecutablePath().GetName() << " start|stop|restart|reload|status [...]" << ios::NewLine;
36 <
37 <        return code;
23 >        echo "Usage: $(basename $0) start|stop|restart|reload|status [...]"
24   }
25  
26 < inline pid_t process()
26 > function start_()
27   {
28 <        api::FileReader fin(pid);
29 <
30 <        return lexical_cast<pid_t>(ios::ReadLine(fin));
28 >        echo -n "Starting Spectre daemon ... "
29 >        if [ "$($(dirname $0)/Spectre2 -pid $@)" != "" ]; then
30 >                echo "already running."
31 >                exit
32 >        fi
33 >        $(dirname $0)/Spectre2 -fork $@
34 >        echo "started."
35   }
36  
37 < 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)
37 > function stop_()
38   {
39 <        api::Cout << "Starting Spectre daemon ... " << ios::Flush;
40 <
41 <        if (pid_t process = api::Posix::CheckError(::fork()))
42 <        {
43 <                api::Cout << "started." << ios::NewLine;
44 <        }
45 <        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 <        }
39 >        echo -n "Stopping Spectre daemon ... "
40 >        check $@
41 >        kill -TERM $pid
42 >        until [ "$($(dirname $0)/Spectre2 -pid $@)" == "" ]; do
43 >                sleep 0;
44 >        done
45 >        echo "stopped."
46   }
47  
48 < void stop()
48 > function restart_()
49   {
50 <        api::Cout << "Stopping Spectre daemon ... " << ios::Flush;
51 <
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; }
50 >        stop_ $@
51 >        start_ $@
52   }
53  
54 < void reload()
54 > function reload_()
55   {
56 <        api::Cout << "Reloading Spectre daemon ... " << ios::Flush;
57 <
58 <        try
59 <        {
148 <                api::Posix::CheckError(::kill(id, SIGUSR1));
149 <
150 <                api::Cout << "reloaded." << ios::NewLine;
151 <        }
152 <        catch (ext::Exception) { api::Cout << "failed." << ios::NewLine; }
56 >        echo -n "Reloading Spectre daemon ... "
57 >        check $@
58 >        kill -USR1 $pid
59 >        echo "reloaded."
60   }
61  
62 < void status()
62 > function status_()
63   {
64 +        echo -n "Checking Spectre daemon ... "
65 +        check $@
66 +        echo "running with PID $pid."
67   }
68 +
69 + case $command in
70 + start)
71 +        start_ $@
72 +        ;;
73 + stop)
74 +        stop_ $@
75 +        ;;
76 + restart)
77 +        restart_ $@
78 +        ;;
79 + reload)
80 +        reload_ $@
81 +        ;;
82 + status)
83 +        status_ $@
84 +        ;;
85 + *)
86 +        usage
87 +        ;;
88 + esac

Comparing:
Spectre2/Spectre2Control.cpp (property svn:executable), Revision 415 by douglas, 2005-01-15T18:26:13-08:00 vs.
Spectre2/Spectre2Control (property svn:executable), Revision 428 by douglas, 2005-03-24T17:38:00-08:00

# Line 0 | Line 1
1 + *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines