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>
28 /* Set aside space for the table of fds */
33 fdtab.extend(NOFILE_INCR);
37 set_std_handle (int fd)
40 SetStdHandle (STD_INPUT_HANDLE, fdtab[fd]->get_handle ());
42 SetStdHandle (STD_OUTPUT_HANDLE, fdtab[fd]->get_output_handle ());
44 SetStdHandle (STD_ERROR_HANDLE, fdtab[fd]->get_output_handle ());
48 dtable::extend (int howmuch)
50 int new_size = size + howmuch;
51 fhandler_base **newfds;
56 /* Try to allocate more space for fd table. We can't call realloc()
57 here to preserve old table if memory allocation fails */
59 if (!(newfds = (fhandler_base **) calloc (new_size, sizeof newfds[0])))
61 debug_printf ("calloc failed");
66 memcpy (newfds, fds, size * sizeof (fds[0]));
72 debug_printf ("size %d, fds %p", size, fds);
76 /* Initialize the file descriptor/handle mapping table.
77 We only initialize the parent table here. The child table is
78 initialized at each fork () call. */
83 /* Set these before trying to output anything from strace.
84 Also, always set them even if we're to pick up our parent's fds
85 in case they're missed. */
87 if (!parent_alive && NOTSTATE(myself, PID_CYGPARENT))
89 HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
90 HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
91 HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
93 fdtab.init_std_file_from_handle (0, in, GENERIC_READ, "{stdin}");
95 /* STD_ERROR_HANDLE has been observed to be the same as
96 STD_OUTPUT_HANDLE. We need separate handles (e.g. using pipes
97 to pass data from child to parent). */
100 /* Since this code is not invoked for forked tasks, we don't have
101 to worry about the close-on-exec flag here. */
102 if (!DuplicateHandle (hMainProc, out, hMainProc, &err, 0,
103 1, DUPLICATE_SAME_ACCESS))
105 /* If that fails, do this as a fall back. */
107 system_printf ("couldn't make stderr distinct from stdout");
111 fdtab.init_std_file_from_handle (1, out, GENERIC_WRITE, "{stdout}");
112 fdtab.init_std_file_from_handle (2, err, GENERIC_WRITE, "{stderr}");
117 dtable::not_open (int fd)
119 SetResourceLock(LOCK_FD_LIST,READ_LOCK," not_open");
121 int res = fd < 0 || fd >= (int)size || fds[fd] == NULL;
123 ReleaseResourceLock(LOCK_FD_LIST,READ_LOCK," not open");
128 dtable::find_unused_handle (int start)
130 AssertResourceOwner(LOCK_FD_LIST, READ_LOCK);
134 for (int i = start; i < (int) size; i++)
135 /* See if open -- no need for overhead of not_open */
139 while (extend (NOFILE_INCR));
144 dtable::release (int fd)
154 dtable::init_std_file_from_handle (int fd, HANDLE handle,
155 DWORD myaccess, const char *name)
157 int bin = binmode ? O_BINARY : 0;
158 /* Check to see if we're being redirected - if not then
159 we open then as consoles */
160 if (fd == 0 || fd == 1 || fd == 2)
162 first_fd_for_open = 0;
163 /* See if we can consoleify it - if it is a console,
164 don't open it in binary. That will screw up our crlfs*/
165 CONSOLE_SCREEN_BUFFER_INFO buf;
166 if (GetConsoleScreenBufferInfo (handle, &buf))
169 if (ISSTATE (myself, PID_USETTY))
172 name = "/dev/conout";
174 else if (FlushConsoleInputBuffer (handle))
177 if (ISSTATE (myself, PID_USETTY))
184 build_fhandler (fd, name, handle)->init (handle, myaccess, bin);
186 paranoid_printf ("fd %d, handle %p", fd, handle);
191 cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
195 fd = fdtab.find_unused_handle();
196 fhandler_base *res = fdtab.build_fhandler (fd, name, handle);
197 res->init (handle, myaccess, bin);
202 dtable::build_fhandler (int fd, const char *name, HANDLE handle)
207 if ((devn = get_device_number (name, unit)) == FH_BAD)
210 int sal = sizeof (sa);
211 CONSOLE_SCREEN_BUFFER_INFO cinfo;
216 else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &cinfo))
218 else if (GetConsoleScreenBufferInfo (handle, &cinfo))
220 else if (wsock32_handle && getpeername ((SOCKET) handle, &sa, &sal))
222 else if (GetFileType (handle) == FILE_TYPE_PIPE)
224 else if (GetCommState (handle, &dcb))
230 return build_fhandler (fd, devn, name, unit);
234 dtable::build_fhandler (int fd, DWORD dev, const char *name, int unit)
237 void *buf = calloc (1, sizeof (fhandler_union) + 100);
243 fh = new (buf) fhandler_tty_master (name, unit);
248 fh = new (buf) fhandler_console (name);
251 fh = new (buf) fhandler_pty_master (name);
255 fh = new (buf) fhandler_tty_slave (name);
257 fh = new (buf) fhandler_tty_slave (unit, name);
260 fh = new (buf) fhandler_windows (name);
263 fh = new (buf) fhandler_serial (name, dev, unit);
268 fh = new (buf) fhandler_pipe (name, dev);
271 fh = new (buf) fhandler_socket (name);
274 fh = new (buf) fhandler_disk_file (NULL);
277 fh = new (buf) fhandler_dev_floppy (name, unit);
280 fh = new (buf) fhandler_dev_tape (name, unit);
283 fh = new (buf) fhandler_dev_null (name);
286 fh = new (buf) fhandler_dev_zero (name);
289 fh = new (buf) fhandler_dev_random (name, unit);
292 /* FIXME - this could recurse forever */
293 return build_fhandler (fd, name, NULL);
296 debug_printf ("%s - cb %d, fd %d, fh %p", fh->get_name () ?: "", fh->cb,
298 return fd >= 0 ? (fds[fd] = fh) : fh;
302 dtable::dup_worker (fhandler_base *oldfh)
304 fhandler_base *newfh = build_fhandler (-1, oldfh->get_device (), NULL);
306 newfh->set_io_handle (NULL);
307 if (oldfh->dup (newfh))
314 newfh->set_close_on_exec_flag (0);
320 dtable::dup2 (int oldfd, int newfd)
323 fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning
326 debug_printf ("dup2 (%d, %d)", oldfd, newfd);
328 if (not_open (oldfd))
330 syscall_printf ("fd %d not open", oldfd);
341 if ((newfh = dup_worker (fds[oldfd])) == NULL)
347 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
348 if ((size_t) newfd >= fdtab.size || newfd < 0)
350 syscall_printf ("new fd out of bounds: %d", newfd);
354 if (!not_open (newfd))
357 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
360 if ((res = newfd) <= 2)
361 set_std_handle (res);
365 syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd);
371 dtable::select_read (int fd, select_record *s)
373 if (fdtab.not_open (fd))
378 fhandler_base *fh = fdtab[fd];
379 s = fh->select_read (s);
383 debug_printf ("%s fd %d", fh->get_name (), fd);
388 dtable::select_write (int fd, select_record *s)
390 if (fdtab.not_open (fd))
395 fhandler_base *fh = fdtab[fd];
396 s = fh->select_write (s);
400 debug_printf ("%s fd %d", fh->get_name (), fd);
405 dtable::select_except (int fd, select_record *s)
407 if (fdtab.not_open (fd))
412 fhandler_base *fh = fdtab[fd];
413 s = fh->select_except (s);
417 debug_printf ("%s fd %d", fh->get_name (), fd);
422 * Function to take an existant dtable array
423 * and linearize it into a memory buffer.
424 * If memory buffer is NULL, it returns the size
425 * of memory buffer needed to do the linearization.
426 * On error returns -1.
430 dtable::linearize_fd_array (unsigned char *in_buf, int buflen)
432 /* If buf == NULL, just precalculate length */
435 buflen = sizeof (size_t);
436 for (int i = 0, max_used_fd = -1; i < (int)size; i++)
437 if (!not_open (i) && !fds[i]->get_close_on_exec ())
439 buflen += i - (max_used_fd + 1);
440 buflen += fds[i]->cb + strlen (fds[i]->get_name ()) + 1
441 + strlen (fds[i]->get_win32_name ()) + 1;
444 debug_printf ("needed buflen %d", buflen);
448 debug_printf ("in_buf = %x, buflen = %d", in_buf, buflen);
451 * Now linearize each open fd (write a 0xff byte for a closed fd).
452 * Write the name of the open fd first (null terminated). This
453 * allows the de_linearizeing code to determine what kind of fhandler_xxx
460 total_size = sizeof (size_t);
461 if (total_size > buflen)
463 system_printf ("FATAL: linearize_fd_array exceeded buffer size");
467 unsigned char *buf = in_buf;
468 buf += sizeof (size_t); /* skip over length which is added later */
470 for (i = 0, total_size = sizeof (size_t); total_size < buflen; i++)
472 if (not_open (i) || fds[i]->get_close_on_exec ())
474 debug_printf ("linearizing closed fd %d",i);
475 *buf = 0xff; /* place holder */
480 len = fds[i]->linearize (buf);
481 debug_printf ("fd %d, len %d, name %s, device %p", i, len, buf,
482 fds[i]->get_device ());
490 memcpy (in_buf, &i, sizeof (size_t));
491 if (total_size != buflen)
492 system_printf ("out of sync %d != %d", total_size, buflen);
497 * Function to take a linearized dtable array in a memory buffer and
498 * re-create the original dtable array.
502 dtable::de_linearize_fd_array (LPBYTE buf)
504 int len, max_used_fd;
507 debug_printf ("buf %x", buf);
509 /* First get the number of fd's - use this to set the fdtabsize.
510 NB. This is the only place in the code this should be done !!
513 memcpy ((char *) &max_used_fd, buf, sizeof (int));
514 buf += sizeof (size_t);
516 inc_size = NOFILE_INCR * ((max_used_fd + NOFILE_INCR - 1) / NOFILE_INCR) -
518 debug_printf ("max_used_fd %d, inc size %d", max_used_fd, inc_size);
519 if (inc_size > 0 && !extend (inc_size))
521 system_printf ("out of memory");
525 for (int i = 0; i <= max_used_fd; i++)
527 /* 0xFF means closed */
532 debug_printf ("closed fd %d", i);
535 /* fd was open - de_linearize it */
536 /* Get the null-terminated name. It is followed by an image of
537 the actual fhandler_* structure. Use the status field from
538 this to build a new fhandler type. */
543 win32 = strchr ((char *)obuf, '\0') + 1;
544 buf = (LPBYTE)strchr ((char *)win32, '\0') + 1;
545 memcpy ((char *)&status, buf + FHSTATOFF, sizeof(DWORD));
546 debug_printf ("fd %d, name %s, win32 name %s, status %p",
547 i, obuf, win32, status);
548 len = build_fhandler (i, status, (const char *) NULL)->
549 de_linearize ((char *) buf, (char *) obuf, win32);
552 debug_printf ("len %d", buf - obuf);
554 first_fd_for_open = 0;
559 dtable::fixup_after_fork (HANDLE parent)
561 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
562 for (size_t i = 0; i < size; i++)
565 fhandler_base *fh = fds[i];
566 if (fh->get_close_on_exec () || fh->get_need_fork_fixup ())
568 debug_printf ("fd %d(%s)", i, fh->get_name ());
569 fh->fixup_after_fork (parent);
572 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
576 dtable::vfork_child_dup ()
578 fhandler_base **newtable;
579 newtable = (fhandler_base **) calloc (size, sizeof(fds[0]));
582 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
583 for (size_t i = 0; i < size; i++)
586 else if ((newtable[i] = dup_worker (fds[i])) == NULL)
595 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
600 dtable::vfork_parent_restore ()
602 SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
605 fhandler_base **deleteme = fds;
610 ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");