4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-15 05:29:10 +08:00

Cygwin: AF_UNIX: code simplification

Add a 'src_pid' argument to dtable::dup_worker.

Use the latter in serialize/deserialize rather than repeating much of
what it does.

Don't duplicate the path_conv handle; it isn't needed.
This commit is contained in:
Ken Brown 2020-11-21 21:43:22 -05:00
parent 81ceb96528
commit d9bb7965f9
3 changed files with 11 additions and 38 deletions

View File

@ -675,7 +675,7 @@ out:
} }
fhandler_base * fhandler_base *
dtable::dup_worker (fhandler_base *oldfh, int flags) dtable::dup_worker (fhandler_base *oldfh, int flags, DWORD src_pid)
{ {
/* Don't call set_name in build_fh_pc. It will be called in /* Don't call set_name in build_fh_pc. It will be called in
fhandler_base::operator= below. Calling it twice will result fhandler_base::operator= below. Calling it twice will result
@ -689,7 +689,7 @@ dtable::dup_worker (fhandler_base *oldfh, int flags)
newfh->set_handle (NULL); newfh->set_handle (NULL);
newfh->pc.reset_conv_handle (); newfh->pc.reset_conv_handle ();
if (oldfh->dup (newfh, flags)) if (oldfh->dup (newfh, flags, src_pid))
{ {
delete newfh; delete newfh;
newfh = NULL; newfh = NULL;

View File

@ -45,7 +45,7 @@ public:
int vfork_child_dup (); int vfork_child_dup ();
void vfork_parent_restore (); void vfork_parent_restore ();
void vfork_child_fixup (); void vfork_child_fixup ();
fhandler_base *dup_worker (fhandler_base *oldfh, int flags); fhandler_base *dup_worker (fhandler_base *oldfh, int flags, DWORD src_pid = 0);
int extend (size_t, size_t) __reg3; int extend (size_t, size_t) __reg3;
void fixup_after_fork (HANDLE); void fixup_after_fork (HANDLE);
void fixup_close (size_t, fhandler_base *); void fixup_close (size_t, fhandler_base *);

View File

@ -1976,7 +1976,7 @@ fhandler_socket_unix::serialize (int fd)
{ {
fh_ser *fhs = NULL; fh_ser *fhs = NULL;
int64_t id; int64_t id;
fhandler_base *oldfh, *newfh = NULL; fhandler_base *newfh = NULL;
cygheap_fdget cfd (fd); cygheap_fdget cfd (fd);
if (cfd < 0) if (cfd < 0)
@ -1984,23 +1984,15 @@ fhandler_socket_unix::serialize (int fd)
set_errno (EBADF); set_errno (EBADF);
goto out; goto out;
} }
oldfh = cfd;
/* For the moment we support disk files only. */ /* For the moment we support disk files only. */
if (oldfh->get_device () != FH_FS) if (cfd->get_device () != FH_FS)
{ {
set_errno (EOPNOTSUPP); set_errno (EOPNOTSUPP);
goto out; goto out;
} }
newfh = oldfh->clone (); newfh = cygheap->fdtab.dup_worker (cfd, 0);
/* newfh needs handles that remain valid if oldfh is closed. */ if (!newfh)
/* FIXME: When we move away from disk files, we might need to pay goto out;
attention to archetype, usecount, refcnt,.... See
dtable::dup_worker. */
if (oldfh->dup (newfh, 0) < 0)
{
delete newfh;
goto out;
}
/* Free allocated memory in clone. */ /* Free allocated memory in clone. */
newfh->pc.free_strings (); newfh->pc.free_strings ();
newfh->dev ().free_strings (); newfh->dev ().free_strings ();
@ -2047,30 +2039,11 @@ fhandler_socket_unix::deserialize (void *bufp)
cygheap_fdnew cfd; cygheap_fdnew cfd;
if (cfd < 0) if (cfd < 0)
return -1; return -1;
newfh = oldfh->clone (); newfh = cygheap->fdtab.dup_worker (oldfh, 0, winpid);
int ret = oldfh->dup (newfh, 0, winpid);
if (!send_scm_fd_ack (fhs->uniq_id)) if (!send_scm_fd_ack (fhs->uniq_id))
debug_printf ("can't send ack"); debug_printf ("can't send ack");
if (ret < 0) if (!newfh)
{ return -1;
debug_printf ("can't duplicate handles");
delete newfh;
return -1;
}
newfh->pc.close_conv_handle ();
if (oldfh->pc.handle ())
{
HANDLE nh;
HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE, false, winpid);
if (!proc)
debug_printf ("can't open process %d", winpid);
else if (!DuplicateHandle (proc, oldfh->pc.handle (),
GetCurrentProcess (), &nh, 0,
TRUE, DUPLICATE_SAME_ACCESS))
debug_printf ("can't duplicate path_conv handle");
else
newfh->pc.set_conv_handle (nh);
}
newfh->set_name_from_handle (); newfh->set_name_from_handle ();
cfd = newfh; cfd = newfh;
return cfd; return cfd;