@@ -112,18 +112,19 @@ iocp_work(LPVOID lpParam) {
112
112
}
113
113
114
114
115
- static int
115
+ static void
116
116
process_connection (HANDLE pipe , int type ) {
117
117
struct agent_connection * con ;
118
118
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
+
123
122
memset (con , 0 , sizeof (struct agent_connection ));
124
123
con -> connection = pipe ;
125
124
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
+
127
128
agent_connection_on_io (con , 0 , & con -> ol );
128
129
return iocp_work (NULL );
129
130
}
@@ -153,16 +154,18 @@ agent_listen_loop() {
153
154
& listeners [i ].sa );
154
155
155
156
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 ());
157
158
SetEvent (event_stop_agent );
158
159
}
159
160
else if (ConnectNamedPipe (listeners [i ].pipe , & listeners [i ].ol ) != FALSE) {
160
- debug ("ConnectNamedPipe returned unexpectedly" );
161
+ verbose ("ConnectNamedPipe returned TRUE unexpectedly " );
161
162
SetEvent (event_stop_agent );
162
163
}
163
164
164
- if (GetLastError () == ERROR_PIPE_CONNECTED )
165
+ if (GetLastError () == ERROR_PIPE_CONNECTED ) {
166
+ debug ("Client has already connection to %d" , i );
165
167
SetEvent (listeners [i ].ol .hEvent );
168
+ }
166
169
167
170
if (GetLastError () != ERROR_IO_PENDING ) {
168
171
debug ("ConnectNamedPipe failed ERROR: %d" , GetLastError ());
@@ -183,7 +186,7 @@ agent_listen_loop() {
183
186
/* process incoming connection */
184
187
HANDLE con = listeners [r - 1 ].pipe ;
185
188
listeners [r - 1 ].pipe = INVALID_HANDLE_VALUE ;
186
-
189
+ verbose ( "client connected on %ls" , pipe_ids [ r - 1 ]);
187
190
if (debug_mode ) {
188
191
process_connection (con , listeners [r - 1 ].type );
189
192
agent_cleanup ();
@@ -198,25 +201,23 @@ agent_listen_loop() {
198
201
si .cb = sizeof (STARTUPINFOW );
199
202
memset (& si , 0 , sizeof (STARTUPINFOW ));
200
203
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,
203
206
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 ());
209
209
}
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 );
213
216
}
214
217
215
218
}
216
219
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 ());
220
221
}
221
222
222
223
}
@@ -231,33 +232,35 @@ void agent_cleanup_connection(struct agent_connection* con) {
231
232
}
232
233
233
234
void agent_shutdown () {
235
+ verbose ("shutdown" );
234
236
SetEvent (event_stop_agent );
235
237
}
236
238
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 ) {
238
241
int i , r ;
239
- HKEY agent_root ;
242
+ HKEY agent_root = NULL ;
240
243
DWORD process_id = GetCurrentProcessId ();
241
244
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 );
243
246
debug_mode = dbg_mode ;
244
247
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 ());
249
250
250
251
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 );
253
256
if ((event_stop_agent = CreateEvent (NULL , TRUE, FALSE, NULL )) == NULL )
254
- return GetLastError ();
257
+ fatal ( "cannot create global stop event ERROR:%d" , GetLastError () );
255
258
if ((r = init_listeners ()) != 0 )
256
- return r ;
259
+ fatal ( "failed to create server pipes ERROR:%d" , r ) ;
257
260
agent_listen_loop ();
258
261
}
259
- else {
260
- return process_connection (pipe , type );
262
+ else { /* this is a child process that processes one connection */
263
+ process_connection (pipe , type );
261
264
}
262
265
263
266
return 0 ;
0 commit comments