@@ -112,18 +112,19 @@ iocp_work(LPVOID lpParam) {
112112}
113113
114114
115- static int
115+ static void
116116process_connection (HANDLE pipe , int type ) {
117117 struct agent_connection * con ;
118118
119- if ((con = malloc (sizeof (struct agent_connection ))) == NULL ) {
120- debug ("out of memory" );
121- return ERROR_OUTOFMEMORY ;
122- }
119+ if ((con = malloc (sizeof (struct agent_connection ))) == NULL )
120+ fatal ("failed to alloc" );
121+
123122 memset (con , 0 , sizeof (struct agent_connection ));
124123 con -> connection = pipe ;
125124 con -> type = type ;
126- CreateIoCompletionPort (pipe , ioc_port , (ULONG_PTR )con , 0 );
125+ if (CreateIoCompletionPort (pipe , ioc_port , (ULONG_PTR )con , 0 ) != ioc_port )
126+ fatal ("failed to assign pipe to ioc_port" );
127+
127128 agent_connection_on_io (con , 0 , & con -> ol );
128129 return iocp_work (NULL );
129130}
@@ -153,16 +154,18 @@ agent_listen_loop() {
153154 & listeners [i ].sa );
154155
155156 if (listeners [i ].pipe == INVALID_HANDLE_VALUE ) {
156- debug ("cannot create listener pipe ERROR:%d" , GetLastError ());
157+ verbose ("cannot create listener pipe ERROR:%d" , GetLastError ());
157158 SetEvent (event_stop_agent );
158159 }
159160 else if (ConnectNamedPipe (listeners [i ].pipe , & listeners [i ].ol ) != FALSE) {
160- debug ("ConnectNamedPipe returned unexpectedly" );
161+ verbose ("ConnectNamedPipe returned TRUE unexpectedly " );
161162 SetEvent (event_stop_agent );
162163 }
163164
164- if (GetLastError () == ERROR_PIPE_CONNECTED )
165+ if (GetLastError () == ERROR_PIPE_CONNECTED ) {
166+ debug ("Client has already connection to %d" , i );
165167 SetEvent (listeners [i ].ol .hEvent );
168+ }
166169
167170 if (GetLastError () != ERROR_IO_PENDING ) {
168171 debug ("ConnectNamedPipe failed ERROR: %d" , GetLastError ());
@@ -183,7 +186,7 @@ agent_listen_loop() {
183186 /* process incoming connection */
184187 HANDLE con = listeners [r - 1 ].pipe ;
185188 listeners [r - 1 ].pipe = INVALID_HANDLE_VALUE ;
186-
189+ verbose ( "client connected on %ls" , pipe_ids [ r - 1 ]);
187190 if (debug_mode ) {
188191 process_connection (con , listeners [r - 1 ].type );
189192 agent_cleanup ();
@@ -198,25 +201,23 @@ agent_listen_loop() {
198201 si .cb = sizeof (STARTUPINFOW );
199202 memset (& si , 0 , sizeof (STARTUPINFOW ));
200203 GetModuleFileNameW (NULL , module_path , MAX_PATH );
201- swprintf_s (path , MAX_PATH , L"%s %d %d" , module_path , con , listeners [r - 1 ].type );
202- if (CreateProcessW (NULL , path , NULL , NULL , TRUE,
204+ if (( swprintf_s (path , MAX_PATH , L"%s %d %d" , module_path , con , listeners [r - 1 ].type ) == -1 ) ||
205+ (CreateProcessW (NULL , path , NULL , NULL , TRUE,
203206 DETACHED_PROCESS , NULL , NULL ,
204- & si , & pi ) == FALSE) {
205- debug ("CreateProcess failure: %d" , GetLastError ());
206- CloseHandle (con );
207- agent_cleanup ();
208- return ;
207+ & si , & pi ) == FALSE)) {
208+ verbose ("Failed to create child process %ls ERROR:%d" , module_path , GetLastError ());
209209 }
210-
211- CloseHandle (con );
212-
210+ else {
211+ debug ("spawned child %d to process %d" , pi .dwProcessId , i );
212+ CloseHandle (pi .hProcess );
213+ CloseHandle (pi .hThread );
214+ }
215+ CloseHandle (con );
213216 }
214217
215218 }
216219 else {
217- debug ("wait on events ended with %d ERROR:%d" , r , GetLastError ());
218- agent_cleanup ();
219- return ;
220+ fatal ("wait on events ended with %d ERROR:%d" , r , GetLastError ());
220221 }
221222
222223 }
@@ -231,33 +232,35 @@ void agent_cleanup_connection(struct agent_connection* con) {
231232}
232233
233234void agent_shutdown () {
235+ verbose ("shutdown" );
234236 SetEvent (event_stop_agent );
235237}
236238
237- int agent_start (BOOL dbg_mode , BOOL child , HANDLE pipe , enum agent_type type ) {
239+ void
240+ agent_start (BOOL dbg_mode , BOOL child , HANDLE pipe , enum agent_type type ) {
238241 int i , r ;
239- HKEY agent_root ;
242+ HKEY agent_root = NULL ;
240243 DWORD process_id = GetCurrentProcessId ();
241244
242- debug ("agent_start pid:%d, dbg:%d, child:%d, pipe:%d" , process_id , dbg_mode , child , pipe );
245+ verbose ("agent_start pid:%d, dbg:%d, child:%d, pipe:%d" , process_id , dbg_mode , child , pipe );
243246 debug_mode = dbg_mode ;
244247
245- if ((ioc_port = CreateIoCompletionPort (INVALID_HANDLE_VALUE , NULL , (ULONG_PTR )NULL , 0 )) == NULL ) {
246- debug ("cannot create ioc port ERROR:%d" , GetLastError ());
247- return GetLastError ();
248- }
248+ if ((ioc_port = CreateIoCompletionPort (INVALID_HANDLE_VALUE , NULL , (ULONG_PTR )NULL , 0 )) == NULL )
249+ fatal ("cannot create ioc port ERROR:%d" , GetLastError ());
249250
250251 if (child == FALSE) {
251- RegCreateKeyExW (HKEY_LOCAL_MACHINE , SSH_AGENT_ROOT , 0 , 0 , 0 , KEY_WRITE , 0 , & agent_root , 0 );
252- RegSetValueExW (agent_root , L"ProcessID" , 0 , REG_DWORD , (BYTE * )& process_id , 4 );
252+ if ((r = RegCreateKeyExW (HKEY_LOCAL_MACHINE , SSH_AGENT_ROOT , 0 , 0 , 0 , KEY_WRITE , 0 , & agent_root , 0 )) != ERROR_SUCCESS )
253+ fatal ("cannot create agent root reg key, ERROR:%d" , r );
254+ if ((r = RegSetValueExW (agent_root , L"ProcessID" , 0 , REG_DWORD , (BYTE * )& process_id , 4 )) != ERROR_SUCCESS )
255+ fatal ("cannot publish agent master process id ERROR:%d" , r );
253256 if ((event_stop_agent = CreateEvent (NULL , TRUE, FALSE, NULL )) == NULL )
254- return GetLastError ();
257+ fatal ( "cannot create global stop event ERROR:%d" , GetLastError () );
255258 if ((r = init_listeners ()) != 0 )
256- return r ;
259+ fatal ( "failed to create server pipes ERROR:%d" , r ) ;
257260 agent_listen_loop ();
258261 }
259- else {
260- return process_connection (pipe , type );
262+ else { /* this is a child process that processes one connection */
263+ process_connection (pipe , type );
261264 }
262265
263266 return 0 ;
0 commit comments