summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2017-06-17 23:36:47 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2017-06-17 23:44:22 +0200
commitf7e0065d61c788e2e3a80d6569150c54deb4099f (patch)
treebe6c794b2f99adeafa0382ee1b2c021ac065edd6
parentba7822e19ca358ef8a75b20d1774e277b4551c4e (diff)
downloadaports-cache-f7e0065d61c788e2e3a80d6569150c54deb4099f.tar.bz2
aports-cache-f7e0065d61c788e2e3a80d6569150c54deb4099f.tar.xz
redirect shell stdout to outfile
-rw-r--r--aports-cache.c22
1 files changed, 19 insertions, 3 deletions
diff --git a/aports-cache.c b/aports-cache.c
index 02899e0..2132c70 100644
--- a/aports-cache.c
+++ b/aports-cache.c
@@ -21,7 +21,7 @@
#endif
-static int spawn_shell_pipe(char *const argv[], pid_t *pid)
+static int spawn_shell_pipe(char *const argv[], pid_t *pid, int outfd)
{
int pipefd[2];
posix_spawn_file_actions_t factions;
@@ -36,6 +36,11 @@ static int spawn_shell_pipe(char *const argv[], pid_t *pid)
goto err1;
}
+ if (outfd >=0 && posix_spawn_file_actions_adddup2(&factions, outfd, STDOUT_FILENO) < 0) {
+ warn("posix_spawn_file_actions_adddup2");
+ goto err2;
+ }
+
if (posix_spawn_file_actions_adddup2(&factions, pipefd[0], 0) < 0) {
warn("posix_spawn_file_actions_adddup2");
goto err2;
@@ -142,7 +147,6 @@ static int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
if (dir == NULL)
return -1;
- debug_printf("dir opened\n");
while(1) {
char buf[PATH_MAX];
struct stat apkbuild;
@@ -192,11 +196,23 @@ int aports_cache_check(int dirfd, const char *cachefile)
int aports_cache_refresh(int dirfd, const char *cachefile, char *const shell_argv[])
{
pid_t shell_pid;
- int shellfd = spawn_shell_pipe(shell_argv, &shell_pid);
+ int shellfd, outfd = -1;
+
+ if (cachefile) {
+ outfd = open(cachefile, O_WRONLY | O_CREAT, 0660);
+ if (outfd < 0) {
+ warn("%s", cachefile);
+ return -1;
+ }
+ }
+
+ shellfd = spawn_shell_pipe(shell_argv, &shell_pid, outfd);
cache_refresh_or_check(dirfd, cachefile, shellfd);
write(shellfd, "exit\n", 5);
wait(NULL);
+ if (outfd >= 0)
+ close(outfd);
return 0;
}