Moved common monitor code to externcmd.c
File existence and permission checks are now done in externcmd_can_execute
This commit is contained in:
@ -93,11 +93,11 @@ EXTERNCMD* externcmd_allocate(char* argstr)
|
||||
{
|
||||
if (access(cmd->argv[0], F_OK) != 0)
|
||||
{
|
||||
skygw_log_write(LE, "Error: Cannot find file: %s", cmd->argv[0]);
|
||||
skygw_log_write(LE, "Cannot find file: %s", cmd->argv[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE, "Error: Cannot execute file '%s'. Missing "
|
||||
skygw_log_write(LE, "Cannot execute file '%s'. Missing "
|
||||
"execution permissions.", cmd->argv[0]);
|
||||
}
|
||||
externcmd_free(cmd);
|
||||
@ -145,8 +145,8 @@ int externcmd_execute(EXTERNCMD* cmd)
|
||||
if(pid < 0)
|
||||
{
|
||||
char errbuf[STRERROR_BUFLEN];
|
||||
skygw_log_write(LOGFILE_ERROR,"Error: Failed to execute command '%s', fork failed: [%d] %s",
|
||||
cmd->argv[0],errno,strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||
skygw_log_write(LOGFILE_ERROR, "Failed to execute command '%s', fork failed: [%d] %s",
|
||||
cmd->argv[0], errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||
rval = -1;
|
||||
}
|
||||
else if(pid == 0)
|
||||
@ -159,9 +159,74 @@ int externcmd_execute(EXTERNCMD* cmd)
|
||||
{
|
||||
cmd->child = pid;
|
||||
cmd->n_exec++;
|
||||
LOGIF(LD,skygw_log_write(LD,"[monitor_exec_cmd] Forked child process %d : %s.",pid,cmd));
|
||||
LOGIF(LD, skygw_log_write(LD, "[monitor_exec_cmd] Forked child process %d : %s.", pid, cmd));
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the command being executed.
|
||||
*
|
||||
* This copies the command being executed into a new string.
|
||||
* @param str Command string, optionally with arguments
|
||||
* @return Command part of the string if arguments were defined
|
||||
*/
|
||||
char* get_command(const char* str)
|
||||
{
|
||||
char* rval = NULL;
|
||||
const char* start = str;
|
||||
|
||||
while (*start && isspace(*start))
|
||||
{
|
||||
start++;
|
||||
}
|
||||
|
||||
const char* end = start;
|
||||
|
||||
while (*end && !isspace(*end))
|
||||
{
|
||||
end++;
|
||||
}
|
||||
|
||||
size_t len = end - start;
|
||||
|
||||
if (len > 0 && (rval = malloc(len + 1)))
|
||||
{
|
||||
memcpy(rval, start, len);
|
||||
rval[len] = '\0';
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a command can be executed.
|
||||
*
|
||||
* Checks if the file being executed exists and if the current user has execution
|
||||
* permissions on the file.
|
||||
* @param argstr Command to check. Can contain arguments for the command.
|
||||
* @return True if the file was found and the use has execution permissions to it.
|
||||
*/
|
||||
bool externcmd_can_execute(const char* argstr)
|
||||
{
|
||||
bool rval = false;
|
||||
char *command = get_command(argstr);
|
||||
|
||||
if (command)
|
||||
{
|
||||
if (access(command, X_OK) == 0)
|
||||
{
|
||||
rval = true;
|
||||
}
|
||||
else if (access(command, F_OK) == 0)
|
||||
{
|
||||
skygw_log_write(LE, "The executable cannot be executed: %s", command);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE, "The executable cannot be found: %s", command);
|
||||
}
|
||||
free(command);
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
|
@ -18,4 +18,5 @@ typedef struct extern_cmd_t{
|
||||
EXTERNCMD* externcmd_allocate(char* argstr);
|
||||
void externcmd_free(EXTERNCMD* cmd);
|
||||
int externcmd_execute(EXTERNCMD* cmd);
|
||||
bool externcmd_can_execute(char* argstr);
|
||||
#endif
|
||||
|
@ -152,31 +152,14 @@ startMonitor(void *arg,void* opt)
|
||||
handle->use_priority = config_truth_value(params->value);
|
||||
else if(!strcmp(params->name,"script"))
|
||||
{
|
||||
if(handle->script)
|
||||
if (externcmd_can_execute(params->value))
|
||||
{
|
||||
free(handle->script);
|
||||
handle->script = NULL;
|
||||
}
|
||||
|
||||
if(access(params->value,X_OK) == 0)
|
||||
{
|
||||
handle->script = strdup(params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
script_error = true;
|
||||
if(access(params->value,F_OK) == 0)
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be executed: %s",
|
||||
params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be found: %s",
|
||||
params->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(!strcmp(params->name,"events"))
|
||||
|
@ -135,30 +135,14 @@ startMonitor(void *arg,void* opt)
|
||||
}
|
||||
else if(!strcmp(params->name,"script"))
|
||||
{
|
||||
if(handle->script)
|
||||
if (externcmd_can_execute(params->value))
|
||||
{
|
||||
free(handle->script);
|
||||
}
|
||||
if(access(params->value,X_OK) == 0)
|
||||
{
|
||||
handle->script = strdup(params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
script_error = true;
|
||||
if(access(params->value,F_OK) == 0)
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be executed: %s",
|
||||
params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be found: %s",
|
||||
params->value);
|
||||
}
|
||||
handle->script = NULL;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(params->name,"events"))
|
||||
|
@ -166,30 +166,16 @@ startMonitor(void *arg, void* opt)
|
||||
handle->detectStaleMaster = config_truth_value(params->value);
|
||||
else if(!strcmp(params->name,"detect_replication_lag"))
|
||||
handle->replicationHeartbeat = config_truth_value(params->value);
|
||||
else if(!strcmp(params->name,"script"))
|
||||
else if (!strcmp(params->name, "script"))
|
||||
{
|
||||
if (externcmd_can_execute(params->value))
|
||||
{
|
||||
if(handle->script)
|
||||
free(handle->script);
|
||||
if(access(params->value,X_OK) == 0)
|
||||
{
|
||||
handle->script = strdup(params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
script_error = true;
|
||||
if(access(params->value,F_OK) == 0)
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be executed: %s",
|
||||
params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be found: %s",
|
||||
params->value);
|
||||
}
|
||||
handle->script = NULL;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(params->name,"events"))
|
||||
|
@ -127,28 +127,14 @@ startMonitor(void *arg,void* opt)
|
||||
{
|
||||
if(!strcmp(params->name,"script"))
|
||||
{
|
||||
if(handle->script)
|
||||
free(handle->script);
|
||||
if(access(params->value,X_OK) == 0)
|
||||
if (externcmd_can_execute(params->value))
|
||||
{
|
||||
free(handle->script);
|
||||
handle->script = strdup(params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
script_error = true;
|
||||
if(access(params->value,F_OK) == 0)
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be executed: %s",
|
||||
params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
skygw_log_write(LE,
|
||||
"Error: The file cannot be found: %s",
|
||||
params->value);
|
||||
}
|
||||
handle->script = NULL;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(params->name,"events"))
|
||||
|
Reference in New Issue
Block a user