[pacman-dev] [PATCH] libalpm: Add support for trigger dropins

Daan De Meyer daan.j.demeyer at gmail.com
Fri Aug 21 21:05:51 UTC 2020


In some scenarios, instead of adding their own hooks, packages want to
augment hooks already installed by other packages with extra triggers.
Currently, this requires modifying the existing hook file which is error
prone and hard to manage for package managers.

A concrete example where packages would want to extend an existing hook
is using systemd's kernel-install script on Arch Linux. An example
pacman hook for kernel-install could be the following:

```
[Trigger]
Operation = Install
Operation = Upgrade
Type = Path
Target = usr/lib/modules/*/vmlinuz
Target = usr/lib/kernel/install.d/*

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = systemd

[Action]
Description = Adding kernel and initramfs images to /boot...
When = PostTransaction
Exec = /etc/pacman.d/scripts/mkosi-kernel-add
NeedsTargets
```

This hook would run on installation and upgrades of kernel packages and
every package that installs kernel-install scripts (which includes
dracut and mkinitcpio). It's also fairly generic in that it doesn't
include specifics of other packages except its source package (and
implicitly the install directory of kernel packages).

However, both mkinitcpio and dracut use the concept of hooks that
can be installed by other packages to extend their functionality. In
mkinitcpio these are stored in /usr/lib/initcpio. When a package is
installed that installs extra hooks for mkinitcpio, the kernel-install
hook will not trigger. We can fix this by adding /usr/lib/initcpio/* to
the kernel-install pacman hook but this requires either modifying the
kernel-install hook when installing mkinitpcio or adding all possible
directories for all possible packages that want to add triggers to the
kernel-install hook in the systemd package itself. Neither of these are
attractive solutions. The first one isn't because we'd have to modify
the hook when installing packages, the second isn't because it would be
a huge maintenance burden and doesn't include for AUR or private
repository packages.

Instead, this commit adds support for trigger dropin directories that
allow extra triggers to be added in separate files. When parsing hooks,
we now also look in a .d directory for extra triggers (for
mkinitcpio.hook, we look in mkinitcpio.hook.d). Trigger files are
required to have the .trigger extension and follow the same ini format
as regular hooks files. However, only [Trigger] sections are allowed.

With this feature, packages can now install extra triggers for an
existing hook into the trigger dropin directory for that package. These
extra triggers can be tracked by the package manager and uninstalled
along with the package.

Signed-off-by: Daan De Meyer <daan.j.demeyer at gmail.com>
---
 lib/libalpm/hook.c | 149 ++++++++++++++++++++++++++++++++++++++++++---
 lib/libalpm/hook.h |   1 +
 2 files changed, 141 insertions(+), 9 deletions(-)

diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c
index aca8707e..e3d833d2 100644
--- a/lib/libalpm/hook.c
+++ b/lib/libalpm/hook.c
@@ -44,6 +44,7 @@ struct _alpm_trigger_t {
 	enum _alpm_hook_op_t op;
 	enum _alpm_trigger_type_t type;
 	alpm_list_t *targets;
+	char *name;
 };
 
 struct _alpm_hook_t {
@@ -66,6 +67,7 @@ static void _alpm_trigger_free(struct _alpm_trigger_t *trigger)
 {
 	if(trigger) {
 		FREELIST(trigger->targets);
+		free(trigger->name);
 		free(trigger);
 	}
 }
@@ -146,16 +148,16 @@ static int _alpm_hook_validate(alpm_handle_t *handle,
 	return ret;
 }
 
-static int _alpm_hook_parse_cb(const char *file, int line,
+#define error(...) _alpm_log(handle, ALPM_LOG_ERROR, __VA_ARGS__); return 1;
+#define warning(...) _alpm_log(handle, ALPM_LOG_WARNING, __VA_ARGS__);
+
+static int _alpm_trigger_parse_cb(const char *file, int line,
 		const char *section, char *key, char *value, void *data)
 {
 	struct _alpm_hook_cb_ctx *ctx = data;
 	alpm_handle_t *handle = ctx->handle;
 	struct _alpm_hook_t *hook = ctx->hook;
 
-#define error(...) _alpm_log(handle, ALPM_LOG_ERROR, __VA_ARGS__); return 1;
-#define warning(...) _alpm_log(handle, ALPM_LOG_WARNING, __VA_ARGS__);
-
 	if(!section && !key) {
 		error(_("error while reading hook %s: %s\n"), file, strerror(errno));
 	} else if(!section) {
@@ -166,8 +168,6 @@ static int _alpm_hook_parse_cb(const char *file, int line,
 			struct _alpm_trigger_t *t;
 			CALLOC(t, sizeof(struct _alpm_trigger_t), 1, return 1);
 			hook->triggers = alpm_list_add(hook->triggers, t);
-		} else if(strcmp(section, "Action") == 0) {
-			/* no special processing required */
 		} else {
 			error(_("hook %s line %d: invalid section %s\n"), file, line, section);
 		}
