Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wait until actually stopped to tell windows we are #2040

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fcc4f60
wait until actually stopped to tell windows we are
tewinget Oct 27, 2022
a9a2a11
debian missing yacc apparently all of a sudden
tewinget Oct 27, 2022
9cdfae2
correct windows service manager behavior.
majestrate Oct 27, 2022
7ddad87
some useful log statements
tewinget Oct 27, 2022
a7f3c35
fix comment to reflect reality
majestrate Oct 27, 2022
a5b5fba
system layer manager
majestrate Oct 27, 2022
cbb57fb
fix ifdef guards
majestrate Oct 27, 2022
35fe9b3
turn off service manager in unit tests
majestrate Oct 27, 2022
f4974a8
Remove dead/redundant code
jagerman Oct 27, 2022
f0c2a9c
Fix crashes in wintun and windivert stopping
jagerman Oct 27, 2022
100ff1d
Add some more debugging
jagerman Oct 28, 2022
3f23118
🤷 Work around ub_ctx_delete crash
jagerman Oct 28, 2022
c7aeb37
Update gui to latest
jagerman Oct 28, 2022
7bc722c
Add networkReady to stats result
jagerman Oct 28, 2022
b993ee5
Extend windows startup timeout
jagerman Oct 28, 2022
0b2a478
fixups
majestrate Oct 28, 2022
20ccd57
format
majestrate Oct 28, 2022
32ef862
clean up service manager for systemd
majestrate Oct 28, 2022
fbea8be
Update llarp/router/router.cpp
majestrate Oct 28, 2022
cfffd89
simplify logic for disabling service manager on windows
majestrate Oct 28, 2022
5776269
fix typo in filename
majestrate Oct 28, 2022
8a51791
Move log init even earlier
jagerman Oct 29, 2022
d62e880
Remove unused VERSION_STR constant
jagerman Oct 29, 2022
f5e4af9
Improve windows running-as-a-service detection works
jagerman Oct 29, 2022
73b5711
remove dead dns resolver code
tewinget Oct 31, 2022
82e85bd
fix crash on shutdown
majestrate Oct 31, 2022
d128182
stop-time debug statements
tewinget Oct 31, 2022
4612a45
Revert "🤷 Work around ub_ctx_delete crash"
jagerman Oct 31, 2022
febfa73
Don't raise log level on shutdown
jagerman Oct 31, 2022
465d4f8
Remove bad assert
jagerman Oct 31, 2022
953da5d
Add more stopping signals
jagerman Oct 31, 2022
916c8c4
windivert: avoid trying to send during shutdown
jagerman Oct 31, 2022
de6f9d4
rewordme
majestrate Oct 31, 2022
98fe769
Merge remote-tracking branch 'tom-ssh/windows-service-issues' into wi…
majestrate Oct 31, 2022
df416ea
format pedant
majestrate Oct 31, 2022
7988724
dont clear m_pending on shutdown that is taken care of that for us on…
majestrate Oct 31, 2022
3548b75
i hate this, a query's packetsource_base needed to be a shared_ptr be…
majestrate Oct 31, 2022
a72e94c
fix previous commit
majestrate Oct 31, 2022
5fa87c0
fix typo remove comment
majestrate Oct 31, 2022
7f9843f
remove pending query after sending a reply of any kind
majestrate Oct 31, 2022
cf25c0b
More debug
jagerman Oct 31, 2022
a390f2a
use weak_from_this()
majestrate Oct 31, 2022
35eb542
format
majestrate Oct 31, 2022
387e1d7
Fix crashy race condition in shutdown
jagerman Oct 31, 2022
eb78f96
Fix crash on unbound cleanup
jagerman Nov 1, 2022
3fc097c
patch unbound to fix windows shutdown crash
jagerman Nov 1, 2022
5868828
RPC: fix "halt" command
jagerman Nov 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions daemon/lokinet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,7 @@ SvcCtrlHandler(DWORD dwCtrl)

case SERVICE_CONTROL_INTERROGATE:
llarp::log::debug(logcat, "Got win32 service interrogate signal");
// report status
llarp::sys::service_manager->report_our_state();
llarp::sys::service_manager->report_changed_state();
return;

