[CRIU] [PATCH 10/11] tty: Parse slave index from link path

Cyrill Gorcunov gorcunov at openvz.org
Mon Oct 6 06:15:49 PDT 2014


Instead of reading link inside tty code lets
make it on a higher level providing the link
inside fd_params structure (just like we do
for other objects).

Also because mount point for devpts fs might
be not /dev/pts simply search for index from
the end of the name (as it's done in the
kernel -- the slave index is a numeric name
under the mountpoint).

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 files.c |  4 ++++
 tty.c   | 23 ++++++++---------------
 2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/files.c b/files.c
index b975cd46d011..10c8c6d16344 100644
--- a/files.c
+++ b/files.c
@@ -268,6 +268,7 @@ static int dump_chrdev(struct fd_parms *p, int lfd, struct cr_img *img)
 {
 	int maj = major(p->stat.st_rdev);
 	const struct fdtype_ops *ops;
+	struct fd_link link;
 
 	switch (maj) {
 	case MEM_MAJOR:
@@ -276,6 +277,9 @@ static int dump_chrdev(struct fd_parms *p, int lfd, struct cr_img *img)
 	case TTYAUX_MAJOR:
 	case UNIX98_PTY_MASTER_MAJOR ... (UNIX98_PTY_MASTER_MAJOR + UNIX98_PTY_MAJOR_COUNT - 1):
 	case UNIX98_PTY_SLAVE_MAJOR:
+		if (fill_fdlink(lfd, p, &link))
+			return -1;
+		p->link = &link;
 		ops = &tty_dump_ops;
 		break;
 	case MISC_MAJOR:
diff --git a/tty.c b/tty.c
index 355e679df9e2..edbe85e7505a 100644
--- a/tty.c
+++ b/tty.c
@@ -201,8 +201,9 @@ int tty_verify_active_pairs(void)
 	return 0;
 }
 
-static int parse_pty_index(u32 id, int lfd, int major)
+static int parse_pty_index(u32 id, int lfd, const struct fd_parms *p, int major)
 {
+	const struct fd_link *link = p->link;
 	int index = -1;
 
 	switch (major) {
@@ -214,22 +215,14 @@ static int parse_pty_index(u32 id, int lfd, int major)
 		break;
 
 	case UNIX98_PTY_SLAVE_MAJOR: {
-		char path[PATH_MAX];
-		char link[32];
-		int len;
-
-		snprintf(link, sizeof(link), "/proc/self/fd/%d", lfd);
-		len = readlink(link, path, sizeof(path) - 1);
-		if (len < 0) {
-			pr_perror("Can't readlink %s", link);
-			return -1;
-		}
-		path[len] = '\0';
+		char *pos;
 
-		if (sscanf(path, PTS_FMT, &index) != 1) {
-			pr_err("Unexpected format on path %s\n", path);
+		pos = strrchr(link->name, '/');
+		if (!pos || pos == (link->name + link->len - 1)) {
+			pr_err("Unexpected format on path %s\n", link->name + 1);
 			return -1;
 		}
+		index = atoi(pos + 1);
 		break;
 	}
 	}
@@ -1172,7 +1165,7 @@ static int dump_one_pty(int lfd, u32 id, const struct fd_parms *p, int major, in
 	TtyFileEntry e = TTY_FILE_ENTRY__INIT;
 	int ret = 0, index;
 
-	index = parse_pty_index(id, lfd, major);
+	index = parse_pty_index(id, lfd, p, major);
 	if (index < 0)
 		return -1;
 
-- 
1.9.3



More information about the CRIU mailing list