aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c39
-rw-r--r--src/common/compat.h7
-rw-r--r--src/common/crypto.c2
-rw-r--r--src/common/util.c7
-rw-r--r--src/common/util.h3
5 files changed, 37 insertions, 21 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 6fb15e3794..8e301a0edd 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -113,14 +113,19 @@ const char compat_c_id[] =
#endif
#ifdef HAVE_SYS_MMAN_H
+typedef struct tor_mmap_impl_t {
+ tor_mmap_t base;
+ size_t mapping_size; /**< Size of the actual mapping. (This is this file
+ * size, rounded up to the nearest page.) */
+} tor_mmap_impl_t;
tor_mmap_t *
tor_mmap_file(const char *filename)
{
int fd; /* router file */
char *string;
int page_size;
- tor_mmap_t *res;
- size_t size;
+ tor_mmap_impl_t *res;
+ size_t size, filesize;
tor_assert(filename);
@@ -130,7 +135,7 @@ tor_mmap_file(const char *filename)
return NULL;
}
- size = lseek(fd, 0, SEEK_END);
+ size = filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
/* ensure page alignment */
page_size = getpagesize();
@@ -146,28 +151,31 @@ tor_mmap_file(const char *filename)
close(fd);
- res = tor_malloc_zero(sizeof(tor_mmap_t));
- res->data = string;
- res->size = size;
+ res = tor_malloc_zero(sizeof(tor_mmap_impl_t));
+ res->base.data = string;
+ res->base.size = filesize;
+ res->mapping_size = size;
- return res;
+ return &(res->base);
}
void
tor_munmap_file(tor_mmap_t *handle)
{
- munmap((char*)handle->data, handle->size);
- tor_free(handle);
+ tor_mmap_impl_t *h = (tor_mmap_impl_t*)
+ (((char*)handle) - STRUCT_OFFSET(tor_mmap_impl_t, base));
+ munmap((char*)h->base.data, h->mapping_size);
+ tor_free(h);
}
#elif defined(MS_WINDOWS)
typedef struct win_mmap_t {
tor_mmap_t base;
HANDLE file_handle;
HANDLE mmap_handle;
-} tor_mmap_impl_t;
+} win_mmap_t;
tor_mmap_t *
tor_mmap_file(const char *filename)
{
- struct win_mmap_t *res = tor_malloc_zero(sizeof(struct win_mmap_t));
+ win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
res->mmap_handle = res->file_handle = INVALID_HANDLE_VALUE;
res->file_handle = CreateFile(filename,
@@ -208,8 +216,8 @@ tor_mmap_file(const char *filename)
void
tor_munmap_file(tor_mmap_t *handle)
{
- struct win_mmap_t *h = (struct win_mmap_t*)
- (((char*)handle) - STRUCT_OFFSET(struct win_mmap_t, base));
+ win_mmap_t *h = (win_mmap_t*)
+ (((char*)handle) - STRUCT_OFFSET(win_mmap_t, base));
if (handle->data)
/*this is an ugly cast, but without it, "data" in struct tor_mmap_t would
@@ -226,13 +234,14 @@ tor_munmap_file(tor_mmap_t *handle)
tor_mmap_t *
tor_mmap_file(const char *filename)
{
- char *res = read_file_to_str(filename, 1);
+ size_t size;
+ char *res = read_file_to_str(filename, 1, &size);
tor_mmap_t *handle;
if (! res)
return NULL;
handle = tor_malloc_zero(sizeof(tor_mmap_t));
handle->data = res;
- handle->size = strlen(res) + 1;
+ handle->size = size;
return handle;
}
void
diff --git a/src/common/compat.h b/src/common/compat.h
index 5567f5e9c1..79825fc057 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -129,10 +129,11 @@ size_t strlcpy(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
#define U64_LITERAL(n) (n ## llu)
#endif
-/** Opaque bookkeeping type used for mmap accounting. */
+/** Represents an mmaped file. Allocated via tor_mmap_file; freed with
+ * tor_munmap_file. */
typedef struct tor_mmap_t {
- const char *data;
- size_t size;
+ const char *data; /**< Mapping of the file's contents. */
+ size_t size; /**< Size of the file. */
} tor_mmap_t;
tor_mmap_t *tor_mmap_file(const char *filename) ATTR_NONNULL((1));
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 9aea7506ae..c7e5d4587f 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -460,7 +460,7 @@ crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env,
int r;
/* Read the file into a string. */
- contents = read_file_to_str(keyfile, 0);
+ contents = read_file_to_str(keyfile, 0, NULL);
if (!contents) {
log_warn(LD_CRYPTO, "Error reading private key from \"%s\"", keyfile);
return -1;
diff --git a/src/common/util.c b/src/common/util.c
index 50211917b3..d5a71d2743 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1288,6 +1288,9 @@ append_bytes_to_file(const char *fname, const char *str, size_t len,
/** Read the contents of <b>filename</b> into a newly allocated
* string; return the string on success or NULL on failure.
+ *
+ * If <b>size_out</b> is provided, store the length of the result in
+ * <b>size_out</b>
*/
/*
* This function <em>may</em> return an erroneous result if the file
@@ -1297,7 +1300,7 @@ append_bytes_to_file(const char *fname, const char *str, size_t len,
* be truncated.
*/
char *
-read_file_to_str(const char *filename, int bin)
+read_file_to_str(const char *filename, int bin, size_t *size_out)
{
int fd; /* router file */
struct stat statbuf;
@@ -1351,6 +1354,8 @@ read_file_to_str(const char *filename, int bin)
}
#endif
close(fd);
+ if (size_out)
+ *size_out = (size_t) r;
return string;
}
diff --git a/src/common/util.h b/src/common/util.h
index 082e9db51a..13d51b4413 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -180,7 +180,8 @@ int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
int append_bytes_to_file(const char *fname, const char *str, size_t len,
int bin);
-char *read_file_to_str(const char *filename, int bin) ATTR_MALLOC;
+char *read_file_to_str(const char *filename, int bin, size_t *size_out)
+ ATTR_MALLOC;
char *parse_line_from_str(char *line, char **key_out, char **value_out);
char *expand_filename(const char *filename);
struct smartlist_t *tor_listdir(const char *dirname);