Linux Container Development
 help / color / mirror / Atom feed
From: Oren Laadan <orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
To: Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Cc: containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org
Subject: Re: [PATCH 3/5] Add a ckpt_read_string() function to allow reading of a	variable-length (but length-capped) string from the checkpoint stream.
Date: Thu, 23 Jul 2009 00:19:56 -0400	[thread overview]
Message-ID: <4A67E4EC.3070509@librato.com> (raw)
In-Reply-To: <1248295301-30930-4-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>



Dan Smith wrote:
> Signed-off-by: Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
> ---
>  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);
> +

You can avoid the memcpy() if you first read only the header, allocate
the string, and then read data into it.

Given that you already want _ckpt_read_obj_data() (or _payload) that
reads certain @len into a given buffer, why not add ckpt_read_obj_data()
that will return an allocate payload buffer.

Something like ...

int _ckpt_read_obj_data(ctx, ptr, len)
{
	return ckpt_kread(ctx, ptr, len);
}

void *ckpt_read_obj_data(ctx, len, type)
{
	void *buf = NULL;

	ret = _ckpt_read_obj_type(ctx, NULL, len, type);
	if (ret < 0)
		return ERR_PTR(ret);
	if (len && ret != len)
		return ERR_PTR(-EINVAL);
	if (ret) {
		buf = kzalloc(len, GFP_KERNEL);
		if (!buf)
			return ERR_PTR(-ENOMEM);
		ret = _ckpt_read_obj_data(ctx, buf, ret);
		if (ret < 0) {
			kfree(buf);
			buf = ERR_PTR(ret);
		}
	}
	return buf;
}

On top of this you can have ckpt_read_string() that will verify that
the buffer is of non-zero length and null terminated ?

Oren.

  parent reply	other threads:[~2009-07-23  4:19 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 20:41 Add Checkpoint/Restart support for UNIX sockets Dan Smith
     [not found] ` <1248295301-30930-1-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-07-22 20:41   ` [PATCH 1/5] Add _ckpt_read_hdr_type() helper Dan Smith
     [not found]     ` <1248295301-30930-2-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-07-23  3:59       ` Oren Laadan
     [not found]         ` <4A67E027.1050602-eQaUEPhvms7ENvBUuze7eA@public.gmane.org>
2009-07-23 16:43           ` Dan Smith
2009-07-22 20:41   ` [PATCH 2/5] Add an errno validation function Dan Smith
     [not found]     ` <1248295301-30930-3-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-07-23  3:17       ` Oren Laadan
     [not found]         ` <4A67D654.2000206-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
2009-07-23 15:40           ` Dan Smith
2009-07-22 20:41   ` [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
     [not found]     ` <1248295301-30930-4-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-07-23  4:19       ` Oren Laadan [this message]
     [not found]         ` <4A67E4EC.3070509-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
2009-07-23 16:58           ` Dan Smith
     [not found]             ` <87y6qfnxoq.fsf-FLMGYpZoEPULwtHQx/6qkW3U47Q5hpJU@public.gmane.org>
2009-07-24  1:32               ` Oren Laadan
2009-07-22 20:41   ` [PATCH 4/5] Add a common sock_bind() helper to unify the security hook Dan Smith
2009-07-22 20:41 ` [PATCH 5/5] c/r: Add AF_UNIX support (v6) Dan Smith
     [not found]   ` <1248295301-30930-6-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-07-28 16:54     ` Oren Laadan
2009-07-28 20:34       ` Dan Smith
2009-07-28 21:18         ` Oren Laadan
2009-07-29 13:36           ` Serge E. Hallyn
2009-07-29 14:49             ` Dan Smith
2009-07-29 14:59               ` Serge E. Hallyn
2009-07-29 15:03                 ` Dan Smith
2009-07-29 15:34             ` Oren Laadan
2009-07-29 18:37           ` Dan Smith
2009-07-30 15:49           ` Dan Smith
2009-07-30 22:10             ` John Dykstra
2009-07-30 22:12               ` John Dykstra
2009-07-30 22:14                 ` Dan Smith
2009-08-04  8:27             ` Oren Laadan
2009-08-04 15:16               ` Dan Smith
2009-08-04 17:05                 ` Oren Laadan
2009-08-04 17:13                   ` Dan Smith
2009-07-29  1:56         ` Serge E. Hallyn

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=4A67E4EC.3070509@librato.com \
    --to=orenl-rdfvbdnroixbdgjk7y7tuq@public.gmane.org \
    --cc=containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org \
    --cc=danms-r/Jw6+rmf7HQT0dZR+AlfA@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