From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Omar Polo Newsgroups: gmane.lisp.guile.bugs Subject: bug#61095: possible misuse of posix_spawn API on non-linux OSes Date: Fri, 27 Jan 2023 12:51:32 +0100 Message-ID: <26OIN3L5D4V9L.2M0KM95K0YSNM@venera> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38792"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mblaze/1.2 To: 61095@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Fri Jan 27 12:53:26 2023 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pLNIM-0009u2-0H for guile-bugs@m.gmane-mx.org; Fri, 27 Jan 2023 12:53:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pLNHz-0001gY-Ri; Fri, 27 Jan 2023 06:53:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pLNHy-0001gP-UV for bug-guile@gnu.org; Fri, 27 Jan 2023 06:53:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pLNHy-0007m1-MD for bug-guile@gnu.org; Fri, 27 Jan 2023 06:53:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pLNHy-0004Ie-64 for bug-guile@gnu.org; Fri, 27 Jan 2023 06:53:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Omar Polo Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Fri, 27 Jan 2023 11:53:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61095 X-GNU-PR-Package: guile X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.167482032816460 (code B ref -1); Fri, 27 Jan 2023 11:53:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 27 Jan 2023 11:52:08 +0000 Original-Received: from localhost ([127.0.0.1]:36849 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pLNH3-0004HO-OS for submit@debbugs.gnu.org; Fri, 27 Jan 2023 06:52:08 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:58508) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pLNGx-0004Gs-9w for submit@debbugs.gnu.org; Fri, 27 Jan 2023 06:52:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pLNGw-0001CZ-I2 for bug-guile@gnu.org; Fri, 27 Jan 2023 06:51:58 -0500 Original-Received: from mail.omarpolo.com ([144.91.116.244]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pLNGi-0007dJ-Pw for bug-guile@gnu.org; Fri, 27 Jan 2023 06:51:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=omarpolo.com; s=20200327; t=1674820296; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc; bh=lXkxayJwYt7c0Gm/qLXJfijDmttThi7UpTRKia9CI1o=; b=3oyU0X88txNXZRKG4ofnVITuKo+SqfnHhV5FItjyHmaIpcgdAiUvcUO/DYYHAt7bTb8OgL G6C/1KlFMwgpdRn4XU3QpDL3aOrWUBbNfe+QH/KgUnUvharx/qUgeeQJ1oXnUlp+JK7c+N i2YxIs+dOUba9NekEzO0aWdE06/Dl0Y= Original-Received: from localhost (host-82-61-20-176.retail.telecomitalia.it [82.61.20.176]) by mail.omarpolo.com (OpenSMTPD) with ESMTPSA id 984b554e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 27 Jan 2023 12:51:35 +0100 (CET) Original-Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 9eed019b for ; Fri, 27 Jan 2023 12:51:32 +0100 (CET) Received-SPF: pass client-ip=144.91.116.244; envelope-from=op@omarpolo.com; helo=mail.omarpolo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10542 Archived-At: Hello, I've noticed that test-system-cmds fails on OpenBSD-CURRENT while testing the update to guile 3.0.9: test-system-cmds: system* exit status was 127 rather than 42 FAIL: test-system-cmds Here's an excerpt of the ktrace of the child process while executing that specific test: (the first fork() is the one implicitly done by posix_spawn(3)) 5590 guile RET fork 0 [...] 5590 guile CALL dup2(0,3) 5590 guile RET dup2 3 5590 guile CALL dup2(1,4) 5590 guile RET dup2 4 5590 guile CALL dup2(2,5) 5590 guile RET dup2 5 5590 guile CALL dup2(3,0) 5590 guile RET dup2 0 5590 guile CALL dup2(4,1) 5590 guile RET dup2 1 5590 guile CALL dup2(5,2) 5590 guile RET dup2 2 5590 guile CALL close(1023) 5590 guile RET close -1 errno 9 Bad file descriptor 5590 guile CALL kbind(0x7f7ffffd51f8,24,0x2b5c5ced59893fa9) 5590 guile RET kbind 0 5590 guile CALL exit(127) (if you prefer I can provide a full ktrace of guile executing that test case) My interpretation is that the sequence of dup2(2) is from posix_spawn_file_actions_adddup2 in do_spawn, while the strange close(1023) is from close_inherited_fds_slow. Such file descriptor is not open, so close(2) fails with EBADF and the posix_spawn machinery exits prematurely. My current RLIMIT_NOFILE is 1024, so the number would make sense. On OpenBSD I've tried to use the following patch to work around the issue: [[[ Index: libguile/posix.c --- libguile/posix.c.orig +++ libguile/posix.c @@ -1325,6 +1325,7 @@ SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, static void close_inherited_fds_slow (posix_spawn_file_actions_t *actions, int max_fd) { + max_fd = getdtablecount(); while (--max_fd > 2) posix_spawn_file_actions_addclose (actions, max_fd); } ]]] getdtablecount(2) returns the number of file descriptor currently open by the process. unfortunately it doesn't seem to be portable. (well, tbf /proc/self/fd is not portable too.) However, while this pleases the system* test, it breaks the pipe tests: Running popen.test FAIL: popen.test: open-input-pipe: echo hello FAIL: popen.test: pipeline - arguments: (expected-value ("HELLO WORLD\n" (0 0)) actual-value ("" (127 0))) the reason seem to be similar: 74865 guile CALL dup2(7,3) 74865 guile RET dup2 3 74865 guile CALL dup2(10,4) 74865 guile RET dup2 4 74865 guile CALL dup2(2,5) 74865 guile RET dup2 5 74865 guile CALL dup2(3,0) 74865 guile RET dup2 0 74865 guile CALL dup2(4,1) 74865 guile RET dup2 1 74865 guile CALL dup2(5,2) 74865 guile RET dup2 2 74865 guile CALL close(8) 74865 guile RET close -1 errno 9 Bad file descriptor 74865 guile CALL kbind(0x7f7ffffcfa88,24,0x2125923bdf2ca9e) 74865 guile RET kbind 0 74865 guile CALL exit(127) I guess it's trying to close the fd of the pipe that was closed. I'm not sure what to do from here, I'm not used to the posix_spawn_* APIs. I'm happy to help testing diffs or by providing more info if needed. Thanks, Omar Polo