Improving script related rpm plugin hooks.

Two new hooks added:
PLUGINHOOK_SCRIPT_PRE_FUNC
Called before script execution
PLUGINHOOK_SCRIPT_POST_FUNC
Called after script execution

Both hooks are called for externals and internal lua scripts.
POST hook is called even if script execution has failed and
the return code is given as an argument.

Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
This commit is contained in:
Elena Reshetova 2012-11-22 20:49:27 +02:00 committed by Panu Matilainen
parent c0e95f1ced
commit d1d050a073
4 changed files with 67 additions and 17 deletions

View File

@ -264,18 +264,35 @@ rpmRC rpmpluginsCallPsmPost(rpmPlugins plugins, rpmte te)
return rc;
}
rpmRC rpmpluginsCallScriptSetup(rpmPlugins plugins, char* path)
rpmRC rpmpluginsCallScriptPre(rpmPlugins plugins, const char* path, int type)
{
rpmRC (*hookFunc)(char*);
rpmRC (*hookFunc)(const char*, int);
int i;
rpmRC rc = RPMRC_OK;
const char *name = NULL;
for (i = 0; i < plugins->count; i++) {
name = plugins->names[i];
RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_SCRIPT_SETUP);
if (hookFunc(path) == RPMRC_FAIL)
rc = RPMRC_FAIL;
name = plugins->names[i];
RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_SCRIPT_PRE);
if (hookFunc(path, type) == RPMRC_FAIL)
rc = RPMRC_FAIL;
}
return rc;
}
rpmRC rpmpluginsCallScriptPost(rpmPlugins plugins, const char* path, int type, int res)
{
rpmRC (*hookFunc)(const char*, int, int);
int i;
rpmRC rc = RPMRC_OK;
const char *name = NULL;
for (i = 0; i < plugins->count; i++) {
name = plugins->names[i];
RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_SCRIPT_POST);
if (hookFunc(path, type, res) == RPMRC_FAIL)
rc = RPMRC_FAIL;
}
return rc;

View File