@@ -205,6 +205,37 @@ static int _alpm_hook_parse_cb(const char *file, int line,
 		} else {
 			error(_("hook %s line %d: invalid option %s\n"), file, line, key);
 		}
+	}
+
+	return 0;
+}
+
+static int _alpm_hook_parse_cb(const char *file, int line,
+		const char *section, char *key, char *value, void *data)
+{
+	struct _alpm_hook_cb_ctx *ctx = data;
+	alpm_handle_t *handle = ctx->handle;
+	struct _alpm_hook_t *hook = ctx->hook;
+
+	if(!section && !key) {
+		error(_("error while reading hook %s: %s\n"), file, strerror(errno));
+	} else if(!section) {
+		error(_("hook %s line %d: invalid option %s\n"), file, line, key);
+	} else if(!key) {
+		/* beginning a new section */
+		if(strcmp(section, "Trigger") == 0) {
+			if (_alpm_trigger_parse_cb(file, line, section, key, value, data) != 0) {
+				return 1;
+			}
+		} else if(strcmp(section, "Action") == 0) {
+			/* no special processing required */
+		} else {
+			error(_("hook %s line %d: invalid section %s\n"), file, line, section);
+		}
+	} else if(strcmp(section, "Trigger") == 0) {
+		if (_alpm_trigger_parse_cb(file, line, section, key, value, data) != 0) {
+			return 1;
+		}
 	} else if(strcmp(section, "Action") == 0) {
 		if(strcmp(key, "When") == 0) {
 			if(hook->when != 0) {
@@ -249,12 +280,12 @@ static int _alpm_hook_parse_cb(const char *file, int line,
 		}
 	}
 
-#undef error
-#undef warning
-
 	return 0;
 }
 
+#undef error
+#undef warning
+
 static int _alpm_hook_trigger_match_file(alpm_handle_t *handle,
 		struct _alpm_hook_t *hook, struct _alpm_trigger_t *t)
 {
@@ -466,6 +497,17 @@ static alpm_list_t *find_hook(alpm_list_t *haystack, const void *needle)
 	return NULL;
 }
 
+static alpm_list_t *find_trigger(alpm_list_t *haystack, const void *needle) {
+	while (haystack) {
+		struct _alpm_trigger_t *t = haystack->data;
+		if (t && t->name && strcmp(t->name, needle) == 0)  {
+			return haystack;
+		}
+		haystack = haystack->next;
+	}
+	return NULL;
+}
+
 static ssize_t _alpm_hook_feed_targets(char *buf, ssize_t needed, alpm_list_t **pos)
 {
 	size_t remaining = needed, written = 0;;
@@ -529,6 +571,87 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook)
 	}
 }
 
