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>
cute-signatures
Paul Spooren 2021-12-18 17:42:13 +01:00 committed by Timo Teräs
parent 2da3f347d8
commit f2a5959139
4 changed files with 10 additions and 2 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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)

View File

@ -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);