summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/util.c')
-rw-r--r--lib/libalpm/util.c118
1 files changed, 73 insertions, 45 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 32eaa442..8a1f3924 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -143,7 +143,15 @@ int _alpm_copyfile(const char *src, const char *dest)
/* do the actual file copy */
while((len = fread(buf, 1, CPBUFSIZE, in))) {
- fwrite(buf, 1, len, out);
+ size_t nwritten = 0;
+ nwritten = fwrite(buf, 1, len, out);
+ if((nwritten != len) || ferror(out)) {
+ pm_errno = PM_ERR_WRITE;
+ _alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
+ dest, strerror(errno));
+ ret = -1;
+ goto cleanup;
+ }
}
/* chmod dest to permissions of src, as long as it is not a symlink */
@@ -364,8 +372,8 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int
cleanup:
umask(oldmask);
archive_read_finish(_archive);
- if(restore_cwd) {
- chdir(cwd);
+ if(restore_cwd && chdir(cwd) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
}
return(ret);
}
@@ -398,7 +406,7 @@ int _alpm_rmrf(const char *path)
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if(dp->d_ino) {
sprintf(name, "%s/%s", path, dp->d_name);
- if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) {
+ if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
errflag += _alpm_rmrf(name);
}
}
@@ -444,10 +452,11 @@ int _alpm_logaction(int usesyslog, FILE *f, const char *fmt, va_list args)
return(ret);
}
-int _alpm_run_chroot(const char *root, const char *cmd)
+int _alpm_run_chroot(const char *root, const char *path, char *const argv[])
{
char cwd[PATH_MAX];
pid_t pid;
+ int pipefd[2];
int restore_cwd = 0;
int retval = 0;
@@ -466,11 +475,17 @@ int _alpm_run_chroot(const char *root, const char *cmd)
goto cleanup;
}
- _alpm_log(PM_LOG_DEBUG, "executing \"%s\" under chroot \"%s\"\n", cmd, root);
+ _alpm_log(PM_LOG_DEBUG, "executing \"%s\" under chroot \"%s\"\n", path, root);
/* Flush open fds before fork() to avoid cloning buffers */
fflush(NULL);
+ if(pipe(pipefd) == -1) {
+ _alpm_log(PM_LOG_ERROR, _("could not create pipe (%s)\n"), strerror(errno));
+ retval = 1;
+ goto cleanup;
+ }
+
/* fork- parent and child each have seperate code blocks below */
pid = fork();
if(pid == -1) {
@@ -480,62 +495,74 @@ int _alpm_run_chroot(const char *root, const char *cmd)
}
if(pid == 0) {
- FILE *pipe;
/* this code runs for the child only (the actual chroot/exec) */
- _alpm_log(PM_LOG_DEBUG, "chrooting in %s\n", root);
+ close(1);
+ close(2);
+ while(dup2(pipefd[1], 1) == -1 && errno == EINTR);
+ while(dup2(pipefd[1], 2) == -1 && errno == EINTR);
+ close(pipefd[0]);
+ close(pipefd[1]);
+
+ /* use fprintf instead of _alpm_log to send output through the parent */
if(chroot(root) != 0) {
- _alpm_log(PM_LOG_ERROR, _("could not change the root directory (%s)\n"),
- strerror(errno));
+ fprintf(stderr, _("could not change the root directory (%s)\n"), strerror(errno));
exit(1);
}
if(chdir("/") != 0) {
- _alpm_log(PM_LOG_ERROR, _("could not change directory to / (%s)\n"),
- strerror(errno));
+ fprintf(stderr, _("could not change directory to / (%s)\n"), strerror(errno));
exit(1);
}
umask(0022);
- pipe = popen(cmd, "r");
- if(!pipe) {
- _alpm_log(PM_LOG_ERROR, _("call to popen failed (%s)\n"),
- strerror(errno));
- exit(1);
- }
- while(!feof(pipe)) {
- char line[PATH_MAX];
- if(fgets(line, PATH_MAX, pipe) == NULL)
- break;
- alpm_logaction("%s", line);
- EVENT(handle->trans, PM_TRANS_EVT_SCRIPTLET_INFO, line, NULL);
- }
- retval = pclose(pipe);
- exit(WEXITSTATUS(retval));
+ execv(path, argv);
+ fprintf(stderr, _("call to execv failed (%s)\n"), strerror(errno));
+ exit(1);
} else {
/* this code runs for the parent only (wait on the child) */
- pid_t retpid;
int status;
- do {
- retpid = waitpid(pid, &status, 0);
- } while(retpid == -1 && errno == EINTR);
- if(retpid == -1) {
- _alpm_log(PM_LOG_ERROR, _("call to waitpid failed (%s)\n"),
- strerror(errno));
+ FILE *pipe;
+
+ close(pipefd[1]);
+ pipe = fdopen(pipefd[0], "r");
+ if(pipe == NULL) {
+ close(pipefd[0]);
retval = 1;
- goto cleanup;
} else {
- /* check the return status, make sure it is 0 (success) */
- if(WIFEXITED(status)) {
- _alpm_log(PM_LOG_DEBUG, "call to waitpid succeeded\n");
- if(WEXITSTATUS(status) != 0) {
- _alpm_log(PM_LOG_ERROR, _("command failed to execute correctly\n"));
- retval = 1;
- }
+ while(!feof(pipe)) {
+ char line[PATH_MAX];
+ if(fgets(line, PATH_MAX, pipe) == NULL)
+ break;
+ alpm_logaction("%s", line);
+ EVENT(handle->trans, PM_TRANS_EVT_SCRIPTLET_INFO, line, NULL);
+ }
+ fclose(pipe);
+ }
+
+ while(waitpid(pid, &status, 0) == -1) {
+ if(errno != EINTR) {
+ _alpm_log(PM_LOG_ERROR, _("call to waitpid failed (%s)\n"), strerror(errno));
+ retval = 1;
+ goto cleanup;
+ }
+ }
+
+ /* report error from above after the child has exited */
+ if(retval != 0) {
+ _alpm_log(PM_LOG_ERROR, _("could not open pipe (%s)\n"), strerror(errno));
+ goto cleanup;
+ }
+ /* check the return status, make sure it is 0 (success) */
+ if(WIFEXITED(status)) {
+ _alpm_log(PM_LOG_DEBUG, "call to waitpid succeeded\n");
+ if(WEXITSTATUS(status) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("command failed to execute correctly\n"));
+ retval = 1;
}
}
}
cleanup:
- if(restore_cwd) {
- chdir(cwd);
+ if(restore_cwd && chdir(cwd) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
}
return(retval);
@@ -551,7 +578,8 @@ int _alpm_ldconfig(const char *root)
if(access(line, F_OK) == 0) {
snprintf(line, PATH_MAX, "%ssbin/ldconfig", root);
if(access(line, X_OK) == 0) {
- _alpm_run_chroot(root, "/sbin/ldconfig");
+ char *argv[] = { "ldconfig", NULL };
+ _alpm_run_chroot(root, "/sbin/ldconfig", argv);
}
}