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 <dan@archlinux.org>
This commit is contained in:
parent
a4ce3edf95
commit
ba7a056d58
5 changed files with 28 additions and 29 deletions
|
@ -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 */
|
/* save the cwd so we can restore it later */
|
||||||
do {
|
OPEN(cwdfd, ".", O_RDONLY);
|
||||||
cwdfd = open(".", O_RDONLY);
|
|
||||||
} while(cwdfd == -1 && errno == EINTR);
|
|
||||||
if(cwdfd < 0) {
|
if(cwdfd < 0) {
|
||||||
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
|
_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,
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
||||||
_("could not restore working directory (%s)\n"), strerror(errno));
|
_("could not restore working directory (%s)\n"), strerror(errno));
|
||||||
}
|
}
|
||||||
close(cwdfd);
|
CLOSE(cwdfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(errors) {
|
if(errors) {
|
||||||
|
|
|
@ -306,9 +306,7 @@ static FILE *create_tempfile(struct dload_payload *payload, const char *localpat
|
||||||
fchmod(fd, ~(_getumask()) & 0666) ||
|
fchmod(fd, ~(_getumask()) & 0666) ||
|
||||||
!(fp = fdopen(fd, payload->tempfile_openmode))) {
|
!(fp = fdopen(fd, payload->tempfile_openmode))) {
|
||||||
unlink(randpath);
|
unlink(randpath);
|
||||||
if(fd >= 0) {
|
CLOSE(fd);
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
_alpm_log(payload->handle, ALPM_LOG_ERROR,
|
_alpm_log(payload->handle, ALPM_LOG_ERROR,
|
||||||
_("failed to create temporary file for download\n"));
|
_("failed to create temporary file for download\n"));
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -24,20 +24,14 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <locale.h> /* setlocale */
|
#include <locale.h> /* setlocale */
|
||||||
|
|
||||||
|
@ -293,9 +287,7 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
|
||||||
oldmask = umask(0022);
|
oldmask = umask(0022);
|
||||||
|
|
||||||
/* save the cwd so we can restore it later */
|
/* save the cwd so we can restore it later */
|
||||||
do {
|
OPEN(cwdfd, ".", O_RDONLY);
|
||||||
cwdfd = open(".", O_RDONLY);
|
|
||||||
} while(cwdfd == -1 && errno == EINTR);
|
|
||||||
if(cwdfd < 0) {
|
if(cwdfd < 0) {
|
||||||
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
|
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
|
||||||
}
|
}
|
||||||
|
@ -367,7 +359,7 @@ cleanup:
|
||||||
_alpm_log(handle, ALPM_LOG_ERROR,
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
||||||
_("could not restore working directory (%s)\n"), strerror(errno));
|
_("could not restore working directory (%s)\n"), strerror(errno));
|
||||||
}
|
}
|
||||||
close(cwdfd);
|
CLOSE(cwdfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -496,9 +488,7 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *path, char *const argv[]
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
/* save the cwd so we can restore it later */
|
/* save the cwd so we can restore it later */
|
||||||
do {
|
OPEN(cwdfd, ".", O_RDONLY);
|
||||||
cwdfd = open(".", O_RDONLY);
|
|
||||||
} while(cwdfd == -1 && errno == EINTR);
|
|
||||||
if(cwdfd < 0) {
|
if(cwdfd < 0) {
|
||||||
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
|
_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) {
|
if(pid == 0) {
|
||||||
/* this code runs for the child only (the actual chroot/exec) */
|
/* this code runs for the child only (the actual chroot/exec) */
|
||||||
close(1);
|
CLOSE(1);
|
||||||
close(2);
|
CLOSE(2);
|
||||||
while(dup2(pipefd[1], 1) == -1 && errno == EINTR);
|
while(dup2(pipefd[1], 1) == -1 && errno == EINTR);
|
||||||
while(dup2(pipefd[1], 2) == -1 && errno == EINTR);
|
while(dup2(pipefd[1], 2) == -1 && errno == EINTR);
|
||||||
close(pipefd[0]);
|
CLOSE(pipefd[0]);
|
||||||
close(pipefd[1]);
|
CLOSE(pipefd[1]);
|
||||||
|
|
||||||
/* use fprintf instead of _alpm_log to send output through the parent */
|
/* use fprintf instead of _alpm_log to send output through the parent */
|
||||||
if(chroot(handle->root) != 0) {
|
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;
|
int status;
|
||||||
FILE *pipe_file;
|
FILE *pipe_file;
|
||||||
|
|
||||||
close(pipefd[1]);
|
CLOSE(pipefd[1]);
|
||||||
pipe_file = fdopen(pipefd[0], "r");
|
pipe_file = fdopen(pipefd[0], "r");
|
||||||
if(pipe_file == NULL) {
|
if(pipe_file == NULL) {
|
||||||
close(pipefd[0]);
|
CLOSE(pipefd[0]);
|
||||||
retval = 1;
|
retval = 1;
|
||||||
} else {
|
} else {
|
||||||
while(!feof(pipe_file)) {
|
while(!feof(pipe_file)) {
|
||||||
|
@ -604,7 +594,7 @@ cleanup:
|
||||||
_alpm_log(handle, ALPM_LOG_ERROR,
|
_alpm_log(handle, ALPM_LOG_ERROR,
|
||||||
_("could not restore working directory (%s)\n"), strerror(errno));
|
_("could not restore working directory (%s)\n"), strerror(errno));
|
||||||
}
|
}
|
||||||
close(cwdfd);
|
CLOSE(cwdfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
|
|
|
@ -35,10 +35,13 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h> /* size_t */
|
#include <stddef.h> /* size_t */
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h> /* struct stat */
|
#include <sys/stat.h> /* struct stat */
|
||||||
#include <archive.h> /* struct archive */
|
|
||||||
#include <math.h> /* fabs */
|
#include <math.h> /* fabs */
|
||||||
#include <float.h> /* DBL_EPSILON */
|
#include <float.h> /* DBL_EPSILON */
|
||||||
|
#include <fcntl.h> /* open, close */
|
||||||
|
|
||||||
|
#include <archive.h> /* struct archive */
|
||||||
|
|
||||||
#ifdef ENABLE_NLS
|
#ifdef ENABLE_NLS
|
||||||
#include <libintl.h> /* here so it doesn't need to be included elsewhere */
|
#include <libintl.h> /* here so it doesn't need to be included elsewhere */
|
||||||
|
@ -81,6 +84,13 @@
|
||||||
#define ALPM_BUFFER_SIZE 8192
|
#define ALPM_BUFFER_SIZE 8192
|
||||||
#endif
|
#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().
|
* Used as a buffer/state holder for _alpm_archive_fgets().
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -203,11 +203,14 @@ static int download_with_xfercommand(const char *url, const char *localpath,
|
||||||
cleanup:
|
cleanup:
|
||||||
/* restore the old cwd if we have it */
|
/* restore the old cwd if we have it */
|
||||||
if(cwdfd >= 0) {
|
if(cwdfd >= 0) {
|
||||||
|
int ret;
|
||||||
if(fchdir(cwdfd) != 0) {
|
if(fchdir(cwdfd) != 0) {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"),
|
pm_printf(ALPM_LOG_ERROR, _("could not restore working directory (%s)\n"),
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
close(cwdfd);
|
do {
|
||||||
|
ret = close(cwdfd);
|
||||||
|
} while(ret == -1 && errno == EINTR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ret == -1) {
|
if(ret == -1) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue