1
0
mirror of https://github.com/samba-team/samba.git synced 2025-08-15 13:49:28 +03:00

lib/util: add allocate_anonymous_shared()

metze
This commit is contained in:
Stefan Metzmacher
2010-03-22 08:27:58 +01:00
parent 13400a6589
commit 01f2c023f7
2 changed files with 33 additions and 0 deletions

View File

@ -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;
}

View File

@ -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.