From 746aab518450a41f2ea9723a1613c8c77013fb98 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Wed, 11 Mar 2020 13:01:17 +0100 Subject: [PATCH] commands: LXC_CMD_GET_INIT_PIDFD Signed-off-by: Christian Brauner --- src/lxc/commands.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lxc/commands.h | 2 ++ 2 files changed, 46 insertions(+) diff --git a/src/lxc/commands.c b/src/lxc/commands.c index e2156c618..ec3f4233a 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -83,6 +83,7 @@ static const char *lxc_cmd_str(lxc_cmd_t cmd) [LXC_CMD_FREEZE] = "freeze", [LXC_CMD_UNFREEZE] = "unfreeze", [LXC_CMD_GET_CGROUP2_FD] = "get_cgroup2_fd", + [LXC_CMD_GET_INIT_PIDFD] = "get_init_pidfd", }; if (cmd >= LXC_CMD_MAX) @@ -146,6 +147,11 @@ static int lxc_cmd_rsp_recv(int sock, struct lxc_cmd_rr *cmd) rsp->data = INT_TO_PTR(cgroup2_fd); } + if (cmd->req.cmd == LXC_CMD_GET_INIT_PIDFD) { + int init_pidfd = move_fd(fd_rsp); + rsp->data = INT_TO_PTR(init_pidfd); + } + if (rsp->datalen == 0) return log_debug(ret, "Response data length for command \"%s\" is 0", @@ -372,6 +378,43 @@ static int lxc_cmd_get_init_pid_callback(int fd, struct lxc_cmd_req *req, return 0; } +int lxc_cmd_get_init_pidfd(const char *name, const char *lxcpath) +{ + int ret, stopped; + struct lxc_cmd_rr cmd = { + .req = { + .cmd = LXC_CMD_GET_INIT_PIDFD, + }, + }; + + ret = lxc_cmd(name, &cmd, &stopped, lxcpath, NULL); + if (ret < 0) + return log_debug_errno(-1, errno, "Failed to process init pidfd command"); + + if (cmd.rsp.ret < 0) + return log_debug_errno(-EBADF, errno, "Failed to receive init pidfd"); + + return PTR_TO_INT(cmd.rsp.data); +} + +static int lxc_cmd_get_init_pidfd_callback(int fd, struct lxc_cmd_req *req, + struct lxc_handler *handler, + struct lxc_epoll_descr *descr) +{ + struct lxc_cmd_rsp rsp = { + .ret = 0, + }; + int ret; + + if (handler->pidfd < 0) + rsp.ret = -EBADF; + ret = lxc_abstract_unix_send_fds(fd, &handler->pidfd, 1, &rsp, sizeof(rsp)); + if (ret < 0) + return log_error(LXC_CMD_REAP_CLIENT_FD, "Failed to send init pidfd"); + + return 0; +} + /* * lxc_cmd_get_clone_flags: Get clone flags container was spawned with * @@ -1329,6 +1372,7 @@ static int lxc_cmd_process(int fd, struct lxc_cmd_req *req, [LXC_CMD_FREEZE] = lxc_cmd_freeze_callback, [LXC_CMD_UNFREEZE] = lxc_cmd_unfreeze_callback, [LXC_CMD_GET_CGROUP2_FD] = lxc_cmd_get_cgroup2_fd_callback, + [LXC_CMD_GET_INIT_PIDFD] = lxc_cmd_get_init_pidfd_callback, }; if (req->cmd >= LXC_CMD_MAX) diff --git a/src/lxc/commands.h b/src/lxc/commands.h index cd78a61a2..9e5248424 100644 --- a/src/lxc/commands.h +++ b/src/lxc/commands.h @@ -37,6 +37,7 @@ typedef enum { LXC_CMD_FREEZE, LXC_CMD_UNFREEZE, LXC_CMD_GET_CGROUP2_FD, + LXC_CMD_GET_INIT_PIDFD, LXC_CMD_MAX, } lxc_cmd_t; @@ -84,6 +85,7 @@ extern char *lxc_cmd_get_config_item(const char *name, const char *item, const c extern char *lxc_cmd_get_name(const char *hashed_sock); extern char *lxc_cmd_get_lxcpath(const char *hashed_sock); extern pid_t lxc_cmd_get_init_pid(const char *name, const char *lxcpath); +extern int lxc_cmd_get_init_pidfd(const char *name, const char *lxcpath); extern int lxc_cmd_get_state(const char *name, const char *lxcpath); extern int lxc_cmd_stop(const char *name, const char *lxcpath); -- 2.47.2