source: trunk/dll/killproc.c@ 771

Last change on this file since 771 was 771, checked in by Gregg Young, 18 years ago

Increase subject to 1024 reduce DosSleep times

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
RevLine 
[145]1
2/***********************************************************************
3
4 $Id: killproc.c 771 2007-08-06 03:05:35Z gyoung $
5
6 Kill a process
7
8 Copyright (c) 1993-98 M. Kimes
[350]9 Copyright (c) 2005, 2006 Steven H. Levine
[145]10
11 24 May 05 SHL Rework Win_Error usage
[350]12 14 Jul 06 SHL Use Runtime_Error
[404]13 29 Jul 06 SHL Use xfgets
[533]14 03 Nov 06 SHL Renames
15 03 Nov 06 SHL Count thread usage
[145]16
17***********************************************************************/
18
[2]19#define INCL_DOSERRORS
20#define INCL_DOS
21#define INCL_WIN
[350]22#include <os2.h>
[2]23
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <time.h>
28#include <ctype.h>
29#include <process.h>
30#include <limits.h>
[350]31
[2]32#include "procstat.h"
33#include "fm3dll.h"
34#include "fm3dlg.h"
35#include "fm3str.h"
36
37#pragma data_seg(DATA2)
38
[350]39static PSZ pszSrcFile = __FILE__;
[2]40
[533]41#pragma alloc_text(KILLPROC,FillKillListThread,FillKillListThread2,GetDosPgmName,KillDlgProc)
[2]42
[551]43CHAR *GetDosPgmName(PID pid, CHAR * string)
[350]44{
[2]45 HSWITCH hs;
46 SWCNTRL swctl;
[551]47 PCH pch;
[2]48
49 *string = 0;
[551]50 hs = WinQuerySwitchHandle(0, pid);
51 if (hs) {
52 WinQuerySwitchEntry(hs, &swctl);
[2]53 pch = swctl.szSwtitle;
[551]54 while (*pch) {
55 if (*pch < 0x10)
56 if (pch != swctl.szSwtitle && *(pch - 1) == 0x20)
57 memmove(pch, pch + 1, strlen(pch));
58 else {
59 *pch = 0x20;
60 pch++;
61 }
[2]62 else
[551]63 pch++;
[2]64 }
[551]65 strcpy(string, swctl.szSwtitle);
[2]66 }
[551]67 if (!*string)
68 strcpy(string, GetPString(IDS_UNKNOWNDOSPROCTEXT));
[2]69 return string;
70}
71
[551]72static VOID FillKillListThread2(VOID * arg)
[350]73{
[551]74 HWND hwnd = *(HWND *) arg;
75 CHAR s[1036];
76 HAB thab;
77 HMQ thmq;
78 INT rc;
79 PROCESSINFO *ppi;
80 BUFFHEADER *pbh;
81 MODINFO *pmi;
[2]82
83 thab = WinInitialize(0);
[551]84 thmq = WinCreateMsgQueue(thab, 0);
85 WinCancelShutdown(thmq, TRUE);
[533]86 IncrThreadUsage();
[2]87
[551]88 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
89 rc = DosAllocMem((PVOID) & pbh, USHRT_MAX + 4096,
90 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]91 if (rc)
[551]92 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
93 GetPString(IDS_OUTOFMEMORY));
[350]94 else {
[551]95 rc = DosQProcStatus(pbh, USHRT_MAX);
[350]96 if (!rc) {
[2]97 ppi = pbh->ppi;
[551]98 while (ppi->ulEndIndicator != PROCESS_END_INDICATOR) {
99 if (ppi->pid != mypid) {
100 pmi = pbh->pmi;
101 while (pmi && ppi->hModRef != pmi->hMod)
102 pmi = pmi->pNext;
103 if (pmi) {
104 sprintf(s, "%04x ", ppi->pid);
105 if (!stricmp(pmi->szModName, "SYSINIT"))
106 GetDosPgmName(ppi->pid, s + strlen(s));
107 else {
108 if (*pmi->szModName)
109 strcat(s, pmi->szModName);
110 else
111 strcat(s, GetPString(IDS_UNKNOWNPROCTEXT));
[350]112 }
[551]113 if (WinIsWindow(thab, hwnd)) {
114 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_INSERTITEM,
115 MPFROM2SHORT(LIT_SORTASCENDING, 0),
116 MPFROMP(s));
117 }
118 else
119 break;
120 }
121 }
122 ppi = (PPROCESSINFO) (ppi->ptiFirst + ppi->usThreadCount);
123 } // while
[2]124 }
125 DosFreeMem(pbh);
126 }
[350]127
[551]128 if (WinIsWindow(thab, hwnd))
129 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]130 WinDestroyMsgQueue(thmq);
[533]131 DecrThreadUsage();
[2]132 WinTerminate(thab);
133}
134
[551]135static VOID FillKillListThread(VOID * arg)
[350]136{
[551]137 HWND hwnd = *(HWND *) arg;
138 CHAR s[1036], progname[1027], *p;
139 HAB thab;
140 HMQ thmq;
[2]141 FILE *fp;
[551]142 BOOL foundstart = FALSE;
143 INT rc;
[2]144 CHAR *startstring = "Process and Thread Information";
145 CHAR *endstring = "System Semaphore Information";
[551]146 PID pid;
147 HFILE oldstdout, newstdout;
[2]148
149 DosError(FERR_DISABLEHARDERR);
150
151 thab = WinInitialize(0);
[551]152 thmq = WinCreateMsgQueue(thab, 0);
153 WinCancelShutdown(thmq, TRUE);
[533]154 IncrThreadUsage();
[2]155
[551]156 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
157 strcpy(s, "$PSTAT#$.#$#");
158 unlinkf("%s", s);
159 fp = fopen(s, "w");
160 if (!fp) {
161 Win_Error(NULLHANDLE, HWND_DESKTOP, __FILE__, __LINE__,
162 GetPString(IDS_REDIRECTERRORTEXT));
[350]163 goto Abort;
164 }
165 else {
[2]166 newstdout = -1;
[551]167 rc = DosDupHandle(fileno(stdout), &newstdout);
[350]168 if (rc)
[551]169 Dos_Error(MB_CANCEL, rc, hwnd, __FILE__, __LINE__, "DosDupHandle");
[2]170 oldstdout = fileno(stdout);
[551]171 DosDupHandle(fileno(fp), &oldstdout);
[2]172 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
[551]173 hwnd, NULL, NULL, "%s", "PSTAT.EXE /C");
[2]174 oldstdout = fileno(stdout);
[551]175 DosDupHandle(newstdout, &oldstdout);
[2]176 DosClose(newstdout);
177 fclose(fp);
[451]178 // fixme to be gone?
[551]179 if (rc == -1) {
[2]180 saymsg(MB_CANCEL,
[551]181 hwnd,
182 GetPString(IDS_ARGHTEXT), GetPString(IDS_CANTRUNPSTATTEXT));
[2]183 goto Abort;
184 }
185 }
[551]186 fp = fopen(s, "r");
[404]187 if (fp) {
188 while (!feof(fp)) {
[551]189 strset(s, 0);
190 if (!xfgets(s, 1025, fp, pszSrcFile, __LINE__))
191 break;
[404]192 if (!foundstart) {
[551]193 if (*s == ' ' && strstr(s, startstring))
194 foundstart = TRUE;
[2]195 }
196 else {
[551]197 if (*s == ' ' && strstr(s, endstring))
198 break;
199 if (*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
200 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
201 p = &s[1];
202 pid = strtol(&s[1], &p, 16);
203 if (pid && pid != mypid) {
204 strcpy(progname, &s[30]);
205 p = strchr(progname, ' ');
206 if (p)
207 *p = 0;
208 if (!stristr(progname, "\\PSTAT.EXE")) {
209 sprintf(s, "%04x %s", pid, progname);
210 WinSendDlgItemMsg(hwnd,
211 KILL_LISTBOX,
212 LM_INSERTITEM,
213 MPFROM2SHORT(LIT_SORTASCENDING, 0),
214 MPFROMP(s));
215 }
216 }
217 }
[2]218 }
219 }
220 fclose(fp);
221 }
222Abort:
223 DosForceDelete("$PSTAT#$.#$#");
[551]224 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]225 WinDestroyMsgQueue(thmq);
[533]226 DecrThreadUsage();
[2]227 WinTerminate(thab);
228}
229
[551]230MRESULT EXPENTRY KillDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[350]231{
[551]232 SHORT sSelect;
233 PID pid;
234 static BOOL listdone;
235 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]236
[551]237 switch (msg) {
238 case WM_INITDLG:
239 hptrIcon = WinLoadPointer(HWND_DESKTOP, FM3ModHandle, KILL_FRAME);
240 WinDefDlgProc(hwnd, WM_SETICON, MPFROMLONG(hptrIcon), MPVOID);
241 WinCheckButton(hwnd, KILL_CHECKBOX, fUseQProcStat);
242 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
243 break;
[2]244
[551]245 case UM_CONTAINER_FILLED:
246 listdone = TRUE;
247 if ((SHORT) WinSendDlgItemMsg(hwnd,
248 KILL_LISTBOX,
249 LM_QUERYITEMCOUNT, MPVOID, MPVOID) == 0) {
250 if (!fUseQProcStat)
251 saymsg(MB_CANCEL,
252 hwnd,
253 GetPString(IDS_ICHOKEDTEXT), GetPString(IDS_ISPSTATTHERETEXT));
254 else
255 saymsg(MB_CANCEL,
256 hwnd,
257 GetPString(IDS_ICHOKEDTEXT),
258 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
259 }
260 return 0;
[2]261
[551]262 case WM_CONTROL:
263 switch (SHORT1FROMMP(mp1)) {
264 case KILL_CHECKBOX:
265 fUseQProcStat = WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX);
266 PrfWriteProfileData(fmprof,
267 FM3Str,
268 "UseQProcStat", &fUseQProcStat, sizeof(BOOL));
269 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
270 break;
[2]271
[551]272 case KILL_LISTBOX:
273 switch (SHORT2FROMMP(mp2)) {
274 case LN_ENTER:
275 WinSendDlgItemMsg(hwnd, DID_OK, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
276 break;
[2]277 }
[551]278 break;
[2]279
[551]280 default:
[2]281 break;
[551]282 }
283 return 0;
[2]284
[551]285 case WM_ADJUSTWINDOWPOS:
286 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
287 break;
[2]288
[551]289 case UM_STRETCH:
290 {
291 SWP swpC, swp, swpH;
292
293 WinQueryWindowPos(hwnd, &swp);
294 if (!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
295 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), &swpC);
296 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_HDR), &swpH);
297 WinSetWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), HWND_TOP,
298 SysVal(SV_CXSIZEBORDER),
299 swpC.y,
300 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
301 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
302 SysVal(SV_CYSIZEBORDER)) -
303 (swpH.cy + 8)), SWP_MOVE | SWP_SIZE);
304 WinSetWindowPos(WinWindowFromID(hwnd, KILL_HDR), HWND_TOP,
305 SysVal(SV_CXSIZEBORDER) + 4,
306 swpC.y + ((swp.cy - swpC.y) -
307 (SysVal(SV_CYTITLEBAR) +
308 SysVal(SV_CYSIZEBORDER)) -
309 (swpH.cy + 4)), swpH.cx, swpH.cy, SWP_MOVE);
[2]310 }
[551]311 }
312 return 0;
[2]313
[551]314 case WM_COMMAND:
315 switch (SHORT1FROMMP(mp1)) {
316 case KILL_RESCAN:
317 listdone = FALSE;
318 if (fUseQProcStat) {
319 if (_beginthread(FillKillListThread2,
320 NULL, 65536, (PVOID) & hwnd) != -1)
[771]321 DosSleep(100);//05 Aug 07 GKY 250
[551]322 else
323 WinDismissDlg(hwnd, 0);
324 }
325 else {
326 if (_beginthread(FillKillListThread,
327 NULL, 65536, (PVOID) & hwnd) != -1)
[771]328 DosSleep(100); //05 Aug 07 GKY 250
[551]329 else
330 WinDismissDlg(hwnd, 0);
331 }
332 break;
[2]333
[551]334 case KILL_SHOW:
335 case DID_OK:
336 sSelect = (USHORT) WinSendDlgItemMsg(hwnd,
337 KILL_LISTBOX,
338 LM_QUERYSELECTION,
339 MPFROMSHORT(LIT_FIRST), MPVOID);
340 if (sSelect >= 0) {
[2]341
[551]342 CHAR s[31], *p;
343 APIRET error;
[2]344
[551]345 *s = 0;
346 WinSendDlgItemMsg(hwnd,
347 KILL_LISTBOX,
348 LM_QUERYITEMTEXT,
349 MPFROM2SHORT(sSelect, 30), MPFROMP(s));
350 if (*s) {
351 p = s;
352 pid = strtol(s, &p, 16);
353 if (pid) {
354 if (SHORT1FROMMP(mp1) == DID_OK) {
355 error = DosKillProcess(DKP_PROCESS, pid);
356 if (error && error != ERROR_INVALID_PROCID) {
357 Dos_Error(MB_CANCEL,
358 error,
359 hwnd,
360 __FILE__,
361 __LINE__, GetPString(IDS_DOSKILLFAILEDTEXT));
362 }
363 else
364 WinSendDlgItemMsg(hwnd,
365 KILL_LISTBOX,
366 LM_DELETEITEM,
367 MPFROM2SHORT(sSelect, 0), MPVOID);
368 }
369 else if (!ShowSession(hwnd, pid))
370 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
371 }
372 }
[2]373 }
[551]374 break;
[2]375
[551]376 case DID_CANCEL:
377 if (!listdone)
378 Runtime_Error(pszSrcFile, __LINE__, "busy");
379 else
380 WinDismissDlg(hwnd, 0);
[2]381 break;
382
[551]383 case IDM_HELP:
384 saymsg(MB_ENTER | MB_ICONASTERISK,
385 hwnd,
386 GetPString(IDS_KILLPROCHELPTITLETEXT),
387 GetPString(IDS_KILLPROCHELPTEXT));
[2]388 break;
[551]389 }
390 return 0;
391
392 case WM_CLOSE:
393 if (!listdone) {
394 Runtime_Error(pszSrcFile, __LINE__, "busy");
395 return 0;
396 }
397 break;
398
399 case WM_DESTROY:
400 if (hptrIcon)
401 WinDestroyPointer(hptrIcon);
402 hptrIcon = (HPOINTER) 0;
403 break;
[2]404 }
[551]405 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]406}
Note: See TracBrowser for help on using the repository browser.