[lxc-devel] [PATCH 1/2] cgroup: move all some functions into cgroup.h

Stéphane Graber stgraber at ubuntu.com
Mon Jan 13 21:32:21 UTC 2014


On Mon, Jan 13, 2014 at 03:26:49PM -0600, Serge Hallyn wrote:
> Some functions which wanted to know about cgroup paths were located
> in other files.  Move them into cgroup.c, so that all knowledge of
> the cgroup backend can be colocated.
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

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

> ---
>  src/lxc/cgroup.c  | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  src/lxc/cgroup.h  |   6 +++
>  src/lxc/freezer.c |  91 ------------------------------------
>  src/lxc/state.c   |  38 ----------------
>  4 files changed, 140 insertions(+), 129 deletions(-)
> 
> diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
> index be43c73..ff237fe 100644
> --- a/src/lxc/cgroup.c
> +++ b/src/lxc/cgroup.c
> @@ -48,6 +48,7 @@
>  #include "log.h"
>  #include "cgroup.h"
>  #include "start.h"
> +#include "state.h"
>  
>  #if IS_BIONIC
>  #include <../include/lxcmntent.h>
> @@ -2022,3 +2023,136 @@ int handle_cgroup_settings(struct cgroup_mount_point *mp, char *cgroup_path)
>  	}
>  	return 0;
>  }
> +
> +extern void lxc_monitor_send_state(const char *name, lxc_state_t state,
> +			    const char *lxcpath);
> +int do_unfreeze(const char *nsgroup, int freeze, const char *name, const char *lxcpath)
> +{
> +	char freezer[MAXPATHLEN], *f;
> +	char tmpf[32];
> +	int fd, ret;
> +
> +	ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup);
> +	if (ret >= MAXPATHLEN) {
> +		ERROR("freezer.state name too long");
> +		return -1;
> +	}
> +
> +	fd = open(freezer, O_RDWR);
> +	if (fd < 0) {
> +		SYSERROR("failed to open freezer at '%s'", nsgroup);
> +		return -1;
> +	}
> +
> +	if (freeze) {
> +		f = "FROZEN";
> +		ret = write(fd, f, strlen(f) + 1);
> +	} else {
> +		f = "THAWED";
> +		ret = write(fd, f, strlen(f) + 1);
> +
> +		/* compatibility code with old freezer interface */
> +		if (ret < 0) {
> +			f = "RUNNING";
> +			ret = write(fd, f, strlen(f) + 1) < 0;
> +		}
> +	}
> +
> +	if (ret < 0) {
> +		SYSERROR("failed to write '%s' to '%s'", f, freezer);
> +		goto out;
> +	}
> +
> +	while (1) {
> +		ret = lseek(fd, 0L, SEEK_SET);
> +		if (ret < 0) {
> +			SYSERROR("failed to lseek on file '%s'", freezer);
> +			goto out;
> +		}
> +
> +		ret = read(fd, tmpf, sizeof(tmpf));
> +		if (ret < 0) {
> +			SYSERROR("failed to read to '%s'", freezer);
> +			goto out;
> +		}
> +
> +		ret = strncmp(f, tmpf, strlen(f));
> +		if (!ret)
> +		{
> +			if (name)
> +				lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
> +			break;		/* Success */
> +		}
> +
> +		sleep(1);
> +
> +		ret = lseek(fd, 0L, SEEK_SET);
> +		if (ret < 0) {
> +			SYSERROR("failed to lseek on file '%s'", freezer);
> +			goto out;
> +		}
> +
> +		ret = write(fd, f, strlen(f) + 1);
> +		if (ret < 0) {
> +			SYSERROR("failed to write '%s' to '%s'", f, freezer);
> +			goto out;
> +		}
> +	}
> +
> +out:
> +	close(fd);
> +	return ret;
> +}
> +
> +int freeze_unfreeze(const char *name, int freeze, const char *lxcpath)
> +{
> +	char *cgabspath;
> +	int ret;
> +
> +	cgabspath = lxc_cgroup_get_hierarchy_abs_path("freezer", name, lxcpath);
> +	if (!cgabspath)
> +		return -1;
> +
> +	ret = do_unfreeze(cgabspath, freeze, name, lxcpath);
> +	free(cgabspath);
> +	return ret;
> +}
> +
> +lxc_state_t freezer_state(const char *name, const char *lxcpath)
> +{
> +	char *cgabspath = NULL;
> +	char freezer[MAXPATHLEN];
> +	char status[MAXPATHLEN];
> +	FILE *file;
> +	int ret;
> +
> +	cgabspath = lxc_cgroup_get_hierarchy_abs_path("freezer", name, lxcpath);
> +	if (!cgabspath)
> +		return -1;
> +
> +	ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", cgabspath);
> +	if (ret < 0 || ret >= MAXPATHLEN)
> +		goto out;
> +
> +	file = fopen(freezer, "r");
> +	if (!file) {
> +		ret = -1;
> +		goto out;
> +	}
> +
> +	ret = fscanf(file, "%s", status);
> +	fclose(file);
> +
> +	if (ret == EOF) {
> +		SYSERROR("failed to read %s", freezer);
> +		ret = -1;
> +		goto out;
> +	}
> +
> +	ret = lxc_str2state(status);
> +
> +out:
> +	free(cgabspath);
> +	return ret;
> +}
> +
> diff --git a/src/lxc/cgroup.h b/src/lxc/cgroup.h
> index 0c0a678..3aab12d 100644
> --- a/src/lxc/cgroup.h
> +++ b/src/lxc/cgroup.h
> @@ -26,6 +26,8 @@
>  #include <stdint.h>
>  #include <stddef.h>
>  
> +#include "state.h"
> +
>  struct cgroup_hierarchy;
>  struct cgroup_meta_data;
>  struct cgroup_mount_point;
> @@ -164,4 +166,8 @@ extern int lxc_setup_mount_cgroup(const char *root, struct cgroup_process_info *
>  
>  extern int lxc_cgroup_nrtasks_handler(struct lxc_handler *handler);
>  
> +extern int do_unfreeze(const char *nsgroup, int freeze, const char *name, const char *lxcpath);
> +extern int freeze_unfreeze(const char *name, int freeze, const char *lxcpath);
> +extern const char *lxc_state2str(lxc_state_t state);
> +extern lxc_state_t freezer_state(const char *name, const char *lxcpath);
>  #endif
> diff --git a/src/lxc/freezer.c b/src/lxc/freezer.c
> index 7b9f45f..53e9f64 100644
> --- a/src/lxc/freezer.c
> +++ b/src/lxc/freezer.c
> @@ -39,97 +39,6 @@
>  
>  lxc_log_define(lxc_freezer, lxc);
>  
> -static int do_unfreeze(const char *nsgroup, int freeze, const char *name, const char *lxcpath)
> -{
> -	char freezer[MAXPATHLEN], *f;
> -	char tmpf[32];
> -	int fd, ret;
> -
> -	ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", nsgroup);
> -	if (ret >= MAXPATHLEN) {
> -		ERROR("freezer.state name too long");
> -		return -1;
> -	}
> -
> -	fd = open(freezer, O_RDWR);
> -	if (fd < 0) {
> -		SYSERROR("failed to open freezer at '%s'", nsgroup);
> -		return -1;
> -	}
> -
> -	if (freeze) {
> -		f = "FROZEN";
> -		ret = write(fd, f, strlen(f) + 1);
> -	} else {
> -		f = "THAWED";
> -		ret = write(fd, f, strlen(f) + 1);
> -
> -		/* compatibility code with old freezer interface */
> -		if (ret < 0) {
> -			f = "RUNNING";
> -			ret = write(fd, f, strlen(f) + 1) < 0;
> -		}
> -	}
> -
> -	if (ret < 0) {
> -		SYSERROR("failed to write '%s' to '%s'", f, freezer);
> -		goto out;
> -	}
> -
> -	while (1) {
> -		ret = lseek(fd, 0L, SEEK_SET);
> -		if (ret < 0) {
> -			SYSERROR("failed to lseek on file '%s'", freezer);
> -			goto out;
> -		}
> -
> -		ret = read(fd, tmpf, sizeof(tmpf));
> -		if (ret < 0) {
> -			SYSERROR("failed to read to '%s'", freezer);
> -			goto out;
> -		}
> -
> -		ret = strncmp(f, tmpf, strlen(f));
> -		if (!ret)
> -		{
> -			if (name)
> -				lxc_monitor_send_state(name, freeze ? FROZEN : THAWED, lxcpath);
> -			break;		/* Success */
> -		}
> -
> -		sleep(1);
> -
> -		ret = lseek(fd, 0L, SEEK_SET);
> -		if (ret < 0) {
> -			SYSERROR("failed to lseek on file '%s'", freezer);
> -			goto out;
> -		}
> -
> -		ret = write(fd, f, strlen(f) + 1);
> -		if (ret < 0) {
> -			SYSERROR("failed to write '%s' to '%s'", f, freezer);
> -			goto out;
> -		}
> -	}
> -
> -out:
> -	close(fd);
> -	return ret;
> -}
> -
> -static int freeze_unfreeze(const char *name, int freeze, const char *lxcpath)
> -{
> -	char *cgabspath;
> -	int ret;
> -
> -	cgabspath = lxc_cgroup_get_hierarchy_abs_path("freezer", name, lxcpath);
> -	if (!cgabspath)
> -		return -1;
> -
> -	ret = do_unfreeze(cgabspath, freeze, name, lxcpath);
> -	free(cgabspath);
> -	return ret;
> -}
>  
>  int lxc_freeze(const char *name, const char *lxcpath)
>  {
> diff --git a/src/lxc/state.c b/src/lxc/state.c
> index a47341f..81648e0 100644
> --- a/src/lxc/state.c
> +++ b/src/lxc/state.c
> @@ -67,44 +67,6 @@ lxc_state_t lxc_str2state(const char *state)
>  	return -1;
>  }
>  
> -static lxc_state_t freezer_state(const char *name, const char *lxcpath)
> -{
> -	char *cgabspath = NULL;
> -	char freezer[MAXPATHLEN];
> -	char status[MAXPATHLEN];
> -	FILE *file;
> -	int ret;
> -
> -	cgabspath = lxc_cgroup_get_hierarchy_abs_path("freezer", name, lxcpath);
> -	if (!cgabspath)
> -		return -1;
> -
> -	ret = snprintf(freezer, MAXPATHLEN, "%s/freezer.state", cgabspath);
> -	if (ret < 0 || ret >= MAXPATHLEN)
> -		goto out;
> -
> -	file = fopen(freezer, "r");
> -	if (!file) {
> -		ret = -1;
> -		goto out;
> -	}
> -
> -	ret = fscanf(file, "%s", status);
> -	fclose(file);
> -
> -	if (ret == EOF) {
> -		SYSERROR("failed to read %s", freezer);
> -		ret = -1;
> -		goto out;
> -	}
> -
> -	ret = lxc_str2state(status);
> -
> -out:
> -	free(cgabspath);
> -	return ret;
> -}
> -
>  lxc_state_t lxc_getstate(const char *name, const char *lxcpath)
>  {
>  	lxc_state_t state = freezer_state(name, lxcpath);
> -- 
> 1.8.5.2
> 
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/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/20140113/96fc90d3/attachment-0001.pgp>


More information about the lxc-devel mailing list