[CRIU] [PATCH 3/5] tty: Add tty_prep_fds, tty_fini_fds helpers

Cyrill Gorcunov gorcunov at openvz.org
Thu Sep 27 08:17:40 EDT 2012


They will serve for one purpose -- to clone
self stdio descriptor and use it with tty
migration if needed.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/tty.h |    3 +++
 tty.c         |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/include/tty.h b/include/tty.h
index eccc3f2..cb860b1 100644
--- a/include/tty.h
+++ b/include/tty.h
@@ -20,4 +20,7 @@ extern void tty_setup_slavery(void);
 
 extern int tty_verify_active_pairs(void);
 
+extern int tty_prep_fds(void);
+extern void tty_fini_fds(void);
+
 #endif /* CR_TTY_H__ */
diff --git a/tty.c b/tty.c
index ee63273..acb89bc 100644
--- a/tty.c
+++ b/tty.c
@@ -80,6 +80,7 @@ struct tty_info {
 
 static LIST_HEAD(all_tty_info_entries);
 static LIST_HEAD(all_ttys);
+static int self_stdin = -1;
 
 /*
  * Usually an application has not that many ttys opened.
@@ -1055,3 +1056,34 @@ int dump_tty(struct fd_parms *p, int lfd, const struct cr_fdset *set)
 {
 	return do_dump_gen_file(p, lfd, &tty_ops, set);
 }
+
+/*
+ * FIXME
+ *
+ * Strictly speaking we don't need to prepare self stdin clone
+ * for each process, but the concept is that only one migrating tty
+ * is allowed so this helpers should be called once only in context
+ * of a process which owns migrating tty fd.
+ */
+int tty_prep_fds(void)
+{
+	self_stdin = get_service_fd(SELF_STDIN_OFF);
+
+	if (!isatty(fileno(stdin))) {
+		pr_err("Standart stream is not a terminal, aborting\n");
+		return -1;
+	}
+
+	if (dup2(fileno(stdin), self_stdin) < 0) {
+		self_stdin = -1;
+		pr_perror("Can't dup stdin to SELF_STDIN_OFF");
+		return -1;
+	}
+
+	return 0;
+}
+
+void tty_fini_fds(void)
+{
+	close_safe(&self_stdin);
+}
-- 
1.7.7.6



More information about the CRIU mailing list