tools/console: Error handling fixes
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
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.
--- tools/console/client/main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tools/console/client/main.c +9 -2
@@ -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
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
Reported-by M A Young <[email protected]>Signed-off-by Ian Jackson <[email protected]>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@@ -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);
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
Acked-by George Dunlap <[email protected]>
Thanks,
Ian.
Re: [PATCH 1/2] tools/console: reset tty when xenconsole fails
In reply to Ian Jacksontools/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
-1console_loop (spty, xs, path); -restore_term (STDIN_FILENO , &attr);free (path);free (dom_path);
PATCH 2/2 tools/console: xenconsole tolerate tty errors
In reply to Ian JacksonSince 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.
--- tools/console/client/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/console/client/main.c +5 -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' ) { + 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
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.
Signed-off-by Ian Jackson <[email protected]>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
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); } }
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
-3if (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.
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
@@ -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
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.
---
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
@@ -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
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.
Signed-off-by Ian Jackson <[email protected]>
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
Acked-by Ian Campbell <[email protected]>
Thanks, I have pushed both of these.
Ian.
Re: [PATCH v2 1/2] tools/console: reset tty when xenconsole fails
In reply to Ian JacksonQuoted 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.
Reported-by M A Young <[email protected]>Signed-off-by Ian Jackson <[email protected]>CC M A Young <[email protected]>CC Ian Campbell <[email protected]>Acked-by George Dunlap <[email protected]>Acked-by Ian Campbell <[email protected]>---
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@@ -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
-2console_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
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.