[lxc-devel] [PATCH 9/9] lxc-user-nic: improve cull_entries

Stéphane Graber stgraber at ubuntu.com
Tue Nov 19 21:53:25 UTC 2013


On Tue, Nov 19, 2013 at 04:17:52PM +0000, Serge Hallyn wrote:
> From: Serge Hallyn <serge.hallyn at ubuntu.com>
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Stéphane Graber <stgraber at ubuntu.com>

> ---
>  src/lxc/lxc_user_nic.c | 42 ++++++++++++++++++++++++++++++++----------
>  1 file changed, 32 insertions(+), 10 deletions(-)
> 
> diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c
> index 7d367c1..7ac3020 100644
> --- a/src/lxc/lxc_user_nic.c
> +++ b/src/lxc/lxc_user_nic.c
> @@ -622,11 +622,19 @@ static bool get_nic_from_line(char *p, char **nic)
>  	return true;
>  }
>  
> +struct entry_line {
> +	char *start;
> +	int len;
> +	bool keep;
> +};
> +
>  static bool cull_entries(int fd, char *me, char *t, char *br)
>  {
>  	struct stat sb;
>  	char *buf, *p, *e, *nic;
>  	off_t len;
> +	struct entry_line *entry_lines = NULL;
> +	int i, n = 0;
>  
>  	nic = alloca(100);
>  
> @@ -643,22 +651,36 @@ static bool cull_entries(int fd, char *me, char *t, char *br)
>  	p = buf;
>  	e = buf + len;
>  	while ((p = find_line(p, e, me, t, br)) != NULL) {
> +		struct entry_line *newe = realloc(entry_lines, n+1);
> +		if (!newe) {
> +			free(entry_lines);
> +			return false;
> +		}
> +		entry_lines = newe;
> +		entry_lines[n].start = p;
> +		entry_lines[n].len = get_eol(p) - entry_lines[n].start;
> +		entry_lines[n].keep = true;
> +		n++;
>  		if (!get_nic_from_line(p, &nic))
>  			continue;
> -		if (nic && !nic_exists(nic)) {
> -			// copy from eol(p)+1..e to p
> -			char *src = get_eol(p) + 1, *dest = p;
> -			int diff = src - p;
> -			while (src < e)
> -				*(dest++) = *(src)++;
> -			e -= diff;
> -		} else
> -			p = get_eol(p) + 1;
> +		if (nic && !nic_exists(nic))
> +			entry_lines[n-1].keep = false;
> +		p += entry_lines[n-1].len + 1;
>  		if (p >= e)
>  			break;
>  	}
> +	p = buf;
> +	for (i=0; i<n; i++) {
> +		if (!entry_lines[i].keep)
> +			continue;
> +		memcpy(p, entry_lines[i].start, entry_lines[i].len);
> +		p += entry_lines[i].len;
> +		*p = '\n';
> +		p++;
> +	}
> +	free(entry_lines);
>  	munmap(buf, sb.st_size);
> -	if (ftruncate(fd, e-buf))
> +	if (ftruncate(fd, p-buf))
>  		fprintf(stderr, "Failed to set new file size\n");
>  	return true;
>  }
> -- 
> 1.8.3.2
> 
> 
> ------------------------------------------------------------------------------
> Shape the Mobile Experience: Free Subscription
> Software experts and developers: Be at the forefront of tech innovation.
> Intel(R) Software Adrenaline delivers strategic insight and game-changing 
> conversations that shape the rapidly evolving mobile landscape. Sign up now. 
> http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel

-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20131119/757cf1ea/attachment.pgp>


More information about the lxc-devel mailing list