3 Copyright 2001, 2002, 2003, 2004, 2009, 2012 Red Hat Inc.
5 Written by Robert Collins <rbtcollins@hotmail.com>
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
13 /* to allow this to link into cygwin and the .dll, a little magic is needed. */
14 #ifdef __OUTSIDE_CYGWIN__
21 #include <sys/types.h>
28 #include <sys/cygwin.h>
31 #include "transport.h"
32 #include "transport_pipes.h"
34 #ifndef __INSIDE_CYGWIN__
35 #include "cygserver.h"
36 #include "cygserver_ipc.h"
41 #ifdef __INSIDE_CYGWIN__
42 #define SET_ERRNO(err) set_errno (err)
44 #define SET_ERRNO(err) errno = (err)
49 MAX_WAIT_NAMED_PIPE_RETRY
= 64,
50 WAIT_NAMED_PIPE_TIMEOUT
= 10 // milliseconds
53 #ifndef __INSIDE_CYGWIN__
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;
60 initialise_pipe_instance_lock ()
62 assert (pipe_instance
== 0);
63 InitializeCriticalSection (&pipe_instance_lock
);
66 #endif /* !__INSIDE_CYGWIN__ */
68 #ifndef __INSIDE_CYGWIN__
70 transport_layer_pipes::transport_layer_pipes (const HANDLE hPipe
)
72 _is_accepted_endpoint (true),
73 _is_listening_endpoint (false)
76 assert (_hPipe
!= INVALID_HANDLE_VALUE
);
77 _pipe_name
[0] = L
'\0';
80 #endif /* !__INSIDE_CYGWIN__ */
82 transport_layer_pipes::transport_layer_pipes ()
84 _is_accepted_endpoint (false),
85 _is_listening_endpoint (false)
87 wchar_t cyg_instkey
[18];
89 wchar_t *p
= wcpcpy (_pipe_name
, PIPE_NAME_PREFIX
);
90 if (!cygwin_internal (CW_GET_INSTKEY
, cyg_instkey
))
91 wcpcpy (wcpcpy (p
, cyg_instkey
), PIPE_NAME_SUFFIX
);
94 transport_layer_pipes::~transport_layer_pipes ()
99 #ifndef __INSIDE_CYGWIN__
102 transport_layer_pipes::listen ()
105 assert (!_is_accepted_endpoint
);
106 assert (!_is_listening_endpoint
);
108 _is_listening_endpoint
= true;
114 class transport_layer_pipes
*
115 transport_layer_pipes::accept (bool *const recoverable
)
118 assert (!_is_accepted_endpoint
);
119 assert (_is_listening_endpoint
);
121 pthread_once (&pipe_instance_lock_once
, &initialise_pipe_instance_lock
);
123 EnterCriticalSection (&pipe_instance_lock
);
125 // Read: http://www.securityinternals.com/research/papers/namedpipe.php
126 // See also the Microsoft security bulletins MS00-053 and MS01-031.
128 // FIXME: Remove FILE_CREATE_PIPE_INSTANCE.
130 const bool first_instance
= (pipe_instance
== 0);
132 debug ("Try to create named pipe: %ls", _pipe_name
);
134 const HANDLE accept_pipe
=
135 CreateNamedPipeW (_pipe_name
,
137 | (first_instance
? FILE_FLAG_FIRST_PIPE_INSTANCE
: 0)),
138 (PIPE_TYPE_BYTE
| PIPE_WAIT
),
139 PIPE_UNLIMITED_INSTANCES
,
143 const bool duplicate
= (accept_pipe
== INVALID_HANDLE_VALUE
144 && pipe_instance
== 0
145 && GetLastError () == ERROR_ACCESS_DENIED
);
147 if (accept_pipe
!= INVALID_HANDLE_VALUE
)
148 InterlockedIncrement (&pipe_instance
);
150 LeaveCriticalSection (&pipe_instance_lock
);
154 *recoverable
= false;
155 system_printf ("failed to create named pipe: "
156 "is the daemon already running?");
160 if (accept_pipe
== INVALID_HANDLE_VALUE
)
162 debug_printf ("error creating pipe (%lu).", GetLastError ());
163 *recoverable
= true; // FIXME: case analysis?
167 assert (accept_pipe
);
169 if (!ConnectNamedPipe (accept_pipe
, NULL
)
170 && GetLastError () != ERROR_PIPE_CONNECTED
)
172 debug_printf ("error connecting to pipe (%lu)", GetLastError ());
173 (void) CloseHandle (accept_pipe
);
174 *recoverable
= true; // FIXME: case analysis?
178 return new transport_layer_pipes (accept_pipe
);
181 #endif /* !__INSIDE_CYGWIN__ */
184 transport_layer_pipes::close ()
186 // verbose: debug_printf ("closing pipe %p", _hPipe);
190 assert (_hPipe
!= INVALID_HANDLE_VALUE
);
192 #ifndef __INSIDE_CYGWIN__
194 if (_is_accepted_endpoint
)
196 (void) FlushFileBuffers (_hPipe
); // Blocks until client reads.
197 (void) DisconnectNamedPipe (_hPipe
);
198 EnterCriticalSection (&pipe_instance_lock
);
199 (void) CloseHandle (_hPipe
);
200 assert (pipe_instance
> 0);
201 InterlockedDecrement (&pipe_instance
);
202 LeaveCriticalSection (&pipe_instance_lock
);
205 (void) CloseHandle (_hPipe
);
207 #else /* __INSIDE_CYGWIN__ */
209 assert (!_is_accepted_endpoint
);
210 (void) ForceCloseHandle (_hPipe
);
212 #endif /* __INSIDE_CYGWIN__ */
219 transport_layer_pipes::read (void *const buf
, const size_t len
)
221 // verbose: debug_printf ("reading from pipe %p", _hPipe);
224 assert (_hPipe
!= INVALID_HANDLE_VALUE
);
225 assert (!_is_listening_endpoint
);
228 if (!ReadFile (_hPipe
, buf
, len
, &count
, NULL
))
230 debug_printf ("error reading from pipe (%lu)", GetLastError ());
231 SET_ERRNO (EINVAL
); // FIXME?
239 transport_layer_pipes::write (void *const buf
, const size_t len
)
241 // verbose: debug_printf ("writing to pipe %p", _hPipe);
244 assert (_hPipe
!= INVALID_HANDLE_VALUE
);
245 assert (!_is_listening_endpoint
);
248 if (!WriteFile (_hPipe
, buf
, len
, &count
, NULL
))
250 debug_printf ("error writing to pipe, error = %lu", GetLastError ());
251 SET_ERRNO (EINVAL
); // FIXME?
259 * This routine holds a static variable, assume_cygserver, that is set
260 * if the transport has good reason to think that cygserver is
261 * running, i.e. if if successfully connected to it with the previous
262 * attempt. If this is set, the code tries a lot harder to get a
263 * connection, making the assumption that any failures are just
264 * congestion and overloading problems.
268 transport_layer_pipes::connect ()
271 assert (!_is_accepted_endpoint
);
272 assert (!_is_listening_endpoint
);
274 static bool assume_cygserver
= false;
279 debug_printf ("Try to connect to named pipe: %W", _pipe_name
);
282 _hPipe
= CreateFileW (_pipe_name
,
283 GENERIC_READ
| GENERIC_WRITE
,
284 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
287 SECURITY_IMPERSONATION
,
290 if (_hPipe
!= INVALID_HANDLE_VALUE
)
293 #ifdef __INSIDE_CYGWIN__
294 ProtectHandle (_hPipe
);
296 assume_cygserver
= true;
302 if (!assume_cygserver
&& GetLastError () != ERROR_PIPE_BUSY
)
304 debug_printf ("Error opening the pipe (%lu)", GetLastError ());
308 /* Note: `If no instances of the specified named pipe exist, the
309 * WaitNamedPipe function returns immediately, regardless of the
310 * time-out value.' Thus the explicit Sleep if the call fails
311 * with ERROR_FILE_NOT_FOUND.
313 while (retries
!= MAX_WAIT_NAMED_PIPE_RETRY
314 && !(rc
= WaitNamedPipeW (_pipe_name
, WAIT_NAMED_PIPE_TIMEOUT
)))
316 if (GetLastError () == ERROR_FILE_NOT_FOUND
)
317 Sleep (0); // Give the server a chance.
323 assert (retries
== MAX_WAIT_NAMED_PIPE_RETRY
);
325 system_printf ("lost connection to cygserver, error = %lu",
328 assume_cygserver
= false;
333 #ifndef __INSIDE_CYGWIN__
336 transport_layer_pipes::impersonate_client ()
339 assert (_hPipe
!= INVALID_HANDLE_VALUE
);
340 assert (_is_accepted_endpoint
);
342 if (_hPipe
&& !ImpersonateNamedPipeClient (_hPipe
))
344 debug_printf ("Failed to Impersonate client, (%lu)", GetLastError ());
352 transport_layer_pipes::revert_to_self ()
354 assert (_is_accepted_endpoint
);
356 if (!RevertToSelf ())
358 debug_printf ("Failed to RevertToSelf, (%lu)", GetLastError ());
364 #endif /* !__INSIDE_CYGWIN__ */