mirror of
https://github.com/akuker/RASCSI.git
synced 2024-09-12 09:55:00 +00:00
Support for RaSCSI shutdown, system shutdown or reboot (#515)
* Initial reboot support * Moved code * Fixed typo * Shutdown update * rasctl passes mode "rascsi" * Use reboot() instead of syscall() * Fixed typo * Renaming * Comment update * Error message update * Updated error handling * Error message update * Added comment
This commit is contained in:
parent
916cf2c72c
commit
0abbff200e
@ -28,6 +28,8 @@
|
|||||||
#include "rascsi_interface.pb.h"
|
#include "rascsi_interface.pb.h"
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||||
|
#include <sys/reboot.h>
|
||||||
|
#include <linux/reboot.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -1088,6 +1090,58 @@ bool ProcessId(const string id_spec, PbDeviceType type, int& id, int& unit)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ShutDown(int fd, const string& mode) {
|
||||||
|
if (mode.empty()) {
|
||||||
|
ReturnStatus(fd, false, "Can't shut down: Missing shutdown mode");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PbResult result;
|
||||||
|
result.set_status(true);
|
||||||
|
|
||||||
|
if (mode == "rascsi") {
|
||||||
|
LOGINFO("RaSCSI shutdown requested");
|
||||||
|
|
||||||
|
SerializeMessage(fd, result);
|
||||||
|
|
||||||
|
TerminationHandler(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The root user has UID 0
|
||||||
|
if (getuid()) {
|
||||||
|
ReturnStatus(fd, false, "Can't shut down or reboot system: Missing root permissions");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode == "system") {
|
||||||
|
LOGINFO("System shutdown requested");
|
||||||
|
|
||||||
|
SerializeMessage(fd, result);
|
||||||
|
|
||||||
|
DetachAll();
|
||||||
|
sync();
|
||||||
|
|
||||||
|
if (reboot(LINUX_REBOOT_CMD_HALT) == -1) {
|
||||||
|
LOGERROR("System shutdown failed: %s", strerror(errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (mode == "reboot") {
|
||||||
|
LOGINFO("System reboot requested");
|
||||||
|
|
||||||
|
SerializeMessage(fd, result);
|
||||||
|
|
||||||
|
DetachAll();
|
||||||
|
sync();
|
||||||
|
|
||||||
|
if (reboot(LINUX_REBOOT_CMD_RESTART) == -1) {
|
||||||
|
LOGERROR("System reboot failed: %s", strerror(errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ReturnStatus(fd, false, "Invalid shutdown mode '" + mode + "'hi");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// Argument Parsing
|
// Argument Parsing
|
||||||
@ -1458,11 +1512,7 @@ static void *MonThread(void *param)
|
|||||||
case SHUT_DOWN: {
|
case SHUT_DOWN: {
|
||||||
LOGTRACE("Received %s command", PbOperation_Name(command.operation()).c_str());
|
LOGTRACE("Received %s command", PbOperation_Name(command.operation()).c_str());
|
||||||
|
|
||||||
PbResult result;
|
ShutDown(fd, GetParam(command, "mode"));
|
||||||
result.set_status(true);
|
|
||||||
SerializeMessage(fd, result);
|
|
||||||
|
|
||||||
TerminationHandler(0);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +114,8 @@ enum PbOperation {
|
|||||||
RESERVE_IDS = 22;
|
RESERVE_IDS = 22;
|
||||||
|
|
||||||
// Shut down the rascsi process
|
// Shut down the rascsi process
|
||||||
|
// Parameters:
|
||||||
|
// "mode": The shutdown mode, one of "rascsi", "system", "reboot"
|
||||||
SHUT_DOWN = 23;
|
SHUT_DOWN = 23;
|
||||||
|
|
||||||
// Create an image file. The image file must not yet exist.
|
// Create an image file. The image file must not yet exist.
|
||||||
|
@ -316,6 +316,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
case 'X':
|
case 'X':
|
||||||
command.set_operation(SHUT_DOWN);
|
command.set_operation(SHUT_DOWN);
|
||||||
|
AddParam(command, "mode", "rascsi");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user