diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-06-21 15:27:57 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-06-21 15:27:57 +0100 |
commit | f51a136f7d4d7e82ebf7f75cd0062a1439651e28 (patch) | |
tree | c218f8b5c3f758563fe45d09c9c661332b34da74 /src | |
parent | afbf09c1fd7fa971dcfdd4c97c8a7ebecda40da3 (diff) | |
download | rspamd-f51a136f7d4d7e82ebf7f75cd0062a1439651e28.tar.gz rspamd-f51a136f7d4d7e82ebf7f75cd0062a1439651e28.zip |
[Feature] Add utility to map shared memory segments
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/util.c | 41 | ||||
-rw-r--r-- | src/libutil/util.h | 10 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c index cf6101e4d..f3effe5cf 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2276,3 +2276,44 @@ rspamd_file_xmap (const char *fname, guint mode, return map; } + + +gpointer +rspamd_shmem_xmap (const char *fname, guint mode, + gsize *size) +{ + gint fd; + struct stat sb; + gpointer map; + + g_assert (fname != NULL); + g_assert (size != NULL); + + if (mode & PROT_WRITE) { + fd = shm_open (fname, O_RDWR, 0); + } + else { + fd = shm_open (fname, O_RDONLY, 0); + } + + if (fd == -1) { + return NULL; + } + + if (fstat (fd, &sb) == -1) { + close (fd); + + return NULL; + } + + map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0); + close (fd); + + if (map == MAP_FAILED) { + return NULL; + } + + *size = sb.st_size; + + return map; +} diff --git a/src/libutil/util.h b/src/libutil/util.h index 0c293ccbe..2497d538f 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -463,4 +463,14 @@ int rspamd_file_xopen (const char *fname, int oflags, guint mode); gpointer rspamd_file_xmap (const char *fname, guint mode, gsize *size); +/** + * Map named shared memory segment + * @param fname filename + * @param mode mode to open + * @param size target size (must NOT be NULL) + * @return pointer to memory (should be freed using munmap) or NULL in case of error + */ +gpointer rspamd_shmem_xmap (const char *fname, guint mode, + gsize *size); + #endif |