mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] waitpid (wait4) on Linux 5 returns invalid values
@ 2021-01-19 18:18 Rasmus Andersson
  2021-01-19 19:33 ` Markus Wichmann
  0 siblings, 1 reply; 11+ messages in thread
From: Rasmus Andersson @ 2021-01-19 18:18 UTC (permalink / raw)
  To: musl

Hello!
I'm having an issue with musl (at git master.) It appears as the
waitpid[1] implementation assumes that the syscall returns values
matching the waitpid specification, but it does not! This causes some
programs to hang.

Runit's "runsv" program is one example. It does something like this:

for (;;) {
  child = waitpid(-1, &wstat, WNOHANG);
  if (!child) break;
  if ((child == -1) && (errno != EINTR)) break;
  if (child == svd[0].pid) {
    // do things with child
  }
}

When I inspect a hung runsv process with strace I find it calling
wait4 at full speed, stuck in this loop. wait4 comes from calling the
waitpid function which in musl performs the wait4 syscall and returns
the value.

The waitpid spec[2] says its return value is either the PID, -1 with
errno set to EINTR or 0 in WNOHANG mode. So, the expected returns
values are: >0, 0, -1.

However the wait4 syscall[3] in Linux 5 returns other values,
specifically it returns errors as negative values. The error that
trips up programs like runit's runsv is ECHILD (-10) which wait4
returns when there are no children (i.e. they have exited.)

I propose that you change the waitpid implementation to handle this.
Something like this:

pid_t waitpid(pid_t pid, int *status, int options)
{
  pid_t r = syscall_cp(SYS_wait4, pid, status, options, 0);
  if (r < 0) {
    errno = -r;
    r = -1;
  }
  return r;
}

-- Rasmus

[1] waitpid in musl:
http://git.musl-libc.org/cgit/musl/tree/src/process/waitpid.c
[2] waitpid spec:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
[2] waitpid in glibc: https://man7.org/linux/man-pages/man2/waitpid.2.html
[3] wait4 syscall implementation in Linux 5.10.1: kernel/exit.c:1638
and kernel/exit.c:1579 (online:
https://elixir.bootlin.com/linux/v5.10.1/source/kernel/exit.c#L1579)

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

end of thread, other threads:[~2021-01-19 23:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-19 18:18 [musl] waitpid (wait4) on Linux 5 returns invalid values Rasmus Andersson
2021-01-19 19:33 ` Markus Wichmann
2021-01-19 20:17   ` Rasmus Andersson
2021-01-19 20:28     ` Rasmus Andersson
2021-01-19 20:35       ` Rasmus Andersson
2021-01-19 20:56         ` Rasmus Andersson
2021-01-19 21:16         ` Rich Felker
2021-01-19 22:00           ` Rasmus Andersson
2021-01-19 22:02             ` Rasmus Andersson
2021-01-19 23:01               ` Zach van Rijn
2021-01-19 23:07                 ` Rasmus Andersson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).