* \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);
}
/**
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);
__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);
}
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;