shmmonitor: non-interactive mode checks and quits

This commit is contained in:
Alexey Rybalchenko 2021-02-09 23:28:39 +01:00
parent c6b13cd3a1
commit 2b3e38d9a4
2 changed files with 25 additions and 29 deletions

View File

@ -45,6 +45,27 @@ namespace
namespace fair::mq::shmem namespace fair::mq::shmem
{ {
struct TerminalConfig
{
TerminalConfig()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag &= ~ICANON; // disable canonical input
t.c_lflag &= ~ECHO; // do not echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}
~TerminalConfig()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag |= ICANON; // re-enable canonical input
t.c_lflag |= ECHO; // echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}
};
void signalHandler(int signal) void signalHandler(int signal)
{ {
gSignalStatus = signal; gSignalStatus = signal;
@ -116,11 +137,8 @@ void Monitor::Run()
if (fInteractive) { if (fInteractive) {
Interactive(); Interactive();
} else { } else {
while (!fTerminating) {
this_thread::sleep_for(chrono::milliseconds(fIntervalInMS));
CheckSegment(); CheckSegment();
} }
}
if (!fViewOnly) { if (!fViewOnly) {
heartbeatThread.join(); heartbeatThread.join();
@ -154,27 +172,6 @@ void Monitor::MonitorHeartbeats()
RemoveQueue(fControlQueueName); RemoveQueue(fControlQueueName);
} }
struct TerminalConfig
{
TerminalConfig()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag &= ~ICANON; // disable canonical input
t.c_lflag &= ~ECHO; // do not echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}
~TerminalConfig()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag |= ICANON; // re-enable canonical input
t.c_lflag |= ECHO; // echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}
};
void Monitor::Interactive() void Monitor::Interactive()
{ {
char c; char c;

View File

@ -125,13 +125,12 @@ int main(int argc, char** argv)
} }
cout << "Starting shared memory monitor for session: \"" << sessionName << "\" (shmId: " << shmId << ")..." << endl; cout << "Starting shared memory monitor for session: \"" << sessionName << "\" (shmId: " << shmId << ")..." << endl;
if (viewOnly && !interactive) {
cout << "running in non-interactive view-only mode, outputting with interval of " << intervalInMS << "ms. (change with --interval), press ctrl+C to exit." << endl;
}
Monitor monitor(shmId, selfDestruct, interactive, viewOnly, timeoutInMS, intervalInMS, runAsDaemon, cleanOnExit); Monitor monitor(shmId, selfDestruct, interactive, viewOnly, timeoutInMS, intervalInMS, runAsDaemon, cleanOnExit);
if (interactive || !viewOnly) {
monitor.CatchSignals(); monitor.CatchSignals();
}
monitor.Run(); monitor.Run();
} catch (Monitor::DaemonPresent& dp) { } catch (Monitor::DaemonPresent& dp) {
return 0; return 0;