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

Commit

Permalink
- Fixed bug where the controller loses control of devices if another …
Browse files Browse the repository at this point in the history
…service attempts to run

Signed-off-by: Hayden Briese <[email protected]>
  • Loading branch information
hbriese committed Jul 22, 2020
1 parent 335617f commit 8bbe4de
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fancon (0.21.0) UNRELEASED; urgency=low
fancon (0.21.2) UNRELEASED; urgency=low

* Initial release. Closes: #00000

Expand Down
7 changes: 7 additions & 0 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 0 additions & 12 deletions src/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
// ...
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions src/Service.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef FANCON_SERVICE_HPP
#define FANCON_SERVICE_HPP

#include "Controller.hpp"
#include <csignal>
#include <grpcpp/grpcpp.h>
#include <grpcpp/server.h>
#include <grpcpp/support/status.h>
#include <mutex>
#include <sys/stat.h>

#include "Controller.hpp"
#include "proto/DevicesSpec.grpc.pb.h"
#include "proto/DevicesSpec.pb.h"

Expand Down Expand Up @@ -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();
};
Expand Down
7 changes: 6 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8bbe4de

Please sign in to comment.