Commit 9554b482 authored by Pavel Emelyanov's avatar Pavel Emelyanov

util: Introduce the shmalloc helper

This one allocates a memory, that will be shared (MAP_SHARED)
between all the subsequent children. This can now be just used
for fd resote, later it will be required at inet socket port
binding.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 69acf64f
......@@ -27,9 +27,6 @@
#include "protobuf.h"
#include "protobuf/fs.pb-c.h"
static struct fdinfo_list_entry *fdinfo_list;
static int nr_fdinfo_list;
#define FDESC_HASH_SIZE 64
static struct list_head file_desc_hash[FDESC_HASH_SIZE];
......@@ -39,13 +36,6 @@ int prepare_shared_fdinfo(void)
{
int i;
fdinfo_list = mmap(NULL, FDINFO_POOL_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
if (fdinfo_list == MAP_FAILED) {
pr_perror("Can't map fdinfo_list");
return -1;
}
for (i = 0; i < FDESC_HASH_SIZE; i++)
INIT_LIST_HEAD(&file_desc_hash[i]);
......@@ -161,17 +151,15 @@ int rst_file_params(int fd, FownEntry *fown, int flags)
static int collect_fd(int pid, FdinfoEntry *e, struct rst_info *rst_info)
{
struct fdinfo_list_entry *l, *le = &fdinfo_list[nr_fdinfo_list];
struct fdinfo_list_entry *l, *le;
struct file_desc *fdesc;
pr_info("Collect fdinfo pid=%d fd=%d id=0x%16x\n",
pid, e->fd, e->id);
nr_fdinfo_list++;
if ((nr_fdinfo_list) * sizeof(struct fdinfo_list_entry) >= FDINFO_POOL_SIZE) {
pr_err("OOM storing fdinfo_list_entries\n");
le = shmalloc(sizeof(*le));
if (!le)
return -1;
}
le->pid = pid;
futex_init(&le->real_pid);
......
......@@ -318,4 +318,6 @@ static inline int read_img_str(int fd, char **pstr, int size)
return 0;
}
extern void *shmalloc(size_t bytes);
#endif /* UTIL_H_ */
......@@ -294,3 +294,34 @@ int is_anon_link_type(int lfd, char *type)
snprintf(aux, sizeof(aux), "anon_inode:%s", type);
return !strcmp(link, aux);
}
static void *sh_buf;
static unsigned int sh_bytes_left;
#define SH_BUF_CHUNK 4096
void *shmalloc(size_t bytes)
{
void *ret;
if (bytes > SH_BUF_CHUNK) {
pr_err("Too big shared buffer requested %lu\n", bytes);
return NULL;
}
if (sh_bytes_left < bytes) {
sh_buf = mmap(NULL, SH_BUF_CHUNK, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, 0, 0);
if (sh_buf == MAP_FAILED) {
pr_perror("Can't alloc shared buffer");
return NULL;
}
sh_bytes_left = SH_BUF_CHUNK;
}
ret = sh_buf;
sh_buf += bytes;
sh_bytes_left -= bytes;
return ret;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment