wuyuoss / rpms / rpm

Forked from rpms/rpm 3 years ago
Clone
4c9460
diff --git a/configure.ac b/configure.ac
4c9460
index 167491e..3bacc1d 100644
4c9460
--- a/configure.ac
4c9460
+++ b/configure.ac
4c9460
@@ -745,6 +745,16 @@ AS_IF([test "$enable_plugins" = yes],[
4c9460
 ])
4c9460
 AM_CONDITIONAL(ENABLE_PLUGINS,[test "$enable_plugins" = yes])
4c9460
 
4c9460
+with_dbus=no
4c9460
+AS_IF([test "$enable_plugins" != no],[
4c9460
+  PKG_CHECK_MODULES([DBUS],
4c9460
+    [dbus-1 >= 1.0],
4c9460
+    [AC_DEFINE(DBUS, 1, [Build with dbus support?]) with_dbus=yes],
4c9460
+    [with_dbus=no])
4c9460
+  AC_SUBST(DBUS_CFLAGS)
4c9460
+  AC_SUBST(DBUS_LIBS)
4c9460
+])
4c9460
+AM_CONDITIONAL(DBUS, [test "$with_dbus" = yes])
4c9460
 
4c9460
 with_dmalloc=no
4c9460
 AC_ARG_WITH(dmalloc, [AS_HELP_STRING([--with-dmalloc],[build with dmalloc debugging support])])
4c9460
diff --git a/lib/psm.c b/lib/psm.c
4c9460
index 8f5376d..e80a90e 100644
4c9460
--- a/lib/psm.c
4c9460
+++ b/lib/psm.c
4c9460
@@ -23,8 +23,11 @@
4c9460
 #include "lib/rpmfi_internal.h" /* XXX replaced/states... */
4c9460
 #include "lib/rpmte_internal.h"	/* XXX internal apis */
4c9460
 #include "lib/rpmdb_internal.h" /* rpmdbAdd/Remove */
4c9460
+#include "lib/rpmts_internal.h" /* ts->plugins */
4c9460
 #include "lib/rpmscript.h"
4c9460
 
4c9460
+#include "lib/rpmplugins.h"
4c9460
+
4c9460
 #include "debug.h"
4c9460
 
