Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Either POSIX is confused about WEXITSTATUS or I am #13292

Open
p5pRT opened this issue Sep 20, 2013 · 3 comments
Open

Either POSIX is confused about WEXITSTATUS or I am #13292

p5pRT opened this issue Sep 20, 2013 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Sep 20, 2013

Migrated from rt.perl.org#119899 (status was 'open')

Searchable as RT119899$

@p5pRT
Copy link
Author

p5pRT commented Sep 20, 2013

From @mauke

Created by @mauke

perldoc POSIX​:
...
  Macros WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG

  WIFEXITED WIFEXITED(${^CHILD_ERROR_NATIVE}) returns true if
  the child process exited normally ("exit()" or by
  falling off the end of "main()")

  WEXITSTATUS WEXITSTATUS(${^CHILD_ERROR_NATIVE}) returns the
  normal exit status of the child process (only
  meaningful if WIFEXITED(${^CHILD_ERROR_NATIVE}) is
  true)
...

This tells me that WEXITSTATUS and friends are supposed to be called with
${^CHILD_ERROR_NATIVE}, which is in an unknown / presumably system-specific
format, and not $?, which is always of the form $exit_status << 8 |
$killed_by_signal << 7 | $signal_number.

But looking at the implementation I see this (in POSIX.xs)​:

/* Background​: in most systems the low byte of the wait status
* is the signal (the lowest 7 bits) and the coredump flag is
* the eight bit, and the second lowest byte is the exit status.
* BeOS bucks the trend and has the bytes in different order.
* See beos/beos.c for how the reality is bent even in BeOS
* to follow the traditional. However, to make the POSIX
* wait W*() macros to work in BeOS, we need to unbend the
* reality back in place. --jhi */
/* In actual fact the code below is to blame here. Perl has an internal
* representation of the exit status ($?), which it re-composes from the
* OS's representation using the W*() POSIX macros. The code below
* incorrectly uses the W*() macros on the internal representation,
* which fails for OSs that have a different representation (namely BeOS
* and Haiku). WMUNGE() is a hack that converts the internal
* representation into the OS specific one, so that the W*() macros work
* as expected. The better solution would be not to use the W*() macros
* in the first place, though. -- Ingo Weinhold
*/
#if defined(__HAIKU__)
# define WMUNGE(x) (((x) & 0xFF00) >> 8 | ((x) & 0x00FF) << 8)
#else
# define WMUNGE(x) (x)
#endif

  RETVAL = WEXITSTATUS(WMUNGE(status));

The WMUNGE() part seems to assume an input in $? format, which has to be
"unmangled" in order to be forwarded to the platform-specific W*() macros.

This contradicts the documentation AIUI. Which is correct, the code/comments or
the documentation?

Perl Info

Flags:
    category=library
    severity=low
    module=POSIX

This perlbug was built using Perl 5.12.1 - Thu Jun  3 20:09:15 CEST 2010
It is being executed now by  Perl 5.18.1 - Tue Aug 13 07:08:47 CEST 2013.

Site configuration information for perl 5.18.1:

Configured by mauke at Tue Aug 13 07:08:47 CEST 2013.

Summary of my perl5 (revision 5 version 18 subversion 1) configuration:
   
  Platform:
    osname=linux, osvers=3.5.7-gentoo, archname=i686-linux
    uname='linux nora 3.5.7-gentoo #5 preempt sat jan 26 16:46:10 cet 2013 i686 amd athlon(tm) 64 processor 3200+ authenticamd gnulinux '
    config_args=''
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2 -flto',
    cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.8.1', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-O2 -flto -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib/../lib /usr/lib/../lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.15.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.15'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -flto -L/usr/local/lib -fstack-protector'

Locally applied patches:
    SAVEARGV0 - disable magic open in <ARGV>


@INC for perl 5.18.1:
    /home/mauke/usr/local/lib/perl5/site_perl/5.18.1/i686-linux
    /home/mauke/usr/local/lib/perl5/site_perl/5.18.1
    /home/mauke/usr/local/lib/perl5/5.18.1/i686-linux
    /home/mauke/usr/local/lib/perl5/5.18.1
    .


Environment for perl 5.18.1:
    HOME=/home/mauke
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LC_COLLATE=POSIX
    LD_LIBRARY_PATH=/home/mauke/usr/local/lib
    LOGDIR (unset)
    PATH=/home/mauke/usr/perlbrew/bin:/home/mauke/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i686-pc-linux-gnu/gcc-bin/4.6.3:/opt/sun-jdk-1.4.2.13/bin:/opt/sun-jdk-1.4.2.13/jre/bin:/opt/sun-jdk-1.4.2.13/jre/javaws:/opt/dmd/bin:/usr/games/bin
    PERLBREW_BASHRC_VERSION=0.43
    PERLBREW_HOME=/home/mauke/.perlbrew
    PERLBREW_PATH=/home/mauke/usr/perlbrew/bin
    PERLBREW_ROOT=/home/mauke/usr/perlbrew
    PERLBREW_VERSION=0.27
    PERL_BADLANG (unset)
    PERL_UNICODE=SAL
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Sep 20, 2013

From @leonerd

On Fri, 20 Sep 2013 05​:56​:32 -0700
l.mai@​web.de (via RT) <perlbug-followup@​perl.org> wrote​:

This contradicts the documentation AIUI. Which is correct, the
code/comments or the documentation?

The background to this question is whether it is always appropriate to
test things like

  if(WIFEXITED($?)) { printf "Exit code %d\n", WEXITSTATUS($?) }

--
Paul "LeoNerd" Evans

leonerd@​leonerd.org.uk
ICQ# 4135350 | Registered Linux# 179460
http​://www.leonerd.org.uk/

@p5pRT
Copy link
Author

p5pRT commented Sep 20, 2013

The RT System itself - Status changed from 'new' to 'open'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants