diff --git a/debian/changelog b/debian/changelog index 181165f..1d5f29c 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -fancon (0.21.0) UNRELEASED; urgency=low +fancon (0.21.2) UNRELEASED; urgency=low * Initial release. Closes: #00000 diff --git a/src/Client.cpp b/src/Client.cpp index 6f0f156..2adc143 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -247,6 +247,13 @@ void fc::Client::print_help(const string &conf) { << "a trace Trace logging level" << endl; } +bool fc::Client::service_running() { + auto creds = grpc::InsecureChannelCredentials(); + auto channel = grpc::CreateChannel(Util::SERVICE_ADDR, creds); + channel->WaitForConnected(Util::deadline(200)); + return channel->GetState(true) == GRPC_CHANNEL_READY; +} + fc::Client::operator bool() const { return bool(client); } bool fc::Client::connected(long timeout_ms) const { diff --git a/src/Client.hpp b/src/Client.hpp index eed58bc..4f11837 100644 --- a/src/Client.hpp +++ b/src/Client.hpp @@ -35,6 +35,7 @@ class Client { bool Sysinfo(const string &p); static void print_help(const string &conf); + static bool service_running(); explicit operator bool() const; diff --git a/src/Service.cpp b/src/Service.cpp index 25d839b..0183d78 100644 --- a/src/Service.cpp +++ b/src/Service.cpp @@ -14,11 +14,6 @@ fc::Service::~Service() { void fc::Service::run() { try { - if (service_running()) { - LOG(llvl::fatal) << "Only 1 instance may be run"; - return; - } - // TODO: use SSL // auto ssl_options = grpc::SslServerCredentialsOptions(); // ... @@ -192,13 +187,6 @@ void fc::Service::daemonize() { LOG(llvl::error) << "Failed to set working directory to '/'"; } -bool fc::Service::service_running() { - auto creds = grpc::InsecureChannelCredentials(); - auto channel = grpc::CreateChannel(Util::SERVICE_ADDR, creds); - channel->WaitForConnected(Util::deadline(200)); - return channel->GetState(true) == GRPC_CHANNEL_READY; -} - // void fc::Service::signal_handler(int signal) { // switch (signal) { // case SIGINT: diff --git a/src/Service.hpp b/src/Service.hpp index 9f18648..040adbe 100644 --- a/src/Service.hpp +++ b/src/Service.hpp @@ -1,7 +1,6 @@ #ifndef FANCON_SERVICE_HPP #define FANCON_SERVICE_HPP -#include "Controller.hpp" #include #include #include @@ -9,6 +8,7 @@ #include #include +#include "Controller.hpp" #include "proto/DevicesSpec.grpc.pb.h" #include "proto/DevicesSpec.pb.h" @@ -72,8 +72,6 @@ class Service : public fc_pb::DService::Service { void disable_controller(); static void daemonize(); - static bool service_running(); - // static void signal_handler(int signal); // static void register_signal_handler(); }; diff --git a/src/main.cpp b/src/main.cpp index 23d2777..6c1165a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,10 +19,15 @@ int main(int argc, char *argv[]) { // SERVICE if (args.service) { if (!is_root()) { - LOG(llvl::error) << "Service must be run as root"; + LOG(llvl::fatal) << "Service must be run as root"; return EXIT_FAILURE; } + if (Client::service_running()) { + LOG(llvl::fatal) << "Only 1 instance may be run"; + return EXIT_SUCCESS; + } + fc::Service service(config_path, args.daemon); service.run(); return EXIT_SUCCESS;