From 849a8c848bc784b9fc72d6915f7aa41f1387e105 Mon Sep 17 00:00:00 2001 From: Lorenzo Stoakes Date: Thu, 15 Oct 2015 18:14:42 +0100 Subject: [PATCH] Prevent non-fatal errors from closing the supervisor. This patch fixes a couple of cases where `log.Fatalf` would cause the supervisor to exit immediately when it's inappropriate to do so. `log.Fatalf` and co. should not be used unless the supervisor utterly cannot run without whatever's being checked being in place. This patch also adjusts the code that relied on the module's values being created here being available, they now check and send an appropriate error message if they're not there. --- gosuper/gosuper/api.go | 19 +++++++++++++++++-- gosuper/systemd/systemd.go | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/gosuper/gosuper/api.go b/gosuper/gosuper/api.go index de633e8b..edc30feb 100644 --- a/gosuper/gosuper/api.go +++ b/gosuper/gosuper/api.go @@ -120,7 +120,12 @@ func inASecond(theFunc func()) { //RebootHandler Reboots the device using Systemd func RebootHandler(writer http.ResponseWriter, request *http.Request) { log.Println("Rebooting") - sendResponse, _ := responseSenders(writer) + + sendResponse, sendError := responseSenders(writer) + if systemd.Logind == nil { + sendError(fmt.Errorf("Logind unavailable, cannot reboot.")) + return + } sendResponse("OK", "", http.StatusAccepted) go inASecond(func() { systemd.Logind.Reboot(false) }) } @@ -129,7 +134,11 @@ func RebootHandler(writer http.ResponseWriter, request *http.Request) { func ShutdownHandler(writer http.ResponseWriter, request *http.Request) { log.Println("Shutting down") - sendResponse, _ := responseSenders(writer) + sendResponse, sendError := responseSenders(writer) + if systemd.Logind == nil { + sendError(fmt.Errorf("Logind unavailable, cannot shut down.")) + return + } sendResponse("OK", "", http.StatusAccepted) go inASecond(func() { systemd.Logind.PowerOff(false) }) } @@ -192,6 +201,12 @@ func VPNControl(writer http.ResponseWriter, request *http.Request) { sendResponse("Error", err.Error(), http.StatusBadRequest) return } + + if systemd.Dbus == nil { + sendError(fmt.Errorf("Systemd dbus unavailable, cannot set VPN state.")) + return + } + action := systemd.Dbus.StopUnit actionDescr := "VPN Disable" if body.Enable { diff --git a/gosuper/systemd/systemd.go b/gosuper/systemd/systemd.go index 3f801f6f..285281db 100644 --- a/gosuper/systemd/systemd.go +++ b/gosuper/systemd/systemd.go @@ -17,9 +17,9 @@ var ( func init() { var err error if Logind, err = login1.New(); err != nil { - log.Fatalf("Failed to connect to host system bus: %s", err) + log.Printf("Failed to connect to host system bus: %s", err) } if Dbus, err = dbus.New(); err != nil { - log.Fatalf("Failed to connect to host DBUS: %s", err) + log.Printf("Failed to connect to host DBUS: %s", err) } }