diff options
Diffstat (limited to 'src/libstrongswan/utils')
-rw-r--r-- | src/libstrongswan/utils/chunk.c | 80 | ||||
-rw-r--r-- | src/libstrongswan/utils/chunk.h | 26 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src/libstrongswan/utils/chunk.c b/src/libstrongswan/utils/chunk.c index 644b8060f..5c00c5b32 100644 --- a/src/libstrongswan/utils/chunk.c +++ b/src/libstrongswan/utils/chunk.c @@ -18,6 +18,7 @@ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/mman.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> @@ -275,6 +276,85 @@ chunk_t chunk_from_fd(int fd) return chunk_clone(chunk_create(buf, total)); } +/** + * Implementation for mmap()ed chunks + */ +typedef struct { + /* public chunk interface */ + chunk_t public; + /* FD of open file */ + int fd; + /* mmap() address */ + void *map; + /* size of map */ + size_t len; +} mmaped_chunk_t; + +/** + * See header. + */ +chunk_t *chunk_map(char *path, bool wr) +{ + mmaped_chunk_t *chunk; + struct stat sb; + int tmp; + + INIT(chunk, + .fd = open(path, wr ? O_RDWR : O_RDONLY), + ); + + if (chunk->fd == -1) + { + free(chunk); + return NULL; + } + if (fstat(chunk->fd, &sb) == -1) + { + tmp = errno; + chunk_unmap(&chunk->public); + errno = tmp; + return NULL; + } + chunk->len = sb.st_size; + /* map non-empty files only, as mmap() complains otherwise */ + if (chunk->len) + { + /* in read-only mode, we allow writes, but don't sync to disk */ + chunk->map = mmap(NULL, chunk->len, PROT_READ | PROT_WRITE, + wr ? MAP_SHARED : MAP_PRIVATE, chunk->fd, 0); + if (chunk->map == MAP_FAILED) + { + tmp = errno; + chunk_unmap(&chunk->public); + errno = tmp; + return NULL; + } + } + chunk->public = chunk_create(chunk->map, chunk->len); + return &chunk->public; +} + +/** + * See header. + */ +bool chunk_unmap(chunk_t *public) +{ + mmaped_chunk_t *chunk; + bool ret = FALSE; + int tmp = 0; + + chunk = (mmaped_chunk_t*)public; + if (chunk->map && chunk->map != MAP_FAILED) + { + ret = munmap(chunk->map, chunk->len) == 0; + tmp = errno; + } + close(chunk->fd); + free(chunk); + errno = tmp; + + return ret; +} /** hex conversion digits */ static char hexdig_upper[] = "0123456789ABCDEF"; diff --git a/src/libstrongswan/utils/chunk.h b/src/libstrongswan/utils/chunk.h index 80b6237ec..92a96ffba 100644 --- a/src/libstrongswan/utils/chunk.h +++ b/src/libstrongswan/utils/chunk.h @@ -108,6 +108,32 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force chunk_t chunk_from_fd(int fd); /** + * mmap() a file to a chunk + * + * The returned chunk structure is allocated from heap, but it must be freed + * through chunk_unmap(). A user may alter the chunk ptr or len, but must pass + * the chunk pointer returned from chunk_map() to chunk_unmap() after use. + * + * On error, errno is set appropriately. + * + * @param path path of file to map + * @param wr TRUE to sync writes to disk + * @return mapped chunk, NULL on error + */ +chunk_t *chunk_map(char *path, bool wr); + +/** + * munmap() a chunk previously mapped with chunk_map() + * + * When unmapping a writeable map, the return value should be checked to + * ensure changes landed on disk. + * + * @param chunk pointer returned from chunk_map() + * @return TRUE of changes written back to file + */ +bool chunk_unmap(chunk_t *chunk); + +/** * Convert a chunk of data to hex encoding. * * The resulting string is '\\0' terminated, but the chunk does not include |