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 v6 1/3] Add spawn*. Date: Thu, 22 Dec 2022 13:49:08 +0100 Message-ID: <93fe711248cf1ebe86a03bb273ae76e86342ddf8.1671710701.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="29006"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 52835@debbugs.gnu.org, Timothy Sample , Josselin Poiret To: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Dec 22 13:50:24 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 1p8L1j-0007GV-Ab for guile-bugs@m.gmane-mx.org; Thu, 22 Dec 2022 13:50:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p8L1V-0003tC-BX; Thu, 22 Dec 2022 07:50:09 -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 1p8L1O-0003qi-L0 for bug-guile@gnu.org; Thu, 22 Dec 2022 07:50:06 -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 1p8L1O-0003YU-9w for bug-guile@gnu.org; Thu, 22 Dec 2022 07:50:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p8L1N-0001cp-OX for bug-guile@gnu.org; Thu, 22 Dec 2022 07:50:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Josselin Poiret Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 22 Dec 2022 12:50:01 +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.16717133746198 (code B ref 52835); Thu, 22 Dec 2022 12:50:01 +0000 Original-Received: (at 52835) by debbugs.gnu.org; 22 Dec 2022 12:49:34 +0000 Original-Received: from localhost ([127.0.0.1]:58187 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p8L0v-0001bs-L2 for submit@debbugs.gnu.org; Thu, 22 Dec 2022 07:49:34 -0500 Original-Received: from jpoiret.xyz ([206.189.101.64]:41518) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p8L0t-0001ba-HF for 52835@debbugs.gnu.org; Thu, 22 Dec 2022 07:49:32 -0500 Original-Received: from authenticated-user (jpoiret.xyz [206.189.101.64]) by jpoiret.xyz (Postfix) with ESMTPA id 0AA45185305; Thu, 22 Dec 2022 12:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpoiret.xyz; s=dkim; t=1671713369; 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=qIy2JR8YVDC78mvLqPjgXbrJfamy1NNzy3nDbntYMz8=; b=Uw93EYIH+IldyR0qGSKkGg39oua3ls3OixDIMZKEIX0NL7onK6pizBe+mO+3V1IDH5ZSSC LbtTnEPanSnTBrEiYC5UFIB0ybt++twefkMcS0qXdxlxp/kwNI2oSDkJgFPgcukOnsCrcy sUWSz5ZeLfAPfFr7WnI1OBgZMsiaHtbrNCFRvgzTWMNxiaI6cHlcvxeu8JPd3bYDIRa8w8 tBW//Nfe+I7HZVT+jrQxmkguUFIs+j+DgI7wql69rv9c0/zRAnQn+S0bis1SH2XOV/i8Mh GbYD9Z37MB4/hahWu2fq/42C7REjacOGK+aX7slhCnvm1rc17Eu6qJoV+RObzQ== 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-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10481 Archived-At: * libguile/posix.c: Include spawn.h from Gnulib. (do_spawn, scm_spawn_process): New functions. --- libguile/posix.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ libguile/posix.h | 2 ++ 2 files changed, 83 insertions(+) diff --git a/libguile/posix.c b/libguile/posix.c index b5352c2c4..e92625483 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef HAVE_SCHED_H # include @@ -1426,6 +1427,86 @@ start_child (const char *exec_file, char **exec_argv, } #endif +static int +do_spawn (char *exec_file, char **exec_argv, char **exec_env, int in, int out, int err) +{ + int pid = -1; + + posix_spawn_file_actions_t actions; + posix_spawnattr_t *attrp = NULL; + + 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) + return -1; + + return pid; +} + +SCM_DEFINE (scm_spawn_process, "spawn*", 5, 0, 0, + (SCM prog, SCM args, SCM in, SCM out, SCM err), +"Spawns a new child process executing @var{prog} with arguments\n" +"@var{args}, with its standard input, output and error file descriptors\n" +"set to @var{in}, @var{out}, @var{err}.") +#define FUNC_NAME s_scm_spawn_process +{ + int pid; + char *exec_file; + char **exec_argv; + char **exec_env = environ; + + exec_file = scm_to_locale_string (prog); + exec_argv = scm_i_allocate_string_pointers (args); + + pid = do_spawn (exec_file, exec_argv, exec_env, + scm_to_int (in), + scm_to_int (out), + scm_to_int (err)); + + free (exec_file); + + if (pid == -1) + SCM_SYSERROR; + + return scm_from_int (pid); +} +#undef FUNC_NAME + #ifdef HAVE_START_CHILD static SCM scm_piped_process (SCM prog, SCM args, SCM from, SCM to) diff --git a/libguile/posix.h b/libguile/posix.h index 6504eaea8..c2703f9ab 100644 --- a/libguile/posix.h +++ b/libguile/posix.h @@ -69,6 +69,8 @@ SCM_API SCM scm_tmpnam (void); SCM_API SCM scm_tmpfile (void); SCM_API SCM scm_open_pipe (SCM pipestr, SCM modes); SCM_API SCM scm_close_pipe (SCM port); +SCM_API SCM scm_spawn_process (SCM prog, SCM args, + SCM in, SCM out, SCM err); SCM_API SCM scm_system_star (SCM cmds); SCM_API SCM scm_utime (SCM object, SCM actime, SCM modtime, SCM actimens, SCM modtimens, SCM flags); -- 2.38.1