summaryrefslogtreecommitdiff
path: root/src/database.c
diff options
context:
space:
mode:
authorHenrik Riomar <henrik.riomar@gmail.com>2017-02-03 00:37:23 +0100
committerTimo Teräs <timo.teras@iki.fi>2017-02-15 13:44:04 +0200
commit349c61c9612a328f3a80f301d37aa8c14adcb43a (patch)
treefb779b62735e2cb47ed9f1d763bc1451ed19bcd9 /src/database.c
parent28a9dcda568c575c569ffa4b68775034b655230a (diff)
downloadapk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.gz
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.bz2
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.tar.xz
apk-tools-349c61c9612a328f3a80f301d37aa8c14adcb43a.zip
add support for pre and post commit hooks
This allows for instance integration of etckeeper [TT: Reorganized code a bit, and modified to use single directory commit_hooks.d with argument for script of stage.]
Diffstat (limited to 'src/database.c')
-rw-r--r--src/database.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/database.c b/src/database.c
index 31ac3e4..0de7490 100644
--- a/src/database.c
+++ b/src/database.c
@@ -13,6 +13,7 @@
#include <stdio.h>
#include <fcntl.h>
#include <mntent.h>
+#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#include <malloc.h>
@@ -1877,6 +1878,34 @@ int apk_db_fire_triggers(struct apk_database *db)
return db->pending_triggers;
}
+int apk_db_run_script(struct apk_database *db, char *fn, char **argv)
+{
+ int status;
+ pid_t pid;
+ static char * const environment[] = {
+ "PATH=/usr/sbin:/usr/bin:/sbin:/bin",
+ NULL
+ };
+
+ pid = fork();
+ if (pid == -1) {
+ apk_error("%s: fork: %s", basename(fn), strerror(errno));
+ return -2;
+ }
+ if (pid == 0) {
+ umask(0022);
+ if (fchdir(db->root_fd) == 0 && chroot(".") == 0)
+ execve(fn, argv, environment);
+ exit(127); /* should not get here */
+ }
+ waitpid(pid, &status, 0);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ apk_error("%s: script exited with error %d", basename(fn), WEXITSTATUS(status));
+ return -1;
+ }
+ return 0;
+}
+
static int update_permissions(apk_hash_item item, void *ctx)
{
struct apk_database *db = (struct apk_database *) ctx;