📄 dtable.cc
字号:
It seems like overkill, but... */ win32_name = strcpy (new_win32_name, unix_name); for (p = (char *) win32_name; (p = strchr (p, '/')); p++) *p = '\\'; } fh->set_name (unix_name, win32_name, fh->get_unit ()); } debug_printf ("fd %d, fh %p", fd, fh); return fd >= 0 ? (fds[fd] = fh) : fh;}fhandler_base *dtable::dup_worker (fhandler_base *oldfh){ fhandler_base *newfh = build_fhandler (-1, oldfh->get_device ()); *newfh = *oldfh; newfh->set_io_handle (NULL); if (oldfh->dup (newfh)) { cfree (newfh); newfh = NULL; return NULL; } newfh->set_close_on_exec_flag (0); MALLOC_CHECK; debug_printf ("duped '%s' old %p, new %p", oldfh->get_name (), oldfh->get_io_handle (), newfh->get_io_handle ()); return newfh;}intdtable::dup2 (int oldfd, int newfd){ int res = -1; fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning MALLOC_CHECK; debug_printf ("dup2 (%d, %d)", oldfd, newfd); SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup"); if (not_open (oldfd)) { syscall_printf ("fd %d not open", oldfd); set_errno (EBADF); goto done; } if (newfd < 0) { syscall_printf ("new fd out of bounds: %d", newfd); set_errno (EBADF); goto done; } if (newfd == oldfd) { res = 0; goto done; } if ((newfh = dup_worker (fds[oldfd])) == NULL) { res = -1; goto done; } debug_printf ("newfh->io_handle %p, oldfh->io_handle %p", newfh->get_io_handle (), fds[oldfd]->get_io_handle ()); if (!not_open (newfd)) close (newfd); else if ((size_t) newfd < size) /* nothing to do */; else if (find_unused_handle (newfd) < 0) { newfh->close (); res = -1; goto done; } fds[newfd] = newfh; if ((res = newfd) <= 2) set_std_handle (res);done: MALLOC_CHECK; ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup"); syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd); return res;}select_record *dtable::select_read (int fd, select_record *s){ if (not_open (fd)) { set_errno (EBADF); return NULL; } fhandler_base *fh = fds[fd]; s = fh->select_read (s); s->fd = fd; s->fh = fh; s->saw_error = 0; debug_printf ("%s fd %d", fh->get_name (), fd); return s;}select_record *dtable::select_write (int fd, select_record *s){ if (not_open (fd)) { set_errno (EBADF); return NULL; } fhandler_base *fh = fds[fd]; s = fh->select_write (s); s->fd = fd; s->fh = fh; s->saw_error = 0; debug_printf ("%s fd %d", fh->get_name (), fd); return s;}select_record *dtable::select_except (int fd, select_record *s){ if (not_open (fd)) { set_errno (EBADF); return NULL; } fhandler_base *fh = fds[fd]; s = fh->select_except (s); s->fd = fd; s->fh = fh; s->saw_error = 0; debug_printf ("%s fd %d", fh->get_name (), fd); return s;}/* Function to walk the fd table after an exec and perform per-fhandler type fixups. */voiddtable::fixup_before_fork (DWORD target_proc_id){ SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork"); fhandler_base *fh; for (size_t i = 0; i < size; i++) if ((fh = fds[i]) != NULL) { debug_printf ("fd %d (%s)", i, fh->get_name ()); fh->fixup_before_fork_exec (target_proc_id); } ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork");}voiddtable::fixup_before_exec (DWORD target_proc_id){ SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec"); fhandler_base *fh; for (size_t i = 0; i < size; i++) if ((fh = fds[i]) != NULL && !fh->get_close_on_exec ()) { debug_printf ("fd %d (%s)", i, fh->get_name ()); fh->fixup_before_fork_exec (target_proc_id); } ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");}voiddtable::set_file_pointers_for_exec (){ SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "set_file_pointers_for_exec"); fhandler_base *fh; for (size_t i = 0; i < size; i++) if ((fh = fds[i]) != NULL && fh->get_flags () & O_APPEND) SetFilePointer (fh->get_handle (), 0, 0, FILE_END); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");}voiddtable::fixup_after_exec (HANDLE parent){ first_fd_for_open = 0; fhandler_base *fh; for (size_t i = 0; i < size; i++) if ((fh = fds[i]) != NULL) { fh->clear_readahead (); if (fh->get_close_on_exec ()) release (i); else { fh->fixup_after_exec (parent); if (i == 0) SetStdHandle (std_consts[i], fh->get_io_handle ()); else if (i <= 2) SetStdHandle (std_consts[i], fh->get_output_handle ()); } }}voiddtable::fixup_after_fork (HANDLE parent){ fhandler_base *fh; for (size_t i = 0; i < size; i++) if ((fh = fds[i]) != NULL) { if (fh->get_close_on_exec () || fh->get_need_fork_fixup ()) { debug_printf ("fd %d (%s)", i, fh->get_name ()); fh->fixup_after_fork (parent); } if (i == 0) SetStdHandle (std_consts[i], fh->get_io_handle ()); else if (i <= 2) SetStdHandle (std_consts[i], fh->get_output_handle ()); }}intdtable::vfork_child_dup (){ fhandler_base **newtable; SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup"); newtable = (fhandler_base **) ccalloc (HEAP_ARGV, size, sizeof (fds[0])); int res = 1; /* Remove impersonation */ if (cygheap->user.issetuid ()) RevertToSelf (); for (size_t i = 0; i < size; i++) if (not_open (i)) continue; else if ((newtable[i] = dup_worker (fds[i])) != NULL) newtable[i]->set_close_on_exec (fds[i]->get_close_on_exec ()); else { res = 0; set_errno (EBADF); goto out; } fds_on_hold = fds; fds = newtable;out: /* Restore impersonation */ if (cygheap->user.issetuid ()) ImpersonateLoggedOnUser (cygheap->user.token); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup"); return 1;}voiddtable::vfork_parent_restore (){ SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore"); close_all_files (); fhandler_base **deleteme = fds; assert (fds_on_hold != NULL); fds = fds_on_hold; fds_on_hold = NULL; cfree (deleteme); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore"); return;}voiddtable::vfork_child_fixup (){ if (!fds_on_hold) return; debug_printf ("here"); fhandler_base **saveme = fds; fds = fds_on_hold; fhandler_base *fh; for (int i = 0; i < (int) cygheap->fdtab.size; i++) if ((fh = cygheap->fdtab[i]) != NULL) { fh->clear_readahead (); if (fh->get_close_on_exec ()) release (i); else { fh->close (); cygheap->fdtab.release (i); } } fds = saveme; cfree (fds_on_hold); fds_on_hold = NULL; return;}#define DEVICE_PREFIX "\\device\\"#define DEVICE_PREFIX_LEN sizeof (DEVICE_PREFIX) - 1#define REMOTE "\\Device\\LanmanRedirector\\"#define REMOTE_LEN sizeof (REMOTE) - 1static const char *handle_to_fn (HANDLE h, char *posix_fn){ OBJECT_NAME_INFORMATION *ntfn; char fnbuf[32768]; memset (fnbuf, 0, sizeof (fnbuf)); ntfn = (OBJECT_NAME_INFORMATION *) fnbuf; ntfn->Name.MaximumLength = sizeof (fnbuf) - sizeof (*ntfn); ntfn->Name.Buffer = (WCHAR *) (ntfn + 1); DWORD res = NtQueryObject (h, ObjectNameInformation, ntfn, sizeof (fnbuf), NULL); if (res) { strcpy (posix_fn, unknown_file); debug_printf ("NtQueryObject failed"); return unknown_file; } // NT seems to do this on an unopened file if (!ntfn->Name.Buffer) { debug_printf ("nt->Name.Buffer == NULL"); return NULL; } ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = 0; char win32_fn[MAX_PATH + 100]; sys_wcstombs (win32_fn, ntfn->Name.Buffer, ntfn->Name.Length); debug_printf ("nt name '%s'", win32_fn); if (!strncasematch (win32_fn, DEVICE_PREFIX, DEVICE_PREFIX_LEN) || !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'); int n = p - win32_fn; int maxmatchlen = 0; char *maxmatchdos = NULL; for (char *s = fnbuf; *s; s = strchr (s, '\0') + 1) { char device[MAX_PATH + 10]; device[MAX_PATH + 9] = '\0'; if (strchr (s, ':') == NULL) continue; if (!QueryDosDevice (s, device, sizeof (device) - 1)) continue; char *q = strrchr (device, ';'); if (q) { char *r = strchr (q, '\\'); if (r) strcpy (q, r + 1); } int devlen = strlen (device); if (device[devlen - 1] == '\\') device[--devlen] = '\0'; if (devlen < maxmatchlen) continue; if (!strncasematch (device, win32_fn, devlen) || (win32_fn[devlen] != '\0' && win32_fn[devlen] != '\\')) continue; maxmatchlen = devlen; maxmatchdos = s; debug_printf ("current match '%s'", device); } char *w32 = win32_fn; if (maxmatchlen) { n = strlen (maxmatchdos); if (maxmatchdos[n - 1] == '\\') n--; w32 += maxmatchlen - n; memcpy (w32, maxmatchdos, n); w32[n] = '\\'; } else if (strncasematch (w32, REMOTE, REMOTE_LEN)) { w32 += REMOTE_LEN - 2; *w32 = '\\'; debug_printf ("remote drive"); } debug_printf ("derived path '%s'", w32); cygwin_conv_to_full_posix_path (w32, posix_fn); return posix_fn;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -