Skip to content

Commit

Permalink
Disable shared memory protocol by default on MacOS (indilib#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
pludov authored Jun 25, 2022
1 parent 1581a87 commit 67b6c5a
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 64 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ OPTION (INDI_BUILD_UNITTESTS "Build INDI tests" OFF)
OPTION (INDI_BUILD_INTEGTESTS "Build INDI integration tests" OFF)
OPTION (INDI_BUILD_WEBSOCKET "Build INDI with Websocket support" OFF)
OPTION (INDI_FAST_BLOB "Build INDI with Fast BLOB support" ON)
if (UNIX AND NOT APPLE)
OPTION(INDI_SHARED_MEMORY "Build INDI with support for UNIX protocol with shared memory" ON)
elseif (UNIX)
OPTION(INDI_SHARED_MEMORY "Build INDI with support for UNIX protocol with shared memory (require shm specific settings)" OFF)
endif()
OPTION (INDI_CALCULATE_MINMAX "Calculate and store image minimum and maximum values in FITS header" OFF)

CHECK_FUNCTION_EXISTS(mremap HAVE_MREMAP)
Expand All @@ -107,6 +112,16 @@ IF (INDI_FAST_BLOB)
add_definitions(-DWITH_ENCLEN)
ENDIF(INDI_FAST_BLOB)

###################################################################################################
#################################### UNIX protocol / SHM ########################################
###################################################################################################
IF (INDI_SHARED_MEMORY)
if (APPLE)
message(WARNING "Shared memory protocol require specific shared memory settings")
endif()
add_definitions(-DENABLE_INDI_SHARED_MEMORY)
endif()

###################################################################################################
###################################### Calculate Min/Max #########################################
###################################################################################################
Expand Down
25 changes: 20 additions & 5 deletions indiserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,8 @@ class TcpServer
void listen();
};

#ifdef ENABLE_INDI_SHARED_MEMORY

class UnixServer
{
std::string path;
Expand All @@ -851,8 +853,13 @@ class UnixServer
* exit on failure
*/
void listen();

static std::string unixSocketPath;
};

std::string UnixServer::unixSocketPath = INDIUNIXSOCK;

#endif

static void log(const std::string &log);
/* Turn a printf format into std::string */
Expand All @@ -862,7 +869,6 @@ static char *indi_tstamp(char *s);

static const char *me; /* our name */
static int port = INDIPORT; /* public INDI port */
static std::string unixSocketPath = INDIUNIXSOCK;
static int verbose; /* chattiness */
static char *ldir; /* where to log driver messages */
static unsigned int maxqsiz = (DEFMAXQSIZ * 1024 * 1024); /* kill if these bytes behind */
Expand Down Expand Up @@ -949,15 +955,17 @@ int main(int ac, char *av[])
maxstreamsiz = 1024 * 1024 * atoi(*++av);
ac--;
break;
#ifdef ENABLE_INDI_SHARED_MEMORY
case 'u':
if (ac < 2)
{
fprintf(stderr, "-f requires local socket path\n");
fprintf(stderr, "-u requires local socket path\n");
usage();
}
unixSocketPath = *++av;
UnixServer::unixSocketPath = *++av;
ac--;
break;
#endif // ENABLE_INDI_SHARED_MEMORY
case 'f':
if (ac < 2)
{
Expand Down Expand Up @@ -1014,9 +1022,10 @@ int main(int ac, char *av[])
/* announce we are online */
(new TcpServer(port))->listen();

#ifdef ENABLE_INDI_SHARED_MEMORY
/* create a new unix server */
(new UnixServer(unixSocketPath))->listen();

(new UnixServer(UnixServer::unixSocketPath))->listen();
#endif
/* Load up FIFO, if available */
if (fifo) fifo->listen();

Expand Down Expand Up @@ -1054,7 +1063,9 @@ static void usage(void)
fprintf(stderr,
" -d m : drop streaming blobs if client gets more than this many MB behind, default %d. 0 to disable\n",
DEFMAXSSIZ);
#ifdef ENABLE_INDI_SHARED_MEMORY
fprintf(stderr, " -u path : Path for the local connection socket (abstract), default %s\n", INDIUNIXSOCK);
#endif
fprintf(stderr, " -p p : alternate IP port, default %d\n", INDIPORT);
fprintf(stderr, " -r r : maximum driver restarts on error, default %d\n", DEFMAXRESTART);
fprintf(stderr, " -f path : Path to fifo for dynamic startup and shutdown of drivers.\n");
Expand Down Expand Up @@ -1356,6 +1367,8 @@ int RemoteDvrInfo::openINDIServer()
return (sockfd);
}

