mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rasmus Andersson <rasmus@rsms.me>
To: musl@lists.openwall.com
Subject: [musl] waitpid (wait4) on Linux 5 returns invalid values
Date: Tue, 19 Jan 2021 10:18:04 -0800	[thread overview]
Message-ID: <CADFzTtdU2h6pG3B+bdvUjFfDy=GxJ-poC18QpjaRinJchayNAA@mail.gmail.com> (raw)

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)

             reply	other threads:[~2021-01-19 18:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-19 18:18 Rasmus Andersson [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADFzTtdU2h6pG3B+bdvUjFfDy=GxJ-poC18QpjaRinJchayNAA@mail.gmail.com' \
    --to=rasmus@rsms.me \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).