1 /* cygserver_transport_pipes.cc
3 Copyright 2001 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
17 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include "cygwin/cygserver_transport.h"
22 #include "cygwin/cygserver_transport_pipes.h"
24 /* to allow this to link into cygwin and the .dll, a little magic is needed. */
25 #ifndef __OUTSIDE_CYGWIN__
29 #define debug_printf if (DEBUG) printf
32 transport_layer_pipes::transport_layer_pipes (HANDLE new_pipe)
39 transport_layer_pipes::transport_layer_pipes ()
42 strcpy(pipe_name, "\\\\.\\pipe\\cygwin_lpc");
49 transport_layer_pipes::init_security()
51 /* FIXME: pthread_once or equivalent needed */
52 InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
53 SetSecurityDescriptorDacl (&sd, TRUE, 0, FALSE);
55 sec_none_nih.nLength = sec_all_nih.nLength = sizeof (SECURITY_ATTRIBUTES);
56 sec_none_nih.bInheritHandle = sec_all_nih.bInheritHandle = FALSE;
57 sec_none_nih.lpSecurityDescriptor = NULL;
58 sec_all_nih.lpSecurityDescriptor = &sd;
63 transport_layer_pipes::listen ()
68 class transport_layer_pipes *
69 transport_layer_pipes::accept ()
73 debug_printf ("Already have a pipe in this %p\n",this);
77 pipe = CreateNamedPipe (pipe_name,
79 PIPE_TYPE_BYTE | PIPE_WAIT,
80 PIPE_UNLIMITED_INSTANCES,
83 if (pipe == INVALID_HANDLE_VALUE)
85 debug_printf ("error creating pipe (%lu)\n.", GetLastError ());
89 if ( !ConnectNamedPipe ( pipe, NULL ) &&
90 GetLastError () != ERROR_PIPE_CONNECTED)
92 printf ("error connecting to pipe (%lu)\n.", GetLastError ());
98 transport_layer_pipes *new_conn = new transport_layer_pipes (pipe);
105 transport_layer_pipes::close()
107 debug_printf ("closing pipe %p\n", pipe);
108 if (pipe && pipe != INVALID_HANDLE_VALUE)
110 FlushFileBuffers (pipe);
111 DisconnectNamedPipe (pipe);
117 transport_layer_pipes::read (char *buf, size_t len)
119 debug_printf ("reading from pipe %p\n", pipe);
120 if (!pipe || pipe == INVALID_HANDLE_VALUE)
124 DWORD rc = ReadFile (pipe, buf, len, &bytes_read, NULL);
127 debug_printf ("error reading from pipe (%lu)\n", GetLastError ());
134 transport_layer_pipes::write (char *buf, size_t len)
136 debug_printf ("writing to pipe %p\n", pipe);
137 DWORD bytes_written, rc;
138 if (!pipe || pipe == INVALID_HANDLE_VALUE)
141 rc = WriteFile (pipe, buf, len, &bytes_written, NULL);
144 debug_printf ("error writing to pipe (%lu)\n", GetLastError ());
147 return bytes_written;
151 transport_layer_pipes::connect ()
153 if (pipe && pipe != INVALID_HANDLE_VALUE)
155 debug_printf ("Already have a pipe in this %p\n",this);
161 pipe = CreateFile (pipe_name,
162 GENERIC_READ | GENERIC_WRITE,
163 FILE_SHARE_READ | FILE_SHARE_WRITE,
168 if (pipe != INVALID_HANDLE_VALUE)
172 if (GetLastError () != ERROR_PIPE_BUSY)
174 debug_printf ("Error opening the pipe (%lu)\n", GetLastError ());
178 if (!WaitNamedPipe (pipe_name, 20000))
179 debug_printf ( "error connecting to server pipe after 20 seconds (%lu)\n", GetLastError () );
180 /* We loop here, because the pipe exists but is busy. If it doesn't exist
181 * the != ERROR_PIPE_BUSY will catch it.
187 transport_layer_pipes::impersonate_client ()
189 debug_printf ("impersonating pipe %p\n", pipe);
190 if (pipe && pipe != INVALID_HANDLE_VALUE)
192 BOOL rv = ImpersonateNamedPipeClient (pipe);
194 debug_printf ("Failed to Impersonate the client, (%lu)\n", GetLastError ());
196 debug_printf("I am who you are\n");
200 transport_layer_pipes::revert_to_self ()
203 debug_printf("I am who I yam\n");