#ifdef ENABLE_INDI_SHARED_MEMORY

UnixServer::UnixServer(const std::string &path): path(path)
{
sfdev.set<UnixServer, &UnixServer::ioCb>(this);
Expand Down Expand Up @@ -1500,6 +1513,8 @@ void UnixServer::accept()
#endif
}

#endif // ENABLE_INDI_SHARED_MEMORY

TcpServer::TcpServer(int port): port(port)
{
sfdev.set<TcpServer, &TcpServer::ioCb>(this);
Expand Down
20 changes: 20 additions & 0 deletions integs/IndiClientMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <unistd.h>

#include "IndiClientMock.h"
#include "IndiServerController.h"
#include "utils.h"

IndiClientMock::IndiClientMock()
Expand All @@ -38,6 +39,25 @@ void IndiClientMock::close()
cnx.setFds(-1, -1);
}

void IndiClientMock::connect(const IndiServerController & server)
{
#ifdef ENABLE_INDI_SHARED_MEMORY
connectUnix(server);
#else
connectTcp(server);
#endif
}

void IndiClientMock::connectUnix(const IndiServerController & server)
{
connectUnix(server.getUnixSocketPath());
}

void IndiClientMock::connectTcp(const IndiServerController & server)
{
connectTcp("127.0.0.1", server.getTcpPort());
}

