From: pbonzini@redhat.com # HG changeset patch # User Paolo Bonzini <pbonzini@redhat.com> # Date 1260787292 -3600 # Node ID 64b613c3664688602fa726f0910ba4bc620cc577 # Parent d44371e6e5d631f58d9a2ce829f12dafd1272f68 Avoid dumping core in xen-detect F12 introduces a tool to automatically report bugs when there are core dumps. Since xen-detect relies on fork+waitpid in order to trap a SIGILL from a child, every time someone runs xen-detect on a bare metal kernel a bug is reported into Red Hat''s Bugzilla. :-) However, even without this contingent need, leaving core dumps around is not nice. So this patch just traps SIGILL using signal/sentjmp/longjmp, without the need to fork. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> diff --git a/tools/misc/xen-detect.c b/tools/misc/xen-detect.c --- a/tools/misc/xen-detect.c +++ b/tools/misc/xen-detect.c @@ -29,9 +29,11 @@ #include <stdlib.h> #include <string.h> #include <sys/types.h> -#include <sys/wait.h> +#include <setjmp.h> +#include <signal.h> #include <unistd.h> +jmp_buf j; static int pv_context; static void cpuid(uint32_t idx, @@ -74,6 +76,11 @@ return 1; } +void sigill_handler (int sig) +{ + longjmp(j, 1); +} + int main(void) { pid_t pid; @@ -84,33 +91,18 @@ if ( check_for_xen() ) return 0; - /* Now we check for execution in PV context. */ - pv_context = 1; - /* - * Fork a child to test the paravirtualised CPUID instruction. - * If executed outside Xen PV context, the extended opcode will fault. + * Setup a signal handler to test the paravirtualised CPUID instruction. + * If executed outside Xen PV context, the extended opcode will fault + * and we''ll print "Not running on Xen". */ - pid = fork(); - switch ( pid ) - { - case 0: - /* Child: test paravirtualised CPUID opcode and then exit cleanly. */ - cpuid(0x40000000, &dummy, &dummy, &dummy, &dummy); - exit(0); - case -1: - fprintf(stderr, "Fork failed.\n"); - return 0; + signal(SIGILL, sigill_handler); + if (setjmp(j) == 0) { + pv_context = 1; + if ( check_for_xen() ) + return 0; } - /* - * Parent waits for child to terminate and checks for clean exit. - * Only if the exit is clean is it safe for us to try the extended CPUID. - */ - waitpid(pid, &status, 0); - if ( WIFEXITED(status) && check_for_xen() ) - return 0; - printf("Not running on Xen.\n"); return 0; } _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Keir Fraser
2009-Dec-14 11:12 UTC
Re: [Xen-devel] [PATCH] Avoid dumping core in xen-detect
Much nicer, thanks! -- Keir On 14/12/2009 11:08, "Paolo Bonzini" <pbonzini@redhat.com> wrote:> From: pbonzini@redhat.com > > # HG changeset patch > # User Paolo Bonzini <pbonzini@redhat.com> > # Date 1260787292 -3600 > # Node ID 64b613c3664688602fa726f0910ba4bc620cc577 > # Parent d44371e6e5d631f58d9a2ce829f12dafd1272f68 > Avoid dumping core in xen-detect > > F12 introduces a tool to automatically report bugs when there are core > dumps. Since xen-detect relies on fork+waitpid in order to trap a SIGILL > from a child, every time someone runs xen-detect on a bare metal kernel > a bug is reported into Red Hat''s Bugzilla. :-) > > However, even without this contingent need, leaving core dumps around is > not nice. So this patch just traps SIGILL using signal/sentjmp/longjmp, > without the need to fork. > > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> > > diff --git a/tools/misc/xen-detect.c b/tools/misc/xen-detect.c > --- a/tools/misc/xen-detect.c > +++ b/tools/misc/xen-detect.c > @@ -29,9 +29,11 @@ > #include <stdlib.h> > #include <string.h> > #include <sys/types.h> > -#include <sys/wait.h> > +#include <setjmp.h> > +#include <signal.h> > #include <unistd.h> > > +jmp_buf j; > static int pv_context; > > static void cpuid(uint32_t idx, > @@ -74,6 +76,11 @@ > return 1; > } > > +void sigill_handler (int sig) > +{ > + longjmp(j, 1); > +} > + > int main(void) > { > pid_t pid; > @@ -84,33 +91,18 @@ > if ( check_for_xen() ) > return 0; > > - /* Now we check for execution in PV context. */ > - pv_context = 1; > - > /* > - * Fork a child to test the paravirtualised CPUID instruction. > - * If executed outside Xen PV context, the extended opcode will fault. > + * Setup a signal handler to test the paravirtualised CPUID instruction. > + * If executed outside Xen PV context, the extended opcode will fault > + * and we''ll print "Not running on Xen". > */ > - pid = fork(); > - switch ( pid ) > - { > - case 0: > - /* Child: test paravirtualised CPUID opcode and then exit cleanly. */ > - cpuid(0x40000000, &dummy, &dummy, &dummy, &dummy); > - exit(0); > - case -1: > - fprintf(stderr, "Fork failed.\n"); > - return 0; > + signal(SIGILL, sigill_handler); > + if (setjmp(j) == 0) { > + pv_context = 1; > + if ( check_for_xen() ) > + return 0; > } > > - /* > - * Parent waits for child to terminate and checks for clean exit. > - * Only if the exit is clean is it safe for us to try the extended CPUID. > - */ > - waitpid(pid, &status, 0); > - if ( WIFEXITED(status) && check_for_xen() ) > - return 0; > - > printf("Not running on Xen.\n"); > return 0; > } > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xensource.com > http://lists.xensource.com/xen-devel_______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Paolo Bonzini
2009-Dec-14 13:30 UTC
[Xen-devel] Re: [PATCH] Avoid dumping core in xen-detect
On 12/14/2009 12:12 PM, Keir Fraser wrote:> Much nicer, thanks!Thanks, can you put this in the 3.4-testing tree too? Paolo _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Keir Fraser
2009-Dec-14 14:03 UTC
Re: [Xen-devel] Re: [PATCH] Avoid dumping core in xen-detect
Will do. -- Keir On 14/12/2009 13:30, "Paolo Bonzini" <pbonzini@redhat.com> wrote:> On 12/14/2009 12:12 PM, Keir Fraser wrote: >> Much nicer, thanks! > > Thanks, can you put this in the 3.4-testing tree too? > > Paolo > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xensource.com > http://lists.xensource.com/xen-devel_______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel