Skip to contentSkip to conversation

tools/console: Error handling fixes

PATCH In review Ian Jackson 4 participants 14 messages Mar 17, 2014 – Mar 20, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tools/console: Error handling fixes

These two were deferred from 4.4.0. They should IMO be backported to 4.4.x via staging in due course.

1/2 tools/console: reset tty when xenconsole fails 2/2 tools/console: xenconsole tolerate tty errors

#1 lore raw

PATCH 1/2 tools/console: reset tty when xenconsole fails

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

CC M A Young <[email protected]>
CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/console/client/main.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +9 -2
index eb6a1a9..62159f6 100644
@@ -257,6 +257,13 @@ typedef enum {
        CONSOLE_SERIAL,
 } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+	restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
 int main(int argc, char **argv)
 {
 	struct termios attr;
@@ -383,9 +390,9 @@ int main(int argc, char **argv)
 	}
 
 	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+        atexit(restore_term_stdin); /* if this fails, oh dear */
 	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
 	free(path);
 	free(dom_path);
Signature · 1 line
1.7.10.4
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] tools/console: reset tty when xenconsole fails

Quoted 8 lines · Ian Jackson

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

Is it the case that the cause of this is still not yet known? It seemed to be explained by the subsequent patch. :-)

In any case:

Quoted 40 lines
CC M A Young <[email protected]>
CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
  tools/console/client/main.c |   11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +9 -2
index eb6a1a9..62159f6 100644
@@ -257,6 +257,13 @@ typedef enum {
         CONSOLE_SERIAL,
  } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+	restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
  int main(int argc, char **argv)
  {
  	struct termios attr;
@@ -383,9 +390,9 @@ int main(int argc, char **argv)
  	}
 
  	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+        atexit(restore_term_stdin); /* if this fails, oh dear */
  	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
  	free(path);
  	free(dom_path);
#3 lore raw

Re: [PATCH 1/2] tools/console: reset tty when xenconsole fails

George Dunlap writes ("Re: [PATCH 1/2] tools/console: reset tty when xenconsole fails"):

Quoted 5 lines · Ian Jackson
Quoted 4 lines

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

Quoted 7 lines
Quoted 4 lines

This routinely happens in Xen 4.4 RC5 with pygrub because something writes the value "" to the tty xenstore key when using xenconsole. The cause of this is not yet known, but after this patch it just results in a harmless error message.

Is it the case that the cause of this is still not yet known? It seemed to be explained by the subsequent patch. :-)

Um :-). I will fix that in the commit message.

Quoted 1 line

Thanks,
Ian.

Acked-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] tools/console: reset tty when xenconsole fails

In reply to Ian Jackson

tools/console/client/main.c int main(int argc, char **argv) · line 394

Quoted 7 lines · Ian Jackson
tools/console/client/main.c +2 -1
@@ -383,9 +390,9 @@ int main(int argc, char **argv)
 	}
 
 	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+        atexit(restore_term_stdin); /* if this fails, oh dear */

Hard tab?

Otherwise, with the commit message amended as discussed with George:

Quoted 5 lines
tools/console/client/main.c +0 -1
 	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
 	free(path);
 	free(dom_path);
#5 lore raw

PATCH 2/2 tools/console: xenconsole tolerate tty errors

In reply to Ian Jackson

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when setting up pv console "frontends". (The actual tty value is later set
by xenconsoled.) Writing an empty node is not strictly necessary to
stop the frontend from writing dangerous values here, but it is a good belt-and-braces approach.

Unfortunately this confuses xenconsole. It reads the empty value, and tries to open it as the tty. xenconsole then exits.

Fix this by having xenconsole treat an empty value the same way as no value at all.

Also, make the error opening the tty be nonfatal: we just print a warning, but do not exit. I think this is helpful in theoretical situations where xenconsole is racing with libxl and/or xenconsoled.

CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/console/client/main.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/console/client/main.c +5 -3
index 62159f6..b882345 100644
@@ -115,9 +115,11 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)
 			 * disambiguate: just read the pty path */
 			pty_path = xs_read(xs, XBT_NULL, path, &len);
 			if (pty_path != NULL) {
-				pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
-				if (pty_fd == -1)
-					err(errno, "Could not open tty `%s'", pty_path);
+				if (pty_path[0] != '\0') {
+					pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
+					if (pty_fd == -1)
+						warn("Could not open tty `%s'", pty_path);
+				}
 				free(pty_path);
 			}
 		}
Signature · 1 line
1.7.10.4
#6 lore raw

Re: [PATCH 2/2] tools/console: xenconsole tolerate tty errors

Quoted 29 lines · Ian Jackson

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when setting up pv console "frontends". (The actual tty value is later set
by xenconsoled.) Writing an empty node is not strictly necessary to
stop the frontend from writing dangerous values here, but it is a good belt-and-braces approach.

