2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Protocol Helper
|
|
|
|
***********************************************************************************************************************************/
|
2019-04-26 08:08:23 -04:00
|
|
|
#include "build.auto.h"
|
|
|
|
|
2019-04-17 08:04:22 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-10 13:27:30 +02:00
|
|
|
#include "common/crypto/common.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/exec.h"
|
|
|
|
#include "common/memContext.h"
|
|
|
|
#include "config/config.h"
|
|
|
|
#include "config/exec.h"
|
2019-02-19 20:57:38 +02:00
|
|
|
#include "config/protocol.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
#include "protocol/helper.h"
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Constants
|
|
|
|
***********************************************************************************************************************************/
|
2019-02-27 22:34:21 +02:00
|
|
|
STRING_EXTERN(PROTOCOL_SERVICE_LOCAL_STR, PROTOCOL_SERVICE_LOCAL);
|
2019-01-18 21:32:51 +02:00
|
|
|
STRING_EXTERN(PROTOCOL_SERVICE_REMOTE_STR, PROTOCOL_SERVICE_REMOTE);
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Local variables
|
|
|
|
***********************************************************************************************************************************/
|
2019-02-27 22:34:21 +02:00
|
|
|
typedef struct ProtocolHelperClient
|
|
|
|
{
|
|
|
|
Exec *exec; // Executed client
|
|
|
|
ProtocolClient *client; // Protocol client
|
|
|
|
} ProtocolHelperClient;
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
MemContext *memContext; // Mem context for protocol helper
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
unsigned int clientRemoteSize; // Remote clients
|
|
|
|
ProtocolHelperClient *clientRemote;
|
|
|
|
|
|
|
|
unsigned int clientLocalSize; // Local clients
|
|
|
|
ProtocolHelperClient *clientLocal;
|
2019-01-18 21:32:51 +02:00
|
|
|
} protocolHelper;
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Init local mem context and data structure
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static void
|
|
|
|
protocolHelperInit(void)
|
|
|
|
{
|
|
|
|
// In the protocol helper has not been initialized
|
|
|
|
if (protocolHelper.memContext == NULL)
|
|
|
|
{
|
|
|
|
// Create a mem context to store protocol objects
|
|
|
|
MEM_CONTEXT_BEGIN(memContextTop())
|
|
|
|
{
|
|
|
|
protocolHelper.memContext = memContextNew("ProtocolHelper");
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Is the repository local?
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
bool
|
|
|
|
repoIsLocal(void)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
2019-01-28 15:06:28 +02:00
|
|
|
FUNCTION_TEST_RETURN(!cfgOptionTest(cfgOptRepoHost));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Is pg local?
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
bool
|
|
|
|
pgIsLocal(void)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
|
|
|
FUNCTION_TEST_RETURN(!cfgOptionTest(cfgOptPgHost + (cfgOptionTest(cfgOptHostId) ? cfgOptionUInt(cfgOptHostId) - 1 : 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Get host id if host-id option is set, otherwise return default id 1
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
unsigned int
|
|
|
|
protocolHostId(void)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
|
|
|
FUNCTION_TEST_RETURN(cfgOptionTest(cfgOptHostId) ? cfgOptionUInt(cfgOptHostId) : 1);
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
2019-02-27 22:34:21 +02:00
|
|
|
Get the command line required for local protocol execution
|
2019-01-18 21:32:51 +02:00
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static StringList *
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolLocalParam(ProtocolStorageType protocolStorageType, unsigned int protocolId)
|
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, protocolId);
|
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
StringList *result = NULL;
|
|
|
|
|
|
|
|
MEM_CONTEXT_TEMP_BEGIN()
|
|
|
|
{
|
|
|
|
// Option replacements
|
|
|
|
KeyValue *optionReplace = kvNew();
|
|
|
|
|
|
|
|
// Add the command option
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_COMMAND_STR), VARSTRZ(cfgCommandName(cfgCommand())));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
// Add the process id -- used when more than one process will be called
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_PROCESS_STR), VARINT((int)protocolId));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
// Add the host id -- for now this is hard-coded to 1
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_HOST_ID_STR), VARINT(1));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
// Add the type
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_TYPE_STR), VARSTRDEF("backup"));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2019-03-16 15:00:02 +04:00
|
|
|
// Only enable file logging on the local when requested
|
|
|
|
kvPut(
|
2019-04-17 08:04:22 -04:00
|
|
|
optionReplace, VARSTR(CFGOPT_LOG_LEVEL_FILE_STR),
|
|
|
|
cfgOptionBool(cfgOptLogSubprocess) ? cfgOption(cfgOptLogLevelFile) : VARSTRDEF("off"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Always output errors on stderr for debugging purposes
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_LOG_LEVEL_STDERR_STR), VARSTRDEF("error"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
result = strLstMove(cfgExecParam(cfgCmdLocal, optionReplace), MEM_CONTEXT_OLD());
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_TEMP_END();
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN(STRING_LIST, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Get the local protocol client
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
ProtocolClient *
|
|
|
|
protocolLocalGet(ProtocolStorageType protocolStorageType, unsigned int protocolId)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, protocolId);
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_END();
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperInit();
|
|
|
|
|
|
|
|
// Allocate the client cache
|
|
|
|
if (protocolHelper.clientLocalSize == 0)
|
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
2019-04-19 11:40:39 -04:00
|
|
|
protocolHelper.clientLocalSize = cfgOptionUInt(cfgOptProcessMax);
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelper.clientLocal = (ProtocolHelperClient *)memNew(
|
|
|
|
protocolHelper.clientLocalSize * sizeof(ProtocolHelperClient));
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(protocolId <= protocolHelper.clientLocalSize);
|
|
|
|
|
|
|
|
// Create protocol object
|
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientLocal[protocolId - 1];
|
|
|
|
|
|
|
|
if (protocolHelperClient->client == NULL)
|
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
|
|
|
// Execute the protocol command
|
|
|
|
protocolHelperClient->exec = execNew(
|
|
|
|
cfgExe(), protocolLocalParam(protocolStorageType, protocolId),
|
|
|
|
strNewFmt(PROTOCOL_SERVICE_LOCAL "-%u process", protocolId),
|
|
|
|
(TimeMSec)(cfgOptionDbl(cfgOptProtocolTimeout) * 1000));
|
|
|
|
execOpen(protocolHelperClient->exec);
|
|
|
|
|
|
|
|
// Create protocol object
|
|
|
|
protocolHelperClient->client = protocolClientNew(
|
|
|
|
strNewFmt(PROTOCOL_SERVICE_LOCAL "-%u protocol", protocolId),
|
|
|
|
PROTOCOL_SERVICE_LOCAL_STR, execIoRead(protocolHelperClient->exec), execIoWrite(protocolHelperClient->exec));
|
|
|
|
|
|
|
|
protocolClientMove(protocolHelperClient->client, execMemContext(protocolHelperClient->exec));
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN(PROTOCOL_CLIENT, protocolHelperClient->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Get the command line required for remote protocol execution
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static StringList *
|
|
|
|
protocolRemoteParam(ProtocolStorageType protocolStorageType, unsigned int protocolId)
|
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, protocolId);
|
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
// Is this a repo remote?
|
|
|
|
bool isRepo = protocolStorageType == protocolStorageTypeRepo;
|
|
|
|
|
|
|
|
// Get the host index. Default to 0 if host-id is not set.
|
|
|
|
unsigned int hostIdx = isRepo ? 0 : protocolHostId() - 1;
|
2019-01-18 21:32:51 +02:00
|
|
|
|
|
|
|
// Fixed parameters for ssh command
|
|
|
|
StringList *result = strLstNew();
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "LogLevel=error");
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "Compression=no");
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "PasswordAuthentication=no");
|
|
|
|
|
|
|
|
// Append port if specified
|
2019-07-17 14:09:50 -04:00
|
|
|
ConfigOption optHostPort = isRepo ? cfgOptRepoHostPort : cfgOptPgHostPort + hostIdx;
|
|
|
|
|
|
|
|
if (cfgOptionTest(optHostPort))
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
|
|
|
strLstAddZ(result, "-p");
|
2019-07-17 14:09:50 -04:00
|
|
|
strLstAdd(result, strNewFmt("%u", cfgOptionUInt(optHostPort)));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append user/host
|
2019-07-17 14:09:50 -04:00
|
|
|
strLstAdd(
|
|
|
|
result,
|
|
|
|
strNewFmt(
|
|
|
|
"%s@%s", strPtr(cfgOptionStr(isRepo ? cfgOptRepoHostUser : cfgOptPgHostUser + hostIdx)),
|
|
|
|
strPtr(cfgOptionStr(isRepo ? cfgOptRepoHost : cfgOptPgHost + hostIdx))));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
// Option replacements
|
2019-01-18 21:32:51 +02:00
|
|
|
KeyValue *optionReplace = kvNew();
|
|
|
|
|
|
|
|
// Replace config options with the host versions
|
2019-07-17 14:09:50 -04:00
|
|
|
unsigned int optConfig = isRepo ? cfgOptRepoHostConfig : cfgOptPgHostConfig + hostIdx;
|
|
|
|
|
|
|
|
if (cfgOptionSource(optConfig) != cfgSourceDefault)
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_CONFIG_STR), cfgOption(optConfig));
|
|
|
|
|
|
|
|
unsigned int optConfigIncludePath = isRepo ? cfgOptRepoHostConfigIncludePath : cfgOptPgHostConfigIncludePath + hostIdx;
|
|
|
|
|
|
|
|
if (cfgOptionSource(optConfigIncludePath) != cfgSourceDefault)
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_CONFIG_INCLUDE_PATH_STR), cfgOption(optConfigIncludePath));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
unsigned int optConfigPath = isRepo ? cfgOptRepoHostConfigPath : cfgOptPgHostConfigPath + hostIdx;
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
if (cfgOptionSource(optConfigPath) != cfgSourceDefault)
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_CONFIG_PATH_STR), cfgOption(optConfigPath));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-05-09 08:55:48 -04:00
|
|
|
// Use a C remote
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_C_STR), VARBOOL(true));
|
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
// Copy pg options to index 0 since that's what the remote will be expecting
|
|
|
|
if (hostIdx != 0)
|
|
|
|
{
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_PG1_PATH_STR), cfgOption(cfgOptPgPath + hostIdx));
|
|
|
|
|
|
|
|
if (cfgOptionSource(cfgOptPgSocketPath + hostIdx) != cfgSourceDefault)
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_PG1_SOCKET_PATH_STR), cfgOption(cfgOptPgSocketPath + hostIdx));
|
|
|
|
|
|
|
|
if (cfgOptionSource(cfgOptPgPort + hostIdx) != cfgSourceDefault)
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_PG1_PORT_STR), cfgOption(cfgOptPgPort + hostIdx));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove pg options that are not needed on the remote. This is to reduce clustter and make debugging options easier.
|
|
|
|
for (unsigned int pgIdx = 1; pgIdx < cfgOptionIndexTotal(cfgOptPgPath); pgIdx++)
|
|
|
|
{
|
|
|
|
kvPut(optionReplace, VARSTRZ(cfgOptionName(cfgOptPgPath + pgIdx)), NULL);
|
|
|
|
kvPut(optionReplace, VARSTRZ(cfgOptionName(cfgOptPgSocketPath + pgIdx)), NULL);
|
|
|
|
kvPut(optionReplace, VARSTRZ(cfgOptionName(cfgOptPgPort + pgIdx)), NULL);
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:51:19 +02:00
|
|
|
// Add the command option (or use the current command option if it is valid)
|
|
|
|
if (!cfgOptionTest(cfgOptCommand))
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_COMMAND_STR), VARSTRZ(cfgCommandName(cfgCommand())));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-28 09:51:19 +02:00
|
|
|
// Add the process id (or use the current process id if it is valid)
|
|
|
|
if (!cfgOptionTest(cfgOptProcess))
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_PROCESS_STR), VARINT((int)protocolId));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-03-16 15:00:02 +04:00
|
|
|
// Don't pass log-path or lock-path since these are host specific
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_LOG_PATH_STR), NULL);
|
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_LOCK_PATH_STR), NULL);
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Only enable file logging on the remote when requested
|
|
|
|
kvPut(
|
2019-04-17 08:04:22 -04:00
|
|
|
optionReplace, VARSTR(CFGOPT_LOG_LEVEL_FILE_STR),
|
|
|
|
cfgOptionBool(cfgOptLogSubprocess) ? cfgOption(cfgOptLogLevelFile) : VARSTRDEF("off"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Always output errors on stderr for debugging purposes
|
2019-04-17 08:04:22 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_LOG_LEVEL_STDERR_STR), VARSTRDEF("error"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Add the type
|
2019-07-17 14:09:50 -04:00
|
|
|
kvPut(optionReplace, VARSTR(CFGOPT_TYPE_STR), isRepo ? VARSTRDEF("backup") : VARSTRDEF("db"));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
|
|
|
StringList *commandExec = cfgExecParam(cfgCmdRemote, optionReplace);
|
2019-07-17 14:09:50 -04:00
|
|
|
strLstInsert(commandExec, 0, cfgOptionStr(isRepo ? cfgOptRepoHostCmd : cfgOptPgHostCmd + hostIdx));
|
2019-01-18 21:32:51 +02:00
|
|
|
strLstAdd(result, strLstJoin(commandExec, " "));
|
|
|
|
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_RETURN(STRING_LIST, result);
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
2019-02-27 22:34:21 +02:00
|
|
|
Get the remote protocol client
|
2019-01-18 21:32:51 +02:00
|
|
|
***********************************************************************************************************************************/
|
|
|
|
ProtocolClient *
|
2019-02-28 09:51:19 +02:00
|
|
|
protocolRemoteGet(ProtocolStorageType protocolStorageType)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_END();
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
// Is this a repo remote?
|
|
|
|
bool isRepo = protocolStorageType == protocolStorageTypeRepo;
|
|
|
|
|
|
|
|
// Get the host index. Default to 0 if host-id is not set.
|
|
|
|
unsigned int hostIdx = isRepo ? 0 : protocolHostId() - 1;
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperInit();
|
|
|
|
|
|
|
|
// Allocate the client cache
|
|
|
|
if (protocolHelper.clientRemoteSize == 0)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-02-27 22:34:21 +02:00
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-07-17 14:09:50 -04:00
|
|
|
// The number of remotes allowed is the greater of allowed repo or pg configs + 1 (0 is reserved for connections from
|
2019-02-27 22:34:21 +02:00
|
|
|
// the main process). Since these are static and only one will be true it presents a problem for coverage. We think
|
|
|
|
// that pg remotes will always be greater but we'll protect that assumption with an assertion.
|
|
|
|
ASSERT(cfgDefOptionIndexTotal(cfgDefOptPgPath) >= cfgDefOptionIndexTotal(cfgDefOptRepoPath));
|
|
|
|
|
2019-04-29 14:23:37 -04:00
|
|
|
protocolHelper.clientRemoteSize = cfgDefOptionIndexTotal(cfgDefOptPgPath) + 1;
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelper.clientRemote = (ProtocolHelperClient *)memNew(
|
|
|
|
protocolHelper.clientRemoteSize * sizeof(ProtocolHelperClient));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:51:19 +02:00
|
|
|
// Determine protocol id for the remote. If the process option is set then use that since we want to remote protocol id to
|
2019-04-29 16:10:27 -04:00
|
|
|
// match the local protocol id (but we'll still save it in position 0 or we'd need to allocated up to process-max slots).
|
|
|
|
// Otherwise set to 0 since the remote is being started from a main process.
|
2019-02-28 09:51:19 +02:00
|
|
|
unsigned int protocolId = 0;
|
2019-04-29 16:10:27 -04:00
|
|
|
unsigned int protocolIdx = 0;
|
2019-02-28 09:51:19 +02:00
|
|
|
|
|
|
|
if (cfgOptionTest(cfgOptProcess))
|
2019-04-19 11:40:39 -04:00
|
|
|
protocolId = cfgOptionUInt(cfgOptProcess);
|
2019-02-28 09:51:19 +02:00
|
|
|
|
2019-04-29 16:10:27 -04:00
|
|
|
CHECK(protocolIdx < protocolHelper.clientRemoteSize);
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Create protocol object
|
2019-04-29 16:10:27 -04:00
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientRemote[protocolIdx];
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
if (protocolHelperClient->client == NULL)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
2019-07-17 14:09:50 -04:00
|
|
|
unsigned int optHost = isRepo ? cfgOptRepoHost : cfgOptPgHost + hostIdx;
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Execute the protocol command
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperClient->exec = execNew(
|
|
|
|
cfgOptionStr(cfgOptCmdSsh), protocolRemoteParam(protocolStorageType, protocolId),
|
2019-07-17 14:09:50 -04:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_REMOTE "-%u process on '%s'", protocolId, strPtr(cfgOptionStr(optHost))),
|
2019-01-18 21:32:51 +02:00
|
|
|
(TimeMSec)(cfgOptionDbl(cfgOptProtocolTimeout) * 1000));
|
2019-02-27 22:34:21 +02:00
|
|
|
execOpen(protocolHelperClient->exec);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
|
|
|
// Create protocol object
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperClient->client = protocolClientNew(
|
2019-07-17 14:09:50 -04:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_REMOTE "-%u protocol on '%s'", protocolId, strPtr(cfgOptionStr(optHost))),
|
2019-02-27 22:34:21 +02:00
|
|
|
PROTOCOL_SERVICE_REMOTE_STR, execIoRead(protocolHelperClient->exec), execIoWrite(protocolHelperClient->exec));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-19 20:57:38 +02:00
|
|
|
// Get cipher options from the remote if none are locally configured
|
2019-07-17 14:09:50 -04:00
|
|
|
if (isRepo && strEq(cfgOptionStr(cfgOptRepoCipherType), CIPHER_TYPE_NONE_STR))
|
2019-02-19 20:57:38 +02:00
|
|
|
{
|
|
|
|
// Options to query
|
|
|
|
VariantList *param = varLstNew();
|
2019-04-12 09:03:34 -04:00
|
|
|
varLstAdd(param, varNewStr(CFGOPT_REPO1_CIPHER_TYPE_STR));
|
|
|
|
varLstAdd(param, varNewStr(CFGOPT_REPO1_CIPHER_PASS_STR));
|
2019-02-19 20:57:38 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
VariantList *optionList = configProtocolOption(protocolHelperClient->client, param);
|
2019-02-19 20:57:38 +02:00
|
|
|
|
|
|
|
if (!strEq(varStr(varLstGet(optionList, 0)), CIPHER_TYPE_NONE_STR))
|
|
|
|
{
|
|
|
|
cfgOptionSet(cfgOptRepoCipherType, cfgSourceConfig, varLstGet(optionList, 0));
|
|
|
|
cfgOptionSet(cfgOptRepoCipherPass, cfgSourceConfig, varLstGet(optionList, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolClientMove(protocolHelperClient->client, execMemContext(protocolHelperClient->exec));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_RETURN(PROTOCOL_CLIENT, protocolHelperClient->client);
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:59:28 +00:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Send keepalives to all remotes
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
protocolKeepAlive(void)
|
|
|
|
{
|
|
|
|
FUNCTION_LOG_VOID(logLevelTrace);
|
|
|
|
|
|
|
|
if (protocolHelper.memContext != NULL)
|
|
|
|
{
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientRemoteSize; clientIdx++)
|
|
|
|
{
|
|
|
|
if (protocolHelper.clientRemote[clientIdx].client != NULL)
|
|
|
|
protocolClientNoOp(protocolHelper.clientRemote[clientIdx].client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Free the protocol objects and shutdown processes
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
protocolFree(void)
|
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_VOID(logLevelTrace);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
if (protocolHelper.memContext != NULL)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-02-27 22:34:21 +02:00
|
|
|
// Free remotes
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientRemoteSize; clientIdx++)
|
|
|
|
{
|
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientRemote[clientIdx];
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
if (protocolHelperClient->client != NULL)
|
|
|
|
{
|
|
|
|
protocolClientFree(protocolHelperClient->client);
|
|
|
|
execFree(protocolHelperClient->exec);
|
|
|
|
|
|
|
|
protocolHelperClient->client = NULL;
|
|
|
|
protocolHelperClient->exec = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free locals
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientLocalSize; clientIdx++)
|
|
|
|
{
|
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientLocal[clientIdx];
|
|
|
|
|
|
|
|
if (protocolHelperClient->client != NULL)
|
|
|
|
{
|
|
|
|
protocolClientFree(protocolHelperClient->client);
|
|
|
|
execFree(protocolHelperClient->exec);
|
|
|
|
|
|
|
|
protocolHelperClient->client = NULL;
|
|
|
|
protocolHelperClient->exec = NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|