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 DCB;
struct json_t; struct json_t;
struct EXTERNCMD; struct ExternalCmd;
/** /**
* @verbatim * @verbatim
@ -557,7 +557,7 @@ private:
* *
* @return Return value of the executed script or -1 on error. * @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 enum class CredentialsApproach
{ {

View File

@ -103,9 +103,9 @@ static int tokenize_arguments(const char* argstr, char** argv)
return 0; 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); char** argv = (char**) MXS_MALLOC(sizeof(char*) * MAXSCALE_EXTCMD_ARG_MAX);
if (argstr && cmd && argv) if (argstr && cmd && argv)
@ -147,7 +147,7 @@ EXTERNCMD* externcmd_allocate(const char* argstr, uint32_t timeout)
return cmd; return cmd;
} }
void externcmd_free(EXTERNCMD* cmd) void ExternalCmd::externcmd_free(ExternalCmd* cmd)
{ {
if (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 // Create a pipe where the command can print output
int fd[2]; int fd[2];
@ -377,7 +378,7 @@ int externcmd_execute(EXTERNCMD* cmd)
return rval; 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; int err;
bool rval = true; bool rval = true;
@ -486,7 +487,7 @@ bool externcmd_can_execute(const char* argstr)
return rval; 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++) for (int i = 0; cmd->argv[i]; i++)
{ {

View File

@ -17,19 +17,9 @@
#define MAXSCALE_EXTCMD_ARG_MAX 256 #define MAXSCALE_EXTCMD_ARG_MAX 256
MXS_BEGIN_DECLS class ExternalCmd
struct EXTERNCMD
{ {
char** argv; /**< Argument vector for the command, first being the public:
* actual command being executed */
int n_exec; /**< Number of times executed */
pid_t child; /**< PID of the child process */
uint32_t timeout; /**< Command timeout in seconds */
};
char* externcmd_extract_command(const char* argstr);
/** /**
* Allocate a new external command. * Allocate a new external command.
* *
@ -41,26 +31,30 @@ char* externcmd_extract_command(const char* argstr);
* *
* @return Pointer to new external command struct or NULL if an error occurred * @return Pointer to new external command struct or NULL if an error occurred
*/ */
EXTERNCMD* externcmd_allocate(const char* argstr, uint32_t timeout); static ExternalCmd* externcmd_allocate(const char* argstr, uint32_t timeout);
/** /**
* Free a previously allocated external command * Free a previously allocated external command
* *
* @param cmd Command to free * @param cmd Command to free
*/ */
void externcmd_free(EXTERNCMD* cmd); static void externcmd_free(ExternalCmd* cmd);
/** /**
* Execute a command * Execute a command
* *
* The output of the command must be freed by the caller by calling MXS_FREE. * 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 * @return The return value of the executed command or -1 on error
*/ */
int externcmd_execute(EXTERNCMD* cmd); int externcmd_execute();
char** argv; /**< Argument vector for the command, first being the
* actual command being executed */
int n_exec; /**< Number of times executed */
pid_t child; /**< PID of the child process */
uint32_t timeout; /**< Command timeout in seconds */
};
/** /**
* Substitute all occurrences of @c match with @c replace in the arguments for @c 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 * @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 * Check if a command can be executed
@ -93,6 +87,4 @@ bool externcmd_can_execute(const char* argstr);
* *
* @return True if the string matched * @return True if the string matched
*/ */
bool externcmd_matches(const EXTERNCMD* cmd, const char* match); bool externcmd_matches(const ExternalCmd* cmd, const char* match);
MXS_END_DECLS

View File

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