Unfortunately this confuses xenconsole. It reads the empty value, and tries to open it as the tty. xenconsole then exits.

Fix this by having xenconsole treat an empty value the same way as no value at all.

Also, make the error opening the tty be nonfatal: we just print a warning, but do not exit. I think this is helpful in theoretical situations where xenconsole is racing with libxl and/or xenconsoled.

CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>
---
 tools/console/client/main.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/console/client/main.c +0 -0
index 62159f6..b882345 100644

tools/console/client/main.c static int get_pty_fd(struct xs_handle *xs, char *path, int seconds) · line 118

Quoted 7 lines
tools/console/client/main.c +1 -3
@@ -115,9 +115,11 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)
 			 * disambiguate: just read the pty path */
 			pty_path = xs_read(xs, XBT_NULL, path, &len);
 			if (pty_path != NULL) {
-				pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
-				if (pty_fd == -1)
-					err(errno, "Could not open tty `%s'", pty_path);
+				if (pty_path[0] != '\0') {

Can the top-level condition not be

	if ( pty_path != NULL && pty_path[0] != '\0' )

avoiding the reindent?

Ack on the intent though.

Quoted 7 lines
tools/console/client/main.c +4 -0
+					pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
+					if (pty_fd == -1)
+						warn("Could not open tty `%s'", pty_path);
+				}
 				free(pty_path);
 			}
 		}
#7 lore raw

Re: [PATCH 2/2] tools/console: xenconsole tolerate tty errors

Ian Campbell writes ("Re: [PATCH 2/2] tools/console: xenconsole tolerate tty errors"):

Quoted 7 lines · Ian Jackson
Quoted 6 lines

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when setting up pv console "frontends". (The actual tty value is later set
by xenconsoled.) Writing an empty node is not strictly necessary to
stop the frontend from writing dangerous values here, but it is a good belt-and-braces approach.

...

Quoted 9 lines

tools/console/client/main.c static int get_pty_fd(struct xs_handle *xs, char *path, int seconds) · line 118

