[PATCH 3/5] Add a ckpt_read_string() function to allow reading of a variable-length (but length-capped) string from the checkpoint stream.

Dan Smith danms at us.ibm.com
Wed Jul 22 13:41:39 PDT 2009


Signed-off-by: Dan Smith <danms at us.ibm.com>
---
 checkpoint/restart.c       |   35 +++++++++++++++++++++++++++++++++++
 include/linux/checkpoint.h |    1 +
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/checkpoint/restart.c b/checkpoint/restart.c
index 5cbe491..06fe47e 100644
--- a/checkpoint/restart.c
+++ b/checkpoint/restart.c
@@ -339,6 +339,41 @@ int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type)
 	return ret;
 }
 
+/**
+ * ckpt_read_string - read a string (variable length)
+ * @ctx: checkpoint context
+ * @max: maximum acceptable length
+ * @str: pointer to buffer to store allocated string (caller must kfree())
+ *
+ * This can be used to read a variable-length string from the checkpoint
+ * stream. @max limits the size of the resulting buffer.  Returns zero on
+ * success, negative on failure.
+ */
+int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max)
+{
+	struct ckpt_hdr *h;
+	char *buf;
+	int len;
+	int ret = 0;
+
+	h = ckpt_read_buf_type(ctx, max, CKPT_HDR_STRING);
+	if (IS_ERR(h))
+		return PTR_ERR(h);
+
+	buf = (char *)(h + 1);
+	len = h->len - sizeof(*h);
+
+	*str = kzalloc(len + 1, GFP_KERNEL);
+	if (!*str)
+		ret = -ENOMEM;
+	else
+		memcpy(*str, buf, len);
+
+	ckpt_hdr_put(ctx, h);
+
+	return ret;
+}
+
 /***********************************************************************
  * Restart
  */
diff --git a/include/linux/checkpoint.h b/include/linux/checkpoint.h
index 93efa10..95940d4 100644
--- a/include/linux/checkpoint.h
+++ b/include/linux/checkpoint.h
@@ -72,6 +72,7 @@ extern int _ckpt_read_obj_type(struct ckpt_ctx *ctx,
 extern int _ckpt_read_nbuffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_buffer(struct ckpt_ctx *ctx, void *ptr, int len);
 extern int _ckpt_read_string(struct ckpt_ctx *ctx, void *ptr, int len);
+extern int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max);
 extern void *ckpt_read_obj_type(struct ckpt_ctx *ctx, int len, int type);
 extern void *ckpt_read_buf_type(struct ckpt_ctx *ctx, int len, int type);
 extern int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type);
-- 
1.6.2.5



More information about the Containers mailing list