]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygserver/transport_pipes.cc
* cygserver.cc (print_usage): Align output to utilities in utils
[newlib-cygwin.git] / winsup / cygserver / transport_pipes.cc
1 /* transport_pipes.cc
2
3 Copyright 2001, 2002, 2003, 2004, 2009 Red Hat Inc.
4
5 Written by Robert Collins <rbtcollins@hotmail.com>
6
7 This file is part of Cygwin.
8
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 details. */
12
13 /* to allow this to link into cygwin and the .dll, a little magic is needed. */
14 #ifdef __OUTSIDE_CYGWIN__
15 #include "woutsup.h"
16 #include <ntdef.h>
17 #else
18 #include "winsup.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <netdb.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <wchar.h>
28 #include <sys/cygwin.h>
29
30 #include "cygerrno.h"
31 #include "transport.h"
32 #include "transport_pipes.h"
33
34 #ifndef __INSIDE_CYGWIN__
35 #include "cygserver.h"
36 #include "cygserver_ipc.h"
37 #else
38 #include "security.h"
39 #endif
40
41 #ifdef __INSIDE_CYGWIN__
42 #define SET_ERRNO(err) set_errno (err)
43 #else
44 #define SET_ERRNO(err) errno = (err)
45 #endif
46
47 enum
48 {
49 MAX_WAIT_NAMED_PIPE_RETRY = 64,
50 WAIT_NAMED_PIPE_TIMEOUT = 10 // milliseconds
51 };
52
53 #ifndef __INSIDE_CYGWIN__
54
55 static pthread_once_t pipe_instance_lock_once = PTHREAD_ONCE_INIT;
56 static CRITICAL_SECTION pipe_instance_lock;
57 static long pipe_instance = 0;
58
59 static void
60 initialise_pipe_instance_lock ()
61 {
62 assert (pipe_instance == 0);
63 InitializeCriticalSection (&pipe_instance_lock);
64 }
65
66 #endif /* !__INSIDE_CYGWIN__ */
67
68 #ifndef __INSIDE_CYGWIN__
69
70 transport_layer_pipes::transport_layer_pipes (const HANDLE hPipe)
71 : _hPipe (hPipe),
72 _is_accepted_endpoint (true),
73 _is_listening_endpoint (false)
74 {
75 assert (_hPipe);
76 assert (_hPipe != INVALID_HANDLE_VALUE);
77 _pipe_name[0] = L'\0';
78 }
79
80 #endif /* !__INSIDE_CYGWIN__ */
81
82 transport_layer_pipes::transport_layer_pipes ()
83 : _hPipe (NULL),
84 _is_accepted_endpoint (false),
85 _is_listening_endpoint (false)
86 {
87 #ifdef __INSIDE_CYGWIN__
88 extern WCHAR installation_key_buf[18];
89 wcpcpy (wcpcpy (wcpcpy (_pipe_name, PIPE_NAME_PREFIX), installation_key_buf),
90 PIPE_NAME_SUFFIX);
91 #else
92 wchar_t cyg_instkey[18];
93
94 wchar_t *p = wcpcpy (_pipe_name, PIPE_NAME_PREFIX);
95 if (!cygwin_internal (CW_GET_INSTKEY, cyg_instkey))
96 wcpcpy (wcpcpy (p, cyg_instkey), PIPE_NAME_SUFFIX);
97 #endif
98 }
99
100 transport_layer_pipes::~transport_layer_pipes ()
101 {
102 close ();
103 }
104
105 #ifndef __INSIDE_CYGWIN__
106
107 int
108 transport_layer_pipes::listen ()
109 {
110 assert (!_hPipe);
111 assert (!_is_accepted_endpoint);
112 assert (!_is_listening_endpoint);
113
114 _is_listening_endpoint = true;
115
116 /* no-op */
117 return 0;
118 }
119
120 class transport_layer_pipes *
121 transport_layer_pipes::accept (bool *const recoverable)
122 {
123 assert (!_hPipe);
124 assert (!_is_accepted_endpoint);
125 assert (_is_listening_endpoint);
126
127 pthread_once (&pipe_instance_lock_once, &initialise_pipe_instance_lock);
128
129 EnterCriticalSection (&pipe_instance_lock);
130
131 // Read: http://www.securityinternals.com/research/papers/namedpipe.php
132 // See also the Microsoft security bulletins MS00-053 and MS01-031.
133
134 // FIXME: Remove FILE_CREATE_PIPE_INSTANCE.
135
136 const bool first_instance = (pipe_instance == 0);
137
138 debug ("Try to create named pipe: %ls", _pipe_name);
139
140 const HANDLE accept_pipe =
141 CreateNamedPipeW (_pipe_name,
142 (PIPE_ACCESS_DUPLEX
143 | (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0)),
144 (PIPE_TYPE_BYTE | PIPE_WAIT),
145 PIPE_UNLIMITED_INSTANCES,
146 0, 0, 1000,
147 &sec_all_nih);
148
149 const bool duplicate = (accept_pipe == INVALID_HANDLE_VALUE
150 && pipe_instance == 0
151 && GetLastError () == ERROR_ACCESS_DENIED);
152
153 if (accept_pipe != INVALID_HANDLE_VALUE)
154 InterlockedIncrement (&pipe_instance);
155
156 LeaveCriticalSection (&pipe_instance_lock);
157
158 if (duplicate)
159 {
160 *recoverable = false;
161 system_printf ("failed to create named pipe: "
162 "is the daemon already running?");
163 return NULL;
164 }
165
166 if (accept_pipe == INVALID_HANDLE_VALUE)
167 {
168 debug_printf ("error creating pipe (%lu).", GetLastError ());
169 *recoverable = true; // FIXME: case analysis?
170 return NULL;
171 }
172
173 assert (accept_pipe);
174
175 if (!ConnectNamedPipe (accept_pipe, NULL)
176 && GetLastError () != ERROR_PIPE_CONNECTED)
177 {
178 debug_printf ("error connecting to pipe (%lu)", GetLastError ());
179 (void) CloseHandle (accept_pipe);
180 *recoverable = true; // FIXME: case analysis?
181 return NULL;
182 }
183
184 return new transport_layer_pipes (accept_pipe);
185 }
186
187 #endif /* !__INSIDE_CYGWIN__ */
188
189 void
190 transport_layer_pipes::close ()
191 {
192 // verbose: debug_printf ("closing pipe %p", _hPipe);
193
194 if (_hPipe)
195 {
196 assert (_hPipe != INVALID_HANDLE_VALUE);
197
198 #ifndef __INSIDE_CYGWIN__
199
200 if (_is_accepted_endpoint)
201 {
202 (void) FlushFileBuffers (_hPipe); // Blocks until client reads.
203 (void) DisconnectNamedPipe (_hPipe);
204 EnterCriticalSection (&pipe_instance_lock);
205 (void) CloseHandle (_hPipe);
206 assert (pipe_instance > 0);
207 InterlockedDecrement (&pipe_instance);
208 LeaveCriticalSection (&pipe_instance_lock);
209 }
210 else
211 (void) CloseHandle (_hPipe);
212
213 #else /* __INSIDE_CYGWIN__ */
214
215 assert (!_is_accepted_endpoint);
216 (void) ForceCloseHandle (_hPipe);
217
218 #endif /* __INSIDE_CYGWIN__ */
219
220 _hPipe = NULL;
221 }
222 }
223
224 ssize_t
225 transport_layer_pipes::read (void *const buf, const size_t len)
226 {
227 // verbose: debug_printf ("reading from pipe %p", _hPipe);
228
229 assert (_hPipe);
230 assert (_hPipe != INVALID_HANDLE_VALUE);
231 assert (!_is_listening_endpoint);
232
233 DWORD count;
234 if (!ReadFile (_hPipe, buf, len, &count, NULL))
235 {
236 debug_printf ("error reading from pipe (%lu)", GetLastError ());
237 SET_ERRNO (EINVAL); // FIXME?
238 return -1;
239 }
240
241 return count;
242 }
243
244 ssize_t
245 transport_layer_pipes::write (void *const buf, const size_t len)
246 {
247 // verbose: debug_printf ("writing to pipe %p", _hPipe);
248
249 assert (_hPipe);
250 assert (_hPipe != INVALID_HANDLE_VALUE);
251 assert (!_is_listening_endpoint);
252
253 DWORD count;
254 if (!WriteFile (_hPipe, buf, len, &count, NULL))
255 {
256 debug_printf ("error writing to pipe, error = %lu", GetLastError ());
257 SET_ERRNO (EINVAL); // FIXME?
258 return -1;
259 }
260
261 return count;
262 }
263
264 /*
265 * This routine holds a static variable, assume_cygserver, that is set
266 * if the transport has good reason to think that cygserver is
267 * running, i.e. if if successfully connected to it with the previous
268 * attempt. If this is set, the code tries a lot harder to get a
269 * connection, making the assumption that any failures are just
270 * congestion and overloading problems.
271 */
272
273 int
274 transport_layer_pipes::connect ()
275 {
276 assert (!_hPipe);
277 assert (!_is_accepted_endpoint);
278 assert (!_is_listening_endpoint);
279
280 static bool assume_cygserver = false;
281
282 BOOL rc = TRUE;
283 int retries = 0;
284
285 debug_printf ("Try to connect to named pipe: %W", _pipe_name);
286 while (rc)
287 {
288 _hPipe = CreateFileW (_pipe_name,
289 GENERIC_READ | GENERIC_WRITE,
290 FILE_SHARE_READ | FILE_SHARE_WRITE,
291 &sec_all_nih,
292 OPEN_EXISTING,
293 SECURITY_IMPERSONATION,
294 NULL);
295
296 if (_hPipe != INVALID_HANDLE_VALUE)
297 {
298 assert (_hPipe);
299 #ifdef __INSIDE_CYGWIN__
300 ProtectHandle (_hPipe);
301 #endif
302 assume_cygserver = true;
303 return 0;
304 }
305
306 _hPipe = NULL;
307
308 if (!assume_cygserver && GetLastError () != ERROR_PIPE_BUSY)
309 {
310 debug_printf ("Error opening the pipe (%lu)", GetLastError ());
311 return -1;
312 }
313
314 /* Note: `If no instances of the specified named pipe exist, the
315 * WaitNamedPipe function returns immediately, regardless of the
316 * time-out value.' Thus the explicit Sleep if the call fails
317 * with ERROR_FILE_NOT_FOUND.
318 */
319 while (retries != MAX_WAIT_NAMED_PIPE_RETRY
320 && !(rc = WaitNamedPipeW (_pipe_name, WAIT_NAMED_PIPE_TIMEOUT)))
321 {
322 if (GetLastError () == ERROR_FILE_NOT_FOUND)
323 Sleep (0); // Give the server a chance.
324
325 retries += 1;
326 }
327 }
328
329 assert (retries == MAX_WAIT_NAMED_PIPE_RETRY);
330
331 system_printf ("lost connection to cygserver, error = %lu",
332 GetLastError ());
333
334 assume_cygserver = false;
335
336 return -1;
337 }
338
339 #ifndef __INSIDE_CYGWIN__
340
341 bool
342 transport_layer_pipes::impersonate_client ()
343 {
344 assert (_hPipe);
345 assert (_hPipe != INVALID_HANDLE_VALUE);
346 assert (_is_accepted_endpoint);
347
348 if (_hPipe && !ImpersonateNamedPipeClient (_hPipe))
349 {
350 debug_printf ("Failed to Impersonate client, (%lu)", GetLastError ());
351 return false;
352 }
353
354 return true;
355 }
356
357 bool
358 transport_layer_pipes::revert_to_self ()
359 {
360 assert (_is_accepted_endpoint);
361
362 if (!RevertToSelf ())
363 {
364 debug_printf ("Failed to RevertToSelf, (%lu)", GetLastError ());
365 return false;
366 }
367 return true;
368 }
369
370 #endif /* !__INSIDE_CYGWIN__ */
This page took 0.051728 seconds and 5 git commands to generate.