From 7ac6173643b13a5a3bc7daf3649eb20c48c9c908 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Thu, 25 Sep 2003 00:37:18 +0000 Subject: * devices.cc: New file. * devices.gperf: New file. * devices.shilka: New file. * cygwin-gperf: New file. * cygwin-shilka: New file. * fhandler_fifo.cc: New file. * fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes fhandler.h throughout. Remove device argument and unit arguments from fhandler constructors throughout. Remove pc arguments to fhandler functions and use internal pc element instead, throughout. Use dev element in pc throughout. Use major/minor elements rather than units and device numbers previously in fhandler class. Use correct methods for fhandler file names rather than directly accessing file name variables, throughout. * Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o * dcrt0.cc (dll_crt0_1): Call device::init. * devices.h: Renumber devices based on more Linux-like major/minor numbers. Add more devices. Declare standard device storage. (device): Declare struct. * dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type. * dtable.cc (dtable::get_debugger_info): Ditto. (cygwin_attach_handle_to_fd): Ditto. (dtable::release): Remove special FH_SOCKET case in favor of generic "need_fixup_before" test. (dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name to build standard fhandler. (dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move out of dtable class. Don't accept a path_conv argument. Just build it here and pass it to: (build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class. Use intrinsic device type in path_conv to create new fhandler. (build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class. Simplify arguments to just take new 'device' type and a name. Just return pointer to fhandler rather than trying to insert into dtable. (dtable::dup_worker): Accommodate above build_fh name changes. (dtable::find_fifo): New (currently broken) function. (handle_to_fn): Use strechr for efficiency. * dtable.h: Reflect above build_fh name changes and argument differences. (fhandler_base *&operator []): Return self rather than copy of self. * fhandler.cc (fhandler_base::operator =): Use pc element to set normalized path. (fhandler_base::set_name): Ditto. (fhandler_base::raw_read): Use method to access name. (fhandler_base::write): Correctly use get_output_handle rather than get_handle. (handler_base::device_access_denied): New function. (fhandler_base::open): Eliminate pc argument and use pc element of fhandler_base throughout. (fhandler_base::fstat): Detect if device is based in filesystem and use fstat_fs to calculate stat, if so. (fhandler_base::fhandler_base): Eliminate handling of file names and, instead, just free appropriate component from pc. (fhandler_base::opendir): Remove path_conv parameter. * fhandler.h: Remove all device flags. (fhandler_base::pc): New element. (fhandler_base::set_name): Change argument to path_conv. (fhandler_base::error): New function. (fhandler_base::exists): New function. (fhandler_base::pc_binmode): New function. (fhandler_base::dev): New function. (fhandler_base::open_fs): New function. (fhandler_base::fstat_fs): New function. (fhandler_base::fstat_by_name): New function. (fhandler_base::fstat_by_handle): New function. (fhandler_base::isfifo): New function. (fhandler_base::is_slow): New function. (fhandler_base::is_auto_device): New function. (fhandler_base::is_fs_special): New function. (fhandler_base::device_access_denied): New function. (fhandler_base::operator DWORD&): New operator. (fhandler_base::get_name): Return normalized path from pc. (fhandler_base::get_win32_name): Return windows path from pc. (fhandler_base::isdevice): Renamed from is_device. (fhandler_base::get_native_name): Return device format. (fhandler_fifo): New class. (fhandler_nodevice): New class. (select_stuff::device_specific): Remove array. (select_stuff::device_specific_pipe): New class element. (select_stuff::device_specific_socket): New class element. (select_stuff::device_specific_serial): New class element. (select_stuff::select_stuff): Initialize new elements. * fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class from fhandler_disk_file. (fhandler_base::fstat_by_name): Ditto. (fhandler_base::fstat_by_name): Ditto. (fhandler_disk_file::open): Move most functionality into fhandler_base::open_fs. (fhandler_base::open_fs): New function. (fhandler_disk_file::close): Move most functionality into fhandler_base::close_fs. (fhandler_base::close_fs): New function. * fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging output. * fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard urandom device into appropriate place. (fhandler_socket::accept): Reflect change in fdsock return value. * fhandler_tty.cc: See "throughouts" above. * net.cc: Accommodate fdsock change throughout. (fdsock): Return success or failure, accept fd argument and device argument. * path.cc (symlink_info::major): New element. (symlink_info::minor): New element. (symlink_info::parse_device): Declare new function. (fs_info::update): Accommodate changes in path_conv class. (path_conv::fillin): Ditto. (path_conv::return_and_clear_normalized_path): Eliminate. (path_conv::set_normalized_path): New function. (path_conv::path_conv): Set info in dev element. Use path_conv methods Check for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff. where appropriate rather than direct access. Use set_normalized_path to set normalized path. (windows_device_names): Eliminate. (get_dev): Ditto. (get_raw_device_number): Ditto. (get_device_number): Ditto. (win32_device_name): Call new device name parser to do most of the heavy lifting. (mount_info::conv_to_win32_path): Fill in dev field as appropriate. (symlink_worker): Handle new device files. (symlink_info::check): Ditto. (symlink_info::parse_device): Define new function. * path.h (executable_states): Move here from fhandler.h. (fs_info): Rename variables to *_storage and create methods for accessing same. (path_conv): Add dev element, remove devn and unit and adjust inline methods to accommodate. (set_normalized_path): Declare new function. * pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos. (_pinfo::commune_send): Ditto. * pipe.cc (fhandler_pipe::close): check for existence of handle before closing it. (handler_pipe::create): Rename from make_pipe. Change arguments to accept fhandler_pipe array. Accommodate fifos. (pipe): Rework to deal with fhandler_pipe::create changes. (_pipe): Ditto. * select.cc: Use individual device_specific types throughout rather than indexing with obsolete device number. (set_bits): Use is_socket call rather than checking device number. * shared_info.h (CURR_MOUNT_MAGIC): Update. (conv_to_win32_path): Reflect addition of device argument. * syscalls.cc (mknod_worker): New function. (open): Use build_fh_name to build fhandler. (chown_worker): Detect if this is an 'auto' device rather than an on-filesystem device and handle appropriately. (chmod_device): New function. (chmod): Detect if this is an 'auto' device rather than an on-filesystem device and handle appropriately. Use chmod_device to set mode of in-filesystem devices. (stat_worker): Eliminate path_conv argument. Call build_fh_name to construct fhandler. Use fh->error() rather than pc->error to detect errors in fhandler construction. (access_worker): New function pulled from access. Accommodate in-filesystem devices. (access): Use access_worker. (fpathconf): Detect if this is an 'auto' device rather than an on-filesystem device and handle appropriately. (mknod_worker): New function. (mknod32): New function. (chroot): Free normalized path -- assuming it was actually cmalloced. * tty.cc (create_tty_master): Tweak for new device class. (tty::common_init): Ditto. * winsup.h (stat_worker): Remove. (symlink_worker): Declare. * exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't worry about pending_signals since sig_dispatch_pending should always do the right thing now. (sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3. * pinfo.h: Move __SIG enum to sigproc.h. (PICOM_FIFO): New enum element. (_pinfo): Remove 'thread2signal' stuff throughout class. (_pinfo::commune_send): Make varargs. (_pinfo::sigtodo): Eliminate. (_pinfo::thread2signal): Ditto. * signal.cc (kill_worker): Eliminate call to setthread2signal. * sigproc.cc (local_sigtodo): Eliminate. (getlocal_sigtodo): Ditto. (sigelem): New class. (pending_signals): New class. (sigqueue): New variable, start of sigqueue linked list. (sigcatch_nonmain): Eliminate. (sigcatch_main): Eliminate. (sigcatch_nosync): Eliminate. (sigcomplete_nonmain): Eliminate. (pending_signals): Eliminate. (sig_clear): Call signal thread to clear pending signals, unless already in signal thread. (sigpending): Call signal thread to get pending signals. (sig_dispatch_pending): Eliminate use of pending_signals and just check sigqueue. (sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close signal pipe handle. (sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send signals. (getevent): Eliminate. (pending_signals::add): New function. (pending_signals::del): New function. (pending_signals::next): New function. (wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to communicate and maintain a linked list of signals. * sigproc.h: Move __SIG defines here. Add __SIGPENDING. (sig_dispatch_pending): Remove "C" specifier. (sig_handle): Accept a mask argument. * thread.cc: Remove signal handling considerations throughout. --- winsup/cygwin/dtable.cc | 245 ++++++++++++++++++++++++++++-------------------- 1 file changed, 142 insertions(+), 103 deletions(-) (limited to 'winsup/cygwin/dtable.cc') diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index fd7d579bb..9ca5d6a86 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -26,8 +26,8 @@ details. */ #include "cygerrno.h" #include "perprocess.h" #include "security.h" -#include "fhandler.h" #include "path.h" +#include "fhandler.h" #include "dtable.h" #include "cygheap.h" #include "ntdll.h" @@ -105,12 +105,12 @@ dtable::get_debugger_info () for (int i = 0; i < 3; i++) if (std[i][0]) { - path_conv pc; HANDLE h = GetStdHandle (std_consts[i]); - fhandler_base *fh = build_fhandler_from_name (i, std[i], NULL, pc); + fhandler_base *fh = build_fh_name (std[i]); if (!fh) continue; - if (!fh->open (&pc, (i ? O_WRONLY : O_RDONLY) | O_BINARY, 0777)) + fds[i] = fh; + if (!fh->open ((i ? O_WRONLY : O_RDONLY) | O_BINARY, 0777)) release (i); else CloseHandle (h); @@ -183,12 +183,8 @@ dtable::release (int fd) { if (!not_open (fd)) { - switch (fds[fd]->get_device ()) - { - case FH_SOCKET: - dec_need_fixup_before (); - break; - } + if (fds[fd]->need_fixup_before ()) + dec_need_fixup_before (); delete fds[fd]; fds[fd] = NULL; } @@ -200,23 +196,24 @@ cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin, { if (fd == -1) fd = cygheap->fdtab.find_unused_handle (); - path_conv pc; - fhandler_base *res = cygheap->fdtab.build_fhandler_from_name (fd, name, handle, - pc); - res->init (handle, myaccess, bin ?: pc.binmode ()); + fhandler_base *fh = build_fh_name (name); + cygheap->fdtab[fd] = fh; + fh->init (handle, myaccess, bin ?: fh->pc_binmode ()); return fd; } void dtable::init_std_file_from_handle (int fd, HANDLE handle) { - const char *name; + const char *name = NULL; CONSOLE_SCREEN_BUFFER_INFO buf; struct sockaddr sa; int sal = sizeof (sa); DCB dcb; unsigned bin = O_BINARY; + device dev; + dev.devn = 0; /* FIXME: device */ first_fd_for_open = 0; if (!not_open (fd)) @@ -224,36 +221,34 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle) SetLastError (0); DWORD ft = GetFileType (handle); - if (ft == FILE_TYPE_UNKNOWN && GetLastError () == ERROR_INVALID_HANDLE) - name = NULL; - else + if (ft != FILE_TYPE_UNKNOWN || GetLastError () != ERROR_INVALID_HANDLE) { /* See if we can consoleify it */ if (GetConsoleScreenBufferInfo (handle, &buf)) { if (ISSTATE (myself, PID_USETTY)) - name = "/dev/tty"; + dev.parse ("/dev/tty"); else - name = "/dev/conout"; + dev = *console_dev; } else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &buf)) { if (ISSTATE (myself, PID_USETTY)) - name = "/dev/tty"; + dev.parse ("/dev/tty"); else - name = "/dev/conin"; + dev = *console_dev; } else if (ft == FILE_TYPE_PIPE) { if (fd == 0) - name = "/dev/piper"; + dev = *piper_dev; else - name = "/dev/pipew"; + dev = *pipew_dev; } else if (wsock_started && getpeername ((SOCKET) handle, &sa, &sal) == 0) - name = "/dev/socket"; + dev = *tcp_dev; else if (GetCommState (handle, &dcb)) - name = "/dev/ttyS0"; // FIXME - determine correct device + dev.parse ("/dev/ttyS0"); else { name = handle_to_fn (handle, (char *) alloca (MAX_PATH + 100)); @@ -261,17 +256,29 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle) } } - if (!name) + if (!name && !dev) fds[fd] = NULL; else { - path_conv pc; - fhandler_base *fh = build_fhandler_from_name (fd, name, handle, pc); + fhandler_base *fh; + + if (dev) + fh = build_fh_dev (dev); + else + fh = build_fh_name (name); + + if (fh) + cygheap->fdtab[fd] = fh; + if (!bin) { bin = fh->get_default_fmode (O_RDWR); - if (!bin && name != unknown_file) - bin = pc.binmode (); + if (bin) + /* nothing */; + else if (dev) + bin = O_BINARY; + else if (name != unknown_file) + bin = fh->pc_binmode (); } fh->init (handle, GENERIC_READ | GENERIC_WRITE, bin); @@ -281,92 +288,110 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle) } fhandler_base * -dtable::build_fhandler_from_name (int fd, const char *name, HANDLE handle, - path_conv& pc, unsigned opt, suffix_info *si) +build_fh_name (const char *name, HANDLE h, unsigned opt, suffix_info *si) { - pc.check (name, opt | PC_NULLEMPTY | PC_FULL | PC_POSIX, si); + path_conv pc (name, opt | PC_NULLEMPTY | PC_FULL | PC_POSIX, si); if (pc.error) { set_errno (pc.error); return NULL; } - if (!pc.exists () && handle) - pc.fillin (handle); + if (!pc.exists () && h) + pc.fillin (h); - char *posix_path = pc.return_and_clear_normalized_path (); - fhandler_base *fh = build_fhandler (fd, pc.get_devn (), posix_path, - pc, pc.get_unitn ()); - if (pc.issocket ()) /* Only true for files pretending an AF_LOCAL socket. */ - { - fhandler_socket * fhs = (fhandler_socket *) fh; - fhs->set_addr_family (AF_LOCAL); - fhs->set_sun_path (posix_path); - } - return fh; + return build_fh_pc (pc); } fhandler_base * -dtable::build_fhandler (int fd, DWORD dev, const char *unix_name, - const char *win32_name, int unit) +build_fh_dev (const device& dev, const char *unix_name) { - return build_fhandler (fd, dev, cstrdup (unix_name), win32_name, unit); + path_conv pc (dev); + char *w32buf = const_cast (pc.get_win32 ()); + + __small_sprintf (w32buf, dev.fmt, dev.minor); + if (unix_name) + pc.set_normalized_path (unix_name); + else if (!dev.upper) + pc.set_normalized_path (dev.name); + else + { + pc.set_normalized_path (w32buf); + for (char *p = strchr (pc.normalized_path, '\\'); + p; + p = strchr (p + 1, '\\')) + *p = '/'; + } + return build_fh_pc (pc); } #define cnew(name) new ((void *) ccalloc (HEAP_FHANDLER, 1, sizeof (name))) name fhandler_base * -dtable::build_fhandler (int fd, DWORD dev, char *unix_name, - const char *win32_name, int unit) +build_fh_pc (path_conv& pc) { - fhandler_base *fh; + fhandler_base *fh = NULL; - dev &= FH_DEVMASK; - switch (dev) - { - case FH_TTYM: - fh = cnew (fhandler_tty_master) (unit); + if (pc.dev.upper) + switch (pc.dev.major) + { + case DEV_TTYS_MAJOR: + fh = cnew (fhandler_tty_slave) (); + break; + case DEV_TTYM_MAJOR: + fh = cnew (fhandler_tty_master) (); + break; + case DEV_CYGDRIVE_MAJOR: + fh = cnew (fhandler_cygdrive) (); break; + case DEV_FLOPPY_MAJOR: + case DEV_CDROM_MAJOR: + case DEV_SD_MAJOR: + case DEV_RAWDRIVE_MAJOR: + fh = cnew (fhandler_dev_floppy) (); + break; + case DEV_TAPE_MAJOR: + fh = cnew (fhandler_dev_tape) (); + break; + } + else + switch (pc.dev) + { case FH_CONSOLE: case FH_CONIN: case FH_CONOUT: fh = cnew (fhandler_console) (); break; + case FH_CYGDRIVE: + fh = cnew (fhandler_cygdrive) (); + break; case FH_PTYM: fh = cnew (fhandler_pty_master) (); break; - case FH_TTYS: - if (unit < 0) - fh = cnew (fhandler_tty_slave) (); - else - fh = cnew (fhandler_tty_slave) (unit); - break; case FH_WINDOWS: fh = cnew (fhandler_windows) (); break; case FH_SERIAL: - fh = cnew (fhandler_serial) (unit); + fh = cnew (fhandler_serial) (); + break; + case FH_FIFO: + fh = cnew (fhandler_fifo) (); break; case FH_PIPE: case FH_PIPER: case FH_PIPEW: - fh = cnew (fhandler_pipe) (dev); + fh = cnew (fhandler_pipe) (); break; - case FH_SOCKET: - if ((fh = cnew (fhandler_socket) (unit))) - inc_need_fixup_before (); + case FH_TCP: + case FH_UCP: + case FH_ICMP: + case FH_UNIX: + case FH_STREAM: + case FH_DGRAM: + fh = cnew (fhandler_socket) (); break; - case FH_DISK: + case FH_FS: fh = cnew (fhandler_disk_file) (); break; - case FH_CYGDRIVE: - fh = cnew (fhandler_cygdrive) (unit); - break; - case FH_FLOPPY: - fh = cnew (fhandler_dev_floppy) (unit); - break; - case FH_TAPE: - fh = cnew (fhandler_dev_tape) (unit); - break; case FH_NULL: fh = cnew (fhandler_dev_null) (); break; @@ -374,10 +399,12 @@ dtable::build_fhandler (int fd, DWORD dev, char *unix_name, fh = cnew (fhandler_dev_zero) (); break; case FH_RANDOM: - fh = cnew (fhandler_dev_random) (unit); + case FH_URANDOM: + fh = cnew (fhandler_dev_random) (); break; case FH_MEM: - fh = cnew (fhandler_dev_mem) (unit); + case FH_PORT: + fh = cnew (fhandler_dev_mem) (); break; case FH_CLIPBOARD: fh = cnew (fhandler_dev_clipboard) (); @@ -394,34 +421,35 @@ dtable::build_fhandler (int fd, DWORD dev, char *unix_name, case FH_PROCESS: fh = cnew (fhandler_process) (); break; - default: - system_printf ("internal error -- unknown device - %p", dev); - fh = NULL; - } - - if (unix_name) - { - char new_win32_name[strlen (unix_name) + 1]; - if (!win32_name) + case FH_TTY: { - char *p; - /* FIXME: ? Should we call win32_device_name here? - It seems like overkill, but... */ - win32_name = strcpy (new_win32_name, unix_name); - for (p = (char *) win32_name; (p = strchr (p, '/')); p++) - *p = '\\'; + device newdev = pc.dev; + newdev.tty_to_real_device (); + switch (newdev) + { + case FH_CONSOLE: + fh = cnew (fhandler_console) (); + break; + case FH_TTYS: + fh = cnew (fhandler_tty_slave) (); + break; + } } - if (!fh->set_name (unix_name, win32_name, fh->get_unit ())) - return NULL; } - debug_printf ("fd %d, fh %p", fd, fh); - return fd >= 0 ? (fds[fd] = fh) : fh; + + if (!fh) + fh = cnew (fhandler_nodevice) (); + + fh->set_name (pc); + + debug_printf ("fh %p", fh); + return fh; } fhandler_base * dtable::dup_worker (fhandler_base *oldfh) { - fhandler_base *newfh = build_fhandler (-1, oldfh->get_device ()); + fhandler_base *newfh = build_fh_pc (oldfh->pc); *newfh = *oldfh; newfh->set_io_handle (NULL); if (oldfh->dup (newfh)) @@ -500,6 +528,19 @@ done: return res; } +fhandler_fifo * +dtable::find_fifo (ATOM hill) +{ + SetResourceLock (LOCK_FD_LIST, READ_LOCK, "dup"); + for (unsigned i = 0; i < size; i++) + { + fhandler_base *fh = fds[i]; + if (fh && fh->isfifo () && ((fhandler_fifo *) fh)->get_atom () == hill) + return (fhandler_fifo *) fh; + } + return NULL; +} + select_record * dtable::select_read (int fd, select_record *s) { @@ -753,9 +794,7 @@ handle_to_fn (HANDLE h, char *posix_fn) || !QueryDosDevice (NULL, fnbuf, sizeof (fnbuf))) return strcpy (posix_fn, win32_fn); - char *p = strchr (win32_fn + DEVICE_PREFIX_LEN, '\\'); - if (!p) - p = strchr (win32_fn + DEVICE_PREFIX_LEN, '\0'); + char *p = strechr (win32_fn + DEVICE_PREFIX_LEN, '\\'); int n = p - win32_fn; int maxmatchlen = 0; -- cgit v1.2.3