Quoted 5 lines
tools/console/client/main.c +1 -3
 			if (pty_path != NULL) {
-				pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
-				if (pty_fd == -1)
-					err(errno, "Could not open tty `%s'", pty_path);
+				if (pty_path[0] != '\0') {

Can the top-level condition not be

	if ( pty_path != NULL && pty_path[0] != '\0' )

avoiding the reindent?

That would leak pty_path. I could move free(pty_path) out of the if, though. I think that would be better.

Ian.

#8 lore raw

PATCH v2 1/2 tools/console: reset tty when xenconsole fails

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because libxl writes the value "" to the tty xenstore key when using xenconsole. After this patch this just results in a harmless error message.

---
v2: Fix whitespace error (reintroduce hard tab)
Fix commit message not to claim ignorance about root cause

---
 tools/console/client/main.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +9 -2
index eb6a1a9..7ce4f24 100644
@@ -257,6 +257,13 @@ typedef enum {
        CONSOLE_SERIAL,
 } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+	restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
 int main(int argc, char **argv)
 {
 	struct termios attr;
@@ -383,9 +390,9 @@ int main(int argc, char **argv)
 	}
 
 	init_term(spty, &attr);
-	init_term(STDIN_FILENO, &attr);
+	init_term(STDIN_FILENO, &stdin_old_attr);
+	atexit(restore_term_stdin); /* if this fails, oh dear */
 	console_loop(spty, xs, path);
-	restore_term(STDIN_FILENO, &attr);
 
 	free(path);
 	free(dom_path);
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH v2 2/2 tools/console: xenconsole tolerate tty errors

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when setting up pv console "frontends". (The actual tty value is later set
by xenconsoled.) Writing an empty node is not strictly necessary to
stop the frontend from writing dangerous values here, but it is a good belt-and-braces approach.

Unfortunately this confuses xenconsole. It reads the empty value, and tries to open it as the tty. xenconsole then exits.

Fix this by having xenconsole treat an empty value the same way as no value at all.

Also, make the error opening the tty be nonfatal: we just print a warning, but do not exit. I think this is helpful in theoretical situations where xenconsole is racing with libxl and/or xenconsoled.

CC Ian Campbell <[email protected]>
CC George Dunlap <[email protected]>

---
v2: Combine two conditions and move the free

---
 tools/console/client/main.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/console/client/main.c +3 -3
index 7ce4f24..f4c783b 100644
@@ -114,12 +114,12 @@ static int get_pty_fd(struct xs_handle *xs, char *path, int seconds)
 			/* We only watch for one thing, so no need to 
 			 * disambiguate: just read the pty path */
 			pty_path = xs_read(xs, XBT_NULL, path, &len);
-			if (pty_path != NULL) {
+			if (pty_path != NULL && pty_path[0] != '\0') {
 				pty_fd = open(pty_path, O_RDWR | O_NOCTTY);
 				if (pty_fd == -1)
-					err(errno, "Could not open tty `%s'", pty_path);
-				free(pty_path);
+					warn("Could not open tty `%s'", pty_path);
 			}
+			free(pty_path);
 		}
 	} while (pty_fd == -1 && (now = time(NULL)) < start + seconds);
 
Signature · 1 line
1.7.10.4
Acked-by on v2 2/2
#10 lore raw

Re: [PATCH v2 2/2] tools/console: xenconsole tolerate tty errors

Quoted 10 lines · Ian Jackson

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when setting up pv console "frontends". (The actual tty value is later set
by xenconsoled.) Writing an empty node is not strictly necessary to
stop the frontend from writing dangerous values here, but it is a good belt-and-braces approach.

Unfortunately this confuses xenconsole. It reads the empty value, and tries to open it as the tty. xenconsole then exits.

Quoted 8 lines

Fix this by having xenconsole treat an empty value the same way as no value at all.

Also, make the error opening the tty be nonfatal: we just print a warning, but do not exit. I think this is helpful in theoretical situations where xenconsole is racing with libxl and/or xenconsoled.

#11 lore raw

Re: [PATCH v2 2/2] tools/console: xenconsole tolerate tty errors

Ian Campbell writes ("Re: [PATCH v2 2/2] tools/console: xenconsole tolerate tty errors"):

Quoted 3 lines · Ian Jackson
Quoted 2 lines

Since 28d386fc4341 (XSA-57), libxl writes an empty value for the console tty node, with read-only permission for the guest, when

...

Quoted 1 line

Thanks, I have pushed both of these.

Ian.

#12 lore raw

Re: [PATCH v2 1/2] tools/console: reset tty when xenconsole fails

In reply to Ian Jackson
Quoted 41 lines · Ian Jackson

If xenconsole (the client program) fails, it calls err. This would previously neglect to reset the user's terminal to sanity. Use atexit to do so.

This routinely happens in Xen 4.4 RC5 with pygrub because libxl writes the value "" to the tty xenstore key when using xenconsole. After this patch this just results in a harmless error message.

---
v2: Fix whitespace error (reintroduce hard tab)
Fix commit message not to claim ignorance about root cause

---
 tools/console/client/main.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +7 -0
index eb6a1a9..7ce4f24 100644
@@ -257,6 +257,13 @@ typedef enum {
        CONSOLE_SERIAL,
 } console_type;
 
+static struct termios stdin_old_attr;
+
+static void restore_term_stdin(void)
+{
+       restore_term(STDIN_FILENO, &stdin_old_attr);
+}
+
 int main(int argc, char **argv)
 {
        struct termios attr;

tools/console/client/main.c int main(int argc, char **argv) · line 394

Quoted 6 lines
tools/console/client/main.c +2 -1
@@ -383,9 +390,9 @@ int main(int argc, char **argv)
        }
 
        init_term(spty, &attr);
-       init_term(STDIN_FILENO, &attr);
+       init_term(STDIN_FILENO, &stdin_old_attr);
+       atexit(restore_term_stdin); /* if this fails, oh dear */

You may want to consider using a signal handler too. atexit() isn't called when exiting due to SIGTERM or SIGSEGV.

Regards,

Anthony Liguori

Quoted 13 lines
tools/console/client/main.c +0 -2
        console_loop(spty, xs, path);
-       restore_term(STDIN_FILENO, &attr);
 
        free(path);
        free(dom_path);
--

1.7.10.4

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

Re: [PATCH v2 1/2] tools/console: reset tty when xenconsole fails

Anthony Liguori writes ("Re: [Xen-devel] [PATCH v2 1/2] tools/console: reset tty when xenconsole fails"):

Quoted 1 line · Ian Jackson

...

Quoted 7 lines

tools/console/client/main.c int main(int argc, char **argv) · line 394

Quoted 4 lines
        init_term(spty, &attr);
tools/console/client/main.c +2 -1
-       init_term(STDIN_FILENO, &attr);
+       init_term(STDIN_FILENO, &stdin_old_attr);
+       atexit(restore_term_stdin); /* if this fails, oh dear */

You may want to consider using a signal handler too. atexit() isn't called when exiting due to SIGTERM or SIGSEGV.

That's would be quite exciting. I doubt tcsetattr is async-signal safe. We could prat about with sigprocmask and pselect perhaps, but that's more than I want to do right now (or, indeed, soon).

In practice, the shell will normally restore the terminal in this situation.

Ian.