CVS: seamlessrdp/ServerExe main.c,1.28,1.29
Pierre Ossman <[email protected]> Wed, 22 Mar 2006 05:31:13 -0800
| Newsgroups | gmane.network.rdesktop.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/rdesktop/seamlessrdp/ServerExe
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29408/ServerExe
Modified Files:
main.c
Log Message:
Since the user can start new processes, it's insufficient to monitor just
the process we started ourselves. Instead, we get a list of processes running
when we start up (to be able to exclude system processes) and terminate the
session once no non-system processes are running.
Index: main.c
===================================================================
RCS file: /cvsroot/rdesktop/seamlessrdp/ServerExe/main.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** main.c 22 Mar 2006 11:56:46 -0000 1.28
--- main.c 22 Mar 2006 13:31:09 -0000 1.29
***************
*** 47,50 ****
--- 47,54 ----
static HWND g_hwnd;
+ static DWORD g_session_id;
+ static DWORD *g_startup_procs;
+ static int g_startup_num_procs;
+
typedef void (*set_hooks_proc_t) ();
typedef void (*remove_hooks_proc_t) ();
***************
*** 276,279 ****
--- 280,360 ----
}
+ static BOOL
+ build_startup_procs(void)
+ {
+ PWTS_PROCESS_INFO pinfo;
+ DWORD i, j, count;
+
+ if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pinfo, &count))
+ return FALSE;
+
+ g_startup_num_procs = 0;
+
+ for (i = 0; i < count; i++)
+ {
+ if (pinfo[i].SessionId != g_session_id)
+ continue;
+
+ g_startup_num_procs++;
+ }
+
+ g_startup_procs = malloc(sizeof(DWORD) * g_startup_num_procs);
+
+ j = 0;
+ for (i = 0; i < count; i++)
+ {
+ if (pinfo[i].SessionId != g_session_id)
+ continue;
+
+ g_startup_procs[j] = pinfo[i].ProcessId;
+ j++;
+ }
+
+ WTSFreeMemory(pinfo);
+
+ return TRUE;
+ }
+
+ static void
+ free_startup_procs(void)
+ {
+ free(g_startup_procs);
+
+ g_startup_procs = NULL;
+ g_startup_num_procs = 0;
+ }
+
+ static BOOL
+ should_terminate(void)
+ {
+ PWTS_PROCESS_INFO pinfo;
+ DWORD i, j, count;
+
+ if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pinfo, &count))
+ return TRUE;
+
+ for (i = 0; i < count; i++)
+ {
+ if (pinfo[i].SessionId != g_session_id)
+ continue;
+
+ for (j = 0; j < g_startup_num_procs; j++)
+ {
+ if (pinfo[i].ProcessId == g_startup_procs[j])
+ break;
+ }
+
+ if (j == g_startup_num_procs)
+ {
+ WTSFreeMemory(pinfo);
+ return FALSE;
+ }
+ }
+
+ WTSFreeMemory(pinfo);
+
+ return TRUE;
+ }
+
int WINAPI
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow)
***************
*** 327,330 ****
--- 408,415 ----
WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION);
+ ProcessIdToSessionId(GetCurrentProcessId(), &g_session_id);
+
+ build_startup_procs();
+
vchannel_open();
***************
*** 351,355 ****
{
BOOL result;
- DWORD exitcode;
PROCESS_INFORMATION proc_info;
STARTUPINFO startup_info;
--- 436,439 ----
***************
*** 361,369 ****
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0,
NULL, NULL, &startup_info, &proc_info);
if (result)
{
! do
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
--- 445,462 ----
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0,
NULL, NULL, &startup_info, &proc_info);
+ // Release handles
+ CloseHandle(proc_info.hProcess);
+ CloseHandle(proc_info.hThread);
if (result)
{
! int check_counter;
!
! check_counter = 5;
! while (check_counter-- || !should_terminate())
{
+ if (check_counter < 0)
+ check_counter = 5;
+
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
***************
*** 373,383 ****
process_cmds();
Sleep(100);
- GetExitCodeProcess(proc_info.hProcess, &exitcode);
}
- while (exitcode == STILL_ACTIVE);
-
- // Release handles
- CloseHandle(proc_info.hProcess);
- CloseHandle(proc_info.hThread);
}
else
--- 466,470 ----
***************
*** 399,402 ****
--- 486,491 ----
vchannel_close();
+ free_startup_procs();
+
return 1;
}
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642