2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "rascsi_exceptions.h"
|
|
|
|
#include "command_context.h"
|
|
|
|
#include "protobuf_serializer.h"
|
|
|
|
#include "localizer.h"
|
|
|
|
#include "rasutil.h"
|
|
|
|
#include "rascsi_service.h"
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
using namespace rascsi_interface;
|
2022-11-04 07:22:32 +00:00
|
|
|
using namespace ras_util;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
void RascsiService::Cleanup() const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
running = false;
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
if (service_socket != -1) {
|
|
|
|
close(service_socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
bool RascsiService::Init(const callback& cb, int port)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2022-10-23 19:51:39 +00:00
|
|
|
if (port <= 0 || port > 65535) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
// Create socket for monitor
|
|
|
|
sockaddr_in server = {};
|
|
|
|
service_socket = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (service_socket == -1) {
|
2022-10-06 14:15:19 +00:00
|
|
|
LOGERROR("Unable to create socket")
|
2022-10-04 15:23:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.sin_family = PF_INET;
|
2022-10-06 14:15:19 +00:00
|
|
|
server.sin_port = htons((uint16_t)port);
|
2022-10-04 15:23:42 +00:00
|
|
|
server.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
// Allow address reuse
|
|
|
|
if (int yes = 1; setsockopt(service_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
if (bind(service_socket, (sockaddr *)&server, sizeof(sockaddr_in)) < 0) {
|
|
|
|
cerr << "Error: Port " << port << " is in use, is rascsi already running?" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
execute = cb;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
monthread = thread(&RascsiService::Execute, this);
|
|
|
|
monthread.detach();
|
|
|
|
|
|
|
|
// Interrupt handler settings
|
|
|
|
return signal(SIGINT, KillHandler) != SIG_ERR && signal(SIGHUP, KillHandler) != SIG_ERR
|
|
|
|
&& signal(SIGTERM, KillHandler) != SIG_ERR;
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
void RascsiService::Execute() const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
// Scheduler Settings
|
|
|
|
sched_param schedparam;
|
|
|
|
schedparam.sched_priority = 0;
|
|
|
|
sched_setscheduler(0, SCHED_IDLE, &schedparam);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Set the affinity to a specific processor core
|
2022-11-04 07:22:32 +00:00
|
|
|
FixCpu(2);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
// Wait for the execution to start
|
2022-10-08 17:26:04 +00:00
|
|
|
const timespec ts = { .tv_sec = 0, .tv_nsec = 1000};
|
2022-10-04 15:23:42 +00:00
|
|
|
while (!running) {
|
2022-10-06 14:15:19 +00:00
|
|
|
nanosleep(&ts, nullptr);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the monitor socket to receive commands
|
|
|
|
listen(service_socket, 1);
|
|
|
|
|
|
|
|
while (true) {
|
2022-10-06 14:15:19 +00:00
|
|
|
CommandContext context;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
try {
|
2022-10-06 14:15:19 +00:00
|
|
|
PbCommand command = ReadCommand(context);
|
|
|
|
if (context.IsValid()) {
|
|
|
|
execute(context, command);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const io_exception& e) {
|
2022-10-23 19:51:39 +00:00
|
|
|
LOGWARN("%s", e.what())
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
// Fall through
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
context.Cleanup();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
PbCommand RascsiService::ReadCommand(CommandContext& context) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
// Wait for connection
|
|
|
|
sockaddr client = {};
|
|
|
|
socklen_t socklen = sizeof(client);
|
2022-10-08 17:26:04 +00:00
|
|
|
const int fd = accept(service_socket, &client, &socklen);
|
2022-10-06 14:15:19 +00:00
|
|
|
if (fd == -1) {
|
2022-10-04 15:23:42 +00:00
|
|
|
throw io_exception("accept() failed");
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
PbCommand command;
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
// Read magic string
|
|
|
|
vector<byte> magic(6);
|
2022-10-08 17:26:04 +00:00
|
|
|
if (const size_t bytes_read = context.GetSerializer().ReadBytes(fd, magic);
|
2022-10-06 14:15:19 +00:00
|
|
|
bytes_read != magic.size() || memcmp(magic.data(), "RASCSI", magic.size())) {
|
2022-10-04 15:23:42 +00:00
|
|
|
throw io_exception("Invalid magic");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the command
|
2022-10-06 14:15:19 +00:00
|
|
|
context.GetSerializer().DeserializeMessage(fd, command);
|
|
|
|
|
|
|
|
context.SetFd(fd);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
return command;
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|