[CRIU] [PATCH 3/6] tty: split pty_open_ptmx_index()

Andrei Vagin avagin at openvz.org
Tue Jan 3 15:40:02 PST 2017


From: Andrei Vagin <avagin at virtuozzo.com>

We are going to create slave pty-s to bind-mount them,
but at this moment we don't have file_desc-s and actually
can't open /dev/ptmx by path.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/tty.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/criu/tty.c b/criu/tty.c
index ae2d463..0966129 100644
--- a/criu/tty.c
+++ b/criu/tty.c
@@ -544,8 +544,9 @@ static int do_open_tty_reg(int ns_root_fd, struct reg_file_info *rfi, void *arg)
 	return fd;
 }
 
-static int open_tty_reg(struct file_desc *reg_d, u32 flags)
+static int open_tty_reg(void *arg, int flags)
 {
+	struct file_desc *reg_d = arg;
 	/*
 	 * Never set as a control terminal automatically, all
 	 * ctty magic happens only in tty_set_sid().
@@ -560,7 +561,8 @@ static char *path_from_reg(struct file_desc *d)
 	return rfi->path;
 }
 
-static int pty_open_ptmx_index(struct file_desc *d, int index, int flags)
+static int __pty_open_ptmx_index(int index, int flags,
+			int (*cb)(void *arg, int flags), void *arg, char *path)
 {
 	int fds[32], i, ret = -1, cur_idx;
 
@@ -569,15 +571,15 @@ static int pty_open_ptmx_index(struct file_desc *d, int index, int flags)
 	mutex_lock(tty_mutex);
 
 	for (i = 0; i < ARRAY_SIZE(fds); i++) {
-		fds[i] = open_tty_reg(d, flags);
+		fds[i] = cb(arg, flags);
 		if (fds[i] < 0) {
-			pr_err("Can't open %s\n", path_from_reg(d));
+			pr_err("Can't open %s\n", path);
 			break;
 		}
 
 		if (ioctl(fds[i], TIOCGPTN, &cur_idx)) {
 			pr_perror("Can't obtain current index on %s",
-				  path_from_reg(d));
+				  path);
 			break;
 		}
 
@@ -598,7 +600,7 @@ static int pty_open_ptmx_index(struct file_desc *d, int index, int flags)
 			continue;
 
 		pr_err("Unable to open %s with specified index %d\n",
-		       path_from_reg(d), index);
+		       path, index);
 		break;
 	}
 
@@ -612,6 +614,12 @@ static int pty_open_ptmx_index(struct file_desc *d, int index, int flags)
 	return ret;
 }
 
+static int pty_open_ptmx_index(struct file_desc *d, struct tty_info *info, int flags)
+{
+	return __pty_open_ptmx_index(info->tie->pty->index, flags,
+					open_tty_reg, d, path_from_reg(d));
+}
+
 static int unlock_pty(int fd)
 {
 	const int lock = 0;
@@ -952,7 +960,7 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
 			fake = pty_alloc_fake_master(slave);
 			if (!fake)
 				goto err;
-			master = pty_open_ptmx_index(&fake->d, slave->tie->pty->index, O_RDWR);
+			master = pty_open_ptmx_index(&fake->d, slave, O_RDWR);
 			if (master < 0) {
 				pr_err("Can't open master pty %x (index %d)\n",
 					  slave->tfe->id, slave->tie->pty->index);
@@ -991,7 +999,7 @@ static int pty_open_unpaired_slave(struct file_desc *d, struct tty_info *slave)
 		fake = pty_alloc_fake_master(slave);
 		if (!fake)
 			goto err;
-		master = pty_open_ptmx_index(&fake->d, slave->tie->pty->index, O_RDONLY);
+		master = pty_open_ptmx_index(&fake->d, slave, O_RDONLY);
 		if (master < 0) {
 			pr_err("Can't open master pty %x (index %d)\n",
 				  slave->tfe->id, slave->tie->pty->index);
@@ -1053,7 +1061,7 @@ static int pty_open_ptmx(struct tty_info *info)
 {
 	int master = -1;
 
-	master = pty_open_ptmx_index(info->reg_d, info->tie->pty->index, info->tfe->flags);
+	master = pty_open_ptmx_index(info->reg_d, info, info->tfe->flags);
 	if (master < 0) {
 		pr_err("Can't open master pty %x (index %d)\n",
 			  info->tfe->id, info->tie->pty->index);
-- 
2.7.4



More information about the CRIU mailing list