From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id C0F873858D1E; Wed, 19 Apr 2023 08:15:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0F873858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681892150; bh=3efZksgETwUtTLXhjshf1Qqjwo/Ob9lzBiNC2P1asAA=; h=From:To:Subject:Date:From; b=uRaOTv6SerrOcJ72wD8l1QuwcgxUgtut0wq9QzjfU+JXx2cXtfzB2+Tv+ZWCIz9Ow r19+hqr4Of9jwwUENTU8JIE8CRyFYlYgDyx9LUNNWXqqbFs4A1YglQ5gVMPal27blI ylJboaORYaDUyo7pGovYh4qH/Ge8qAZ6+LzpZoEA= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin/main] Add posix_spawn_file_actions_add{f}chdir_np X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: d30a5917a9c40aec18c2243dfc2848c616dca77a X-Git-Newrev: 7e03fc35f528c7776dac0b4dc0b6814ca012986b Message-Id: <20230419081550.C0F873858D1E@sourceware.org> Date: Wed, 19 Apr 2023 08:15:50 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D7e03fc35f52= 8c7776dac0b4dc0b6814ca012986b commit 7e03fc35f528c7776dac0b4dc0b6814ca012986b Author: Corinna Vinschen AuthorDate: Tue Apr 18 23:47:28 2023 +0200 Commit: Corinna Vinschen CommitDate: Tue Apr 18 23:47:28 2023 +0200 Add posix_spawn_file_actions_add{f}chdir_np =20 These are defined as _np functions and available in glibc, musl, macOS, FreeBSD, Solaris =E2=89=A5 11.3 =20 They are likely to be standardized without the _np suffix as a result of Austin Group issue 1208. if so, both names will be kept as aliases. =20 Introduce HAVE_CHDIR and HAVE_FCHDIR to allow building on systems not providing these calls. =20 Signed-off-by: Corinna Vinschen Diff: --- newlib/configure.host | 2 +- newlib/libc/include/spawn.h | 7 ++++ newlib/libc/posix/posix_spawn.c | 89 +++++++++++++++++++++++++++++++++++++= +++- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/newlib/configure.host b/newlib/configure.host index 0a1ba282a3ce..5f427526d051 100644 --- a/newlib/configure.host +++ b/newlib/configure.host @@ -587,7 +587,7 @@ case "${host}" in default_newlib_io_long_double=3D"yes" default_newlib_io_pos_args=3D"yes" CC=3D"${CC} -I${cygwin_srcdir}/include" - newlib_cflags=3D"${newlib_cflags} -DHAVE_OPENDIR -DHAVE_RENAME -DGETREENT= _PROVIDED -DSIGNAL_PROVIDED -DHAVE_BLKSIZE -DHAVE_FCNTL -DMALLOC_PROVIDED" + newlib_cflags=3D"${newlib_cflags} -DHAVE_OPENDIR -DHAVE_RENAME -DGETREENT= _PROVIDED -DSIGNAL_PROVIDED -DHAVE_BLKSIZE -DHAVE_FCNTL -DMALLOC_PROVIDED -= DHAVE_CHDIR -DHAVE_FCHDIR" syscall_dir=3Dsyscalls ;; # RTEMS supplies its own versions of some routines: diff --git a/newlib/libc/include/spawn.h b/newlib/libc/include/spawn.h index d172177b125d..d6ae992dbd8e 100644 --- a/newlib/libc/include/spawn.h +++ b/newlib/libc/include/spawn.h @@ -70,6 +70,13 @@ int posix_spawn_file_actions_addopen (posix_spawn_file_a= ctions_t * __restrict, int, const char * __restrict, int, mode_t); int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *, int, i= nt); int posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *, int); +#if __MISC_VISIBLE || __GNU_VISIBLE +int posix_spawn_file_actions_addchdir_np (posix_spawn_file_actions_t *__re= strict +, + const char *__restrict); +int posix_spawn_file_actions_addfchdir_np (posix_spawn_file_actions_t *__r= estrict, + int); +#endif =20 /* * Spawn attributes diff --git a/newlib/libc/posix/posix_spawn.c b/newlib/libc/posix/posix_spaw= n.c index 85bfa6477efb..3506b0f8bffe 100644 --- a/newlib/libc/posix/posix_spawn.c +++ b/newlib/libc/posix/posix_spawn.c @@ -123,7 +123,13 @@ struct __posix_spawn_file_actions { =20 typedef struct __posix_spawn_file_actions_entry { STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list; - enum { FAE_OPEN, FAE_DUP2, FAE_CLOSE } fae_action; + enum { + FAE_OPEN, + FAE_DUP2, + FAE_CLOSE, + FAE_CHDIR, + FAE_FCHDIR + } fae_action; =20 int fae_fildes; union { @@ -139,6 +145,10 @@ typedef struct __posix_spawn_file_actions_entry { int newfildes; #define fae_newfildes fae_data.dup2.newfildes } dup2; + char *dir; +#define fae_dir fae_data.dir + int dirfd; +#define fae_dirfd fae_data.dirfd } fae_data; } posix_spawn_file_actions_entry_t; =20 @@ -235,7 +245,21 @@ process_file_actions_entry(posix_spawn_file_actions_en= try_t *fae) /* Perform a close(), do not fail if already closed */ (void)_close(fae->fae_fildes); break; +#ifdef HAVE_CHDIR + case FAE_CHDIR: + /* Perform a chdir. */ + if (chdir (fae->fae_dir) =3D=3D -1) + return (errno); + break; +#endif +#ifdef HAVE_FCHDIR + case FAE_FCHDIR: + /* Perform a chdir. */ + if (fchdir (fae->fae_dirfd) =3D=3D -1) + return (errno); + break; } +#endif return (0); } =20 @@ -407,8 +431,18 @@ posix_spawn_file_actions_destroy (posix_spawn_file_act= ions_t *fa) STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list); =20 /* Deallocate file action entry */ - if (fae->fae_action =3D=3D FAE_OPEN) + switch (fae->fae_action) { + case FAE_OPEN: free(fae->fae_path); + break; +#ifdef HAVE_CHDIR + case FAE_CHDIR: + free(fae->fae_dir); + break; +#endif + default: + break; + } free(fae); } =20 @@ -496,6 +530,57 @@ posix_spawn_file_actions_addclose (posix_spawn_file_ac= tions_t *fa, return (0); } =20 +#ifdef HAVE_CHDIR +int +posix_spawn_file_actions_addchdir_np ( + posix_spawn_file_actions_t * __restrict fa, + const char * __restrict path) +{ + posix_spawn_file_actions_entry_t *fae; + int error; + + /* Allocate object */ + fae =3D malloc(sizeof(posix_spawn_file_actions_entry_t)); + if (fae =3D=3D NULL) + return (errno); + + /* Set values and store in queue */ + fae->fae_action =3D FAE_CHDIR; + fae->fae_dir =3D strdup(path); + if (fae->fae_dir =3D=3D NULL) { + error =3D errno; + free(fae); + return (error); + } + + STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list); + return (0); +} +#endif + +#ifdef HAVE_FCHDIR +int +posix_spawn_file_actions_addfchdir_np ( + posix_spawn_file_actions_t * __restrict fa, + int fd) +{ + posix_spawn_file_actions_entry_t *fae; + int error; + + /* Allocate object */ + fae =3D malloc(sizeof(posix_spawn_file_actions_entry_t)); + if (fae =3D=3D NULL) + return (errno); + + /* Set values and store in queue */ + fae->fae_action =3D FAE_FCHDIR; + fae->fae_dirfd =3D fd; + + STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list); + return (0); +} +#endif + /* * Spawn attributes */