282113ba89
throughout. * Makefile.in: Accomodate all new files and name changes. Add a *.d dependency. (sbindir): Add. (etcdir): Drop in favor of more appropriate sysconfdir definition. (sysconfdir): Add. (CXXFLAGS): Add -MMD flag. Add SYSCONFDIR definition. (.SUFFIXES): Add. (install): Add action items. (libclean): New target. (fullclean): Ditto. * bsd_helper.cc: New file. * bsd_helper.h: Ditto. * bsd_log.cc: Ditto. * bsd_log.h: Ditto. * bsd_mutex.cc: Ditto. * bsd_mutex.h: Ditto. * client.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. (client_request::handle_request): Add Message Queue and Semaphore handling. * cygserver.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. Use new debug/log/panic logging functions. (DEF_CONFIG_FILE): New definition for configuration file. Use throughout. (getfunc): Remove. (__cygserver__printf): Remove. (client_request_attach_tty::serve): Return error if impersonation fails. (print_usage): Pump up help message. (print_version): Add output of default configuration file. (main): Accommodate new options. Allow overwrite of threading options from config file. Call several new initialization functions. Drop printing dots. Don't define SIGHANDLE inline. * cygserver.conf: New file. * cygserver_process.h: Rename to process.h. * cygserver_transport.h: Rename to transport.h. * cygserver_transport_pipes.h: Rename to transport_pipes.h. * cygserver_transport_sockets.h: Rename to transport_sockets.h. * msg.cc: Rewrite. * sem.cc: Rewrite. * shm.cc: Rewrite. * sysv_msg.cc: New file, derived from FreeBSD version 1.52. * sysv_sem.cc: New file, derived from FreeBSD version 1.66. * sysv_shm.cc: New file, derived from FreeBSD version 1.89. * threaded_queue.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. * transport.cc (transport_layer_base::impersonate_client): Define bool. (transport_layer_base::revert_to_self): Ditto. * transport.h (transport_layer_base::impersonate_client): Declare bool. (transport_layer_base::revert_to_self): Ditto. * transport_pipes.cc (transport_layer_pipes::transport_layer_pipes): Don't call init_security. (init_security): Remove. (transport_layer_pipes::accept): Use global sec_all_nih. (transport_layer_pipes::connect): Ditto. (transport_layer_pipes::impersonate_client): Define bool. (transport_layer_pipes::revert_to_self): Ditt. * transport_pipes.h (transport_layer_pipes::impersonate_client): Declare bool. (transport_layer_pipes::revert_to_self): Ditto. * woutsup.h: Include bsd compatibility headers. (SIGHANDLE): Add definition. (__cygserver__printf): Remove definition. (__noop_printf): Ditto. (debug_printf): Define using debug. (syscall_printf): Define using log. (system_printf): Ditto. Drop all other _printf definitions.
350 lines
7.8 KiB
C++
350 lines
7.8 KiB
C++
/* transport_pipes.cc
|
|
|
|
Copyright 2001, 2002, 2003 Red Hat Inc.
|
|
|
|
Written by Robert Collins <rbtcollins@hotmail.com>
|
|
|
|
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. */
|
|
|
|
/* to allow this to link into cygwin and the .dll, a little magic is needed. */
|
|
#ifdef __OUTSIDE_CYGWIN__
|
|
#include "woutsup.h"
|
|
#else
|
|
#include "winsup.h"
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
|
#include <netdb.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
#include "cygerrno.h"
|
|
#include "transport.h"
|
|
#include "transport_pipes.h"
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
#include "cygserver.h"
|
|
#include "cygserver_ipc.h"
|
|
#else
|
|
#include "security.h"
|
|
#endif
|
|
|
|
enum
|
|
{
|
|
MAX_WAIT_NAMED_PIPE_RETRY = 64,
|
|
WAIT_NAMED_PIPE_TIMEOUT = 10 // milliseconds
|
|
};
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
|
|
static pthread_once_t pipe_instance_lock_once = PTHREAD_ONCE_INIT;
|
|
static CRITICAL_SECTION pipe_instance_lock;
|
|
static long pipe_instance = 0;
|
|
|
|
static void
|
|
initialise_pipe_instance_lock ()
|
|
{
|
|
assert (pipe_instance == 0);
|
|
InitializeCriticalSection (&pipe_instance_lock);
|
|
}
|
|
|
|
#endif /* !__INSIDE_CYGWIN__ */
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
|
|
transport_layer_pipes::transport_layer_pipes (const HANDLE hPipe)
|
|
: _pipe_name (""),
|
|
_hPipe (hPipe),
|
|
_is_accepted_endpoint (true),
|
|
_is_listening_endpoint (false)
|
|
{
|
|
assert (_hPipe);
|
|
assert (_hPipe != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
#endif /* !__INSIDE_CYGWIN__ */
|
|
|
|
transport_layer_pipes::transport_layer_pipes ()
|
|
: _pipe_name ("\\\\.\\pipe\\cygwin_lpc"),
|
|
_hPipe (NULL),
|
|
_is_accepted_endpoint (false),
|
|
_is_listening_endpoint (false)
|
|
{
|
|
}
|
|
|
|
transport_layer_pipes::~transport_layer_pipes ()
|
|
{
|
|
close ();
|
|
}
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
|
|
int
|
|
transport_layer_pipes::listen ()
|
|
{
|
|
assert (!_hPipe);
|
|
assert (!_is_accepted_endpoint);
|
|
assert (!_is_listening_endpoint);
|
|
|
|
_is_listening_endpoint = true;
|
|
|
|
/* no-op */
|
|
return 0;
|
|
}
|
|
|
|
class transport_layer_pipes *
|
|
transport_layer_pipes::accept (bool *const recoverable)
|
|
{
|
|
assert (!_hPipe);
|
|
assert (!_is_accepted_endpoint);
|
|
assert (_is_listening_endpoint);
|
|
|
|
pthread_once (&pipe_instance_lock_once, &initialise_pipe_instance_lock);
|
|
|
|
EnterCriticalSection (&pipe_instance_lock);
|
|
|
|
// Read: http://www.securityinternals.com/research/papers/namedpipe.php
|
|
// See also the Microsoft security bulletins MS00-053 and MS01-031.
|
|
|
|
// FIXME: Remove FILE_CREATE_PIPE_INSTANCE.
|
|
|
|
const bool first_instance = (pipe_instance == 0);
|
|
|
|
const HANDLE accept_pipe =
|
|
CreateNamedPipe (_pipe_name,
|
|
(PIPE_ACCESS_DUPLEX
|
|
| (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0)),
|
|
(PIPE_TYPE_BYTE | PIPE_WAIT),
|
|
PIPE_UNLIMITED_INSTANCES,
|
|
0, 0, 1000,
|
|
&sec_all_nih);
|
|
|
|
const bool duplicate = (accept_pipe == INVALID_HANDLE_VALUE
|
|
&& pipe_instance == 0
|
|
&& GetLastError () == ERROR_ACCESS_DENIED);
|
|
|
|
if (accept_pipe != INVALID_HANDLE_VALUE)
|
|
InterlockedIncrement (&pipe_instance);
|
|
|
|
LeaveCriticalSection (&pipe_instance_lock);
|
|
|
|
if (duplicate)
|
|
{
|
|
*recoverable = false;
|
|
system_printf ("failed to create named pipe: "
|
|
"is the daemon already running?");
|
|
return NULL;
|
|
}
|
|
|
|
if (accept_pipe == INVALID_HANDLE_VALUE)
|
|
{
|
|
debug_printf ("error creating pipe (%lu).", GetLastError ());
|
|
*recoverable = true; // FIXME: case analysis?
|
|
return NULL;
|
|
}
|
|
|
|
assert (accept_pipe);
|
|
|
|
if (!ConnectNamedPipe (accept_pipe, NULL)
|
|
&& GetLastError () != ERROR_PIPE_CONNECTED)
|
|
{
|
|
debug_printf ("error connecting to pipe (%lu)", GetLastError ());
|
|
(void) CloseHandle (accept_pipe);
|
|
*recoverable = true; // FIXME: case analysis?
|
|
return NULL;
|
|
}
|
|
|
|
return new transport_layer_pipes (accept_pipe);
|
|
}
|
|
|
|
#endif /* !__INSIDE_CYGWIN__ */
|
|
|
|
void
|
|
transport_layer_pipes::close ()
|
|
{
|
|
// verbose: debug_printf ("closing pipe %p", _hPipe);
|
|
|
|
if (_hPipe)
|
|
{
|
|
assert (_hPipe != INVALID_HANDLE_VALUE);
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
|
|
if (_is_accepted_endpoint)
|
|
{
|
|
(void) FlushFileBuffers (_hPipe); // Blocks until client reads.
|
|
(void) DisconnectNamedPipe (_hPipe);
|
|
EnterCriticalSection (&pipe_instance_lock);
|
|
(void) CloseHandle (_hPipe);
|
|
assert (pipe_instance > 0);
|
|
InterlockedDecrement (&pipe_instance);
|
|
LeaveCriticalSection (&pipe_instance_lock);
|
|
}
|
|
else
|
|
(void) CloseHandle (_hPipe);
|
|
|
|
#else /* __INSIDE_CYGWIN__ */
|
|
|
|
assert (!_is_accepted_endpoint);
|
|
(void) ForceCloseHandle (_hPipe);
|
|
|
|
#endif /* __INSIDE_CYGWIN__ */
|
|
|
|
_hPipe = NULL;
|
|
}
|
|
}
|
|
|
|
ssize_t
|
|
transport_layer_pipes::read (void *const buf, const size_t len)
|
|
{
|
|
// verbose: debug_printf ("reading from pipe %p", _hPipe);
|
|
|
|
assert (_hPipe);
|
|
assert (_hPipe != INVALID_HANDLE_VALUE);
|
|
assert (!_is_listening_endpoint);
|
|
|
|
DWORD count;
|
|
if (!ReadFile (_hPipe, buf, len, &count, NULL))
|
|
{
|
|
debug_printf ("error reading from pipe (%lu)", GetLastError ());
|
|
set_errno (EINVAL); // FIXME?
|
|
return -1;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
ssize_t
|
|
transport_layer_pipes::write (void *const buf, const size_t len)
|
|
{
|
|
// verbose: debug_printf ("writing to pipe %p", _hPipe);
|
|
|
|
assert (_hPipe);
|
|
assert (_hPipe != INVALID_HANDLE_VALUE);
|
|
assert (!_is_listening_endpoint);
|
|
|
|
DWORD count;
|
|
if (!WriteFile (_hPipe, buf, len, &count, NULL))
|
|
{
|
|
debug_printf ("error writing to pipe, error = %lu", GetLastError ());
|
|
set_errno (EINVAL); // FIXME?
|
|
return -1;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
/*
|
|
* This routine holds a static variable, assume_cygserver, that is set
|
|
* if the transport has good reason to think that cygserver is
|
|
* running, i.e. if if successfully connected to it with the previous
|
|
* attempt. If this is set, the code tries a lot harder to get a
|
|
* connection, making the assumption that any failures are just
|
|
* congestion and overloading problems.
|
|
*/
|
|
|
|
int
|
|
transport_layer_pipes::connect ()
|
|
{
|
|
assert (!_hPipe);
|
|
assert (!_is_accepted_endpoint);
|
|
assert (!_is_listening_endpoint);
|
|
|
|
static bool assume_cygserver = false;
|
|
|
|
BOOL rc = TRUE;
|
|
int retries = 0;
|
|
|
|
while (rc)
|
|
{
|
|
_hPipe = CreateFile (_pipe_name,
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
&sec_all_nih,
|
|
OPEN_EXISTING,
|
|
SECURITY_IMPERSONATION,
|
|
NULL);
|
|
|
|
if (_hPipe != INVALID_HANDLE_VALUE)
|
|
{
|
|
assert (_hPipe);
|
|
#ifdef __INSIDE_CYGWIN__
|
|
ProtectHandle (_hPipe);
|
|
#endif
|
|
assume_cygserver = true;
|
|
return 0;
|
|
}
|
|
|
|
_hPipe = NULL;
|
|
|
|
if (!assume_cygserver && GetLastError () != ERROR_PIPE_BUSY)
|
|
{
|
|
debug_printf ("Error opening the pipe (%lu)", GetLastError ());
|
|
return -1;
|
|
}
|
|
|
|
/* Note: `If no instances of the specified named pipe exist, the
|
|
* WaitNamedPipe function returns immediately, regardless of the
|
|
* time-out value.' Thus the explicit Sleep if the call fails
|
|
* with ERROR_FILE_NOT_FOUND.
|
|
*/
|
|
while (retries != MAX_WAIT_NAMED_PIPE_RETRY
|
|
&& !(rc = WaitNamedPipe (_pipe_name, WAIT_NAMED_PIPE_TIMEOUT)))
|
|
{
|
|
if (GetLastError () == ERROR_FILE_NOT_FOUND)
|
|
Sleep (0); // Give the server a chance.
|
|
|
|
retries += 1;
|
|
}
|
|
}
|
|
|
|
assert (retries == MAX_WAIT_NAMED_PIPE_RETRY);
|
|
|
|
system_printf ("lost connection to cygserver, error = %lu",
|
|
GetLastError ());
|
|
|
|
assume_cygserver = false;
|
|
|
|
return -1;
|
|
}
|
|
|
|
#ifndef __INSIDE_CYGWIN__
|
|
|
|
bool
|
|
transport_layer_pipes::impersonate_client ()
|
|
{
|
|
assert (_hPipe);
|
|
assert (_hPipe != INVALID_HANDLE_VALUE);
|
|
assert (_is_accepted_endpoint);
|
|
|
|
if (_hPipe && !ImpersonateNamedPipeClient (_hPipe))
|
|
{
|
|
debug_printf ("Failed to Impersonate client, (%lu)", GetLastError ());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
transport_layer_pipes::revert_to_self ()
|
|
{
|
|
assert (_is_accepted_endpoint);
|
|
|
|
if (!RevertToSelf ())
|
|
{
|
|
debug_printf ("Failed to RevertToSelf, (%lu)", GetLastError ());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#endif /* !__INSIDE_CYGWIN__ */
|