Welcome to mirror list, hosted at ThFree Co, Russian Federation.

process_fd.cc « fhandler « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d81495103e58e7bc2bc4659db92276e5fec6a286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* 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"
#include "tls_pbuf.h"

fhandler_base *
fhandler_process_fd::fetch_fh (HANDLE &out_hdl, uint32_t flags)
{
  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;
      if ((flags & FFH_LINKAT)
	 && (cfd->get_flags () & (O_TMPFILE | O_EXCL)) == (O_TMPFILE | O_EXCL))
	{
	  set_errno (ENOENT);
	  return NULL;
	}
      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;
      void *buf = p->file_pathconv (fd, flags, size);
      if (size == 0)
	{
	  set_errno (ENOENT);
	  CloseHandle (proc);
	  return NULL;
	}
      hdl = pc.deserialize (buf);
    }
  if (hdl == NULL)
    {
      if (proc != GetCurrentProcess ())
	CloseHandle (proc);
      set_errno (EACCES);
      return NULL;
    }
  BOOL ret = DuplicateHandle (proc, hdl, GetCurrentProcess (), &hdl,
			      0, FALSE, DUPLICATE_SAME_ACCESS);
  if (proc != GetCurrentProcess ())
    CloseHandle (proc);
  if (!ret)
    {
      __seterrno ();
      CloseHandle (hdl);
      return NULL;
    }
  /* 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);
    }
  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, mode_t mode)
{
  fhandler_base *fh;
  HANDLE hdl;

  fh = fetch_fh (hdl, 0);
  if (!fh)
    return NULL;
  fh->set_handle (hdl);
  int ret = fh->open_with_arch (flags, mode);
  CloseHandle (hdl);
  if (!ret)
    {
      delete fh;
      fh = NULL;
    }
  return fh;
}

int
fhandler_process_fd::fstat (struct stat *statbuf)
{
  if (!pc.follow_fd_symlink ())
    return fhandler_process::fstat (statbuf);

  fhandler_base *fh;
  HANDLE hdl;

  fh = fetch_fh (hdl, 0);
  if (!fh)
    return -1;
  fh->set_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;

  fh = fetch_fh (hdl, FFH_LINKAT);
  if (!fh)
    return -1;
  fh->set_handle (hdl);
  int ret = fh->link (newpath);
  CloseHandle (hdl);
  delete fh;
  return ret;
}