4c9460
 typedef enum pkgStage_e {
4c9460
@@ -421,7 +424,7 @@ static rpmRC runScript(rpmpsm psm, ARGV_const_t prefixes,
4c9460
 
4c9460
     rpmswEnter(rpmtsOp(psm->ts, RPMTS_OP_SCRIPTLETS), 0);
4c9460
     rc = rpmScriptRun(script, arg1, arg2, sfd,
4c9460
-		      prefixes, warn_only, selinux);
4c9460
+		      prefixes, warn_only, selinux, psm->ts->plugins);
4c9460
     rpmswExit(rpmtsOp(psm->ts, RPMTS_OP_SCRIPTLETS), 0);
4c9460
 
4c9460
     /* Map warn-only errors to "notfound" for script stop callback */
4c9460
@@ -1033,16 +1036,23 @@ rpmRC rpmpsmRun(rpmts ts, rpmte te, pkgGoal goal)
4c9460
 	switch (goal) {
4c9460
 	case PKG_INSTALL:
4c9460
 	case PKG_ERASE:
4c9460
-	    op = (goal == PKG_INSTALL) ? RPMTS_OP_INSTALL : RPMTS_OP_ERASE;
4c9460
-	    rpmswEnter(rpmtsOp(psm->ts, op), 0);
4c9460
+	    /* Run pre transaction element hook for all plugins */
4c9460
+	    if (rpmpluginsCallPsmPre(ts->plugins, te) != RPMRC_FAIL) {
4c9460
+
4c9460
+		op = (goal == PKG_INSTALL) ? RPMTS_OP_INSTALL : RPMTS_OP_ERASE;
4c9460
+		rpmswEnter(rpmtsOp(psm->ts, op), 0);
4c9460
 
4c9460
-	    rc = rpmpsmNext(psm, PSM_INIT);
4c9460
-	    if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
4c9460
-	    if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
4c9460
-	    if (!rc) rc = rpmpsmNext(psm, PSM_POST);
4c9460
-	    (void) rpmpsmNext(psm, PSM_FINI);
4c9460
+		rc = rpmpsmNext(psm, PSM_INIT);
4c9460
+		if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
4c9460
+		if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
4c9460
+		if (!rc) rc = rpmpsmNext(psm, PSM_POST);
4c9460
+		(void) rpmpsmNext(psm, PSM_FINI);
4c9460
+
4c9460
+		rpmswExit(rpmtsOp(psm->ts, op), 0);
4c9460
+	    }
4c9460
 
4c9460
-	    rpmswExit(rpmtsOp(psm->ts, op), 0);
4c9460
+	    /* Run post transaction element hook for all plugins */
4c9460
+	    rpmpluginsCallPsmPost(ts->plugins, te, rc);
4c9460
 	    break;
4c9460
 	case PKG_PRETRANS:
4c9460
 	case PKG_POSTTRANS:
4c9460
diff --git a/lib/rpmplugins.c b/lib/rpmplugins.c
4c9460
index 9098aa5..7285f54 100644
4c9460
--- a/lib/rpmplugins.c
4c9460
+++ b/lib/rpmplugins.c
4c9460
@@ -76,16 +76,16 @@ rpmRC rpmpluginsAdd(rpmPlugins plugins, const char *name, const char *path,
4c9460
     return rpmpluginsCallInit(plugins, name, opts);
4c9460
 }
4c9460
 
4c9460
-rpmRC rpmpluginsAddCollectionPlugin(rpmPlugins plugins, const char *name)
4c9460
+rpmRC rpmpluginsAddPlugin(rpmPlugins plugins, const char *type, const char *name)
4c9460
 {
4c9460
     char *path;
4c9460
     char *options;
4c9460
     rpmRC rc = RPMRC_FAIL;
4c9460
 
4c9460
-    path = rpmExpand("%{?__collection_", name, "}", NULL);
4c9460
+    path = rpmExpand("%{?__", type, "_", name, "}", NULL);
4c9460
     if (!path || rstreq(path, "")) {
4c9460
-	rpmlog(RPMLOG_ERR, _("Failed to expand %%__collection_%s macro\n"),
4c9460
-	       name);
4c9460
+	rpmlog(RPMLOG_ERR, _("Failed to expand %%__%s_%s macro\n"),
4c9460
+	       type, name);
4c9460
 	goto exit;
4c9460
     }
4c9460
 
4c9460
@@ -195,3 +195,88 @@ rpmRC rpmpluginsCallCollectionPreRemove(rpmPlugins plugins, const char *name)
4c9460
     RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_COLL_PRE_REMOVE);
4c9460
     return hookFunc();
4c9460
 }
4c9460
+
4c9460
+rpmRC rpmpluginsCallTsmPre(rpmPlugins plugins, rpmts ts)
4c9460
+{
4c9460
+    rpmRC (*hookFunc)(rpmts);
4c9460
+    int i;
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    const char *name = NULL;
4c9460
+
4c9460
+    for (i = 0; i < plugins->count; i++) {
4c9460
+	name = plugins->names[i];
4c9460
+	RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_TSM_PRE);
4c9460
+	if (hookFunc(ts) == RPMRC_FAIL)
4c9460
+	    rc = RPMRC_FAIL;
4c9460
+    }
4c9460
+
4c9460
+    return rc;
4c9460
+}
4c9460
+
4c9460
+rpmRC rpmpluginsCallTsmPost(rpmPlugins plugins, rpmts ts, int res)
4c9460
+{
4c9460
+    rpmRC (*hookFunc)(rpmts, int);
4c9460
+    int i;
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    const char *name = NULL;
4c9460
+
4c9460
+    for (i = 0; i < plugins->count; i++) {
4c9460
+	name = plugins->names[i];
4c9460
+	RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_TSM_POST);
4c9460
+	if (hookFunc(ts, res) == RPMRC_FAIL)
4c9460
+	    rc = RPMRC_FAIL;
4c9460
+    }
4c9460
+
4c9460
+    return rc;
4c9460
+}
4c9460
+
4c9460
+rpmRC rpmpluginsCallPsmPre(rpmPlugins plugins, rpmte te)
4c9460
+{
4c9460
+    rpmRC (*hookFunc)(rpmte);
4c9460
+    int i;
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    const char *name = NULL;
4c9460
+
4c9460
+    for (i = 0; i < plugins->count; i++) {
4c9460
+	name = plugins->names[i];
4c9460
+	RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_PSM_PRE);
4c9460
+	if (hookFunc(te) == RPMRC_FAIL)
4c9460
+	    rc = RPMRC_FAIL;
4c9460
+    }
4c9460
+
4c9460
+    return rc;
4c9460
+}
4c9460
+
4c9460
+rpmRC rpmpluginsCallPsmPost(rpmPlugins plugins, rpmte te, int res)
4c9460
+{
4c9460
+    rpmRC (*hookFunc)(rpmte, int);
4c9460
+    int i;
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    const char *name = NULL;
4c9460
+
4c9460
+    for (i = 0; i < plugins->count; i++) {
4c9460
+	name = plugins->names[i];
4c9460
+	RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_PSM_POST);
4c9460
+	if (hookFunc(te, res) == RPMRC_FAIL)
4c9460
+	    rc = RPMRC_FAIL;
4c9460
+    }
4c9460
+
4c9460
+    return rc;
4c9460
+}
4c9460
+
4c9460
+rpmRC rpmpluginsCallScriptSetup(rpmPlugins plugins, char* path)
4c9460
+{
4c9460
+    rpmRC (*hookFunc)(char*);
4c9460
+    int i;
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    const char *name = NULL;
4c9460
+
4c9460
+    for (i = 0; i < plugins->count; i++) {
4c9460
+	name = plugins->names[i];
4c9460
+	RPMPLUGINS_SET_HOOK_FUNC(PLUGINHOOK_SCRIPT_SETUP);
4c9460
+	if (hookFunc(path) == RPMRC_FAIL)
4c9460
+	    rc = RPMRC_FAIL;
4c9460
+    }
4c9460
+
4c9460
+    return rc;
4c9460
+}
4c9460
diff --git a/lib/rpmplugins.h b/lib/rpmplugins.h
4c9460
index 7985559..c462eae 100644
4c9460
--- a/lib/rpmplugins.h
4c9460
+++ b/lib/rpmplugins.h
4c9460
@@ -11,11 +11,20 @@ extern "C" {
4c9460
 
4c9460
 #define PLUGINHOOK_INIT_FUNC			pluginhook_init
4c9460
 #define PLUGINHOOK_CLEANUP_FUNC			pluginhook_cleanup
4c9460
+
4c9460
 #define PLUGINHOOK_OPENTE_FUNC			pluginhook_opente
4c9460
 #define PLUGINHOOK_COLL_POST_ADD_FUNC		pluginhook_coll_post_add
4c9460
 #define PLUGINHOOK_COLL_POST_ANY_FUNC		pluginhook_coll_post_any
4c9460
 #define PLUGINHOOK_COLL_PRE_REMOVE_FUNC		pluginhook_coll_pre_remove
4c9460
 
4c9460
+#define PLUGINHOOK_TSM_PRE_FUNC        pluginhook_tsm_pre
4c9460
+#define PLUGINHOOK_TSM_POST_FUNC        pluginhook_tsm_post
4c9460
+
4c9460
+#define PLUGINHOOK_PSM_PRE_FUNC        pluginhook_psm_pre
4c9460
+#define PLUGINHOOK_PSM_POST_FUNC        pluginhook_psm_post
4c9460
+ 
4c9460
+#define PLUGINHOOK_SCRIPT_SETUP_FUNC    pluginhook_script_setup
4c9460
+
4c9460
 enum rpmPluginHook_e {
4c9460
     PLUGINHOOK_NONE		= 0,
4c9460
     PLUGINHOOK_INIT		= 1 << 0,
4c9460
@@ -23,7 +32,12 @@ enum rpmPluginHook_e {
4c9460
     PLUGINHOOK_OPENTE		= 1 << 2,
4c9460
     PLUGINHOOK_COLL_POST_ADD	= 1 << 3,
4c9460
     PLUGINHOOK_COLL_POST_ANY	= 1 << 4,
4c9460
-    PLUGINHOOK_COLL_PRE_REMOVE	= 1 << 5
4c9460
+    PLUGINHOOK_COLL_PRE_REMOVE	= 1 << 5,
4c9460
+    PLUGINHOOK_TSM_PRE         = 1 << 6,
4c9460
+    PLUGINHOOK_TSM_POST        = 1 << 7,
4c9460
+    PLUGINHOOK_PSM_PRE         = 1 << 8,
4c9460
+    PLUGINHOOK_PSM_POST        = 1 << 9,
4c9460
+    PLUGINHOOK_SCRIPT_SETUP    = 1 << 10
4c9460
 };
4c9460
 
4c9460
 typedef rpmFlags rpmPluginHook;
4c9460
@@ -53,12 +67,13 @@ rpmPlugins rpmpluginsFree(rpmPlugins plugins);
4c9460
 rpmRC rpmpluginsAdd(rpmPlugins plugins, const char *name, const char *path, const char *opts);
4c9460
 
4c9460
 /** \ingroup rpmplugins
4c9460
- * Add and open a collection plugin
4c9460
+ * Add and open a rpm plugin
4c9460
  * @param plugins	plugins structure to add a collection plugin to
4c9460
- * @param name		name of collection to open
4c9460
+ * @param type     type of plugin
4c9460
+ * @param name		name of plugin
4c9460
  * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
  */
4c9460
-rpmRC rpmpluginsAddCollectionPlugin(rpmPlugins plugins, const char *name);
4c9460
+rpmRC rpmpluginsAddPlugin(rpmPlugins plugins, const char *type, const char *name);
4c9460
 
4c9460
 /** \ingroup rpmplugins
4c9460
  * Determine if a plugin has been added already
4c9460
@@ -119,6 +134,48 @@ rpmRC rpmpluginsCallCollectionPostAny(rpmPlugins plugins, const char *name);
4c9460
  */
4c9460
 rpmRC rpmpluginsCallCollectionPreRemove(rpmPlugins plugins, const char *name);
4c9460
 
4c9460
+/** \ingroup rpmplugins
4c9460
+ * Call the pre transaction plugin hook
4c9460
+ * @param plugins	plugins structure
4c9460
+ * @param ts		processed transaction
4c9460
+ * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
+ */
4c9460
+rpmRC rpmpluginsCallTsmPre(rpmPlugins plugins, rpmts ts);
4c9460
+
4c9460
+/** \ingroup rpmplugins
4c9460
+ * Call the post transaction plugin hook
4c9460
+ * @param plugins	plugins structure
4c9460
+ * @param ts		processed transaction
4c9460
+ * @param res		transaction result code
4c9460
+ * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
+ */
4c9460
+rpmRC rpmpluginsCallTsmPost(rpmPlugins plugins, rpmts ts, int res);
4c9460
+
4c9460
+/** \ingroup rpmplugins
4c9460
+ * Call the pre transaction element plugin hook
4c9460
+ * @param plugins	plugins structure
4c9460
+ * @param te		processed transaction element
4c9460
+ * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
+ */
4c9460
+rpmRC rpmpluginsCallPsmPre(rpmPlugins plugins, rpmte te);
4c9460
+
4c9460
+/** \ingroup rpmplugins
4c9460
+ * Call the post transaction element plugin hook
4c9460
+ * @param plugins	plugins structure
4c9460
+ * @param te		processed transaction element
4c9460
+ * @param res		transaction element result code
4c9460
+ * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
+ */
4c9460
+rpmRC rpmpluginsCallPsmPost(rpmPlugins plugins, rpmte te, int res);
4c9460
+
4c9460
+/** \ingroup rpmplugins
4c9460
+ * Call the script setup plugin hook
4c9460
+ * @param plugins	plugins structure
4c9460
+ * @param path		script path
4c9460
+ * @return		RPMRC_OK on success, RPMRC_FAIL otherwise
4c9460
+ */
4c9460
+rpmRC rpmpluginsCallScriptSetup(rpmPlugins plugins, char* path);
4c9460
+
4c9460
 #ifdef __cplusplus
4c9460
 }
4c9460
 #endif
4c9460
diff --git a/lib/rpmscript.c b/lib/rpmscript.c
4c9460
index 57c24c6..f8c5fc7 100644
4c9460
--- a/lib/rpmscript.c
4c9460
+++ b/lib/rpmscript.c
4c9460
@@ -14,6 +14,8 @@
4c9460
 #include "rpmio/rpmlua.h"
4c9460
 #include "lib/rpmscript.h"
4c9460
 
4c9460
+#include "lib/rpmplugins.h"     /* rpm plugins hooks */
4c9460
+
4c9460
 #include "debug.h"
4c9460
 
4c9460
 struct rpmScript_s {
4c9460
@@ -91,7 +93,7 @@ static rpmRC runLuaScript(int selinux, ARGV_const_t prefixes,
4c9460
 
4c9460
 static const char * const SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";
4c9460
 
4c9460
-static void doScriptExec(int selinux, ARGV_const_t argv, ARGV_const_t prefixes,
4c9460
+static void doScriptExec(rpmPlugins plugins, int selinux, ARGV_const_t argv, ARGV_const_t prefixes,
4c9460
 			FD_t scriptFd, FD_t out)
4c9460
 {
4c9460
     int pipes[2];
4c9460
@@ -169,7 +171,10 @@ static void doScriptExec(int selinux, ARGV_const_t argv, ARGV_const_t prefixes,
4c9460
 	}
4c9460
 
4c9460
 	if (xx == 0) {
4c9460
-	    xx = execv(argv[0], argv);
4c9460
+	    /* Run script setup hook for all plugins */
4c9460
+	    if (rpmpluginsCallScriptSetup(plugins, argv[0]) != RPMRC_FAIL) {
4c9460
+		xx = execv(argv[0], argv);
4c9460
+	    }
4c9460
 	}
4c9460
     }
4c9460
     _exit(127); /* exit 127 for compatibility with bash(1) */
4c9460
@@ -202,7 +207,7 @@ exit:
4c9460
 /**
4c9460
  * Run an external script.
4c9460
  */
4c9460
-static rpmRC runExtScript(int selinux, ARGV_const_t prefixes,
4c9460
+static rpmRC runExtScript(rpmPlugins plugins, int selinux, ARGV_const_t prefixes,
4c9460
 		   const char *sname, rpmlogLvl lvl, FD_t scriptFd,
4c9460
 		   ARGV_t * argvp, const char *script, int arg1, int arg2)
4c9460
 {
4c9460
@@ -258,7 +263,7 @@ static rpmRC runExtScript(int selinux, ARGV_const_t prefixes,
4c9460
     } else if (pid == 0) {/* Child */
4c9460
 	rpmlog(RPMLOG_DEBUG, "%s: execv(%s) pid %d\n",
4c9460
 	       sname, *argvp[0], (unsigned)getpid());
4c9460
-	doScriptExec(selinux, *argvp, prefixes, scriptFd, out);
4c9460
+	doScriptExec(plugins, selinux, *argvp, prefixes, scriptFd, out);
4c9460
     }
4c9460
 
4c9460
     do {
4c9460
@@ -297,7 +302,7 @@ exit:
4c9460
 }
4c9460
 
4c9460
 rpmRC rpmScriptRun(rpmScript script, int arg1, int arg2, FD_t scriptFd,
4c9460
-		   ARGV_const_t prefixes, int warn_only, int selinux)
4c9460
+		   ARGV_const_t prefixes, int warn_only, int selinux, rpmPlugins plugins)
4c9460
 {
4c9460
     ARGV_t args = NULL;
4c9460
     rpmlogLvl lvl = warn_only ? RPMLOG_WARNING : RPMLOG_ERR;
4c9460
@@ -315,7 +320,7 @@ rpmRC rpmScriptRun(rpmScript script, int arg1, int arg2, FD_t scriptFd,
4c9460
     if (rstreq(args[0], "<lua>")) {
4c9460
 	rc = runLuaScript(selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
4c9460
     } else {
4c9460
-	rc = runExtScript(selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
4c9460
+	rc = runExtScript(plugins, selinux, prefixes, script->descr, lvl, scriptFd, &args, script->body, arg1, arg2);
4c9460
     }
4c9460
     argvFree(args);
4c9460
 
4c9460
diff --git a/lib/rpmscript.h b/lib/rpmscript.h
4c9460
index 7d584bc..852735b 100644
4c9460
--- a/lib/rpmscript.h
4c9460
+++ b/lib/rpmscript.h
4c9460
@@ -29,7 +29,7 @@ rpmScript rpmScriptFree(rpmScript script);
4c9460
 
4c9460
 RPM_GNUC_INTERNAL
4c9460
 rpmRC rpmScriptRun(rpmScript script, int arg1, int arg2, FD_t scriptFd,
4c9460
-                   ARGV_const_t prefixes, int warn_only, int selinux);
4c9460
+                   ARGV_const_t prefixes, int warn_only, int selinux, rpmPlugins plugins);
4c9460
 
4c9460
 RPM_GNUC_INTERNAL
4c9460
 rpmTagVal rpmScriptTag(rpmScript script);
4c9460
diff --git a/lib/rpmte.c b/lib/rpmte.c
4c9460
index 87fb391..9fc5522 100644
4c9460
--- a/lib/rpmte.c
4c9460
+++ b/lib/rpmte.c
4c9460
@@ -889,7 +889,7 @@ rpmRC rpmteSetupCollectionPlugins(rpmte te)
4c9460
     rpmteOpen(te, 0);
4c9460
     for (; colls && *colls; colls++) {
4c9460
 	if (!rpmpluginsPluginAdded(plugins, *colls)) {
4c9460
-	    rc = rpmpluginsAddCollectionPlugin(plugins, *colls);
4c9460
+	    rc = rpmpluginsAddPlugin(plugins, "collection", *colls);
4c9460
 	    if (rc != RPMRC_OK) {
4c9460
 		break;
4c9460
 	    }
4c9460
diff --git a/lib/transaction.c b/lib/transaction.c
4c9460
index 45c30b5..08a5643 100644
4c9460
--- a/lib/transaction.c
4c9460
+++ b/lib/transaction.c
4c9460
@@ -22,6 +22,8 @@
4c9460
 #include "lib/rpmts_internal.h"
4c9460
 #include "rpmio/rpmhook.h"
4c9460
 
4c9460
+#include "lib/rpmplugins.h"
4c9460
+
4c9460
 /* XXX FIXME: merge with existing (broken?) tests in system.h */
4c9460
 /* portability fiddles */
4c9460
 #if STATFS_IN_SYS_STATVFS
4c9460
@@ -1435,12 +1437,43 @@ static int rpmtsProcess(rpmts ts)
4c9460
     return rc;
4c9460
 }
4c9460
 
4c9460
+static rpmRC rpmtsSetupTransactionPlugins(rpmts ts)
4c9460
+{
4c9460
+    rpmRC rc = RPMRC_OK;
4c9460
+    char *plugins = NULL, *plugin = NULL;
4c9460
+    const char *delims = ",";
4c9460
+
4c9460
+    plugins = rpmExpand("%{?__transaction_plugins}", NULL);
4c9460
+    if (!plugins || rstreq(plugins, "")) {
4c9460
+	goto exit;
4c9460
+    }
4c9460
+
4c9460
+    plugin = strtok(plugins, delims);
4c9460
+    while(plugin != NULL) {
4c9460
+	rpmlog(RPMLOG_DEBUG, "plugin is %s\n", plugin);
4c9460
+	if (!rpmpluginsPluginAdded(ts->plugins, (const char*)plugin)) {
4c9460
+	    if (rpmpluginsAddPlugin(ts->plugins, "transaction",
4c9460
+				    (const char*)plugin) == RPMRC_FAIL) {
4c9460
+		/* any configured plugin failing to load is a failure */
4c9460
+		rc = RPMRC_FAIL;
4c9460
+	    }
4c9460
+	}
4c9460
+	plugin = strtok(NULL, delims);
4c9460
+    }
4c9460
+
4c9460
+exit:
4c9460
+    free(plugins);
4c9460
+    return rc;
4c9460
+}
4c9460
+
4c9460
 int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
4c9460
 {
4c9460
     int rc = -1; /* assume failure */
4c9460
     tsMembers tsmem = rpmtsMembers(ts);
4c9460
     rpmlock lock = NULL;
4c9460
     rpmps tsprobs = NULL;
4c9460
+    int TsmPreDone = 0; /* TsmPre hook hasn't been called */
4c9460
+    
4c9460
     /* Force default 022 umask during transaction for consistent results */
4c9460
     mode_t oldmask = umask(022);
4c9460
 
4c9460
@@ -1462,11 +1495,21 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
4c9460
 	goto exit;
4c9460
     }
4c9460
 
4c9460
+    if (rpmtsSetupTransactionPlugins(ts) == RPMRC_FAIL) {
4c9460
+	goto exit;
4c9460
+    }
4c9460
+
4c9460
     rpmtsSetupCollections(ts);
4c9460
 
4c9460
     /* Check package set for problems */
4c9460
     tsprobs = checkProblems(ts);
4c9460
 
4c9460
+    /* Run pre transaction hook for all plugins */
4c9460
+    TsmPreDone = 1;
4c9460
+    if (rpmpluginsCallTsmPre(ts->plugins, ts) == RPMRC_FAIL) {
4c9460
+	goto exit;
4c9460
+    }
4c9460
+
4c9460
     /* Run pre-transaction scripts, but only if there are no known
4c9460
      * problems up to this point and not disabled otherwise. */
4c9460
     if (!((rpmtsFlags(ts) & (RPMTRANS_FLAG_BUILD_PROBS|RPMTRANS_FLAG_NOPRE))
4c9460
@@ -1511,6 +1554,10 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
4c9460
     }
4c9460
 
4c9460
 exit:
4c9460
+    /* Run post transaction hook for all plugins */
4c9460
+    if (TsmPreDone) /* If TsmPre hook has been called, call the TsmPost hook */
4c9460
+	rpmpluginsCallTsmPost(ts->plugins, ts, rc);
4c9460
+
4c9460
     /* Finish up... */
4c9460
     (void) umask(oldmask);
4c9460
     (void) rpmtsFinish(ts);
4c9460
diff --git a/macros.in b/macros.in
4c9460
index 3aaebcd..fb030b5 100644
4c9460
--- a/macros.in
4c9460
+++ b/macros.in
4c9460
@@ -1032,6 +1032,9 @@ done \
4c9460
 %__collection_sepolicy		%{__plugindir}/sepolicy.so
4c9460
 %__collection_sepolicy_flags	1
4c9460
 
4c9460
+# Transaction plugin macros
4c9460
+%__transaction_systemd_inhibit	%{__plugindir}/systemd_inhibit.so
4c9460
+
4c9460
 #------------------------------------------------------------------------------
4c9460
 # Macros for further automated spec %setup and patch application
4c9460
 
4c9460
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
4c9460
index a9c962c..0c0a410 100644
4c9460
--- a/plugins/Makefile.am
4c9460
+++ b/plugins/Makefile.am
4c9460
@@ -24,3 +24,10 @@ sepolicy_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmi
4c9460
 
4c9460
 plugins_LTLIBRARIES += sepolicy.la
4c9460
 endif
4c9460
+
4c9460
+if DBUS
4c9460
+systemd_inhibit_la_SOURCES = systemd_inhibit.c
4c9460
+systemd_inhibit_la_CPPFLAGS = $(AM_CPPFLAGS) @DBUS_CFLAGS@
4c9460
+systemd_inhibit_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la @DBUS_LIBS@
4c9460
+plugins_LTLIBRARIES += systemd_inhibit.la
4c9460
+endif
4c9460
diff --git a/plugins/plugin.h b/plugins/plugin.h
4c9460
index 5156f93..ad4171a 100644
4c9460
--- a/plugins/plugin.h
4c9460
+++ b/plugins/plugin.h
4c9460
@@ -7,9 +7,23 @@
4c9460
 #include "lib/rpmplugins.h"
4c9460
 #include "lib/rpmchroot.h"
4c9460
 
4c9460
+/* general plugin hooks */
4c9460
 rpmRC PLUGINHOOK_INIT_FUNC(rpmts ts, const char * name, const char * opts);
4c9460
 rpmRC PLUGINHOOK_CLEANUP_FUNC(void);
4c9460
+
4c9460
+/* collection plugin hooks */
4c9460
 rpmRC PLUGINHOOK_OPENTE_FUNC(rpmte te);
4c9460
 rpmRC PLUGINHOOK_COLL_POST_ANY_FUNC(void);
4c9460
 rpmRC PLUGINHOOK_COLL_POST_ADD_FUNC(void);
4c9460
 rpmRC PLUGINHOOK_COLL_PRE_REMOVE_FUNC(void);
4c9460
+
4c9460
+/* per transaction plugin hooks */
4c9460
+rpmRC PLUGINHOOK_TSM_PRE_FUNC(rpmts ts);
4c9460
+rpmRC PLUGINHOOK_TSM_POST_FUNC(rpmts ts, int res);
4c9460
+
4c9460
+/* per transaction element plugin hooks */
4c9460
+rpmRC PLUGINHOOK_PSM_PRE_FUNC(rpmte te);
4c9460
+rpmRC PLUGINHOOK_PSM_POST_FUNC(rpmte te, int res);
4c9460
+
4c9460
+/*per script plugin hooks */
4c9460
+rpmRC PLUGINHOOK_SCRIPT_SETUP_FUNC(char* path);
4c9460
diff --git a/plugins/systemd_inhibit.c b/plugins/systemd_inhibit.c
4c9460
new file mode 100644
4c9460
index 0000000..e990bec
4c9460
--- /dev/null
4c9460
+++ b/plugins/systemd_inhibit.c
4c9460
@@ -0,0 +1,111 @@
4c9460
+#include <dbus/dbus.h>
4c9460
+#include <sys/types.h>
4c9460
+#include <sys/stat.h>
4c9460
+#include <unistd.h>
4c9460
+#include <rpm/rpmlog.h>
4c9460
+#include <rpm/rpmts.h>
4c9460
+#include "plugin.h"
4c9460
+
4c9460
+rpmPluginHook PLUGIN_HOOKS = (
4c9460
+    PLUGINHOOK_INIT |
4c9460
+    PLUGINHOOK_CLEANUP |
4c9460
+    PLUGINHOOK_TSM_PRE |
4c9460
+    PLUGINHOOK_TSM_POST
4c9460
+);
4c9460
+
4c9460
+static int lock_fd = -1;
4c9460
+
4c9460
+rpmRC PLUGINHOOK_INIT_FUNC(rpmts ts, const char *name, const char *opts)
4c9460
+{
4c9460
+    struct stat st;
4c9460
+
4c9460
+    if (lstat("/run/systemd/system/", &st) == 0) {
4c9460
+        if (S_ISDIR(st.st_mode)) {
4c9460
+            return RPMRC_OK;
4c9460
+        }
4c9460
+    }
4c9460
+
4c9460
+    return RPMRC_NOTFOUND;
4c9460
+}
4c9460
+
4c9460
+rpmRC PLUGINHOOK_CLEANUP_FUNC(void)
4c9460
+{
4c9460
+    return RPMRC_OK;
4c9460
+}
4c9460
+
4c9460
+static int inhibit(void)
4c9460
+{
4c9460
+    DBusError err;
4c9460
+    DBusConnection *bus = NULL;
4c9460
+    DBusMessage *msg = NULL;
4c9460
+    DBusMessage *reply = NULL;
4c9460
+    int fd = -1;
4c9460
+
4c9460
+    dbus_error_init(&err;;
4c9460
+    bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err;;
4c9460
+
4c9460
+    if (bus) {
4c9460
+	msg = dbus_message_new_method_call("org.freedesktop.login1",
4c9460
+					   "/org/freedesktop/login1",
4c9460
+					   "org.freedesktop.login1.Manager",
4c9460
+					   "Inhibit");
4c9460
+    }
4c9460
+
4c9460
+    if (msg) {
4c9460
+	const char *what = "shutdown";
4c9460
+	const char *mode = "block";
4c9460
+	const char *who = "RPM";
4c9460
+	const char *reason = "Transaction running";
4c9460
+
4c9460
+	dbus_message_append_args(msg,
4c9460
+				 DBUS_TYPE_STRING, &what,
4c9460
+				 DBUS_TYPE_STRING, &who,
4c9460
+				 DBUS_TYPE_STRING, &reason,
4c9460
+				 DBUS_TYPE_STRING, &mode,
4c9460
+				 DBUS_TYPE_INVALID);
4c9460
+
4c9460
+	reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err;;
4c9460
+	dbus_message_unref(msg);
4c9460
+    }
4c9460
+
4c9460
+    if (reply) {
4c9460
+	dbus_message_get_args(reply, &err,
4c9460
+			      DBUS_TYPE_UNIX_FD, &fd,
4c9460
+			      DBUS_TYPE_INVALID);
4c9460
+	dbus_message_unref(reply);
4c9460
+    }
4c9460
+    
4c9460
+    if (dbus_error_is_set(&err))
4c9460
+	dbus_error_free(&err;;
4c9460
+    if (bus)
4c9460
+	dbus_connection_close(bus);
4c9460
+
4c9460
+    return fd;
4c9460
+}
4c9460
+
4c9460
+rpmRC PLUGINHOOK_TSM_PRE_FUNC(rpmts ts)
4c9460
+{
4c9460
+    if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
4c9460
+	return RPMRC_OK;
4c9460
+
4c9460
+    lock_fd = inhibit();
4c9460
+
4c9460
+    if (lock_fd < 0) {
4c9460
+	rpmlog(RPMLOG_WARNING,
4c9460
+	       "Unable to get systemd shutdown inhibition lock\n");
4c9460
+    } else {
4c9460
+	rpmlog(RPMLOG_DEBUG, "System shutdown blocked (fd %d)\n", lock_fd);
4c9460
+    }
4c9460
+
4c9460
+    return RPMRC_OK;
4c9460
+}
4c9460
+
4c9460
+rpmRC PLUGINHOOK_TSM_POST_FUNC(rpmts ts, int res)
4c9460
+{
4c9460
+    if (lock_fd >= 0) {
4c9460
+	close(lock_fd);
4c9460
+	lock_fd = -1;
4c9460
+	rpmlog(RPMLOG_DEBUG, "System shutdown unblocked\n");
4c9460
+    }
4c9460
+    return RPMRC_OK;
4c9460
+}