chantra / rpms / rpm

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