2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-04 15:23:42 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-04 15:23:42 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "command_context.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "piscsi_service.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <sys/socket.h>
|
2022-10-04 15:23:42 +00:00
|
|
|
#include <netinet/in.h>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <arpa/inet.h>
|
2022-10-04 15:23:42 +00:00
|
|
|
#include <csignal>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <cassert>
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_util;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string PiscsiService::Init(const callback& cb, int port)
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
assert(service_socket == -1);
|
2022-11-04 07:22:32 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
if (port <= 0 || port > 65535) {
|
2023-10-15 06:38:15 +00:00
|
|
|
return "Invalid port number " + to_string(port);
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
service_socket = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (service_socket == -1) {
|
2023-10-15 06:38:15 +00:00
|
|
|
return "Unable to create service socket: " + string(strerror(errno));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (const int yes = 1; setsockopt(service_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
|
|
|
|
Stop();
|
|
|
|
return "Can't reuse address";
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_in server = {};
|
2022-10-04 15:23:42 +00:00
|
|
|
server.sin_family = PF_INET;
|
2022-10-06 14:15:19 +00:00
|
|
|
server.sin_port = htons((uint16_t)port);
|
2023-10-15 06:38:15 +00:00
|
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
if (bind(service_socket, reinterpret_cast<const sockaddr *>(&server), sizeof(sockaddr_in)) < 0) { //NOSONAR bit_cast is not supported by the bullseye compiler
|
|
|
|
Stop();
|
|
|
|
return "Port " + to_string(port) + " is in use, is piscsi already running?";
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (listen(service_socket, 2) == -1) {
|
|
|
|
Stop();
|
|
|
|
return "Can't listen to service socket: " + string(strerror(errno));
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 14:15:19 +00:00
|
|
|
execute = cb;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
return "";
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiService::Start()
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
assert(service_socket != -1);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
service_thread = jthread([this] () { Execute(); } );
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiService::Stop()
|
|
|
|
{
|
|
|
|
assert(service_socket != -1);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
shutdown(service_socket, SHUT_RD);
|
|
|
|
close(service_socket);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
service_socket = -1;
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiService::Execute() const
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
// Run this thread with very low priority
|
|
|
|
sched_param schedparam = { .sched_priority = 0 };
|
|
|
|
sched_setscheduler(0, SCHED_IDLE, &schedparam);
|
|
|
|
#endif
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// TODO Accept more than one command instead of closing the socket after a single command
|
|
|
|
while (service_socket != -1) {
|
|
|
|
const int fd = accept(service_socket, nullptr, nullptr);
|
|
|
|
if (fd != -1) {
|
|
|
|
ExecuteCommand(fd);
|
|
|
|
close(fd);
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void PiscsiService::ExecuteCommand(int fd) const
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
CommandContext context(fd);
|
|
|
|
try {
|
|
|
|
if (context.ReadCommand()) {
|
|
|
|
execute(context);
|
|
|
|
}
|
2022-11-16 09:01:51 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
catch(const io_exception& e) {
|
|
|
|
spdlog::warn(e.what());
|
2022-11-16 09:01:51 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// Try to return an error message (this may fail if the exception was caused when returning the actual result)
|
|
|
|
PbResult result;
|
|
|
|
result.set_msg(e.what());
|
|
|
|
try {
|
|
|
|
context.WriteResult(result);
|
|
|
|
}
|
|
|
|
catch(const io_exception&) { //NOSONAR Not handled on purpose
|
|
|
|
// Ignore
|
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
}
|