void IndiClientMock::connectUnix(const std::string &path)
{
fd = unixSocketConnect(path);
Expand Down
7 changes: 7 additions & 0 deletions integs/IndiClientMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "ConnectionMock.h"

class IndiServerController;

/**
* Interface to a mocked connection to indi server
*/
Expand All @@ -33,6 +35,11 @@ class IndiClientMock
ConnectionMock cnx;
IndiClientMock();
virtual ~IndiClientMock();

void connect(const IndiServerController & server);
void connectUnix(const IndiServerController & server);
void connectTcp(const IndiServerController & server);

void connectUnix(const std::string &path = "/tmp/indiserver");
void connectTcp(const std::string &host = "127.0.0.1", int port = 7624);
void associate(int fd);
Expand Down
28 changes: 27 additions & 1 deletion integs/IndiServerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@
#include "utils.h"
#include "IndiServerController.h"

void IndiServerController::start(const std::vector<std::string> args) {

#define TEST_TCP_PORT 17624
#define TEST_UNIX_SOCKET "/tmp/indi-test-server"
#define STRINGIFY_TOK(x) #x
#define TO_STRING(x) STRINGIFY_TOK(x)

void IndiServerController::start(const std::vector<std::string> & args) {
ProcessController::start("../indiserver", args);
}

void IndiServerController::startDriver(const std::string & path) {
std::vector<std::string> args = { "-p", TO_STRING(TEST_TCP_PORT), "-r", "0", "-vvv" };
#ifdef ENABLE_INDI_SHARED_MEMORY
args.push_back("-u");
args.push_back(TEST_UNIX_SOCKET);
#endif
args.push_back(path);

start(args);
}

std::string IndiServerController::getUnixSocketPath() const {
return TEST_UNIX_SOCKET;
}

int IndiServerController::getTcpPort() const {
return TEST_TCP_PORT;
}

7 changes: 6 additions & 1 deletion integs/IndiServerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class IndiServerController : public ProcessController
{

public:
void start(const std::vector<std::string> args);
void start(const std::vector<std::string> & args);

void startDriver(const std::string & driver);

std::string getUnixSocketPath() const;
int getTcpPort() const;
};


Expand Down
11 changes: 3 additions & 8 deletions integs/TestClientQueries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
#include "IndiServerController.h"
#include "IndiClientMock.h"

#define TEST_TCP_PORT 17624
#define TEST_UNIX_SOCKET "/tmp/indi-test-server"
#define STRINGIFY_TOK(x) #x
#define TO_STRING(x) STRINGIFY_TOK(x)

#define PROP_COUNT 5

static void driverSendsProps(DriverMock & fakeDriver) {
Expand Down Expand Up @@ -67,7 +62,7 @@ static void startFakeDev1(IndiServerController & indiServer, DriverMock & fakeDr
std::string fakeDriverPath = getTestExePath("fakedriver");

// Start indiserver with one instance, repeat 0
indiServer.start({ "-p", TO_STRING(TEST_TCP_PORT), "-u", TEST_UNIX_SOCKET, "-vvv", "-r", "0", fakeDriverPath.c_str() });
indiServer.startDriver(fakeDriverPath);
fprintf(stderr, "indiserver started\n");

fakeDriver.waitEstablish();
Expand Down Expand Up @@ -99,7 +94,7 @@ TEST(TestClientQueries, ServerForwardRequest) {

IndiClientMock indiClient;

indiClient.connectUnix(TEST_UNIX_SOCKET);
indiClient.connect(indiServer);

connectFakeDev1Client(indiServer, fakeDriver, indiClient);

Expand Down Expand Up @@ -127,7 +122,7 @@ TEST(TestClientQueries, ServerForwardRequestOfHalfDeadClient) {

IndiClientMock indiClient;

indiClient.connectUnix(TEST_UNIX_SOCKET);
indiClient.connect(indiServer);

connectFakeDev1Client(indiServer, fakeDriver, indiClient);

Expand Down
15 changes: 5 additions & 10 deletions integs/TestIndiSetProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#include "ProcessController.h"
#include "IndiClientMock.h"

#define TEST_TCP_PORT 17624
#define TEST_UNIX_SOCKET "/tmp/indi-test-server"
#define STRINGIFY_TOK(x) #x
#define TO_STRING(x) STRINGIFY_TOK(x)

// Having a large number of properties ensures cases with buffer not empty on exits occur on server side
#define PROP_COUNT 100

Expand Down Expand Up @@ -66,7 +61,7 @@ static void startFakeDev1(IndiServerController & indiServer, DriverMock & fakeDr
std::string fakeDriverPath = getTestExePath("fakedriver");

// Start indiserver with one instance, repeat 0
indiServer.start({ "-p", TO_STRING(TEST_TCP_PORT), "-u", TEST_UNIX_SOCKET, "-vvv", "-r", "0", fakeDriverPath.c_str() });
indiServer.startDriver(fakeDriverPath);
fprintf(stderr, "indiserver started\n");

fakeDriver.waitEstablish();
Expand All @@ -83,7 +78,7 @@ TEST(TestIndiSetProperties, SetFirstPropertyUntyped) {
startFakeDev1(indiServer, fakeDriver);

ProcessController indiSetProp;
startIndiSetProp(indiSetProp, { "-p", TO_STRING(TEST_TCP_PORT), "-v", "fakedev1.testnumber0.content=8" });
startIndiSetProp(indiSetProp, { "-p", std::to_string(indiServer.getTcpPort()), "-v", "fakedev1.testnumber0.content=8" });

driverIsAskedProps(fakeDriver);

Expand Down Expand Up @@ -111,7 +106,7 @@ TEST(TestIndiSetProperties, SetFirstPropertyTyped) {
startFakeDev1(indiServer, fakeDriver);

ProcessController indiSetProp;
startIndiSetProp(indiSetProp, { "-p", TO_STRING(TEST_TCP_PORT), "-v", "-n", "fakedev1.testnumber0.content=8" });
startIndiSetProp(indiSetProp, { "-p", std::to_string(indiServer.getTcpPort()), "-v", "-n", "fakedev1.testnumber0.content=8" });

indiSetProp.join();
indiSetProp.expectExitCode(0);
Expand All @@ -137,7 +132,7 @@ TEST(TestIndiSetProperties, SetLastProperty) {
startFakeDev1(indiServer, fakeDriver);

ProcessController indiSetProp;
startIndiSetProp(indiSetProp, { "-p", TO_STRING(TEST_TCP_PORT), "-v", "fakedev1.testnumber" + std::to_string(PROP_COUNT - 1) + ".content=8" });
startIndiSetProp(indiSetProp, { "-p", std::to_string(indiServer.getTcpPort()), "-v", "fakedev1.testnumber" + std::to_string(PROP_COUNT - 1) + ".content=8" });

driverIsAskedProps(fakeDriver);

Expand Down Expand Up @@ -165,7 +160,7 @@ TEST(TestIndiSetProperties, SetLastPropertyTyped) {
startFakeDev1(indiServer, fakeDriver);

ProcessController indiSetProp;
startIndiSetProp(indiSetProp, { "-p", TO_STRING(TEST_TCP_PORT), "-v", "-n", "fakedev1.testnumber" + std::to_string(PROP_COUNT - 1) + ".content=8" });
startIndiSetProp(indiSetProp, { "-p", std::to_string(indiServer.getTcpPort()), "-v", "-n", "fakedev1.testnumber" + std::to_string(PROP_COUNT - 1) + ".content=8" });

indiSetProp.join();
indiSetProp.expectExitCode(0);
Expand Down
Loading

0 comments on commit 67b6c5a

Please sign in to comment.