source: trunk/dll/killproc.c@ 1036

Last change on this file since 1036 was 1036, checked in by Gregg Young, 17 years ago

More fortify changes eliminated the leave scope wrapper except in mainwnd.c Wrapper only unlaods stuuff (archivers, commands, etc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
RevLine 
[145]1
2/***********************************************************************
3
4 $Id: killproc.c 1036 2008-07-01 22:53:51Z 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
[775]16 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[793]17 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[828]18 02 Sep 07 GKY Replaced DosQProcStatus with DosQuerySysState to fix trap in thunk code
[829]19 02 Sep 07 SHL Expand FillKillListThread2 stack to avoid exception in __TNK
[145]20
21***********************************************************************/
22
[2]23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26#include <process.h>
27#include <limits.h>
[350]28
[907]29#define INCL_DOS
30#define INCL_DOSERRORS
31#define INCL_WIN
32#define INCL_LONGLONG // dircnrs.h
33
[2]34#include "fm3dlg.h"
35#include "fm3str.h"
[828]36#include "procstat.h"
[907]37#include "errutil.h" // Dos_Error...
38#include "strutil.h" // GetPString
39#include "fm3dll.h"
[1036]40#include "fortify.h"
[2]41
42#pragma data_seg(DATA2)
43
[350]44static PSZ pszSrcFile = __FILE__;
[2]45
[551]46CHAR *GetDosPgmName(PID pid, CHAR * string)
[350]47{
[2]48 HSWITCH hs;
49 SWCNTRL swctl;
[551]50 PCH pch;
[2]51
52 *string = 0;
[551]53 hs = WinQuerySwitchHandle(0, pid);
54 if (hs) {
55 WinQuerySwitchEntry(hs, &swctl);
[2]56 pch = swctl.szSwtitle;
[551]57 while (*pch) {
58 if (*pch < 0x10)
59 if (pch != swctl.szSwtitle && *(pch - 1) == 0x20)
60 memmove(pch, pch + 1, strlen(pch));
61 else {
62 *pch = 0x20;
63 pch++;
64 }
[2]65 else
[551]66 pch++;
[2]67 }
[551]68 strcpy(string, swctl.szSwtitle);
[2]69 }
[551]70 if (!*string)
71 strcpy(string, GetPString(IDS_UNKNOWNDOSPROCTEXT));
[2]72 return string;
73}
74
[551]75static VOID FillKillListThread2(VOID * arg)
[350]76{
[551]77 HWND hwnd = *(HWND *) arg;
78 CHAR s[1036];
79 HAB thab;
80 HMQ thmq;
81 INT rc;
82 PROCESSINFO *ppi;
83 BUFFHEADER *pbh;
84 MODINFO *pmi;
[2]85
86 thab = WinInitialize(0);
[551]87 thmq = WinCreateMsgQueue(thab, 0);
88 WinCancelShutdown(thmq, TRUE);
[533]89 IncrThreadUsage();
[2]90
[551]91 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
92 rc = DosAllocMem((PVOID) & pbh, USHRT_MAX + 4096,
93 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
[350]94 if (rc)
[551]95 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
96 GetPString(IDS_OUTOFMEMORY));
[350]97 else {
[829]98 rc = DosQProcStatus((ULONG *)pbh, USHRT_MAX);
[350]99 if (!rc) {
[2]100 ppi = pbh->ppi;
[551]101 while (ppi->ulEndIndicator != PROCESS_END_INDICATOR) {
102 if (ppi->pid != mypid) {
103 pmi = pbh->pmi;
104 while (pmi && ppi->hModRef != pmi->hMod)
105 pmi = pmi->pNext;
106 if (pmi) {
107 sprintf(s, "%04x ", ppi->pid);
108 if (!stricmp(pmi->szModName, "SYSINIT"))
109 GetDosPgmName(ppi->pid, s + strlen(s));
[830]110 else {
[551]111 if (*pmi->szModName)
[830]112 strcat(s, pmi->szModName);
[551]113 else
114 strcat(s, GetPString(IDS_UNKNOWNPROCTEXT));
[350]115 }
[551]116 if (WinIsWindow(thab, hwnd)) {
117 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_INSERTITEM,
118 MPFROM2SHORT(LIT_SORTASCENDING, 0),
119 MPFROMP(s));
120 }
121 else
122 break;
123 }
124 }
125 ppi = (PPROCESSINFO) (ppi->ptiFirst + ppi->usThreadCount);
126 } // while
[2]127 }
128 DosFreeMem(pbh);
129 }
[350]130
[551]131 if (WinIsWindow(thab, hwnd))
132 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]133 WinDestroyMsgQueue(thmq);
[533]134 DecrThreadUsage();
[2]135 WinTerminate(thab);
136}
137
[828]138static VOID FillKillListThread3(VOID * arg)
139{
140 HWND hwnd = *(HWND *) arg;
141 CHAR s[1036];
142 HAB thab;
143 HMQ thmq;
144 INT rc;
145 QSPREC *ppi;
146 QSPTRREC *pbh;
147 QSLREC *pmi;
148
149 thab = WinInitialize(0);
150 thmq = WinCreateMsgQueue(thab, 0);
151 WinCancelShutdown(thmq, TRUE);
152 IncrThreadUsage();
153
154 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
155 rc = DosAllocMem((PVOID) & pbh, USHRT_MAX + 4096,
156 PAG_COMMIT | OBJ_TILE | PAG_READ | PAG_WRITE);
157 if (rc)
158 Dos_Error(MB_CANCEL, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
159 GetPString(IDS_OUTOFMEMORY));
160 else {
161 rc = DosQuerySysState(QS_PROCESS | QS_MTE, 0, 0, 0, pbh, USHRT_MAX);
162 if (!rc) {
163 ppi = pbh->pProcRec;
164 while (ppi->RecType == 1) {
165 if (ppi->pid != mypid) {
166 pmi = pbh->pLibRec;
167 while (pmi && ppi->hMte != pmi->hmte)
168 pmi = pmi->pNextRec;
169 if (pmi) {
170 sprintf(s, "%04x ", ppi->pid);
171 if (!stricmp((CHAR *) pmi->pName, "SYSINIT"))
172 GetDosPgmName(ppi->pid, s + strlen(s));
[830]173 else {
[828]174 if (*pmi->pName)
[830]175 strcat(s, (CHAR *) pmi->pName);
[828]176 else
177 strcat(s, GetPString(IDS_UNKNOWNPROCTEXT));
178 }
179 if (WinIsWindow(thab, hwnd)) {
180 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_INSERTITEM,
181 MPFROM2SHORT(LIT_SORTASCENDING, 0),
182 MPFROMP(s));
183 }
184 else
185 break;
186 }
187 }
188 ppi = (QSPREC *) (ppi->pThrdRec + ppi->cTCB);
189 } // while
190 }
191 DosFreeMem(pbh);
192 }
193
194 if (WinIsWindow(thab, hwnd))
195 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
196 WinDestroyMsgQueue(thmq);
197 DecrThreadUsage();
198 WinTerminate(thab);
199}
200
[551]201static VOID FillKillListThread(VOID * arg)
[350]202{
[551]203 HWND hwnd = *(HWND *) arg;
204 CHAR s[1036], progname[1027], *p;
205 HAB thab;
206 HMQ thmq;
[2]207 FILE *fp;
[551]208 BOOL foundstart = FALSE;
209 INT rc;
[2]210 CHAR *startstring = "Process and Thread Information";
211 CHAR *endstring = "System Semaphore Information";
[551]212 PID pid;
213 HFILE oldstdout, newstdout;
[2]214
215 DosError(FERR_DISABLEHARDERR);
216
217 thab = WinInitialize(0);
[551]218 thmq = WinCreateMsgQueue(thab, 0);
219 WinCancelShutdown(thmq, TRUE);
[533]220 IncrThreadUsage();
[1036]221# ifdef FORTIFY
222 Fortify_EnterScope();
223# endif
[2]224
[551]225 WinSendDlgItemMsg(hwnd, KILL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
226 strcpy(s, "$PSTAT#$.#$#");
227 unlinkf("%s", s);
228 fp = fopen(s, "w");
229 if (!fp) {
230 Win_Error(NULLHANDLE, HWND_DESKTOP, __FILE__, __LINE__,
231 GetPString(IDS_REDIRECTERRORTEXT));
[350]232 goto Abort;
233 }
234 else {
[2]235 newstdout = -1;
[551]236 rc = DosDupHandle(fileno(stdout), &newstdout);
[350]237 if (rc)
[551]238 Dos_Error(MB_CANCEL, rc, hwnd, __FILE__, __LINE__, "DosDupHandle");
[2]239 oldstdout = fileno(stdout);
[551]240 DosDupHandle(fileno(fp), &oldstdout);
[2]241 rc = runemf2(SEPARATE | INVISIBLE | FULLSCREEN | BACKGROUND | WAIT,
[888]242 hwnd, pszSrcFile, __LINE__, NULL, NULL,
243 "%s", "PSTAT.EXE /C");
[2]244 oldstdout = fileno(stdout);
[551]245 DosDupHandle(newstdout, &oldstdout);
[2]246 DosClose(newstdout);
247 fclose(fp);
[451]248 // fixme to be gone?
[551]249 if (rc == -1) {
[2]250 saymsg(MB_CANCEL,
[551]251 hwnd,
252 GetPString(IDS_ARGHTEXT), GetPString(IDS_CANTRUNPSTATTEXT));
[2]253 goto Abort;
254 }
255 }
[551]256 fp = fopen(s, "r");
[404]257 if (fp) {
258 while (!feof(fp)) {
[551]259 strset(s, 0);
260 if (!xfgets(s, 1025, fp, pszSrcFile, __LINE__))
261 break;
[404]262 if (!foundstart) {
[551]263 if (*s == ' ' && strstr(s, startstring))
264 foundstart = TRUE;
[2]265 }
266 else {
[551]267 if (*s == ' ' && strstr(s, endstring))
268 break;
269 if (*s == ' ' && s[5] == ' ' && isxdigit(s[1]) &&
270 isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4])) {
271 p = &s[1];
272 pid = strtol(&s[1], &p, 16);
273 if (pid && pid != mypid) {
274 strcpy(progname, &s[30]);
275 p = strchr(progname, ' ');
276 if (p)
277 *p = 0;
278 if (!stristr(progname, "\\PSTAT.EXE")) {
279 sprintf(s, "%04x %s", pid, progname);
280 WinSendDlgItemMsg(hwnd,
281 KILL_LISTBOX,
282 LM_INSERTITEM,
283 MPFROM2SHORT(LIT_SORTASCENDING, 0),
284 MPFROMP(s));
285 }
286 }
287 }
[2]288 }
289 }
290 fclose(fp);
[1036]291# ifdef FORTIFY
292 Fortify_LeaveScope(pszSrcFile, __LINE__);
293# endif
[2]294 }
295Abort:
296 DosForceDelete("$PSTAT#$.#$#");
[551]297 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[2]298 WinDestroyMsgQueue(thmq);
[533]299 DecrThreadUsage();
[2]300 WinTerminate(thab);
301}
302
[551]303MRESULT EXPENTRY KillDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[350]304{
[551]305 SHORT sSelect;
306 PID pid;
307 static BOOL listdone;
308 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]309
[551]310 switch (msg) {
311 case WM_INITDLG:
312 hptrIcon = WinLoadPointer(HWND_DESKTOP, FM3ModHandle, KILL_FRAME);
313 WinDefDlgProc(hwnd, WM_SETICON, MPFROMLONG(hptrIcon), MPVOID);
314 WinCheckButton(hwnd, KILL_CHECKBOX, fUseQProcStat);
[828]315 WinCheckButton(hwnd, KILL2_CHECKBOX, fUseQSysState);
316 if (WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX)) {
317 WinCheckButton(hwnd, KILL_CHECKBOX, FALSE);
318 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), FALSE);
319 }
320 if (WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX)) {
321 WinCheckButton(hwnd, KILL2_CHECKBOX, FALSE);
322 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), FALSE);
323 }
[551]324 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
325 break;
[2]326
[551]327 case UM_CONTAINER_FILLED:
328 listdone = TRUE;
329 if ((SHORT) WinSendDlgItemMsg(hwnd,
330 KILL_LISTBOX,
331 LM_QUERYITEMCOUNT, MPVOID, MPVOID) == 0) {
332 if (!fUseQProcStat)
333 saymsg(MB_CANCEL,
334 hwnd,
335 GetPString(IDS_ICHOKEDTEXT), GetPString(IDS_ISPSTATTHERETEXT));
336 else
337 saymsg(MB_CANCEL,
338 hwnd,
339 GetPString(IDS_ICHOKEDTEXT),
340 GetPString(IDS_DOSQPROCSTATFAILEDTEXT));
341 }
342 return 0;
[2]343
[551]344 case WM_CONTROL:
345 switch (SHORT1FROMMP(mp1)) {
346 case KILL_CHECKBOX:
347 fUseQProcStat = WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX);
348 PrfWriteProfileData(fmprof,
349 FM3Str,
[828]350 "UseQProcStat", &fUseQProcStat, sizeof(BOOL));
[551]351 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
[828]352 if (WinQueryButtonCheckstate(hwnd, KILL_CHECKBOX)) {
353 WinCheckButton(hwnd, KILL2_CHECKBOX, FALSE);
354 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), FALSE);
355 }
356 else
357 WinEnableWindow(WinWindowFromID(hwnd, KILL2_CHECKBOX), TRUE);
[551]358 break;
[828]359 case KILL2_CHECKBOX:
360 fUseQSysState = WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX);
361 PrfWriteProfileData(fmprof,
362 FM3Str,
363 "UseQSysState", &fUseQSysState, sizeof(BOOL));
364 PostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(KILL_RESCAN, 0), MPVOID);
365 if (WinQueryButtonCheckstate(hwnd, KILL2_CHECKBOX)) {
366 WinCheckButton(hwnd, KILL_CHECKBOX, FALSE);
367 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), FALSE);
368 }
369 else
370 WinEnableWindow(WinWindowFromID(hwnd, KILL_CHECKBOX), TRUE);
371 break;
[2]372
[551]373 case KILL_LISTBOX:
374 switch (SHORT2FROMMP(mp2)) {
375 case LN_ENTER:
376 WinSendDlgItemMsg(hwnd, DID_OK, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
377 break;
[2]378 }
[551]379 break;
[2]380
[551]381 default:
[2]382 break;
[551]383 }
384 return 0;
[2]385
[551]386 case WM_ADJUSTWINDOWPOS:
387 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
388 break;
[2]389
[551]390 case UM_STRETCH:
391 {
392 SWP swpC, swp, swpH;
393
394 WinQueryWindowPos(hwnd, &swp);
395 if (!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
396 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), &swpC);
397 WinQueryWindowPos(WinWindowFromID(hwnd, KILL_HDR), &swpH);
398 WinSetWindowPos(WinWindowFromID(hwnd, KILL_LISTBOX), HWND_TOP,
399 SysVal(SV_CXSIZEBORDER),
400 swpC.y,
401 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
402 ((swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
403 SysVal(SV_CYSIZEBORDER)) -
404 (swpH.cy + 8)), SWP_MOVE | SWP_SIZE);
405 WinSetWindowPos(WinWindowFromID(hwnd, KILL_HDR), HWND_TOP,
406 SysVal(SV_CXSIZEBORDER) + 4,
407 swpC.y + ((swp.cy - swpC.y) -
408 (SysVal(SV_CYTITLEBAR) +
409 SysVal(SV_CYSIZEBORDER)) -
410 (swpH.cy + 4)), swpH.cx, swpH.cy, SWP_MOVE);
[2]411 }
[551]412 }
413 return 0;
[2]414
[551]415 case WM_COMMAND:
416 switch (SHORT1FROMMP(mp1)) {
417 case KILL_RESCAN:
418 listdone = FALSE;
419 if (fUseQProcStat) {
420 if (_beginthread(FillKillListThread2,
[829]421 NULL, 65536 + 8192, (PVOID)&hwnd) != -1)
422 DosSleep(100); // 05 Aug 07 GKY 250
[551]423 else
[828]424 WinDismissDlg(hwnd, 0);
[551]425 }
[828]426 else if (fUseQSysState)
427 if (_beginthread(FillKillListThread3,
428 NULL, 65536, (PVOID) & hwnd) != -1)
429 DosSleep(100);//05 Aug 07 GKY 250
430 else
431 WinDismissDlg(hwnd, 0);
[551]432 else {
433 if (_beginthread(FillKillListThread,
434 NULL, 65536, (PVOID) & hwnd) != -1)
[829]435 DosSleep(100); // 05 Aug 07 GKY 250
[551]436 else
437 WinDismissDlg(hwnd, 0);
438 }
439 break;
[2]440
[551]441 case KILL_SHOW:
442 case DID_OK:
443 sSelect = (USHORT) WinSendDlgItemMsg(hwnd,
444 KILL_LISTBOX,
445 LM_QUERYSELECTION,
446 MPFROMSHORT(LIT_FIRST), MPVOID);
447 if (sSelect >= 0) {
[2]448
[551]449 CHAR s[31], *p;
450 APIRET error;
[2]451
[551]452 *s = 0;
453 WinSendDlgItemMsg(hwnd,
454 KILL_LISTBOX,
455 LM_QUERYITEMTEXT,
456 MPFROM2SHORT(sSelect, 30), MPFROMP(s));
457 if (*s) {
458 p = s;
459 pid = strtol(s, &p, 16);
460 if (pid) {
461 if (SHORT1FROMMP(mp1) == DID_OK) {
462 error = DosKillProcess(DKP_PROCESS, pid);
463 if (error && error != ERROR_INVALID_PROCID) {
464 Dos_Error(MB_CANCEL,
465 error,
466 hwnd,
467 __FILE__,
468 __LINE__, GetPString(IDS_DOSKILLFAILEDTEXT));
469 }
470 else
471 WinSendDlgItemMsg(hwnd,
472 KILL_LISTBOX,
473 LM_DELETEITEM,
474 MPFROM2SHORT(sSelect, 0), MPVOID);
475 }
476 else if (!ShowSession(hwnd, pid))
477 Notify(GetPString(IDS_SORRYCANTSHOWTEXT));
478 }
479 }
[2]480 }
[551]481 break;
[2]482
[551]483 case DID_CANCEL:
484 if (!listdone)
485 Runtime_Error(pszSrcFile, __LINE__, "busy");
486 else
487 WinDismissDlg(hwnd, 0);
[2]488 break;
489
[551]490 case IDM_HELP:
491 saymsg(MB_ENTER | MB_ICONASTERISK,
492 hwnd,
493 GetPString(IDS_KILLPROCHELPTITLETEXT),
494 GetPString(IDS_KILLPROCHELPTEXT));
[2]495 break;
[551]496 }
497 return 0;
498
499 case WM_CLOSE:
500 if (!listdone) {
501 Runtime_Error(pszSrcFile, __LINE__, "busy");
502 return 0;
503 }
504 break;
505
506 case WM_DESTROY:
507 if (hptrIcon)
508 WinDestroyPointer(hptrIcon);
509 hptrIcon = (HPOINTER) 0;
510 break;
[2]511 }
[551]512 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]513}
[793]514
515#pragma alloc_text(KILLPROC,FillKillListThread,FillKillListThread2,GetDosPgmName,KillDlgProc)
[830]516#pragma alloc_text(KILLPROC,FillKillListThread3)
Note: See TracBrowser for help on using the repository browser.