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"
|
2019-02-05 22:32:08 +08:00
|
|
|
#include "tls_pbuf.h"
|
2019-01-07 03:18:14 +08:00
|
|
|
|
|
|
|
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-02-05 22:20:50 +08:00
|
|
|
void *buf = p->file_pathconv (fd, flags, 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);
|
|
|
|
}
|
2019-01-14 06:09:48 +08:00
|
|
|
if (hdl == NULL)
|
|
|
|
{
|
|
|
|
if (proc != GetCurrentProcess ())
|
|
|
|
CloseHandle (proc);
|
|
|
|
set_errno (EACCES);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-07 03:18:14 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-02-05 22:32:08 +08:00
|
|
|
/* relative path? This happens for special types like pipes and sockets. */
|
|
|
|
if (*pc.get_posix () != '/')
|
|
|
|
{
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *fullpath = tp.c_get ();
|
|
|
|
|
|
|
|
stpcpy (stpncpy (fullpath, get_name (), path - get_name ()),
|
|
|
|
pc.get_posix ());
|
|
|
|
pc.set_posix (fullpath);
|
|
|
|
}
|
2019-01-07 03:18:14 +08:00
|
|
|
fhandler_base *fh = build_fh_pc (pc);
|
|
|
|
if (!fh)
|
|
|
|
{
|
|
|
|
CloseHandle (hdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
out_hdl = hdl;
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2019-02-05 22:20:13 +08:00
|
|
|
fhandler_process_fd::fd_reopen (int flags, mode_t mode)
|
2019-01-07 03:18:14 +08:00
|
|
|
{
|
|
|
|
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;
|
2019-03-30 15:12:02 +08:00
|
|
|
fh->set_handle (hdl);
|
2019-02-05 22:20:13 +08:00
|
|
|
int ret = fh->open_with_arch (flags, mode);
|
2019-01-07 03:18:14 +08:00
|
|
|
CloseHandle (hdl);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
delete fh;
|
|
|
|
fh = NULL;
|
|
|
|
}
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:52:52 +08:00
|
|
|
int
|
2019-01-07 03:18:14 +08:00
|
|
|
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;
|
2019-03-30 15:12:02 +08:00
|
|
|
fh->set_handle (hdl);
|
2019-01-07 03:18:14 +08:00
|
|
|
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;
|
2019-03-30 15:12:02 +08:00
|
|
|
fh->set_handle (hdl);
|
2019-01-07 03:18:14 +08:00
|
|
|
int ret = fh->link (newpath);
|
|
|
|
CloseHandle (hdl);
|
|
|
|
delete fh;
|
|
|
|
return ret;
|
|
|
|
}
|