qemu-ga: guest-shutdown: use only async-signal-safe functions
PATCH 1/1 qemu-ga: guest-shutdown: use only async-signal-safe functions
POSIX mandates[1] that a child process of a multi-thread program uses only async-signal-safe functions before exec(). We consider qemu-ga to be multi-thread, because it uses glib.
However, qmp_guest_shutdown() uses functions that are not async-signal-safe. Fix it the following way:
- fclose() -> reopen_fd_to_null()
- execl() -> execle()
- exit() -> _exit()
- drop slog() usage (which is not safe)
[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
--- qapi-schema-guest.json | 3 +-- qga/commands-posix.c | 17 +++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-)
qapi-schema-guest.json +1 -2
@@ -126,8 +126,7 @@ # @guest-shutdown: # # Initiate guest-activated shutdown. Note: this is an asynchronous -# shutdown request, with no guaruntee of successful shutdown. Errors -# will be logged to guest's syslog. +# shutdown request, with no guaruntee of successful shutdown. # # @mode: #optional "halt", "powerdown" (default), or "reboot" #
qga/commands-posix.c +7 -10
@@ -57,16 +57,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)if (pid ==0 ) {/* child, start the shutdown */ setsid (); -fclose (stdin); -fclose (stdout); -fclose (stderr); - - ret =execl ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , -"hypervisor initiated shutdown" , (char *)NULL ); -if (ret) { -slog ("guest-shutdown failed: %s" ,strerror (errno)); - } -exit (!!ret); +reopen_fd_to_null (0 ); +reopen_fd_to_null (1 ); +reopen_fd_to_null (2 ); + + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ); +_exit (!!ret); }else if (pid <0 ) { goto exit_err; }
Signature · 1 line
1.7.9.2.384.g4a92a
Quoted 18 lines · Luiz Capitulino
POSIX mandates[1] that a child process of a multi-thread program uses only async-signal-safe functions before
exec(). We consider qemu-ga to be multi-thread, because it uses glib.However,
qmp_guest_shutdown()uses functions that are not async-signal-safe. Fix it the following way:-
fclose()->reopen_fd_to_null()
-execl()->execle()
-exit()->_exit()
- dropslog()usage (which is not safe)[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
# @guest-shutdown:
#
# Initiate guest-activated shutdown. Note: this is an asynchronous
qapi-schema-guest.json line 129
Quoted 3 lines
qapi-schema-guest.json +1
-2-# shutdown request, with no guaruntee of successful shutdown. Errors -# will be logged to guest's syslog. +# shutdown request, with no guaruntee of successful shutdown.
As long as you are changing docs, fix the typo:
s/guaruntee/guarantee/
Quoted 8 lines
+++
b/qga/commands-posix.cqga/commands-posix.c +0
-3@@ -57,16 +57,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)if (pid ==0 ) {/* child, start the shutdown */ setsid (); -fclose (stdin); -fclose (stdout); -fclose (stderr);
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 62
Quoted 10 lines
qga/commands-posix.c +3
-7- - ret =execl ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , -"hypervisor initiated shutdown" , (char *)NULL ); -if (ret) { -slog ("guest-shutdown failed: %s" ,strerror (errno)); - } -exit (!!ret); +reopen_fd_to_null (0 ); +reopen_fd_to_null (1 ); +reopen_fd_to_null (2 );
I prefer the POSIX-mandated macros STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO, but don't know if qemu intends to rely on them (according to gnulib, at least older mingw lacked those macro names from <unistd.h>). So I won't make you change this.
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 65
Quoted 3 lines
qga/commands-posix.c +3
-0+ + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ);
Where was 'environ' declared? POSIX says that environ must exist, but that it is the one variable where you must declare it yourself rather than getting it from a public header. (For convenience, glibc declares environ in <unistd.h> when using _GNU_SOURCE, but when you are asking for strict standards namespace compliance, it disappears.)
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 66
Quoted 1 line
qga/commands-posix.c +1
-0+_exit (!!ret);
Why are we even bothering with ret? If execle() returns, we _know_ we had a failure, and !!ret will always be 1.
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 55 lines · On Mon, 14 May 2012 11:51:13 -0600 Eric Blake <[email protected]> wrote:
Quoted 18 lines · Luiz Capitulino
POSIX mandates[1] that a child process of a multi-thread program uses only async-signal-safe functions before
exec(). We consider qemu-ga to be multi-thread, because it uses glib.However,
qmp_guest_shutdown()uses functions that are not async-signal-safe. Fix it the following way:-
fclose()->reopen_fd_to_null()
-execl()->execle()
-exit()->_exit()
- dropslog()usage (which is not safe)[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
# @guest-shutdown:
#
# Initiate guest-activated shutdown. Note: this is an asynchronousqapi-schema-guest.json line 129
Quoted 3 lines
qapi-schema-guest.json +1
-2-# shutdown request, with no guaruntee of successful shutdown. Errors -# will be logged to guest's syslog. +# shutdown request, with no guaruntee of successful shutdown.As long as you are changing docs, fix the typo:
s/guaruntee/guarantee/
Quoted 8 lines
+++
b/qga/commands-posix.cqga/commands-posix.c +0
-3@@ -57,16 +57,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)if (pid ==0 ) {/* child, start the shutdown */ setsid (); -fclose (stdin); -fclose (stdout); -fclose (stderr);qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 62
Quoted 10 lines
qga/commands-posix.c +3
-7- - ret =execl ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , -"hypervisor initiated shutdown" , (char *)NULL ); -if (ret) { -slog ("guest-shutdown failed: %s" ,strerror (errno)); - } -exit (!!ret); +reopen_fd_to_null (0 ); +reopen_fd_to_null (1 ); +reopen_fd_to_null (2 );I prefer the POSIX-mandated macros
STDIN_FILENO,STDOUT_FILENO, andSTDERR_FILENO, but don't know if qemu intends to rely on them (according to gnulib, at least older mingw lacked those macro names from <unistd.h>). So I won't make you change this.qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 65
Quoted 3 lines
qga/commands-posix.c +3
-0+ + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ);
Quoted 5 lines
Where was 'environ' declared? POSIX says that environ must exist, but that it is the one variable where you must declare it yourself rather than getting it from a public header. (For convenience, glibc declares environ in <
unistd.h> when using_GNU_SOURCE, but when you are asking for strict standards namespace compliance, it disappears.)
I'll declare it then.
Quoted 5 lines
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 66
Quoted 2 lines
qga/commands-posix.c +1
-0+_exit (!!ret);Why are we even bothering with ret? If
execle()returns, we _know_ we had a failure, and !!ret will always be 1.
True, will drop it.
Quoted 11 lines · On Mon, 14 May 2012 15:01:17 -0300 Luiz Capitulino <[email protected]>…
Quoted 9 lines
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 65
Quoted 3 lines
qga/commands-posix.c +3
-0+ + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ);Where was 'environ' declared? POSIX says that environ must exist, but that it is the one variable where you must declare it yourself rather than getting it from a public header. (For convenience, glibc declares environ in <
unistd.h> when using_GNU_SOURCE, but when you are asking for strict standards namespace compliance, it disappears.)I'll declare it then.
-Wredundant-decls doesn't like it:
/home/lcapitulino/work/src/qmp-unstable/qga/commands-posix.c:38:15: warning: redundant redeclaration of ‘environ’ [-Wredundant-decls] /usr/include/unistd.h:546:15: note: previous declaration of ‘environ’ was here
LINK qemu-ga
Quoted 13 lines · Luiz Capitulino
Quoted 10 lines · On Mon, 14 May 2012 15:01:17 -0300 Luiz Capitulino <[email protected]>…
Quoted 9 lines
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 65
Quoted 3 lines
qga/commands-posix.c +3
-0+ + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ);Where was 'environ' declared? POSIX says that environ must exist, but that it is the one variable where you must declare it yourself rather than getting it from a public header. (For convenience, glibc declares environ in <
unistd.h> when using_GNU_SOURCE, but when you are asking for strict standards namespace compliance, it disappears.)
Quoted 7 lines
Quoted 1 line
I'll declare it then.
-Wredundant-decls doesn't like it:
/home/lcapitulino/work/src/qmp-unstable/qga/commands-posix.c:38:15: warning: redundant redeclaration of ‘environ’ [-Wredundant-decls]/usr/include/unistd.h:546:15: note: previous declaration of ‘environ’ was here
LINK qemu-ga
Hmm, gnulib works around that by probing for whether environ was declared at configure time, in order to conditionalize whether to output a declaration of its own; but obviously we aren't using gnulib. Maybe, since we know that the glibc declaration is guarded by _GNU_SOURCE, we could likewise guard our declaration to only occur in the situations where glibc is not declaring it? Or maybe we can factor things into a common header used by other qemu files, where the header file itself is tagged in such a way to silence gcc warnings about any possible duplicate declaration, while still leaving the .c files that use this common header clean for use of -Wredundant-decls?
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 22 lines · On Mon, 14 May 2012 12:41:16 -0600 Eric Blake <[email protected]> wrote:
Quoted 13 lines · Luiz Capitulino
Quoted 10 lines · On Mon, 14 May 2012 15:01:17 -0300 Luiz Capitulino <[email protected]>…
Quoted 9 lines
qga/commands-posix.c void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) · line 65
Quoted 3 lines
qga/commands-posix.c +3
-0+ + ret =execle ("/sbin/shutdown" ,"shutdown" , shutdown_flag,"+0" , +"hypervisor initiated shutdown" , (char *)NULL , environ);Where was 'environ' declared? POSIX says that environ must exist, but that it is the one variable where you must declare it yourself rather than getting it from a public header. (For convenience, glibc declares environ in <
unistd.h> when using_GNU_SOURCE, but when you are asking for strict standards namespace compliance, it disappears.)Quoted 7 lines
Quoted 1 line
I'll declare it then.
-Wredundant-decls doesn't like it:
/home/lcapitulino/work/src/qmp-unstable/qga/commands-posix.c:38:15: warning: redundant redeclaration of ‘environ’ [-Wredundant-decls]/usr/include/unistd.h:546:15: note: previous declaration of ‘environ’ was here
LINK qemu-ga
Quoted 10 lines
Hmm, gnulib works around that by probing for whether environ was declared at configure time, in order to conditionalize whether to output a declaration of its own; but obviously we aren't using gnulib. Maybe, since we know that the glibc declaration is guarded by
_GNU_SOURCE, we could likewise guard our declaration to only occur in the situations where glibc is not declaring it? Or maybe we can factor things into a common header used by other qemu files, where the header file itself is tagged in such a way to silence gcc warnings about any possible duplicate declaration, while still leaving the .c files that use this common header clean for use of -Wredundant-decls?
What's the worst case if we leave it as is? The build will brake if environ ends up not being declared by some arch, right? In that case we could leave it as is and fix it when it brakes :)
Quoted 4 lines · Luiz Capitulino
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
I'll declare it then.
-Wredundant-decls doesn't like it:
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
/home/lcapitulino/work/src/qmp-unstable/qga/commands-posix.c:38:15: warning: redundant redeclaration of ‘environ’ [-Wredundant-decls]/usr/include/unistd.h:546:15: note: previous declaration of ‘environ’ was here
LINK qemu-gaWhat's the worst case if we leave it as is? The build will brake if environ ends up not being declared by some arch, right? In that case we could leave it as is and fix it when it brakes :)
Yep, which is precisely why I added my reviewed-by to v2.
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org