From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.17.24]) by sourceware.org (Postfix) with ESMTPS id 67940382E071 for ; Thu, 30 Jul 2020 17:17:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 67940382E071 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cygwin.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=corinna-cygwin@cygwin.com Received: from calimero.vinschen.de ([217.91.18.234]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.183]) with ESMTPSA (Nemesis) id 1M5Q6l-1k21f92Iuz-001QEU; Thu, 30 Jul 2020 19:17:34 +0200 Received: by calimero.vinschen.de (Postfix, from userid 500) id 08309A80D72; Thu, 30 Jul 2020 19:17:23 +0200 (CEST) Date: Thu, 30 Jul 2020 19:17:23 +0200 From: Corinna Vinschen To: Ken Brown Cc: cygwin@cygwin.com Subject: Re: Synchronization problem with posix_spawn Message-ID: <20200730171723.GA460314@calimero.vinschen.de> Reply-To: cygwin@cygwin.com Mail-Followup-To: Ken Brown , cygwin@cygwin.com References: <864b3031-9fc8-beb3-ba7c-1ade4c31a288@cornell.edu> <20200730115913.GL4206@calimero.vinschen.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200730115913.GL4206@calimero.vinschen.de> X-Provags-ID: V03:K1:NWag13+1iYa9oN9J10Z+4nm62nRaN4XLaxSkCZXfbp6EbEHrSID 3P02HBacBf3JU6TNdP5f3D4LzRqFiCp7yFXPm5sAV7SJ6XSFTOAiPriq/gkmTPTL1Io8Hy2 zy16/O7TMg4N/Q23pBzIgLTYwQS/LWndbF8acRL8cPubUg4VGPhhnPqo35z6B3gVO/OWlXQ crp4Pf0IaeiXEavaPl4/g== X-UI-Out-Filterresults: notjunk:1;V03:K0:DKDh+X6NBN8=:pYgSBjv9L1oGx7PDK3/qS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5xbWaT5ssOOZu1Cro554zpkDr1JygqS +1rsAk3IG15g5dShXB/7WT6ur9RaQp6R5oywMEW0uQNcT/wrN+AqOpjSduphac9uBWuPU6C16 kdiQdAp9qHn+o678rcmIOkCG+Q7Q0MWdY/NCF/k+DTqokeWV4coappIJ0qjAHMoa7dv/o= X-Spam-Status: No, score=-104.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, GOOD_FROM_CORINNA_CYGWIN, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: cygwin@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jul 2020 17:17:41 -0000 Hi Ken, On Jul 30 13:59, Corinna Vinschen wrote: > On Jul 29 19:12, Ken Brown via Cygwin wrote: > > On 7/29/2020 4:17 PM, Ken Brown via Cygwin wrote: > > > posix_spawn(p) returns before the spawned process is fully up and > > > running.  [...] > > I just took a look at the source, and I see that posix_spawn was taken from > > FreeBSD. Does FreeBSD have the same problem? Should applications just be > > aware of this issue and insert a sleep after posix_spawn before sending > > signals? > > Actually, this is a Cygwin problem. I just had a look into the > newlib implementation myself, and it turns out that the code, > in particular the do_posix_spawn() function, is BSD specific. It > relies on the BSD implementation of vfork(2). Cygwin's vfork(2) > on the other hand, is NOT following the historic idea of the > BSD vfork(2), rather it's equivalent to fork(2). This is > POSIX compliant, but certainly the reliance of the BSD vfork > behaviour makes do_posix_spawn() as it's implemented right now, > not overly functional for Cygwin. > > IOW, we need a Cygwin-specific do_posix_spawn() using fork(2) > in conjunction with some synchronization the BSD function > gets "for free" by using its specific vfork(2). Below is a POC implementation for a Cygwin-specific do_posix_spawn(). If this does the trick (at least your testcase works in my testing), then I'm planning to move the function over to the winsup/cygwin dir so it can be streamlined further. Can you give it a try? Thanks, Corinna diff --git a/newlib/libc/posix/posix_spawn.c b/newlib/libc/posix/posix_spawn.c index 19c5cd0fe986..3adbac29d7fd 100644 --- a/newlib/libc/posix/posix_spawn.c +++ b/newlib/libc/posix/posix_spawn.c @@ -254,6 +254,82 @@ process_file_actions(const posix_spawn_file_actions_t fa) return (0); } +#ifdef __CYGWIN__ +#include +#include +#include + +typedef struct +{ + pid_t pid; + int error; +} child_info; + +static int +do_posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *fa, + const posix_spawnattr_t *sa, + char * const argv[], char * const envp[], int use_env_path) +{ + pid_t p; + int ret = 0; + child_info *ci; + + ci = (child_info *) mmap(NULL, sizeof(child_info), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (ci == MAP_FAILED) + return (errno); + + /* Cygwin's vfork does not follow BSD vfork semantics. Rather it's + equivalent to fork. While that's POSIX compliant, it means the + below FreeBSD implementation relying on BSD vfork semantics + doesn't work as expected on Cygwin. */ + p = fork(); + switch (p) { + case -1: + ret = errno; + break; + case 0: + if (sa != NULL) { + ci->error = process_spawnattr(*sa); + if (ci->error) + _exit(127); + } + if (fa != NULL) { + ci->error = process_file_actions(*fa); + if (ci->error) + _exit(127); + } + if (use_env_path) + p = spawnvpe(_P_NOWAITO, path, + (const char * const *) argv, + (const char * const *) + (envp != NULL ? envp : *p_environ)); + else + p = spawnve(_P_NOWAITO, path, + (const char * const *) argv, + (const char * const *) + (envp != NULL ? envp : *p_environ)); + if (p < 0) { + ci->error = errno; + _exit(127); + } + ci->pid = p; + _exit (0); + default: + if (waitpid(p, NULL, 0) < 0) + ret = errno; + else if (ci->error) + ret = ci->error; + else if (pid != NULL) + *pid = ci->pid; + break; + } + munmap(ci, sizeof(child_info)); + return ret; +} +#else static int do_posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa, @@ -292,6 +368,7 @@ do_posix_spawn(pid_t *pid, const char *path, return (error); } } +#endif int posix_spawn (pid_t *pid, -- Corinna Vinschen Cygwin Maintainer