Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Sync with 2017.10.27-1
Browse files Browse the repository at this point in the history
 * fixed timeout return value for svwait
 * Enhanced svscan.
  • Loading branch information
Morgan-Stanley authored and keithhendry committed Oct 31, 2017
1 parent 7ef9195 commit bdfac06
Show file tree
Hide file tree
Showing 51 changed files with 799 additions and 209 deletions.
37 changes: 22 additions & 15 deletions bin/winss-svscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include "winss/winss.hpp"
#include "optionparser/optionparser.hpp"
#include "easylogging/easylogging++.hpp"
#include "winss/ctrl_handler.hpp"
#include "winss/filesystem_interface.hpp"
#include "winss/not_owning_ptr.hpp"
#include "winss/wait_multiplexer.hpp"
#include "winss/svscan/svscan.hpp"
#include "winss/svscan/controller.hpp"
#include "winss/pipe_server.hpp"
#include "winss/pipe_name.hpp"
#include "winss/ctrl_handler.hpp"
#include "resource/resource.h"

INITIALIZE_EASYLOGGINGPP
Expand All @@ -37,6 +37,7 @@ namespace fs = std::experimental::filesystem;
struct Settings {
fs::path scan_dir;
DWORD rescan = INFINITE;
bool signals = false;
int verbose_level = 0;
};

Expand All @@ -56,7 +57,7 @@ struct Arg : public option::Arg {
}
};

