From 9627e8157c8c916555b993d7254b0b638815ebc1 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 12 Jan 2009 19:57:01 +0100 Subject: [PATCH] Improve memory mapping. para_mmap(): Do not exit on errors. Fix the only caller in vss.c accordingly. mmap_full_file(): Use para_mmap(). --- error.h | 2 -- fd.c | 39 ++++++++++++++++++--------------------- fd.h | 3 ++- vss.c | 6 ++++-- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/error.h b/error.h index 897aeb28..2a8a6b5a 100644 --- a/error.h +++ b/error.h @@ -358,8 +358,6 @@ extern const char **para_errlist[]; #define FD_ERRORS \ PARA_ERROR(FGETS, "fgets error"), \ - PARA_ERROR(EMPTY, "file empty"), \ - PARA_ERROR(MMAP, "mmap error"), \ #define WRITE_ERRORS \ diff --git a/fd.c b/fd.c index 6d32e93e..8759d509 100644 --- a/fd.c +++ b/fd.c @@ -225,19 +225,26 @@ again: * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE. * \param fd The file to mmap from. * \param offset Mmap start. + * \param map Result pointer. * - * \return This function either returns a valid pointer to the mapped area - * or calls exit() on errors. + * \return Standard. + * + * \sa mmap(2). */ -void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset) +int para_mmap(size_t length, int prot, int flags, int fd, off_t offset, + void *map) { - void *ret = mmap(NULL, length, prot, flags, fd, offset); - if (ret != MAP_FAILED) - return ret; - PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno)); - PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n", - length, flags, fd, (size_t)offset); - exit(EXIT_FAILURE); + void **m = map; + + errno = EINVAL; + if (!length) + goto err; + *m = mmap(NULL, length, prot, flags, fd, offset); + if (*m != MAP_FAILED) + return 1; +err: + *m = NULL; + return -ERRNO_TO_PARA_ERROR(errno); } /** @@ -398,17 +405,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, goto out; } *size = file_status.st_size; - ret = -E_EMPTY; - PARA_DEBUG_LOG("%s: size %zu\n", path, *size); - if (!*size) - goto out; - *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0); - if (*map == MAP_FAILED) { - *map = NULL; - ret = -E_MMAP; - goto out; - } - ret = 1; + ret = para_mmap(*size, mmap_prot, mmap_flags, fd, 0, map); out: if (ret < 0 || !fd_ptr) close(fd); diff --git a/fd.h b/fd.h index 041a7732..7c364120 100644 --- a/fd.h +++ b/fd.h @@ -14,7 +14,8 @@ __must_check int mark_fd_nonblocking(int fd); __must_check int mark_fd_blocking(int fd); void para_fd_set(int fd, fd_set *fds, int *max_fileno); __must_check int para_fgets(char *line, int size, FILE *f); -void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset); +int para_mmap(size_t length, int prot, int flags, int fd, off_t offset, + void *map); int para_open(const char *path, int flags, mode_t mode); int para_opendir(const char *dirname, DIR **dir, int *cwd); int para_mkdir(const char *path, mode_t mode); diff --git a/vss.c b/vss.c index c1189702..d2053493 100644 --- a/vss.c +++ b/vss.c @@ -382,8 +382,10 @@ static void recv_afs_result(struct vss_task *vsst) } mmd->size = statbuf.st_size; mmd->mtime = statbuf.st_mtime; - vsst->map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, - passed_fd, 0); + ret = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE, passed_fd, + 0, &vsst->map); + if (ret < 0) + goto err; close(passed_fd); mmd->chunks_sent = 0; mmd->current_chunk = 0; -- 2.39.5