2022-11-02 22:41:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
|
|
|
// for Raspberry Pi
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Powered by XM6 TypeG Technology.
|
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
|
|
|
// Copyright (C) 2020-2023 Contributors to the PiSCSI project
|
|
|
|
// Copyright (C) 2023 Uwe Seimet
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/config.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_util.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
|
|
|
#include "shared/piscsi_version.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include "controllers/scsi_controller.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "devices/device_logger.h"
|
2022-11-04 07:22:32 +00:00
|
|
|
#include "devices/storage_device.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include "hal/gpiobus_factory.h"
|
2022-11-09 07:40:26 +00:00
|
|
|
#include "hal/gpiobus.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include "hal/systimer.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "piscsi/piscsi_core.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <csignal>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <vector>
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-11-10 06:44:06 +00:00
|
|
|
using namespace filesystem;
|
2023-10-30 12:57:46 +00:00
|
|
|
using namespace spdlog;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_interface;
|
|
|
|
using namespace piscsi_util;
|
2022-11-02 22:41:45 +00:00
|
|
|
using namespace protobuf_util;
|
2022-12-03 04:20:27 +00:00
|
|
|
using namespace scsi_defs;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void Piscsi::Banner(span<char *> args) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-05 17:58:23 +00:00
|
|
|
cout << piscsi_util::Banner("(Backend Service)");
|
|
|
|
cout << "Connection type: " << CONNECT_DESC << '\n' << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
if ((args.size() > 1 && strcmp(args[1], "-h") == 0) || (args.size() > 1 && strcmp(args[1], "--help") == 0)){
|
2022-11-10 06:44:06 +00:00
|
|
|
cout << "\nUsage: " << args[0] << " [-idID[:LUN] FILE] ...\n\n";
|
2023-10-15 06:38:15 +00:00
|
|
|
cout << " ID is SCSI device ID (0-" << (ControllerManager::GetScsiIdMax() - 1) << ").\n";
|
|
|
|
cout << " LUN is the optional logical unit (0-" << (ControllerManager::GetScsiLunMax() - 1) <<").\n";
|
2022-11-02 22:41:45 +00:00
|
|
|
cout << " FILE is a disk image file, \"daynaport\", \"bridge\", \"printer\" or \"services\".\n\n";
|
|
|
|
cout << " Image type is detected based on file extension if no explicit type is specified.\n";
|
|
|
|
cout << " hd1 : SCSI-1 HD image (Non-removable generic SCSI-1 HD image)\n";
|
|
|
|
cout << " hds : SCSI HD image (Non-removable generic SCSI HD image)\n";
|
|
|
|
cout << " hdr : SCSI HD image (Removable generic HD image)\n";
|
|
|
|
cout << " hda : SCSI HD image (Apple compatible image)\n";
|
|
|
|
cout << " hdn : SCSI HD image (NEC compatible image)\n";
|
|
|
|
cout << " hdi : SCSI HD image (Anex86 HD image)\n";
|
|
|
|
cout << " nhd : SCSI HD image (T98Next HD image)\n";
|
|
|
|
cout << " mos : SCSI MO image (MO image)\n";
|
2023-02-27 02:52:19 +00:00
|
|
|
cout << " iso : SCSI CD image (ISO 9660 image)\n";
|
|
|
|
cout << " is1 : SCSI CD image (ISO 9660 image, SCSI-1)\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool Piscsi::InitBus()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
bus = GPIOBUS_Factory::Create(BUS::mode_e::TARGET);
|
|
|
|
if (bus == nullptr) {
|
2022-11-02 22:41:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
executor = make_unique<PiscsiExecutor>(*bus, controller_manager);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void Piscsi::CleanUp()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
if (service.IsRunning()) {
|
|
|
|
service.Stop();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
executor->DetachAll();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
// TODO Check why there are rare cases where bus is NULL on a remote interface shutdown
|
|
|
|
// even though it is never set to NULL anywhere
|
|
|
|
assert(bus);
|
|
|
|
if (bus) {
|
|
|
|
bus->Cleanup();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void Piscsi::ReadAccessToken(const path& filename)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
if (error_code error; !is_regular_file(filename, error)) {
|
|
|
|
throw parser_exception("Access token file '" + filename.string() + "' must be a regular file");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (struct stat st; stat(filename.c_str(), &st) || st.st_uid || st.st_gid) {
|
|
|
|
throw parser_exception("Access token file '" + filename.string() + "' must be owned by root");
|
2022-11-10 06:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto perms = filesystem::status(filename).permissions();
|
|
|
|
(perms & perms::group_read) != perms::none || (perms & perms::others_read) != perms::none ||
|
|
|
|
(perms & perms::group_write) != perms::none || (perms & perms::others_write) != perms::none) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Access token file '" + filename.string() + "' must be readable by root only");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ifstream token_file(filename);
|
|
|
|
if (token_file.fail()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Can't open access token file '" + filename.string() + "'");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getline(token_file, access_token);
|
|
|
|
if (token_file.fail()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Can't read access token file '" + filename.string() + "'");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (access_token.empty()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Access token file '" + filename.string() + "' must not be empty");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
void Piscsi::LogDevices(string_view devices) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
stringstream ss(devices.data());
|
|
|
|
string line;
|
|
|
|
|
|
|
|
while (getline(ss, line, '\n')) {
|
2023-10-15 06:38:15 +00:00
|
|
|
spdlog::info(line);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 18:26:49 +00:00
|
|
|
void Piscsi::TerminationHandler(int)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
instance->CleanUp();
|
2022-11-06 18:44:44 +00:00
|
|
|
|
2023-01-14 18:26:49 +00:00
|
|
|
// Process will terminate automatically
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string Piscsi::ParseArguments(span<char *> args, PbCommand& command, int& port, string& reserved_ids)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
string log_level = "info";
|
2022-11-02 22:41:45 +00:00
|
|
|
PbDeviceType type = UNDEFINED;
|
|
|
|
int block_size = 0;
|
|
|
|
string name;
|
2022-11-11 20:08:48 +00:00
|
|
|
string id_and_lun;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string locale = GetLocale();
|
|
|
|
|
|
|
|
// Avoid duplicate messages while parsing
|
|
|
|
set_level(level::off);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
opterr = 1;
|
2023-10-15 06:38:15 +00:00
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "-Iib:d:n:p:r:t:z:D:F:L:P:R:C:v")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
// The two options below are kind of a compound option with two letters
|
2022-11-02 22:41:45 +00:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'd':
|
2022-11-11 20:08:48 +00:00
|
|
|
case 'D':
|
2023-10-15 06:38:15 +00:00
|
|
|
id_and_lun = optarg;
|
2022-11-02 22:41:45 +00:00
|
|
|
continue;
|
|
|
|
|
2023-10-01 21:50:30 +00:00
|
|
|
case 'b':
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!GetAsUnsignedInt(optarg, block_size)) {
|
|
|
|
throw parser_exception("Invalid block size " + string(optarg));
|
2023-10-01 21:50:30 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
case 'z':
|
2023-10-15 06:38:15 +00:00
|
|
|
locale = optarg;
|
2022-11-02 22:41:45 +00:00
|
|
|
continue;
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
case 'F':
|
2023-10-15 06:38:15 +00:00
|
|
|
if (const string error = piscsi_image.SetDefaultFolder(optarg); !error.empty()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception(error);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case 'L':
|
|
|
|
log_level = optarg;
|
|
|
|
continue;
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
case 'R':
|
|
|
|
int depth;
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!GetAsUnsignedInt(optarg, depth)) {
|
|
|
|
throw parser_exception("Invalid image file scan depth " + string(optarg));
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-12-05 17:58:23 +00:00
|
|
|
piscsi_image.SetDepth(depth);
|
2022-11-02 22:41:45 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'n':
|
2023-10-15 06:38:15 +00:00
|
|
|
name = optarg;
|
2022-11-02 22:41:45 +00:00
|
|
|
continue;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case 'p':
|
|
|
|
if (!GetAsUnsignedInt(optarg, port) || port <= 0 || port > 65535) {
|
|
|
|
throw parser_exception("Invalid port " + string(optarg) + ", port must be between 1 and 65535");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case 'P':
|
|
|
|
ReadAccessToken(optarg);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
reserved_ids = optarg;
|
|
|
|
continue;
|
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
case 't':
|
2023-10-15 06:38:15 +00:00
|
|
|
type = ParseDeviceType(optarg);
|
2022-11-02 22:41:45 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// Encountered filename
|
|
|
|
break;
|
2022-11-04 07:22:32 +00:00
|
|
|
|
|
|
|
default:
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Parser error");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (optopt) {
|
|
|
|
throw parser_exception("Parser error");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the device data
|
|
|
|
|
|
|
|
auto device = command.add_devices();
|
2022-11-11 20:08:48 +00:00
|
|
|
|
|
|
|
if (!id_and_lun.empty()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
if (const string error = SetIdAndLun(*device, id_and_lun); !error.empty()) {
|
2022-11-11 20:08:48 +00:00
|
|
|
throw parser_exception(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
device->set_type(type);
|
|
|
|
device->set_block_size(block_size);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
ParseParameters(*device, optarg);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
SetProductData(*device, name);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
type = UNDEFINED;
|
|
|
|
block_size = 0;
|
|
|
|
name = "";
|
2022-11-11 20:08:48 +00:00
|
|
|
id_and_lun = "";
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!SetLogLevel(log_level)) {
|
|
|
|
throw parser_exception("Invalid log level '" + log_level + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
return locale;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
PbDeviceType Piscsi::ParseDeviceType(const string& value)
|
|
|
|
{
|
|
|
|
string t;
|
|
|
|
ranges::transform(value, back_inserter(t), ::toupper);
|
|
|
|
if (PbDeviceType type; PbDeviceType_Parse(t, &type)) {
|
|
|
|
return type;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Illegal device type '" + value + "'");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool Piscsi::SetLogLevel(const string& log_level) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
int id = -1;
|
|
|
|
int lun = -1;
|
|
|
|
string level = log_level;
|
|
|
|
|
|
|
|
if (const auto& components = Split(log_level, COMPONENT_SEPARATOR, 2); !components.empty()) {
|
|
|
|
level = components[0];
|
|
|
|
|
|
|
|
if (components.size() > 1) {
|
|
|
|
if (const string error = ProcessId(components[1], id, lun); !error.empty()) {
|
|
|
|
spdlog::warn("Error setting log level: " + error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const level::level_enum l = level::from_str(level);
|
|
|
|
// Compensate for spdlog using 'off' for unknown levels
|
|
|
|
if (to_string_view(l) != level) {
|
|
|
|
spdlog::warn("Invalid log level '" + level + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_level(l);
|
|
|
|
DeviceLogger::SetLogIdAndLun(id, lun);
|
|
|
|
|
|
|
|
if (id != -1) {
|
|
|
|
if (lun == -1) {
|
|
|
|
spdlog::info("Set log level for device " + to_string(id) + " to '" + level + "'");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
spdlog::info("Set log level for device " + to_string(id) + ":" + to_string(lun) + " to '" + level + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
spdlog::info("Set log level to '" + level + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
bool Piscsi::ExecuteCommand(const CommandContext& context)
|
2023-10-15 06:38:15 +00:00
|
|
|
{
|
|
|
|
const PbCommand& command = context.GetCommand();
|
2023-10-30 12:57:46 +00:00
|
|
|
const PbOperation operation = command.operation();
|
2023-10-15 06:38:15 +00:00
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
if (!access_token.empty() && access_token != GetParam(command, "token")) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_AUTHENTICATION, UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
if (!PbOperation_IsValid(operation)) {
|
|
|
|
spdlog::trace("Ignored unknown command with operation opcode " + to_string(operation));
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_OPERATION, UNKNOWN_OPERATION, to_string(operation));
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
spdlog::trace("Received " + PbOperation_Name(operation) + " command");
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
PbResult result;
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
switch(operation) {
|
2023-10-15 06:38:15 +00:00
|
|
|
case LOG_LEVEL:
|
|
|
|
if (const string log_level = GetParam(command, "level"); !SetLogLevel(log_level)) {
|
2022-11-02 22:41:45 +00:00
|
|
|
context.ReturnLocalizedError(LocalizationKey::ERROR_LOG_LEVEL, log_level);
|
|
|
|
}
|
|
|
|
else {
|
2023-10-15 06:38:15 +00:00
|
|
|
context.ReturnSuccessStatus();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case DEFAULT_FOLDER:
|
|
|
|
if (const string error = piscsi_image.SetDefaultFolder(GetParam(command, "folder")); !error.empty()) {
|
|
|
|
context.ReturnErrorStatus(error);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-10-15 06:38:15 +00:00
|
|
|
context.ReturnSuccessStatus();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case DEVICES_INFO:
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result, command, piscsi_image.GetDefaultFolder());
|
|
|
|
context.WriteResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case DEVICE_TYPES_INFO:
|
|
|
|
response.GetDeviceTypesInfo(*result.mutable_device_types_info());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case SERVER_INFO:
|
2023-10-30 12:32:45 +00:00
|
|
|
response.GetServerInfo(*result.mutable_server_info(), command, controller_manager.GetAllDevices(),
|
|
|
|
executor->GetReservedIds(), piscsi_image.GetDefaultFolder(), piscsi_image.GetDepth());
|
2023-10-15 06:38:15 +00:00
|
|
|
context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case VERSION_INFO:
|
|
|
|
response.GetVersionInfo(*result.mutable_version_info());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case LOG_LEVEL_INFO:
|
|
|
|
response.GetLogLevelInfo(*result.mutable_log_level_info());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case DEFAULT_IMAGE_FILES_INFO:
|
|
|
|
response.GetImageFilesInfo(*result.mutable_image_files_info(), piscsi_image.GetDefaultFolder(),
|
|
|
|
GetParam(command, "folder_pattern"), GetParam(command, "file_pattern"), piscsi_image.GetDepth());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case IMAGE_FILE_INFO:
|
2022-11-02 22:41:45 +00:00
|
|
|
if (string filename = GetParam(command, "file"); filename.empty()) {
|
|
|
|
context.ReturnLocalizedError( LocalizationKey::ERROR_MISSING_FILENAME);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto image_file = make_unique<PbImageFile>();
|
2023-10-15 06:38:15 +00:00
|
|
|
const bool status = response.GetImageFile(*image_file.get(), piscsi_image.GetDefaultFolder(),
|
|
|
|
filename);
|
2022-11-02 22:41:45 +00:00
|
|
|
if (status) {
|
|
|
|
result.set_allocated_image_file_info(image_file.get());
|
2023-10-15 06:38:15 +00:00
|
|
|
result.set_status(true);
|
|
|
|
context.WriteResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
context.ReturnLocalizedError(LocalizationKey::ERROR_IMAGE_FILE_INFO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case NETWORK_INTERFACES_INFO:
|
|
|
|
response.GetNetworkInterfacesInfo(*result.mutable_network_interfaces_info());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case MAPPING_INFO:
|
|
|
|
response.GetMappingInfo(*result.mutable_mapping_info());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
case STATISTICS_INFO:
|
|
|
|
response.GetStatisticsInfo(*result.mutable_statistics_info(), controller_manager.GetAllDevices());
|
|
|
|
context.WriteSuccessResult(result);
|
|
|
|
break;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case OPERATION_INFO:
|
|
|
|
response.GetOperationInfo(*result.mutable_operation_info(), piscsi_image.GetDepth());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case RESERVED_IDS_INFO:
|
|
|
|
response.GetReservedIds(*result.mutable_reserved_ids_info(), executor->GetReservedIds());
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.WriteSuccessResult(result);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case SHUT_DOWN:
|
2023-10-30 12:57:46 +00:00
|
|
|
return ShutDown(context, GetParam(command, "mode"));
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case NO_OPERATION:
|
2023-10-30 12:57:46 +00:00
|
|
|
return context.ReturnSuccessStatus();
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
case CREATE_IMAGE:
|
2023-10-30 12:57:46 +00:00
|
|
|
return piscsi_image.CreateImage(context);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case DELETE_IMAGE:
|
2023-10-30 12:57:46 +00:00
|
|
|
return piscsi_image.DeleteImage(context);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case RENAME_IMAGE:
|
2023-10-30 12:57:46 +00:00
|
|
|
return piscsi_image.RenameImage(context);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case COPY_IMAGE:
|
2023-10-30 12:57:46 +00:00
|
|
|
return piscsi_image.CopyImage(context);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case PROTECT_IMAGE:
|
|
|
|
case UNPROTECT_IMAGE:
|
2023-10-30 12:57:46 +00:00
|
|
|
return piscsi_image.SetImagePermissions(context);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case RESERVE_IDS:
|
|
|
|
return executor->ProcessCmd(context);
|
|
|
|
|
|
|
|
default:
|
2023-10-30 12:57:46 +00:00
|
|
|
// The remaining commands may only be executed when the target is idle
|
|
|
|
if (!ExecuteWithLock(context)) {
|
|
|
|
return false;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2023-10-30 12:57:46 +00:00
|
|
|
|
|
|
|
return HandleDeviceListChange(context, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Piscsi::ExecuteWithLock(const CommandContext& context)
|
|
|
|
{
|
|
|
|
scoped_lock<mutex> lock(execution_locker);
|
|
|
|
return executor->ProcessCmd(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Piscsi::HandleDeviceListChange(const CommandContext& context, PbOperation operation) const
|
|
|
|
{
|
|
|
|
// ATTACH and DETACH return the resulting device list
|
|
|
|
if (operation == ATTACH || operation == DETACH) {
|
|
|
|
// A command with an empty device list is required here in order to return data for all devices
|
|
|
|
PbCommand command;
|
|
|
|
PbResult result;
|
|
|
|
response.GetDevicesInfo(controller_manager.GetAllDevices(), result, command, piscsi_image.GetDefaultFolder());
|
|
|
|
context.WriteResult(result);
|
|
|
|
return result.status();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
int Piscsi::run(span<char *> args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
|
|
|
|
|
|
|
Banner(args);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// The -v option shall result in no other action except displaying the version
|
|
|
|
if (ranges::find_if(args, [] (const char *arg) { return !strcasecmp(arg, "-v"); } ) != args.end()) {
|
|
|
|
cout << piscsi_get_version_string() << '\n';
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
PbCommand command;
|
|
|
|
string locale;
|
|
|
|
string reserved_ids;
|
2022-11-02 22:41:45 +00:00
|
|
|
int port = DEFAULT_PORT;
|
2022-11-10 06:44:06 +00:00
|
|
|
try {
|
2023-10-15 06:38:15 +00:00
|
|
|
locale = ParseArguments(args, command, port, reserved_ids);
|
2022-11-10 06:44:06 +00:00
|
|
|
}
|
|
|
|
catch(const parser_exception& e) {
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!InitBus()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
cerr << "Error: Can't initialize bus" << endl;
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
if (const string error = service.Init([this] (CommandContext& context) {
|
|
|
|
context.SetDefaultFolder(piscsi_image.GetDefaultFolder());
|
|
|
|
return ExecuteCommand(context);
|
|
|
|
}, port); !error.empty()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
cerr << "Error: " << error << endl;
|
2022-11-10 06:44:06 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
CleanUp();
|
2022-11-10 06:44:06 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (const string error = executor->SetReservedIds(reserved_ids); !error.empty()) {
|
|
|
|
cerr << "Error: " << error << endl;
|
|
|
|
|
|
|
|
CleanUp();
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (command.devices_size()) {
|
|
|
|
// Attach all specified devices
|
|
|
|
command.set_operation(ATTACH);
|
|
|
|
|
|
|
|
if (const CommandContext context(command, piscsi_image.GetDefaultFolder(), locale); !executor->ProcessCmd(context)) {
|
|
|
|
cerr << "Error: Can't attach devices" << endl;
|
|
|
|
|
|
|
|
CleanUp();
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display and log the device list
|
|
|
|
PbServerInfo server_info;
|
|
|
|
response.GetDevices(controller_manager.GetAllDevices(), server_info, piscsi_image.GetDefaultFolder());
|
|
|
|
const vector<PbDevice>& devices = { server_info.devices_info().devices().begin(), server_info.devices_info().devices().end() };
|
|
|
|
const string device_list = ListDevices(devices);
|
|
|
|
LogDevices(device_list);
|
|
|
|
cout << device_list << flush;
|
|
|
|
|
|
|
|
instance = this;
|
2022-11-02 22:41:45 +00:00
|
|
|
// Signal handler to detach all devices on a KILL or TERM signal
|
|
|
|
struct sigaction termination_handler;
|
|
|
|
termination_handler.sa_handler = TerminationHandler;
|
|
|
|
sigemptyset(&termination_handler.sa_mask);
|
|
|
|
termination_handler.sa_flags = 0;
|
|
|
|
sigaction(SIGINT, &termination_handler, nullptr);
|
|
|
|
sigaction(SIGTERM, &termination_handler, nullptr);
|
2023-10-15 06:38:15 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
// Set the affinity to a specific processor core
|
|
|
|
FixCpu(3);
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
service.Start();
|
|
|
|
|
|
|
|
Process();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piscsi::Process()
|
|
|
|
{
|
2022-11-02 22:41:45 +00:00
|
|
|
#ifdef USE_SEL_EVENT_ENABLE
|
|
|
|
// Scheduling policy setting (highest priority)
|
2023-10-15 06:38:15 +00:00
|
|
|
// TODO Check whether this results in any performance gain
|
|
|
|
sched_param schparam;
|
2022-11-02 22:41:45 +00:00
|
|
|
schparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
sched_setscheduler(0, SCHED_FIFO, &schparam);
|
|
|
|
#else
|
2022-12-05 17:58:23 +00:00
|
|
|
cout << "Note: No PiSCSI hardware support, only client interface calls are supported" << endl;
|
2022-11-02 22:41:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Main Loop
|
|
|
|
while (service.IsRunning()) {
|
|
|
|
#ifdef USE_SEL_EVENT_ENABLE
|
|
|
|
// SEL signal polling
|
|
|
|
if (!bus->PollSelectEvent()) {
|
|
|
|
// Stop on interrupt
|
|
|
|
if (errno == EINTR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the bus
|
|
|
|
bus->Acquire();
|
|
|
|
#else
|
|
|
|
bus->Acquire();
|
|
|
|
if (!bus->GetSEL()) {
|
|
|
|
const timespec ts = { .tv_sec = 0, .tv_nsec = 0};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// Only process the SCSI command if the bus is not busy and no other device responded
|
|
|
|
if (IsNotBusy() && bus->GetSEL()) {
|
2023-10-30 12:57:46 +00:00
|
|
|
scoped_lock<mutex> lock(execution_locker);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// Process command on the responsible controller based on the current initiator and target ID
|
|
|
|
if (const auto shutdown_mode = controller_manager.ProcessOnController(bus->GetDAT());
|
|
|
|
shutdown_mode != AbstractController::piscsi_shutdown_mode::NONE) {
|
|
|
|
// When the bus is free PiSCSI or the Pi may be shut down.
|
|
|
|
ShutDown(shutdown_mode);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
// Shutdown on a remote interface command
|
|
|
|
bool Piscsi::ShutDown(const CommandContext& context, const string& m) {
|
|
|
|
if (m.empty()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_MODE_MISSING);
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
AbstractController::piscsi_shutdown_mode mode = AbstractController::piscsi_shutdown_mode::NONE;
|
|
|
|
if (m == "rascsi") {
|
|
|
|
mode = AbstractController::piscsi_shutdown_mode::STOP_PISCSI;
|
|
|
|
}
|
|
|
|
else if (m == "system") {
|
|
|
|
mode = AbstractController::piscsi_shutdown_mode::STOP_PI;
|
|
|
|
}
|
|
|
|
else if (m == "reboot") {
|
|
|
|
mode = AbstractController::piscsi_shutdown_mode::RESTART_PI;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_MODE_INVALID, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown modes other than rascsi require root permissions
|
|
|
|
if (mode != AbstractController::piscsi_shutdown_mode::STOP_PISCSI && getuid()) {
|
|
|
|
return context.ReturnLocalizedError(LocalizationKey::ERROR_SHUTDOWN_PERMISSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report success now because after a shutdown nothing can be reported anymore
|
|
|
|
PbResult result;
|
|
|
|
context.WriteSuccessResult(result);
|
|
|
|
|
|
|
|
return ShutDown(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown on a SCSI command
|
|
|
|
bool Piscsi::ShutDown(AbstractController::piscsi_shutdown_mode shutdown_mode)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
switch(shutdown_mode) {
|
|
|
|
case AbstractController::piscsi_shutdown_mode::STOP_PISCSI:
|
|
|
|
spdlog::info("PiSCSI shutdown requested");
|
2023-10-30 12:57:46 +00:00
|
|
|
CleanUp();
|
|
|
|
return true;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case AbstractController::piscsi_shutdown_mode::STOP_PI:
|
|
|
|
spdlog::info("Raspberry Pi shutdown requested");
|
2023-10-30 12:57:46 +00:00
|
|
|
CleanUp();
|
2023-10-15 06:38:15 +00:00
|
|
|
if (system("init 0") == -1) {
|
|
|
|
spdlog::error("Raspberry Pi shutdown failed");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
break;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case AbstractController::piscsi_shutdown_mode::RESTART_PI:
|
|
|
|
spdlog::info("Raspberry Pi restart requested");
|
2023-10-30 12:57:46 +00:00
|
|
|
CleanUp();
|
2023-10-15 06:38:15 +00:00
|
|
|
if (system("init 6") == -1) {
|
|
|
|
spdlog::error("Raspberry Pi restart failed");
|
|
|
|
}
|
|
|
|
break;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
case AbstractController::piscsi_shutdown_mode::NONE:
|
|
|
|
assert(false);
|
|
|
|
break;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2023-10-30 12:57:46 +00:00
|
|
|
|
|
|
|
return false;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-11-11 20:08:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool Piscsi::IsNotBusy() const
|
2022-11-11 20:08:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
// Wait until BSY is released as there is a possibility for the
|
|
|
|
// initiator to assert it while setting the ID (for up to 3 seconds)
|
2022-11-11 20:08:48 +00:00
|
|
|
if (bus->GetBSY()) {
|
|
|
|
const uint32_t now = SysTimer::GetTimerLow();
|
|
|
|
|
|
|
|
// Wait for 3s
|
|
|
|
while ((SysTimer::GetTimerLow() - now) < 3'000'000) {
|
|
|
|
bus->Acquire();
|
|
|
|
|
|
|
|
if (!bus->GetBSY()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
return true;
|
2022-11-11 20:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
return false;
|
2022-11-11 20:08:48 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
return true;
|
2022-11-11 20:08:48 +00:00
|
|
|
}
|