From e83be730910c341f2f02ccc207b0586bb04fc21a Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Fri, 16 Sep 2016 17:44:50 -0300 Subject: posix: Fix open file action for posix_spawn on Linux On posix_spawn open file action (issued by posix_spawn_file_actions_addopen) POSIX states that if fildes was already an open file descriptor, it shall be closed before the new file is openedi [1]. This avoid pontential issues when posix_spawn plus addopen action is called with the process already at maximum number of file descriptor opened and also for multiple actions on single-open special paths (like /dev/watchdog). This fixes its behavior on Linux posix_spawn implementation and also adds a tests to check for its behavior. Checked on x86_64. * posix/Makefile (tests): Add tst-spawn3. * posix/tst-spawn3.c: New file. * sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Close file descriptor if it is already opened for open action. [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html --- sysdeps/unix/sysv/linux/spawni.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'sysdeps/unix') diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index 679534b79d..fda8593287 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -219,6 +219,14 @@ __spawni_child (void *arguments) case spawn_do_open: { + /* POSIX states that if fildes was already an open file descriptor, + it shall be closed before the new file is opened. This avoid + pontential issues when posix_spawn plus addopen action is called + with the process already at maximum number of file descriptor + opened and also for multiple actions on single-open special + paths (like /dev/watchdog). */ + close_not_cancel (action->action.open_action.fd); + ret = open_not_cancel (action->action.open_action.path, action->action. open_action.oflag | O_LARGEFILE, -- cgit v1.2.3-70-g09d2