default:
Expand Down
10 changes: 8 additions & 2 deletions llarp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ target_link_libraries(lokinet-platform PUBLIC lokinet-cryptography lokinet-util
target_link_libraries(lokinet-platform PRIVATE oxenmq::oxenmq)

if (ANDROID)
target_sources(lokinet-platform PRIVATE android/ifaddrs.c android/service_manager.cpp)
target_sources(lokinet-platform PRIVATE android/ifaddrs.c util/nop_service_manager.cpp)
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
target_sources(lokinet-platform PRIVATE linux/dbus.cpp linux/service_manager.cpp)
target_sources(lokinet-platform PRIVATE linux/dbus.cpp)
if(WITH_SYSTEMD)
target_sources(lokinet-platform PRIVATE linux/sd_service_manager.cpp)
else()
target_sources(lokinet-platform PRIVATE util/nop_service_manager.cpp)
endif()
endif()

if (WIN32)
Expand Down Expand Up @@ -313,6 +318,7 @@ endif()

if(APPLE)
add_subdirectory(apple)
target_sources(lokinet-platform PRIVATE util/nop_system_manager.cpp)
endif()

file(GLOB_RECURSE docs_SRC */*.hpp *.hpp)
Expand Down
8 changes: 0 additions & 8 deletions llarp/android/service_manager.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion llarp/apple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ find_library(COREFOUNDATION CoreFoundation REQUIRED)

target_link_libraries(lokinet-util PUBLIC ${FOUNDATION})

target_sources(lokinet-platform PRIVATE vpn_platform.cpp vpn_interface.cpp route_manager.cpp context_wrapper.cpp service_manager.cpp)
target_sources(lokinet-platform PRIVATE vpn_platform.cpp vpn_interface.cpp route_manager.cpp context_wrapper.cpp)

add_executable(lokinet-extension MACOSX_BUNDLE
PacketTunnelProvider.m
Expand Down
60 changes: 60 additions & 0 deletions llarp/linux/sd_service_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <llarp/util/service_manager.hpp>

#include <systemd/sd-daemon.h>
#include <cassert>
#include <llarp.hpp>
#include <llarp/router/router.hpp>
#include <llarp/util/logging.hpp>

namespace llarp::sys
{
class SD_Manager : public I_SystemLayerManager
{
llarp::sys::ServiceState m_State{ServiceState::Initial};

public:
/// change our state and report it to the system layer
void
we_changed_our_state(ServiceState st) override
{
assert(m_State != st);
m_State = st;
report_changed_state();
}

void
report_changed_state() override
{
if (m_State == ServiceState::Running)
{
::sd_notify(0, "READY=1");
return;
}
if (m_State == ServiceState::Stopping)
{
::sd_notify(0, "STOPPING=1");
return;
}
}

void
report_periodic_stats() override
{
if (m_Context and m_Context->router and not m_disable)
{
auto status = m_Context->router->status_line();
::sd_notify(0, status.c_str());
}
}

void
system_changed_our_state(ServiceState) override
{
// not applicable on systemd
}
};

SD_Manager _manager{};
I_SystemLayerManager* const service_manager = &_manager;

} // namespace llarp::sys
18 changes: 4 additions & 14 deletions llarp/linux/service_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#include <llarp/util/service_manager.hpp>
majestrate marked this conversation as resolved.
Show resolved Hide resolved

#if defined(WITH_SYSTEMD)
#include <systemd/sd-daemon.h>
#include <cassert>
#endif

namespace llarp::sys
{
#if defined(WITH_SYSTEMD)
class SD_Manager : public I_SystemLayerManager
{
llarp::sys::ServiceState m_State{ServiceState::Initial};
Expand All @@ -26,12 +23,6 @@ namespace llarp::sys
void
report_our_state() override
{
if (m_State == ServiceState::Starting)
{
// TODO: maybe request more time?
::sd_notify(0, "READY=0");
return;
}
if (m_State == ServiceState::Running)
{
::sd_notify(0, "READY=1");
Expand All @@ -51,11 +42,10 @@ namespace llarp::sys
}
};

SD_Manager _manager{};
#else
NOP_SystemLayerHandler _manager{};
#endif

SD_Manager _manager
{
;
}
I_SystemLayerManager* const service_manager = &_manager;

} // namespace llarp::sys
3 changes: 3 additions & 0 deletions llarp/router/abstractrouter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ namespace llarp
virtual void
GossipRCIfNeeded(const RouterContact rc) = 0;

virtual std::string
status_line() = 0;

/// Templated convenience function to generate a RouterHive event and
/// delegate to non-templated (and overridable) function for handling.
template <class EventType, class... Params>
Expand Down
106 changes: 55 additions & 51 deletions llarp/router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,60 @@ namespace llarp
m_LastStatsReport = now;
}

std::string
Router::status_line()
{
std::string status;
#if defined(WITH_SYSTEMD)
majestrate marked this conversation as resolved.
Show resolved Hide resolved
auto out = std::back_inserter(status);
fmt::format_to(out, "WATCHDOG=1\nSTATUS=v{}", llarp::VERSION_STR);
majestrate marked this conversation as resolved.
Show resolved Hide resolved
if (IsServiceNode())
{
fmt::format_to(
out,
" snode | known/svc/clients: {}/{}/{}",
nodedb()->NumLoaded(),
NumberOfConnectedRouters(),
NumberOfConnectedClients());
fmt::format_to(
out,
" | {} active paths | block {} ",
pathContext().CurrentTransitPaths(),
(m_lokidRpcClient ? m_lokidRpcClient->BlockHeight() : 0));
auto maybe_last = _rcGossiper.LastGossipAt();
fmt::format_to(
out,
" | gossip: (next/last) {} / {}",
short_time_from_now(_rcGossiper.NextGossipAt()),
maybe_last ? short_time_from_now(*maybe_last) : "never");
}
else
{
fmt::format_to(
out,
" client | known/connected: {}/{}",
nodedb()->NumLoaded(),
NumberOfConnectedRouters());

if (auto ep = hiddenServiceContext().GetDefault())
{
fmt::format_to(
out,
" | paths/endpoints {}/{}",
pathContext().CurrentOwnedPaths(),
ep->UniqueEndpoints());

if (auto success_rate = ep->CurrentBuildStats().SuccessRatio(); success_rate < 0.5)
{
fmt::format_to(
out, " [ !!! Low Build Success Rate ({:.1f}%) !!! ]", (100.0 * success_rate));
}
};
}
#endif
return status;
}

void
Router::Tick()
{
Expand All @@ -888,57 +942,7 @@ namespace llarp
Thaw();
}

#if defined(WITH_SYSTEMD)
{
std::string status;
auto out = std::back_inserter(status);
fmt::format_to(out, "WATCHDOG=1\nSTATUS=v{}", llarp::VERSION_STR);
if (IsServiceNode())
{
fmt::format_to(
out,
" snode | known/svc/clients: {}/{}/{}",
nodedb()->NumLoaded(),
NumberOfConnectedRouters(),
NumberOfConnectedClients());
fmt::format_to(
out,
" | {} active paths | block {} ",
pathContext().CurrentTransitPaths(),
(m_lokidRpcClient ? m_lokidRpcClient->BlockHeight() : 0));
auto maybe_last = _rcGossiper.LastGossipAt();
fmt::format_to(
out,
" | gossip: (next/last) {} / {}",
short_time_from_now(_rcGossiper.NextGossipAt()),
maybe_last ? short_time_from_now(*maybe_last) : "never");
}
else
{
fmt::format_to(
out,
" client | known/connected: {}/{}",
nodedb()->NumLoaded(),
NumberOfConnectedRouters());

if (auto ep = hiddenServiceContext().GetDefault())
{
fmt::format_to(
out,
" | paths/endpoints {}/{}",
pathContext().CurrentOwnedPaths(),
ep->UniqueEndpoints());

if (auto success_rate = ep->CurrentBuildStats().SuccessRatio(); success_rate < 0.5)
{
fmt::format_to(
out, " [ !!! Low Build Success Rate ({:.1f}%) !!! ]", (100.0 * success_rate));
}
};
}
::sd_notify(0, status.c_str());
}
#endif
llarp::sys::service_manager->report_periodic_stats();

m_PathBuildLimiter.Decay(now);

Expand Down
3 changes: 3 additions & 0 deletions llarp/router/router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ namespace llarp
RCLookupHandler _rcLookupHandler;
RCGossiper _rcGossiper;

std::string
status_line() override;

using Clock_t = std::chrono::steady_clock;
using TimePoint_t = Clock_t::time_point;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <llarp/util/service_manager.hpp>
#include "service_manager.hpp"

namespace llarp::sys
{
Expand Down
8 changes: 6 additions & 2 deletions llarp/util/service_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ namespace llarp::sys

/// report our current state to the system layer
virtual void
report_our_state() = 0;
report_changed_state() = 0;

/// report our stats on each timer tick
virtual void
report_periodic_stats(){};

void
starting()
Expand Down Expand Up @@ -108,7 +112,7 @@ namespace llarp::sys

public:
void
report_our_state() override{};
report_changed_state() override{};
void system_changed_our_state(ServiceState) override{};
};
} // namespace llarp::sys
9 changes: 5 additions & 4 deletions llarp/win32/service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace llarp::sys
}

void
SVC_Manager::report_our_state()
SVC_Manager::report_changed_state()
{
if (m_disable)
return;
Expand Down Expand Up @@ -82,7 +82,7 @@ namespace llarp::sys
{
_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
_status.dwServiceSpecificExitCode = 2; // TODO: propagate more info ?
report_our_state();
report_changed_state();
}
else if (auto maybe_state = to_win32_state(st))
{
Expand All @@ -104,9 +104,10 @@ namespace llarp::sys
_status.dwCheckPoint++;
else
_status.dwCheckPoint = 0;

report_our_state();
report_changed_state();
}

}

SVC_Manager _manager{};
Expand Down
2 changes: 1 addition & 1 deletion llarp/win32/service_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace llarp::sys
system_changed_our_state(ServiceState st) override;

void
report_our_state() override;
report_changed_state() override;

void
we_changed_our_state(ServiceState st) override;
Expand Down