enum OptionIndex { UNKNOWN, HELP, VERSION, VERBOSE, TIMEOUT };
enum OptionIndex { UNKNOWN, HELP, VERSION, VERBOSE, TIMEOUT, SIGNALS };
const option::Descriptor usage[] = {
{
UNKNOWN, 0, "", "", Arg::None,
Expand All @@ -68,7 +69,7 @@ const option::Descriptor usage[] = {
" --help \tPrint usage and exit."
},
{
VERSION, 0, "", "version", option::Arg::None,
VERSION, 0, "", "version", Arg::None,
" --version \tPrint the current version of winss and exit."
},
{
Expand All @@ -79,6 +80,10 @@ const option::Descriptor usage[] = {
TIMEOUT, 0, "t", "timeout", Arg::Required,
" -t<rescan>, \t--timeout=<rescan> \tSets the rescan timeout."
},
{
SIGNALS, 0, "s", "signals", Arg::None,
" -s, \t--signals \tDivert signals."
},
{ 0, 0, 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -128,16 +133,6 @@ Settings ParseArgs(int argc, char* argv[]) {
option::Option& opt = buffer[i];

switch (opt.index()) {
case TIMEOUT:
try {
settings.rescan = std::strtoul(opt.arg, nullptr, 10);
} catch (const std::exception&) {
std::cerr
<< "Option "
<< opt.name
<< " requires a numeric argument";
}
break;
case VERBOSE:
if (opt.arg == nullptr) {
settings.verbose_level = el::base::consts::kMaxVerboseLevel;
Expand All @@ -152,6 +147,19 @@ Settings ParseArgs(int argc, char* argv[]) {
}
}
break;
case TIMEOUT:
try {
settings.rescan = std::strtoul(opt.arg, nullptr, 10);
} catch (const std::exception&) {
std::cerr
<< "Option "
<< opt.name
<< " requires a numeric argument";
}
break;
case SIGNALS:
settings.signals = true;
break;
}
}

Expand All @@ -177,7 +185,6 @@ int main(int argc, char* argv[]) {
ConfigureLogger(settings);

winss::WaitMultiplexer multiplexer;
multiplexer.AddCloseEvent(winss::GetCloseEvent(), 0);

winss::PipeName pipe_name(settings.scan_dir, winss::SvScan::kMutexName);
winss::InboundPipeServer inbound({
Expand All @@ -186,7 +193,7 @@ int main(int argc, char* argv[]) {
});

winss::SvScan svscan(winss::NotOwned(&multiplexer), settings.scan_dir,
settings.rescan);
settings.rescan, settings.signals, winss::GetCloseEvent());
winss::SvScanController controller(winss::NotOwned(&svscan),
winss::NotOwned(&inbound));
return multiplexer.Start();
Expand Down
4 changes: 3 additions & 1 deletion bin/winss-svwait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ INITIALIZE_EASYLOGGINGPP

namespace fs = std::experimental::filesystem;

static const int kTimeoutExitCode = 99;

struct Settings {
std::vector<fs::path> service_dirs;
winss::SuperviseStateListenerAction wait =
Expand Down Expand Up @@ -233,7 +235,7 @@ int main(int argc, char* argv[]) {
multiplexer.AddCloseEvent(winss::GetCloseEvent(), 0);

winss::Control control(winss::NotOwned(&multiplexer),
settings.timeout, settings.wait_all);
settings.timeout, kTimeoutExitCode, settings.wait_all);

std::vector<WaitItem> wait_items;

Expand Down
40 changes: 31 additions & 9 deletions docs/sphinx/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ single :term:`service`. It is designed to be either the root or a branch of a

.. code-block:: bat
Usage: winss-svscan.exe [options] [scandir]
Options:
--help Print usage and exit.
--version Print the current version of winss and exit.
-v[<level>], --verbose[=<level>]
Sets the verbose level.
-t<rescan>, --timeout=<rescan>
Sets the rescan timeout.
Usage: winss-svscan-g.exe [options] [scandir]
Options:
--help Print usage and exit.
--version Print the current version of winss and exit.
-v[<level>], --verbose[=<level>]
Sets the verbose level.
-t<rescan>, --timeout=<rescan>
Sets the rescan timeout.
-s, --signals
Divert signals.
- If given a ``scandir`` is specified then that is used. Otherwise then the
current directory is used.
Expand All @@ -119,6 +121,26 @@ single :term:`service`. It is designed to be either the root or a branch of a
Options
^^^^^^^

-s\, --signals
By default, :ref:`winss-svscan` will handle any termination signals that
it receives and attempt to propagate these and close. Using divert signals
it will instead launch the process defined in **.winss-svscan/SIGTERM**.

:ref:`winss-svscan` will not exit its loop on its own when it receives a
termination signal and the -s option has been given. To make it exit its
loop, invoke a :ref:`winss-svscanctl` command from the signal handling
process. For instance, a **.winss-svscan/SIGTERM** file could point to a
Powershell script like the following:

.. code-block:: bat
# cleanup here
& winss-svscanctl.exe -q .
If an action cannot be taken (the relevant file doesn't exist, or cannot
run, or any kind of error happens), :ref:`winss-svscan` prints a warning
message but does nothing else with the signal.

-t<rescan>\, --timeout=<rescan>
Perform a scan every ``rescan`` milliseconds. If rescan is **0**
(the default), automatic scans are never performed after the first one and
Expand Down
12 changes: 7 additions & 5 deletions lib/winss/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ const char winss::Control::kTimeoutGroup[] = "control";

winss::Control::Control(
winss::NotOwningPtr<winss::WaitMultiplexer> multiplexer,
DWORD timeout, bool finish_all) : multiplexer(multiplexer),
timeout(timeout), finish_all(finish_all) {
DWORD timeout, int timeout_exit_code, bool finish_all) :
multiplexer(multiplexer), timeout(timeout),
timeout_exit_code(timeout_exit_code), finish_all(finish_all) {
if (timeout != INFINITE) {
multiplexer->AddInitCallback([timeout](winss::WaitMultiplexer& m) {
m.AddTimeoutCallback(timeout, [](
multiplexer->AddInitCallback([timeout, timeout_exit_code](
winss::WaitMultiplexer& m) {
m.AddTimeoutCallback(timeout, [timeout_exit_code](
winss::WaitMultiplexer& m) {
VLOG(3) << "Control timeout!";
m.Stop(winss::Control::kTimeoutExitCode);
m.Stop(timeout_exit_code);
}, kTimeoutGroup);
});

Expand Down
12 changes: 8 additions & 4 deletions lib/winss/control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Control {
std::set<std::string> ready; /**< Items that are ready. */
bool started = false; /**< Orchestration has started. */
const DWORD timeout; /**< Timeout for orchestration. */
const int timeout_exit_code; /**< Exit code for timeout. */
const bool finish_all; /**< If all control items must finish. */

public:
Expand All @@ -95,10 +96,13 @@ class Control {
*
* \param multiplexer The shared multiplexer.
* \param timeout Orchestration timeout.
* \param timeout_exit_code exit code for timeout.
* \param finish_all If all control items must finish.
* \
*/
explicit Control(winss::NotOwningPtr<winss::WaitMultiplexer> multiplexer,
DWORD timeout = INFINITE, bool finish_all = true);
DWORD timeout = INFINITE, int timeout_exit_code = 1,
bool finish_all = true);
Control(const Control&) = delete; /**< No copy. */
Control(Control&&) = delete; /**< No move. */

Expand Down Expand Up @@ -135,7 +139,7 @@ class Control {
*/
int Start();

void operator=(const Control&) = delete; /**< No copy. */
Control& operator=(const Control&) = delete; /**< No copy. */
Control& operator=(Control&&) = delete; /**< No move. */
};

Expand Down Expand Up @@ -202,7 +206,7 @@ class OutboundControlItem :
bool Disconnected();

/** No copy. */
void operator=(const OutboundControlItem&) = delete;
OutboundControlItem& operator=(const OutboundControlItem&) = delete;
/** No move. */
OutboundControlItem& operator=(OutboundControlItem&&) = delete;
};
Expand Down Expand Up @@ -307,7 +311,7 @@ class InboundControlItem :
bool Disconnected();

/** No copy. */
void operator=(const InboundControlItem&) = delete;
InboundControlItem& operator=(const InboundControlItem&) = delete;
/** No move. */
InboundControlItem& operator=(InboundControlItem&&) = delete;
};
Expand Down
17 changes: 8 additions & 9 deletions lib/winss/event_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
#include "windows_interface.hpp"
#include "handle_wrapper.hpp"

winss::EventWrapper::EventWrapper() {
handle = WINDOWS.CreateEvent(nullptr, true, false, nullptr);
}
winss::EventWrapper::EventWrapper() : handle(winss::TrustedHandleWrapper(
WINDOWS.CreateEvent(nullptr, true, false, nullptr), SYNCHRONIZE)) {}

bool winss::EventWrapper::IsSet() const {
return WINDOWS.WaitForSingleObject(handle, 0) != WAIT_TIMEOUT;
return WINDOWS.WaitForSingleObject(handle.GetHandle(), 0) != WAIT_TIMEOUT;
}

bool winss::EventWrapper::Set() {
return WINDOWS.SetEvent(handle);
return WINDOWS.SetEvent(handle.GetHandle());
}

winss::HandleWrapper winss::EventWrapper::GetHandle() const {
return winss::HandleWrapper(handle, false, SYNCHRONIZE);
bool winss::EventWrapper::Reset() {
return WINDOWS.ResetEvent(handle.GetHandle());
}

winss::EventWrapper::~EventWrapper() {
WINDOWS.CloseHandle(handle);
winss::HandleWrapper winss::EventWrapper::GetHandle() const {
return handle.GetHandleWrapper();
}
55 changes: 45 additions & 10 deletions lib/winss/event_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@ namespace winss {
*/
class EventWrapper {
private:
HANDLE handle; /**< The handle to the event. */
winss::TrustedHandleWrapper handle; /**< The handle to the event. */

public:
/**
* Creates the event.
* Creates the event wrapper.
*/
EventWrapper();
EventWrapper(const EventWrapper&) = delete; /**< No copy. */
EventWrapper(EventWrapper&&) = delete; /**< No move. */

/**
* Copies the event wrapper.
*
* Any ownership rights will not be copied.
*
* \param e The event wrapper to copy.
*/
EventWrapper(const EventWrapper& e) = default;

/**
* Move the event wrapper.
*
* Any ownership rights will be moved
*
* \param e The event wrapper to move.
*/
EventWrapper(EventWrapper&& e) = default;

/**
* Gets if the event is set or not.
Expand All @@ -58,13 +74,32 @@ class EventWrapper {
*/
bool Set();

void operator=(const EventWrapper&) = delete; /**< No copy. */
EventWrapper& operator=(EventWrapper&&) = delete; /**< No move. */
/**
* Resets the event.
*
* \return True if the reset event succeeded.
*/
bool Reset();

/**
* Clean up the event.
*/
~EventWrapper();
/**
* Copies the event wrapper.
*
* Any ownership rights will not be copied.
*
* \param e The event wrapper to copy.
* \return This event wrapper.
*/
EventWrapper& operator=(const EventWrapper& e) = default;

/**
* Move the event wrapper.
*
* Any ownership rights will be moved
*
* \param e The event wrapper to move.
* \return This event wrapper.
*/
EventWrapper& operator=(EventWrapper&& e) = default;
};
} // namespace winss

Expand Down
2 changes: 1 addition & 1 deletion lib/winss/filesystem_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class FilesystemInterface {
static const FilesystemInterface& GetInstance();

/** No copy. */
void operator=(const FilesystemInterface&) = delete;
FilesystemInterface& operator=(const FilesystemInterface&) = delete;
/** No move. */
FilesystemInterface& operator=(FilesystemInterface&&) = delete;
};
Expand Down
Loading

0 comments on commit bdfac06

Please sign in to comment.