2019-01-07 03:18:14 +08:00
|
|
|
/* fhandler_process_fd.cc: fhandler for /proc/<pid>/fd/<desc> operations
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "fhandler_virtual.h"
|
|
|
|
#include "pinfo.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
|
|
|
|
fhandler_base *
|
2019-01-07 05:39:45 +08:00
|
|
|
fhandler_process_fd::fetch_fh (HANDLE &out_hdl, uint32_t flags)
|
2019-01-07 03:18:14 +08:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
char *e;
|
|
|
|
int fd;
|
|
|
|
HANDLE proc;
|
|
|
|
HANDLE hdl = NULL;
|
|
|
|
path_conv pc;
|
|
|
|
|
|
|
|
path = get_name () + proc_len + 1;
|
|
|
|
pid = strtoul (path, &e, 10);
|
|
|
|
path = e + 4;
|
|
|
|
fd = strtoul (path, &e, 10);
|
|
|
|
|
|
|
|
out_hdl = NULL;
|
|
|
|
if (pid == myself->pid)
|
|
|
|
{
|
|
|
|
cygheap_fdget cfd (fd, true);
|
|
|
|
if (cfd < 0)
|
|
|
|
return NULL;
|
2019-01-07 05:39:45 +08:00
|
|
|
if ((flags & FFH_LINKAT)
|
|
|
|
&& (cfd->get_flags () & (O_TMPFILE | O_EXCL)) == (O_TMPFILE | O_EXCL))
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-07 03:18:14 +08:00
|
|
|
proc = GetCurrentProcess ();
|
|
|
|
pc << cfd->pc;
|
|
|
|
hdl = cfd->get_handle ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pinfo p (pid);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
proc = OpenProcess (PROCESS_DUP_HANDLE, false, p->dwProcessId);
|
|
|
|
if (!proc)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t size;
|
2019-01-07 05:39:45 +08:00
|
|
|
void *buf = p->file_pathconv (fd, FFH_LINKAT, size);
|
2019-01-07 03:18:14 +08:00
|
|
|
if (size == 0)
|
|
|
|
{
|
2019-01-07 05:39:45 +08:00
|
|
|
set_errno (ENOENT);
|
2019-01-07 03:18:14 +08:00
|
|
|
CloseHandle (proc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
hdl = pc.deserialize (buf);
|
|
|
|
}
|
|
|
|
BOOL ret = DuplicateHandle (proc, hdl, GetCurrentProcess (), &hdl,
|
2019-01-09 04:38:04 +08:00
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS);
|
2019-01-07 03:18:14 +08:00
|
|
|
if (proc != GetCurrentProcess ())
|
|
|
|
CloseHandle (proc);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
CloseHandle (hdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fhandler_base *fh = build_fh_pc (pc);
|
|
|
|
if (!fh)
|
|
|
|
{
|
|
|
|
CloseHandle (hdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
out_hdl = hdl;
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
|
|
|
fhandler_process_fd::fd_reopen (int flags)
|
|
|
|
{
|
|
|
|
fhandler_base *fh;
|
|
|
|
HANDLE hdl;
|
|
|
|
|
2019-01-07 05:39:45 +08:00
|
|
|
fh = fetch_fh (hdl, 0);
|
2019-01-07 03:18:14 +08:00
|
|
|
if (!fh)
|
|
|
|
return NULL;
|
|
|
|
fh->set_io_handle (hdl);
|
|
|
|
int ret = fh->open_with_arch (flags, 0);
|
|
|
|
CloseHandle (hdl);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
delete fh;
|
|
|
|
fh = NULL;
|
|
|
|
}
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __reg2
|
|
|
|
fhandler_process_fd::fstat (struct stat *statbuf)
|
|
|
|
{
|
|
|
|
if (!pc.follow_fd_symlink ())
|
|
|
|
return fhandler_process::fstat (statbuf);
|
|
|
|
|
|
|
|
fhandler_base *fh;
|
|
|
|
HANDLE hdl;
|
|
|
|
|
2019-01-07 05:39:45 +08:00
|
|
|
fh = fetch_fh (hdl, 0);
|
2019-01-07 03:18:14 +08:00
|
|
|
if (!fh)
|
|
|
|
return -1;
|
|
|
|
fh->set_io_handle (hdl);
|
|
|
|
int ret = fh->fstat (statbuf);
|
|
|
|
CloseHandle (hdl);
|
|
|
|
delete fh;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_process_fd::link (const char *newpath)
|
|
|
|
{
|
|
|
|
fhandler_base *fh;
|
|
|
|
HANDLE hdl;
|
|
|
|
|
2019-01-07 05:39:45 +08:00
|
|
|
fh = fetch_fh (hdl, FFH_LINKAT);
|
2019-01-07 03:18:14 +08:00
|
|
|
if (!fh)
|
|
|
|
return -1;
|
|
|
|
fh->set_io_handle (hdl);
|
|
|
|
int ret = fh->link (newpath);
|
|
|
|
CloseHandle (hdl);
|
|
|
|
delete fh;
|
|
|
|
return ret;
|
|
|
|
}
|