summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Spooren <mail@aparcar.org>2021-12-18 17:42:13 +0100
committerTimo Teräs <timo.teras@iki.fi>2021-12-29 09:50:50 +0200
commitf2a595913960a5a68806d4e32f1b849bfdce6933 (patch)
treee792233983fddbabf9e1b73f133542bcfb4e91f9
parent2da3f347d8851f6344ea6dba47676e488084debf (diff)
downloadapk-tools-f2a595913960a5a68806d4e32f1b849bfdce6933.tar.gz
apk-tools-f2a595913960a5a68806d4e32f1b849bfdce6933.tar.bz2
apk-tools-f2a595913960a5a68806d4e32f1b849bfdce6933.tar.xz
apk-tools-f2a595913960a5a68806d4e32f1b849bfdce6933.zip
apk: add --preserve-env option
This options is useful for (post)install scripts to run with the same environment variables as apk is executed. [TT: minor stylistic changes] Signed-off-by: Paul Spooren <mail@aparcar.org>
-rw-r--r--doc/apk.8.scd3
-rw-r--r--src/apk.c4
-rw-r--r--src/apk_context.h1
-rw-r--r--src/database.c4
4 files changed, 10 insertions, 2 deletions
diff --git a/doc/apk.8.scd b/doc/apk.8.scd
index 2a096f7..7b104a5 100644
--- a/doc/apk.8.scd
+++ b/doc/apk.8.scd
@@ -150,6 +150,9 @@ The following options are available for all commands.
*--no-progress*
Disable progress bar even for TTYs.
+*--preserve-env*
+ Pass user environment down to scripts.
+
*--print-arch*
Print default arch and exit.
diff --git a/src/apk.c b/src/apk.c
index da5fdd7..34f5c72 100644
--- a/src/apk.c
+++ b/src/apk.c
@@ -74,6 +74,7 @@ static void version(struct apk_out *out, const char *prefix)
OPT(OPT_GLOBAL_no_logfile, "no-logfile") \
OPT(OPT_GLOBAL_no_network, "no-network") \
OPT(OPT_GLOBAL_no_progress, "no-progress") \
+ OPT(OPT_GLOBAL_preserve_env, "preserve-env") \
OPT(OPT_GLOBAL_print_arch, "print-arch") \
OPT(OPT_GLOBAL_progress, "progress") \
OPT(OPT_GLOBAL_progress_fd, APK_OPT_ARG "progress-fd") \
@@ -155,6 +156,9 @@ static int option_parse_global(void *ctx, struct apk_ctx *ac, int opt, const cha
case OPT_GLOBAL_interactive:
ac->flags |= APK_INTERACTIVE;
break;
+ case OPT_GLOBAL_preserve_env:
+ ac->flags |= APK_PRESERVE_ENV;
+ break;
case OPT_GLOBAL_progress:
ac->progress.out = &ac->out;
break;
diff --git a/src/apk_context.h b/src/apk_context.h
index 5071404..ac0d3d3 100644
--- a/src/apk_context.h
+++ b/src/apk_context.h
@@ -29,6 +29,7 @@
#define APK_NO_COMMIT_HOOKS BIT(10)
#define APK_NO_CHROOT BIT(11)
#define APK_NO_LOGFILE BIT(12)
+#define APK_PRESERVE_ENV BIT(13)
#define APK_FORCE_OVERWRITE BIT(0)
#define APK_FORCE_OLD_APK BIT(1)
diff --git a/src/database.c b/src/database.c
index be7a0a1..b0c00aa 100644
--- a/src/database.c
+++ b/src/database.c
@@ -1873,7 +1873,7 @@ int apk_db_run_script(struct apk_database *db, char *fn, char **argv)
struct apk_out *out = &db->ctx->out;
int status;
pid_t pid;
- static char * const environment[] = {
+ static char * const clean_environment[] = {
"PATH=/usr/sbin:/usr/bin:/sbin:/bin",
NULL
};
@@ -1896,7 +1896,7 @@ int apk_db_run_script(struct apk_database *db, char *fn, char **argv)
exit(127);
}
- execve(fn, argv, environment);
+ execve(fn, argv, (db->ctx->flags & APK_PRESERVE_ENV) ? environ : clean_environment);
exit(127); /* should not get here */
}
while (waitpid(pid, &status, 0) < 0 && errno == EINTR);