[lxc-devel] [PATCH 2/2] lxc_attach: Clean up cgroup attaching code

Serge Hallyn serge.hallyn at ubuntu.com
Mon Mar 4 16:00:38 UTC 2013


Quoting Christian Seiler (christian at iwakd.de):
> Since lxc_attach now works with two fork()s anyway due to user
> namespaces, the code for attaching to cgroups can be simplified again.
> 
> This patch removes the prepare/finish/dispose functions for attaching
> to cgroups and just keeps the lxc_cgroup_attach function.
> ---

(Note - no signed-off-by in this patch.  How are you generating them?
I'd recommend either using git-send-email, or get format-patch...)

Thanks, Christian.  Unfortunately this will clash badly with my cgroup
update which does the same thing, so while I 100% ack the concept,
Stéphane please do not apply this.


>  src/lxc/cgroup.c |  154 ++++++-----------------------------------------------
>  src/lxc/cgroup.h |    3 -
>  2 files changed, 18 insertions(+), 139 deletions(-)
> 
> diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
> index 6630d6c..8420e08 100644
> --- a/src/lxc/cgroup.c
> +++ b/src/lxc/cgroup.c
> @@ -259,37 +259,12 @@ static int cgroup_enable_clone_children(const char *path)
>  	return ret;
>  }
>  
> -static int lxc_one_cgroup_finish_attach(int fd, pid_t pid)
> -{
> -       char buf[32];
> -       int ret;
> -
> -       snprintf(buf, 32, "%ld", (long)pid);
> -
> -       ret = write(fd, buf, strlen(buf));
> -       if (ret <= 0) {
> -               SYSERROR("failed to write pid '%ld' to fd '%d'", (long)pid, fd);
> -               ret = -1;
> -       } else {
> -               ret = 0;
> -       }
> -
> -       close(fd);
> -       return ret;
> -}
> -
> -static int lxc_one_cgroup_dispose_attach(int fd)
> -{
> -       close(fd);
> -       return 0;
> -}
> -
> -static int lxc_one_cgroup_prepare_attach(const char *name,
> -					 struct mntent *mntent)
> +static int lxc_one_cgroup_attach(const char *name, struct mntent *mntent, pid_t pid)
>  {
>  	int fd;
>  	char tasks[MAXPATHLEN], initcgroup[MAXPATHLEN];
>  	char *cgmnt = mntent->mnt_dir;
> +	char buf[32];
>  	int flags;
>  	int rc;
>  
> @@ -310,77 +285,26 @@ static int lxc_one_cgroup_prepare_attach(const char *name,
>  		return -1;
>  	}
>  
> -	return fd;
> -}
> -
> -static int lxc_one_cgroup_attach(const char *name, struct mntent *mntent, pid_t pid)
> -{
> -	int fd;
> -
> -	fd = lxc_one_cgroup_prepare_attach(name, mntent);
> -	if (fd < 0) {
> -		return -1;
> -	}
> -
> -	return lxc_one_cgroup_finish_attach(fd, pid);
> -}
> -
> -int lxc_cgroup_dispose_attach(void *data)
> -{
> -	int *fds = data;
> -	int ret, err;
> -
> -	if (!fds) {
> -		return 0;
> -	}
> -
> -	ret = 0;
> -
> -	for (; *fds >= 0; fds++) {
> -		err = lxc_one_cgroup_dispose_attach(*fds);
> -		if (err) {
> -			ret = err;
> -		}
> -	}
> -
> -	free(data);
> -
> -	return ret;
> -}
> -
> -int lxc_cgroup_finish_attach(void *data, pid_t pid)
> -{
> -	int *fds = data;
> -	int err;
> +	snprintf(buf, 32, "%ld", (long)pid);
>  
> -	if (!fds) {
> -		return 0;
> +	rc = write(fd, buf, strlen(buf));
> +	if (rc <= 0) {
> +		SYSERROR("failed to write pid '%ld' to fd '%d'", (long)pid, fd);
> +		rc = -1;
> +	} else {
> +		rc = 0;
>  	}
>  
> -	for (; *fds >= 0; fds++) {
> -		err = lxc_one_cgroup_finish_attach(*fds, pid);
> -		if (err) {
> -			/* get rid of the rest of them */
> -			lxc_cgroup_dispose_attach(data);
> -			return -1;
> -		}
> -		*fds = -1;
> -	}
> -
> -	free(data);
> -
> -	return 0;
> +	close(fd);
> +	return rc;
>  }
>  
> -int lxc_cgroup_prepare_attach(const char *name, void **data)
> +int lxc_cgroup_attach(const char *name, pid_t pid)
>  {
>  	struct mntent *mntent;
>  	FILE *file = NULL;
> -	int err = -1;
>  	int found = 0;
> -	int *fds;
> -	int i;
> -	static const int MAXFDS = 256;
> +	int err = 0;
>  
>  	file = setmntent(MTAB, "r");
>  	if (!file) {
> @@ -388,29 +312,7 @@ int lxc_cgroup_prepare_attach(const char *name, void **data)
>  		return -1;
>  	}
>  
> -	/* create a large enough buffer for all practical
> -	 * use cases
> -	 */
> -	fds = malloc(sizeof(int) * MAXFDS);
> -	if (!fds) {
> -		err = -1;
> -		goto out;
> -	}
> -	for (i = 0; i < MAXFDS; i++) {
> -		fds[i] = -1;
> -	}
> -
> -	err = 0;
> -	i = 0;
>  	while ((mntent = getmntent(file))) {
> -		if (i >= MAXFDS - 1) {
> -			ERROR("too many cgroups to attach to, aborting");
> -			lxc_cgroup_dispose_attach(fds);
> -			errno = ENOMEM;
> -			err = -1;
> -			goto out;
> -		}
> -
>  		DEBUG("checking '%s' (%s)", mntent->mnt_dir, mntent->mnt_type);
>  
>  		if (strcmp(mntent->mnt_type, "cgroup"))
> @@ -421,42 +323,22 @@ int lxc_cgroup_prepare_attach(const char *name, void **data)
>  		INFO("[%d] found cgroup mounted at '%s',opts='%s'",
>  		     ++found, mntent->mnt_dir, mntent->mnt_opts);
>  
> -		fds[i] = lxc_one_cgroup_prepare_attach(name, mntent);
> -		if (fds[i] < 0) {
> -			err = fds[i];
> -			lxc_cgroup_dispose_attach(fds);
> +		err = lxc_one_cgroup_attach(name, mntent, pid);
> +		if (err < 0)
>  			goto out;
> -		}
> -		i++;
> -	};
> +
> +		found = 1;
> +	}
>  
>  	if (!found)
>  		ERROR("No cgroup mounted on the system");
>  
> -	*data = fds;
> -
>  out:
>  	endmntent(file);
>  	return err;
>  }
>  
>  /*
> - * for each mounted cgroup, attach a pid to the cgroup for the container
> - */
> -int lxc_cgroup_attach(const char *name, pid_t pid)
> -{
> -	void *data = NULL;
> -	int ret;
> -
> -	ret = lxc_cgroup_prepare_attach(name, &data);
> -	if (ret < 0) {
> -		return ret;
> -	}
> -
> -	return lxc_cgroup_finish_attach(data, pid);
> -}
> -
> -/*
>   * rename cgname, which is under cgparent, to a new name starting
>   * with 'cgparent/dead'.  That way cgname can be reused.  Return
>   * 0 on success, -1 on failure.
> diff --git a/src/lxc/cgroup.h b/src/lxc/cgroup.h
> index 8167f39..3c90696 100644
> --- a/src/lxc/cgroup.h
> +++ b/src/lxc/cgroup.h
> @@ -31,8 +31,5 @@ extern int lxc_cgroup_destroy(const char *name);
>  extern int lxc_cgroup_path_get(char **path, const char *subsystem, const char *name);
>  extern int lxc_cgroup_nrtasks(const char *name);
>  extern int lxc_cgroup_attach(const char *name, pid_t pid);
> -extern int lxc_cgroup_prepare_attach(const char *name, void **data);
> -extern int lxc_cgroup_finish_attach(void *data, pid_t pid);
> -extern int lxc_cgroup_dispose_attach(void *data);
>  extern int lxc_ns_is_mounted(void);
>  #endif
> -- 
> 1.7.8.6
> 
> 
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_feb
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel




More information about the lxc-devel mailing list