[lxc-devel] [lxc/master] conf: cleanups

brauner on Github lxc-bot at linuxcontainers.org
Sat May 26 11:13:27 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180526/0df1c4e3/attachment.bin>
-------------- next part --------------
From 3a0e314db530d849c76d6d8660a9fdacf5804bad Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:48:09 +0200
Subject: [PATCH 1/8] conf: non-functional changes

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h | 64 ++++++++++++++++++++++------------------------------------
 1 file changed, 24 insertions(+), 40 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index d77a48415..c4ae2324d 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -78,12 +78,13 @@ struct lxc_cgroup {
 };
 
 #if !HAVE_SYS_RESOURCE_H
-# define RLIM_INFINITY ((unsigned long)-1)
+#define RLIM_INFINITY ((unsigned long)-1)
 struct rlimit {
 	unsigned long rlim_cur;
 	unsigned long rlim_max;
 };
 #endif
+
 /*
  * Defines a structure to configure resource limits to set via setrlimit().
  * @resource : the resource name in lowercase without the RLIMIT_ prefix
@@ -161,51 +162,34 @@ struct lxc_rootfs {
  * Automatic mounts for LXC to perform inside the container
  */
 enum {
-	LXC_AUTO_PROC_RW              = 0x001,   /* /proc read-write */
-	LXC_AUTO_PROC_MIXED           = 0x002,   /* /proc/sys and /proc/sysrq-trigger read-only */
+	LXC_AUTO_PROC_RW              = 0x001, /* /proc read-write */
+	LXC_AUTO_PROC_MIXED           = 0x002, /* /proc/sys and /proc/sysrq-trigger read-only */
 	LXC_AUTO_PROC_MASK            = 0x003,
 
-	LXC_AUTO_SYS_RW               = 0x004,   /* /sys */
-	LXC_AUTO_SYS_RO               = 0x008,   /* /sys read-only */
-	LXC_AUTO_SYS_MIXED            = 0x00C,   /* /sys read-only and /sys/class/net read-write */
+	LXC_AUTO_SYS_RW               = 0x004, /* /sys */
+	LXC_AUTO_SYS_RO               = 0x008, /* /sys read-only */
+	LXC_AUTO_SYS_MIXED            = 0x00C, /* /sys read-only and /sys/class/net read-write */
 	LXC_AUTO_SYS_MASK             = 0x00C,
 
-	LXC_AUTO_CGROUP_RO            = 0x010,   /* /sys/fs/cgroup (partial mount, read-only) */
-	LXC_AUTO_CGROUP_RW            = 0x020,   /* /sys/fs/cgroup (partial mount, read-write) */
-	LXC_AUTO_CGROUP_MIXED         = 0x030,   /* /sys/fs/cgroup (partial mount, paths r/o, cgroup r/w) */
-	LXC_AUTO_CGROUP_FULL_RO       = 0x040,   /* /sys/fs/cgroup (full mount, read-only) */
-	LXC_AUTO_CGROUP_FULL_RW       = 0x050,   /* /sys/fs/cgroup (full mount, read-write) */
-	LXC_AUTO_CGROUP_FULL_MIXED    = 0x060,   /* /sys/fs/cgroup (full mount, parent r/o, own r/w) */
-	/* These are defined in such a way as to retain
-	 * binary compatibility with earlier versions of
-	 * this code. If the previous mask is applied,
-	 * both of these will default back to the _MIXED
-	 * variants, which is safe. */
-	LXC_AUTO_CGROUP_NOSPEC        = 0x0B0,   /* /sys/fs/cgroup (partial mount, r/w or mixed, depending on caps) */
-	LXC_AUTO_CGROUP_FULL_NOSPEC   = 0x0E0,   /* /sys/fs/cgroup (full mount, r/w or mixed, depending on caps) */
-	LXC_AUTO_CGROUP_FORCE         = 0x100,   /* mount cgroups even when cgroup namespaces are supported */
-	LXC_AUTO_CGROUP_MASK          = 0x1F0,   /* all known cgroup options, doe not contain LXC_AUTO_CGROUP_FORCE */
-	LXC_AUTO_ALL_MASK             = 0x1FF,   /* all known settings */
+	LXC_AUTO_CGROUP_RO            = 0x010, /* /sys/fs/cgroup (partial mount, read-only) */
+	LXC_AUTO_CGROUP_RW            = 0x020, /* /sys/fs/cgroup (partial mount, read-write) */
+	LXC_AUTO_CGROUP_MIXED         = 0x030, /* /sys/fs/cgroup (partial mount, paths r/o, cgroup r/w) */
+	LXC_AUTO_CGROUP_FULL_RO       = 0x040, /* /sys/fs/cgroup (full mount, read-only) */
+	LXC_AUTO_CGROUP_FULL_RW       = 0x050, /* /sys/fs/cgroup (full mount, read-write) */
+	LXC_AUTO_CGROUP_FULL_MIXED    = 0x060, /* /sys/fs/cgroup (full mount, parent r/o, own r/w) */
+	/*
+	 * These are defined in such a way as to retain binary compatibility
+	 * with earlier versions of this code. If the previous mask is applied,
+	 * both of these will default back to the _MIXED variants, which is
+	 * safe.
+	 */
+	LXC_AUTO_CGROUP_NOSPEC        = 0x0B0, /* /sys/fs/cgroup (partial mount, r/w or mixed, depending on caps) */
+	LXC_AUTO_CGROUP_FULL_NOSPEC   = 0x0E0, /* /sys/fs/cgroup (full mount, r/w or mixed, depending on caps) */
+	LXC_AUTO_CGROUP_FORCE         = 0x100, /* mount cgroups even when cgroup namespaces are supported */
+	LXC_AUTO_CGROUP_MASK          = 0x1F0, /* all known cgroup options, doe not contain LXC_AUTO_CGROUP_FORCE */
+	LXC_AUTO_ALL_MASK             = 0x1FF, /* all known settings */
 };
 