@ -23,7 +23,8 @@ extern "C" {
#define PLUGINHOOK_PSM_PRE_FUNC pluginhook_psm_pre
#define PLUGINHOOK_PSM_POST_FUNC pluginhook_psm_post
#define PLUGINHOOK_SCRIPT_SETUP_FUNC pluginhook_script_setup
#define PLUGINHOOK_SCRIPT_PRE_FUNC pluginhook_script_pre
#define PLUGINHOOK_SCRIPT_POST_FUNC pluginhook_script_post
enum rpmPluginHook_e {
PLUGINHOOK_NONE = 0,
@ -37,9 +38,17 @@ enum rpmPluginHook_e {
PLUGINHOOK_TSM_POST = 1 << 7,
PLUGINHOOK_PSM_PRE = 1 << 8,
PLUGINHOOK_PSM_POST = 1 << 9,
PLUGINHOOK_SCRIPT_SETUP = 1 << 10
PLUGINHOOK_SCRIPT_PRE = 1 << 10,
PLUGINHOOK_SCRIPT_POST = 1 << 11
};
/* indicates if a script is internal rpm script or external one */
typedef enum rpmScriptType_e {
SCRIPT_TYPE_NONE = 0,
SCRIPT_TYPE_INTERNAL = 1 << 0,
SCRIPT_TYPE_EXTERNAL = 1 << 1
} rpmScriptType;
typedef rpmFlags rpmPluginHook;
/** \ingroup rpmplugins
@ -167,12 +176,23 @@ rpmRC rpmpluginsCallPsmPre(rpmPlugins plugins, rpmte te);
rpmRC rpmpluginsCallPsmPost(rpmPlugins plugins, rpmte te);
/** \ingroup rpmplugins
* Call the script setup plugin hook
* Call the pre script setup plugin hook
* @param plugins plugins structure
* @param path script path
* @param path script path or name depending on script type
* @param type type of the script, see rpmScriptType
* @return RPMRC_OK on success, RPMRC_FAIL otherwise
*/
rpmRC rpmpluginsCallScriptSetup(rpmPlugins plugins, char* path);
rpmRC rpmpluginsCallScriptPre(rpmPlugins plugins, const char* path, int type);
/** \ingroup rpmplugins
* Call the post script setup plugin hook
* @param plugins plugins structure
* @param path script path or name depending on script type
* @param type type of the script, see rpmScriptType
* @param res script execution result code
* @return RPMRC_OK on success, RPMRC_FAIL otherwise
*/
rpmRC rpmpluginsCallScriptPost(rpmPlugins plugins, const char* path, int type, int res);
#ifdef __cplusplus
}

View File

@ -29,7 +29,7 @@ struct rpmScript_s {
/**
* Run internal Lua script.
*/
static rpmRC runLuaScript(int selinux, ARGV_const_t prefixes,
static rpmRC runLuaScript(rpmPlugins plugins, int selinux, ARGV_const_t prefixes,
const char *sname, rpmlogLvl lvl, FD_t scriptFd,
ARGV_t * argvp, const char *script, int arg1, int arg2)
{
@ -69,9 +69,18 @@ static rpmRC runLuaScript(int selinux, ARGV_const_t prefixes,
mode_t oldmask = umask(0);
umask(oldmask);
/* Run script pre hook for all plugins */
if (rpmpluginsCallScriptPre(plugins, sname, SCRIPT_TYPE_INTERNAL) == RPMRC_FAIL) {
return rc; /* do not execute the lua script if plugin indicates to do so */
}
if (chdir("/") == 0 && rpmluaRunScript(lua, script, sname) == 0) {
rc = RPMRC_OK;
}
/* Run script post hook for all plugins */
rpmpluginsCallScriptPost(plugins, sname, SCRIPT_TYPE_INTERNAL, rc);
/* This failing would be fatal, return something different for it... */
if (fchdir(cwd)) {
rpmlog(RPMLOG_ERR, _("Unable to restore current directory: %m"));
@ -171,9 +180,9 @@ static void doScriptExec(rpmPlugins plugins, int selinux, ARGV_const_t argv, ARG
}
if (xx == 0) {
/* Run script setup hook for all plugins */
if (rpmpluginsCallScriptSetup(plugins, argv[0]) != RPMRC_FAIL) {
xx = execv(argv[0], argv);
/* Run script pre hook for all plugins */
if (rpmpluginsCallScriptPre(plugins, argv[0], SCRIPT_TYPE_EXTERNAL) != RPMRC_FAIL) {
xx = execv(argv[0], argv);
}
}
}
@ -270,6 +279,9 @@ static rpmRC runExtScript(rpmPlugins plugins, int selinux, ARGV_const_t prefixes
reaped = waitpid(pid, &status, 0);
} while (reaped == -1 && errno == EINTR);
/* Run script post hook for all plugins */
rpmpluginsCallScriptPost(plugins, *argvp[0], SCRIPT_TYPE_EXTERNAL, status);
rpmlog(RPMLOG_DEBUG, "%s: waitpid(%d) rc %d status %x\n",
sname, (unsigned)pid, (unsigned)reaped, status);
@ -318,7 +330,7 @@ rpmRC rpmScriptRun(rpmScript script, int arg1, int arg2, FD_t scriptFd,
}
if (rstreq(args[0], "<lua>")) {
rc = runLuaScript(selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
rc = runLuaScript(plugins, selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
} else {
rc = runExtScript(plugins, selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
}

View File

@ -26,4 +26,5 @@ rpmRC PLUGINHOOK_PSM_PRE_FUNC(rpmte te);
rpmRC PLUGINHOOK_PSM_POST_FUNC(rpmte te);
/*per script plugin hooks */
rpmRC PLUGINHOOK_SCRIPT_SETUP_FUNC(char* path);
rpmRC PLUGINHOOK_SCRIPT_PRE_FUNC(const char* path, int type);
rpmRC PLUGINHOOK_SCRIPT_POST_FUNC(const char* path, int type, int res);