public inbox for newlib-cvs@sourceware.org help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org> To: newlib-cvs@sourceware.org Subject: [newlib-cygwin/main] Add posix_spawn_file_actions_add{f}chdir_np Date: Wed, 19 Apr 2023 08:15:50 +0000 (GMT) [thread overview] Message-ID: <20230419081550.C0F873858D1E@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=7e03fc35f528c7776dac0b4dc0b6814ca012986b commit 7e03fc35f528c7776dac0b4dc0b6814ca012986b Author: Corinna Vinschen <corinna@vinschen.de> AuthorDate: Tue Apr 18 23:47:28 2023 +0200 Commit: Corinna Vinschen <corinna@vinschen.de> CommitDate: Tue Apr 18 23:47:28 2023 +0200 Add posix_spawn_file_actions_add{f}chdir_np These are defined as _np functions and available in glibc, musl, macOS, FreeBSD, Solaris ≥ 11.3 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. Introduce HAVE_CHDIR and HAVE_FCHDIR to allow building on systems not providing these calls. Signed-off-by: Corinna Vinschen <corinna@vinschen.de> 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="yes" default_newlib_io_pos_args="yes" CC="${CC} -I${cygwin_srcdir}/include" - newlib_cflags="${newlib_cflags} -DHAVE_OPENDIR -DHAVE_RENAME -DGETREENT_PROVIDED -DSIGNAL_PROVIDED -DHAVE_BLKSIZE -DHAVE_FCNTL -DMALLOC_PROVIDED" + newlib_cflags="${newlib_cflags} -DHAVE_OPENDIR -DHAVE_RENAME -DGETREENT_PROVIDED -DSIGNAL_PROVIDED -DHAVE_BLKSIZE -DHAVE_FCNTL -DMALLOC_PROVIDED -DHAVE_CHDIR -DHAVE_FCHDIR" syscall_dir=syscalls ;; # 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_actions_t * __restrict, int, const char * __restrict, int, mode_t); int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *, int, int); 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 *__restrict +, + const char *__restrict); +int posix_spawn_file_actions_addfchdir_np (posix_spawn_file_actions_t *__restrict, + int); +#endif /* * Spawn attributes diff --git a/newlib/libc/posix/posix_spawn.c b/newlib/libc/posix/posix_spawn.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 { 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; 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; @@ -235,7 +245,21 @@ process_file_actions_entry(posix_spawn_file_actions_entry_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) == -1) + return (errno); + break; +#endif +#ifdef HAVE_FCHDIR + case FAE_FCHDIR: + /* Perform a chdir. */ + if (fchdir (fae->fae_dirfd) == -1) + return (errno); + break; } +#endif return (0); } @@ -407,8 +431,18 @@ posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *fa) STAILQ_REMOVE_HEAD(&(*fa)->fa_list, fae_list); /* Deallocate file action entry */ - if (fae->fae_action == 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); } @@ -496,6 +530,57 @@ posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *fa, return (0); } +#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 = malloc(sizeof(posix_spawn_file_actions_entry_t)); + if (fae == NULL) + return (errno); + + /* Set values and store in queue */ + fae->fae_action = FAE_CHDIR; + fae->fae_dir = strdup(path); + if (fae->fae_dir == NULL) { + error = 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 = malloc(sizeof(posix_spawn_file_actions_entry_t)); + if (fae == NULL) + return (errno); + + /* Set values and store in queue */ + fae->fae_action = FAE_FCHDIR; + fae->fae_dirfd = fd; + + STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list); + return (0); +} +#endif + /* * Spawn attributes */
reply other threads:[~2023-04-19 8:15 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230419081550.C0F873858D1E@sourceware.org \ --to=corinna@sourceware.org \ --cc=newlib-cvs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).