* cygheap.h (class cygheap_fdenum): New class to enumerate used

fhandlers.
	* dtable.h (class dtable): Add cygheap_fdenum as friend class.
	* fhandler.h (fhandler_base::get_proc_fd_name): New virtual method
	to return a name for /proc/<pid>/fd.
	(fhandler_socket::get_proc_fd_name): Ditto.
	(fhandler_pipe::get_proc_fd_name): Ditto.
	(fhandler_virtual::opendir): Make virtual method.
	(fhandler_process::opendir): New method.
	* fhandler.cc (fhandler_base::get_proc_fd_name): New method.
	* fhandler_process.cc: Include ctype.h.
	(PROCESS_FD): Define.
	(process_listing): Add "fd".
	(fhandler_process::exists): Fix comment.  Return 1 in case of "fd"
	directory. Handle files below "fd".
	(fhandler_process::fstat): Drop "self" handling.  Set correct link
	count for directories.
	(fhandler_process::opendir): New method to handle "fd" directory.
	(fhandler_process::readdir): Add "fd" handling.
	(fhandler_process::open): Drop "self" handling.
	(fhandler_process::fill_filebuf): Ditto.  Add "fd" handling.  Fix
	"maps" output string.
	* fhandler_registry.cc (fhandler_registry::fstat): Set correct link
	count for directories.
	* fhandler_socket.cc (fhandler_socket::get_proc_fd_name): New method.
	* path.cc (symlink_info::set): Fix thinko.
	* pinfo.cc (_pinfo::commune_recv): Rename pathbuf to path throughout.
	Drop local path variable in PICOM_FIFO case.  Fix debug output.
	Close handles as early as possible. Add PICOM_FDS and PICOM_FD
	handling.
	(_pinfo::commune_send): Add PICOM_FDS and PICOM_FD handling.
	(_pinfo::fd): New method.
	(_pinfo::fds): New method.
	* pinfo.h (enum picom): Add PICOM_FDS and PICOM_FD.
	(_pinfo::fd): Declare.
	(_pinfo::fds): Declare.
	* pipe.cc (fhandler_pipe::get_proc_fd_name): New method.
This commit is contained in:
Corinna Vinschen
2005-01-31 10:28:55 +00:00
parent d0ae095b8c
commit 4f27e288c5
12 changed files with 309 additions and 60 deletions

View File

