From ba7a056d586f78954ac874a3bebc6325f7e6eeb1 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Thu, 27 Oct 2011 15:47:18 -0500 Subject: Add OPEN() and CLOSE() util macros These wrap the normal open() and close() low-level I/O calls and ensure EINTR is handled correctly. Signed-off-by: Dan McGee --- lib/libalpm/add.c | 6 ++---- lib/libalpm/dload.c | 4 +--- lib/libalpm/util.c | 30 ++++++++++-------------------- lib/libalpm/util.h | 12 +++++++++++- 4 files changed, 24 insertions(+), 28 deletions(-) (limited to 'lib/libalpm') diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c index c6830dce..d66ab934 100644 --- a/lib/libalpm/add.c +++ b/lib/libalpm/add.c @@ -547,9 +547,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg, } /* save the cwd so we can restore it later */ - do { - cwdfd = open(".", O_RDONLY); - } while(cwdfd == -1 && errno == EINTR); + OPEN(cwdfd, ".", O_RDONLY); if(cwdfd < 0) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n")); } @@ -606,7 +604,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg, _alpm_log(handle, ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"), strerror(errno)); } - close(cwdfd); + CLOSE(cwdfd); } if(errors) { diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c index efd469d5..76bb00f9 100644 --- a/lib/libalpm/dload.c +++ b/lib/libalpm/dload.c @@ -306,9 +306,7 @@ static FILE *create_tempfile(struct dload_payload *payload, const char *localpat fchmod(fd, ~(_getumask()) & 0666) || !(fp = fdopen(fd, payload->tempfile_openmode))) { unlink(randpath); - if(fd >= 0) { - close(fd); - } + CLOSE(fd); _alpm_log(payload->handle, ALPM_LOG_ERROR, _("failed to create temporary file for download\n")); return NULL; diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index a60062bc..5e32280f 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -24,20 +24,14 @@ #include "config.h" -#include #include -#include -#include #include #include #include -#include #include #include #include #include -#include -#include #include #include /* setlocale */ @@ -293,9 +287,7 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix, oldmask = umask(0022); /* save the cwd so we can restore it later */ - do { - cwdfd = open(".", O_RDONLY); - } while(cwdfd == -1 && errno == EINTR); + OPEN(cwdfd, ".", O_RDONLY); if(cwdfd < 0) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n")); } @@ -367,7 +359,7 @@ cleanup: _alpm_log(handle, ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"), strerror(errno)); } - close(cwdfd); + CLOSE(cwdfd); } return ret; @@ -496,9 +488,7 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *path, char *const argv[] int retval = 0; /* save the cwd so we can restore it later */ - do { - cwdfd = open(".", O_RDONLY); - } while(cwdfd == -1 && errno == EINTR); + OPEN(cwdfd, ".", O_RDONLY); if(cwdfd < 0) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n")); } @@ -532,12 +522,12 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *path, char *const argv[] if(pid == 0) { /* this code runs for the child only (the actual chroot/exec) */ - close(1); - close(2); + 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]); + CLOSE(pipefd[0]); + CLOSE(pipefd[1]); /* use fprintf instead of _alpm_log to send output through the parent */ if(chroot(handle->root) != 0) { @@ -559,10 +549,10 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *path, char *const argv[] int status; FILE *pipe_file; - close(pipefd[1]); + CLOSE(pipefd[1]); pipe_file = fdopen(pipefd[0], "r"); if(pipe_file == NULL) { - close(pipefd[0]); + CLOSE(pipefd[0]); retval = 1; } else { while(!feof(pipe_file)) { @@ -604,7 +594,7 @@ cleanup: _alpm_log(handle, ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"), strerror(errno)); } - close(cwdfd); + CLOSE(cwdfd); } return retval; diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 8d20a281..36e4960f 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -35,10 +35,13 @@ #include #include #include /* size_t */ +#include #include /* struct stat */ -#include /* struct archive */ #include /* fabs */ #include /* DBL_EPSILON */ +#include /* open, close */ + +#include /* struct archive */ #ifdef ENABLE_NLS #include /* here so it doesn't need to be included elsewhere */ @@ -81,6 +84,13 @@ #define ALPM_BUFFER_SIZE 8192 #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +#define OPEN(fd, path, flags) do { fd = open(path, flags | O_BINARY); } while(fd == -1 && errno == EINTR) +#define CLOSE(fd) do { int ret; do { ret = close(fd); } while(ret == -1 && errno == EINTR); } while(0) + /** * Used as a buffer/state holder for _alpm_archive_fgets(). */ -- cgit v1.2.3-70-g09d2