1 /* dtable.cc: file descriptor support.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 #define __INSIDE_CYGWIN_NET__
14 #include <sys/socket.h>
18 #include <sys/cygwin.h>
23 #define USE_SYS_TYPES_FD_SET
27 #include "perprocess.h"
35 static const NO_COPY DWORD std_consts[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
38 static const char *handle_to_fn (HANDLE, char *);
40 static const char NO_COPY unknown_file[] = "some disk file";
42 /* Set aside space for the table of fds */
46 if (!cygheap->fdtab.size)
47 cygheap->fdtab.extend (NOFILE_INCR);
51 set_std_handle (int fd)
54 SetStdHandle (std_consts[fd], cygheap->fdtab[fd]->get_handle ());
56 SetStdHandle (std_consts[fd], cygheap->fdtab[fd]->get_output_handle ());
60 dtable::extend (int howmuch)
62 int new_size = size + howmuch;
63 fhandler_base **newfds;
68 if (new_size > (100 * NOFILE_INCR))
74 /* Try to allocate more space for fd table. We can't call realloc ()
75 here to preserve old table if memory allocation fails */
77 if (!(newfds = (fhandler_base **) ccalloc (HEAP_ARGV, new_size, sizeof newfds[0])))
79 debug_printf ("calloc failed");
86 memcpy (newfds, fds, size * sizeof (fds[0]));
91 debug_printf ("size %d, fds %p", size, fds);
96 dtable::get_debugger_info ()
98 if (being_debugged ())
100 char std[3][sizeof ("/dev/ttyNNNN")];
101 std[0][0] = std[1][0] = std [2][0] = '\0';
102 char buf[sizeof ("cYgstd %x") + 32];
103 sprintf (buf, "cYgstd %x %x %x", (unsigned) &std, sizeof (std[0]), 3);
104 OutputDebugString (buf);
105 for (int i = 0; i < 3; i++)
108 HANDLE h = GetStdHandle (std_consts[i]);
109 fhandler_base *fh = build_fh_name (std[i]);
113 if (!fh->open ((i ? O_WRONLY : O_RDONLY) | O_BINARY, 0777))
121 /* Initialize the file descriptor/handle mapping table.
122 This function should only be called when a cygwin function is invoked
123 by a non-cygwin function, i.e., it should only happen very rarely. */
126 dtable::stdio_init ()
128 extern void set_console_ctty ();
129 /* Set these before trying to output anything from strace.
130 Also, always set them even if we're to pick up our parent's fds
131 in case they're missed. */
133 if (myself->ppid_handle || ISSTATE (myself, PID_CYGPARENT))
136 HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
137 HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
138 HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
140 init_std_file_from_handle (0, in);
142 /* STD_ERROR_HANDLE has been observed to be the same as
143 STD_OUTPUT_HANDLE. We need separate handles (e.g. using pipes
144 to pass data from child to parent). */
147 /* Since this code is not invoked for forked tasks, we don't have
148 to worry about the close-on-exec flag here. */
149 if (!DuplicateHandle (hMainProc, out, hMainProc, &err, 0,
150 1, DUPLICATE_SAME_ACCESS))
152 /* If that fails, do this as a fall back. */
154 system_printf ("couldn't make stderr distinct from stdout");
158 init_std_file_from_handle (1, out);
159 init_std_file_from_handle (2, err);
160 /* Assign the console as the controlling tty for this process if we actually
161 have a console and no other controlling tty has been assigned. */
162 if (myself->ctty < 0 && GetConsoleCP () > 0)
167 dtable::find_unused_handle (int start)
169 AssertResourceOwner (LOCK_FD_LIST, READ_LOCK);
172 for (size_t i = start; i < size; i++)
173 /* See if open -- no need for overhead of not_open */
177 while (extend (NOFILE_INCR));
182 dtable::release (int fd)
186 if (fds[fd]->need_fixup_before ())
187 dec_need_fixup_before ();
194 cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
198 fd = cygheap->fdtab.find_unused_handle ();
199 fhandler_base *fh = build_fh_name (name);
200 cygheap->fdtab[fd] = fh;
201 fh->init (handle, myaccess, bin ?: fh->pc_binmode ());
206 dtable::init_std_file_from_handle (int fd, HANDLE handle)
208 const char *name = NULL;
209 CONSOLE_SCREEN_BUFFER_INFO buf;
211 int sal = sizeof (sa);
213 unsigned bin = O_BINARY;
216 dev.devn = 0; /* FIXME: device */
217 first_fd_for_open = 0;
223 DWORD ft = GetFileType (handle);
224 if (ft != FILE_TYPE_UNKNOWN || GetLastError () != ERROR_INVALID_HANDLE)
226 /* See if we can consoleify it */
227 if (GetConsoleScreenBufferInfo (handle, &buf))
229 if (ISSTATE (myself, PID_USETTY))
230 dev.parse ("/dev/tty");
234 else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &buf))
236 if (ISSTATE (myself, PID_USETTY))
237 dev.parse ("/dev/tty");
241 else if (ft == FILE_TYPE_PIPE)
248 else if (wsock_started && getpeername ((SOCKET) handle, &sa, &sal) == 0)
250 else if (GetCommState (handle, &dcb))
251 dev.parse ("/dev/ttyS0");
254 name = handle_to_fn (handle, (char *) alloca (MAX_PATH + 100));
266 fh = build_fh_dev (dev);
268 fh = build_fh_name (name);
271 cygheap->fdtab[fd] = fh;
275 bin = fh->get_default_fmode (O_RDWR);
280 else if (name != unknown_file)
281 bin = fh->pc_binmode ();
284 fh->init (handle, GENERIC_READ | GENERIC_WRITE, bin);
286 paranoid_printf ("fd %d, handle %p", fd, handle);
290 #define cnew(name) new ((void *) ccalloc (HEAP_FHANDLER, 1, sizeof (name))) name
292 build_fh_name (const char *name, HANDLE h, unsigned opt, suffix_info *si)
294 path_conv pc (name, opt | PC_NULLEMPTY | PC_FULL | PC_POSIX, si);
297 set_errno (pc.error);
298 return cnew (fhandler_nodevice) ();
301 if (!pc.exists () && h)
304 return build_fh_pc (pc);
308 build_fh_dev (const device& dev, const char *unix_name)
311 char *w32buf = const_cast<char *> (pc.get_win32 ());
313 __small_sprintf (w32buf, dev.fmt, dev.minor);
315 pc.set_normalized_path (unix_name);
317 pc.set_normalized_path (dev.name);
320 pc.set_normalized_path (w32buf);
321 for (char *p = strchr (pc.normalized_path, '\\');
323 p = strchr (p + 1, '\\'))
326 return build_fh_pc (pc);
330 build_fh_pc (path_conv& pc)
332 fhandler_base *fh = NULL;
335 switch (pc.dev.major)
338 fh = cnew (fhandler_tty_slave) ();
341 fh = cnew (fhandler_tty_master) ();
343 case DEV_CYGDRIVE_MAJOR:
344 fh = cnew (fhandler_cygdrive) ();
346 case DEV_FLOPPY_MAJOR:
347 case DEV_CDROM_MAJOR:
349 case DEV_RAWDRIVE_MAJOR:
350 fh = cnew (fhandler_dev_floppy) ();
353 fh = cnew (fhandler_dev_tape) ();
362 fh = cnew (fhandler_console) ();
365 fh = cnew (fhandler_cygdrive) ();
368 fh = cnew (fhandler_pty_master) ();
371 fh = cnew (fhandler_windows) ();
374 fh = cnew (fhandler_serial) ();
377 fh = cnew (fhandler_fifo) ();
382 fh = cnew (fhandler_pipe) ();
390 fh = cnew (fhandler_socket) ();
393 fh = cnew (fhandler_disk_file) ();
396 fh = cnew (fhandler_dev_null) ();
399 fh = cnew (fhandler_dev_zero) ();
403 fh = cnew (fhandler_dev_random) ();
407 fh = cnew (fhandler_dev_mem) ();
410 fh = cnew (fhandler_dev_clipboard) ();
413 fh = cnew (fhandler_dev_dsp) ();
416 fh = cnew (fhandler_proc) ();
419 fh = cnew (fhandler_registry) ();
422 fh = cnew (fhandler_process) ();
426 device newdev = pc.dev;
427 newdev.tty_to_real_device ();
431 fh = cnew (fhandler_console) ();
434 fh = cnew (fhandler_tty_slave) ();
441 fh = cnew (fhandler_nodevice) ();
445 debug_printf ("fh %p", fh);
450 dtable::dup_worker (fhandler_base *oldfh)
452 fhandler_base *newfh = build_fh_pc (oldfh->pc);
454 newfh->set_io_handle (NULL);
455 if (oldfh->dup (newfh))
462 newfh->set_close_on_exec_flag (0);
464 debug_printf ("duped '%s' old %p, new %p", oldfh->get_name (), oldfh->get_io_handle (), newfh->get_io_handle ());
469 dtable::dup2 (int oldfd, int newfd)
472 fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning
475 debug_printf ("dup2 (%d, %d)", oldfd, newfd);
476 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
478 if (not_open (oldfd))
480 syscall_printf ("fd %d not open", oldfd);
487 syscall_printf ("new fd out of bounds: %d", newfd);
498 if ((newfh = dup_worker (fds[oldfd])) == NULL)
504 debug_printf ("newfh->io_handle %p, oldfh->io_handle %p",
505 newfh->get_io_handle (), fds[oldfd]->get_io_handle ());
507 if (!not_open (newfd))
509 else if ((size_t) newfd < size)
511 else if (find_unused_handle (newfd) < 0)
520 if ((res = newfd) <= 2)
521 set_std_handle (res);
525 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
526 syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd);
532 dtable::find_fifo (ATOM hill)
534 SetResourceLock (LOCK_FD_LIST, READ_LOCK, "dup");
535 for (unsigned i = 0; i < size; i++)
537 fhandler_base *fh = fds[i];
538 if (fh && fh->isfifo () && ((fhandler_fifo *) fh)->get_atom () == hill)
539 return (fhandler_fifo *) fh;
545 dtable::select_read (int fd, select_record *s)
552 fhandler_base *fh = fds[fd];
553 s = fh->select_read (s);
557 debug_printf ("%s fd %d", fh->get_name (), fd);
562 dtable::select_write (int fd, select_record *s)
569 fhandler_base *fh = fds[fd];
570 s = fh->select_write (s);
574 debug_printf ("%s fd %d", fh->get_name (), fd);
579 dtable::select_except (int fd, select_record *s)
586 fhandler_base *fh = fds[fd];
587 s = fh->select_except (s);
591 debug_printf ("%s fd %d", fh->get_name (), fd);
595 /* Function to walk the fd table after an exec and perform
596 per-fhandler type fixups. */
598 dtable::fixup_before_fork (DWORD target_proc_id)
600 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork");
602 for (size_t i = 0; i < size; i++)
603 if ((fh = fds[i]) != NULL)
605 debug_printf ("fd %d (%s)", i, fh->get_name ());
606 fh->fixup_before_fork_exec (target_proc_id);
608 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork");
612 dtable::fixup_before_exec (DWORD target_proc_id)
614 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
616 for (size_t i = 0; i < size; i++)
617 if ((fh = fds[i]) != NULL && !fh->get_close_on_exec ())
619 debug_printf ("fd %d (%s)", i, fh->get_name ());
620 fh->fixup_before_fork_exec (target_proc_id);
622 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
626 dtable::set_file_pointers_for_exec ()
628 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "set_file_pointers_for_exec");
630 for (size_t i = 0; i < size; i++)
631 if ((fh = fds[i]) != NULL && fh->get_flags () & O_APPEND)
632 SetFilePointer (fh->get_handle (), 0, 0, FILE_END);
633 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
637 dtable::fixup_after_exec (HANDLE parent)
639 first_fd_for_open = 0;
641 for (size_t i = 0; i < size; i++)
642 if ((fh = fds[i]) != NULL)
644 fh->clear_readahead ();
645 if (fh->get_close_on_exec ())
649 fh->fixup_after_exec (parent);
651 SetStdHandle (std_consts[i], fh->get_io_handle ());
653 SetStdHandle (std_consts[i], fh->get_output_handle ());
659 dtable::fixup_after_fork (HANDLE parent)
662 for (size_t i = 0; i < size; i++)
663 if ((fh = fds[i]) != NULL)
665 if (fh->get_close_on_exec () || fh->get_need_fork_fixup ())
667 debug_printf ("fd %d (%s)", i, fh->get_name ());
668 fh->fixup_after_fork (parent);
671 SetStdHandle (std_consts[i], fh->get_io_handle ());
673 SetStdHandle (std_consts[i], fh->get_output_handle ());
678 dtable::vfork_child_dup ()
680 fhandler_base **newtable;
681 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
682 newtable = (fhandler_base **) ccalloc (HEAP_ARGV, size, sizeof (fds[0]));
685 /* Remove impersonation */
686 cygheap->user.deimpersonate ();
688 for (size_t i = 0; i < size; i++)
691 else if ((newtable[i] = dup_worker (fds[i])) != NULL)
692 newtable[i]->set_close_on_exec (fds[i]->get_close_on_exec ());
704 /* Restore impersonation */
705 cygheap->user.reimpersonate ();
707 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
712 dtable::vfork_parent_restore ()
714 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore");
717 fhandler_base **deleteme = fds;
722 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore");
727 dtable::vfork_child_fixup ()
731 debug_printf ("here");
732 fhandler_base **saveme = fds;
736 for (int i = 0; i < (int) cygheap->fdtab.size; i++)
737 if ((fh = cygheap->fdtab[i]) != NULL)
739 fh->clear_readahead ();
740 if (fh->get_close_on_exec ())
745 cygheap->fdtab.release (i);
756 #define DEVICE_PREFIX "\\device\\"
757 #define DEVICE_PREFIX_LEN sizeof (DEVICE_PREFIX) - 1
758 #define REMOTE "\\Device\\LanmanRedirector\\"
759 #define REMOTE_LEN sizeof (REMOTE) - 1
762 handle_to_fn (HANDLE h, char *posix_fn)
764 OBJECT_NAME_INFORMATION *ntfn;
767 memset (fnbuf, 0, sizeof (fnbuf));
768 ntfn = (OBJECT_NAME_INFORMATION *) fnbuf;
769 ntfn->Name.MaximumLength = sizeof (fnbuf) - sizeof (*ntfn);
770 ntfn->Name.Buffer = (WCHAR *) (ntfn + 1);
772 DWORD res = NtQueryObject (h, ObjectNameInformation, ntfn, sizeof (fnbuf), NULL);
776 strcpy (posix_fn, unknown_file);
777 debug_printf ("NtQueryObject failed");
781 // NT seems to do this on an unopened file
782 if (!ntfn->Name.Buffer)
784 debug_printf ("nt->Name.Buffer == NULL");
788 ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = 0;
790 char win32_fn[MAX_PATH + 100];
791 sys_wcstombs (win32_fn, ntfn->Name.Buffer, ntfn->Name.Length);
792 debug_printf ("nt name '%s'", win32_fn);
793 if (!strncasematch (win32_fn, DEVICE_PREFIX, DEVICE_PREFIX_LEN)
794 || !QueryDosDevice (NULL, fnbuf, sizeof (fnbuf)))
795 return strcpy (posix_fn, win32_fn);
797 char *p = strechr (win32_fn + DEVICE_PREFIX_LEN, '\\');
799 int n = p - win32_fn;
801 char *maxmatchdos = NULL;
802 for (char *s = fnbuf; *s; s = strchr (s, '\0') + 1)
804 char device[MAX_PATH + 10];
805 device[MAX_PATH + 9] = '\0';
806 if (strchr (s, ':') == NULL)
808 if (!QueryDosDevice (s, device, sizeof (device) - 1))
810 char *q = strrchr (device, ';');
813 char *r = strchr (q, '\\');
817 int devlen = strlen (device);
818 if (device[devlen - 1] == '\\')
819 device[--devlen] = '\0';
820 if (devlen < maxmatchlen)
822 if (!strncasematch (device, win32_fn, devlen) ||
823 (win32_fn[devlen] != '\0' && win32_fn[devlen] != '\\'))
825 maxmatchlen = devlen;
827 debug_printf ("current match '%s'", device);
830 char *w32 = win32_fn;
833 n = strlen (maxmatchdos);
834 if (maxmatchdos[n - 1] == '\\')
836 w32 += maxmatchlen - n;
837 memcpy (w32, maxmatchdos, n);
840 else if (strncasematch (w32, REMOTE, REMOTE_LEN))
842 w32 += REMOTE_LEN - 2;
844 debug_printf ("remote drive");
848 debug_printf ("derived path '%s'", w32);
849 cygwin_conv_to_full_posix_path (w32, posix_fn);