From ead93ef06f2b0b7b583eef0851b8a919f3125bd4 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sat, 1 Feb 2025 13:31:29 +0100 Subject: [PATCH] Improve socket handling, log SG device, renaming --- api/{target_api.proto => s2p_interface.proto} | 1 + cpp/Makefile | 28 +++++++++---------- cpp/base/device.h | 2 +- cpp/base/device_factory.h | 2 +- cpp/command/command_context.h | 2 +- cpp/command/command_response.h | 2 +- cpp/devices/cache.h | 2 +- cpp/protobuf/target_api_util.h | 2 +- cpp/s2p/s2p_parser.cpp | 2 +- cpp/s2p/s2p_thread.cpp | 14 ++++++++-- cpp/s2p/s2p_thread.h | 2 +- cpp/s2pctl/s2pctl_commands.h | 2 +- cpp/s2pctl/s2pctl_core.h | 2 +- cpp/s2pctl/s2pctl_display.h | 2 +- cpp/s2pproto/s2pproto_core.cpp | 2 +- cpp/s2pproto/s2pproto_executor.cpp | 2 +- cpp/shared/sg_adapter.cpp | 10 ++++--- cpp/shared/sg_adapter.h | 2 ++ cpp/test/s2p_parser_test.cpp | 2 +- cpp/test/test_shared.h | 2 +- 20 files changed, 49 insertions(+), 36 deletions(-) rename api/{target_api.proto => s2p_interface.proto} (99%) diff --git a/api/target_api.proto b/api/s2p_interface.proto similarity index 99% rename from api/target_api.proto rename to api/s2p_interface.proto index 6b067767..b7ce7a55 100644 --- a/api/target_api.proto +++ b/api/s2p_interface.proto @@ -20,6 +20,7 @@ syntax = "proto3"; package s2p_interface; +option java_package = "net.scsi2pi.s2p_interface"; // The available device types enum PbDeviceType { diff --git a/cpp/Makefile b/cpp/Makefile index 6373f24a..9d721a53 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -168,9 +168,9 @@ DIR_CONTROLLERS := controllers DIR_DEVICES := devices DIR_PI := pi -PROTO_TARGET_API := ../api/target_api.proto +PROTO_API := ../api/s2p_interface.proto -SRC_TARGET_API = $(GENERATED_DIR)/target_api.pb.cpp +SRC_API = $(GENERATED_DIR)/s2p_interface.pb.cpp SRC_SHARED = $(shell ls -1 $(DIR_SHARED)/*.cpp | grep -v sg_) ifdef IS_LINUX @@ -327,7 +327,7 @@ OBJ_S2PPROTO := $(call obj_files,$(SRC_S2PPROTO)) OBJ_S2PSIMH := $(call obj_files,$(SRC_S2PSIMH)) OBJ_S2PFORMAT := $(call obj_files,$(SRC_S2PFORMAT)) OBJ_S2PTOOL := $(call obj_files,$(SRC_S2PTOOL)) -OBJ_TARGET_API := $(call obj_files,$(SRC_TARGET_API)) +OBJ_API := $(call obj_files,$(SRC_API)) OBJ_S2P_TEST := $(call obj_files,$(SRC_S2P_TEST)) BINARIES = $(INSTALL_BIN)/$(S2PCTL) \ @@ -352,10 +352,10 @@ ALL_DEPS := $(patsubst %.o,%.d,$(OBJ_S2P_CORE) $(OBJ_S2PCTL_CORE) $(OBJ_S2P) $(O $(OBJ_SHARED_INITIATOR) $(OBJ_SHARED_COMMAND) $(OBJ_BASE) $(OBJ_BUSES) $(OBJ_CONTROLLERS) $(OBJ_DEVICES) $(OBJ_S2P_TEST)) -include $(ALL_DEPS) -$(OBJ_TARGET_API): $(SRC_TARGET_API) +$(OBJ_API): $(SRC_API) $(LIB_SHARED): $(OBJ_SHARED) -$(LIB_SHARED_PROTOBUF): $(OBJ_TARGET_API) $(OBJ_SHARED_PROTOBUF) $(OBJ_SHARED) -$(LIB_SHARED_COMMAND): $(OBJ_SHARED_COMMAND) $(OBJ_TARGET_API) $(OBJ_SHARED_PROTOBUF) $(OBJ_SHARED) +$(LIB_SHARED_PROTOBUF): $(OBJ_API) $(OBJ_SHARED_PROTOBUF) $(OBJ_SHARED) +$(LIB_SHARED_COMMAND): $(OBJ_SHARED_COMMAND) $(OBJ_API) $(OBJ_SHARED_PROTOBUF) $(OBJ_SHARED) $(LIB_SHARED_INITIATOR): $(OBJ_SHARED_INITIATOR) $(LIB_BUS): $(OBJ_BUSES) $(OBJ_PI) $(LIB_CONTROLLER): $(OBJ_CONTROLLERS) @@ -367,15 +367,15 @@ $(OBJDIR) $(LIBDIR) $(BINDIR) $(GENERATED_DIR): $(LIB_SHARED) $(LIB_SHARED_PROTOBUF) $(LIB_SHARED_INITIATOR) $(LIB_SHARED_COMMAND) $(LIB_BUS) $(LIB_CONTROLLER) $(LIB_DEVICE): | $(LIBDIR) $(AR) rcs $@ $^ -$(OBJDIR)/%.o: %.cpp | $(OBJDIR) $(SRC_TARGET_API) +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) $(SRC_API) $(CXX) $(CXXFLAGS) -c $< -o $@ -$(OBJ_TARGET_API): | $(OBJDIR) +$(OBJ_API): | $(OBJDIR) $(CXX) $(CXXFLAGS) -c $< -o $@ -$(SRC_TARGET_API): $(PROTO_TARGET_API) | $(GENERATED_DIR) - protoc -I ../api --cpp_out=$(GENERATED_DIR) $(PROTO_TARGET_API) - mv $(GENERATED_DIR)/target_api.pb.cc $@ +$(SRC_API): $(PROTO_API) | $(GENERATED_DIR) + protoc -I ../api --cpp_out=$(GENERATED_DIR) $(PROTO_API) + mv $(GENERATED_DIR)/s2p_interface.pb.cc $@ ## Build targets: ## all Buidl all executable files (default target) @@ -395,7 +395,7 @@ test: $(BINDIR)/$(S2P_TEST) coverage: CXXFLAGS += --coverage coverage: test -$(SRC_S2P_CORE) $(SRC_S2PCTL_CORE) $(SRC_S2PPROTO) $(SRC_S2PTOOL): $(OBJ_TARGET_API) +$(SRC_S2P_CORE) $(SRC_S2PCTL_CORE) $(SRC_S2PPROTO) $(SRC_S2PTOOL): $(OBJ_API) $(BINDIR)/$(S2P): $(LIB_SHARED_COMMAND) $(LIB_BUS) $(LIB_CONTROLLER) $(LIB_DEVICE) $(LIB_SHARED) $(OBJ_S2P_CORE) $(OBJ_S2P) | $(BINDIR) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(OBJ_S2P_CORE) $(OBJ_S2P) $(LIB_SHARED_COMMAND) $(LIB_BUS) $(LIB_CONTROLLER) \ @@ -410,9 +410,9 @@ $(BINDIR)/$(S2PDUMP): $(OBJ_S2PDUMP) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SH $(BINDIR)/$(S2PEXEC): $(OBJ_S2PEXEC) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SHARED) | $(BINDIR) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(OBJ_S2PEXEC) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SHARED) -$(BINDIR)/$(S2PPROTO): $(OBJ_S2PPROTO) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SHARED) $(OBJ_TARGET_API) | $(BINDIR) +$(BINDIR)/$(S2PPROTO): $(OBJ_S2PPROTO) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SHARED) $(OBJ_API) | $(BINDIR) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(OBJ_S2PPROTO) $(LIB_SHARED_INITIATOR) $(LIB_BUS) $(LIB_SHARED) \ - $(OBJ_TARGET_API) $(ABSEIL_LIBS) -lprotobuf + $(OBJ_API) $(ABSEIL_LIBS) -lprotobuf $(BINDIR)/$(S2PSIMH): $(OBJ_S2PSIMH) $(LIB_SHARED) | $(BINDIR) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(OBJ_S2PSIMH) $(LIB_SHARED) diff --git a/cpp/base/device.h b/cpp/base/device.h index e02a73f8..25d70ec8 100644 --- a/cpp/base/device.h +++ b/cpp/base/device.h @@ -11,7 +11,7 @@ #include #include "shared/s2p_util.h" #include "shared/s2p_version.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace spdlog; using namespace s2p_interface; diff --git a/cpp/base/device_factory.h b/cpp/base/device_factory.h index 54d064b4..ad96b3cb 100644 --- a/cpp/base/device_factory.h +++ b/cpp/base/device_factory.h @@ -12,7 +12,7 @@ #include #include "shared/s2p_util.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/command/command_context.h b/cpp/command/command_context.h index 799d06ca..e612dcdd 100644 --- a/cpp/command/command_context.h +++ b/cpp/command/command_context.h @@ -10,7 +10,7 @@ #include #include "command_localizer.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace spdlog; diff --git a/cpp/command/command_response.h b/cpp/command/command_response.h index b65a385c..cc89077b 100644 --- a/cpp/command/command_response.h +++ b/cpp/command/command_response.h @@ -11,7 +11,7 @@ #include #include #include -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" class PrimaryDevice; diff --git a/cpp/devices/cache.h b/cpp/devices/cache.h index dcd47f48..54f7ac6f 100644 --- a/cpp/devices/cache.h +++ b/cpp/devices/cache.h @@ -9,7 +9,7 @@ #pragma once #include "shared/s2p_defs.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/protobuf/target_api_util.h b/cpp/protobuf/target_api_util.h index 89a35edb..6fd58196 100644 --- a/cpp/protobuf/target_api_util.h +++ b/cpp/protobuf/target_api_util.h @@ -11,7 +11,7 @@ #include #include #include -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/s2p/s2p_parser.cpp b/cpp/s2p/s2p_parser.cpp index 507f5b6f..6e849719 100644 --- a/cpp/s2p/s2p_parser.cpp +++ b/cpp/s2p/s2p_parser.cpp @@ -13,7 +13,7 @@ #include #include "controllers/controller_factory.h" #include "shared/s2p_exceptions.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace s2p_util; using namespace s2p_interface; diff --git a/cpp/s2p/s2p_thread.cpp b/cpp/s2p/s2p_thread.cpp index 551c0365..2deb7257 100644 --- a/cpp/s2p/s2p_thread.cpp +++ b/cpp/s2p/s2p_thread.cpp @@ -44,20 +44,26 @@ bool S2pThread::IsRunning() const void S2pThread::Execute() const { + int fd = -1; while (server.IsRunning()) { - if (const int fd = server.Accept(); fd != -1) { - ExecuteCommand(fd); + if (fd == -1) { + fd = server.Accept(); + } + + if (fd != -1 && !ExecuteCommand(fd)) { close(fd); + fd = -1; } } } -void S2pThread::ExecuteCommand(int fd) const +bool S2pThread::ExecuteCommand(int fd) const { CommandContext context(fd, *s2p_logger); try { if (context.ReadCommand()) { exec(context); + return true; } } catch (const IoException &e) { @@ -73,4 +79,6 @@ void S2pThread::ExecuteCommand(int fd) const // Ignore } } + + return false; } diff --git a/cpp/s2p/s2p_thread.h b/cpp/s2p/s2p_thread.h index 5709cf7d..ac7dea28 100644 --- a/cpp/s2p/s2p_thread.h +++ b/cpp/s2p/s2p_thread.h @@ -34,7 +34,7 @@ class S2pThread private: void Execute() const; - void ExecuteCommand(int) const; + bool ExecuteCommand(int) const; callback exec; diff --git a/cpp/s2pctl/s2pctl_commands.h b/cpp/s2pctl/s2pctl_commands.h index a5ffb47d..d28831a2 100644 --- a/cpp/s2pctl/s2pctl_commands.h +++ b/cpp/s2pctl/s2pctl_commands.h @@ -8,7 +8,7 @@ #pragma once -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/s2pctl/s2pctl_core.h b/cpp/s2pctl/s2pctl_core.h index 18a2e47c..d5101103 100644 --- a/cpp/s2pctl/s2pctl_core.h +++ b/cpp/s2pctl/s2pctl_core.h @@ -11,7 +11,7 @@ #include #include #include -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/s2pctl/s2pctl_display.h b/cpp/s2pctl/s2pctl_display.h index 7308d38d..a2556237 100644 --- a/cpp/s2pctl/s2pctl_display.h +++ b/cpp/s2pctl/s2pctl_display.h @@ -8,7 +8,7 @@ #pragma once -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace std; using namespace s2p_interface; diff --git a/cpp/s2pproto/s2pproto_core.cpp b/cpp/s2pproto/s2pproto_core.cpp index 100d874d..fced443f 100644 --- a/cpp/s2pproto/s2pproto_core.cpp +++ b/cpp/s2pproto/s2pproto_core.cpp @@ -16,7 +16,7 @@ #include "buses/bus_factory.h" #include "initiator/initiator_util.h" #include "shared/s2p_exceptions.h" -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace google::protobuf; using namespace google::protobuf::util; diff --git a/cpp/s2pproto/s2pproto_executor.cpp b/cpp/s2pproto/s2pproto_executor.cpp index 5db2e05c..c19fb75b 100644 --- a/cpp/s2pproto/s2pproto_executor.cpp +++ b/cpp/s2pproto/s2pproto_executor.cpp @@ -12,7 +12,7 @@ #include #include #include -#include "generated/target_api.pb.h" +#include "generated/s2p_interface.pb.h" using namespace filesystem; using namespace google::protobuf; diff --git a/cpp/shared/sg_adapter.cpp b/cpp/shared/sg_adapter.cpp index 1ae8695e..29ec9cdf 100644 --- a/cpp/shared/sg_adapter.cpp +++ b/cpp/shared/sg_adapter.cpp @@ -2,7 +2,7 @@ // // SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi // -// Copyright (C) 2024 Uwe Seimet +// Copyright (C) 2024-2025 Uwe Seimet // //--------------------------------------------------------------------------- @@ -18,15 +18,17 @@ using namespace memory_util; using namespace sg_util; -string SgAdapter::Init(const string &device) +string SgAdapter::Init(const string &d) { try { - fd = OpenDevice(device); + fd = OpenDevice(d); } catch (const IoException &e) { return e.what(); } + device = d; + GetBlockSize(); return ""; @@ -107,7 +109,7 @@ SgAdapter::SgResult SgAdapter::SendCommandInternal(span cdb, span