[lxc-devel] [lxc/master] commands: pass around intmax_t

brauner on Github lxc-bot at linuxcontainers.org
Fri Aug 31 19:33:17 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180831/f181bfd4/attachment.bin>
-------------- next part --------------
From 2bfcb8920490be10b4e17cd6fa78cfd7c183c27c Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 31 Aug 2018 21:25:45 +0200
Subject: [PATCH] commands: pass around intmax_t

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/commands.c | 16 +++++++++++++---
 src/lxc/macro.h    |  3 +++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/lxc/commands.c b/src/lxc/commands.c
index 458749ab5..2205095b4 100644
--- a/src/lxc/commands.c
+++ b/src/lxc/commands.c
@@ -375,12 +375,13 @@ int lxc_try_cmd(const char *name, const char *lxcpath)
 pid_t lxc_cmd_get_init_pid(const char *name, const char *lxcpath)
 {
 	int ret, stopped;
+	intmax_t pid;
 	struct lxc_cmd_rr cmd = {
 		.req = {
 			.cmd = LXC_CMD_GET_INIT_PID
 		},
 		.rsp = {
-			.data = INT_TO_PTR((int){-1})
+			.data = INTMAX_TO_PTR((int64_t){-1})
 		}
 	};
 
@@ -388,13 +389,22 @@ pid_t lxc_cmd_get_init_pid(const char *name, const char *lxcpath)
 	if (ret < 0)
 		return -1;
 
-	return PTR_TO_INT(cmd.rsp.data);
+	pid = PTR_TO_INTMAX(cmd.rsp.data);
+	if (pid < 0)
+		return -1;
+
+	/* We need to assume that pid_t can actually hold any pid given to us
+	 * by the kernel. If it can't it's a libc bug.
+	 */
+	return (pid_t)pid;
 }
 
 static int lxc_cmd_get_init_pid_callback(int fd, struct lxc_cmd_req *req,
 					 struct lxc_handler *handler)
 {
-	struct lxc_cmd_rsp rsp = { .data = INT_TO_PTR(handler->pid) };
+	struct lxc_cmd_rsp rsp = {
+		.data = INTMAX_TO_PTR(handler->pid)
+	};
 
 	return lxc_cmd_rsp_send(fd, &rsp);
 }
diff --git a/src/lxc/macro.h b/src/lxc/macro.h
index 8bad2d89d..ad95fcb26 100644
--- a/src/lxc/macro.h
+++ b/src/lxc/macro.h
@@ -312,4 +312,7 @@ extern int __build_bug_on_failed;
 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
 
+#define PTR_TO_INTMAX(p) ((int64_t)((intptr_t)(p)))
+#define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u)))
+
 #endif /* __LXC_MACRO_H */


More information about the lxc-devel mailing list