-/*
- * Defines the global container configuration
- * @rootfs     : root directory to run the container
- * @mount      : list of mount points
- * @tty        : numbers of tty
- * @pts        : new pts instance
- * @mount_list : list of mount point (alternative to fstab file)
- * @network    : network configuration
- * @utsname    : container utsname
- * @fstab      : path to a fstab file format
- * @caps       : list of the capabilities to drop
- * @keepcaps   : list of the capabilities to keep
- * @ttys       : tty data
- * @console    : console data
- * @ttydir     : directory (under /dev) in which to create console and ttys
- * @lsm_aa_profile : apparmor profile to switch to or NULL
- * @lsm_se_context : selinux type to switch to or NULL
- */
 enum lxchooks {
 	LXCHOOK_PRESTART,
 	LXCHOOK_PREMOUNT,

From 07c4ea310d0f62f163bbfbb829752827691e2cd5 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:50:03 +0200
Subject: [PATCH 2/8] conf: make is_execute a boolean

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h    | 2 +-
 src/lxc/execute.c | 2 +-
 src/lxc/start.c   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index c4ae2324d..758b36667 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -214,10 +214,10 @@ struct lxc_state_client {
 struct lxc_conf {
 	/* Pointer to the name of the container. Do not free! */
 	const char *name;
-	int is_execute;
 	char *fstab;
 	unsigned int tty;
 	unsigned int pts;
+	bool is_execute;
 	int reboot;
 	signed long personality;
 	struct utsname *utsname;
diff --git a/src/lxc/execute.c b/src/lxc/execute.c
index d895e5be9..1a87b2321 100644
--- a/src/lxc/execute.c
+++ b/src/lxc/execute.c
@@ -152,7 +152,7 @@ int lxc_execute(const char *name, char *const argv[], int quiet,
 	struct execute_args args = {.argv = argv, .quiet = quiet};
 
 	TRACE("Doing lxc_execute");
-	handler->conf->is_execute = 1;
+	handler->conf->is_execute = true;
 	return __lxc_start(name, handler, &execute_start_ops, &args, lxcpath,
 			   backgrounded, error_num);
 }
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 94abff377..21a1140e6 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1247,7 +1247,7 @@ static int do_start(void *data)
 	 * make sure that that pty is stdin,stdout,stderr.
 	 */
 	 if (handler->conf->console.slave >= 0) {
-		 if (handler->backgrounded || handler->conf->is_execute == 0)
+		 if (handler->backgrounded || !handler->conf->is_execute)
 			 ret = set_stdfds(handler->conf->console.slave);
 		 else
 			 ret = lxc_terminal_set_stdfds(handler->conf->console.slave);

From edd64e17dcd4eba679c46b36fccc9d3a365073ca Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:50:53 +0200
Subject: [PATCH 3/8] conf: non-functional changes

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 758b36667..cc6bda780 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -221,21 +221,28 @@ struct lxc_conf {
 	int reboot;
 	signed long personality;
 	struct utsname *utsname;
+
 	struct {
 		struct lxc_list cgroup;
 		struct lxc_list cgroup2;
 	};
+
 	struct {
 		struct lxc_list id_map;
 
-		/* Pointer to the idmap entry for the container's root uid in
-		 * the id_map list. Do not free! */
+		/*
+		 * Pointer to the idmap entry for the container's root uid in
+		 * the id_map list. Do not free!
+		 */
 		struct id_map *root_nsuid_map;
 
-		/* Pointer to the idmap entry for the container's root gid in
-		 * the id_map list. Do not free! */
+		/*
+		 * Pointer to the idmap entry for the container's root gid in
+		 * the id_map list. Do not free!
+		 */
 		struct id_map *root_nsgid_map;
 	};
+
 	struct lxc_list network;
 	int auto_mounts;
 	struct lxc_list mount_list;

From b3187a814a4732689f4a08e1da760ff0e3a79e91 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:52:09 +0200
Subject: [PATCH 4/8] conf: make close_all_fds a boolean

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h            | 2 +-
 src/lxc/tools/arguments.h | 2 +-
 src/lxc/tools/lxc_start.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index cc6bda780..8dfc0dc96 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -254,7 +254,7 @@ struct lxc_conf {
 	struct lxc_terminal console;
 	struct lxc_rootfs rootfs;
 	char *ttydir;
-	int close_all_fds;
+	bool close_all_fds;
 
 	struct {
 		unsigned int hooks_version;
diff --git a/src/lxc/tools/arguments.h b/src/lxc/tools/arguments.h
index 15941bcc3..788f056e3 100644
--- a/src/lxc/tools/arguments.h
+++ b/src/lxc/tools/arguments.h
@@ -83,7 +83,7 @@ struct lxc_arguments {
 	int force;
 
 	/* close fds from parent? */
-	int close_all_fds;
+	bool close_all_fds;
 
 	/* lxc-create */
 	char *bdevtype, *configfile, *template;
diff --git a/src/lxc/tools/lxc_start.c b/src/lxc/tools/lxc_start.c
index 1a68453f8..a4217736e 100644
--- a/src/lxc/tools/lxc_start.c
+++ b/src/lxc/tools/lxc_start.c
@@ -96,7 +96,7 @@ static int my_parser(struct lxc_arguments *args, int c, char *arg)
 		args->rcfile = arg;
 		break;
 	case 'C':
-		args->close_all_fds = 1;
+		args->close_all_fds = true;
 		break;
 	case 's':
 		return lxc_config_define_add(&defines, arg);

From 448d7b0c0f14bf342b71dcf95e2a4b5369743905 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:53:12 +0200
Subject: [PATCH 5/8] conf: reshuffle mount members

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 8dfc0dc96..74a55699b 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -214,7 +214,6 @@ struct lxc_state_client {
 struct lxc_conf {
 	/* Pointer to the name of the container. Do not free! */
 	const char *name;
-	char *fstab;
 	unsigned int tty;
 	unsigned int pts;
 	bool is_execute;
@@ -244,8 +243,13 @@ struct lxc_conf {
 	};
 
 	struct lxc_list network;
-	int auto_mounts;
-	struct lxc_list mount_list;
+
+	struct {
+		char *fstab;
+		int auto_mounts;
+		struct lxc_list mount_list;
+	};
+
 	struct lxc_list caps;
 	struct lxc_list keepcaps;
 	struct lxc_tty_info ttys;

From 885766f5d2840d4f28fac94b30447611725bc24d Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 12:53:56 +0200
Subject: [PATCH 6/8] conf: simplify tty handling

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.c          | 45 +++++++++++++++++++++------------------------
 src/lxc/conf.h          | 15 ++++++++-------
 src/lxc/confile.c       | 24 ++++++++++++++++--------
 src/lxc/confile_utils.c | 10 ++++++++++
 src/lxc/confile_utils.h |  1 +
 src/lxc/start.c         | 16 +++++++---------
 src/lxc/terminal.c      |  8 ++++----
 7 files changed, 67 insertions(+), 52 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 220f76c58..7f583d5b2 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -835,7 +835,7 @@ static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs)
 }
 
 /* Build a space-separate list of ptys to pass to systemd. */
-static bool append_ptyname(char **pp, char *name)
+static bool append_ttyname(char **pp, char *name)
 {
 	char *p;
 
@@ -863,13 +863,13 @@ static int lxc_setup_ttys(struct lxc_conf *conf)
 {
 	int i, ret;
 	const struct lxc_tty_info *ttys = &conf->ttys;
-	char *ttydir = conf->ttydir;
+	char *ttydir = ttys->dir;
 	char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
 
 	if (!conf->rootfs.path)
 		return 0;
 
-	for (i = 0; i < ttys->nbtty; i++) {
+	for (i = 0; i < ttys->max; i++) {
 		struct lxc_terminal_info *tty = &ttys->tty[i];
 
 		ret = snprintf(path, sizeof(path), "/dev/tty%d", i + 1);
@@ -942,13 +942,13 @@ static int lxc_setup_ttys(struct lxc_conf *conf)
 			      path);
 		}
 
-		if (!append_ptyname(&conf->pty_names, tty->name)) {
+		if (!append_ttyname(&conf->ttys.tty_names, tty->name)) {
 			ERROR("Error setting up container_ttys string");
 			return -1;
 		}
 	}
 
-	INFO("Finished setting up %d /dev/tty<N> device(s)", ttys->nbtty);
+	INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys->max);
 	return 0;
 }
 
@@ -958,21 +958,21 @@ int lxc_allocate_ttys(const char *name, struct lxc_conf *conf)
 	struct lxc_tty_info *ttys = &conf->ttys;
 
 	/* no tty in the configuration */
-	if (!conf->tty)
+	if (ttys->max == 0)
 		return 0;
 
-	ttys->tty = malloc(sizeof(*ttys->tty) * conf->tty);
+	ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
 	if (!ttys->tty)
 		return -ENOMEM;
 
-	for (i = 0; i < conf->tty; i++) {
+	for (i = 0; i < ttys->max; i++) {
 		struct lxc_terminal_info *tty = &ttys->tty[i];
 
 		ret = openpty(&tty->master, &tty->slave,
 			      tty->name, NULL, NULL);
 		if (ret) {
 			SYSERROR("Failed to create tty %d", i);
-			ttys->nbtty = i;
+			ttys->max = i;
 			lxc_delete_tty(ttys);
 			return -ENOTTY;
 		}
@@ -996,9 +996,7 @@ int lxc_allocate_ttys(const char *name, struct lxc_conf *conf)
 		tty->busy = 0;
 	}
 
-	ttys->nbtty = conf->tty;
-
-	INFO("Finished creating %d tty devices", conf->tty);
+	INFO("Finished creating %zu tty devices", ttys->max);
 	return 0;
 }
 
@@ -1006,7 +1004,7 @@ void lxc_delete_tty(struct lxc_tty_info *ttys)
 {
 	int i;
 
-	for (i = 0; i < ttys->nbtty; i++) {
+	for (i = 0; i < ttys->max; i++) {
 		struct lxc_terminal_info *tty = &ttys->tty[i];
 
 		close(tty->master);
@@ -1015,7 +1013,6 @@ void lxc_delete_tty(struct lxc_tty_info *ttys)
 
 	free(ttys->tty);
 	ttys->tty = NULL;
-	ttys->nbtty = 0;
 }
 
 static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
@@ -1026,10 +1023,10 @@ static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
 	struct lxc_tty_info *ttys = &conf->ttys;
 	int sock = handler->data_sock[0];
 
-	if (conf->tty == 0)
+	if (ttys->max == 0)
 		return 0;
 
-	for (i = 0; i < conf->tty; i++) {
+	for (i = 0; i < ttys->max; i++) {
 		int ttyfds[2];
 		struct lxc_terminal_info *tty = &ttys->tty[i];
 
@@ -1045,10 +1042,10 @@ static int lxc_send_ttys_to_parent(struct lxc_handler *handler)
 	}
 
 	if (ret < 0)
-		ERROR("Failed to send %d ttys to parent: %s", conf->tty,
+		ERROR("Failed to send %zu ttys to parent: %s", ttys->max,
 		      strerror(errno));
 	else
-		TRACE("Sent %d ttys to parent", conf->tty);
+		TRACE("Sent %zu ttys to parent", ttys->max);
 
 	return ret;
 }
@@ -1078,10 +1075,10 @@ static int lxc_create_ttys(struct lxc_handler *handler)
 		}
 	}
 
-	if (conf->pty_names) {
-		ret = setenv("container_ttys", conf->pty_names, 1);
+	if (conf->ttys.tty_names) {
+		ret = setenv("container_ttys", conf->ttys.tty_names, 1);
 		if (ret < 0)
-			SYSERROR("Failed to set \"container_ttys=%s\"", conf->pty_names);
+			SYSERROR("Failed to set \"container_ttys=%s\"", conf->ttys.tty_names);
 	}
 
 	ret = 0;
@@ -3480,7 +3477,7 @@ int lxc_setup(struct lxc_handler *handler)
 	}
 
 	ret = lxc_setup_console(&lxc_conf->rootfs, &lxc_conf->console,
-				lxc_conf->ttydir);
+				lxc_conf->ttys.dir);
 	if (ret < 0) {
 		ERROR("Failed to setup console");
 		return -1;
@@ -3890,14 +3887,14 @@ void lxc_conf_free(struct lxc_conf *conf)
 	if (conf->logfd != -1)
 		close(conf->logfd);
 	free(conf->utsname);
-	free(conf->ttydir);
+	free(conf->ttys.dir);
+	free(conf->ttys.tty_names);
 	free(conf->fstab);
 	free(conf->rcfile);
 	free(conf->execute_cmd);
 	free(conf->init_cmd);
 	free(conf->init_cwd);
 	free(conf->unexpanded_config);
-	free(conf->pty_names);
 	free(conf->syslog);
 	lxc_free_networks(&conf->network);
 	free(conf->lsm_aa_profile);
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 74a55699b..0520d55a5 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -137,10 +137,12 @@ struct id_map {
 
 /* Defines the number of tty configured and contains the
  * instantiated ptys
- * @nbtty = number of configured ttys
+ * @max = number of configured ttys
  */
 struct lxc_tty_info {
-	int nbtty;
+	size_t max;
+	char *dir;
+	char *tty_names;
 	struct lxc_terminal_info *tty;
 };
 
@@ -214,8 +216,6 @@ struct lxc_state_client {
 struct lxc_conf {
 	/* Pointer to the name of the container. Do not free! */
 	const char *name;
-	unsigned int tty;
-	unsigned int pts;
 	bool is_execute;
 	int reboot;
 	signed long personality;
@@ -252,12 +252,13 @@ struct lxc_conf {
 
 	struct lxc_list caps;
 	struct lxc_list keepcaps;
-	struct lxc_tty_info ttys;
+
 	/* Comma-separated list of lxc.tty.max pty names. */
-	char *pty_names;
+	struct lxc_tty_info ttys;
+
+	unsigned int pts;
 	struct lxc_terminal console;
 	struct lxc_rootfs rootfs;
-	char *ttydir;
 	bool close_all_fds;
 
 	struct {
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 3ccc31cd4..fb161575b 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -1127,18 +1127,26 @@ static int set_config_environment(const char *key, const char *value,
 static int set_config_tty_max(const char *key, const char *value,
 			      struct lxc_conf *lxc_conf, void *data)
 {
+	int ret;
+	unsigned int nbtty = 0;
+
 	if (lxc_config_value_empty(value)) {
-		lxc_conf->tty = 0;
+		lxc_conf->ttys.max = 0;
 		return 0;
 	}
 
-	return lxc_safe_uint(value, &lxc_conf->tty);
+	ret = lxc_safe_uint(value, &nbtty);
+	if (ret < 0)
+		return -1;
+
+	lxc_conf->ttys.max = nbtty;
+	return 0;
 }
 
 static int set_config_tty_dir(const char *key, const char *value,
 			     struct lxc_conf *lxc_conf, void *data)
 {
-	return set_config_string_item_max(&lxc_conf->ttydir, value,
+	return set_config_string_item_max(&lxc_conf->ttys.dir, value,
 					  NAME_MAX + 1);
 }
 
@@ -2917,13 +2925,13 @@ static int get_config_pty_max(const char *key, char *retv, int inlen,
 static int get_config_tty_max(const char *key, char *retv, int inlen,
 			      struct lxc_conf *c, void *data)
 {
-	return lxc_get_conf_int(c, retv, inlen, c->tty);
+	return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
 }
 
 static int get_config_tty_dir(const char *key, char *retv, int inlen,
 			     struct lxc_conf *c, void *data)
 {
-	return lxc_get_conf_str(retv, inlen, c->ttydir);
+	return lxc_get_conf_str(retv, inlen, c->ttys.dir);
 }
 
 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
@@ -3695,15 +3703,15 @@ static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
 				     void *data)
 {
-	c->tty = 0;
+	c->ttys.tty = 0;
 	return 0;
 }
 
 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
 				    void *data)
 {
-	free(c->ttydir);
-	c->ttydir = NULL;
+	free(c->ttys.dir);
+	c->ttys.dir = NULL;
 	return 0;
 }
 
diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c
index fe5e078c4..30fec0583 100644
--- a/src/lxc/confile_utils.c
+++ b/src/lxc/confile_utils.c
@@ -643,6 +643,16 @@ int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v)
 	return snprintf(retv, inlen, "%d", v);
 }
 
+int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v)
+{
+	if (!retv)
+		inlen = 0;
+	else
+		memset(retv, 0, inlen);
+
+	return snprintf(retv, inlen, "%zu", v);
+}
+
 int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v)
 {
 	if (!retv)
diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h
index 33822faf2..072124f25 100644
--- a/src/lxc/confile_utils.h
+++ b/src/lxc/confile_utils.h
@@ -84,6 +84,7 @@ extern void update_hwaddr(const char *line);
 extern bool new_hwaddr(char *hwaddr);
 extern int lxc_get_conf_str(char *retv, int inlen, const char *value);
 extern int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v);
+extern int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v);
 extern int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v);
 extern bool parse_limit_value(const char **value, rlim_t *res);
 extern int lxc_inherit_namespace(const char *lxcname_or_pid,
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 21a1140e6..048b97b4f 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1331,8 +1331,8 @@ static int do_start(void *data)
 		goto out_warn_father;
 	}
 
-	if (handler->conf->pty_names) {
-		ret = putenv(handler->conf->pty_names);
+	if (handler->conf->ttys.tty_names) {
+		ret = putenv(handler->conf->ttys.tty_names);
 		if (ret < 0) {
 			SYSERROR("Failed to set environment variable for container ptys");
 			goto out_warn_father;
@@ -1397,14 +1397,14 @@ static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
 	struct lxc_conf *conf = handler->conf;
 	struct lxc_tty_info *ttys = &conf->ttys;
 
-	if (!conf->tty)
+	if (!conf->ttys.max)
 		return 0;
 
-	ttys->tty = malloc(sizeof(*ttys->tty) * conf->tty);
+	ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
 	if (!ttys->tty)
 		return -1;
 
-	for (i = 0; i < conf->tty; i++) {
+	for (i = 0; i < conf->ttys.max; i++) {
 		int ttyfds[2];
 
 		ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0);
@@ -1419,12 +1419,10 @@ static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
 		      "parent", tty->master, tty->slave);
 	}
 	if (ret < 0)
-		ERROR("Failed to receive %d ttys from child: %s", conf->tty,
+		ERROR("Failed to receive %zu ttys from child: %s", ttys->max,
 		      strerror(errno));
 	else
-		TRACE("Received %d ttys from child", conf->tty);
-
-	ttys->nbtty = conf->tty;
+		TRACE("Received %zu ttys from child", ttys->max);
 
 	return ret;
 }
diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c
index 234ad672e..18714db0b 100644
--- a/src/lxc/terminal.c
+++ b/src/lxc/terminal.c
@@ -618,7 +618,7 @@ int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq)
 	}
 
 	if (*ttyreq > 0) {
-		if (*ttyreq > ttys->nbtty)
+		if (*ttyreq > ttys->max)
 			goto out;
 
 		if (ttys->tty[*ttyreq - 1].busy)
@@ -630,12 +630,12 @@ int lxc_terminal_allocate(struct lxc_conf *conf, int sockfd, int *ttyreq)
 	}
 
 	/* Search for next available tty, fixup index tty1 => [0]. */
-	for (ttynum = 1; ttynum <= ttys->nbtty && ttys->tty[ttynum - 1].busy; ttynum++) {
+	for (ttynum = 1; ttynum <= ttys->max && ttys->tty[ttynum - 1].busy; ttynum++) {
 		;
 	}
 
 	/* We didn't find any available slot for tty. */
-	if (ttynum > ttys->nbtty)
+	if (ttynum > ttys->max)
 		goto out;
 
 	*ttyreq = ttynum;
@@ -654,7 +654,7 @@ void lxc_terminal_free(struct lxc_conf *conf, int fd)
 	struct lxc_tty_info *ttys = &conf->ttys;
 	struct lxc_terminal *terminal = &conf->console;
 
-	for (i = 0; i < ttys->nbtty; i++)
+	for (i = 0; i < ttys->max; i++)
 		if (ttys->tty[i].busy == fd)
 			ttys->tty[i].busy = 0;
 

From e528c7356b99eb042567db786d59f36162c0b8a8 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 13:09:13 +0200
Subject: [PATCH 7/8] conf: pts -> pty_max

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.c    |  6 +++---
 src/lxc/conf.h    |  2 +-
 src/lxc/confile.c | 13 +++++++++----
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 7f583d5b2..d7984bd0f 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -1523,14 +1523,14 @@ static int lxc_setup_devpts(struct lxc_conf *conf)
 	const char *default_devpts_mntopts = "gid=5,newinstance,ptmxmode=0666,mode=0620";
 	char devpts_mntopts[256];
 
-	if (conf->pts <= 0) {
+	if (conf->pty_max <= 0) {
 		DEBUG("No new devpts instance will be mounted since no pts "
 		      "devices are requested");
 		return 0;
 	}
 
-	ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%d",
-		       default_devpts_mntopts, conf->pts);
+	ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%zu",
+		       default_devpts_mntopts, conf->pty_max);
 	if (ret < 0 || (size_t)ret >= sizeof(devpts_mntopts))
 		return -1;
 
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 0520d55a5..5381072a0 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -255,8 +255,8 @@ struct lxc_conf {
 
 	/* Comma-separated list of lxc.tty.max pty names. */
 	struct lxc_tty_info ttys;
+	size_t pty_max;
 
-	unsigned int pts;
 	struct lxc_terminal console;
 	struct lxc_rootfs rootfs;
 	bool close_all_fds;
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index fb161575b..558e32e8c 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -974,14 +974,19 @@ static int set_config_personality(const char *key, const char *value,
 static int set_config_pty_max(const char *key, const char *value,
 			      struct lxc_conf *lxc_conf, void *data)
 {
+	int ret;
+	unsigned int max = 0;
+
 	if (lxc_config_value_empty(value)) {
-		lxc_conf->pts = 0;
+		lxc_conf->pty_max = 0;
 		return 0;
 	}
 
-	if (lxc_safe_uint(value, &lxc_conf->pts) < 0)
+	ret = lxc_safe_uint(value, &max);
+	if (ret < 0)
 		return -1;
 
+	lxc_conf->pty_max = max;
 	return 0;
 }
 
@@ -2919,7 +2924,7 @@ static int get_config_personality(const char *key, char *retv, int inlen,
 static int get_config_pty_max(const char *key, char *retv, int inlen,
 			      struct lxc_conf *c, void *data)
 {
-	return lxc_get_conf_int(c, retv, inlen, c->pts);
+	return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
 }
 
 static int get_config_tty_max(const char *key, char *retv, int inlen,
@@ -3696,7 +3701,7 @@ static inline int clr_config_personality(const char *key, struct lxc_conf *c,
 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
 				     void *data)
 {
-	c->pts = 0;
+	c->pty_max = 0;
 	return 0;
 }
 

From a2db71c0412b91b29875e7d8c66704dde4a8ccda Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 26 May 2018 13:12:32 +0200
Subject: [PATCH 8/8] conf: non-functional changes

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/conf.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 5381072a0..e8fb4d0ec 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -253,11 +253,13 @@ struct lxc_conf {
 	struct lxc_list caps;
 	struct lxc_list keepcaps;
 
-	/* Comma-separated list of lxc.tty.max pty names. */
+	/* /dev/tty<idx> devices */
 	struct lxc_tty_info ttys;
+	/* /dev/console device */
+	struct lxc_terminal console;
+	/* maximum pty devices allowed by devpts mount */
 	size_t pty_max;
 
-	struct lxc_terminal console;
 	struct lxc_rootfs rootfs;
 	bool close_all_fds;
 


More information about the lxc-devel mailing list