+static int _alpm_hook_parse_triggers(alpm_handle_t *handle, struct _alpm_hook_t *hook,
+		const char *hook_path)
+{
+	char path[PATH_MAX];
+	size_t dirlen;
+	struct dirent *entry;
+	DIR *d;
+	size_t suflen = strlen(ALPM_TRIGGER_SUFFIX);
+	int ret = 0;
+
+	if((dirlen = strlen(hook_path) + 3) >= PATH_MAX) {
+		_alpm_log(handle, ALPM_LOG_ERROR, _("could not open directory: %s%s: %s\n"),
+				hook_path, ".d", strerror(ENAMETOOLONG));
+		return -1;
+	}
+	sprintf(path, "%s.d/", hook_path);
+
+	if(!(d = opendir(path))) {
+		if(errno == ENOENT) {
+			return 0;
+		} else {
+			_alpm_log(handle, ALPM_LOG_ERROR,
+					_("could not open directory: %s: %s\n"), path, strerror(errno));
+			return -1;
+		}
+	}
+
+	while((errno = 0, entry = readdir(d))) {
+		struct _alpm_hook_cb_ctx ctx = { handle, hook };
+		size_t name_len;
+		struct stat buf;
+
+		if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+			continue;
+		}
+
+		if((name_len = strlen(entry->d_name)) >= PATH_MAX - dirlen) {
+			_alpm_log(handle, ALPM_LOG_ERROR, _("could not open file: %s%s: %s\n"),
+					path, entry->d_name, strerror(ENAMETOOLONG));
+			ret = -1;
+			continue;
+		}
+		memcpy(path + dirlen, entry->d_name, name_len + 1);
+
+		if(name_len < suflen
+				|| strcmp(entry->d_name + name_len - suflen, ALPM_TRIGGER_SUFFIX) != 0) {
+			_alpm_log(handle, ALPM_LOG_DEBUG, "skipping non-trigger file %s\n", path);
+			continue;
+		}
+
+		if(find_trigger(hook->triggers, entry->d_name)) {
+			_alpm_log(handle, ALPM_LOG_DEBUG, "skipping overridden trigger %s\n", path);
+			continue;
+		}
+
+		if(stat(path, &buf) != 0) {
+			_alpm_log(handle, ALPM_LOG_ERROR,
+					_("could not stat file %s: %s\n"), path, strerror(errno));
+			ret = -1;
+			continue;
+		}
+
+		if(S_ISDIR(buf.st_mode)) {
+			_alpm_log(handle, ALPM_LOG_DEBUG, "skipping directory %s\n", path);
+			continue;
+		}
+
+		if(parse_ini(path, _alpm_trigger_parse_cb, &ctx) != 0) {
+			_alpm_log(handle, ALPM_LOG_DEBUG, "parsing trigger file %s failed\n", path);
+			ret = -1;
+			continue;
+		}
+
+		STRDUP(hook->name, entry->d_name, ret = -1);
+	}
+
+	closedir(d);
+
+	return ret;
+}
+
 int _alpm_hook_run(alpm_handle_t *handle, alpm_hook_when_t when)
 {
 	alpm_event_hook_t event = { .when = when };
@@ -605,6 +728,14 @@ int _alpm_hook_run(alpm_handle_t *handle, alpm_hook_when_t when)
 			CALLOC(ctx.hook, sizeof(struct _alpm_hook_t), 1,
 					ret = -1; closedir(d); goto cleanup);
 
+			if (_alpm_hook_parse_triggers(handle, ctx.hook, path) != 0) {
+				_alpm_log(handle, ALPM_LOG_DEBUG,
+						"parsing trigger dropins for hook file %s failed\n", path);
+				_alpm_hook_free(ctx.hook);
+				ret = -1;
+				continue;
+			}
+
 			_alpm_log(handle, ALPM_LOG_DEBUG, "parsing hook file %s\n", path);
 			if(parse_ini(path, _alpm_hook_parse_cb, &ctx) != 0
 					|| _alpm_hook_validate(handle, ctx.hook, path)) {
diff --git a/lib/libalpm/hook.h b/lib/libalpm/hook.h
index ff5de4f2..4a3bc409 100644
--- a/lib/libalpm/hook.h
+++ b/lib/libalpm/hook.h
@@ -23,6 +23,7 @@
 #include "alpm.h"
 
 #define ALPM_HOOK_SUFFIX ".hook"
+#define ALPM_TRIGGER_SUFFIX ".trigger"
 
 int _alpm_hook_run(alpm_handle_t *handle, alpm_hook_when_t when);
 
-- 
2.28.0


More information about the pacman-dev mailing list