This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 2/2] Implement fchdir action for posix_spawn()
- From: Jonathan Nieder <jrnieder at gmail dot com>
- To: Eric Blake <eblake at redhat dot com>
- Cc: austin-group-futures-l at opengroup dot org, libc-alpha at sourceware dot org
- Date: Fri, 27 Aug 2010 01:38:18 -0500
- Subject: [PATCH 2/2] Implement fchdir action for posix_spawn()
- References: <alpine.LFD.2.00.1008241029530.1046@i5.linux-foundation.org><20100825013625.GC10423@burratino><4C74BFA7.1090907@viscovery.net><4C752739.3010808@redhat.com><20100826061815.GH9708@burratino><4C767682.7030700@redhat.com><20100827063546.GB32454@burratino>
Even operating systems without COW fork tend to provide a way to set
the working directory for a new process without affecting the current
one. Provide a convenient interface for semi-portable code to do
that.
---
NEWS | 4 +++-
posix/Makefile | 2 +-
posix/Versions | 3 +++
posix/spawn.h | 8 ++++++++
...faction_addclose.c => spawn_faction_addchdir.c} | 10 +++++-----
posix/spawn_int.h | 7 ++++++-
sysdeps/posix/spawni.c | 6 ++++++
7 files changed, 32 insertions(+), 8 deletions(-)
copy posix/{spawn_faction_addclose.c => spawn_faction_addchdir.c} (86%)
diff --git a/NEWS b/NEWS
index 6c336c2..396c74a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-GNU C Library NEWS -- history of user-visible changes. 2010-8-25
+GNU C Library NEWS -- history of user-visible changes. 2010-8-27
Copyright (C) 1992-2009, 2010 Free Software Foundation, Inc.
See the end for copying conditions.
@@ -11,6 +11,8 @@ Version 2.13
10851, 11640, 11701, 11840, 11856, 11883, 11903, 11904
+* New interface: posix_spawn_file_actions_addchdir_np
+
* New Linux interfaces: prlimit, prlimit64, fanotify_init, fanotify_mark
* POWER7 optimizations: memset, memcmp, strncmp
diff --git a/posix/Makefile b/posix/Makefile
index 4f29963..4c21704 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -58,7 +58,7 @@ routines := \
getaddrinfo gai_strerror wordexp \
pread pwrite pread64 pwrite64 \
spawn_faction_init spawn_faction_destroy spawn_faction_addclose \
- spawn_faction_addopen spawn_faction_adddup2 \
+ spawn_faction_addopen spawn_faction_adddup2 spawn_faction_addchdir \
spawnattr_init spawnattr_destroy \
spawnattr_getdefault spawnattr_setdefault \
spawnattr_getflags spawnattr_setflags \
diff --git a/posix/Versions b/posix/Versions
index 686c446..5f52bba 100644
--- a/posix/Versions
+++ b/posix/Versions
@@ -134,6 +134,9 @@ libc {
GLIBC_2.11 {
execvpe;
}
+ GLIBC_2.13 {
+ posix_spawn_file_actions_addchdir_np;
+ }
GLIBC_PRIVATE {
__libc_fork; __libc_pwrite;
}
diff --git a/posix/spawn.h b/posix/spawn.h
index 85ac69b..6edebd8 100644
--- a/posix/spawn.h
+++ b/posix/spawn.h
@@ -185,6 +185,14 @@ extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *
__file_actions,
int __fd, int __newfd) __THROW;
+#ifdef __USE_GNU
+/* Add an action to FILE-ACTIONS which tells the implementation to call
+ `fchdir' for the given file descriptors during the `spawn' call. */
+extern int posix_spawn_file_actions_addchdir_np (posix_spawn_file_actions_t *
+ __file_actions, int __fd)
+ __THROW;
+#endif
+
__END_DECLS
#endif /* spawn.h */
diff --git a/posix/spawn_faction_addclose.c b/posix/spawn_faction_addchdir.c
similarity index 86%
copy from posix/spawn_faction_addclose.c
copy to posix/spawn_faction_addchdir.c
index 69ce917..f8736cf 100644
--- a/posix/spawn_faction_addclose.c
+++ b/posix/spawn_faction_addchdir.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000 Free Software Foundation, Inc.
+/* Copyright (C) 2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -23,9 +23,9 @@
#include "spawn_int.h"
/* Add an action to FILE-ACTIONS which tells the implementation to call
- `close' for the given file descriptor during the `spawn' call. */
+ `fchdir' for the given file descriptor during the `spawn' call. */
int
-posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
+posix_spawn_file_actions_addchdir (posix_spawn_file_actions_t *file_actions,
int fd)
{
int maxfd = __sysconf (_SC_OPEN_MAX);
@@ -43,8 +43,8 @@ posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
/* Add the new value. */
rec = &file_actions->__actions[file_actions->__used];
- rec->tag = spawn_do_close;
- rec->action.close_action.fd = fd;
+ rec->tag = spawn_do_chdir;
+ rec->action.chdir_action.fd = fd;
/* Account for the new entry. */
++file_actions->__used;
diff --git a/posix/spawn_int.h b/posix/spawn_int.h
index a3e9347..2dd05a1 100644
--- a/posix/spawn_int.h
+++ b/posix/spawn_int.h
@@ -5,7 +5,8 @@ struct __spawn_action
{
spawn_do_close,
spawn_do_dup2,
- spawn_do_open
+ spawn_do_open,
+ spawn_do_chdir
} tag;
union
@@ -26,6 +27,10 @@ struct __spawn_action
int oflag;
mode_t mode;
} open_action;
+ struct
+ {
+ int fd;
+ } chdir_action;
} action;
};
diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c
index 29803a8..c7993ee 100644
--- a/sysdeps/posix/spawni.c
+++ b/sysdeps/posix/spawni.c
@@ -210,6 +210,12 @@ __spawni (pid_t *pid, const char *file,
/* The `dup2' call failed. */
_exit (SPAWN_ERROR);
break;
+
+ case spawn_do_chdir:
+ if (__fchdir (action->action.chdir_action.fd))
+ /* The `fchdir' call failed. */
+ _exit (SPAWN_ERROR);
+ break;
}
}
}
--
1.7.2.2