(pipe2): Export. * dtable.cc (dtable::dup_worker): Take additional flags parameter. Handle O_CLOEXEC flag. (dtable::dup3): Rename from dup2. Take additional flags parameter. Check for valid flags. Drop check for newfd == oldfd. * dtable.h (dtable::dup_worker): Add flags parameter. (dtable::dup3): Rename from dup2. * fcntl.cc (fcntl64): Add F_DUPFD_CLOEXEC case. * fhandler.h (fhandler_mailslot::get_object_attr): Add flags parameter. * fhandler.cc (fhandler_base::open): Use security attribute with inheritance according to setting of O_CLOEXEC flag. * fhandler_console.cc (fhandler_console::open): Ditto. * fhandler_fifo.cc (sec_user_cloexec): New inline function to create security attribute with inheritance according to setting of O_CLOEXEC flag. (fhandler_fifo::open): Call sec_user_cloexec to fetch security attribute. (fhandler_fifo::wait): Ditto. * fhandler_mem.cc (fhandler_dev_mem::open): Ditto. * fhandler_mailslot.cc (fhandler_mailslot::get_object_attr): Take additional flags parameter. Use security attribute with inheritance according to setting of O_CLOEXEC flag. (fhandler_mailslot::open): Call get_object_attr with flags parameter. * fhandler_registry.cc (fhandler_registry::open): Call set_close_on_exec on real handles to accommodate O_CLOEXEC flag. * fhandler_tty.cc (fhandler_tty_slave::open): Ditto. * fhandler_tape.cc: Create mutex with inheritance according to setting of O_CLOEXEC flag. * pipe.cc: Replace usage of O_NOINHERIT with O_CLOEXEC. (fhandler_pipe::init): Simplify setting close_on_exec flag. (fhandler_pipe::open): Remove setting close_on_exec flag. (fhandler_pipe::create): Use security attribute with inheritance according to setting of O_CLOEXEC flag. (pipe2): New exported function. * posix_ipc.cc: Throughout, open backing files with O_CLOEXEC flag to follow POSIX semantics. * security.h (sec_none_cloexec): New define. * syscalls.cc (dup): Add missing extern "C" qualifier. Accommodate renaming of dtable::dup2 to dtable::dup3. (dup2): Ditto. Check newfd == oldfd here. (dup3): New function. Check newfd == oldfd here. (open): Set close_on_exec flag according to O_CLOEXEC flag before calling fhandler->open. * include/cygwin/version.h (CYGWIN_VERSION_API_MINOR): Bump.
		
			
				
	
	
		
			106 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			106 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /* dtable.h: fd table definition.
 | |
| 
 | |
|    Copyright 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
 | |
|    2010 Red Hat, Inc.
 | |
| 
 | |
| This file is part of Cygwin.
 | |
| 
 | |
| This software is a copyrighted work licensed under the terms of the
 | |
| Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
 | |
| details. */
 | |
| 
 | |
| /* Initial and increment values for cygwin's fd table */
 | |
| #define NOFILE_INCR    32
 | |
| /* Maximum size we allow expanding to.  */
 | |
| #define OPEN_MAX_MAX (100 * NOFILE_INCR)
 | |
| 
 | |
| #include "thread.h"
 | |
| #include "sync.h"
 | |
| 
 | |
| class suffix_info;
 | |
| 
 | |
| #define BFH_OPTS (PC_NULLEMPTY | PC_FULL | PC_POSIX)
 | |
| class dtable
 | |
| {
 | |
|   fhandler_base **fds;
 | |
| #ifdef NEWVFORK
 | |
|   fhandler_base **fds_on_hold;
 | |
| #endif
 | |
|   fhandler_base **archetypes;
 | |
|   unsigned narchetypes;
 | |
|   unsigned farchetype;
 | |
|   static const int initial_archetype_size = 8;
 | |
|   int first_fd_for_open;
 | |
|   int cnt_need_fixup_before;
 | |
|   void lock () {lock_process::locker.acquire ();}
 | |
|   void unlock () {lock_process::locker.release ();}
 | |
| public:
 | |
|   size_t size;
 | |
| 
 | |
|   dtable () : archetypes (NULL), narchetypes (0), farchetype (0), first_fd_for_open(3), cnt_need_fixup_before(0) {}
 | |
|   void init () {first_fd_for_open = 3;}
 | |
| 
 | |
|   void dec_need_fixup_before ()
 | |
|     { if (cnt_need_fixup_before > 0) --cnt_need_fixup_before; }
 | |
|   void inc_need_fixup_before ()
 | |
|     { cnt_need_fixup_before++; }
 | |
|   bool need_fixup_before ()
 | |
|     { return cnt_need_fixup_before > 0; }
 | |
| 
 | |
|   void move_fd (int, int);
 | |
|   int vfork_child_dup ();
 | |
|   void vfork_parent_restore ();
 | |
|   void vfork_child_fixup ();
 | |
|   fhandler_base *dup_worker (fhandler_base *oldfh, int flags);
 | |
|   int extend (int howmuch);
 | |
|   void fixup_after_fork (HANDLE);
 | |
|   inline int not_open (int fd)
 | |
|   {
 | |
|     lock ();
 | |
|     int res = fd < 0 || fd >= (int) size || fds[fd] == NULL;
 | |
|     unlock ();
 | |
|     return res;
 | |
|   }
 | |
|   int find_unused_handle (int start);
 | |
|   int find_unused_handle () { return find_unused_handle (first_fd_for_open);}
 | |
|   void release (int fd);
 | |
|   void init_std_file_from_handle (int fd, HANDLE handle);
 | |
|   int dup3 (int oldfd, int newfd, int flags);
 | |
|   void fixup_after_exec ();
 | |
|   inline fhandler_base *&operator [](int fd) const { return fds[fd]; }
 | |
|   bool select_read (int fd, select_stuff *);
 | |
|   bool select_write (int fd, select_stuff *);
 | |
|   bool select_except (int fd, select_stuff *);
 | |
|   operator fhandler_base **() {return fds;}
 | |
|   void stdio_init ();
 | |
|   void get_debugger_info ();
 | |
|   void set_file_pointers_for_exec ();
 | |
| #ifdef NEWVFORK
 | |
|   bool in_vfork_cleanup () {return fds_on_hold == fds;}
 | |
| #endif
 | |
|   fhandler_base *find_archetype (device& dev);
 | |
|   fhandler_base **add_archetype ();
 | |
|   void delete_archetype (fhandler_base *);
 | |
|   void fixup_before_exec (DWORD win_proc_id);
 | |
|   void fixup_before_fork (DWORD win_proc_id);
 | |
|   friend void dtable_init ();
 | |
|   friend void __stdcall close_all_files (bool);
 | |
|   friend class fhandler_disk_file;
 | |
|   friend class cygheap_fdmanip;
 | |
|   friend class cygheap_fdget;
 | |
|   friend class cygheap_fdnew;
 | |
|   friend class cygheap_fdenum;
 | |
|   friend class lock_process;
 | |
| };
 | |
| 
 | |
| fhandler_base *build_fh_dev (const device&, const char * = NULL);
 | |
| fhandler_base *build_fh_name (const char *, unsigned = 0, suffix_info * = NULL);
 | |
| fhandler_base *build_fh_pc (path_conv& pc, bool set_name = true);
 | |
| 
 | |
| void dtable_init ();
 | |
| void stdio_init ();
 | |
| extern dtable fdtab;
 | |
| 
 | |
| extern "C" int getfdtabsize ();
 | |
| extern "C" void setfdtabsize (int);
 |