public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/25715] New: system() returns wrong errors when posix_spawn fails
@ 2020-03-23 13:48 pokogiv215 at sweatmail dot com
  2020-03-23 14:01 ` [Bug libc/25715] " adhemerval.zanella at linaro dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pokogiv215 at sweatmail dot com @ 2020-03-23 13:48 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=25715

            Bug ID: 25715
           Summary: system() returns wrong errors when posix_spawn fails
           Product: glibc
           Version: 2.30
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: pokogiv215 at sweatmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

On linux, I observe a call to system() claiming it's killed by sigbus when
really the underlying execv failed with E2BIG. See reproduction in [1].

What I expected to see is system() returning with exit code 127, as per `man
system`:
       *  If a shell could not be executed in the child process, then the
return value is as though the child shell terminated by calling _exit(2) with
the status 127.

Less important, but what I also expected is for posix_spawn to return 0 in this
case and to fill in the pid, but instead it returns E2BIG and doesn't fill in
the pid, which is not the behavior described by posix_spawn's man page:
       The posix_spawn() and posix_spawnp() functions fail only in the case
where the underlying fork(2) or vfork(2) call fails;  in these cases, these
functions return an error number,  which  will
       be one of the errors described for fork(2) or vfork(2).

The bad return value from system() was introduced in
5fb7fc96350575c9adb1316833e48ca11553be49, which returns posix_spawn's result
(an errno) from system (which returns a wait status).

The posix_spawn handling of errors in the child seems to have changed
unintentionally in 4b4d4056bb154603f36c6f8845757c1012758158 (because ec didn't
use to be set).


[1]

$ cat a.c
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <spawn.h>

int main(int argc, char* argv[], char** env) {
  char cmd[150000];
  memset(cmd, 'a', sizeof(cmd));
  cmd[sizeof(cmd) - 1] = 0;
  int ret = posix_spawn(NULL, "/bin/sh", 0, NULL,
                        (char *[]){"sh", "-c", (char *)cmd, 0}, env);
  printf("posix_spawn returns: %d = %s\n", ret, strerror(ret));
  int wstatus = system(cmd);
  if (WIFEXITED(wstatus)) {
    printf("exited, status=%d\n", WEXITSTATUS(wstatus));
  } else if (WIFSIGNALED(wstatus)) {
    printf("killed by signal %d = %s\n", WTERMSIG(wstatus),
strsignal(WTERMSIG(wstatus)));
  } else if (WIFSTOPPED(wstatus)) {
    printf("stopped by signal %d\n", WSTOPSIG(wstatus));
  } else if (WIFCONTINUED(wstatus)) {
    printf("continued\n");
  }
  return 0;
}
$ gcc a.c && ./a.out
posix_spawn returns: 7 = Argument list too long
killed by signal 7 = Bus error

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-03-24 12:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-23 13:48 [Bug libc/25715] New: system() returns wrong errors when posix_spawn fails pokogiv215 at sweatmail dot com
2020-03-23 14:01 ` [Bug libc/25715] " adhemerval.zanella at linaro dot org
2020-03-23 16:24 ` adhemerval.zanella at linaro dot org
2020-03-23 21:05 ` cvs-commit at gcc dot gnu.org
2020-03-23 21:06 ` adhemerval.zanella at linaro dot org
2020-03-24 11:51 ` cvs-commit at gcc dot gnu.org
2020-03-24 12:16 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).