@@ -380,7 +380,7 @@ extern char **__argv;
void
_pinfo::commune_recv ()
{
char pathbuf[CYG_MAX_PATH];
char path[CYG_MAX_PATH + 1];
DWORD nr;
DWORD code;
HANDLE hp;
@@ -460,33 +460,78 @@ _pinfo::commune_recv ()
}
case PICOM_CWD:
{
unsigned int n = strlen (cygheap->cwd.get (pathbuf, 1, 1, CYG_MAX_PATH)) + 1;
CloseHandle (__fromthem); __fromthem = NULL;
CloseHandle (hp);
unsigned int n = strlen (cygheap->cwd.get (path, 1, 1,
CYG_MAX_PATH)) + 1;
if (!WriteFile (__tothem, &n, sizeof n, &nr, NULL))
sigproc_printf ("WriteFile sizeof argv failed, %E");
else if (!WriteFile (__tothem, pathbuf, n, &nr, NULL))
sigproc_printf ("WriteFile sizeof argv failed, %E");
sigproc_printf ("WriteFile sizeof cwd failed, %E");
else if (!WriteFile (__tothem, path, n, &nr, NULL))
sigproc_printf ("WriteFile cwd failed, %E");
break;
}
case PICOM_ROOT:
{
unsigned int n;
if (cygheap->root.exists ())
n = strlen (strcpy (pathbuf, cygheap->root.posix_path ())) + 1;
else
n = strlen (strcpy (pathbuf, "/")) + 1;
CloseHandle (__fromthem); __fromthem = NULL;
CloseHandle (hp);
unsigned int n;
if (cygheap->root.exists ())
n = strlen (strcpy (path, cygheap->root.posix_path ())) + 1;
else
n = strlen (strcpy (path, "/")) + 1;
if (!WriteFile (__tothem, &n, sizeof n, &nr, NULL))
sigproc_printf ("WriteFile sizeof argv failed, %E");
else if (!WriteFile (__tothem, pathbuf, n, &nr, NULL))
sigproc_printf ("WriteFile sizeof argv failed, %E");
sigproc_printf ("WriteFile sizeof root failed, %E");
else if (!WriteFile (__tothem, path, n, &nr, NULL))
sigproc_printf ("WriteFile root failed, %E");
break;
}
case PICOM_FDS:
{
CloseHandle (__fromthem); __fromthem = NULL;
CloseHandle (hp);
unsigned int n = 0;
int fd;
cygheap_fdenum cfd;
while ((fd = cfd.next ()) >= 0)
n += sizeof (int);
cfd.rewind ();
if (!WriteFile (__tothem, &n, sizeof n, &nr, NULL))
sigproc_printf ("WriteFile sizeof fds failed, %E");
else
while ((fd = cfd.next ()) >= 0)
if (!WriteFile (__tothem, &fd, sizeof fd, &nr, NULL))
{
sigproc_printf ("WriteFile fd %d failed, %E", fd);
break;
}
break;
}
case PICOM_FD:
{
int fd;
if (!ReadFile (__fromthem, &fd, sizeof fd, &nr, NULL)
|| nr != sizeof fd)
{
sigproc_printf ("ReadFile fd failed, %E");
CloseHandle (hp);
goto out;
}
CloseHandle (__fromthem); __fromthem = NULL;
CloseHandle (hp);
unsigned int n;
cygheap_fdget cfd (fd);
if (cfd < 0)
n = strlen (strcpy (path, "<disconnected>")) + 1;
else
n = strlen (cfd->get_proc_fd_name (path)) + 1;
if (!WriteFile (__tothem, &n, sizeof n, &nr, NULL))
sigproc_printf ("WriteFile sizeof fd failed, %E");
else if (!WriteFile (__tothem, path, n, &nr, NULL))
sigproc_printf ("WriteFile fd failed, %E");
break;
}
case PICOM_FIFO:
{
char path[CYG_MAX_PATH + 1];
unsigned len;
if (!ReadFile (__fromthem, &len, sizeof len, &nr, NULL)
|| nr != sizeof len)
@@ -614,9 +659,21 @@ _pinfo::commune_send (DWORD code, ...)
size_t n;
switch (code)
{
case PICOM_FD:
{
int fd = va_arg (args, int);
if (!WriteFile (tothem, &fd, sizeof fd, &nr, NULL)
|| nr != sizeof fd)
{
__seterrno ();
goto err;
}
}
/*FALLTHRU*/
case PICOM_CMDLINE:
case PICOM_CWD:
case PICOM_ROOT:
case PICOM_FDS:
if (!ReadFile (fromthem, &n, sizeof n, &nr, NULL) || nr != sizeof n)
{
__seterrno ();
@@ -683,6 +740,58 @@ out:
return res;
}
char *
_pinfo::fd (int fd, size_t &n)
{
char *s;
if (!this || !pid)
return NULL;
if (pid != myself->pid)
{
commune_result cr = commune_send (PICOM_FD, fd);
s = cr.s;
n = cr.n;
}
else
{
cygheap_fdget cfd (fd);
if (cfd < 0)
s = strdup ("<disconnected>");
else
s = cfd->get_proc_fd_name ((char *) malloc (CYG_MAX_PATH + 1));
n = strlen (s) + 1;
}
return s;
}
char *
_pinfo::fds (size_t &n)
{
char *s;
if (!this || !pid)
return NULL;
if (pid != myself->pid)
{
commune_result cr = commune_send (PICOM_FDS);
s = cr.s;
n = cr.n;
}
else
{
n = 0;
int fd;
cygheap_fdenum cfd;
while ((fd = cfd.next ()) >= 0)
n += sizeof (int);
cfd.rewind ();
s = (char *) malloc (n);
int *p = (int *) s;
while ((fd = cfd.next ()) >= 0 && (char *) p - s < (int) n)
*p++ = fd;
}
return s;
}
char *
_pinfo::root (size_t& n)
{