Begin converting EXTERNCMD to a class

Mostly renaming for now.
This commit is contained in:
Esa Korhonen 2019-05-23 17:40:03 +03:00
parent 5043293b97
commit 11a8488162
4 changed files with 46 additions and 53 deletions

View File

@ -36,7 +36,7 @@ class Monitor;
struct DCB;
struct json_t;
struct EXTERNCMD;
struct ExternalCmd;
/**
* @verbatim
@ -557,7 +557,7 @@ private:
*
* @return Return value of the executed script or -1 on error.
*/
int launch_command(MonitorServer* ptr, EXTERNCMD* cmd);
int launch_command(MonitorServer* ptr, ExternalCmd* cmd);
enum class CredentialsApproach
{

View File

@ -103,9 +103,9 @@ static int tokenize_arguments(const char* argstr, char** argv)
return 0;
}
EXTERNCMD* externcmd_allocate(const char* argstr, uint32_t timeout)
ExternalCmd* ExternalCmd::externcmd_allocate(const char* argstr, uint32_t timeout)
{
EXTERNCMD* cmd = (EXTERNCMD*) MXS_MALLOC(sizeof(EXTERNCMD));
ExternalCmd* cmd = (ExternalCmd*) MXS_MALLOC(sizeof(ExternalCmd));
char** argv = (char**) MXS_MALLOC(sizeof(char*) * MAXSCALE_EXTCMD_ARG_MAX);
if (argstr && cmd && argv)
@ -147,7 +147,7 @@ EXTERNCMD* externcmd_allocate(const char* argstr, uint32_t timeout)
return cmd;
}
void externcmd_free(EXTERNCMD* cmd)
void ExternalCmd::externcmd_free(ExternalCmd* cmd)
{
if (cmd)
{
@ -225,8 +225,9 @@ static void log_output(const char* cmd, const std::string& str)
}
}
int externcmd_execute(EXTERNCMD* cmd)
int ExternalCmd::externcmd_execute()
{
auto cmd = this;
// Create a pipe where the command can print output
int fd[2];
@ -377,7 +378,7 @@ int externcmd_execute(EXTERNCMD* cmd)
return rval;
}
bool externcmd_substitute_arg(EXTERNCMD* cmd, const char* match, const char* replace)
bool externcmd_substitute_arg(ExternalCmd* cmd, const char* match, const char* replace)
{
int err;
bool rval = true;
@ -486,7 +487,7 @@ bool externcmd_can_execute(const char* argstr)
return rval;
}
bool externcmd_matches(const EXTERNCMD* cmd, const char* match)
bool externcmd_matches(const ExternalCmd* cmd, const char* match)
{
for (int i = 0; cmd->argv[i]; i++)
{

View File

@ -17,10 +17,38 @@
#define MAXSCALE_EXTCMD_ARG_MAX 256
MXS_BEGIN_DECLS
struct EXTERNCMD
class ExternalCmd
{
public:
/**
* Allocate a new external command.
*
* The name and parameters are copied into the external command structure so
* the original memory can be freed if needed.
*
* @param command Command to execute with the parameters
* @param timeout Command timeout in seconds
*
* @return Pointer to new external command struct or NULL if an error occurred
*/
static ExternalCmd* externcmd_allocate(const char* argstr, uint32_t timeout);
/**
* Free a previously allocated external command
*
* @param cmd Command to free
*/
static void externcmd_free(ExternalCmd* cmd);
/**
* Execute a command
*
* The output of the command must be freed by the caller by calling MXS_FREE.
*
* @return The return value of the executed command or -1 on error
*/
int externcmd_execute();
char** argv; /**< Argument vector for the command, first being the
* actual command being executed */
int n_exec; /**< Number of times executed */
@ -28,40 +56,6 @@ struct EXTERNCMD
uint32_t timeout; /**< Command timeout in seconds */
};
char* externcmd_extract_command(const char* argstr);
/**
* Allocate a new external command.
*
* The name and parameters are copied into the external command structure so
* the original memory can be freed if needed.
*
* @param command Command to execute with the parameters
* @param timeout Command timeout in seconds
*
* @return Pointer to new external command struct or NULL if an error occurred
*/
EXTERNCMD* externcmd_allocate(const char* argstr, uint32_t timeout);
/**
* Free a previously allocated external command
*
* @param cmd Command to free
*/
void externcmd_free(EXTERNCMD* cmd);
/**
* Execute a command
*
* The output of the command must be freed by the caller by calling MXS_FREE.
*
* @param cmd Command to execute
*
* @return The return value of the executed command or -1 on error
*/
int externcmd_execute(EXTERNCMD* cmd);
/**
* Substitute all occurrences of @c match with @c replace in the arguments for @c cmd
*
@ -71,7 +65,7 @@ int externcmd_execute(EXTERNCMD* cmd);
*
* @return True if replacement was successful, false on error
*/
bool externcmd_substitute_arg(EXTERNCMD* cmd, const char* re, const char* replace);
bool externcmd_substitute_arg(ExternalCmd* cmd, const char* re, const char* replace);
/**
* Check if a command can be executed
@ -93,6 +87,4 @@ bool externcmd_can_execute(const char* argstr);
*
* @return True if the string matched
*/
bool externcmd_matches(const EXTERNCMD* cmd, const char* match);
MXS_END_DECLS
bool externcmd_matches(const ExternalCmd* cmd, const char* match);

View File

@ -1054,7 +1054,7 @@ std::string Monitor::child_nodes(MonitorServer* parent)
return ss.str();
}
int Monitor::launch_command(MonitorServer* ptr, EXTERNCMD* cmd)
int Monitor::launch_command(MonitorServer* ptr, ExternalCmd* cmd)
{
if (externcmd_matches(cmd, "$INITIATOR"))
{
@ -1124,7 +1124,7 @@ int Monitor::launch_command(MonitorServer* ptr, EXTERNCMD* cmd)
externcmd_substitute_arg(cmd, "[$]SYNCEDLIST", nodelist);
}
int rv = externcmd_execute(cmd);
int rv = cmd->externcmd_execute();
if (rv)
{
@ -1202,7 +1202,7 @@ int Monitor::launch_script(MonitorServer* ptr)
char arg[strlen(script) + 1];
strcpy(arg, script);
EXTERNCMD* cmd = externcmd_allocate(arg, m_settings.script_timeout);
ExternalCmd* cmd = ExternalCmd::externcmd_allocate(arg, m_settings.script_timeout);
if (cmd == NULL)
{
@ -1214,7 +1214,7 @@ int Monitor::launch_script(MonitorServer* ptr)
int rv = launch_command(ptr, cmd);
externcmd_free(cmd);
ExternalCmd::externcmd_free(cmd);
return rv;
}