From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 25857 invoked from network); 19 Jan 2021 18:18:41 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 19 Jan 2021 18:18:41 -0000 Received: (qmail 7198 invoked by uid 550); 19 Jan 2021 18:18:27 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 6133 invoked from network); 19 Jan 2021 18:18:27 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rsms-me.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=ND9hmVSW7MO4nHlw9YJGGcWjMT/5bH12Mk+8e3kiIwU=; b=LH6dchOWugQlAQbwJ80flc3LcGMtsWOiSB4GADANT9APHGR7bpPXKGzegsEZLHZccw MsgRNHVvg/Ye3A9Uk60Y66D4v+R0oWe2HqsoNVPCwbNF6sdqnBXQ+dRiCx3TwL81BcEM 8ljTwBgQT556RKjPPqkj0iZrew8ru0LUWVSex3Vc2UVQnHxvAP994S9TUYbYvoUFVw4O ruyp6RZoqO8kmWSy6j7GYrDzvS25jgQ+Se13YnwbJazaIZ5RG4WDuMmy2YhJSjtsk0uZ +uPZ8gIbV49sbcBai45E2vaY2LDsIMRlUScY/7qvdf3gVyiFiC+p3QuU4QTijq0kq5YH mkEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ND9hmVSW7MO4nHlw9YJGGcWjMT/5bH12Mk+8e3kiIwU=; b=KkhEvNFsUfESXNguKUKPVgFV53N9NCndf9jjxlPesxNSdViHDvS8CzXOIcEVqIECWe FaihJY933ikkJ9NMNfndlFaf2aQFJ5e5hIi5liNN+MBjIT4HHP5W8ENAK8TEL+p3VFhT p9L1DeQqa1gCt9EX98cQTjckIvIEjotG93SRDSZf0XZsw1qDmhEpGg3eCHkw97PAF0GO lfH8HM1OSRq1B17bXKFRUm1eDXdl72ZO3K9ntFv1HeFj3wmftiEGDcSXDco7qVkaO4QL 1NuZK2KIJk+AQYhS/CDezaMU1lz/AW9DDVp1XPLiwkuV/Iqlrkm91LWIqyCXPMRz/PF3 KpuA== X-Gm-Message-State: AOAM532q0MsXoi/YEg6uPmVQl04Z36mqK0fZIvlhth8szWbyEfnQyipb zDa8CEHM7pbBlzx4iNPv20vq0bccKx4Q8RmBjf0iwBprnBUH9g== X-Google-Smtp-Source: ABdhPJwcjpvdfRTvWiS4PdNpqvUVtiZzhYMtjMTZiTHKefrR/wvQMTq5FgB/ksrqTnIE3T52tz51tsBs22Ix29P+BH0= X-Received: by 2002:a17:90b:358d:: with SMTP id mm13mr1054490pjb.146.1611080294716; Tue, 19 Jan 2021 10:18:14 -0800 (PST) MIME-Version: 1.0 From: Rasmus Andersson Date: Tue, 19 Jan 2021 10:18:04 -0800 Message-ID: To: musl@lists.openwall.com Content-Type: text/plain; charset="UTF-8" Subject: [musl] waitpid (wait4) on Linux 5 returns invalid values 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)