From: Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
To: containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org
Subject: [PATCH 2/5] Add a ckpt_read_string() function (v3)
Date: Mon, 10 Aug 2009 08:32:56 -0700 [thread overview]
Message-ID: <1249918379-29414-3-git-send-email-danms@us.ibm.com> (raw)
In-Reply-To: <1249918379-29414-1-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Add a ckpt_read_string() function to allow reading of a variable-length
(but length-capped) string from the checkpoint stream.
Changes in v3:
- Return immediately on allocation failure instead of falling through to the
inevitable crash
Changes in v2:
- Avoid memcpy() by reading into the allocated buffer directly
Acked-by: Serge Hallyn <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Signed-off-by: Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
---
checkpoint/restart.c | 36 ++++++++++++++++++++++++++++++++++++
include/linux/checkpoint.h | 1 +
2 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/checkpoint/restart.c b/checkpoint/restart.c
index 65cafd9..b1ffc54 100644
--- a/checkpoint/restart.c
+++ b/checkpoint/restart.c
@@ -285,6 +285,42 @@ 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
+ * @str: pointer to buffer to store allocated string (caller must kfree())
+ * @max: maximum acceptable length
+ *
+ * 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)
+{
+ int len;
+ int ret = 0;
+
+ *str = NULL;
+
+ len = _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
+ if (len < 0)
+ return len;
+ else if (len > max)
+ return -EINVAL;
+
+ *str = kzalloc(len + 1, GFP_KERNEL);
+ if (!*str)
+ return -ENOMEM;
+
+ ret = ckpt_kread(ctx, *str, len);
+ if (ret < 0) {
+ kfree(*str);
+ *str = NULL;
+ }
+
+ return ret;
+}
+
/***********************************************************************
* Restart
*/
diff --git a/include/linux/checkpoint.h b/include/linux/checkpoint.h
index 87b683b..a6935b3 100644
--- a/include/linux/checkpoint.h
+++ b/include/linux/checkpoint.h
@@ -68,6 +68,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
next prev parent reply other threads:[~2009-08-10 15:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-10 15:32 C/R support of UNIX sockets Dan Smith
2009-08-10 15:32 ` [PATCH 3/5] Add common socket helpers to unify the security hooks Dan Smith
2009-08-10 15:32 ` [PATCH 5/5] c/r: Add AF_UNIX support (v8) Dan Smith
2009-08-10 21:02 ` Serge E. Hallyn
2009-08-10 21:06 ` Dan Smith
[not found] ` <1249918379-29414-1-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-08-10 15:32 ` [PATCH 1/5] Add an errno validation function (v2) Dan Smith
[not found] ` <1249918379-29414-2-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-08-10 18:31 ` Serge E. Hallyn
[not found] ` <20090810183106.GA24373-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-08-10 18:34 ` Dan Smith
[not found] ` <87k51b5xi6.fsf-FLMGYpZoEPULwtHQx/6qkW3U47Q5hpJU@public.gmane.org>
2009-08-12 6:12 ` Oren Laadan
2009-08-10 15:32 ` Dan Smith [this message]
[not found] ` <1249918379-29414-3-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-08-12 6:17 ` [PATCH 2/5] Add a ckpt_read_string() function (v3) Oren Laadan
2009-08-10 15:32 ` [PATCH 4/5] Export fill_fname() as ckpt_fill_fname() Dan Smith
2009-08-12 6:12 ` C/R support of UNIX sockets Oren Laadan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1249918379-29414-3-git-send-email-danms@us.ibm.com \
--to=danms-r/jw6+rmf7hqt0dzr+alfa@public.gmane.org \
--cc=containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox