From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Josselin Poiret via "Bug reports for GUILE, GNU's Ubiquitous Extension Language" Newsgroups: gmane.lisp.guile.bugs Subject: bug#52835: [PATCH v5 2/3] Add spawn*. Date: Mon, 5 Sep 2022 08:48:14 +0200 Message-ID: <95dbb2e804221fc369203d38c75238a190d3ff6f.1662358976.git.dev@jpoiret.xyz> References: Reply-To: Josselin Poiret Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26714"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Timothy Sample , Josselin Poiret To: 52835@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon Sep 05 08:49:15 2022 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 1oV5v1-0006kT-7o for guile-bugs@m.gmane-mx.org; Mon, 05 Sep 2022 08:49:15 +0200 Original-Received: from localhost ([::1]:54000 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oV5v0-0001yr-4L for guile-bugs@m.gmane-mx.org; Mon, 05 Sep 2022 02:49:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39612) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oV5uq-0001yM-6l for bug-guile@gnu.org; Mon, 05 Sep 2022 02:49:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57555) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oV5up-0006Sc-P3 for bug-guile@gnu.org; Mon, 05 Sep 2022 02:49:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oV5up-0000cu-Hs for bug-guile@gnu.org; Mon, 05 Sep 2022 02:49:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Mon, 05 Sep 2022 06:49:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 52835 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch Original-Received: via spool by 52835-submit@debbugs.gnu.org id=B52835.16623605092347 (code B ref 52835); Mon, 05 Sep 2022 06:49:03 +0000 Original-Received: (at 52835) by debbugs.gnu.org; 5 Sep 2022 06:48:29 +0000 Original-Received: from localhost ([127.0.0.1]:46250 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oV5uH-0000bd-52 for submit@debbugs.gnu.org; Mon, 05 Sep 2022 02:48:29 -0400 Original-Received: from jpoiret.xyz ([206.189.101.64]:38528) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oV5uF-0000bH-CR for 52835@debbugs.gnu.org; Mon, 05 Sep 2022 02:48:27 -0400 Original-Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 89DF118530C; Mon, 5 Sep 2022 06:48:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1662360506; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=puvWFbocnfy6+9e0je87eDLt4ASL6ZhTFPo5Sa0wDg4=; b=CeGmxm6zf1RwYGrp2zgUeng0YqPYmEt1nkU6RAFqPRpQqnEhzVWoM7ViwtbqlyHpuAe7A8 IT7ngRC61JJXKVKkhRfOVvpYrOQYJfS7Dvqe2hx+aBE+P54PjfhWpY7TEIRW+Ci4RfLrmO PwOxnK85VkTtfpImOw9V5INiarvfFfaou4tc9CYSSLQZRuYg2LjkKLkbZ8K2icFPrXkYe/ frZUDHexgfCIsUCs7Tl1BXkRVE9DSKmxjstELD2wxt2CTPDIq4APKtWOj5XlwI2RfLGSGN Qp9lz1D9CwNAz0OkdWYKpBzO+r2CF9CXqCBHCF3oRhFdLkv/H/OltnV2SKds6A== In-Reply-To: Authentication-Results: jpoiret.xyz; auth=pass smtp.auth=jpoiret@jpoiret.xyz smtp.mailfrom=dev@jpoiret.xyz X-Spamd-Bar: / 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" Xref: news.gmane.io gmane.lisp.guile.bugs:10362 Archived-At: * libguile/posix.c: Include spawn.h from Gnulib. (scm_spawn_process): New function. (scm_init_popen): Define spawn*. --- libguile/posix.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/libguile/posix.c b/libguile/posix.c index f4ca72d3e..5d287ff2a 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -32,6 +32,7 @@ #include #include #include +#include #ifdef HAVE_SCHED_H # include @@ -1472,6 +1473,75 @@ scm_piped_process (SCM prog, SCM args, SCM from, SCM to) } #undef FUNC_NAME +static SCM +scm_spawn_process (SCM prog, SCM args, SCM scm_in, SCM scm_out, SCM scm_err) +#define FUNC_NAME "spawn*" +{ + int in, out, err; + int pid; + char *exec_file; + char **exec_argv; + char **exec_env = NULL; + + posix_spawn_file_actions_t actions; + posix_spawnattr_t *attrp = NULL; + + exec_file = scm_to_locale_string (prog); + exec_argv = scm_i_allocate_string_pointers (args); + + in = scm_to_int (scm_in); + out = scm_to_int (scm_out); + err = scm_to_int (scm_err); + + int max_fd = 1024; + +#if defined (HAVE_GETRLIMIT) && defined (RLIMIT_NOFILE) + { + struct rlimit lim = { 0, 0 }; + if (getrlimit (RLIMIT_NOFILE, &lim) == 0) + max_fd = lim.rlim_cur; + } +#endif + + posix_spawn_file_actions_init (&actions); + + int free_fd_slots = 0; + int fd_slot[3]; + + for (int fdnum = 3;free_fd_slots < 3 && fdnum < max_fd;fdnum++) + { + if (fdnum != in && fdnum != out && fdnum != err) + { + fd_slot[free_fd_slots] = fdnum; + free_fd_slots++; + } + } + + /* Move the fds out of the way, so that duplicate fds or fds equal + to 0, 1, 2 don't trample each other */ + + posix_spawn_file_actions_adddup2 (&actions, in, fd_slot[0]); + posix_spawn_file_actions_adddup2 (&actions, out, fd_slot[1]); + posix_spawn_file_actions_adddup2 (&actions, err, fd_slot[2]); + posix_spawn_file_actions_adddup2 (&actions, fd_slot[0], 0); + posix_spawn_file_actions_adddup2 (&actions, fd_slot[1], 1); + posix_spawn_file_actions_adddup2 (&actions, fd_slot[2], 2); + + while (--max_fd > 2) + posix_spawn_file_actions_addclose (&actions, max_fd); + + if (posix_spawnp (&pid, exec_file, &actions, attrp, exec_argv, environ) != 0) + { + int errno_save = errno; + free (exec_file); + errno = errno_save; + SCM_SYSERROR; + } + + return scm_from_int (pid); +} +#undef FUNC_NAME + static void restore_sigaction (SCM pair) { @@ -2381,6 +2451,7 @@ static void scm_init_popen (void) { scm_c_define_gsubr ("piped-process", 2, 2, 0, scm_piped_process); + scm_c_define_gsubr ("spawn*", 5, 0, 0, scm_spawn_process); } #endif /* HAVE_START_CHILD */ -- 2.37.2