Skip to content

Commit

Permalink
Improve socket handling, log SG device, renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Feb 1, 2025
1 parent 2713338 commit ead93ef
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 36 deletions.
1 change: 1 addition & 0 deletions api/target_api.proto → api/s2p_interface.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
syntax = "proto3";

package s2p_interface;
option java_package = "net.scsi2pi.s2p_interface";

// The available device types
enum PbDeviceType {
Expand Down
28 changes: 14 additions & 14 deletions cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) \
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) \
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cpp/base/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <spdlog/spdlog.h>
#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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/base/device_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <unordered_map>
#include "shared/s2p_util.h"
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace std;
using namespace s2p_interface;
Expand Down
2 changes: 1 addition & 1 deletion cpp/command/command_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <spdlog/spdlog.h>
#include "command_localizer.h"
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace std;
using namespace spdlog;
Expand Down
2 changes: 1 addition & 1 deletion cpp/command/command_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <string>
#include <unordered_set>
#include <spdlog/spdlog.h>
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

class PrimaryDevice;

Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/protobuf/target_api_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <span>
#include <unordered_map>
#include <vector>
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace std;
using namespace s2p_interface;
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2p/s2p_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <spdlog/spdlog.h>
#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;
Expand Down
14 changes: 11 additions & 3 deletions cpp/s2p/s2p_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -73,4 +79,6 @@ void S2pThread::ExecuteCommand(int fd) const
// Ignore
}
}

return false;
}
2 changes: 1 addition & 1 deletion cpp/s2p/s2p_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class S2pThread
private:

void Execute() const;
void ExecuteCommand(int) const;
bool ExecuteCommand(int) const;

callback exec;

Expand Down
2 changes: 1 addition & 1 deletion cpp/s2pctl/s2pctl_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2pctl/s2pctl_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <string>
#include <unordered_map>
#include <vector>
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace std;
using namespace s2p_interface;
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2pctl/s2pctl_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2pproto/s2pproto_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2pproto/s2pproto_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <sstream>
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/json_util.h>
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace filesystem;
using namespace google::protobuf;
Expand Down
10 changes: 6 additions & 4 deletions cpp/shared/sg_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
//---------------------------------------------------------------------------

Expand All @@ -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 "";
Expand Down Expand Up @@ -107,7 +109,7 @@ SgAdapter::SgResult SgAdapter::SendCommandInternal(span<uint8_t> cdb, span<uint8
io_hdr.timeout = timeout * 1000;

if (enable_log && sg_logger.level() <= level::debug) {
sg_logger.debug(command_meta_data.LogCdb(cdb, "SG driver"));
sg_logger.debug(command_meta_data.LogCdb(cdb, fmt::format("SG driver ({})", device)));
}

const int status = ioctl(fd, SG_IO, &io_hdr) < 0 ? -1 : io_hdr.status;
Expand Down
2 changes: 2 additions & 0 deletions cpp/shared/sg_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class SgAdapter

void GetBlockSize();

string device;

logger &sg_logger;

CommandMetaData &command_meta_data = CommandMetaData::GetInstance();
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/s2p_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "base/property_handler.h"
#include "s2p/s2p_parser.h"
#include "shared/s2p_exceptions.h"
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace s2p_interface;
using namespace s2p_parser;
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/test_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "shared/scsi.h"
#include "base/property_handler.h"
#include "devices/storage_device.h"
#include "generated/target_api.pb.h"
#include "generated/s2p_interface.pb.h"

using namespace filesystem;
using namespace s2p_interface;
Expand Down

0 comments on commit ead93ef

Please sign in to comment.