xenconsole: Allow non-interactive use
PATCH 1/1 xenconsole: Allow non-interactive use
If xenconsole is run with stdin closed or redirected to /dev/null, console_loop() will return immediately due to failure to read from STDIN_FILENO. This patch tests if stdin and stdout are both connected to a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.
Existing behaviour when xenconsole is run from a terminal does not change.
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
--- tools/console/client/main.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
tools/console/client/main.c +18 -6
@@ -168,7 +168,8 @@ static void restore_term(int fd, struct termios *old)tcsetattr (fd,TCSANOW , old); } -static int console_loop (int fd,struct xs_handle *xs,char *pty_path) +static int console_loop (int fd,struct xs_handle *xs,char *pty_path, +bool interactive) {int ret, xs_fd =xs_fileno (xs), max_fd;
@@ -176,8 +177,13 @@ static int console_loop(int fd, struct xs_handle *xs, char *pty_path)fd_set fds;FD_ZERO (&fds); -FD_SET (STDIN_FILENO , &fds); - max_fd =STDIN_FILENO ; +if (interactive) { +FD_SET (STDIN_FILENO , &fds); + max_fd =STDIN_FILENO ; + } +else { + max_fd =-1 ; + }FD_SET (xs_fd, &fds);if (xs_fd > max_fd) max_fd = xs_fd;if (fd !=-1 )FD_SET (fd, &fds);
@@ -284,6 +290,10 @@ int main(int argc, char **argv)struct xs_handle *xs;char *end;console_type type =CONSOLE_INVAL ; +bool interactive =0 ; + +if (isatty (STDIN_FILENO ) &&isatty (STDOUT_FILENO )) + interactive =1 ;while ((ch =getopt_long (argc, argv, sopt, lopt, &opt_ind)) !=-1 ) {switch (ch) {
@@ -390,9 +400,11 @@ int main(int argc, char **argv) }init_term (spty, &attr); -init_term (STDIN_FILENO , &stdin_old_attr); -atexit (restore_term_stdin);/* if this fails, oh dear */ -console_loop (spty, xs, path); +if (interactive) { +init_term (STDIN_FILENO , &stdin_old_attr); +atexit (restore_term_stdin);/* if this fails, oh dear */ + } +console_loop (spty, xs, path, interactive);free (path);free (dom_path);
Signature · 1 line
2.1.4
Quoted 10 lines · Martin Lucina
If xenconsole is run with stdin closed or redirected to
/dev/null,console_loop()will return immediately due to failure to read fromSTDIN_FILENO. This patch tests if stdin and stdout are both connected to
a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.Existing behaviour when xenconsole is run from a terminal does not change.
Quoted 8 lines
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
Signed-off-by Martin Lucina <[email protected]>Cc Ian Jackson <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Ian Campbell <[email protected]>Cc Wei Liu <[email protected]>
WRT the 4.6 freeze I'm torn between calling this a feature or a bugfix.
A pair of nits, which probably aren't worth acting on:
Quoted 4 lines
tools/console/client/main.c +0
-0@@ -176,8 +177,13 @@ static int console_loop(int fd, struct xs_handle *xs, char *pty_path)fd_set fds;
tools/console/client/main.c static int console_loop(int fd, struct xs_handle · line 187
Quoted 10 lines
tools/console/client/main.c +7
-2FD_ZERO (&fds); -FD_SET (STDIN_FILENO , &fds); - max_fd =STDIN_FILENO ; +if (interactive) { +FD_SET (STDIN_FILENO , &fds); + max_fd =STDIN_FILENO ; + } +else { + max_fd =-1 ; + }
Looking at the rest of the file and tools/console subtree it seems the prevailing coding style is:
} else
max_fd = -1;(i.e. } brace on the same line as the else and no {} for single statements after an else).
But maybe it would be better to set max_fd = -1 on declaration and do the max dance here as with the following cases?
Ian.
Quoted 20 lines · Ian Campbell
Quoted 10 lines · Martin Lucina
If xenconsole is run with stdin closed or redirected to
/dev/null,console_loop()will return immediately due to failure to read fromSTDIN_FILENO. This patch tests if stdin and stdout are both connected to
a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.Existing behaviour when xenconsole is run from a terminal does not change.
Quoted 8 lines
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
Signed-off-by Martin Lucina <[email protected]>Cc Ian Jackson <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Ian Campbell <[email protected]>Cc Wei Liu <[email protected]>
Quoted 4 lines
Acked-by Ian Campbell <[email protected]>WRT the 4.6 freeze I'm torn between calling this a feature or a bugfix.
I'm inclined to say it's a bugfix. It is reasonable that users want to pipe console output to logging daemon.
Wei.
Quoted 30 lines
A pair of nits, which probably aren't worth acting on:
Quoted 4 lines
tools/console/client/main.c +0
-0@@ -176,8 +177,13 @@ static int console_loop(int fd, struct xs_handle *xs, char *pty_path)fd_set fds;tools/console/client/main.c static int console_loop(int fd, struct xs_handle · line 187
Quoted 10 lines
tools/console/client/main.c +7
-2FD_ZERO (&fds); -FD_SET (STDIN_FILENO , &fds); - max_fd =STDIN_FILENO ; +if (interactive) { +FD_SET (STDIN_FILENO , &fds); + max_fd =STDIN_FILENO ; + } +else { + max_fd =-1 ; + }Looking at the rest of the file and
tools/consolesubtree it seems the prevailing coding style is:} else max_fd = -1;(i.e. } brace on the same line as the else and no {} for single statements after an else).
But maybe it would be better to set
max_fd= -1 on declaration and do the max dance here as with the following cases?Ian.
Quoted 44 lines · Ian Campbell
Quoted 10 lines · Martin Lucina
If xenconsole is run with stdin closed or redirected to
/dev/null,console_loop()will return immediately due to failure to read fromSTDIN_FILENO. This patch tests if stdin and stdout are both connected to
a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.Existing behaviour when xenconsole is run from a terminal does not change.
Quoted 8 lines
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
Signed-off-by Martin Lucina <[email protected]>Cc Ian Jackson <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Ian Campbell <[email protected]>Cc Wei Liu <[email protected]>Acked-by Ian Campbell <[email protected]>WRT the 4.6 freeze I'm torn between calling this a feature or a bugfix.
A pair of nits, which probably aren't worth acting on:
Quoted 4 lines
tools/console/client/main.c +0
-0@@ -176,8 +177,13 @@ static int console_loop(int fd, struct xs_handle *xs, char *pty_path)fd_set fds;tools/console/client/main.c static int console_loop(int fd, struct xs_handle · line 187
Quoted 10 lines
tools/console/client/main.c +7
-2FD_ZERO (&fds); -FD_SET (STDIN_FILENO , &fds); - max_fd =STDIN_FILENO ; +if (interactive) { +FD_SET (STDIN_FILENO , &fds); + max_fd =STDIN_FILENO ; + } +else { + max_fd =-1 ; + }Looking at the rest of the file and
tools/consolesubtree it seems the prevailing coding style is:
Quoted 8 lines
} else max_fd = -1;(i.e. } brace on the same line as the else and no {} for single statements after an else).
But maybe it would be better to set
max_fd= -1 on declaration and do the max dance here as with the following cases?
Declaring max_fd = -1 is indeed clearer, I can do a v2 with that change if you like.
One other bug that my change makes potentially easier to trigger is that you can run "xl console DOMID" multiple times with the same DOMID and the result is broken; each instance gets part of the data written to the console.
Any ideas on how to address this in a simple fashion?
Martin
Quoted 6 lines · Martin Lucina
Quoted 3 lines
But maybe it would be better to set
max_fd= -1 on declaration and do
the max dance here as with the following cases?Declaring
max_fd= -1 is indeed clearer, I can do a v2 with that change if you like.
If you are happy to then yes, please.
Quoted 6 lines
One other bug that my change makes potentially easier to trigger is that you can run "xl console DOMID" multiple times with the same DOMID and the result is broken; each instance gets part of the data written to the console.
Any ideas on how to address this in a simple fashion?
Perhaps the client should take some exclusive lock (fcntl based?) on an fd of an open file with domid in the name. Failure to get the lock should result in the client exiting with some message indicating the console is in use.
Ian.
PATCH v2 1/1 xenconsole: Allow non-interactive use
If xenconsole is run with stdin closed or redirected to /dev/null, console_loop() will return immediately due to failure to read from STDIN_FILENO. This patch tests if stdin and stdout are both connected to a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.
Existing behaviour when xenconsole is run from a terminal does not change.
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
--- tools/console/client/main.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
tools/console/client/main.c +16 -7
@@ -168,16 +168,19 @@ static void restore_term(int fd, struct termios *old)tcsetattr (fd,TCSANOW , old); } -static int console_loop (int fd,struct xs_handle *xs,char *pty_path) +static int console_loop (int fd,struct xs_handle *xs,char *pty_path, +bool interactive) { -int ret, xs_fd =xs_fileno (xs), max_fd; +int ret, xs_fd =xs_fileno (xs), max_fd =-1 ; do { fd_set fds; FD_ZERO(&fds); - FD_SET(STDIN_FILENO, &fds); - max_fd = STDIN_FILENO; + if (interactive) { + FD_SET(STDIN_FILENO, &fds); + max_fd = STDIN_FILENO; + } FD_SET(xs_fd, &fds); if (xs_fd > max_fd) max_fd = xs_fd; if (fd != -1) FD_SET(fd, &fds);
@@ -284,6 +287,10 @@ int main(int argc, char **argv)struct xs_handle *xs;char *end;console_type type =CONSOLE_INVAL ; +bool interactive =0 ; + +if (isatty (STDIN_FILENO ) &&isatty (STDOUT_FILENO )) + interactive =1 ;while ((ch =getopt_long (argc, argv, sopt, lopt, &opt_ind)) !=-1 ) {switch (ch) {
@@ -390,9 +397,11 @@ int main(int argc, char **argv) }init_term (spty, &attr); -init_term (STDIN_FILENO , &stdin_old_attr); -atexit (restore_term_stdin);/* if this fails, oh dear */ -console_loop (spty, xs, path); +if (interactive) { +init_term (STDIN_FILENO , &stdin_old_attr); +atexit (restore_term_stdin);/* if this fails, oh dear */ + } +console_loop (spty, xs, path, interactive);free (path);free (dom_path);
Signature · 1 line
2.1.4
Quoted 10 lines · Martin Lucina
If xenconsole is run with stdin closed or redirected to
/dev/null,console_loop()will return immediately due to failure to read fromSTDIN_FILENO. This patch tests if stdin and stdout are both connected to
a TTY and, if not, xenconsole will not attempt to read from stdin or modify stdout terminal attributes.Existing behaviour when xenconsole is run from a terminal does not change.
Quoted 8 lines
This allows for non-interactive use, eg. running "xl create -c" under systemd or piping the output of "xl console" to another command.
Signed-off-by Martin Lucina <[email protected]>Cc Ian Jackson <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Ian Campbell <[email protected]>Cc Wei Liu <[email protected]>
Acked + applied, thanks.
Quoted 16 lines · Ian Campbell
Quoted 6 lines · Martin Lucina
Quoted 3 lines
But maybe it would be better to set
max_fd= -1 on declaration and do
the max dance here as with the following cases?Declaring
max_fd= -1 is indeed clearer, I can do a v2 with that change if you like.If you are happy to then yes, please.
Quoted 5 lines
One other bug that my change makes potentially easier to trigger is that you can run "xl console DOMID" multiple times with the same DOMID and the result is broken; each instance gets part of the data written to the console.
Quoted 6 lines
Quoted 1 line
Any ideas on how to address this in a simple fashion?
Perhaps the client should take some exclusive lock (fcntl based?) on an fd of an open file with domid in the name. Failure to get the lock should result in the client exiting with some message indicating the console is in use.
That begs the question of where to put the lock file. eg. NetBSD does not seem to have /var/lock.
I tried using flock(pty_fd, LOCK_EX | LOCK_NB) locks directly on the pty device and contrary to the documentation claiming support for regular files only it worked fine. I tested using the xenconsole code on my Debian dom0, and flock(1) on NetBSD. Would you consider this approach?
Martin
Quoted 30 lines · Martin Lucina
Quoted 20 lines · Ian Campbell
Quoted 7 lines · Martin Lucina
Quoted 4 lines
But maybe it would be better to set
max_fd= -1 on declaration and
do
the max dance here as with the following cases?Declaring
max_fd= -1 is indeed clearer, I can do a v2 with that change if you like.If you are happy to then yes, please.
Quoted 8 lines
One other bug that my change makes potentially easier to trigger is that
you can run "xl console DOMID" multiple times with the same DOMID and the
result is broken; each instance gets part of the data written to the
console.Quoted 8 lines
Quoted 1 line
Any ideas on how to address this in a simple fashion?
Perhaps the client should take some exclusive lock (fcntl based?) on an
fd of an open file with domid in the name. Failure to get the lock should result in the client exiting with some message indicating the
console is in use.
Quoted 3 lines
That begs the question of where to put the lock file. eg. NetBSD does not
seem to have/var/lock.
Configure ends up defining XEN_LOCK_DIR, so it should go in there alongside the other locks.
Quoted 4 lines
I tried using
flock(pty_fd, LOCK_EX | LOCK_NB)locks directly on the pty device and contrary to the documentation claiming support for regular files only it worked fine. I tested using the xenconsole code on my Debian dom0, and flock(1) on NetBSD. Would you consider this approach?
Only if that functionality was required by POSIX IMHO.
Ian.
PATCH 1/1 xenconsole: Ensure exclusive access to console using locks
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by creating and obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
--- tools/console/Makefile | 2 +- tools/console/client/main.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)
tools/console/Makefile +1 -1
@@ -3,7 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk CFLAGS += -Werror -CFLAGS += $(CFLAGS_libxenctrl) +CFLAGS += $(CFLAGS_libxenctrl) -I$(XEN_ROOT)/tools/libxc CFLAGS += $(CFLAGS_libxenstore) LDLIBS += $(LDLIBS_libxenctrl) LDLIBS += $(LDLIBS_libxenstore)
tools/console/client/main.c +31 -0
@@ -18,6 +18,7 @@* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \*/ +#include <sys/file.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h>
@@ -40,10 +41,13 @@#include <xenstore.h> #include "xenctrl.h" +#include "_paths.h" #define ESCAPE_CHARACTER 0x1dstatic volatile sig_atomic_t received_signal =0 ; +static char *lockfile =NULL ; +static int lockfd =-1 ;static void sighandler (int signum) {
@@ -267,6 +271,30 @@ static void restore_term_stdin(void)restore_term (STDIN_FILENO , &stdin_old_attr); } +static void console_lock (int domid) +{ + lockfile =malloc (PATH_MAX ); +if (lockfile ==NULL ) +err (ENOMEM ,"malloc" ); +snprintf (lockfile,PATH_MAX -1 ,"%s/xenconsole.%d" ,XEN_LOCK_DIR , domid); + + lockfd =open (lockfile,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR ); +if (lockfd ==-1 ) +err (errno,"Could not open %s" , lockfile); +if (flock (lockfd,LOCK_EX |LOCK_NB ) !=0 ) +err (errno,"Could not lock %s" , lockfile); +} + +static void console_unlock (void ) +{ +if (lockfd !=-1 ) { +flock (lockfd,LOCK_UN ); +close (lockfd); + } +if (lockfile !=NULL ) +unlink (lockfile); +} +int main (int argc,char **argv) {struct termios attr;
@@ -382,6 +410,9 @@ int main(int argc, char **argv)exit (EINVAL ); } +console_lock (domid); +atexit (console_unlock); +/* Set a watch on this domain's console pty */ if (!xs_watch (xs, path,"" ))err (errno,"Can't set watch for console pty" );
Signature · 1 line
2.1.4
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
Quoted 21 lines · Martin Lucina
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by creating and obtaining an exclusive
lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.Signed-off-by Martin Lucina <[email protected]>Cc Ian Jackson <[email protected]>Cc Stefano Stabellini <[email protected]>Cc Ian Campbell <[email protected]>Cc Wei Liu <[email protected]>--- tools/console/Makefile | 2 +- tools/console/client/main.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)tools/console/Makefile +0
-0
tools/console/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 6
Quoted 5 lines
tools/console/Makefile +1
-1@@ -3,7 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk CFLAGS += -Werror -CFLAGS += $(CFLAGS_libxenctrl) +CFLAGS += $(CFLAGS_libxenctrl) -I$(XEN_ROOT)/tools/libxc
I'm afraid this (delving into another components private headers) isn't allowed. Instead you should add the two lines to tools/console/Makefile to generate a local _paths.h:
genpath-target = $(call buildmakevars2header,_paths.h) $(eval $(genpath-target))
Plus a dependency on it in the xenconsole rule and a .gitignore entry.
You might want to generate client/_paths.h instead to avoid needing to muck around with CFLAGS.
Quoted 30 lines
tools/console/Makefile +0
-0CFLAGS += $(CFLAGS_libxenstore) LDLIBS += $(LDLIBS_libxenctrl) LDLIBS += $(LDLIBS_libxenstore)tools/console/client/main.c +4
-0@@ -18,6 +18,7 @@* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \*/ +#include #include #include #include@@ -40,10 +41,13 @@ #include#include "xenctrl.h" +#include "_paths.h" #define ESCAPE_CHARACTER 0x1dstatic volatile sig_atomic_t received_signal =0 ; +static char *lockfile =NULL ; +static int lockfd =-1 ;static void sighandler (int signum) {
tools/console/client/main.c static void restore_term_stdin(void) · line 278
Quoted 8 lines
tools/console/client/main.c +5
-0@@ -267,6 +271,30 @@ static void restore_term_stdin(void) > > restore_term(STDIN_FILENO, &stdin_old_attr); } +static void console_lock (int domid) +{ +> > lockfile = malloc(PATH_MAX); +> > if (lockfile == NULL) +> > > err(ENOMEM, "malloc");
malloc sets errno, so I think you can pass that here as well.
Also, a static buffer would be fine in this context I think.
tools/console/client/main.c static void restore_term_stdin(void) · line 279
Quoted 1 line
tools/console/client/main.c +1
-0+snprintf (lockfile,PATH_MAX -1 ,"%s/xenconsole.%d" ,XEN_LOCK_DIR , domid);
I think you can use string concatenation here, e.g.
XEN_LOCK_DIR "/xenconsole.%d"
Given the known limits on the size of domid it would probably be possible to figure out a tighter limit than PATH_MAX.
Quoted 6 lines
tools/console/client/main.c +6
-0+ +> > lockfd = open(lockfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); +> > if (lockfd == -1) +> > > err(errno, "Could not open %s", lockfile); +> > if (flock(lockfd, LOCK_EX|LOCK_NB) != 0) +> > > err(errno, "Could not lock %s", lockfile);
tools/console/client/main.c static void restore_term_stdin(void) · line 295
Quoted 10 lines
tools/console/client/main.c +10
-0+} + +static void console_unlock (void ) +{ +> > if (lockfd != -1) { +> > > flock(lockfd, LOCK_UN); +> > > close(lockfd); +> > } +> > if (lockfile != NULL) +> > > unlink(lockfile);
I think this introduces a race, if another client arrives between the unlock/close and the unlink then you will delete the file which that second client now has a lock on, resulting in a third client being able to take the lock (by creating a new file) when it should already be locked.
I think the answer is to either not remove the lockfile or to do so _before_ dropping the lock, which makes the unlink itself the effective unlock point.
Ian.
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
Ian Campbell writes ("Re: [PATCH] xenconsole: Ensure exclusive access to console using locks"):
Quoted 1 line
I think the answer is to either not remove the lockfile
That is a bit messy (although not entirely wrong).
Quoted 2 lines
or to do so _before_ dropping the lock, which makes the unlink itself the effective unlock point.
This is necessary but not sufficient. With only that change:
A open flock unlink close
B open flock .o{ I have the lock }
C open flock .o{ I have the lock }
B is mistaken because it olds an fd onto an unlinked file.
Ian.
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
In reply to Ian CampbellQuoted 11 lines · Ian Campbell
I'm afraid this (delving into another components private headers) isn't allowed. Instead you should add the two lines to
tools/console/Makefileto generate a local_paths.h:genpath-target = $(call buildmakevars2header,_paths.h) $(eval $(genpath-target))
Plus a dependency on it in the xenconsole rule and a .gitignore entry.
You might want to generate
client/_paths.hinstead to avoid needing to muck around with CFLAGS.
The above works, but causes genpath-target to be re-run each time "make" is called in tools/console, i.e. no longer says "Nothing to be done for all". I presume that's ok?
Quoted 6 lines
I think you can use string concatenation here, e.g.
XEN_LOCK_DIR"/xenconsole.%d"Given the known limits on the size of domid it would probably be possible to figure out a tighter limit than
PATH_MAX.
Ok, I can just make it the usual 255 ...
Martin
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
Quoted 10 lines · Martin Lucina
Quoted 9 lines · Ian Campbell
I'm afraid this (delving into another components private headers) isn't allowed. Instead you should add the two lines to
tools/console/Makefileto generate a local_paths.h:genpath-target = $(call buildmakevars2header,_paths.h) $(eval $(genpath-target))
Plus a dependency on it in the xenconsole rule and a .gitignore entry.
Quoted 8 lines
Quoted 2 lines
You might want to generate
client/_paths.hinstead to avoid needing to muck around with CFLAGS.The above works, but causes genpath-target to be re-run each time "make" is
called intools/console, i.e. no longer says "Nothing to be done for all".
I presume that's ok?
I think so, yes.
Quoted 8 lines
Quoted 6 lines
I think you can use string concatenation here, e.g.
XEN_LOCK_DIR"/xenconsole.%d"Given the known limits on the size of domid it would probably be possible to figure out a tighter limit than
PATH_MAX.Ok, I can just make it the usual 255 ...
Or sizeof(XEN_LOCK_DIR"/xenconsole.") + 8 ? (8 being more digits that any valid domid can have), which avoids problems with people putting the lock dir somewhere ludicrous.
Ian.
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
In reply to Martin LucinaMartin Lucina writes ("[PATCH] xenconsole: Ensure exclusive access to console using locks"):
Quoted 4 lines
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by creating and obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
It is a shame that we are still using ptys for the xenconsole connection. If it were a socket we could allow as many clients as we like. But I haven't got round to fixing this for the last n years so I think the general plan you have makes sense.
tools/console/client/main.c static void restore_term_stdin(void) · line 279
Quoted 6 lines
tools/console/client/main.c +6
-0+static void console_lock (int domid) +{ + lockfile =malloc (PATH_MAX ); +if (lockfile ==NULL ) +err (ENOMEM ,"malloc" ); +snprintf (lockfile,PATH_MAX -1 ,"%s/xenconsole.%d" ,XEN_LOCK_DIR , domid);
Why not use asprintf ?
tools/console/client/main.c static void restore_term_stdin(void) · line 286
Quoted 6 lines
tools/console/client/main.c +6
-0+ lockfd =open (lockfile,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR ); +if (lockfd ==-1 ) +err (errno,"Could not open %s" , lockfile); +if (flock (lockfd,LOCK_EX |LOCK_NB ) !=0 ) +err (errno,"Could not lock %s" , lockfile); +}
This locking strategy is not safe if the lockfile is ever unlinked, because it allows:
A open flock .o{ I have the lock }
B unlink
C open flock .o{ I have the lock }tools/console/client/main.c static void restore_term_stdin(void) · line 295
Quoted 8 lines
tools/console/client/main.c +8
-0+static void console_unlock (void ) +{ +if (lockfd !=-1 ) { +flock (lockfd,LOCK_UN ); +close (lockfd); + } +if (lockfile !=NULL ) +unlink (lockfile);
And this unlinking strategy is not safe even if we are more careful with our locking. You must only unlink with the lock held.
You should use the same recipe as with-lock-ex (from chiark-utils)
(which we also use in tools/hotplug/Linux/locking.sh and tools/libxl/libxl_internal.c:libxl__lock_domain_userdata)
Ian.
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
Quoted 8 lines · Ian Jackson
tools/console/client/main.c static void restore_term_stdin(void) · line 279
Quoted 6 lines
tools/console/client/main.c +6
-0+static void console_lock (int domid) +{ + lockfile =malloc (PATH_MAX ); +if (lockfile ==NULL ) +err (ENOMEM ,"malloc" ); +snprintf (lockfile,PATH_MAX -1 ,"%s/xenconsole.%d" ,XEN_LOCK_DIR , domid);Why not use asprintf ?
asprintf is a GNU/BSD extension, not available on e.g. Solaris. Do we care about that?
Quoted 6 lines
You should use the same recipe as with-lock-ex (from chiark-utils)
(which we also use in
tools/hotplug/Linux/locking.shand tools/libxl/libxl_internal.c:libxl__lock_domain_userdata)
Thanks for the pointers, I'll use that strategy.
Martin
PATCH v2 1/1 xenconsole: Ensure exclusive access to console using locks
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
The locking strategy used is based on tools/libxl/libxl_internal.c:libxl__lock_domain_userdata().
--- .gitignore | 1 + tools/console/Makefile | 5 ++++- tools/console/client/main.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-)
.gitignore +1 -0
@@ -100,6 +100,7 @@ tools/blktap2/vhd/vhd-update tools/blktap2/vhd/vhd-util tools/console/xenconsole tools/console/xenconsoled +tools/console/client/_paths.h tools/debugger/gdb/gdb-6.2.1-linux-i386-xen/* tools/debugger/gdb/gdb-6.2.1/* tools/debugger/gdb/gdb-6.2.1.tar.bz2
tools/console/Makefile +4 -1
@@ -28,9 +28,12 @@ distclean: clean xenconsoled: $(patsubst %.c,%.o,$(wildcard daemon/*.c)) $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsoled) $(APPEND_LDFLAGS) -xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c)) +xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c)) client/_paths.h $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsole) $(APPEND_LDFLAGS) +genpath-target = $(call buildmakevars2header,client/_paths.h) +$(eval $(genpath-target)) + .PHONY: install install: $(BIN) $(INSTALL_DIR) $(DESTDIR)/$(sbindir)
tools/console/client/main.c +53 -0
82 lines not on this page · raw message
Signature · 1 line
2.1.4
Re: [PATCH v2] xenconsole: Ensure exclusive access to console using locks
Martin Lucina writes ("[PATCH v2] xenconsole: Ensure exclusive access to console using locks"):
Quoted 4 lines
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
...
tools/console/client/main.c static void restore_term_stdin(void) · line 318
Quoted 7 lines
tools/console/client/main.c +7
-0+static void console_unlock (void ) +{ +if (lockfile[0 ]) +unlink (lockfile); +if (lockfd !=-1 ) +close (lockfd); +}
If called when unlocked this could unlink the lockfile anyway.
Ian.
PATCH v3 1/1 xenconsole: Ensure exclusive access to console using locks
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
The locking strategy used is based on tools/libxl/libxl_internal.c:libxl__lock_domain_userdata().
--- .gitignore | 1 + tools/console/Makefile | 6 ++++- tools/console/client/main.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-)
.gitignore +1 -0
@@ -100,6 +100,7 @@ tools/blktap2/vhd/vhd-update tools/blktap2/vhd/vhd-util tools/console/xenconsole tools/console/xenconsoled +tools/console/client/_paths.h tools/debugger/gdb/gdb-6.2.1-linux-i386-xen/* tools/debugger/gdb/gdb-6.2.1/* tools/debugger/gdb/gdb-6.2.1.tar.bz2
tools/console/Makefile +5 -1
@@ -21,6 +21,7 @@ all: $(BIN) clean: $(RM) *.a *.so *.o *.rpm $(BIN) $(DEPS) $(RM) client/*.o daemon/*.o + $(RM) client/_paths.h .PHONY: distclean distclean: clean
@@ -28,9 +29,12 @@ distclean: clean xenconsoled: $(patsubst %.c,%.o,$(wildcard daemon/*.c)) $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsoled) $(APPEND_LDFLAGS) -xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c)) +xenconsole: client/_paths.h $(patsubst %.c,%.o,$(wildcard client/*.c)) $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsole) $(APPEND_LDFLAGS) +genpath-target = $(call buildmakevars2header,client/_paths.h) +$(eval $(genpath-target)) + .PHONY: install install: $(BIN) $(INSTALL_DIR) $(DESTDIR)/$(sbindir)
tools/console/client/main.c +55 -0
84 lines not on this page · raw message
Signature · 1 line
2.1.4
Re: [PATCH v3] xenconsole: Ensure exclusive access to console using locks
Martin Lucina writes ("[PATCH v3] xenconsole: Ensure exclusive access to console using locks"):
Quoted 4 lines
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
Re: [PATCH v3] xenconsole: Ensure exclusive access to console using locks
Quoted 7 lines · Ian Jackson
Martin Lucina writes ("[PATCH v3] xenconsole: Ensure exclusive access to console using locks"):
Quoted 4 lines
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
Acked-by Ian Jackson <[email protected]>
Can this also make it into the 4.6 release on the grounds of being a bugfix for xenconsole, or is this change too invasive?
Martin
Re: [PATCH v3] xenconsole: Ensure exclusive access to console using locks
Quoted 7 lines · Martin Lucina
Quoted 6 lines · Ian Jackson
Martin Lucina writes ("[PATCH v3] xenconsole: Ensure exclusive access to console using locks"):
Quoted 4 lines
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
Quoted 5 lines
Quoted 1 line
Acked-by Ian Jackson <[email protected]>Can this also make it into the 4.6 release on the grounds of being a bugfix for xenconsole, or is this change too invasive?
I would say this is a bugfix. The locking pattern and implementation are well understood. The risk is minimal.
Quoted 1 line
Martin
Ian Campbell
AppliedRe: [PATCH v3] xenconsole: Ensure exclusive access to console using locks
Quoted 17 lines · Wei Liu
Quoted 9 lines · Martin Lucina
Quoted 8 lines · Ian Jackson
Martin Lucina writes ("[PATCH v3] xenconsole: Ensure exclusive access to console using locks"):
Quoted 5 lines
If more than one instance of xenconsole is run against the same DOMID
then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.Quoted 6 lines
Quoted 1 line
Acked-by Ian Jackson <[email protected]>Can this also make it into the 4.6 release on the grounds of being a bugfix
for xenconsole, or is this change too invasive?
Quoted 4 lines
I would say this is a bugfix. The locking pattern and implementation are well understood. The risk is minimal.
Release-acked-by Wei Liu <[email protected]>
Applied, thanks.
Re: [PATCH v2] xenconsole: Ensure exclusive access to console using locks
In reply to Martin LucinaQuoted 7 lines · Martin Lucina
If more than one instance of xenconsole is run against the same DOMID then each instance will only get some data. This change ensures exclusive access to the console by obtaining an exclusive lock on <XEN_LOCK_DIR>/xenconsole.<DOMID>.
The locking strategy used is based on tools/libxl/libxl_internal.c:libxl__lock_domain_userdata().
I'll let Ian deal with reviewing the locking but, only one small comment from me:
tools/console/Makefile
Quoted 5 lines
tools/console/Makefile +0
-0
I forgot to mention adding the paths file to the clean rule too, sorry.
Re: [PATCH] xenconsole: Ensure exclusive access to console using locks
In reply to Ian JacksonQuoted 11 lines · Ian Jackson
Quoted 9 lines
tools/console/client/main.c +6
-0+static void console_lock (int domid) +{ + lockfile =malloc (PATH_MAX ); +if (lockfile ==NULL ) +err (ENOMEM ,"malloc" ); + snprintf(lockfile, PATH_MAX - 1, "%s/xenconsole.%d",
XEN_LOCK_DIR, domid);Why not use asprintf ?
I thought that, but it isn't generally available (not on one of the BSDs, I think) e.g. libxl provides its own (libxl_osdep.c) if configure says it isn't available.
It's a shame that isn't more generally useful throughout the tree.
But IMHO a static buffer should be fine for this use case.
Ian.