summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2010-03-22 08:27:58 +0100
committerStefan Metzmacher <metze@samba.org>2010-03-22 17:15:10 +0100
commit01f2c023f7d2a4b0e016676638a062a5ba29ec0b (patch)
tree3aec66024fd8a61aebd50b6d6824fff3581c883c
parent13400a6589a20452097bc338fa742d834bbd6a34 (diff)
downloadsamba-01f2c023f7d2a4b0e016676638a062a5ba29ec0b.tar.gz
samba-01f2c023f7d2a4b0e016676638a062a5ba29ec0b.tar.bz2
samba-01f2c023f7d2a4b0e016676638a062a5ba29ec0b.zip
lib/util: add allocate_anonymous_shared()
metze
-rw-r--r--lib/util/util.c28
-rw-r--r--lib/util/util.h5
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/util/util.c b/lib/util/util.c
index 0064cef8f2..d645f7edc9 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -25,6 +25,8 @@
#include "system/network.h"
#include "system/filesys.h"
#include "system/locale.h"
+#include "system/shmem.h"
+
#undef malloc
#undef strcasecmp
#undef strncasecmp
@@ -862,4 +864,30 @@ bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
}
+/* Map a shared memory buffer of at least nelem counters. */
+void *allocate_anonymous_shared(size_t bufsz)
+{
+ void *buf;
+ size_t pagesz = getpagesize();
+
+ if (bufsz % pagesz) {
+ bufsz = (bufsz + pagesz) % pagesz; /* round up to pagesz */
+ }
+
+#ifdef MAP_ANON
+ /* BSD */
+ buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
+ -1 /* fd */, 0 /* offset */);
+#else
+ buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
+ open("/dev/zero", O_RDWR), 0 /* offset */);
+#endif
+
+ if (buf == MAP_FAILED) {
+ return NULL;
+ }
+
+ return buf;
+
+}
diff --git a/lib/util/util.h b/lib/util/util.h
index e1160d5a3e..2d4a02549f 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -880,6 +880,11 @@ bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
gid_t **gids, size_t *num_gids);
+/**
+ * Allocate anonymous shared memory of the given size
+ */
+void *allocate_anonymous_shared(size_t bufsz);
+
/*
run a command as a child process, with a timeout.