From 4e37d72e90596037b501bb3f2b09a910d70c1367 Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Mon, 30 Mar 2015 13:09:25 +0300 Subject: [PATCH] service: add ability to set inherit file descriptors (v3) This is required to use criu swrk in libcontainer. v2: remove useless function declaration allow to set inherit_fd only for swrk v3: check swrk out of loop Cc: Saied Kazemi Signed-off-by: Andrey Vagin Signed-off-by: Andrew Vagin Signed-off-by: Pavel Emelyanov --- cr-service.c | 10 ++++++++++ crtools.c | 2 +- files.c | 14 ++++++++++---- include/files.h | 3 ++- protobuf/rpc.proto | 6 ++++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cr-service.c b/cr-service.c index d202d14af..45db04bd1 100644 --- a/cr-service.c +++ b/cr-service.c @@ -18,6 +18,7 @@ #include "util.h" #include "log.h" #include "cpu.h" +#include "files.h" #include "pstree.h" #include "cr-service.h" #include "cr-service-const.h" @@ -344,6 +345,15 @@ static int setup_opts_from_req(int sk, CriuOpts *req) goto err; } + if (req->n_inherit_fd && !opts.swrk_restore) { + pr_err("inherit_fd is not allowed in standalone service\n"); + goto err; + } + for (i = 0; i < req->n_inherit_fd; i++) { + if (inherit_fd_add(req->inherit_fd[i]->fd, req->inherit_fd[i]->key)) + goto err; + } + for (i = 0; i < req->n_cg_root; i++) { if (new_cg_root_add(req->cg_root[i]->ctrl, req->cg_root[i]->path)) diff --git a/crtools.c b/crtools.c index 0b3c497c4..4412e4cb5 100644 --- a/crtools.c +++ b/crtools.c @@ -409,7 +409,7 @@ int main(int argc, char *argv[], char *envp[]) } break; case 1062: - if (inherit_fd_add(optarg) < 0) + if (inherit_fd_parse(optarg) < 0) return 1; break; case 1063: diff --git a/files.c b/files.c index 62749821b..db3745702 100644 --- a/files.c +++ b/files.c @@ -1266,14 +1266,12 @@ static int inherit_fd_reused(struct inherit_fd *inh) * We can't print diagnostics messages in this function because the * log file isn't initialized yet. */ -int inherit_fd_add(char *optarg) +int inherit_fd_parse(char *optarg) { char *cp = NULL; int n = -1; int fd = -1; int dbg = 0; - struct stat sbuf; - struct inherit_fd *inh; /* * Parse the argument. @@ -1308,6 +1306,14 @@ int inherit_fd_add(char *optarg) return 0; } + return inherit_fd_add(fd, cp); +} + +int inherit_fd_add(int fd, char *key) +{ + struct inherit_fd *inh; + struct stat sbuf; + if (fstat(fd, &sbuf) == -1) { pr_perror("Can't fstat inherit fd %d", fd); return -1; @@ -1317,7 +1323,7 @@ int inherit_fd_add(char *optarg) if (inh == NULL) return -1; - inh->inh_id = cp; + inh->inh_id = key; inh->inh_fd = fd; inh->inh_dev = sbuf.st_dev; inh->inh_ino = sbuf.st_ino; diff --git a/include/files.h b/include/files.h index 67cb6890c..db7e108c8 100644 --- a/include/files.h +++ b/include/files.h @@ -169,7 +169,8 @@ extern struct collect_image_info ext_file_cinfo; extern int dump_unsupp_fd(struct fd_parms *p, int lfd, struct cr_img *, char *more, char *info); -extern int inherit_fd_add(char *optarg); +extern int inherit_fd_parse(char *optarg); +extern int inherit_fd_add(int fd, char *key); extern void inherit_fd_log(void); extern int inherit_fd_resolve_clash(int fd); extern int inherit_fd_fini(void); diff --git a/protobuf/rpc.proto b/protobuf/rpc.proto index ed76d4fb2..5229a5f27 100644 --- a/protobuf/rpc.proto +++ b/protobuf/rpc.proto @@ -15,6 +15,11 @@ message ext_mount_map { required string val = 2; }; +message inherit_fd { + required string key = 1; + required int32 fd = 2; +}; + message cgroup_root { optional string ctrl = 1; required string path = 2; @@ -55,6 +60,7 @@ message criu_opts { repeated cgroup_root cg_root = 25; optional bool rst_sibling = 26; /* swrk only */ + repeated inherit_fd inherit_fd = 27; /* swrk only */ } message criu_dump_resp {