1 /* dtable.cc: file descriptor support.
3 Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
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__
16 #include <sys/socket.h>
21 #include <sys/cygwin.h>
35 static DWORD std_consts[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
38 /* Set aside space for the table of fds */
43 fdtab.extend(NOFILE_INCR);
47 set_std_handle (int fd)
50 SetStdHandle (std_consts[fd], fdtab[fd]->get_handle ());
52 SetStdHandle (std_consts[fd], fdtab[fd]->get_output_handle ());
56 dtable::extend (int howmuch)
58 int new_size = size + howmuch;
59 fhandler_base **newfds;
64 /* Try to allocate more space for fd table. We can't call realloc()
65 here to preserve old table if memory allocation fails */
67 if (!(newfds = (fhandler_base **) ccalloc (HEAP_ARGV, new_size, sizeof newfds[0])))
69 debug_printf ("calloc failed");
74 memcpy (newfds, fds, size * sizeof (fds[0]));
80 debug_printf ("size %d, fds %p", size, fds);
84 /* Initialize the file descriptor/handle mapping table.
85 We only initialize the parent table here. The child table is
86 initialized at each fork () call. */
91 /* Set these before trying to output anything from strace.
92 Also, always set them even if we're to pick up our parent's fds
93 in case they're missed. */
95 if (!myself->ppid_handle && NOTSTATE(myself, PID_CYGPARENT))
97 HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
98 HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
99 HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
101 fdtab.init_std_file_from_handle (0, in, GENERIC_READ, "{stdin}");
103 /* STD_ERROR_HANDLE has been observed to be the same as
104 STD_OUTPUT_HANDLE. We need separate handles (e.g. using pipes
105 to pass data from child to parent). */
108 /* Since this code is not invoked for forked tasks, we don't have
109 to worry about the close-on-exec flag here. */
110 if (!DuplicateHandle (hMainProc, out, hMainProc, &err, 0,
111 1, DUPLICATE_SAME_ACCESS))
113 /* If that fails, do this as a fall back. */
115 system_printf ("couldn't make stderr distinct from stdout");
119 fdtab.init_std_file_from_handle (1, out, GENERIC_WRITE, "{stdout}");
120 fdtab.init_std_file_from_handle (2, err, GENERIC_WRITE, "{stderr}");
125 dtable::not_open (int fd)
127 SetResourceLock(LOCK_FD_LIST,READ_LOCK," not_open");
129 int res = fd < 0 || fd >= (int)size || fds[fd] == NULL;
131 ReleaseResourceLock(LOCK_FD_LIST,READ_LOCK," not open");
136 dtable::find_unused_handle (int start)
138 AssertResourceOwner(LOCK_FD_LIST, READ_LOCK);
142 for (int i = start; i < (int) size; i++)
143 /* See if open -- no need for overhead of not_open */
147 while (extend (NOFILE_INCR));
152 dtable::release (int fd)
156 if ((fds[fd]->get_device () & FH_DEVMASK) == FH_SOCKET)
157 dec_need_fixup_before ();
164 dtable::init_std_file_from_handle (int fd, HANDLE handle,
165 DWORD myaccess, const char *name)
167 int bin = binmode ? O_BINARY : 0;
168 /* Check to see if we're being redirected - if not then
169 we open then as consoles */
170 if (fd == 0 || fd == 1 || fd == 2)
172 first_fd_for_open = 0;
173 /* See if we can consoleify it - if it is a console,
174 don't open it in binary. That will screw up our crlfs*/
175 CONSOLE_SCREEN_BUFFER_INFO buf;
176 if (GetConsoleScreenBufferInfo (handle, &buf))
179 if (ISSTATE (myself, PID_USETTY))
182 name = "/dev/conout";
184 else if (FlushConsoleInputBuffer (handle))
187 if (ISSTATE (myself, PID_USETTY))
194 build_fhandler (fd, name, handle)->init (handle, myaccess, bin);
196 paranoid_printf ("fd %d, handle %p", fd, handle);
201 cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
205 fd = fdtab.find_unused_handle();
206 fhandler_base *res = fdtab.build_fhandler (fd, name, handle);
207 res->init (handle, myaccess, bin);
212 dtable::build_fhandler (int fd, const char *name, HANDLE handle)
217 if ((devn = get_device_number (name, unit)) == FH_BAD)
220 int sal = sizeof (sa);
221 CONSOLE_SCREEN_BUFFER_INFO cinfo;
226 else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &cinfo))
228 else if (GetConsoleScreenBufferInfo (handle, &cinfo))
230 else if (wsock32_handle && getpeername ((SOCKET) handle, &sa, &sal))
232 else if (GetFileType (handle) == FILE_TYPE_PIPE)
234 else if (GetCommState (handle, &dcb))
240 return build_fhandler (fd, devn, name, unit);
244 dtable::build_fhandler (int fd, DWORD dev, const char *name, int unit)
247 void *buf = ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_union) + 100);
253 fh = new (buf) fhandler_tty_master (name, unit);
258 fh = new (buf) fhandler_console (name);
261 fh = new (buf) fhandler_pty_master (name);
265 fh = new (buf) fhandler_tty_slave (name);
267 fh = new (buf) fhandler_tty_slave (unit, name);
270 fh = new (buf) fhandler_windows (name);
273 fh = new (buf) fhandler_serial (name, dev, unit);
278 fh = new (buf) fhandler_pipe (name, dev);
281 fh = new (buf) fhandler_socket (name);
284 fh = new (buf) fhandler_disk_file (NULL);
287 fh = new (buf) fhandler_dev_floppy (name, unit);
290 fh = new (buf) fhandler_dev_tape (name, unit);
293 fh = new (buf) fhandler_dev_null (name);
296 fh = new (buf) fhandler_dev_zero (name);
299 fh = new (buf) fhandler_dev_random (name, unit);
302 fh = new (buf) fhandler_dev_mem (name, unit);
305 fh = new (buf) fhandler_dev_clipboard (name);
308 /* FIXME - this could recurse forever */
309 return build_fhandler (fd, name, NULL);
312 debug_printf ("%s - cb %d, fd %d, fh %p", fh->get_name () ?: "", fh->cb,
314 return fd >= 0 ? (fds[fd] = fh) : fh;
318 dtable::dup_worker (fhandler_base *oldfh)
320 fhandler_base *newfh = build_fhandler (-1, oldfh->get_device (), NULL);
322 newfh->set_io_handle (NULL);
323 if (oldfh->dup (newfh))
330 newfh->set_close_on_exec_flag (0);
336 dtable::dup2 (int oldfd, int newfd)
339 fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning
342 debug_printf ("dup2 (%d, %d)", oldfd, newfd);
344 if (not_open (oldfd))
346 syscall_printf ("fd %d not open", oldfd);
357 if ((newfh = dup_worker (fds[oldfd])) == NULL)
363 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
365 if ((size_t) newfd >= fdtab.size || newfd < 0)
367 syscall_printf ("new fd out of bounds: %d", newfd);
372 if ((size_t) newfd >= fdtab.size)
374 int inc_size = NOFILE_INCR * ((newfd + NOFILE_INCR - 1) / NOFILE_INCR) -
376 fdtab.extend (inc_size);
379 if (!not_open (newfd))
384 if ((fds[newfd]->get_device () & FH_DEVMASK) == FH_SOCKET)
385 inc_need_fixup_before ();
387 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
390 if ((res = newfd) <= 2)
391 set_std_handle (res);
395 syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd);
401 dtable::select_read (int fd, select_record *s)
408 fhandler_base *fh = fds[fd];
409 s = fh->select_read (s);
413 debug_printf ("%s fd %d", fh->get_name (), fd);
418 dtable::select_write (int fd, select_record *s)
425 fhandler_base *fh = fds[fd];
426 s = fh->select_write (s);
430 debug_printf ("%s fd %d", fh->get_name (), fd);
435 dtable::select_except (int fd, select_record *s)
442 fhandler_base *fh = fds[fd];
443 s = fh->select_except (s);
447 debug_printf ("%s fd %d", fh->get_name (), fd);
451 /* Function to walk the fd table after an exec and perform
452 per-fhandler type fixups. */
454 dtable::fixup_before_fork (DWORD target_proc_id)
456 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
458 for (size_t i = 0; i < size; i++)
459 if ((fh = fds[i]) != NULL)
461 debug_printf ("fd %d(%s)", i, fh->get_name ());
462 fh->fixup_before_fork_exec (target_proc_id);
464 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
468 dtable::fixup_before_exec (DWORD target_proc_id)
470 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
472 for (size_t i = 0; i < size; i++)
473 if ((fh = fds[i]) != NULL && (!fh->get_close_on_exec ()))
475 debug_printf ("fd %d(%s)", i, fh->get_name ());
476 fh->fixup_before_fork_exec (target_proc_id);
478 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
482 dtable::fixup_after_exec (HANDLE parent, size_t sz, fhandler_base **f)
486 first_fd_for_open = 0;
488 for (size_t i = 0; i < size; i++)
489 if ((fh = fds[i]) != NULL)
491 fh->clear_readahead ();
492 if (fh->get_close_on_exec ())
496 fh->fixup_after_exec (parent);
498 SetStdHandle (std_consts[i], fh->get_io_handle ());
500 SetStdHandle (std_consts[i], fh->get_output_handle ());
506 dtable::fixup_after_fork (HANDLE parent)
508 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
510 for (size_t i = 0; i < size; i++)
511 if ((fh = fds[i]) != NULL)
513 if (fh->get_close_on_exec () || fh->get_need_fork_fixup ())
515 debug_printf ("fd %d(%s)", i, fh->get_name ());
516 fh->fixup_after_fork (parent);
519 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
523 dtable::vfork_child_dup ()
525 fhandler_base **newtable;
526 newtable = (fhandler_base **) ccalloc (HEAP_ARGV, size, sizeof(fds[0]));
529 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
530 for (size_t i = 0; i < size; i++)
533 else if ((newtable[i] = dup_worker (fds[i])) == NULL)
543 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
548 dtable::vfork_parent_restore ()
550 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
553 fhandler_base **deleteme = fds;
558 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");