source: trunk/dll/killproc.c@ 1063

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

Fortify ifdef reformat

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