Skip to content

Commit

Permalink
memfd: add --inherit-fd support
Browse files Browse the repository at this point in the history
Upon file restore, inherited_fd() is called to check for a user-defined
inerit-fd override. Note that the MEMFD_INODE image is read at each
invocation (memfd name is not cached).

Signed-off-by: Nicolas Viennot <[email protected]>
  • Loading branch information
nviennot authored and avagin committed Mar 27, 2020
1 parent 875ac4d commit b25684e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions criu/crtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ int main(int argc, char *argv[], char *envp[])
" pipe[inode]\n"
" socket[inode]\n"
" file[mnt_id:inode]\n"
" /memfd:name\n"
" path/to/file\n"
" --empty-ns net Create a namespace, but don't restore its properties\n"
" (assuming it will be restored by action scripts)\n"
Expand Down
36 changes: 36 additions & 0 deletions criu/memfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ static int memfd_open(struct file_desc *d, u32 *fdflags)
mfi = container_of(d, struct memfd_info, d);
mfe = mfi->mfe;

if (inherited_fd(d, &fd))
return fd;

pr_info("Restoring memfd id=%d\n", mfe->id);

fd = memfd_open_inode(mfi->inode);
Expand Down Expand Up @@ -325,9 +328,42 @@ static int memfd_open_fe_fd(struct file_desc *fd, int *new_fd)
return 0;
}

static char *memfd_d_name(struct file_desc *d, char *buf, size_t s)
{
MemfdInodeEntry *mie = NULL;
struct cr_img *img = NULL;
struct memfd_info *mfi;
char *ret = NULL;

mfi = container_of(d, struct memfd_info, d);

img = open_image(CR_FD_MEMFD_INODE, O_RSTR, mfi->inode->id);
if (!img)
goto out;

if (pb_read_one(img, &mie, PB_MEMFD_INODE) < 0)
goto out;

if (snprintf(buf, s, "%s%s", MEMFD_PREFIX, mie->name) >= s) {
pr_err("Buffer too small for memfd name %s\n", mie->name);
goto out;
}

ret = buf;

out:
if (img)
close_image(img);
if (mie)
memfd_inode_entry__free_unpacked(mie, NULL);

return ret;
}

static struct file_desc_ops memfd_desc_ops = {
.type = FD_TYPES__MEMFD,
.open = memfd_open_fe_fd,
.name = memfd_d_name,
};

static int collect_one_memfd(void *o, ProtobufCMessage *msg, struct cr_img *i)
Expand Down

0 comments on commit b25684e

Please sign in to comment.